Changed error system to CRTP, rough draft
This commit is contained in:
+20
-17
@@ -3,29 +3,32 @@
|
||||
|
||||
using namespace nb;
|
||||
|
||||
class TestError : public nb::Error {
|
||||
class TestError : public ErrorBase<TestError> {
|
||||
public:
|
||||
TestError(unsigned int code, nb::Error* const trace=nullptr) : _type("nb::TestError"), Error(code, ErrorCodeLookup<TestError>(code), trace) {}
|
||||
|
||||
friend const char* ErrorCodeLookup<TestError>(unsigned int);
|
||||
|
||||
protected:
|
||||
static const nb::ErrorCodeMap ErrorCodes;
|
||||
const char* _type;
|
||||
TestError(unsigned int code, std::exception* const trace=nullptr)
|
||||
: ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code), trace) {}
|
||||
|
||||
enum ErrorCodes : unsigned int {
|
||||
A, B, C, D
|
||||
};
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
const ErrorCodeMap TestError::ErrorCodes{
|
||||
{0, "Hey!"},
|
||||
{1, "How"},
|
||||
{2, "You"},
|
||||
{3, "Doin"}
|
||||
const std::string TestError::type="TestError";
|
||||
const ErrorCodeMap TestError::ErrorMessages{
|
||||
{TestError::ErrorCodes::A, "Hey!"},
|
||||
{TestError::ErrorCodes::B, "How"},
|
||||
{TestError::ErrorCodes::C, "You"},
|
||||
{TestError::ErrorCodes::D, "Doin"}
|
||||
};
|
||||
|
||||
int main() {
|
||||
TestError a(0);
|
||||
TestError b(1, &a);
|
||||
TestError c(2, &b);
|
||||
throw TestError(3, &c);
|
||||
TestError d(TestError::ErrorCodes::D);
|
||||
TestError c(TestError::ErrorCodes::C, &d);
|
||||
TestError b(TestError::ErrorCodes::B, &c);
|
||||
TestError a(TestError::ErrorCodes::A, &b);
|
||||
throw Error(Error::ErrorCodes::UNDEFINED, &a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user