basic_result_storage.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Storage for a very simple basic_result type
  2. (C) 2017-2019 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 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. template <class R, class EC, class NoValuePolicy> //
  52. 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)) //
  53. class basic_result_storage
  54. {
  55. static_assert(trait::type_can_be_used_in_basic_result<R>, "The type R cannot be used in a basic_result");
  56. static_assert(trait::type_can_be_used_in_basic_result<EC>, "The type S cannot be used in a basic_result");
  57. static_assert(std::is_void<EC>::value || std::is_default_constructible<EC>::value, "The type S must be void or default constructible");
  58. friend struct policy::base;
  59. template <class T, class U, class V> //
  60. BOOST_OUTCOME_REQUIRES(trait::type_can_be_used_in_basic_result<T> &&trait::type_can_be_used_in_basic_result<U> && (std::is_void<U>::value || std::is_default_constructible<U>::value)) //
  61. friend class basic_result_storage;
  62. template <class T, class U, class V> friend class basic_result_final;
  63. 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
  64. 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
  65. template <bool value_throws, bool error_throws> struct basic_result_storage_swap;
  66. struct disable_in_place_value_type
  67. {
  68. };
  69. struct disable_in_place_error_type
  70. {
  71. };
  72. protected:
  73. using _value_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_value_type, R>;
  74. using _error_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_error_type, EC>;
  75. #ifdef BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE
  76. detail::value_storage_trivial<_value_type> _state;
  77. #else
  78. detail::value_storage_select_impl<_value_type> _state;
  79. #endif
  80. detail::devoid<_error_type> _error;
  81. public:
  82. // Used by iostream support to access state
  83. detail::value_storage_select_impl<_value_type> &_iostreams_state() { return _state; }
  84. const detail::value_storage_select_impl<_value_type> &_iostreams_state() const { return _state; }
  85. // Hack to work around MSVC bug in /permissive-
  86. detail::value_storage_select_impl<_value_type> &_msvc_nonpermissive_state() { return _state; }
  87. detail::devoid<_error_type> &_msvc_nonpermissive_error() { return _error; }
  88. protected:
  89. basic_result_storage() = default;
  90. basic_result_storage(const basic_result_storage &) = default; // NOLINT
  91. basic_result_storage(basic_result_storage &&) = default; // NOLINT
  92. basic_result_storage &operator=(const basic_result_storage &) = default; // NOLINT
  93. basic_result_storage &operator=(basic_result_storage &&) = default; // NOLINT
  94. ~basic_result_storage() = default;
  95. template <class... Args>
  96. constexpr explicit basic_result_storage(in_place_type_t<_value_type> _, Args &&... args) noexcept(std::is_nothrow_constructible<_value_type, Args...>::value)
  97. : _state{_, static_cast<Args &&>(args)...}
  98. , _error()
  99. {
  100. }
  101. template <class U, class... Args>
  102. 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)
  103. : _state{_, il, static_cast<Args &&>(args)...}
  104. , _error()
  105. {
  106. }
  107. template <class... Args>
  108. constexpr explicit basic_result_storage(in_place_type_t<_error_type> /*unused*/, Args &&... args) noexcept(std::is_nothrow_constructible<_error_type, Args...>::value)
  109. : _state{detail::status_have_error}
  110. , _error(static_cast<Args &&>(args)...)
  111. {
  112. _set_error_is_errno(_state, _error);
  113. }
  114. template <class U, class... Args>
  115. 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)
  116. : _state{detail::status_have_error}
  117. , _error{il, static_cast<Args &&>(args)...}
  118. {
  119. _set_error_is_errno(_state, _error);
  120. }
  121. struct compatible_conversion_tag
  122. {
  123. };
  124. template <class T, class U, class V>
  125. 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)
  126. : _state(o._state)
  127. , _error(o._error)
  128. {
  129. }
  130. template <class T, class V>
  131. 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)
  132. : _state(o._state)
  133. , _error(_error_type{})
  134. {
  135. }
  136. template <class T, class U, class V>
  137. 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)
  138. : _state(static_cast<decltype(o._state) &&>(o._state))
  139. , _error(static_cast<U &&>(o._error))
  140. {
  141. }
  142. template <class T, class V>
  143. constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage<T, void, V> &&o) noexcept(std::is_nothrow_constructible<_value_type, T>::value)
  144. : _state(static_cast<decltype(o._state) &&>(o._state))
  145. , _error(_error_type{})
  146. {
  147. }
  148. struct make_error_code_compatible_conversion_tag
  149. {
  150. };
  151. template <class T, class U, class V>
  152. constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(std::is_nothrow_constructible<_value_type, T>::value &&noexcept(make_error_code(std::declval<U>())))
  153. : _state(o._state)
  154. , _error(make_error_code(o._error))
  155. {
  156. }
  157. template <class T, class U, class V>
  158. constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(std::is_nothrow_constructible<_value_type, T>::value &&noexcept(make_error_code(std::declval<U>())))
  159. : _state(static_cast<decltype(o._state) &&>(o._state))
  160. , _error(make_error_code(static_cast<U &&>(o._error)))
  161. {
  162. }
  163. struct make_exception_ptr_compatible_conversion_tag
  164. {
  165. };
  166. template <class T, class U, class V>
  167. constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(std::is_nothrow_constructible<_value_type, T>::value &&noexcept(make_exception_ptr(std::declval<U>())))
  168. : _state(o._state)
  169. , _error(make_exception_ptr(o._error))
  170. {
  171. }
  172. template <class T, class U, class V>
  173. constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(std::is_nothrow_constructible<_value_type, T>::value &&noexcept(make_exception_ptr(std::declval<U>())))
  174. : _state(static_cast<decltype(o._state) &&>(o._state))
  175. , _error(make_exception_ptr(static_cast<U &&>(o._error)))
  176. {
  177. }
  178. };
  179. // Neither value nor error type can throw during swap
  180. #ifndef BOOST_NO_EXCEPTIONS
  181. template <> struct basic_result_storage_swap<false, false>
  182. #else
  183. template <bool value_throws, bool error_throws> struct basic_result_storage_swap
  184. #endif
  185. {
  186. 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)
  187. {
  188. using std::swap;
  189. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  190. swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  191. }
  192. };
  193. #ifndef BOOST_NO_EXCEPTIONS
  194. // Swap potentially throwing value first
  195. template <> struct basic_result_storage_swap<true, false>
  196. {
  197. 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)
  198. {
  199. using std::swap;
  200. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  201. swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  202. }
  203. };
  204. // Swap potentially throwing error first
  205. template <> struct basic_result_storage_swap<false, true>
  206. {
  207. 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)
  208. {
  209. struct _
  210. {
  211. unsigned &a, &b;
  212. bool all_good{false};
  213. ~_()
  214. {
  215. if(!all_good)
  216. {
  217. // We lost one of the values
  218. a |= status_lost_consistency;
  219. b |= status_lost_consistency;
  220. }
  221. }
  222. } _{a._msvc_nonpermissive_state()._status, b._msvc_nonpermissive_state()._status};
  223. strong_swap(_.all_good, a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  224. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  225. }
  226. };
  227. // Both could throw
  228. template <> struct basic_result_storage_swap<true, true>
  229. {
  230. 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)
  231. {
  232. using std::swap;
  233. // Swap value and status first, if it throws, status will remain unchanged
  234. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  235. bool all_good = false;
  236. try
  237. {
  238. strong_swap(all_good, a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error());
  239. }
  240. catch(...)
  241. {
  242. if(!all_good)
  243. {
  244. a._msvc_nonpermissive_state()._status |= detail::status_lost_consistency;
  245. b._msvc_nonpermissive_state()._status |= detail::status_lost_consistency;
  246. }
  247. else
  248. {
  249. // We may still be able to rescue tis
  250. // First try to put the value and status back
  251. try
  252. {
  253. a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state());
  254. // If that succeeded, continue by rethrowing the exception
  255. }
  256. catch(...)
  257. {
  258. all_good = false;
  259. }
  260. }
  261. if(!all_good)
  262. {
  263. // We are now trapped. The value swapped, the error did not,
  264. // trying to restore the value failed. We now have
  265. // inconsistent result objects. Best we can do is fix up the
  266. // status bits to prevent has_value() == has_error().
  267. auto check = [](basic_result_storage<R, EC, NoValuePolicy> &x) {
  268. bool has_value = (x._state._status & detail::status_have_value) != 0;
  269. bool has_error = (x._state._status & detail::status_have_error) != 0;
  270. bool has_exception = (x._state._status & detail::status_have_exception) != 0;
  271. x._state._status |= detail::status_lost_consistency;
  272. if(has_value == (has_error || has_exception))
  273. {
  274. if(has_value)
  275. {
  276. // We know the value swapped and is now set, so clear error and exception
  277. x._state._status &= ~(detail::status_have_error | detail::status_have_exception);
  278. }
  279. else
  280. {
  281. // We know the value swapped and is now unset, so set error
  282. x._state._status |= detail::status_have_error;
  283. // TODO: Should I default construct reset _error? It's guaranteed default constructible.
  284. }
  285. }
  286. };
  287. check(a);
  288. check(b);
  289. }
  290. throw;
  291. }
  292. }
  293. };
  294. #endif
  295. } // namespace detail
  296. BOOST_OUTCOME_V2_NAMESPACE_END
  297. #endif