basic_result_storage.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Storage for a very simple basic_result type
  2. (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
  3. File Created: Oct 2017
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_BASIC_RESULT_STORAGE_HPP
  26. #define BOOST_OUTCOME_BASIC_RESULT_STORAGE_HPP
  27. #include "../success_failure.hpp"
  28. #include "../trait.hpp"
  29. #include "value_storage.hpp"
  30. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  31. namespace detail
  32. {
  33. template <class R, class EC, class NoValuePolicy> class basic_result_storage;
  34. } // namespace detail
  35. namespace hooks
  36. {
  37. template <class R, class S, class NoValuePolicy> constexpr inline uint16_t spare_storage(const detail::basic_result_storage<R, S, NoValuePolicy> *r) noexcept;
  38. template <class R, class S, class NoValuePolicy>
  39. constexpr inline void set_spare_storage(detail::basic_result_storage<R, S, NoValuePolicy> *r, uint16_t v) noexcept;
  40. } // namespace hooks
  41. namespace policy
  42. {
  43. struct base;
  44. } // namespace policy
  45. namespace detail
  46. {
  47. template <class R, class EC, class NoValuePolicy> //
  48. class basic_result_storage
  49. {
  50. static_assert(trait::type_can_be_used_in_basic_result<R>, "The type R cannot be used in a basic_result");
  51. static_assert(trait::type_can_be_used_in_basic_result<EC>, "The type S cannot be used in a basic_result");
  52. friend struct policy::base;
  53. template <class T, class U, class V> //
  54. friend class basic_result_storage;
  55. template <class T, class U, class V> friend class basic_result_final;
  56. template <class T, class U, class V>
  57. friend constexpr inline uint16_t hooks::spare_storage(const detail::basic_result_storage<T, U, V> *r) noexcept; // NOLINT
  58. template <class T, class U, class V>
  59. friend constexpr inline void hooks::set_spare_storage(detail::basic_result_storage<T, U, V> *r, uint16_t v) noexcept; // NOLINT
  60. struct disable_in_place_value_type
  61. {
  62. };
  63. struct disable_in_place_error_type
  64. {
  65. };
  66. protected:
  67. using _value_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_value_type, R>;
  68. using _error_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_error_type, EC>;
  69. using _state_type = value_storage_select_impl<_value_type, _error_type>;
  70. #ifdef BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE
  71. value_storage_trivial<_value_type, _error_type> _state;
  72. #else
  73. _state_type _state;
  74. #endif
  75. public:
  76. // Used by iostream support to access state
  77. _state_type &_iostreams_state() { return _state; }
  78. const _state_type &_iostreams_state() const { return _state; }
  79. protected:
  80. basic_result_storage() = default;
  81. basic_result_storage(const basic_result_storage &) = default; // NOLINT
  82. basic_result_storage(basic_result_storage &&) = default; // NOLINT
  83. basic_result_storage &operator=(const basic_result_storage &) = default; // NOLINT
  84. basic_result_storage &operator=(basic_result_storage &&) = default; // NOLINT
  85. ~basic_result_storage() = default;
  86. template <class... Args>
  87. constexpr explicit basic_result_storage(in_place_type_t<_value_type> _,
  88. Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, Args...>)
  89. : _state{_, static_cast<Args &&>(args)...}
  90. {
  91. }
  92. template <class U, class... Args>
  93. constexpr basic_result_storage(in_place_type_t<_value_type> _, std::initializer_list<U> il,
  94. Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, std::initializer_list<U>, Args...>)
  95. : _state{_, il, static_cast<Args &&>(args)...}
  96. {
  97. }
  98. template <class... Args>
  99. constexpr explicit basic_result_storage(in_place_type_t<_error_type> _,
  100. Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, Args...>)
  101. : _state{_, static_cast<Args &&>(args)...}
  102. {
  103. }
  104. template <class U, class... Args>
  105. constexpr basic_result_storage(in_place_type_t<_error_type> _, std::initializer_list<U> il,
  106. Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, std::initializer_list<U>, Args...>)
  107. : _state{_, il, static_cast<Args &&>(args)...}
  108. {
  109. }
  110. struct compatible_conversion_tag
  111. {
  112. };
  113. template <class T, class U, class V>
  114. constexpr basic_result_storage(compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(
  115. detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>)
  116. : _state(o._state)
  117. {
  118. }
  119. template <class T, class U, class V>
  120. constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(
  121. detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>)
  122. : _state(static_cast<decltype(o._state) &&>(o._state))
  123. {
  124. }
  125. struct make_error_code_compatible_conversion_tag
  126. {
  127. };
  128. template <class T, class U, class V>
  129. constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(
  130. detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval<U>())))
  131. : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, o._state._value) :
  132. _state_type(in_place_type<_error_type>, make_error_code(o._state._error)))
  133. {
  134. }
  135. template <class T, class U, class V>
  136. constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(
  137. detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval<U>())))
  138. : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, static_cast<T &&>(o._state._value)) :
  139. _state_type(in_place_type<_error_type>, make_error_code(static_cast<U &&>(o._state._error))))
  140. {
  141. }
  142. struct make_exception_ptr_compatible_conversion_tag
  143. {
  144. };
  145. template <class T, class U, class V>
  146. constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(
  147. detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval<U>())))
  148. : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, o._state._value) :
  149. _state_type(in_place_type<_error_type>, make_exception_ptr(o._state._error)))
  150. {
  151. }
  152. template <class T, class U, class V>
  153. constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(
  154. detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval<U>())))
  155. : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, static_cast<T &&>(o._state._value)) :
  156. _state_type(in_place_type<_error_type>, make_exception_ptr(static_cast<U &&>(o._state._error))))
  157. {
  158. }
  159. };
  160. } // namespace detail
  161. BOOST_OUTCOME_V2_NAMESPACE_END
  162. #endif