NBEngine/engine/NBCore/Errors.hpp

280 lines
6.8 KiB
C++

#pragma once
#ifndef _NB_ERROR
#define _NB_ERROR
#include <exception>
#include <memory>
#include <string>
#include <type_traits>
#include "TypeTraits.hpp"
#include <unordered_map>
#include "StringUtils.hpp"
namespace nb {
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
template<typename T>
constexpr bool IsValidException_v = std::is_base_of_v<std::exception, T>;
template <typename T>
using IsValidException = std::enable_if_t<IsValidException_v<T>, bool>;
template<class ErrorType=NoneType>
class ErrorBase;
template <class ErrorType=NoneType>
class Error;
template<typename T>
constexpr bool IsValidNBError_v = std::is_base_of_v<ErrorBase<NoneType>, T>;
template <typename T>
using IsValidNBError = std::enable_if_t<IsValidNBError_v<T>, bool>;
template<>
class ErrorBase<NoneType> {
public:
const unsigned int code;
const std::string msg;
const std::string type;
const std::shared_ptr<const void> trace;
const bool traceIsNBError;
virtual std::string what() const noexcept = 0;
virtual std::shared_ptr<ErrorBase> make_shared() const noexcept = 0;
protected:
template<typename T=NoneType>
ErrorBase(const ErrorBase<T>& err) : ErrorBase(std::move(err)) {}
template<typename T=NoneType>
ErrorBase(ErrorBase<T>&& err) : ErrorBase(
err.code,
err.msg,
err.type,
(err.traceIsNBError)
? static_cast<const ErrorBase*>(err.trace.get())
: static_cast<const std::exception*>(err.trace.get())
) {}
ErrorBase(
unsigned int code_,
std::string msg_,
std::string type_,
const std::exception* trace_
) noexcept :
code(code_),
msg(msg_),
type(type_),
trace{ trace_ ?
std::static_pointer_cast<const void>(
std::make_shared<Error<NoneType>>(*trace_))
: nullptr,
},
traceIsNBError(false)
{}
template<typename T, std::enable_if_t<IsValidNBError_v<T>, bool> = true>
ErrorBase(
unsigned int code_,
std::string msg_,
std::string type_,
const T* trace_
) noexcept :
code(code_),
msg(msg_),
type(type_),
trace{trace_ ? trace_->make_shared() : nullptr},
traceIsNBError(true)
{}
};
template <class ErrorType>
class ErrorBase : public ErrorBase<NoneType> {
private:
using Base = ErrorBase<NoneType>;
void inline check_asserts() {
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."
);
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
"enum Codes must be of underlying type unsigned int."
);
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
"const std::string type must be a class member."
);
}
public:
template <typename T>
ErrorBase(const ErrorBase<T>& cpy) : Base(cpy) { check_asserts(); }
virtual std::shared_ptr<Base> make_shared() const noexcept override {
return std::static_pointer_cast<Base>(
std::make_shared<ErrorBase>(*this)
);
}
virtual std::string what() const noexcept override {
std::string ret = msg;
if (trace) {
std::string trace_msg;
if (traceIsNBError) {
trace_msg = std::static_pointer_cast<const Base>(trace)->what();
} else {
trace_msg = std::string(std::static_pointer_cast<const Base>(trace)->what());
}
ret += nb::NEWLINE + indent_strblock(
trace_msg,
nb::TABOVER,
nb::TABOVER+"Trace: "
);
}
return ret;
}
static const std::string type;
static const ErrorCodeMap ErrorMessages;
using Base::code;
using Base::msg;
using Base::trace;
using Base::traceIsNBError;
friend ErrorType;
friend Error<ErrorType>;
protected:
using Base::Base;
template <typename T>
ErrorBase(
unsigned int code_,
std::string msg_,
std::string type_,
const T* trace_
) : Base(
code_,
msg_,
type_,
trace_
) { check_asserts(); }
};
template <typename ErrorType>
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
template <typename ErrorType>
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
template <class ErrorType>
class Error : public ErrorBase<ErrorType> {
using Base = ErrorBase<ErrorType>;
public:
template <typename T>
Error(
unsigned int code_,
const T& trace_
) noexcept : Base(
code_,
ErrorType::ErrorMessages.at(code_),
ErrorType::type,
&trace_
) {}
template<typename ET>
Error(
std::string msg_,
const ErrorBase<ET>& trace_
) noexcept : Base(
0,
msg_,
ErrorType::type,
&trace_
) {}
Error(
std::string msg_,
const std::exception& trace_
) noexcept : Base(
0,
msg_,
ErrorType::type,
&trace_
) {}
Error(unsigned int code_) noexcept : Base(
code_,
ErrorType::ErrorMessages.at(code_),
ErrorType::type,
NULLPTR<std::exception>
) {}
Error(std::string msg_) noexcept : Base(
0,
msg_,
ErrorType::type,
NULLPTR<std::exception>
) {}
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
using Base::traceIsNBError;
using Base::type;
using Base::ErrorMessages;
protected:
using Base::Base;
};
template<>
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
using Base = ErrorBase<Error<NoneType>>;
public:
using Base::Base;
Error(unsigned int code_=1) noexcept : Base(
code_,
ErrorMessages.at(code_),
type,
NULLPTR<std::exception>
) {}
Error(const std::exception& err) : Base(
Codes::STANDARD,
std::string(err.what()),
"std::exception",
nullptr
) {}
Error(std::string msg_) noexcept : Base(
Codes::UNDEFINED,
msg_,
type,
NULLPTR<std::exception>
) {}
enum Codes : unsigned int {
STANDARD, UNDEFINED
};
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
using Base::traceIsNBError;
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
} // namespace nb
#endif // _NB_ERROR