result.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. template<class T, class... A> struct is_constructible: std::is_constructible<T, A...> {};
  70. template<class A> struct is_constructible<bool, A>: std::is_convertible<A, bool> {};
  71. template<class A> struct is_constructible<bool const, A>: std::is_convertible<A, bool> {};
  72. } // namespace detail
  73. // result
  74. template<class T, class E = error_code> class result
  75. {
  76. private:
  77. variant2::variant<T, E> v_;
  78. public:
  79. using value_type = T;
  80. using error_type = E;
  81. static constexpr in_place_value_t in_place_value{};
  82. static constexpr in_place_error_t in_place_error{};
  83. public:
  84. // constructors
  85. // default
  86. template<class En2 = void, class En = typename std::enable_if<
  87. std::is_void<En2>::value &&
  88. std::is_default_constructible<T>::value
  89. >::type>
  90. constexpr result()
  91. noexcept( std::is_nothrow_default_constructible<T>::value )
  92. : v_( in_place_value )
  93. {
  94. }
  95. // implicit, value
  96. template<class A = T, typename std::enable_if<
  97. std::is_convertible<A, T>::value &&
  98. !(detail::is_errc_t<A>::value && std::is_arithmetic<T>::value) &&
  99. !std::is_convertible<A, E>::value, int>::type = 0>
  100. constexpr result( A&& a )
  101. noexcept( std::is_nothrow_constructible<T, A>::value )
  102. : v_( in_place_value, std::forward<A>(a) )
  103. {
  104. }
  105. // implicit, error
  106. template<class A = E, class = void, typename std::enable_if<
  107. std::is_convertible<A, E>::value &&
  108. !std::is_convertible<A, T>::value, int>::type = 0>
  109. constexpr result( A&& a )
  110. noexcept( std::is_nothrow_constructible<E, A>::value )
  111. : v_( in_place_error, std::forward<A>(a) )
  112. {
  113. }
  114. // explicit, value
  115. template<class... A, class En = typename std::enable_if<
  116. detail::is_constructible<T, A...>::value &&
  117. !(detail::is_errc_t<A...>::value && std::is_arithmetic<T>::value) &&
  118. !detail::is_constructible<E, A...>::value &&
  119. sizeof...(A) >= 1
  120. >::type>
  121. explicit constexpr result( A&&... a )
  122. noexcept( std::is_nothrow_constructible<T, A...>::value )
  123. : v_( in_place_value, std::forward<A>(a)... )
  124. {
  125. }
  126. // explicit, error
  127. template<class... A, class En2 = void, class En = typename std::enable_if<
  128. !detail::is_constructible<T, A...>::value &&
  129. detail::is_constructible<E, A...>::value &&
  130. sizeof...(A) >= 1
  131. >::type>
  132. explicit constexpr result( A&&... a )
  133. noexcept( std::is_nothrow_constructible<E, A...>::value )
  134. : v_( in_place_error, std::forward<A>(a)... )
  135. {
  136. }
  137. // tagged, value
  138. template<class... A, class En = typename std::enable_if<
  139. std::is_constructible<T, A...>::value
  140. >::type>
  141. constexpr result( in_place_value_t, A&&... a )
  142. noexcept( std::is_nothrow_constructible<T, A...>::value )
  143. : v_( in_place_value, std::forward<A>(a)... )
  144. {
  145. }
  146. // tagged, error
  147. template<class... A, class En = typename std::enable_if<
  148. std::is_constructible<E, A...>::value
  149. >::type>
  150. constexpr result( in_place_error_t, A&&... a )
  151. noexcept( std::is_nothrow_constructible<E, A...>::value )
  152. : v_( in_place_error, std::forward<A>(a)... )
  153. {
  154. }
  155. // converting
  156. template<class T2, class E2, class En = typename std::enable_if<
  157. std::is_convertible<T2, T>::value &&
  158. std::is_convertible<E2, E>::value &&
  159. !std::is_convertible<result<T2, E2> const&, T>::value
  160. >::type>
  161. BOOST_CXX14_CONSTEXPR result( result<T2, E2> const& r2 )
  162. noexcept(
  163. std::is_nothrow_constructible<T, T2 const&>::value &&
  164. std::is_nothrow_constructible<E, E2>::value &&
  165. std::is_nothrow_default_constructible<E2>::value &&
  166. std::is_nothrow_copy_constructible<E2>::value )
  167. : v_( in_place_error, r2.error() )
  168. {
  169. if( r2 )
  170. {
  171. v_.template emplace<0>( *r2 );
  172. }
  173. }
  174. template<class T2, class E2, class En = typename std::enable_if<
  175. std::is_convertible<T2, T>::value &&
  176. std::is_convertible<E2, E>::value &&
  177. !std::is_convertible<result<T2, E2>&&, T>::value
  178. >::type>
  179. BOOST_CXX14_CONSTEXPR result( result<T2, E2>&& r2 )
  180. noexcept(
  181. std::is_nothrow_constructible<T, T2&&>::value &&
  182. std::is_nothrow_constructible<E, E2>::value &&
  183. std::is_nothrow_default_constructible<E2>::value &&
  184. std::is_nothrow_copy_constructible<E2>::value )
  185. : v_( in_place_error, r2.error() )
  186. {
  187. if( r2 )
  188. {
  189. v_.template emplace<0>( std::move( *r2 ) );
  190. }
  191. }
  192. // queries
  193. constexpr bool has_value() const noexcept
  194. {
  195. return v_.index() == 0;
  196. }
  197. constexpr bool has_error() const noexcept
  198. {
  199. return v_.index() == 1;
  200. }
  201. constexpr explicit operator bool() const noexcept
  202. {
  203. return v_.index() == 0;
  204. }
  205. // checked value access
  206. #if defined( BOOST_NO_CXX11_REF_QUALIFIERS )
  207. BOOST_CXX14_CONSTEXPR T value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const
  208. {
  209. if( has_value() )
  210. {
  211. return variant2::unsafe_get<0>( v_ );
  212. }
  213. else
  214. {
  215. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  216. }
  217. }
  218. #else
  219. BOOST_CXX14_CONSTEXPR T& value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) &
  220. {
  221. if( has_value() )
  222. {
  223. return variant2::unsafe_get<0>( v_ );
  224. }
  225. else
  226. {
  227. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  228. }
  229. }
  230. BOOST_CXX14_CONSTEXPR T const& value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const&
  231. {
  232. if( has_value() )
  233. {
  234. return variant2::unsafe_get<0>( v_ );
  235. }
  236. else
  237. {
  238. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  239. }
  240. }
  241. template<class U = T>
  242. BOOST_CXX14_CONSTEXPR
  243. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  244. value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) &&
  245. {
  246. return std::move( value( loc ) );
  247. }
  248. template<class U = T>
  249. BOOST_CXX14_CONSTEXPR
  250. typename std::enable_if<!std::is_move_constructible<U>::value, T&&>::type
  251. value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) &&
  252. {
  253. return std::move( value( loc ) );
  254. }
  255. template<class U = T>
  256. BOOST_CXX14_CONSTEXPR
  257. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  258. value() const && = delete;
  259. template<class U = T>
  260. BOOST_CXX14_CONSTEXPR
  261. typename std::enable_if<!std::is_move_constructible<U>::value, T const&&>::type
  262. value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const &&
  263. {
  264. return std::move( value( loc ) );
  265. }
  266. #endif
  267. // unchecked value access
  268. BOOST_CXX14_CONSTEXPR T* operator->() noexcept
  269. {
  270. return variant2::get_if<0>( &v_ );
  271. }
  272. BOOST_CXX14_CONSTEXPR T const* operator->() const noexcept
  273. {
  274. return variant2::get_if<0>( &v_ );
  275. }
  276. #if defined( BOOST_NO_CXX11_REF_QUALIFIERS )
  277. BOOST_CXX14_CONSTEXPR T& operator*() noexcept
  278. {
  279. T* p = operator->();
  280. BOOST_ASSERT( p != 0 );
  281. return *p;
  282. }
  283. BOOST_CXX14_CONSTEXPR T const& operator*() const noexcept
  284. {
  285. T const* p = operator->();
  286. BOOST_ASSERT( p != 0 );
  287. return *p;
  288. }
  289. #else
  290. BOOST_CXX14_CONSTEXPR T& operator*() & noexcept
  291. {
  292. T* p = operator->();
  293. BOOST_ASSERT( p != 0 );
  294. return *p;
  295. }
  296. BOOST_CXX14_CONSTEXPR T const& operator*() const & noexcept
  297. {
  298. T const* p = operator->();
  299. BOOST_ASSERT( p != 0 );
  300. return *p;
  301. }
  302. template<class U = T>
  303. BOOST_CXX14_CONSTEXPR
  304. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  305. operator*() && noexcept(std::is_nothrow_move_constructible<T>::value)
  306. {
  307. return std::move(**this);
  308. }
  309. template<class U = T>
  310. BOOST_CXX14_CONSTEXPR
  311. typename std::enable_if<!std::is_move_constructible<U>::value, T&&>::type
  312. operator*() && noexcept
  313. {
  314. return std::move(**this);
  315. }
  316. template<class U = T>
  317. BOOST_CXX14_CONSTEXPR
  318. typename std::enable_if<std::is_move_constructible<U>::value, T>::type
  319. operator*() const && noexcept = delete;
  320. template<class U = T>
  321. BOOST_CXX14_CONSTEXPR
  322. typename std::enable_if<!std::is_move_constructible<U>::value, T const&&>::type
  323. operator*() const && noexcept
  324. {
  325. return std::move(**this);
  326. }
  327. #endif
  328. // error access
  329. constexpr E error() const
  330. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
  331. {
  332. return has_error()? variant2::unsafe_get<1>( v_ ): E();
  333. }
  334. // emplace
  335. template<class... A>
  336. BOOST_CXX14_CONSTEXPR T& emplace( A&&... a )
  337. {
  338. return v_.template emplace<0>( std::forward<A>(a)... );
  339. }
  340. // swap
  341. BOOST_CXX14_CONSTEXPR void swap( result& r )
  342. noexcept( noexcept( v_.swap( r.v_ ) ) )
  343. {
  344. v_.swap( r.v_ );
  345. }
  346. friend BOOST_CXX14_CONSTEXPR void swap( result & r1, result & r2 )
  347. noexcept( noexcept( r1.swap( r2 ) ) )
  348. {
  349. r1.swap( r2 );
  350. }
  351. // equality
  352. friend constexpr bool operator==( result const & r1, result const & r2 )
  353. noexcept( noexcept( r1.v_ == r2.v_ ) )
  354. {
  355. return r1.v_ == r2.v_;
  356. }
  357. friend constexpr bool operator!=( result const & r1, result const & r2 )
  358. noexcept( noexcept( !( r1 == r2 ) ) )
  359. {
  360. return !( r1 == r2 );
  361. }
  362. };
  363. 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 )
  364. {
  365. if( r.has_value() )
  366. {
  367. os << "value:" << *r;
  368. }
  369. else
  370. {
  371. os << "error:" << r.error();
  372. }
  373. return os;
  374. }
  375. // result<void>
  376. template<class E> class result<void, E>
  377. {
  378. private:
  379. variant2::variant<variant2::monostate, E> v_;
  380. public:
  381. using value_type = void;
  382. using error_type = E;
  383. static constexpr in_place_value_t in_place_value{};
  384. static constexpr in_place_error_t in_place_error{};
  385. public:
  386. // constructors
  387. // default
  388. constexpr result() noexcept
  389. : v_( in_place_value )
  390. {
  391. }
  392. // explicit, error
  393. template<class A, class En = typename std::enable_if<
  394. std::is_constructible<E, A>::value &&
  395. !std::is_convertible<A, E>::value
  396. >::type>
  397. explicit constexpr result( A&& a )
  398. noexcept( std::is_nothrow_constructible<E, A>::value )
  399. : v_( in_place_error, std::forward<A>(a) )
  400. {
  401. }
  402. // implicit, error
  403. template<class A, class En2 = void, class En = typename std::enable_if<
  404. std::is_convertible<A, E>::value
  405. >::type>
  406. constexpr result( A&& a )
  407. noexcept( std::is_nothrow_constructible<E, A>::value )
  408. : v_( in_place_error, std::forward<A>(a) )
  409. {
  410. }
  411. // more than one arg, error
  412. template<class... A, class En2 = void, class En3 = void, class En = typename std::enable_if<
  413. std::is_constructible<E, A...>::value &&
  414. sizeof...(A) >= 2
  415. >::type>
  416. constexpr result( A&&... a )
  417. noexcept( std::is_nothrow_constructible<E, A...>::value )
  418. : v_( in_place_error, std::forward<A>(a)... )
  419. {
  420. }
  421. // tagged, value
  422. constexpr result( in_place_value_t ) noexcept
  423. : v_( in_place_value )
  424. {
  425. }
  426. // tagged, error
  427. template<class... A, class En = typename std::enable_if<
  428. std::is_constructible<E, A...>::value
  429. >::type>
  430. constexpr result( in_place_error_t, A&&... a )
  431. noexcept( std::is_nothrow_constructible<E, A...>::value )
  432. : v_( in_place_error, std::forward<A>(a)... )
  433. {
  434. }
  435. // queries
  436. constexpr bool has_value() const noexcept
  437. {
  438. return v_.index() == 0;
  439. }
  440. constexpr bool has_error() const noexcept
  441. {
  442. return v_.index() == 1;
  443. }
  444. constexpr explicit operator bool() const noexcept
  445. {
  446. return v_.index() == 0;
  447. }
  448. // checked value access
  449. BOOST_CXX14_CONSTEXPR void value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const
  450. {
  451. if( has_value() )
  452. {
  453. }
  454. else
  455. {
  456. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  457. }
  458. }
  459. // unchecked value access
  460. BOOST_CXX14_CONSTEXPR void* operator->() noexcept
  461. {
  462. return variant2::get_if<0>( &v_ );
  463. }
  464. BOOST_CXX14_CONSTEXPR void const* operator->() const noexcept
  465. {
  466. return variant2::get_if<0>( &v_ );
  467. }
  468. BOOST_CXX14_CONSTEXPR void operator*() const noexcept
  469. {
  470. BOOST_ASSERT( has_value() );
  471. }
  472. // error access
  473. constexpr E error() const
  474. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
  475. {
  476. return has_error()? variant2::unsafe_get<1>( v_ ): E();
  477. }
  478. // emplace
  479. BOOST_CXX14_CONSTEXPR void emplace()
  480. {
  481. v_.template emplace<0>();
  482. }
  483. // swap
  484. BOOST_CXX14_CONSTEXPR void swap( result& r )
  485. noexcept( noexcept( v_.swap( r.v_ ) ) )
  486. {
  487. v_.swap( r.v_ );
  488. }
  489. friend BOOST_CXX14_CONSTEXPR void swap( result & r1, result & r2 )
  490. noexcept( noexcept( r1.swap( r2 ) ) )
  491. {
  492. r1.swap( r2 );
  493. }
  494. // equality
  495. friend constexpr bool operator==( result const & r1, result const & r2 )
  496. noexcept( noexcept( r1.v_ == r2.v_ ) )
  497. {
  498. return r1.v_ == r2.v_;
  499. }
  500. friend constexpr bool operator!=( result const & r1, result const & r2 )
  501. noexcept( noexcept( !( r1 == r2 ) ) )
  502. {
  503. return !( r1 == r2 );
  504. }
  505. };
  506. template<class Ch, class Tr, class E> std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, result<void, E> const & r )
  507. {
  508. if( r.has_value() )
  509. {
  510. os << "value:void";
  511. }
  512. else
  513. {
  514. os << "error:" << r.error();
  515. }
  516. return os;
  517. }
  518. } // namespace system
  519. } // namespace boost
  520. #endif // #ifndef BOOST_SYSTEM_RESULT_HPP_INCLUDED