1 /* Proposed SG14 status_code
2 (C) 2018-2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: Jun 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_ERRORED_STATUS_CODE_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_ERRORED_STATUS_CODE_HPP
33
34 #include "quick_status_code_from_enum.hpp"
35 #include "status_code_ptr.hpp"
36
37 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
38
39 /*! A `status_code` which is always a failure. The closest equivalent to
40 `std::error_code`, except it cannot be modified, and is templated.
41
42 Differences from `status_code`:
43
44 - Never successful (this contract is checked on construction, if fails then it
45 terminates the process).
46 - Is immutable.
47 */
48 template <class DomainType> class errored_status_code : public status_code<DomainType>
49 {
50 using _base = status_code<DomainType>;
51 using _base::clear;
52 using _base::success;
53
_check()54 void _check()
55 {
56 if(_base::success())
57 {
58 std::terminate();
59 }
60 }
61
62 public:
63 //! The type of the erased error code.
64 using typename _base::value_type;
65 //! The type of a reference to a message string.
66 using typename _base::string_ref;
67
68 //! Default constructor.
69 errored_status_code() = default;
70 //! Copy constructor.
71 errored_status_code(const errored_status_code &) = default;
72 //! Move constructor.
73 errored_status_code(errored_status_code &&) = default; // NOLINT
74 //! Copy assignment.
75 errored_status_code &operator=(const errored_status_code &) = default;
76 //! Move assignment.
77 errored_status_code &operator=(errored_status_code &&) = default; // NOLINT
78 ~errored_status_code() = default;
79
80 //! Explicitly construct from any similarly erased status code
errored_status_code(const _base & o)81 explicit errored_status_code(const _base &o) noexcept(std::is_nothrow_copy_constructible<_base>::value)
82 : _base(o)
83 {
84 _check();
85 }
86 //! Explicitly construct from any similarly erased status code
errored_status_code(_base && o)87 explicit errored_status_code(_base &&o) noexcept(std::is_nothrow_move_constructible<_base>::value)
88 : _base(static_cast<_base &&>(o))
89 {
90 _check();
91 }
92
93 /***** KEEP THESE IN SYNC WITH STATUS_CODE *****/
94 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
95 template <class T, class... Args, //
96 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<T, Args...>::type, // Safe ADL lookup of make_status_code(), returns void if not found
97 typename std::enable_if<!std::is_same<typename std::decay<T>::type, errored_status_code>::value // not copy/move of self
98 && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
99 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
100 && std::is_constructible<errored_status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
101 bool>::type = true>
errored_status_code(T && v,Args &&...args)102 errored_status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
103 : errored_status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
104 {
105 _check();
106 }
107 //! Implicit construction from any `quick_status_code_from_enum<Enum>` enumerated type.
108 template <class Enum, //
109 class QuickStatusCodeType = typename quick_status_code_from_enum<Enum>::code_type, // Enumeration has been activated
110 typename std::enable_if<std::is_constructible<errored_status_code, QuickStatusCodeType>::value, // Its status code is compatible
111
112 bool>::type = true>
errored_status_code(Enum && v)113 errored_status_code(Enum &&v) noexcept(std::is_nothrow_constructible<errored_status_code, QuickStatusCodeType>::value) // NOLINT
114 : errored_status_code(QuickStatusCodeType(static_cast<Enum &&>(v)))
115 {
116 _check();
117 }
118 //! Explicit in-place construction.
119 template <class... Args>
errored_status_code(in_place_t _,Args &&...args)120 explicit errored_status_code(in_place_t _, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
121 : _base(_, static_cast<Args &&>(args)...)
122 {
123 _check();
124 }
125 //! Explicit in-place construction from initialiser list.
126 template <class T, class... Args>
errored_status_code(in_place_t _,std::initializer_list<T> il,Args &&...args)127 explicit errored_status_code(in_place_t _, std::initializer_list<T> il, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, std::initializer_list<T>, Args &&...>::value)
128 : _base(_, il, static_cast<Args &&>(args)...)
129 {
130 _check();
131 }
132 //! Explicit copy construction from a `value_type`.
errored_status_code(const value_type & v)133 explicit errored_status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
134 : _base(v)
135 {
136 _check();
137 }
138 //! Explicit move construction from a `value_type`.
errored_status_code(value_type && v)139 explicit errored_status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
140 : _base(static_cast<value_type &&>(v))
141 {
142 _check();
143 }
144 /*! Explicit construction from an erased status code. Available only if
145 `value_type` is trivially destructible and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
146 Does not check if domains are equal.
147 */
148 template <class ErasedType, //
149 typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
errored_status_code(const status_code<erased<ErasedType>> & v)150 explicit errored_status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
151 : errored_status_code(detail::erasure_cast<value_type>(v.value())) // NOLINT
152 {
153 assert(v.domain() == this->domain()); // NOLINT
154 _check();
155 }
156
157 //! Return a const reference to the `value_type`.
value() const158 constexpr const value_type &value() const &noexcept { return this->_value; }
159 };
160
161 namespace traits
162 {
163 template <class DomainType> struct is_move_bitcopying<errored_status_code<DomainType>>
164 {
165 static constexpr bool value = is_move_bitcopying<typename DomainType::value_type>::value;
166 };
167 } // namespace traits
168
169 template <class ErasedType> class errored_status_code<erased<ErasedType>> : public status_code<erased<ErasedType>>
170 {
171 using _base = status_code<erased<ErasedType>>;
172 using _base::success;
173
_check()174 void _check()
175 {
176 if(_base::success())
177 {
178 std::terminate();
179 }
180 }
181
182 public:
183 using value_type = typename _base::value_type;
184 using string_ref = typename _base::string_ref;
185
186 //! Default construction to empty
187 errored_status_code() = default;
188 //! Copy constructor
189 errored_status_code(const errored_status_code &) = default;
190 //! Move constructor
191 errored_status_code(errored_status_code &&) = default; // NOLINT
192 //! Copy assignment
193 errored_status_code &operator=(const errored_status_code &) = default;
194 //! Move assignment
195 errored_status_code &operator=(errored_status_code &&) = default; // NOLINT
196 ~errored_status_code() = default;
197
198 //! Explicitly construct from any similarly erased status code
errored_status_code(const _base & o)199 explicit errored_status_code(const _base &o) noexcept(std::is_nothrow_copy_constructible<_base>::value)
200 : _base(o)
201 {
202 _check();
203 }
204 //! Explicitly construct from any similarly erased status code
errored_status_code(_base && o)205 explicit errored_status_code(_base &&o) noexcept(std::is_nothrow_move_constructible<_base>::value)
206 : _base(static_cast<_base &&>(o))
207 {
208 _check();
209 }
210
211 /***** KEEP THESE IN SYNC WITH STATUS_CODE *****/
212 //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
213 template <class DomainType, //
214 typename std::enable_if<std::is_trivially_copyable<typename DomainType::value_type>::value //
215 && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
216 bool>::type = true>
errored_status_code(const status_code<DomainType> & v)217 errored_status_code(const status_code<DomainType> &v) noexcept
218 : _base(v) // NOLINT
219 {
220 _check();
221 }
222 //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
223 template <class DomainType, //
224 typename std::enable_if<std::is_trivially_copyable<typename DomainType::value_type>::value //
225 && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
226 bool>::type = true>
errored_status_code(const errored_status_code<DomainType> & v)227 errored_status_code(const errored_status_code<DomainType> &v) noexcept
228 : _base(static_cast<const status_code<DomainType> &>(v)) // NOLINT
229 {
230 _check();
231 }
232 //! Implicit move construction from any other status code if its value type is trivially copyable or move bitcopying and it would fit into our storage
233 template <class DomainType, //
234 typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
235 bool>::type = true>
errored_status_code(status_code<DomainType> && v)236 errored_status_code(status_code<DomainType> &&v) noexcept
237 : _base(static_cast<status_code<DomainType> &&>(v)) // NOLINT
238 {
239 _check();
240 }
241 //! Implicit move construction from any other status code if its value type is trivially copyable or move bitcopying and it would fit into our storage
242 template <class DomainType, //
243 typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
244 bool>::type = true>
errored_status_code(errored_status_code<DomainType> && v)245 errored_status_code(errored_status_code<DomainType> &&v) noexcept
246 : _base(static_cast<status_code<DomainType> &&>(v)) // NOLINT
247 {
248 _check();
249 }
250 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
251 template <class T, class... Args, //
252 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<T, Args...>::type, // Safe ADL lookup of make_status_code(), returns void if not found
253 typename std::enable_if<!std::is_same<typename std::decay<T>::type, errored_status_code>::value // not copy/move of self
254 && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
255 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
256 && std::is_constructible<errored_status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
257 bool>::type = true>
errored_status_code(T && v,Args &&...args)258 errored_status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
259 : errored_status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
260 {
261 _check();
262 }
263 //! Implicit construction from any `quick_status_code_from_enum<Enum>` enumerated type.
264 template <class Enum, //
265 class QuickStatusCodeType = typename quick_status_code_from_enum<Enum>::code_type, // Enumeration has been activated
266 typename std::enable_if<std::is_constructible<errored_status_code, QuickStatusCodeType>::value, // Its status code is compatible
267
268 bool>::type = true>
errored_status_code(Enum && v)269 errored_status_code(Enum &&v) noexcept(std::is_nothrow_constructible<errored_status_code, QuickStatusCodeType>::value) // NOLINT
270 : errored_status_code(QuickStatusCodeType(static_cast<Enum &&>(v)))
271 {
272 _check();
273 }
274 //! Return the erased `value_type` by value.
value() const275 constexpr value_type value() const noexcept { return this->_value; }
276 };
277
278 namespace traits
279 {
280 template <class ErasedType> struct is_move_bitcopying<errored_status_code<erased<ErasedType>>>
281 {
282 static constexpr bool value = true;
283 };
284 } // namespace traits
285
286
287 //! True if the status code's are semantically equal via `equivalent()`.
operator ==(const errored_status_code<DomainType1> & a,const errored_status_code<DomainType2> & b)288 template <class DomainType1, class DomainType2> inline bool operator==(const errored_status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
289 {
290 return a.equivalent(static_cast<const status_code<DomainType2> &>(b));
291 }
292 //! True if the status code's are semantically equal via `equivalent()`.
operator ==(const status_code<DomainType1> & a,const errored_status_code<DomainType2> & b)293 template <class DomainType1, class DomainType2> inline bool operator==(const status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
294 {
295 return a.equivalent(static_cast<const status_code<DomainType2> &>(b));
296 }
297 //! True if the status code's are semantically equal via `equivalent()`.
operator ==(const errored_status_code<DomainType1> & a,const status_code<DomainType2> & b)298 template <class DomainType1, class DomainType2> inline bool operator==(const errored_status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
299 {
300 return static_cast<const status_code<DomainType1> &>(a).equivalent(b);
301 }
302 //! True if the status code's are not semantically equal via `equivalent()`.
operator !=(const errored_status_code<DomainType1> & a,const errored_status_code<DomainType2> & b)303 template <class DomainType1, class DomainType2> inline bool operator!=(const errored_status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
304 {
305 return !a.equivalent(static_cast<const status_code<DomainType2> &>(b));
306 }
307 //! True if the status code's are not semantically equal via `equivalent()`.
operator !=(const status_code<DomainType1> & a,const errored_status_code<DomainType2> & b)308 template <class DomainType1, class DomainType2> inline bool operator!=(const status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
309 {
310 return !a.equivalent(static_cast<const status_code<DomainType2> &>(b));
311 }
312 //! True if the status code's are not semantically equal via `equivalent()`.
operator !=(const errored_status_code<DomainType1> & a,const status_code<DomainType2> & b)313 template <class DomainType1, class DomainType2> inline bool operator!=(const errored_status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
314 {
315 return !static_cast<const status_code<DomainType1> &>(a).equivalent(b);
316 }
317 //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
318 template <class DomainType1, class T, //
319 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
320 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
operator ==(const errored_status_code<DomainType1> & a,const T & b)321 inline bool operator==(const errored_status_code<DomainType1> &a, const T &b)
322 {
323 return a.equivalent(make_status_code(b));
324 }
325 //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
326 template <class T, class DomainType1, //
327 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
328 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
operator ==(const T & a,const errored_status_code<DomainType1> & b)329 inline bool operator==(const T &a, const errored_status_code<DomainType1> &b)
330 {
331 return b.equivalent(make_status_code(a));
332 }
333 //! True if the status code's are not semantically equal via `equivalent()` to `make_status_code(T)`.
334 template <class DomainType1, class T, //
335 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
336 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
operator !=(const errored_status_code<DomainType1> & a,const T & b)337 inline bool operator!=(const errored_status_code<DomainType1> &a, const T &b)
338 {
339 return !a.equivalent(make_status_code(b));
340 }
341 //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
342 template <class T, class DomainType1, //
343 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
344 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
operator !=(const T & a,const errored_status_code<DomainType1> & b)345 inline bool operator!=(const T &a, const errored_status_code<DomainType1> &b)
346 {
347 return !b.equivalent(make_status_code(a));
348 }
349 //! True if the status code's are semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(b)`.
350 template <class DomainType1, class T, //
351 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
352 >
operator ==(const errored_status_code<DomainType1> & a,const T & b)353 inline bool operator==(const errored_status_code<DomainType1> &a, const T &b)
354 {
355 return a.equivalent(QuickStatusCodeType(b));
356 }
357 //! True if the status code's are semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(a)`.
358 template <class T, class DomainType1, //
359 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
360 >
operator ==(const T & a,const errored_status_code<DomainType1> & b)361 inline bool operator==(const T &a, const errored_status_code<DomainType1> &b)
362 {
363 return b.equivalent(QuickStatusCodeType(a));
364 }
365 //! True if the status code's are not semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(b)`.
366 template <class DomainType1, class T, //
367 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
368 >
operator !=(const errored_status_code<DomainType1> & a,const T & b)369 inline bool operator!=(const errored_status_code<DomainType1> &a, const T &b)
370 {
371 return !a.equivalent(QuickStatusCodeType(b));
372 }
373 //! True if the status code's are not semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(a)`.
374 template <class T, class DomainType1, //
375 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
376 >
operator !=(const T & a,const errored_status_code<DomainType1> & b)377 inline bool operator!=(const T &a, const errored_status_code<DomainType1> &b)
378 {
379 return !b.equivalent(QuickStatusCodeType(a));
380 }
381
382
383 namespace detail
384 {
385 template <class T> struct is_errored_status_code
386 {
387 static constexpr bool value = false;
388 };
389 template <class T> struct is_errored_status_code<errored_status_code<T>>
390 {
391 static constexpr bool value = true;
392 };
393 template <class T> struct is_erased_errored_status_code
394 {
395 static constexpr bool value = false;
396 };
397 template <class T> struct is_erased_errored_status_code<errored_status_code<erased<T>>>
398 {
399 static constexpr bool value = true;
400 };
401 } // namespace detail
402
403 //! Trait returning true if the type is an errored status code.
404 template <class T> struct is_errored_status_code
405 {
406 static constexpr bool value = detail::is_errored_status_code<typename std::decay<T>::type>::value || detail::is_erased_errored_status_code<typename std::decay<T>::type>::value;
407 };
408
409
410 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
411
412 #endif
413