Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / outcome / utils.hpp
1 /* Tries to convert an exception ptr into its equivalent error code
2 (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (11 commits)
3 File Created: July 2017
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_UTILS_HPP
32 #define BOOST_OUTCOME_UTILS_HPP
33
34 #include "config.hpp"
35
36 #include <exception>
37 #include <string>
38 #include <system_error>
39
40 BOOST_OUTCOME_V2_NAMESPACE_BEGIN
41
42 #ifndef BOOST_NO_EXCEPTIONS
43 /*! AWAITING HUGO JSON CONVERSION TOOL 
44 SIGNATURE NOT RECOGNISED
45 */
46 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
47 {
48   if(!ep)
49   {
50     return {};
51   }
52   try
53   {
54     std::rethrow_exception(ep);
55   }
56   catch(const std::invalid_argument & /*unused*/)
57   {
58     ep = std::exception_ptr();
59     return std::make_error_code(std::errc::invalid_argument);
60   }
61   catch(const std::domain_error & /*unused*/)
62   {
63     ep = std::exception_ptr();
64     return std::make_error_code(std::errc::argument_out_of_domain);
65   }
66   catch(const std::length_error & /*unused*/)
67   {
68     ep = std::exception_ptr();
69     return std::make_error_code(std::errc::argument_list_too_long);
70   }
71   catch(const std::out_of_range & /*unused*/)
72   {
73     ep = std::exception_ptr();
74     return std::make_error_code(std::errc::result_out_of_range);
75   }
76   catch(const std::logic_error & /*unused*/) /* base class for this group */
77   {
78     ep = std::exception_ptr();
79     return std::make_error_code(std::errc::invalid_argument);
80   }
81   catch(const std::system_error &e) /* also catches ios::failure */
82   {
83     ep = std::exception_ptr();
84     return e.code();
85   }
86   catch(const std::overflow_error & /*unused*/)
87   {
88     ep = std::exception_ptr();
89     return std::make_error_code(std::errc::value_too_large);
90   }
91   catch(const std::range_error & /*unused*/)
92   {
93     ep = std::exception_ptr();
94     return std::make_error_code(std::errc::result_out_of_range);
95   }
96   catch(const std::runtime_error & /*unused*/) /* base class for this group */
97   {
98     ep = std::exception_ptr();
99     return std::make_error_code(std::errc::resource_unavailable_try_again);
100   }
101   catch(const std::bad_alloc & /*unused*/)
102   {
103     ep = std::exception_ptr();
104     return std::make_error_code(std::errc::not_enough_memory);
105   }
106   catch(...)
107   {
108   }
109   return not_matched;
110 }
111
112 /*! AWAITING HUGO JSON CONVERSION TOOL 
113 SIGNATURE NOT RECOGNISED
114 */
115 inline void try_throw_std_exception_from_error(std::error_code ec, const std::string &msg = std::string{})
116 {
117   if(!ec || (ec.category() != std::generic_category()
118 #ifndef _WIN32
119              && ec.category() != std::system_category()
120 #endif
121              ))
122   {
123     return;
124   }
125   switch(ec.value())
126   {
127   case EINVAL:
128     throw msg.empty() ? std::invalid_argument("invalid argument") : std::invalid_argument(msg);
129   case EDOM:
130     throw msg.empty() ? std::domain_error("domain error") : std::domain_error(msg);
131   case E2BIG:
132     throw msg.empty() ? std::length_error("length error") : std::length_error(msg);
133   case ERANGE:
134     throw msg.empty() ? std::out_of_range("out of range") : std::out_of_range(msg);
135   case EOVERFLOW:
136     throw msg.empty() ? std::overflow_error("overflow error") : std::overflow_error(msg);
137   case ENOMEM:
138     throw std::bad_alloc();
139   }
140 }
141 #endif
142
143 BOOST_OUTCOME_V2_NAMESPACE_END
144
145 #endif