Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 08:52:04

0001 // Copyright (c) 2018-2025 Jean-Louis Leroy
0002 // Distributed under the Boost Software License, Version 1.0.
0003 // See accompanying file LICENSE_1_0.txt
0004 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_OPENMETHOD_POLICY_THROW_ERROR_HPP
0007 #define BOOST_OPENMETHOD_POLICY_THROW_ERROR_HPP
0008 
0009 #include <boost/openmethod/preamble.hpp>
0010 
0011 #include <sstream>
0012 #include <stdexcept>
0013 #include <string>
0014 
0015 namespace boost::openmethod::policies {
0016 
0017 //! Throws error as an exception.
0018 //!
0019 struct throw_error_handler : error_handler {
0020     //! A ErrorHandlerFn metafunction.
0021     //!
0022     //! @tparam Registry The registry containing this policy.
0023     template<class Registry>
0024     class fn {
0025       public:
0026         //! Throws the error.
0027         //!
0028         //! Wraps the error in an object that can be caught either as an
0029         //! `Error`, or as a `std::runtime_error`, and throws it as an exception.
0030         //!
0031         //! @tparam Error A subclass of @ref openmethod_error.
0032         //! @param error The error object.
0033         template<class Error>
0034         [[noreturn]] static auto error(const Error& error) -> void {
0035             struct wrapper : Error, std::runtime_error {
0036                 wrapper(const Error& error, std::string&& description)
0037                     : Error(error), std::runtime_error(description) {
0038                 }
0039             };
0040 
0041             std::ostringstream os;
0042             error.template write<Registry>(os);
0043 
0044             throw wrapper(error, os.str());
0045         }
0046     };
0047 };
0048 
0049 } // namespace boost::openmethod::policies
0050 
0051 #endif