Simplifying error design

This commit is contained in:
NaifBanana 2026-07-18 16:37:39 -05:00
parent e4b164f29a
commit abbae156a2
6 changed files with 75 additions and 33 deletions

View File

@ -73,8 +73,10 @@ endif()
# External Dep paths
set(GLFW_PATH ../glfw/)
set(GLAD_PATH ../glad/)
set(STBIMAGE_PATH ../stbi_image)
get_filename_component(GLFW_PATH ${GLFW_PATH} ABSOLUTE)
get_filename_component(GLAD_PATH ${GLAD_PATH} ABSOLUTE)
get_filename_component(STBIMAGE_PATH ${STBIMAGE_PATH} ABSOLUTE)
add_subdirectory(./engine)

View File

@ -9,15 +9,11 @@
#include <unordered_map>
#include "StringUtils.hpp"
#include "TypeTraits.hpp"
namespace nb {
typedef std::unordered_map<unsigned int, std::string> ErrorCodeMap;
template <class ErrorType=NoneType>
class Error;
class ErrorBase {
protected:
@ -136,40 +132,40 @@ class Error : public ErrorBase {
using ErrorBase::type;
};
template<>
class Error<NoneType> : public Error<Error<NoneType>> {
using Base = Error<Error<NoneType>>;
class NBError : public Error<NBError> {
protected:
using Base = Error<NBError>;
public:
using Base::Base;
enum Codes : unsigned int {
STANDARD, UNDEFINED, INDEX_ERROR
};
Error(unsigned int code_=1) noexcept : Base(code_) {}
Error(const std::exception& err) : Base(
Codes::STANDARD,
NBError(unsigned int code_=1) noexcept : Base(code_) {}
NBError(const std::exception& err) : Base(
STANDARD,
std::string(err.what()),
"std::exception",
nullptr
) {}
// fix
Error(std::string msg_) noexcept : Base(
NBError(std::string msg_) noexcept : Base(
Codes::UNDEFINED,
msg_
) {}
enum Codes : unsigned int {
STANDARD, UNDEFINED
inline static const std::string type = "nb::Error";
inline static const ErrorCodeMap ErrorMessages = {
{STANDARD, "std::exception"},
{UNDEFINED, "Error"},
{INDEX_ERROR, "Indexing error"}
};
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
// template<typename... Args>
// Error(Args...) -> Error<NoneType>;
} // namespace nb
#endif // _NB_ERRORS_IMPL

View File

@ -138,6 +138,15 @@ public:
static_cast<LoggerType*>(this)->write_message(err.what(), lvl, file, line);
}
template<typename U>
std::enable_if_t<std::is_integral_v<U>, void> write_message(
const U& val,
uint8_t lvl=0x00,
std::string file="",
unsigned int line=0
) {
static_cast<LoggerType*>(this)->write_message(std::to_string(val), lvl, file, line);
}
};
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {

View File

@ -33,6 +33,9 @@ namespace detail {
struct NoneType{};
template<typename T, unsigned int Arg>
using Const_t = std::integral_constant<T, Arg>;
template <
template <class...> class Op,
class... Args
@ -109,6 +112,41 @@ ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
}
template<typename T, typename... Pack>
struct Find;
template<typename T, typename U, typename... Pack>
struct Find<T, U, Pack...> {
protected:
using here = Find<T, U>;
static constexpr unsigned int downstream = std::conditional_t<
here::value,
Const_t<unsigned int, 0>,
Find<T, Pack...>
>::value;
public:
static constexpr unsigned int value = std::conditional_t<
here::value,
Const_t<unsigned int, 1>,
std::conditional_t<
downstream,
Const_t<unsigned int, 1+downstream>,
Const_t<unsigned int, 0>
>
>::value;
};
template<typename T, typename U>
struct Find<T, U> {
public:
static constexpr unsigned int value = std::conditional_t<
std::is_base_of<T, U>::value,
std::integral_constant<unsigned int, 1>,
std::integral_constant<unsigned int, 0>
>::value;
};
template <typename T, typename Pack1, typename... Pack>
struct RefPackToPtrVec {
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)

View File

@ -54,7 +54,9 @@ ByteVector concatVectorBytes(const std::vector<T>& vec1, const std::vector<S>& v
template<typename T>
std::vector<T> bytesToVector(const ByteVector& vec) {
if (vec.size() % sizeof(T) != 0) {
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
THROW(Error<NoneType>(
std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.")
));
}
unsigned int num_elmts = vec.size() / sizeof(T);
std::vector<T> ret(num_elmts);

View File

@ -2,15 +2,10 @@
namespace nb {
const std::string Error<>::type = "nb::Error";
const ErrorCodeMap Error<>::ErrorMessages = {
{Error::Codes::STANDARD, "std::exception"},
{Error::Codes::UNDEFINED, "Error"}
};
//ErrorBase::
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
: ErrorBase(Error<NoneType>(exception_)) {}
: ErrorBase(NBError(exception_)) {}
} // namespace nb
}