Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:59:14

0001 /* Tries to convert an exception ptr into its equivalent error code
0002 (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (11 commits)
0003 File Created: July 2017
0004 
0005 
0006 Boost Software License - Version 1.0 - August 17th, 2003
0007 
0008 Permission is hereby granted, free of charge, to any person or organization
0009 obtaining a copy of the software and accompanying documentation covered by
0010 this license (the "Software") to use, reproduce, display, distribute,
0011 execute, and transmit the Software, and to prepare derivative works of the
0012 Software, and to permit third-parties to whom the Software is furnished to
0013 do so, all subject to the following:
0014 
0015 The copyright notices in the Software and this entire statement, including
0016 the above license grant, this restriction and the following disclaimer,
0017 must be included in all copies of the Software, in whole or in part, and
0018 all derivative works of the Software, unless such copies or derivative
0019 works are solely in the form of machine-executable object code generated by
0020 a source language processor.
0021 
0022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0024 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
0025 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
0026 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
0027 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0028 DEALINGS IN THE SOFTWARE.
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 /*! AWAITING HUGO JSON CONVERSION TOOL 
0044 SIGNATURE NOT RECOGNISED
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 & /*unused*/)
0057   {
0058     ep = std::exception_ptr();
0059     return std::make_error_code(std::errc::invalid_argument);
0060   }
0061   catch(const std::domain_error & /*unused*/)
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 & /*unused*/)
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 & /*unused*/)
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 & /*unused*/) /* base class for this group */
0077   {
0078     ep = std::exception_ptr();
0079     return std::make_error_code(std::errc::invalid_argument);
0080   }
0081   catch(const std::system_error &e) /* also catches ios::failure */
0082   {
0083     ep = std::exception_ptr();
0084     return e.code();
0085   }
0086   catch(const std::overflow_error & /*unused*/)
0087   {
0088     ep = std::exception_ptr();
0089     return std::make_error_code(std::errc::value_too_large);
0090   }
0091   catch(const std::range_error & /*unused*/)
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 & /*unused*/) /* base class for this group */
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 & /*unused*/)
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 /*! AWAITING HUGO JSON CONVERSION TOOL 
0113 SIGNATURE NOT RECOGNISED
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