1 /* Proposed SG14 status_code
2 (C) 2018 - 2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License in the accompanying file
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
20 Distributed under the Boost Software License, Version 1.0.
21 (See accompanying file Licence.txt or copy at
22 http://www.boost.org/LICENSE_1_0.txt)
25 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_HPP
26 #define BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_HPP
28 #include "status_code_domain.hpp"
30 #if(__cplusplus >= 201700 || _HAS_CXX17) && !defined(BOOST_OUTCOME_SYSTEM_ERROR2_DISABLE_STD_IN_PLACE)
32 #include <utility> // for in_place
34 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
35 using in_place_t = std::in_place_t;
37 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
41 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
42 //! Aliases `std::in_place_t` if on C++ 17 or later, else defined locally.
45 explicit in_place_t() = default;
47 //! Aliases `std::in_place` if on C++ 17 or later, else defined locally.
48 constexpr in_place_t in_place{};
49 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
52 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
54 //! Namespace for user injected mixins
57 template <class Base, class T> struct mixin : public Base
63 /*! A tag for an erased value type for `status_code<D>`.
64 Available only if `ErasedType` satisfies `traits::is_move_bitcopying<ErasedType>::value`.
66 template <class ErasedType, //
67 typename std::enable_if<traits::is_move_bitcopying<ErasedType>::value, bool>::type = true>
70 using value_type = ErasedType;
75 template <class T> struct is_status_code
77 static constexpr bool value = false;
79 template <class T> struct is_status_code<status_code<T>>
81 static constexpr bool value = true;
83 template <class T> struct is_erased_status_code
85 static constexpr bool value = false;
87 template <class T> struct is_erased_status_code<status_code<erased<T>>>
89 static constexpr bool value = true;
92 // From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4436.pdf
95 template <typename... Ts> struct make_void
99 template <typename... Ts> using void_t = typename make_void<Ts...>::type;
100 template <class...> struct types
104 template <template <class...> class T, class types, class = void> struct test_apply
108 template <template <class...> class T, class... Ts> struct test_apply<T, types<Ts...>, void_t<T<Ts...>>>
110 using type = T<Ts...>;
113 template <template <class...> class T, class... Ts> using test_apply = impl::test_apply<T, impl::types<Ts...>>;
115 template <class T, class... Args> using get_make_status_code_result = decltype(make_status_code(std::declval<T>(), std::declval<Args>()...));
116 template <class... Args> using safe_get_make_status_code_result = test_apply<get_make_status_code_result, Args...>;
117 } // namespace detail
119 //! Trait returning true if the type is a status code.
120 template <class T> struct is_status_code
122 static constexpr bool value = detail::is_status_code<typename std::decay<T>::type>::value || detail::is_erased_status_code<typename std::decay<T>::type>::value;
125 /*! A type erased lightweight status code reflecting empty, success, or failure.
126 Differs from `status_code<erased<>>` by being always available irrespective of
127 the domain's value type, but cannot be copied, moved, nor destructed. Thus one
128 always passes this around by const lvalue reference.
130 template <> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code<void>
132 template <class T> friend class status_code;
135 //! The type of the domain.
136 using domain_type = void;
137 //! The type of the status code.
138 using value_type = void;
139 //! The type of a reference to a message string.
140 using string_ref = typename status_code_domain::string_ref;
143 const status_code_domain *_domain{nullptr};
146 //! No default construction at type erased level
147 status_code() = default;
148 //! No public copying at type erased level
149 status_code(const status_code &) = default;
150 //! No public moving at type erased level
151 status_code(status_code &&) = default;
152 //! No public assignment at type erased level
153 status_code &operator=(const status_code &) = default;
154 //! No public assignment at type erased level
155 status_code &operator=(status_code &&) = default;
156 //! No public destruction at type erased level
157 ~status_code() = default;
159 //! Used to construct a non-empty type erased status code
160 constexpr explicit status_code(const status_code_domain *v) noexcept
165 constexpr const status_code_domain *_domain_ptr() const noexcept { return _domain; }
168 //! Return the status code domain.
169 constexpr const status_code_domain &domain() const noexcept { return *_domain; }
170 //! True if the status code is empty.
171 BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD constexpr bool empty() const noexcept { return _domain == nullptr; }
173 //! Return a reference to a string textually representing a code.
174 string_ref message() const noexcept { return (_domain != nullptr) ? _domain->_do_message(*this) : string_ref("(empty)"); }
175 //! True if code means success.
176 bool success() const noexcept { return (_domain != nullptr) ? !_domain->_do_failure(*this) : false; }
177 //! True if code means failure.
178 bool failure() const noexcept { return (_domain != nullptr) ? _domain->_do_failure(*this) : false; }
179 /*! True if code is strictly (and potentially non-transitively) semantically equivalent to another code in another domain.
180 Note that usually non-semantic i.e. pure value comparison is used when the other status code has the same domain.
181 As `equivalent()` will try mapping to generic code, this usually captures when two codes have the same semantic
182 meaning in `equivalent()`.
184 template <class T> bool strictly_equivalent(const status_code<T> &o) const noexcept
186 if(_domain && o._domain)
188 return _domain->_do_equivalent(*this, o);
190 // If we are both empty, we are equivalent
191 if(!_domain && !o._domain)
193 return true; // NOLINT
195 // Otherwise not equivalent
198 /*! True if code is equivalent, by any means, to another code in another domain (guaranteed transitive).
199 Firstly `strictly_equivalent()` is run in both directions. If neither succeeds, each domain is asked
200 for the equivalent generic code and those are compared.
202 template <class T> inline bool equivalent(const status_code<T> &o) const noexcept;
203 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
204 //! Throw a code as a C++ exception.
205 BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN void throw_exception() const
207 _domain->_do_throw_exception(*this);
208 abort(); // suppress buggy GCC warning
215 template <class DomainType> struct get_domain_value_type
217 using domain_type = DomainType;
218 using value_type = typename domain_type::value_type;
220 template <class ErasedType> struct get_domain_value_type<erased<ErasedType>>
222 using domain_type = status_code_domain;
223 using value_type = ErasedType;
225 template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code_storage : public status_code<void>
227 using _base = status_code<void>;
230 //! The type of the domain.
231 using domain_type = typename get_domain_value_type<DomainType>::domain_type;
232 //! The type of the status code.
233 using value_type = typename get_domain_value_type<DomainType>::value_type;
234 //! The type of a reference to a message string.
235 using string_ref = typename domain_type::string_ref;
238 static_assert(std::is_move_constructible<value_type>::value || std::is_copy_constructible<value_type>::value, "DomainType::value_type is neither move nor copy constructible!");
239 static_assert(!std::is_default_constructible<value_type>::value || std::is_nothrow_default_constructible<value_type>::value, "DomainType::value_type is not nothrow default constructible!");
240 static_assert(!std::is_move_constructible<value_type>::value || std::is_nothrow_move_constructible<value_type>::value, "DomainType::value_type is not nothrow move constructible!");
241 static_assert(std::is_nothrow_destructible<value_type>::value, "DomainType::value_type is not nothrow destructible!");
244 // Replace the type erased implementations with type aware implementations for better codegen
245 //! Return the status code domain.
246 constexpr const domain_type &domain() const noexcept { return *static_cast<const domain_type *>(this->_domain); }
248 //! Reset the code to empty.
249 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept
251 this->_value.~value_type();
252 this->_domain = nullptr;
253 new(&this->_value) value_type();
256 #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
257 //! Return a reference to the `value_type`.
258 constexpr value_type &value() & noexcept { return this->_value; }
259 //! Return a reference to the `value_type`.
260 constexpr value_type &&value() && noexcept { return this->_value; }
262 //! Return a reference to the `value_type`.
263 constexpr const value_type &value() const &noexcept { return this->_value; }
264 //! Return a reference to the `value_type`.
265 constexpr const value_type &&value() const &&noexcept { return this->_value; }
268 status_code_storage() = default;
269 status_code_storage(const status_code_storage &) = default;
270 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage(status_code_storage &&o) noexcept
271 : _base(static_cast<status_code_storage &&>(o))
272 , _value(static_cast<status_code_storage &&>(o)._value)
276 status_code_storage &operator=(const status_code_storage &) = default;
277 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage &operator=(status_code_storage &&o) noexcept
279 this->~status_code_storage();
280 new(this) status_code_storage(static_cast<status_code_storage &&>(o));
283 ~status_code_storage() = default;
286 struct _value_type_constructor
289 template <class... Args>
290 constexpr status_code_storage(_value_type_constructor /*unused*/, const status_code_domain *v, Args &&... args)
292 , _value(static_cast<Args &&>(args)...)
296 } // namespace detail
298 /*! A lightweight, typed, status code reflecting empty, success, or failure.
299 This is the main workhorse of the system_error2 library. Its characteristics reflect the value type
300 set by its domain type, so if that value type is move-only or trivial, so is this.
302 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
303 If it is found, and it generates a status code compatible with this status code, implicit construction
306 You may mix in custom member functions and member function overrides by injecting a specialisation of
307 `mixins::mixin<Base, YourDomainType>`. Your mixin must inherit from `Base`.
309 template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code : public mixins::mixin<detail::status_code_storage<DomainType>, DomainType>
311 template <class T> friend class status_code;
312 using _base = mixins::mixin<detail::status_code_storage<DomainType>, DomainType>;
315 //! The type of the domain.
316 using domain_type = DomainType;
317 //! The type of the status code.
318 using value_type = typename domain_type::value_type;
319 //! The type of a reference to a message string.
320 using string_ref = typename domain_type::string_ref;
323 //! Default construction to empty
324 status_code() = default;
326 status_code(const status_code &) = default;
328 status_code(status_code &&) = default; // NOLINT
330 status_code &operator=(const status_code &) = default;
332 status_code &operator=(status_code &&) = default; // NOLINT
333 ~status_code() = default;
335 //! Return a copy of the code.
336 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code clone() const { return *this; }
338 /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
339 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
340 template <class T, class... Args, //
341 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
342 typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
343 && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
344 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
345 && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
348 constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
349 : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
352 //! Explicit in-place construction.
353 template <class... Args>
354 constexpr explicit status_code(in_place_t /*unused */, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
355 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<Args &&>(args)...)
358 //! Explicit in-place construction from initialiser list.
359 template <class T, class... Args>
360 constexpr explicit status_code(in_place_t /*unused */, std::initializer_list<T> il, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, std::initializer_list<T>, Args &&...>::value)
361 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), il, static_cast<Args &&>(args)...)
364 //! Explicit copy construction from a `value_type`.
365 constexpr explicit status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
366 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), v)
369 //! Explicit move construction from a `value_type`.
370 constexpr explicit status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
371 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<value_type &&>(v))
374 /*! Explicit construction from an erased status code. Available only if
375 `value_type` is trivially copyable or move bitcopying, and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
376 Does not check if domains are equal.
378 template <class ErasedType, //
379 typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
380 constexpr explicit status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
381 : status_code(detail::erasure_cast<value_type>(v.value()))
383 #if __cplusplus >= 201400
384 assert(v.domain() == this->domain());
388 //! Assignment from a `value_type`.
389 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code &operator=(const value_type &v) noexcept(std::is_nothrow_copy_assignable<value_type>::value)
395 //! Return a reference to a string textually representing a code.
396 string_ref message() const noexcept { return this->_domain ? string_ref(this->domain()._do_message(*this)) : string_ref("(empty)"); }
401 template <class DomainType> struct is_move_bitcopying<status_code<DomainType>>
403 static constexpr bool value = is_move_bitcopying<typename DomainType::value_type>::value;
405 } // namespace traits
408 /*! Type erased, move-only status_code, unlike `status_code<void>` which cannot be moved nor destroyed. Available
409 only if `erased<>` is available, which is when the domain's type is trivially
410 copyable or is move relocatable, and if the size of the domain's typed error code is less than or equal to
411 this erased error code. Copy construction is disabled, but if you want a copy call `.clone()`.
413 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
414 If it is found, and it generates a status code compatible with this status code, implicit construction
417 template <class ErasedType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code<erased<ErasedType>> : public mixins::mixin<detail::status_code_storage<erased<ErasedType>>, erased<ErasedType>>
419 template <class T> friend class status_code;
420 using _base = mixins::mixin<detail::status_code_storage<erased<ErasedType>>, erased<ErasedType>>;
423 //! The type of the domain (void, as it is erased).
424 using domain_type = void;
425 //! The type of the erased status code.
426 using value_type = ErasedType;
427 //! The type of a reference to a message string.
428 using string_ref = typename _base::string_ref;
431 //! Default construction to empty
432 status_code() = default;
434 status_code(const status_code &) = delete;
436 status_code(status_code &&) = default; // NOLINT
438 status_code &operator=(const status_code &) = delete;
440 status_code &operator=(status_code &&) = default; // NOLINT
443 if(nullptr != this->_domain)
445 this->_domain->_do_erased_destroy(*this, sizeof(*this));
449 //! Return a copy of the erased code by asking the domain to perform the erased copy.
450 status_code clone() const
452 if(nullptr == this->_domain)
457 this->_domain->_do_erased_copy(x, *this, sizeof(*this));
461 /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
462 //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
463 template <class DomainType, //
464 typename std::enable_if<!detail::is_erased_status_code<status_code<DomainType>>::value //
465 && std::is_trivially_copyable<typename DomainType::value_type>::value //
466 && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
468 constexpr status_code(const status_code<DomainType> &v) noexcept // NOLINT
469 : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
472 //! 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
473 template <class DomainType, //
474 typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value, bool>::type = true>
475 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code(status_code<DomainType> &&v) noexcept // NOLINT
476 : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
480 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
481 template <class T, class... Args, //
482 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
483 typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
484 && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
485 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
486 && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
488 constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
489 : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
493 /**** By rights ought to be removed in any formal standard ****/
494 //! Reset the code to empty.
495 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept { *this = status_code(); }
496 //! Return the erased `value_type` by value.
497 constexpr value_type value() const noexcept { return this->_value; }
502 template <class ErasedType> struct is_move_bitcopying<status_code<erased<ErasedType>>>
504 static constexpr bool value = true;
506 } // namespace traits
508 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END