diff options
-rw-r--r-- | lib/msgException.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/msgException.h b/lib/msgException.h new file mode 100644 index 0000000..660738d --- /dev/null +++ b/lib/msgException.h @@ -0,0 +1,23 @@ +#pragma once + +#include <optional> +#include <string> + +template<typename BaseException> class MsgException : public BaseException { +protected: + using BaseException::BaseException; + [[nodiscard]] virtual std::string getMsg() const noexcept = 0; + +public: + [[nodiscard]] const char * + what() const noexcept override + { + if (!msg) { + msg.emplace(getMsg()); + } + return msg->c_str(); + } + +private: + mutable std::optional<std::string> msg; +}; |