123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_SYSTEM_CODE_FROM_EXCEPTION_HPP
- #define BOOST_OUTCOME_SYSTEM_ERROR2_SYSTEM_CODE_FROM_EXCEPTION_HPP
- #include "system_code.hpp"
- #include "status_error.hpp"
- #include <exception> // for exception_ptr
- #include <stdexcept> // for the exception types
- #include <system_error> // for std::system_error
- BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
- inline system_code system_code_from_exception(std::exception_ptr &&ep = std::current_exception(),
- system_code not_matched = generic_code(errc::resource_unavailable_try_again)) noexcept
- {
- if(!ep)
- {
- return generic_code(errc::success);
- }
- try
- {
- try
- {
- std::rethrow_exception(ep);
- }
- catch(const status_error<void> &e)
- {
- try
- {
- system_code erased(in_place, e.code());
- if(!erased.empty())
- {
- return erased;
- }
- }
- catch(...)
- {
-
-
- }
- throw;
- }
- catch(...)
- {
- throw;
- }
- }
- catch(const std::invalid_argument & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::invalid_argument);
- }
- catch(const std::domain_error & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::argument_out_of_domain);
- }
- catch(const std::length_error & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::argument_list_too_long);
- }
- catch(const std::out_of_range & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::result_out_of_range);
- }
- catch(const std::logic_error & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::invalid_argument);
- }
- catch(const std::system_error &e)
- {
- ep = std::exception_ptr();
- if(e.code().category() == std::generic_category())
- {
- return generic_code(static_cast<errc>(static_cast<int>(e.code().value())));
- }
- if(e.code().category() == std::system_category())
- {
- #ifdef _WIN32
- return win32_code(e.code().value());
- #else
- #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX
- return posix_code(e.code().value());
- #else
- return generic_code(static_cast<errc>(e.code().value()));
- #endif
- #endif
- }
-
-
- }
- catch(const std::overflow_error & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::value_too_large);
- }
- catch(const std::range_error & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::result_out_of_range);
- }
- catch(const std::runtime_error & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::resource_unavailable_try_again);
- }
- catch(const std::bad_alloc & )
- {
- ep = std::exception_ptr();
- return generic_code(errc::not_enough_memory);
- }
- catch(...)
- {
- }
- return not_matched;
- }
- BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
- #endif
|