except: add string_error mixin
This commit is contained in:
parent
8c4b7ac93c
commit
f3f59e1f37
34
except.hpp
34
except.hpp
@ -8,7 +8,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
|
||||
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 <typename BaseT>
|
||||
class string_error : public BaseT {
|
||||
public:
|
||||
template <typename... Args>
|
||||
string_error (std::string const &_message, Args &&...args)
|
||||
: BaseT (std::forward<Args> (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&
|
||||
|
Loading…
Reference in New Issue
Block a user