File indexing completed on 2025-12-16 09:59:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #ifndef BOOST_OUTCOME_UTILS_HPP
0032 #define BOOST_OUTCOME_UTILS_HPP
0033
0034 #include "config.hpp"
0035
0036 #include <exception>
0037 #include <string>
0038 #include <system_error>
0039
0040 BOOST_OUTCOME_V2_NAMESPACE_BEGIN
0041
0042 #ifndef BOOST_NO_EXCEPTIONS
0043
0044
0045
0046 inline std::error_code error_from_exception(std::exception_ptr &&ep = std::current_exception(), std::error_code not_matched = std::make_error_code(std::errc::resource_unavailable_try_again)) noexcept
0047 {
0048 if(!ep)
0049 {
0050 return {};
0051 }
0052 try
0053 {
0054 std::rethrow_exception(ep);
0055 }
0056 catch(const std::invalid_argument & )
0057 {
0058 ep = std::exception_ptr();
0059 return std::make_error_code(std::errc::invalid_argument);
0060 }
0061 catch(const std::domain_error & )
0062 {
0063 ep = std::exception_ptr();
0064 return std::make_error_code(std::errc::argument_out_of_domain);
0065 }
0066 catch(const std::length_error & )
0067 {
0068 ep = std::exception_ptr();
0069 return std::make_error_code(std::errc::argument_list_too_long);
0070 }
0071 catch(const std::out_of_range & )
0072 {
0073 ep = std::exception_ptr();
0074 return std::make_error_code(std::errc::result_out_of_range);
0075 }
0076 catch(const std::logic_error & )
0077 {
0078 ep = std::exception_ptr();
0079 return std::make_error_code(std::errc::invalid_argument);
0080 }
0081 catch(const std::system_error &e)
0082 {
0083 ep = std::exception_ptr();
0084 return e.code();
0085 }
0086 catch(const std::overflow_error & )
0087 {
0088 ep = std::exception_ptr();
0089 return std::make_error_code(std::errc::value_too_large);
0090 }
0091 catch(const std::range_error & )
0092 {
0093 ep = std::exception_ptr();
0094 return std::make_error_code(std::errc::result_out_of_range);
0095 }
0096 catch(const std::runtime_error & )
0097 {
0098 ep = std::exception_ptr();
0099 return std::make_error_code(std::errc::resource_unavailable_try_again);
0100 }
0101 catch(const std::bad_alloc & )
0102 {
0103 ep = std::exception_ptr();
0104 return std::make_error_code(std::errc::not_enough_memory);
0105 }
0106 catch(...)
0107 {
0108 }
0109 return not_matched;
0110 }
0111
0112
0113
0114
0115 inline void try_throw_std_exception_from_error(std::error_code ec, const std::string &msg = std::string{})
0116 {
0117 if(!ec || (ec.category() != std::generic_category()
0118 #ifndef _WIN32
0119 && ec.category() != std::system_category()
0120 #endif
0121 ))
0122 {
0123 return;
0124 }
0125 switch(ec.value())
0126 {
0127 case EINVAL:
0128 throw msg.empty() ? std::invalid_argument("invalid argument") : std::invalid_argument(msg);
0129 case EDOM:
0130 throw msg.empty() ? std::domain_error("domain error") : std::domain_error(msg);
0131 case E2BIG:
0132 throw msg.empty() ? std::length_error("length error") : std::length_error(msg);
0133 case ERANGE:
0134 throw msg.empty() ? std::out_of_range("out of range") : std::out_of_range(msg);
0135 case EOVERFLOW:
0136 throw msg.empty() ? std::overflow_error("overflow error") : std::overflow_error(msg);
0137 case ENOMEM:
0138 throw std::bad_alloc();
0139 }
0140 }
0141 #endif
0142
0143 BOOST_OUTCOME_V2_NAMESPACE_END
0144
0145 #endif