1 /* Proposed SG14 status_code
2 (C) 2018-2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: Feb 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_POSIX_CODE_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_POSIX_CODE_HPP
33
34 #ifdef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX
35 #error <posix_code.hpp> is not includable when BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX is defined!
36 #endif
37
38 #include "quick_status_code_from_enum.hpp"
39
40 #include <cstring> // for strchr and strerror_r
41
42 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
43
44 class _posix_code_domain;
45 //! A POSIX error code, those returned by `errno`.
46 using posix_code = status_code<_posix_code_domain>;
47 //! A specialisation of `status_error` for the POSIX error code domain.
48 using posix_error = status_error<_posix_code_domain>;
49
50 namespace mixins
51 {
52 template <class Base> struct mixin<Base, _posix_code_domain> : public Base
53 {
54 using Base::Base;
55
56 //! Returns a `posix_code` for the current value of `errno`.
57 static posix_code current() noexcept;
58 };
59 } // namespace mixins
60
61 /*! The implementation of the domain for POSIX error codes, those returned by `errno`.
62 */
63 class _posix_code_domain : public status_code_domain
64 {
65 template <class DomainType> friend class status_code;
66 template <class StatusCode> friend class detail::indirecting_domain;
67 using _base = status_code_domain;
68
_make_string_ref(int c)69 static _base::string_ref _make_string_ref(int c) noexcept
70 {
71 char buffer[1024] = "";
72 #ifdef _WIN32
73 strerror_s(buffer, sizeof(buffer), c);
74 #elif defined(__gnu_linux__) && !defined(__ANDROID__) // handle glibc's weird strerror_r()
75 char *s = strerror_r(c, buffer, sizeof(buffer)); // NOLINT
76 if(s != nullptr)
77 {
78 strncpy(buffer, s, sizeof(buffer)); // NOLINT
79 buffer[1023] = 0;
80 }
81 #else
82 strerror_r(c, buffer, sizeof(buffer));
83 #endif
84 size_t length = strlen(buffer); // NOLINT
85 auto *p = static_cast<char *>(malloc(length + 1)); // NOLINT
86 if(p == nullptr)
87 {
88 return _base::string_ref("failed to get message from system");
89 }
90 memcpy(p, buffer, length + 1); // NOLINT
91 return _base::atomic_refcounted_string_ref(p, length);
92 }
93
94 public:
95 //! The value type of the POSIX code, which is an `int`
96 using value_type = int;
97 using _base::string_ref;
98
99 //! Default constructor
_posix_code_domain(typename _base::unique_id_type id=0xa59a56fe5f310933)100 constexpr explicit _posix_code_domain(typename _base::unique_id_type id = 0xa59a56fe5f310933) noexcept
101 : _base(id)
102 {
103 }
104 _posix_code_domain(const _posix_code_domain &) = default;
105 _posix_code_domain(_posix_code_domain &&) = default;
106 _posix_code_domain &operator=(const _posix_code_domain &) = default;
107 _posix_code_domain &operator=(_posix_code_domain &&) = default;
108 ~_posix_code_domain() = default;
109
110 //! Constexpr singleton getter. Returns constexpr posix_code_domain variable.
111 static inline constexpr const _posix_code_domain &get();
112
name() const113 virtual string_ref name() const noexcept override { return string_ref("posix domain"); } // NOLINT
114 protected:
_do_failure(const status_code<void> & code) const115 virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
116 {
117 assert(code.domain() == *this); // NOLINT
118 return static_cast<const posix_code &>(code).value() != 0; // NOLINT
119 }
_do_equivalent(const status_code<void> & code1,const status_code<void> & code2) const120 virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
121 {
122 assert(code1.domain() == *this); // NOLINT
123 const auto &c1 = static_cast<const posix_code &>(code1); // NOLINT
124 if(code2.domain() == *this)
125 {
126 const auto &c2 = static_cast<const posix_code &>(code2); // NOLINT
127 return c1.value() == c2.value();
128 }
129 if(code2.domain() == generic_code_domain)
130 {
131 const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
132 if(static_cast<int>(c2.value()) == c1.value())
133 {
134 return true;
135 }
136 }
137 return false;
138 }
_generic_code(const status_code<void> & code) const139 virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
140 {
141 assert(code.domain() == *this); // NOLINT
142 const auto &c = static_cast<const posix_code &>(code); // NOLINT
143 return generic_code(static_cast<errc>(c.value()));
144 }
_do_message(const status_code<void> & code) const145 virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
146 {
147 assert(code.domain() == *this); // NOLINT
148 const auto &c = static_cast<const posix_code &>(code); // NOLINT
149 return _make_string_ref(c.value());
150 }
151 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
_do_throw_exception(const status_code<void> & code) const152 BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
153 {
154 assert(code.domain() == *this); // NOLINT
155 const auto &c = static_cast<const posix_code &>(code); // NOLINT
156 throw status_error<_posix_code_domain>(c);
157 }
158 #endif
159 };
160 //! A constexpr source variable for the POSIX code domain, which is that of `errno`. Returned by `_posix_code_domain::get()`.
161 constexpr _posix_code_domain posix_code_domain;
get()162 inline constexpr const _posix_code_domain &_posix_code_domain::get()
163 {
164 return posix_code_domain;
165 }
166
167 namespace mixins
168 {
current()169 template <class Base> inline posix_code mixin<Base, _posix_code_domain>::current() noexcept { return posix_code(errno); }
170 } // namespace mixins
171
172 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
173
174 #endif
175