1 /* Proposed SG14 status_code
2 (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: Aug 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_STD_ERROR_CODE_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_STD_ERROR_CODE_HPP
33 
34 #include "posix_code.hpp"
35 
36 #if defined(_WIN32) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
37 #include "win32_code.hpp"
38 #endif
39 
40 #include <system_error>
41 
42 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
43 
44 namespace detail
45 {
46   struct make_std_categories
47   {
generic_categorydetail::make_std_categories48     static const std::error_category &generic_category() { return std::generic_category(); }
system_categorydetail::make_std_categories49     static const std::error_category &system_category() { return std::system_category(); }
50   };
51 }
52 
53 template <class error_code_type, class make_category_types> class _error_code_domain;
54 //! A wrapper of `std::error_code`.
55 using std_error_code = status_code<_error_code_domain<std::error_code, detail::make_std_categories>>;
56 
57 /*! The implementation of the domain for `std::error_code` error codes.
58 */
59 template <class error_code_type, class make_categories_type> class _error_code_domain : public status_code_domain
60 {
61   template <class DomainType> friend class status_code;
62   template <class StatusCode> friend class detail::indirecting_domain;
63   using _base = status_code_domain;
64   using _status_code = status_code<_error_code_domain>;
65 
_make_string_ref(error_code_type c)66   static _base::string_ref _make_string_ref(error_code_type c) noexcept
67   {
68     try
69     {
70       std::string msg = c.message();
71       auto *p = static_cast<char *>(malloc(msg.size() + 1));  // NOLINT
72       if(p == nullptr)
73       {
74         return _base::string_ref("failed to allocate message");
75       }
76       memcpy(p, msg.c_str(), msg.size() + 1);
77       return _base::atomic_refcounted_string_ref(p, msg.size());
78     }
79     catch(...)
80     {
81       return _base::string_ref("failed to allocate message");
82     }
83   }
84 
85 public:
86   //! The value type of the `std::error_code` code, which is the `std::error_code`
87   using value_type = error_code_type;
88   using _base::string_ref;
89 
90   //! Default constructor
_error_code_domain(typename _base::unique_id_type id=0x223a160d20de97b4)91   constexpr explicit _error_code_domain(typename _base::unique_id_type id = 0x223a160d20de97b4) noexcept : _base(id) {}
92   _error_code_domain(const _error_code_domain &) = default;
93   _error_code_domain(_error_code_domain &&) = default;
94   _error_code_domain &operator=(const _error_code_domain &) = default;
95   _error_code_domain &operator=(_error_code_domain &&) = default;
96   ~_error_code_domain() = default;
97 
98   //! Constexpr singleton getter. Returns constexpr error_code_domain variable.
99   static inline constexpr const _error_code_domain &get();
100 
name() const101   virtual string_ref name() const noexcept override { return string_ref("error_code compatibility domain"); }  // NOLINT
102 protected:
_do_failure(const status_code<void> & code) const103   virtual bool _do_failure(const status_code<void> &code) const noexcept override final  // NOLINT
104   {
105     assert(code.domain() == *this);
106     return static_cast<const _status_code &>(code).value().value() != 0;  // NOLINT
107   }
_do_equivalent(const status_code<void> & code1,const status_code<void> & code2) const108   virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override final  // NOLINT
109   {
110     assert(code1.domain() == *this);
111     const auto &c1 = static_cast<const _status_code &>(code1);  // NOLINT
112     const auto &cat1 = c1.value().category();
113     // Are we comparing to another wrapped error_code?
114     if(code2.domain() == *this)
115     {
116       const auto &c2 = static_cast<const _status_code &>(code2);  // NOLINT
117       const auto &cat2 = c2.value().category();
118       // If the error code categories are identical, do literal comparison
119       if(cat1 == cat2)
120       {
121         return c1.value().value() == c2.value().value();
122       }
123       // Otherwise fall back onto the _generic_code comparison, which uses default_error_condition()
124       return false;
125     }
126     // Am I an error code with generic category?
127     const auto &generic_category = make_categories_type::generic_category();
128     if(cat1 == generic_category)
129     {
130       // Convert to generic code, and compare that
131       generic_code _c1(static_cast<errc>(c1.value().value()));
132       return _c1 == code2;
133     }
134     // Am I an error code with system category?
135     const auto &system_category = make_categories_type::system_category();
136     if(cat1 == system_category)
137     {
138 // Convert to POSIX or Win32 code, and compare that
139 #ifdef _WIN32
140       win32_code _c1((win32::DWORD) c1.value().value());
141 #else
142       posix_code _c1(c1.value().value());
143 #endif
144       return _c1 == code2;
145     }
146     return false;
147   }
_generic_code(const status_code<void> & code) const148   virtual generic_code _generic_code(const status_code<void> &code) const noexcept override final  // NOLINT
149   {
150     assert(code.domain() == *this);
151     const auto &c = static_cast<const _status_code &>(code);  // NOLINT
152     // Ask my embedded error code for its mapping to std::errc, which is a subset of our generic_code errc.
153     return generic_code(static_cast<errc>(c.value().default_error_condition().value()));
154   }
_do_message(const status_code<void> & code) const155   virtual string_ref _do_message(const status_code<void> &code) const noexcept override  // NOLINT
156   {
157     assert(code.domain() == *this);
158     const auto &c = static_cast<const _status_code &>(code);  // NOLINT
159     return _make_string_ref(c.value());
160   }
161 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
_do_throw_exception(const status_code<void> & code) const162   BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override  // NOLINT
163   {
164     assert(code.domain() == *this);
165     const auto &c = static_cast<const _status_code &>(code);  // NOLINT
166     throw std::system_error(c.value());
167   }
168 #endif
169 };
170 //! A constexpr source variable for the `std::error_code` code domain. Returned by `_error_code_domain<error_code_type, detail::make_std_categoriesy>::get()`.
171 constexpr _error_code_domain<std::error_code, detail::make_std_categories> std_error_code_domain;
get()172 template <class error_code_type, class make_categories_type> inline constexpr const _error_code_domain<error_code_type, make_categories_type> &_error_code_domain<error_code_type, make_categories_type>::get()
173 {
174   return std_error_code_domain;
175 }
176 // Enable implicit construction of `std_error_code` from `std::error_code`.
make_status_code(std::error_code c)177 inline std_error_code make_status_code(std::error_code c) noexcept
178 {
179   return std_error_code(in_place, c);
180 }
181 
182 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
183 
184 #endif
185