From 3f0486b8894e3c00a0bd943a3553769f4317ecb5 Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Mon, 10 Nov 2025 00:02:06 -0600 Subject: [PATCH] Added automagic constructor inheritance. --- include/Errors.hpp | 53 ++++++++++++++++++++-------------------- tests/ErrorTest/main.cpp | 10 +++++--- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/include/Errors.hpp b/include/Errors.hpp index 151bca0..8edab0a 100644 --- a/include/Errors.hpp +++ b/include/Errors.hpp @@ -23,6 +23,29 @@ typedef std::unordered_map ErrorCodeMap; template class ErrorBase : public std::exception { public: + ErrorBase( + const unsigned int code, + unsigned int line=0, + std::string filename="" + ) noexcept : ErrorBase( + code, + ErrorBase::lookup(code), + line, + filename + ) {} + ErrorBase( + const unsigned int code, + const std::exception& trace, + unsigned int line=0, + std::string filename="" + ) noexcept : ErrorBase( + code, + ErrorBase::lookup(code), + trace, + line, + filename + ) {} + static std::string lookup(unsigned int); unsigned int code() const noexcept { return static_cast(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::lookup(code), - line, - filename - ) {} - - ErrorBase( - const unsigned int code, - const std::exception& trace, - unsigned int line=0, - std::string filename="" - ) noexcept : ErrorBase( - code, - ErrorBase::lookup(code), - trace, - line, - filename - ) {} unsigned int _code; std::string _msg; @@ -105,9 +105,10 @@ public: GENERAL, UNDEFINED, BADERRORCODE }; - Error(unsigned int code) : ErrorBase(code, ErrorBase::lookup(code)) {} - Error(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="") - : ErrorBase(code, ErrorBase::lookup(code), trace, line, filename) {} + using ErrorBase::ErrorBase; + //Error(unsigned int code) : ErrorBase(code, ErrorBase::lookup(code)) {} + //Error(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="") + // : ErrorBase(code, ErrorBase::lookup(code), trace, line, filename) {} friend ErrorBase; diff --git a/tests/ErrorTest/main.cpp b/tests/ErrorTest/main.cpp index 25e426d..f0eda15 100644 --- a/tests/ErrorTest/main.cpp +++ b/tests/ErrorTest/main.cpp @@ -7,11 +7,13 @@ using namespace nb; class TestError : public ErrorBase { public: - TestError(unsigned int code, unsigned int line=0, const std::string& filename="") - : ErrorBase(code, ErrorBase::lookup(code)) {} - TestError(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="") - : ErrorBase(code, ErrorBase::lookup(code), trace, line, filename) {} + //TestError(unsigned int code, unsigned int line=0, const std::string& filename="") + // : ErrorBase(code, ErrorBase::lookup(code)) {} + //TestError(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="") + // : ErrorBase(code, ErrorBase::lookup(code), trace, line, filename) {} + using ErrorBase::ErrorBase; + enum ErrorCodes : unsigned int { A, B, C, D };