nb::Error objects now working
This commit is contained in:
parent
18de24649a
commit
0b5189dd2c
@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#ifndef _NB_ERROR
|
||||
#define _NB_ERROR
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include "TypeTraits.hpp"
|
||||
#include <unordered_map>
|
||||
@ -34,41 +34,86 @@ 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;
|
||||
template<class ErrorType = NoneType>
|
||||
class ErrorBase;
|
||||
|
||||
class ErrorBase_what_str_impl : public std::exception {
|
||||
template<>
|
||||
class ErrorBase<NoneType> : public std::exception {
|
||||
public:
|
||||
virtual std::string what_str_impl() const = 0;
|
||||
virtual std::wstring what_wstr_impl() const = 0;
|
||||
};
|
||||
|
||||
template<class ErrorType>
|
||||
class ErrorBase : public ErrorBase_what_str_impl {
|
||||
public:
|
||||
ErrorBase(const ErrorBase&) = default;
|
||||
ErrorBase& operator=(const ErrorBase&) = delete;
|
||||
|
||||
template<typename StringType = std::string>
|
||||
inline const StringType what_str() const noexcept {
|
||||
return NBErrorWhatString<StringType,ErrorType>::what(*this);
|
||||
}
|
||||
|
||||
virtual const char* what() const noexcept override final {
|
||||
const std::string& ret = what_str();
|
||||
return ret.c_str();
|
||||
}
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
|
||||
const unsigned int code;
|
||||
const std::string msg;
|
||||
const std::shared_ptr<const std::exception> trace;
|
||||
const bool traceIsNBError;
|
||||
|
||||
virtual inline std::string str() const noexcept = 0;
|
||||
|
||||
virtual const char* what() const noexcept override final {
|
||||
const std::string& ret = str();
|
||||
return ret.c_str();
|
||||
}
|
||||
|
||||
protected:
|
||||
ErrorBase() = delete;
|
||||
template <
|
||||
typename TraceType = std::exception,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
ErrorBase(
|
||||
const unsigned int& code_,
|
||||
const std::string& msg_,
|
||||
const std::shared_ptr<TraceType> trace_,
|
||||
const unsigned int& line_,
|
||||
std::string filename_
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
trace{std::dynamic_pointer_cast<std::exception>(trace_)},
|
||||
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
|
||||
{
|
||||
static_assert(std::is_base_of<std::exception, TraceType>::value,
|
||||
"`trace_` must be a pointer to a child object of std::exception."
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template <class ErrorType>
|
||||
class ErrorBase : public ErrorBase<NoneType> {
|
||||
using Base = ErrorBase<NoneType>;
|
||||
public:
|
||||
ErrorBase(const ErrorBase&) = default;
|
||||
ErrorBase& operator=(const ErrorBase&) = delete;
|
||||
|
||||
virtual inline std::string str() const noexcept override {
|
||||
std::string ret = msg;
|
||||
if (trace) {
|
||||
const std::string replace = nb::NEWLINE+" ";
|
||||
std::string trace_msg;
|
||||
if (traceIsNBError) {
|
||||
trace_msg = std::static_pointer_cast<const ErrorBase>(trace)->str();
|
||||
trace_msg = std::string(std::string_view(trace_msg).substr(
|
||||
0, trace_msg.length()-nb::NEWLINE.length()
|
||||
));
|
||||
} else {
|
||||
trace_msg = std::string(trace->what());
|
||||
}
|
||||
ret += replace + "Trace: " + find_and_replace<std::string>(
|
||||
trace_msg,
|
||||
nb::NEWLINE,
|
||||
replace
|
||||
);
|
||||
}
|
||||
return ret + nb::NEWLINE;
|
||||
}
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
using Base::traceIsNBError;
|
||||
|
||||
protected:
|
||||
using Base::Base;
|
||||
|
||||
template <
|
||||
typename TraceType = std::exception,
|
||||
@ -80,12 +125,13 @@ protected:
|
||||
const TraceType* trace_=nullptr,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
trace{trace_ ? std::make_shared<TraceType>(*trace_) : nullptr},
|
||||
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
|
||||
{
|
||||
) : Base(
|
||||
code_,
|
||||
msg_,
|
||||
trace_ ? std::make_shared<TraceType>(*trace_) : nullptr,
|
||||
line_,
|
||||
filename_
|
||||
) {
|
||||
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 "
|
||||
"a class member."
|
||||
@ -97,9 +143,6 @@ protected:
|
||||
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
||||
"const std::string type must be a class member."
|
||||
);
|
||||
static_assert(std::is_base_of<std::exception, TraceType>::value,
|
||||
"`trace_` must be a pointer to a child object of std::exception."
|
||||
);
|
||||
}
|
||||
|
||||
template <
|
||||
@ -159,55 +202,12 @@ protected:
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
|
||||
std::string what_str_impl() const override final { return what_str(); }
|
||||
std::wstring what_wstr_impl() const override final { return what_str<std::wstring>(); }
|
||||
};
|
||||
template <typename ErrorType>
|
||||
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
||||
template <typename ErrorType>
|
||||
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
||||
|
||||
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) {
|
||||
// How can I access
|
||||
trace_msg = std::static_pointer_cast<const ErrorBase_what_str_impl>(err.trace)->what_wstr_impl();
|
||||
} 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<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;
|
||||
|
||||
@ -221,6 +221,7 @@ public:
|
||||
template <typename... Args>
|
||||
Error(Args... T) : Base(T...) {}
|
||||
|
||||
using Base::str;
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
@ -242,6 +243,7 @@ public:
|
||||
GENERAL, UNDEFINED, BADERRORCODE
|
||||
};
|
||||
|
||||
using Base::str;
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
|
||||
@ -37,11 +37,6 @@ template <
|
||||
>
|
||||
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) {}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
#pragma once
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
const std::string NEWLINE = "\r\n";
|
||||
const std::wstring WNEWLINE = L"\r\n";
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
const std::string NEWLINE = "\n";
|
||||
@ -18,14 +18,14 @@ namespace nb {
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_convertible_v<const T&, std::string>, std::wstring> str_to_wstr(const T& in) {
|
||||
std::enable_if_t<std::is_convertible_v<T, std::string>, std::wstring> str_to_wstr(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<const T&, std::wstring>, std::string> wstr_to_str(const T& in) {
|
||||
std::enable_if_t<std::is_convertible_v<T, std::wstring>, std::string> wstr_to_str(T in) {
|
||||
std::wstring wstr(in);
|
||||
std::size_t wstrlen = wstr.length();
|
||||
char* c_str= new char[wstrlen];
|
||||
@ -35,11 +35,25 @@ std::enable_if_t<std::is_convertible_v<const T&, std::wstring>, std::string> wst
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args) {
|
||||
(s << ... << args);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
||||
|
||||
template<typename... Args>
|
||||
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
||||
|
||||
template <typename T = std::string, typename A, typename B, typename C>
|
||||
T find_and_replace(
|
||||
const A& original,
|
||||
const B& find,
|
||||
const C& replace
|
||||
A original,
|
||||
B find,
|
||||
C replace
|
||||
) {
|
||||
const T& original_t = T(original);
|
||||
const T& find_t = T(find);
|
||||
@ -50,10 +64,8 @@ T find_and_replace(
|
||||
std::size_t find_len = find_t.length();
|
||||
std::size_t replace_len = replace_t.length();
|
||||
std::size_t currpos = 0;
|
||||
std::size_t lastpos = 0;
|
||||
while(true) {
|
||||
lastpos = currpos;
|
||||
currpos = original_t.find(find_t, lastpos);
|
||||
currpos = ret.find(find_t, currpos);
|
||||
if (currpos == T::npos) {
|
||||
break;
|
||||
}
|
||||
|
||||
@ -3,9 +3,8 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "TypeTraits.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
using namespace nb;
|
||||
|
||||
@ -36,12 +35,6 @@ const ErrorCodeMap TestError::ErrorMessages{
|
||||
|
||||
|
||||
TEST(ErrorTest, Test) {
|
||||
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"
|
||||
);
|
||||
auto err = TestError(0, TestError(1, TestError(2, TestError(3, TestError(2)))));
|
||||
ASSERT_STREQ(err.what(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You\n");
|
||||
}
|
||||
@ -1,4 +1,3 @@
|
||||
#include <iterator>
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user