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
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_relocating<ErasedType>::value`.
66 template <class ErasedType, //
67 typename std::enable_if<traits::is_move_relocating<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
166 //! Return the status code domain.
167 constexpr const status_code_domain &domain() const noexcept { return *_domain; }
168 //! True if the status code is empty.
169 BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD constexpr bool empty() const noexcept { return _domain == nullptr; }
171 //! Return a reference to a string textually representing a code.
172 string_ref message() const noexcept { return (_domain != nullptr) ? _domain->_do_message(*this) : string_ref("(empty)"); }
173 //! True if code means success.
174 bool success() const noexcept { return (_domain != nullptr) ? !_domain->_do_failure(*this) : false; }
175 //! True if code means failure.
176 bool failure() const noexcept { return (_domain != nullptr) ? _domain->_do_failure(*this) : false; }
177 /*! True if code is strictly (and potentially non-transitively) semantically equivalent to another code in another domain.
178 Note that usually non-semantic i.e. pure value comparison is used when the other status code has the same domain.
179 As `equivalent()` will try mapping to generic code, this usually captures when two codes have the same semantic
180 meaning in `equivalent()`.
182 template <class T> bool strictly_equivalent(const status_code<T> &o) const noexcept
184 if(_domain && o._domain)
186 return _domain->_do_equivalent(*this, o);
188 // If we are both empty, we are equivalent
189 if(!_domain && !o._domain)
191 return true; // NOLINT
193 // Otherwise not equivalent
196 /*! True if code is equivalent, by any means, to another code in another domain (guaranteed transitive).
197 Firstly `strictly_equivalent()` is run in both directions. If neither succeeds, each domain is asked
198 for the equivalent generic code and those are compared.
200 template <class T> inline bool equivalent(const status_code<T> &o) const noexcept;
201 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
202 //! Throw a code as a C++ exception.
203 BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN void throw_exception() const { _domain->_do_throw_exception(*this); }
209 template <class DomainType> struct get_domain_value_type
211 using domain_type = DomainType;
212 using value_type = typename domain_type::value_type;
214 template <class ErasedType> struct get_domain_value_type<erased<ErasedType>>
216 using domain_type = status_code_domain;
217 using value_type = ErasedType;
219 template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code_storage : public status_code<void>
221 using _base = status_code<void>;
224 //! The type of the domain.
225 using domain_type = typename get_domain_value_type<DomainType>::domain_type;
226 //! The type of the status code.
227 using value_type = typename get_domain_value_type<DomainType>::value_type;
228 //! The type of a reference to a message string.
229 using string_ref = typename domain_type::string_ref;
232 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!");
233 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!");
234 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!");
235 static_assert(std::is_nothrow_destructible<value_type>::value, "DomainType::value_type is not nothrow destructible!");
238 // Replace the type erased implementations with type aware implementations for better codegen
239 //! Return the status code domain.
240 constexpr const domain_type &domain() const noexcept { return *static_cast<const domain_type *>(this->_domain); }
242 //! Reset the code to empty.
243 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept
245 this->_value.~value_type();
246 this->_domain = nullptr;
247 new(&this->_value) value_type();
250 #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
251 //! Return a reference to the `value_type`.
252 constexpr value_type &value() & noexcept { return this->_value; }
253 //! Return a reference to the `value_type`.
254 constexpr value_type &&value() && noexcept { return this->_value; }
256 //! Return a reference to the `value_type`.
257 constexpr const value_type &value() const &noexcept { return this->_value; }
258 //! Return a reference to the `value_type`.
259 constexpr const value_type &&value() const &&noexcept { return this->_value; }
262 status_code_storage() = default;
263 status_code_storage(const status_code_storage &) = default;
264 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage(status_code_storage &&o) noexcept
265 : _base(static_cast<status_code_storage &&>(o))
266 , _value(static_cast<status_code_storage &&>(o)._value)
270 status_code_storage &operator=(const status_code_storage &) = default;
271 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage &operator=(status_code_storage &&o) noexcept
273 this->~status_code_storage();
274 new(this) status_code_storage(static_cast<status_code_storage &&>(o));
277 ~status_code_storage() = default;
280 struct _value_type_constructor
283 template <class... Args>
284 constexpr status_code_storage(_value_type_constructor /*unused*/, const status_code_domain *v, Args &&... args)
286 , _value(static_cast<Args &&>(args)...)
290 } // namespace detail
292 /*! A lightweight, typed, status code reflecting empty, success, or failure.
293 This is the main workhorse of the system_error2 library. Its characteristics reflect the value type
294 set by its domain type, so if that value type is move-only or trivial, so is this.
296 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
297 If it is found, and it generates a status code compatible with this status code, implicit construction
300 You may mix in custom member functions and member function overrides by injecting a specialisation of
301 `mixins::mixin<Base, YourDomainType>`. Your mixin must inherit from `Base`.
303 template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code : public mixins::mixin<detail::status_code_storage<DomainType>, DomainType>
305 template <class T> friend class status_code;
306 using _base = mixins::mixin<detail::status_code_storage<DomainType>, DomainType>;
309 //! The type of the domain.
310 using domain_type = DomainType;
311 //! The type of the status code.
312 using value_type = typename domain_type::value_type;
313 //! The type of a reference to a message string.
314 using string_ref = typename domain_type::string_ref;
317 //! Default construction to empty
318 status_code() = default;
320 status_code(const status_code &) = default;
322 status_code(status_code &&) = default; // NOLINT
324 status_code &operator=(const status_code &) = default;
326 status_code &operator=(status_code &&) = default; // NOLINT
327 ~status_code() = default;
329 //! Return a copy of the code.
330 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code clone() const { return *this; }
332 /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
333 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
334 template <class T, class... Args, //
335 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
336 typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
337 && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
338 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
339 && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
342 constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
343 : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
346 //! Explicit in-place construction.
347 template <class... Args>
348 constexpr explicit status_code(in_place_t /*unused */, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
349 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<Args &&>(args)...)
352 //! Explicit in-place construction from initialiser list.
353 template <class T, class... Args>
354 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)
355 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), il, static_cast<Args &&>(args)...)
358 //! Explicit copy construction from a `value_type`.
359 constexpr explicit status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
360 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), v)
363 //! Explicit move construction from a `value_type`.
364 constexpr explicit status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
365 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<value_type &&>(v))
368 /*! Explicit construction from an erased status code. Available only if
369 `value_type` is trivially copyable or move relocating, and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
370 Does not check if domains are equal.
372 template <class ErasedType, //
373 typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
374 constexpr explicit status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
375 : status_code(detail::erasure_cast<value_type>(v.value()))
377 #if __cplusplus >= 201400
378 assert(v.domain() == this->domain());
382 //! Assignment from a `value_type`.
383 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code &operator=(const value_type &v) noexcept(std::is_nothrow_copy_assignable<value_type>::value)
389 //! Return a reference to a string textually representing a code.
390 string_ref message() const noexcept { return this->_domain ? string_ref(this->domain()._do_message(*this)) : string_ref("(empty)"); }
395 template <class DomainType> struct is_move_relocating<status_code<DomainType>>
397 static constexpr bool value = is_move_relocating<typename DomainType::value_type>::value;
399 } // namespace traits
402 /*! Type erased, move-only status_code, unlike `status_code<void>` which cannot be moved nor destroyed. Available
403 only if `erased<>` is available, which is when the domain's type is trivially
404 copyable or is move relocatable, and if the size of the domain's typed error code is less than or equal to
405 this erased error code. Copy construction is disabled, but if you want a copy call `.clone()`.
407 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
408 If it is found, and it generates a status code compatible with this status code, implicit construction
411 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>>
413 template <class T> friend class status_code;
414 using _base = mixins::mixin<detail::status_code_storage<erased<ErasedType>>, erased<ErasedType>>;
417 //! The type of the domain (void, as it is erased).
418 using domain_type = void;
419 //! The type of the erased status code.
420 using value_type = ErasedType;
421 //! The type of a reference to a message string.
422 using string_ref = typename _base::string_ref;
425 //! Default construction to empty
426 status_code() = default;
428 status_code(const status_code &) = delete;
430 status_code(status_code &&) = default; // NOLINT
432 status_code &operator=(const status_code &) = delete;
434 status_code &operator=(status_code &&) = default; // NOLINT
437 if(nullptr != this->_domain)
439 this->_domain->_do_erased_destroy(*this, sizeof(*this));
443 //! Return a copy of the erased code by asking the domain to perform the erased copy.
444 status_code clone() const
446 if(nullptr == this->_domain)
451 this->_domain->_do_erased_copy(x, *this, sizeof(*this));
455 /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
456 //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
457 template <class DomainType, //
458 typename std::enable_if<!detail::is_erased_status_code<status_code<DomainType>>::value //
459 && std::is_trivially_copyable<typename DomainType::value_type>::value //
460 && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
462 constexpr status_code(const status_code<DomainType> &v) noexcept // NOLINT
463 : _base(typename _base::_value_type_constructor{}, &v.domain(), detail::erasure_cast<value_type>(v.value()))
466 //! Implicit move construction from any other status code if its value type is trivially copyable or move relocating and it would fit into our storage
467 template <class DomainType, //
468 typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value, bool>::type = true>
469 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code(status_code<DomainType> &&v) noexcept // NOLINT
470 : _base(typename _base::_value_type_constructor{}, &v.domain(), detail::erasure_cast<value_type>(v.value()))
474 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
475 template <class T, class... Args, //
476 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
477 typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
478 && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
479 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
480 && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
482 constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
483 : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
487 /**** By rights ought to be removed in any formal standard ****/
488 //! Reset the code to empty.
489 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept { *this = status_code(); }
490 //! Return the erased `value_type` by value.
491 constexpr value_type value() const noexcept { return this->_value; }
496 template <class ErasedType> struct is_move_relocating<status_code<erased<ErasedType>>>
498 static constexpr bool value = true;
500 } // namespace traits
502 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END