diff --git a/except.hpp b/except.hpp index 286d4cdf..8cb96ab0 100644 --- a/except.hpp +++ b/except.hpp @@ -8,7 +8,9 @@ #pragma once +#include #include +#include namespace cruft { /// A base exception class for all cruft libraries. @@ -24,6 +26,38 @@ namespace cruft { }; + /// A mixin error class that can be used when inheriting from + /// `cruft::error`. + /// + /// It accepts a single std::string, saving it so that it can be passed + /// through to a std::ostream later on. + /// + /// By setting `BaseT` to a locally useful error class we can still + /// maintain an appropriate exception hierarchy for a given module. + /// eg, pass `foo::error` (which derives eventually from `cruft::error`) + /// in the module `foo`. + /// + /// \tparam BaseT The desired base class. + template + class string_error : public BaseT { + public: + template + string_error (std::string const &_message, Args &&...args) + : BaseT (std::forward (args)...) + , m_message (_message) + { ; } + + std::ostream& + describe (std::ostream &os) const noexcept override + { + return os << m_message; + } + + private: + std::string m_message; + }; + + /// Use `error::describe` to render the supplied error object to a /// std::ostream. std::ostream&