basic_result_storage.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Storage for a very simple basic_result type
  2. (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (59 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 State, class E> constexpr inline void _set_error_is_errno(State & /*unused*/, const E & /*unused*/) {}
  34. template <class R, class S, class NoValuePolicy> class basic_result_final;
  35. } // namespace detail
  36. namespace hooks
  37. {
  38. template <class R, class S, class NoValuePolicy> constexpr inline uint16_t spare_storage(const detail::basic_result_final<R, S, NoValuePolicy> *r) noexcept;
  39. template <class R, class S, class NoValuePolicy> constexpr inline void set_spare_storage(detail::basic_result_final<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 <bool value_throws, bool error_throws> struct basic_result_storage_swap;
  48. template <class R, class EC, class NoValuePolicy> //
  49. BOOST_OUTCOME_REQUIRES(trait::type_can_be_used_in_basic_result<R> &&trait::type_can_be_used_in_basic_result<EC> && (std::is_void<EC>::value || std::is_default_constructible<EC>::value)) //
  50. class basic_result_storage
  51. {
  52. static_assert(trait::type_can_be_used_in_basic_result<R>, "The type R cannot be used in a basic_result");
  53. static_assert(trait::type_can_be_used_in_basic_result<EC>, "The type S cannot be used in a basic_result");
  54. static_assert(std::is_void<EC>::value || std::is_default_constructible<EC>::value, "The type S must be void or default constructible");
  55. friend struct policy::base;
  56. template <class T, class U, class V> friend class basic_result_storage;
  57. template <class T, class U, class V> friend class basic_result_final;
  58. template <class T, class U, class V> friend constexpr inline uint16_t hooks::spare_storage(const detail::basic_result_final<T, U, V> *r) noexcept; // NOLINT
  59. template <class T, class U, class V> friend constexpr inline void hooks::set_spare_storage(detail::basic_result_final<T, U, V> *r, uint16_t v) noexcept; // NOLINT
  60. template <bool value_throws, bool error_throws> struct basic_result_storage_swap;
  61. struct disable_in_place_value_type
  62. {
  63. };
  64. struct disable_in_place_error_type
  65. {
  66. };
  67. protected:
  68. using _value_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_value_type, R>;
  69. using _error_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_error_type, EC>;
  70. #ifdef BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE
  71. detail::value_storage_trivial<_value_type> _state;
  72. #else
  73. detail::value_storage_select_impl<_value_type> _state;
  74. #endif
  75. detail::devoid<_error_type> _error;
  76. public:
  77. // Used by iostream support to access state
  78. detail::value_storage_select_impl<_value_type> &_iostreams_state() { return _state; }
  79. const detail::value_storage_select_impl<_value_type> &_iostreams_state() const { return _state; }
  80. // Hack to work around MSVC bug in /permissive-
  81. detail::value_storage_select_impl<_value_type> &_msvc_nonpermissive_state() { return _state; }
  82. detail::devoid<_error_type> &_msvc_nonpermissive_error() { return _error; }
  83. protected:
  84. basic_result_storage() = default;
  85. basic_result_storage(const basic_result_storage &) = default; // NOLINT
  86. basic_result_storage(basic_result_storage &&) = default; // NOLINT
  87. basic_result_storage &operator=(const basic_result_storage &) = default; // NOLINT
  88. basic_result_storage &operator=(basic_result_storage &&) = default; // NOLINT
  89. ~basic_result_storage() = default;
  90. template <class... Args>
  91. constexpr explicit basic_result_storage(in_place_type_t<_value_type> _, Args &&... args) noexcept(std::is_nothrow_constructible<_value_type, Args...>::value)
  92. : _state{_, static_cast<Args &&>(args)...}
  93. , _error()
  94. {
  95. }
  96. template <class U, class... Args>
  97. constexpr basic_result_storage(in_place_type_t<_value_type> _, std::initializer_list<U> il, Args &&... args) noexcept(std::is_nothrow_constructible<_value_type, std::initializer_list<U>, Args...>::value)
  98. : _state{_, il, static_cast<Args &&>(args)...}
  99. , _error()
  100. {
  101. }
  102. template <class... Args>
  103. constexpr explicit basic_result_storage(in_place_type_t<_error_type> /*unused*/, Args &&... args) noexcept(std::is_nothrow_constructible<_error_type, Args...>::value)
  104. : _state{detail::status_have_error}
  105. , _error(static_cast<Args &&>(args)...)
  106. {
  107. _set_error_is_errno(_state, _error);
  108. }
  109. template <class U, class... Args>
  110. constexpr basic_result_storage(in_place_type_t<_error_type> /*unused*/, std::initializer_list<U> il, Args &&... args) noexcept(std::is_nothrow_constructible<_error_type, std::initializer_list<U>, Args...>::value)
  111. : _state{detail::status_have_error}
  112. , _error{il, static_cast<Args &&>(args)...}
  113. {
  114. _set_error_is_errno(_state, _error);
  115. }
  116. struct compatible_conversion_tag
  117. {
  118. };
  119. template <class T, class U, class V>
  120. constexpr basic_result_storage(compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(std::is_nothrow_constructible<_value_type, T>::value &&std::is_nothrow_constructible<_error_type, U>::value)
  121. : _state(o._state)
  122. , _error(o._error)
  123. {
  124. }
  125. template <class T, class V>
  126. constexpr basic_result_storage(compatible_conversion_tag /*unused*/, const basic_result_storage<T, void, V> &o) noexcept(std::is_nothrow_constructible<_value_type, T>::value)
  127. : _state(o._state)
  128. , _error(_error_type{})
  129. {
  130. }
  131. template <class T, class U, class V>
  132. constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(std::is_nothrow_constructible<_value_type, T>::value &&std::is_nothrow_constructible<_error_type, U>::value)
  133. : _state(static_cast<decltype(o._state) &&>(o._state))
  134. , _error(static_cast<U &&>(o._error))
  135. {
  136. }
  137. template <class T, class V>
  138. constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage<T, void, V> &&o) noexcept(std::is_nothrow_constructible<_value_type, T>::value)
  139. : _state(static_cast<decltype(o._state) &&>(o._state))
  140. , _error(_error_type{})
  141. {
  142. }
  143. };
  144. // Neither value nor error type can throw during swap
  145. #ifndef BOOST_NO_EXCEPTIONS
  146. template <> struct basic_result_storage_swap<false, false>
  147. #else
  148. template <bool value_throws, bool error_throws> struct basic_result_storage_swap
  149. #endif
  150. {
  151. template <class R, class EC, class NoValuePolicy> constexpr basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b)
  152. {
  153. using std::swap;
  154. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  155. swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  156. }
  157. };
  158. #ifndef BOOST_NO_EXCEPTIONS
  159. // Swap potentially throwing value first
  160. template <> struct basic_result_storage_swap<true, false>
  161. {
  162. template <class R, class EC, class NoValuePolicy> constexpr basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b)
  163. {
  164. using std::swap;
  165. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  166. swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  167. }
  168. };
  169. // Swap potentially throwing error first
  170. template <> struct basic_result_storage_swap<false, true>
  171. {
  172. template <class R, class EC, class NoValuePolicy> constexpr basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b)
  173. {
  174. using std::swap;
  175. swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  176. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  177. }
  178. };
  179. // Both could throw
  180. template <> struct basic_result_storage_swap<true, true>
  181. {
  182. template <class R, class EC, class NoValuePolicy> basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b)
  183. {
  184. using std::swap;
  185. // Swap value and status first, if it throws, status will remain unchanged
  186. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  187. try
  188. {
  189. swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  190. }
  191. catch(...)
  192. {
  193. // First try to put the value and status back
  194. try
  195. {
  196. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  197. // If that succeeded, continue by rethrowing the exception
  198. }
  199. catch(...)
  200. {
  201. // We are now trapped. The value swapped, the error did not,
  202. // trying to restore the value failed. We now have
  203. // inconsistent result objects. Best we can do is fix up the
  204. // status bits to prevent has_value() == has_error().
  205. auto check = [](basic_result_storage<R, EC, NoValuePolicy> &x) {
  206. bool has_value = (x._state._status & detail::status_have_value) != 0;
  207. bool has_error = (x._state._status & detail::status_have_error) != 0;
  208. bool has_exception = (x._state._status & detail::status_have_exception) != 0;
  209. if(has_value == (has_error || has_exception))
  210. {
  211. if(has_value)
  212. {
  213. // We know the value swapped and is now set, so clear error and exception
  214. x._state._status &= ~(detail::status_have_error | detail::status_have_exception);
  215. }
  216. else
  217. {
  218. // We know the value swapped and is now unset, so set error
  219. x._state._status |= detail::status_have_error;
  220. // TODO: Should I default construct reset _error? It's guaranteed default constructible.
  221. }
  222. }
  223. };
  224. check(a);
  225. check(b);
  226. }
  227. throw;
  228. }
  229. }
  230. };
  231. #endif
  232. } // namespace detail
  233. BOOST_OUTCOME_V2_NAMESPACE_END
  234. #endif