Added automagic constructor inheritance.

This commit is contained in:
NaifBanana 2025-11-10 00:02:06 -06:00
parent 3ac76f1fac
commit 3f0486b889
2 changed files with 33 additions and 30 deletions

View File

@ -23,6 +23,29 @@ typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
template<class ErrorType>
class ErrorBase : public std::exception {
public:
ErrorBase(
const unsigned int code,
unsigned int line=0,
std::string filename=""
) noexcept : ErrorBase(
code,
ErrorBase<ErrorType>::lookup(code),
line,
filename
) {}
ErrorBase(
const unsigned int code,
const std::exception& trace,
unsigned int line=0,
std::string filename=""
) noexcept : ErrorBase(
code,
ErrorBase<ErrorType>::lookup(code),
trace,
line,
filename
) {}
static std::string lookup(unsigned int);
unsigned int code() const noexcept {
return static_cast<const ErrorType*>(this)->_code;
@ -71,29 +94,6 @@ protected:
}
_msg += "\n Trace: " + what_msg;
}
ErrorBase(
const unsigned int code,
unsigned int line=0,
std::string filename=""
) noexcept : ErrorBase(
code,
ErrorBase<ErrorType>::lookup(code),
line,
filename
) {}
ErrorBase(
const unsigned int code,
const std::exception& trace,
unsigned int line=0,
std::string filename=""
) noexcept : ErrorBase(
code,
ErrorBase<ErrorType>::lookup(code),
trace,
line,
filename
) {}
unsigned int _code;
std::string _msg;
@ -105,9 +105,10 @@ public:
GENERAL, UNDEFINED, BADERRORCODE
};
Error(unsigned int code) : ErrorBase<Error>(code, ErrorBase<Error>::lookup(code)) {}
Error(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="")
: ErrorBase<Error>(code, ErrorBase<Error>::lookup(code), trace, line, filename) {}
using ErrorBase<Error>::ErrorBase;
//Error(unsigned int code) : ErrorBase<Error>(code, ErrorBase<Error>::lookup(code)) {}
//Error(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="")
// : ErrorBase<Error>(code, ErrorBase<Error>::lookup(code), trace, line, filename) {}
friend ErrorBase<Error>;

View File

@ -7,11 +7,13 @@ using namespace nb;
class TestError : public ErrorBase<TestError> {
public:
TestError(unsigned int code, unsigned int line=0, const std::string& filename="")
: ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code)) {}
TestError(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="")
: ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code), trace, line, filename) {}
//TestError(unsigned int code, unsigned int line=0, const std::string& filename="")
// : ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code)) {}
//TestError(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="")
// : ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code), trace, line, filename) {}
using ErrorBase<TestError>::ErrorBase;
enum ErrorCodes : unsigned int {
A, B, C, D
};