From 39798db6b8669a8d3b4605a0bc8bb70d65f0b0aa Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 4 Aug 2024 13:02:38 +0100 Subject: Help exception class for lazy/cached message content --- lib/msgException.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/msgException.h (limited to 'lib') 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 +#include + +template 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 msg; +}; -- cgit v1.2.3