WIP: Custom Erroring system #1
@ -1,6 +1,6 @@
|
||||
toAbsolutePath(NB_CORE_SOURCE
|
||||
./src/Errors.cpp
|
||||
./src/Logger.cpp
|
||||
# ./src/Logger.cpp
|
||||
./src/Processes.cpp
|
||||
./src/Utils.cpp
|
||||
)
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#ifndef _NB_ERROR
|
||||
#define _NB_ERROR
|
||||
|
||||
@ -6,10 +7,11 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include "TypeTraits.hpp"
|
||||
#include <unordered_map>
|
||||
#include "Utils.hpp"
|
||||
|
||||
#ifndef THROW_WITH_INFO
|
||||
/* #ifndef THROW_WITH_INFO
|
||||
#ifdef CODE_ERROR_LOCATIONS
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
@ -23,30 +25,43 @@
|
||||
#else
|
||||
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
|
||||
#endif // LOGGING
|
||||
#endif // THROW
|
||||
#endif // THROW */
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
||||
|
||||
template <typename T>
|
||||
using has_type = decltype(T::type);
|
||||
|
||||
template<typename StringType, typename ErrorType>
|
||||
struct NBErrorWhatString;
|
||||
|
||||
class ErrorBase_what_str_impl : public std::exception {
|
||||
template <typename T>
|
||||
T what_str() const;
|
||||
|
||||
template <>
|
||||
virtual std::wstring what_str() const;
|
||||
|
||||
template<>
|
||||
virtual std::string what_str() const;
|
||||
};
|
||||
|
||||
template<class ErrorType>
|
||||
class ErrorBase : public std::exception {
|
||||
class ErrorBase : public ErrorBase_what_str_impl {
|
||||
public:
|
||||
ErrorBase(const ErrorBase&) = default;
|
||||
ErrorBase& operator=(const ErrorBase&) = delete;
|
||||
|
||||
virtual const char* what() const noexcept override final {
|
||||
std::string ret = this->msg;
|
||||
const std::string replace = nb::NEWLINE + "\t";
|
||||
if (trace) {
|
||||
ret += replace + find_and_replace(
|
||||
std::string(trace->what()),
|
||||
nb::NEWLINE,
|
||||
replace
|
||||
);
|
||||
template<typename StringType = std::string>
|
||||
inline const StringType what_str() const noexcept {
|
||||
return NBErrorWhatString<StringType,ErrorType>::what(*this);
|
||||
}
|
||||
const std::string& ret_ref = ret;
|
||||
return ret_ref.c_str();
|
||||
|
||||
virtual const char* what() const noexcept override final {
|
||||
const std::string& ret = what_str();
|
||||
return ret.c_str();
|
||||
}
|
||||
|
||||
static const std::string type;
|
||||
@ -54,7 +69,8 @@ public:
|
||||
|
||||
const unsigned int code;
|
||||
const std::string msg;
|
||||
const std::exception* trace;
|
||||
const std::shared_ptr<const std::exception> trace;
|
||||
const bool traceIsNBError;
|
||||
|
||||
protected:
|
||||
ErrorBase() = delete;
|
||||
@ -72,7 +88,8 @@ protected:
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
trace{static_cast<std::exception*>(trace_ ? new TraceType(*trace_) : nullptr) }
|
||||
trace{trace_ ? std::make_shared<TraceType>(*trace_) : nullptr},
|
||||
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
|
||||
{
|
||||
static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
|
||||
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
|
||||
@ -148,19 +165,58 @@ protected:
|
||||
filename_
|
||||
) {}
|
||||
|
||||
std::string (ErrorBase::*_trace_what_str)();
|
||||
std::wstring (ErrorBase::*_trace_what_wstr)();
|
||||
};
|
||||
template <typename ErrorType>
|
||||
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
||||
template <typename ErrorType>
|
||||
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
||||
|
||||
class DefaultError {DefaultError() = default;};
|
||||
template<typename ErrorType>
|
||||
struct NBErrorWhatString<std::wstring, ErrorType> {
|
||||
inline static const std::wstring what(const ErrorBase<ErrorType>& err) noexcept {
|
||||
std::wstring ret = nb::str_to_wstr(err.msg);
|
||||
const std::wstring replace = nb::WNEWLINE + L"┗-";
|
||||
if (err.trace) {
|
||||
std::wstring trace_msg;
|
||||
if (err.traceIsNBError) {
|
||||
trace_msg = std::static_pointer_cast<ErrorBase_what_str_impl>(err.trace)->template what_str<std::wstring>();
|
||||
} else {
|
||||
trace_msg = nb::str_to_wstr(std::string(err.trace->what()));
|
||||
}
|
||||
trace_msg = find_and_replace<std::wstring>(trace_msg, L"┗", L"-");
|
||||
ret += replace + find_and_replace<std::wstring>(
|
||||
trace_msg,
|
||||
nb::WNEWLINE,
|
||||
replace
|
||||
);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template <class ErrorType = DefaultError>
|
||||
template<typename ErrorType>
|
||||
struct NBErrorWhatString<std::string, ErrorType> {
|
||||
inline static const std::string what(const ErrorBase<ErrorType>& err) noexcept {
|
||||
std::string ret = err.msg;
|
||||
const std::string replace = nb::NEWLINE+" ";
|
||||
if (err.trace) {
|
||||
ret += replace + find_and_replace(
|
||||
std::string(err.trace->what()),
|
||||
nb::NEWLINE,
|
||||
replace
|
||||
);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template <class ErrorType = NoneType>
|
||||
class Error;
|
||||
|
||||
template<>
|
||||
class Error<DefaultError>;
|
||||
class Error<NoneType>;
|
||||
|
||||
template <class ErrorType>
|
||||
class Error : public ErrorBase<ErrorType> {
|
||||
@ -180,8 +236,8 @@ public:
|
||||
};
|
||||
|
||||
template<>
|
||||
class Error<DefaultError> : public ErrorBase<Error<DefaultError>> {
|
||||
using Base = ErrorBase<Error<DefaultError>>;
|
||||
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
||||
using Base = ErrorBase<Error<NoneType>>;
|
||||
public:
|
||||
using Base::Base;
|
||||
Error() : Base(0) {}
|
||||
|
||||
@ -6,7 +6,43 @@
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<std::size_t N=0, typename Func, typename... Pack>
|
||||
namespace detail {
|
||||
template <
|
||||
class Default,
|
||||
class AlwaysVoid,
|
||||
template <class...> class Op,
|
||||
class... Args
|
||||
>
|
||||
struct detector {
|
||||
using value = std::false_type;
|
||||
using type = Default;
|
||||
};
|
||||
|
||||
template <
|
||||
class Default,
|
||||
template <class...> class Op,
|
||||
class... Args
|
||||
>
|
||||
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
|
||||
using value = std::true_type;
|
||||
using type = Op<Args...>;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
struct NoneType{};
|
||||
|
||||
template <
|
||||
template <class...> class Op,
|
||||
class... Args
|
||||
>
|
||||
using is_detected = typename detail::detector<NoneType, void, Op, Args...>::value;
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args) {
|
||||
(s << ... << args);
|
||||
}
|
||||
|
||||
/*template<std::size_t N=0, typename Func, typename... Pack>
|
||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>&, Func) {}
|
||||
|
||||
@ -68,7 +104,7 @@ struct PackIsSameType<T, PackType> {
|
||||
template <typename T, typename FirstPack, typename... Pack>
|
||||
struct PackIsSameType<T, FirstPack, Pack...> {
|
||||
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
|
||||
};
|
||||
};*/
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
@ -4,40 +4,33 @@
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <array>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
const std::string NEWLINE;
|
||||
const std::wstring WNEWLINE;
|
||||
const std::string NEWLINE = "\r\n";
|
||||
const std::wstring WNEWLINE = L"\r\n";
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
const std::string NEWLINE;
|
||||
const std::wstring WNEWLINE;
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_convertible_v<T, std::string>, std::wstring> str_to_wstr(const T& str) {
|
||||
std::mbstate_t state = std::mbstate_t();
|
||||
const char* cstr = std::string(str).c_str();
|
||||
std::size_t wlen = 1 + std::mbsrtowcs(nullptr, &cstr, 0, &state);
|
||||
wchar_t* c_wstr= new wchar_t[wlen];
|
||||
std::mbsrtowcs(c_wstr, &cstr, wlen, &state);
|
||||
std::wstring ret(c_wstr, wlen);
|
||||
delete[] c_wstr;
|
||||
std::enable_if_t<std::is_convertible_v<const T&, std::string>, std::wstring> str_to_wstr(const T& in) {
|
||||
std::string str(in);
|
||||
std::wstring ret(str.begin(), str.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_convertible_v<T, std::wstring>, std::string> wstr_to_str(const T& wstr) {
|
||||
const wchar_t* c_wstr = std::wstring(wstr).c_str();
|
||||
std::size_t strlen = 1 + std::wcstombs(nullptr, c_wstr, 0);
|
||||
char* c_str= new char[strlen];
|
||||
std::wcstombs(c_str, c_wstr, strlen);
|
||||
std::string ret(c_str, strlen);
|
||||
std::enable_if_t<std::is_convertible_v<const T&, std::wstring>, std::string> wstr_to_str(const T& in) {
|
||||
std::wstring wstr(in);
|
||||
std::size_t wstrlen = wstr.length();
|
||||
char* c_str= new char[wstrlen];
|
||||
std::wcstombs(c_str, wstr.c_str(), wstrlen);
|
||||
std::string ret(c_str, wstrlen);
|
||||
delete[] c_str;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
#include "Errors.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
const std::string Error<DefaultError>::type = "nb::Error";
|
||||
const std::string Error<nb::NoneType>::type = "nb::Error";
|
||||
|
||||
const ErrorCodeMap Error<DefaultError>::ErrorMessages = {
|
||||
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
|
||||
{Error::Codes::GENERAL, "General std::exception."},
|
||||
{Error::Codes::UNDEFINED, "Undefined / general error."},
|
||||
{Error::Codes::BADERRORCODE, "Unrecognized error code."}
|
||||
|
||||
@ -1,14 +1,8 @@
|
||||
#include <corecrt.h>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
const std::string NEWLINE("\r\n");
|
||||
const std::wstring WNEWLINE(L"\r\n");
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
const std::string NEWLINE("\n");
|
||||
const std::wstring WNEWLINE(L"\n");
|
||||
#endif // _NB_TARGET_LINUX
|
||||
template<>
|
||||
std::wstring nb::str_to_wstr(const std::string& in) {
|
||||
std::wstring ret(in.begin(), in.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
using namespace nb;
|
||||
|
||||
@ -33,5 +36,12 @@ const ErrorCodeMap TestError::ErrorMessages{
|
||||
|
||||
|
||||
TEST(ErrorTest, Test) {
|
||||
std::cout << TestError(0, TestError(1, TestError(2))).what();
|
||||
auto err = TestError(0, TestError(1));
|
||||
nb::stream(std::cout,
|
||||
"len string: ", err.what_str().length(), "\n",
|
||||
"len wstring: ", err.what_str<std::wstring>().length(), "\n"
|
||||
);
|
||||
nb::stream(std::wcout,
|
||||
err.what_str<std::wstring>(), L"\n"
|
||||
);
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include "TypeTraits.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace nb {
|
||||
@ -112,5 +113,37 @@ TEST(UtilsTest, TestStrToWstr) {
|
||||
);
|
||||
}
|
||||
|
||||
struct A { bool x(); };
|
||||
struct B { bool y(); };
|
||||
struct C { bool x(); bool y; };
|
||||
struct D : C {};
|
||||
|
||||
template <typename T>
|
||||
using has_x = decltype(T::x);
|
||||
|
||||
template <typename T>
|
||||
using has_y = decltype(T::y);
|
||||
|
||||
TEST(UtilsTest, TestIsDetected) {
|
||||
auto ret = is_detected<has_x, A>::value;
|
||||
ASSERT_TRUE(ret);
|
||||
ret = is_detected<has_y, A>::value;
|
||||
ASSERT_FALSE(ret);
|
||||
|
||||
ret = is_detected<has_y, B>::value;
|
||||
ASSERT_TRUE(ret);
|
||||
ret = is_detected<has_x, B>::value;
|
||||
ASSERT_FALSE(ret);
|
||||
|
||||
ret = is_detected<has_x, C>::value;
|
||||
ASSERT_TRUE(ret);
|
||||
ret = is_detected<has_y, C>::value;
|
||||
ASSERT_TRUE(ret);
|
||||
|
||||
ret = is_detected<has_x, D>::value;
|
||||
ASSERT_TRUE(ret);
|
||||
ret = is_detected<has_y, D>::value;
|
||||
ASSERT_TRUE(ret);
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
Loading…
x
Reference in New Issue
Block a user