• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Proposed SG14 status_code
2 (C) 2018-2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: June 2018
4 
5 
6 Boost Software License - Version 1.0 - August 17th, 2003
7 
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
14 
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
29 */
30 
31 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_SYSTEM_CODE_FROM_EXCEPTION_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_SYSTEM_CODE_FROM_EXCEPTION_HPP
33 
34 #include "system_code.hpp"
35 
36 #include <exception>     // for exception_ptr
37 #include <stdexcept>     // for the exception types
38 #include <system_error>  // for std::system_error
39 
40 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
41 
42 /*! A utility function which returns the closest matching system_code to a supplied
43 exception ptr.
44 */
system_code_from_exception(std::exception_ptr && ep=std::current_exception (),system_code not_matched=generic_code (errc::resource_unavailable_try_again))45 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
46 {
47   if(!ep)
48   {
49     return generic_code(errc::success);
50   }
51   try
52   {
53     std::rethrow_exception(ep);
54   }
55   catch(const std::invalid_argument & /*unused*/)
56   {
57     ep = std::exception_ptr();
58     return generic_code(errc::invalid_argument);
59   }
60   catch(const std::domain_error & /*unused*/)
61   {
62     ep = std::exception_ptr();
63     return generic_code(errc::argument_out_of_domain);
64   }
65   catch(const std::length_error & /*unused*/)
66   {
67     ep = std::exception_ptr();
68     return generic_code(errc::argument_list_too_long);
69   }
70   catch(const std::out_of_range & /*unused*/)
71   {
72     ep = std::exception_ptr();
73     return generic_code(errc::result_out_of_range);
74   }
75   catch(const std::logic_error & /*unused*/) /* base class for this group */
76   {
77     ep = std::exception_ptr();
78     return generic_code(errc::invalid_argument);
79   }
80   catch(const std::system_error &e) /* also catches ios::failure */
81   {
82     ep = std::exception_ptr();
83     if(e.code().category() == std::generic_category())
84     {
85       return generic_code(static_cast<errc>(static_cast<int>(e.code().value())));
86     }
87     if(e.code().category() == std::system_category())
88     {
89 #ifdef _WIN32
90       return win32_code(e.code().value());
91 #else
92       return posix_code(e.code().value());
93 #endif
94     }
95     // Don't know this error code category, can't wrap it into std_error_code
96     // as its payload won't fit into system_code, so fall through.
97   }
98   catch(const std::overflow_error & /*unused*/)
99   {
100     ep = std::exception_ptr();
101     return generic_code(errc::value_too_large);
102   }
103   catch(const std::range_error & /*unused*/)
104   {
105     ep = std::exception_ptr();
106     return generic_code(errc::result_out_of_range);
107   }
108   catch(const std::runtime_error & /*unused*/) /* base class for this group */
109   {
110     ep = std::exception_ptr();
111     return generic_code(errc::resource_unavailable_try_again);
112   }
113   catch(const std::bad_alloc & /*unused*/)
114   {
115     ep = std::exception_ptr();
116     return generic_code(errc::not_enough_memory);
117   }
118   catch(...)
119   {
120   }
121   return not_matched;
122 }
123 
124 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
125 
126 #endif
127