result.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. #ifndef BOOST_SYSTEM_RESULT_HPP_INCLUDED
  2. #define BOOST_SYSTEM_RESULT_HPP_INCLUDED
  3. // Copyright 2017, 2021, 2022 Peter Dimov.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/system/errc.hpp>
  7. #include <boost/system/system_error.hpp>
  8. #include <boost/system/detail/error_code.hpp>
  9. #include <boost/system/detail/error_category_impl.hpp>
  10. #include <boost/variant2/variant.hpp>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/assert/source_location.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/config.hpp>
  15. #include <type_traits>
  16. #include <utility>
  17. #include <iosfwd>
  18. #include <system_error>
  19. #include <exception>
  20. //
  21. namespace boost
  22. {
  23. namespace system
  24. {
  25. // throw_exception_from_error
  26. #if defined(__GNUC__) && __GNUC__ >= 7 && __GNUC__ <= 8
  27. # pragma GCC diagnostic push
  28. # pragma GCC diagnostic ignored "-Wattributes"
  29. #endif
  30. BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( error_code const & e, boost::source_location const& loc )
  31. {
  32. boost::throw_with_location( system_error( e ), loc );
  33. }
  34. BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( errc::errc_t const & e, boost::source_location const& loc )
  35. {
  36. boost::throw_with_location( system_error( make_error_code( e ) ), loc );
  37. }
  38. BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( std::error_code const & e, boost::source_location const& loc )
  39. {
  40. boost::throw_with_location( std::system_error( e ), loc );
  41. }
  42. BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( std::errc const & e, boost::source_location const& loc )
  43. {
  44. boost::throw_with_location( std::system_error( make_error_code( e ) ), loc );
  45. }
  46. BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( std::exception_ptr const & p, boost::source_location const& loc )
  47. {
  48. if( p )
  49. {
  50. std::rethrow_exception( p );
  51. }
  52. else
  53. {
  54. boost::throw_with_location( std::bad_exception(), loc );
  55. }
  56. }
  57. #if defined(__GNUC__) && __GNUC__ >= 7 && __GNUC__ <= 8
  58. # pragma GCC diagnostic pop
  59. #endif
  60. // in_place_*
  61. using in_place_value_t = variant2::in_place_index_t<0>;
  62. constexpr in_place_value_t in_place_value{};
  63. using in_place_error_t = variant2::in_place_index_t<1>;
  64. constexpr in_place_error_t in_place_error{};
  65. namespace detail
  66. {
  67. template<class T> using remove_cvref = typename std::remove_cv< typename std::remove_reference<T>::type >::type;
  68. template<class... T> using is_errc_t = std::is_same<mp11::mp_list<remove_cvref<T>...>, mp11::mp_list<errc::errc_t>>;
  69. } // namespace detail
  70. // result
  71. template<class T, class E = error_code> class result
  72. {
  73. private:
  74. variant2::variant<T, E> v_;
  75. public:
  76. // constructors
  77. // default
  78. template<class En2 = void, class En = typename std::enable_if<
  79. std::is_void<En2>::value &&
  80. std::is_default_constructible<T>::value
  81. >::type>
  82. constexpr result()
  83. noexcept( std::is_nothrow_default_constructible<T>::value )
  84. : v_( in_place_value )
  85. {
  86. }
  87. // implicit, value
  88. template<class A = T, typename std::enable_if<
  89. std::is_convertible<A, T>::value &&
  90. !(detail::is_errc_t<A>::value && std::is_arithmetic<T>::value) &&
  91. !std::is_constructible<E, A>::value, int>::type = 0>
  92. constexpr result( A&& a )
  93. noexcept( std::is_nothrow_constructible<T, A>::value )
  94. : v_( in_place_value, std::forward<A>(a) )
  95. {
  96. }
  97. // implicit, error
  98. template<class A = E, class = void, typename std::enable_if<
  99. std::is_convertible<A, E>::value &&
  100. !std::is_constructible<T, A>::value, int>::type = 0>
  101. constexpr result( A&& a )
  102. noexcept( std::is_nothrow_constructible<E, A>::value )
  103. : v_( in_place_error, std::forward<A>(a) )
  104. {
  105. }
  106. // explicit, value
  107. template<class... A, class En = typename std::enable_if<
  108. std::is_constructible<T, A...>::value &&
  109. !(detail::is_errc_t<A...>::value && std::is_arithmetic<T>::value) &&
  110. !std::is_constructible<E, A...>::value
  111. >::type>
  112. explicit constexpr result( A&&... a )
  113. noexcept( std::is_nothrow_constructible<T, A...>::value )
  114. : v_( in_place_value, std::forward<A>(a)... )
  115. {
  116. }
  117. // explicit, error
  118. template<class... A, class En2 = void, class En = typename std::enable_if<
  119. !std::is_constructible<T, A...>::value &&
  120. std::is_constructible<E, A...>::value
  121. >::type>
  122. explicit constexpr result( A&&... a )
  123. noexcept( std::is_nothrow_constructible<E, A...>::value )
  124. : v_( in_place_error, std::forward<A>(a)... )
  125. {
  126. }
  127. // tagged, value
  128. template<class... A, class En = typename std::enable_if<
  129. std::is_constructible<T, A...>::value
  130. >::type>
  131. constexpr result( in_place_value_t, A&&... a )
  132. noexcept( std::is_nothrow_constructible<T, A...>::value )
  133. : v_( in_place_value, std::forward<A>(a)... )
  134. {
  135. }
  136. // tagged, error
  137. template<class... A, class En = typename std::enable_if<
  138. std::is_constructible<E, A...>::value
  139. >::type>
  140. constexpr result( in_place_error_t, A&&... a )
  141. noexcept( std::is_nothrow_constructible<E, A...>::value )
  142. : v_( in_place_error, std::forward<A>(a)... )
  143. {
  144. }
  145. // queries
  146. constexpr bool has_value() const noexcept
  147. {
  148. return v_.index() == 0;
  149. }
  150. constexpr bool has_error() const noexcept
  151. {
  152. return v_.index() != 0;
  153. }
  154. constexpr explicit operator bool() const noexcept
  155. {
  156. return v_.index() == 0;
  157. }
  158. // checked value access
  159. #if defined( BOOST_NO_CXX11_REF_QUALIFIERS )
  160. BOOST_CXX14_CONSTEXPR T value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const
  161. {
  162. if( has_value() )
  163. {
  164. return variant2::unsafe_get<0>( v_ );
  165. }
  166. else
  167. {
  168. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  169. }
  170. }
  171. #else
  172. BOOST_CXX14_CONSTEXPR T& value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) &
  173. {
  174. if( has_value() )
  175. {
  176. return variant2::unsafe_get<0>( v_ );
  177. }
  178. else
  179. {
  180. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  181. }
  182. }
  183. BOOST_CXX14_CONSTEXPR T const& value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const&
  184. {
  185. if( has_value() )
  186. {
  187. return variant2::unsafe_get<0>( v_ );
  188. }
  189. else
  190. {
  191. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  192. }
  193. }
  194. template<class U = T>
  195. BOOST_CXX14_CONSTEXPR
  196. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  197. value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) &&
  198. {
  199. return std::move( value( loc ) );
  200. }
  201. template<class U = T>
  202. BOOST_CXX14_CONSTEXPR
  203. typename std::enable_if<!std::is_move_constructible<U>::value, T&&>::type
  204. value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) &&
  205. {
  206. return std::move( value( loc ) );
  207. }
  208. template<class U = T>
  209. BOOST_CXX14_CONSTEXPR
  210. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  211. value() const && = delete;
  212. template<class U = T>
  213. BOOST_CXX14_CONSTEXPR
  214. typename std::enable_if<!std::is_move_constructible<U>::value, T const&&>::type
  215. value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const &&
  216. {
  217. return std::move( value( loc ) );
  218. }
  219. #endif
  220. // unchecked value access
  221. BOOST_CXX14_CONSTEXPR T* operator->() noexcept
  222. {
  223. return variant2::get_if<0>( &v_ );
  224. }
  225. BOOST_CXX14_CONSTEXPR T const* operator->() const noexcept
  226. {
  227. return variant2::get_if<0>( &v_ );
  228. }
  229. #if defined( BOOST_NO_CXX11_REF_QUALIFIERS )
  230. BOOST_CXX14_CONSTEXPR T& operator*() noexcept
  231. {
  232. T* p = operator->();
  233. BOOST_ASSERT( p != 0 );
  234. return *p;
  235. }
  236. BOOST_CXX14_CONSTEXPR T const& operator*() const noexcept
  237. {
  238. T const* p = operator->();
  239. BOOST_ASSERT( p != 0 );
  240. return *p;
  241. }
  242. #else
  243. BOOST_CXX14_CONSTEXPR T& operator*() & noexcept
  244. {
  245. T* p = operator->();
  246. BOOST_ASSERT( p != 0 );
  247. return *p;
  248. }
  249. BOOST_CXX14_CONSTEXPR T const& operator*() const & noexcept
  250. {
  251. T const* p = operator->();
  252. BOOST_ASSERT( p != 0 );
  253. return *p;
  254. }
  255. template<class U = T>
  256. BOOST_CXX14_CONSTEXPR
  257. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  258. operator*() && noexcept(std::is_nothrow_move_constructible<T>::value)
  259. {
  260. return std::move(**this);
  261. }
  262. template<class U = T>
  263. BOOST_CXX14_CONSTEXPR
  264. typename std::enable_if<!std::is_move_constructible<U>::value, T&&>::type
  265. operator*() && noexcept
  266. {
  267. return std::move(**this);
  268. }
  269. template<class U = T>
  270. BOOST_CXX14_CONSTEXPR
  271. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  272. operator*() const && noexcept = delete;
  273. template<class U = T>
  274. BOOST_CXX14_CONSTEXPR
  275. typename std::enable_if<!std::is_move_constructible<U>::value, T const&&>::type
  276. operator*() const && noexcept
  277. {
  278. return std::move(**this);
  279. }
  280. #endif
  281. // error access
  282. constexpr E error() const
  283. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
  284. {
  285. return has_error()? variant2::unsafe_get<1>( v_ ): E();
  286. }
  287. // swap
  288. BOOST_CXX14_CONSTEXPR void swap( result& r )
  289. noexcept( noexcept( v_.swap( r.v_ ) ) )
  290. {
  291. v_.swap( r.v_ );
  292. }
  293. friend BOOST_CXX14_CONSTEXPR void swap( result & r1, result & r2 )
  294. noexcept( noexcept( r1.swap( r2 ) ) )
  295. {
  296. r1.swap( r2 );
  297. }
  298. // equality
  299. friend constexpr bool operator==( result const & r1, result const & r2 )
  300. noexcept( noexcept( r1.v_ == r2.v_ ) )
  301. {
  302. return r1.v_ == r2.v_;
  303. }
  304. friend constexpr bool operator!=( result const & r1, result const & r2 )
  305. noexcept( noexcept( !( r1 == r2 ) ) )
  306. {
  307. return !( r1 == r2 );
  308. }
  309. };
  310. template<class Ch, class Tr, class T, class E> std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, result<T, E> const & r )
  311. {
  312. if( r.has_value() )
  313. {
  314. os << "value:" << *r;
  315. }
  316. else
  317. {
  318. os << "error:" << r.error();
  319. }
  320. return os;
  321. }
  322. // result<void>
  323. template<class E> class result<void, E>
  324. {
  325. private:
  326. variant2::variant<variant2::monostate, E> v_;
  327. public:
  328. // constructors
  329. // default
  330. constexpr result() noexcept
  331. : v_( in_place_value )
  332. {
  333. }
  334. // explicit, error
  335. template<class A, class En = typename std::enable_if<
  336. std::is_constructible<E, A>::value &&
  337. !std::is_convertible<A, E>::value
  338. >::type>
  339. explicit constexpr result( A&& a )
  340. noexcept( std::is_nothrow_constructible<E, A>::value )
  341. : v_( in_place_error, std::forward<A>(a) )
  342. {
  343. }
  344. // implicit, error
  345. template<class A, class En2 = void, class En = typename std::enable_if<
  346. std::is_convertible<A, E>::value
  347. >::type>
  348. constexpr result( A&& a )
  349. noexcept( std::is_nothrow_constructible<E, A>::value )
  350. : v_( in_place_error, std::forward<A>(a) )
  351. {
  352. }
  353. // more than one arg, error
  354. template<class... A, class En2 = void, class En3 = void, class En = typename std::enable_if<
  355. std::is_constructible<E, A...>::value &&
  356. sizeof...(A) >= 2
  357. >::type>
  358. constexpr result( A&&... a )
  359. noexcept( std::is_nothrow_constructible<E, A...>::value )
  360. : v_( in_place_error, std::forward<A>(a)... )
  361. {
  362. }
  363. // tagged, value
  364. constexpr result( in_place_value_t ) noexcept
  365. : v_( in_place_value )
  366. {
  367. }
  368. // tagged, error
  369. template<class... A, class En = typename std::enable_if<
  370. std::is_constructible<E, A...>::value
  371. >::type>
  372. constexpr result( in_place_error_t, A&&... a )
  373. noexcept( std::is_nothrow_constructible<E, A...>::value )
  374. : v_( in_place_error, std::forward<A>(a)... )
  375. {
  376. }
  377. // queries
  378. constexpr bool has_value() const noexcept
  379. {
  380. return v_.index() == 0;
  381. }
  382. constexpr bool has_error() const noexcept
  383. {
  384. return v_.index() != 0;
  385. }
  386. constexpr explicit operator bool() const noexcept
  387. {
  388. return v_.index() == 0;
  389. }
  390. // checked value access
  391. BOOST_CXX14_CONSTEXPR void value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const
  392. {
  393. if( has_value() )
  394. {
  395. }
  396. else
  397. {
  398. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  399. }
  400. }
  401. // unchecked value access
  402. BOOST_CXX14_CONSTEXPR void* operator->() noexcept
  403. {
  404. return variant2::get_if<0>( &v_ );
  405. }
  406. BOOST_CXX14_CONSTEXPR void const* operator->() const noexcept
  407. {
  408. return variant2::get_if<0>( &v_ );
  409. }
  410. BOOST_CXX14_CONSTEXPR void operator*() const noexcept
  411. {
  412. BOOST_ASSERT( has_value() );
  413. }
  414. // error access
  415. constexpr E error() const
  416. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
  417. {
  418. return has_error()? variant2::unsafe_get<1>( v_ ): E();
  419. }
  420. // swap
  421. BOOST_CXX14_CONSTEXPR void swap( result& r )
  422. noexcept( noexcept( v_.swap( r.v_ ) ) )
  423. {
  424. v_.swap( r.v_ );
  425. }
  426. friend BOOST_CXX14_CONSTEXPR void swap( result & r1, result & r2 )
  427. noexcept( noexcept( r1.swap( r2 ) ) )
  428. {
  429. r1.swap( r2 );
  430. }
  431. // equality
  432. friend constexpr bool operator==( result const & r1, result const & r2 )
  433. noexcept( noexcept( r1.v_ == r2.v_ ) )
  434. {
  435. return r1.v_ == r2.v_;
  436. }
  437. friend constexpr bool operator!=( result const & r1, result const & r2 )
  438. noexcept( noexcept( !( r1 == r2 ) ) )
  439. {
  440. return !( r1 == r2 );
  441. }
  442. };
  443. template<class Ch, class Tr, class E> std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, result<void, E> const & r )
  444. {
  445. if( r.has_value() )
  446. {
  447. os << "value:void";
  448. }
  449. else
  450. {
  451. os << "error:" << r.error();
  452. }
  453. return os;
  454. }
  455. } // namespace system
  456. } // namespace boost
  457. #endif // #ifndef BOOST_SYSTEM_RESULT_HPP_INCLUDED