result.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. BOOST_INLINE_CONSTEXPR in_place_value_t in_place_value{};
  63. using in_place_error_t = variant2::in_place_index_t<1>;
  64. BOOST_INLINE_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. BOOST_CXX14_CONSTEXPR E error() &&
  335. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_move_constructible<E>::value )
  336. {
  337. return has_error()? std::move( variant2::unsafe_get<1>( v_ ) ): E();
  338. }
  339. // emplace
  340. template<class... A>
  341. BOOST_CXX14_CONSTEXPR T& emplace( A&&... a )
  342. {
  343. return v_.template emplace<0>( std::forward<A>(a)... );
  344. }
  345. // swap
  346. BOOST_CXX14_CONSTEXPR void swap( result& r )
  347. noexcept( noexcept( v_.swap( r.v_ ) ) )
  348. {
  349. v_.swap( r.v_ );
  350. }
  351. friend BOOST_CXX14_CONSTEXPR void swap( result & r1, result & r2 )
  352. noexcept( noexcept( r1.swap( r2 ) ) )
  353. {
  354. r1.swap( r2 );
  355. }
  356. // equality
  357. friend constexpr bool operator==( result const & r1, result const & r2 )
  358. noexcept( noexcept( r1.v_ == r2.v_ ) )
  359. {
  360. return r1.v_ == r2.v_;
  361. }
  362. friend constexpr bool operator!=( result const & r1, result const & r2 )
  363. noexcept( noexcept( !( r1 == r2 ) ) )
  364. {
  365. return !( r1 == r2 );
  366. }
  367. };
  368. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  369. template<class T, class E> constexpr in_place_value_t result<T, E>::in_place_value;
  370. template<class T, class E> constexpr in_place_error_t result<T, E>::in_place_error;
  371. #endif
  372. 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 )
  373. {
  374. if( r.has_value() )
  375. {
  376. os << "value:" << *r;
  377. }
  378. else
  379. {
  380. os << "error:" << r.error();
  381. }
  382. return os;
  383. }
  384. // result<void>
  385. template<class E> class result<void, E>
  386. {
  387. private:
  388. variant2::variant<variant2::monostate, E> v_;
  389. public:
  390. using value_type = void;
  391. using error_type = E;
  392. static constexpr in_place_value_t in_place_value{};
  393. static constexpr in_place_error_t in_place_error{};
  394. public:
  395. // constructors
  396. // default
  397. constexpr result() noexcept
  398. : v_( in_place_value )
  399. {
  400. }
  401. // explicit, error
  402. template<class A, class En = typename std::enable_if<
  403. std::is_constructible<E, A>::value &&
  404. !std::is_convertible<A, E>::value
  405. >::type>
  406. explicit 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. // implicit, error
  412. template<class A, class En2 = void, class En = typename std::enable_if<
  413. std::is_convertible<A, E>::value
  414. >::type>
  415. constexpr result( A&& a )
  416. noexcept( std::is_nothrow_constructible<E, A>::value )
  417. : v_( in_place_error, std::forward<A>(a) )
  418. {
  419. }
  420. // more than one arg, error
  421. template<class... A, class En2 = void, class En3 = void, class En = typename std::enable_if<
  422. std::is_constructible<E, A...>::value &&
  423. sizeof...(A) >= 2
  424. >::type>
  425. constexpr result( A&&... a )
  426. noexcept( std::is_nothrow_constructible<E, A...>::value )
  427. : v_( in_place_error, std::forward<A>(a)... )
  428. {
  429. }
  430. // tagged, value
  431. constexpr result( in_place_value_t ) noexcept
  432. : v_( in_place_value )
  433. {
  434. }
  435. // tagged, error
  436. template<class... A, class En = typename std::enable_if<
  437. std::is_constructible<E, A...>::value
  438. >::type>
  439. constexpr result( in_place_error_t, A&&... a )
  440. noexcept( std::is_nothrow_constructible<E, A...>::value )
  441. : v_( in_place_error, std::forward<A>(a)... )
  442. {
  443. }
  444. // converting
  445. template<class E2, class En = typename std::enable_if<
  446. std::is_convertible<E2, E>::value
  447. >::type>
  448. BOOST_CXX14_CONSTEXPR result( result<void, E2> const& r2 )
  449. noexcept(
  450. std::is_nothrow_constructible<E, E2>::value &&
  451. std::is_nothrow_default_constructible<E2>::value &&
  452. std::is_nothrow_copy_constructible<E2>::value )
  453. : v_( in_place_error, r2.error() )
  454. {
  455. if( r2 )
  456. {
  457. this->emplace();
  458. }
  459. }
  460. // queries
  461. constexpr bool has_value() const noexcept
  462. {
  463. return v_.index() == 0;
  464. }
  465. constexpr bool has_error() const noexcept
  466. {
  467. return v_.index() == 1;
  468. }
  469. constexpr explicit operator bool() const noexcept
  470. {
  471. return v_.index() == 0;
  472. }
  473. // checked value access
  474. BOOST_CXX14_CONSTEXPR void value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const
  475. {
  476. if( has_value() )
  477. {
  478. }
  479. else
  480. {
  481. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  482. }
  483. }
  484. // unchecked value access
  485. BOOST_CXX14_CONSTEXPR void* operator->() noexcept
  486. {
  487. return variant2::get_if<0>( &v_ );
  488. }
  489. BOOST_CXX14_CONSTEXPR void const* operator->() const noexcept
  490. {
  491. return variant2::get_if<0>( &v_ );
  492. }
  493. BOOST_CXX14_CONSTEXPR void operator*() const noexcept
  494. {
  495. BOOST_ASSERT( has_value() );
  496. }
  497. // error access
  498. constexpr E error() const &
  499. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
  500. {
  501. return has_error()? variant2::unsafe_get<1>( v_ ): E();
  502. }
  503. BOOST_CXX14_CONSTEXPR E error() &&
  504. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_move_constructible<E>::value )
  505. {
  506. return has_error()? std::move( variant2::unsafe_get<1>( v_ ) ): E();
  507. }
  508. // emplace
  509. BOOST_CXX14_CONSTEXPR void emplace()
  510. {
  511. v_.template emplace<0>();
  512. }
  513. // swap
  514. BOOST_CXX14_CONSTEXPR void swap( result& r )
  515. noexcept( noexcept( v_.swap( r.v_ ) ) )
  516. {
  517. v_.swap( r.v_ );
  518. }
  519. friend BOOST_CXX14_CONSTEXPR void swap( result & r1, result & r2 )
  520. noexcept( noexcept( r1.swap( r2 ) ) )
  521. {
  522. r1.swap( r2 );
  523. }
  524. // equality
  525. friend constexpr bool operator==( result const & r1, result const & r2 )
  526. noexcept( noexcept( r1.v_ == r2.v_ ) )
  527. {
  528. return r1.v_ == r2.v_;
  529. }
  530. friend constexpr bool operator!=( result const & r1, result const & r2 )
  531. noexcept( noexcept( !( r1 == r2 ) ) )
  532. {
  533. return !( r1 == r2 );
  534. }
  535. };
  536. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  537. template<class E> constexpr in_place_value_t result<void, E>::in_place_value;
  538. template<class E> constexpr in_place_error_t result<void, E>::in_place_error;
  539. #endif
  540. template<class Ch, class Tr, class E> std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, result<void, E> const & r )
  541. {
  542. if( r.has_value() )
  543. {
  544. os << "value:void";
  545. }
  546. else
  547. {
  548. os << "error:" << r.error();
  549. }
  550. return os;
  551. }
  552. // result<T&, E>
  553. namespace detail
  554. {
  555. template<class U, class A> struct reference_to_temporary: std::integral_constant<bool,
  556. !std::is_reference<A>::value ||
  557. !std::is_convertible<typename std::remove_reference<A>::type*, U*>::value
  558. > {};
  559. } // namespace detail
  560. template<class U, class E> class result<U&, E>
  561. {
  562. private:
  563. variant2::variant<U*, E> v_;
  564. public:
  565. using value_type = U&;
  566. using error_type = E;
  567. static constexpr in_place_value_t in_place_value{};
  568. static constexpr in_place_error_t in_place_error{};
  569. public:
  570. // constructors
  571. // implicit, value
  572. template<class A, typename std::enable_if<
  573. std::is_convertible<A, U&>::value &&
  574. !detail::reference_to_temporary<U, A>::value &&
  575. !std::is_convertible<A, E>::value, int>::type = 0>
  576. constexpr result( A&& a )
  577. noexcept( std::is_nothrow_constructible<U&, A>::value )
  578. : v_( in_place_value, &static_cast<U&>( std::forward<A>(a) ) )
  579. {
  580. }
  581. // implicit, error
  582. template<class A = E, class = void, typename std::enable_if<
  583. std::is_convertible<A, E>::value &&
  584. !std::is_convertible<A, U&>::value, int>::type = 0>
  585. constexpr result( A&& a )
  586. noexcept( std::is_nothrow_constructible<E, A>::value )
  587. : v_( in_place_error, std::forward<A>(a) )
  588. {
  589. }
  590. // explicit, value
  591. template<class A, class En = typename std::enable_if<
  592. detail::is_constructible<U&, A>::value &&
  593. !std::is_convertible<A, U&>::value &&
  594. !detail::reference_to_temporary<U, A>::value &&
  595. !detail::is_constructible<E, A>::value
  596. >::type>
  597. explicit constexpr result( A&& a )
  598. noexcept( std::is_nothrow_constructible<U&, A>::value )
  599. : v_( in_place_value, &static_cast<U&>( std::forward<A>(a) ) )
  600. {
  601. }
  602. // explicit, error
  603. template<class... A, class En2 = void, class En = typename std::enable_if<
  604. !detail::is_constructible<U&, A...>::value &&
  605. detail::is_constructible<E, A...>::value &&
  606. sizeof...(A) >= 1
  607. >::type>
  608. explicit constexpr result( A&&... a )
  609. noexcept( std::is_nothrow_constructible<E, A...>::value )
  610. : v_( in_place_error, std::forward<A>(a)... )
  611. {
  612. }
  613. // tagged, value
  614. template<class A, class En = typename std::enable_if<
  615. std::is_constructible<U&, A>::value &&
  616. !detail::reference_to_temporary<U, A>::value
  617. >::type>
  618. constexpr result( in_place_value_t, A&& a )
  619. noexcept( std::is_nothrow_constructible<U&, A>::value )
  620. : v_( in_place_value, &static_cast<U&>( std::forward<A>(a) ) )
  621. {
  622. }
  623. // tagged, error
  624. template<class... A, class En = typename std::enable_if<
  625. std::is_constructible<E, A...>::value
  626. >::type>
  627. constexpr result( in_place_error_t, A&&... a )
  628. noexcept( std::is_nothrow_constructible<E, A...>::value )
  629. : v_( in_place_error, std::forward<A>(a)... )
  630. {
  631. }
  632. // converting
  633. template<class U2, class E2, class En = typename std::enable_if<
  634. std::is_convertible<U2&, U&>::value &&
  635. !detail::reference_to_temporary<U, U2&>::value &&
  636. std::is_convertible<E2, E>::value &&
  637. !std::is_convertible<result<U2&, E2> const&, U&>::value
  638. >::type>
  639. BOOST_CXX14_CONSTEXPR result( result<U2&, E2> const& r2 )
  640. noexcept(
  641. std::is_nothrow_constructible<U&, U2&>::value &&
  642. std::is_nothrow_constructible<E, E2>::value &&
  643. std::is_nothrow_default_constructible<E2>::value &&
  644. std::is_nothrow_copy_constructible<E2>::value )
  645. : v_( in_place_error, r2.error() )
  646. {
  647. if( r2 )
  648. {
  649. this->emplace( *r2 );
  650. }
  651. }
  652. // queries
  653. constexpr bool has_value() const noexcept
  654. {
  655. return v_.index() == 0;
  656. }
  657. constexpr bool has_error() const noexcept
  658. {
  659. return v_.index() == 1;
  660. }
  661. constexpr explicit operator bool() const noexcept
  662. {
  663. return v_.index() == 0;
  664. }
  665. // checked value access
  666. BOOST_CXX14_CONSTEXPR U& value( boost::source_location const& loc = BOOST_CURRENT_LOCATION ) const
  667. {
  668. if( has_value() )
  669. {
  670. return *variant2::unsafe_get<0>( v_ );
  671. }
  672. else
  673. {
  674. throw_exception_from_error( variant2::unsafe_get<1>( v_ ), loc );
  675. }
  676. }
  677. // unchecked value access
  678. BOOST_CXX14_CONSTEXPR U* operator->() const noexcept
  679. {
  680. return has_value()? variant2::unsafe_get<0>( v_ ): 0;
  681. }
  682. BOOST_CXX14_CONSTEXPR U& operator*() const noexcept
  683. {
  684. U* p = operator->();
  685. BOOST_ASSERT( p != 0 );
  686. return *p;
  687. }
  688. // error access
  689. constexpr E error() const &
  690. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
  691. {
  692. return has_error()? variant2::unsafe_get<1>( v_ ): E();
  693. }
  694. BOOST_CXX14_CONSTEXPR E error() &&
  695. noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_move_constructible<E>::value )
  696. {
  697. return has_error()? std::move( variant2::unsafe_get<1>( v_ ) ): E();
  698. }
  699. // emplace
  700. template<class A, class En = typename std::enable_if<
  701. detail::is_constructible<U&, A>::value &&
  702. !detail::reference_to_temporary<U, A>::value
  703. >::type>
  704. BOOST_CXX14_CONSTEXPR U& emplace( A&& a )
  705. {
  706. return *v_.template emplace<0>( &static_cast<U&>( a ) );
  707. }
  708. // swap
  709. BOOST_CXX14_CONSTEXPR void swap( result& r )
  710. noexcept( noexcept( v_.swap( r.v_ ) ) )
  711. {
  712. v_.swap( r.v_ );
  713. }
  714. friend BOOST_CXX14_CONSTEXPR void swap( result & r1, result & r2 )
  715. noexcept( noexcept( r1.swap( r2 ) ) )
  716. {
  717. r1.swap( r2 );
  718. }
  719. // equality
  720. friend constexpr bool operator==( result const & r1, result const & r2 )
  721. noexcept( noexcept( r1 && r2? *r1 == *r2: r1.v_ == r2.v_ ) )
  722. {
  723. return r1 && r2? *r1 == *r2: r1.v_ == r2.v_;
  724. }
  725. friend constexpr bool operator!=( result const & r1, result const & r2 )
  726. noexcept( noexcept( !( r1 == r2 ) ) )
  727. {
  728. return !( r1 == r2 );
  729. }
  730. };
  731. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  732. template<class U, class E> constexpr in_place_value_t result<U&, E>::in_place_value;
  733. template<class U, class E> constexpr in_place_error_t result<U&, E>::in_place_error;
  734. #endif
  735. // operator|
  736. namespace detail
  737. {
  738. // is_value_convertible_to
  739. template<class T, class U> struct is_value_convertible_to: std::is_convertible<T, U>
  740. {
  741. };
  742. template<class T, class U> struct is_value_convertible_to<T, U&>:
  743. std::integral_constant<bool,
  744. std::is_lvalue_reference<T>::value &&
  745. std::is_convertible<typename std::remove_reference<T>::type*, U*>::value>
  746. {
  747. };
  748. // is_result
  749. template<class T> struct is_result: std::false_type {};
  750. template<class T, class E> struct is_result< result<T, E> >: std::true_type {};
  751. } // namespace detail
  752. // result | value
  753. template<class T, class E, class U,
  754. class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
  755. >
  756. T operator|( result<T, E> const& r, U&& u )
  757. {
  758. if( r )
  759. {
  760. return *r;
  761. }
  762. else
  763. {
  764. return std::forward<U>( u );
  765. }
  766. }
  767. template<class T, class E, class U,
  768. class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
  769. >
  770. T operator|( result<T, E>&& r, U&& u )
  771. {
  772. if( r )
  773. {
  774. return *std::move( r );
  775. }
  776. else
  777. {
  778. return std::forward<U>( u );
  779. }
  780. }
  781. // result | nullary-returning-value
  782. template<class T, class E, class F,
  783. class U = decltype( std::declval<F>()() ),
  784. class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
  785. >
  786. T operator|( result<T, E> const& r, F&& f )
  787. {
  788. if( r )
  789. {
  790. return *r;
  791. }
  792. else
  793. {
  794. return std::forward<F>( f )();
  795. }
  796. }
  797. template<class T, class E, class F,
  798. class U = decltype( std::declval<F>()() ),
  799. class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
  800. >
  801. T operator|( result<T, E>&& r, F&& f )
  802. {
  803. if( r )
  804. {
  805. return *std::move( r );
  806. }
  807. else
  808. {
  809. return std::forward<F>( f )();
  810. }
  811. }
  812. // result | nullary-returning-result
  813. template<class T, class E, class F,
  814. class U = decltype( std::declval<F>()() ),
  815. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  816. class En2 = typename std::enable_if<detail::is_value_convertible_to<T, typename U::value_type>::value>::type
  817. >
  818. U operator|( result<T, E> const& r, F&& f )
  819. {
  820. if( r )
  821. {
  822. return *r;
  823. }
  824. else
  825. {
  826. return std::forward<F>( f )();
  827. }
  828. }
  829. template<class T, class E, class F,
  830. class U = decltype( std::declval<F>()() ),
  831. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  832. class En2 = typename std::enable_if<detail::is_value_convertible_to<T, typename U::value_type>::value>::type
  833. >
  834. U operator|( result<T, E>&& r, F&& f )
  835. {
  836. if( r )
  837. {
  838. return *std::move( r );
  839. }
  840. else
  841. {
  842. return std::forward<F>( f )();
  843. }
  844. }
  845. template<class E, class F,
  846. class U = decltype( std::declval<F>()() ),
  847. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  848. class En2 = typename std::enable_if<std::is_void<typename U::value_type>::value>::type
  849. >
  850. U operator|( result<void, E> const& r, F&& f )
  851. {
  852. if( r )
  853. {
  854. return {};
  855. }
  856. else
  857. {
  858. return std::forward<F>( f )();
  859. }
  860. }
  861. template<class E, class F,
  862. class U = decltype( std::declval<F>()() ),
  863. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  864. class En2 = typename std::enable_if<std::is_void<typename U::value_type>::value>::type
  865. >
  866. U operator|( result<void, E>&& r, F&& f )
  867. {
  868. if( r )
  869. {
  870. return {};
  871. }
  872. else
  873. {
  874. return std::forward<F>( f )();
  875. }
  876. }
  877. // operator|=
  878. // result |= value
  879. template<class T, class E, class U,
  880. class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
  881. >
  882. result<T, E>& operator|=( result<T, E>& r, U&& u )
  883. {
  884. if( !r )
  885. {
  886. r = std::forward<U>( u );
  887. }
  888. return r;
  889. }
  890. // result |= nullary-returning-value
  891. template<class T, class E, class F,
  892. class U = decltype( std::declval<F>()() ),
  893. class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
  894. >
  895. result<T, E>& operator|=( result<T, E>& r, F&& f )
  896. {
  897. if( !r )
  898. {
  899. r = std::forward<F>( f )();
  900. }
  901. return r;
  902. }
  903. // result |= nullary-returning-result
  904. template<class T, class E, class F,
  905. class U = decltype( std::declval<F>()() ),
  906. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  907. class En2 = typename std::enable_if<detail::is_value_convertible_to<typename U::value_type, T>::value>::type,
  908. class En3 = typename std::enable_if<std::is_convertible<typename U::error_type, E>::value>::type
  909. >
  910. result<T, E>& operator|=( result<T, E>& r, F&& f )
  911. {
  912. if( !r )
  913. {
  914. r = std::forward<F>( f )();
  915. }
  916. return r;
  917. }
  918. // operator&
  919. // result & unary-returning-value
  920. template<class T, class E, class F,
  921. class U = decltype( std::declval<F>()( std::declval<T const&>() ) ),
  922. class En1 = typename std::enable_if<!detail::is_result<U>::value>::type,
  923. class En2 = typename std::enable_if<!std::is_void<U>::value>::type
  924. >
  925. result<U, E> operator&( result<T, E> const& r, F&& f )
  926. {
  927. if( r.has_error() )
  928. {
  929. return r.error();
  930. }
  931. else
  932. {
  933. return std::forward<F>( f )( *r );
  934. }
  935. }
  936. template<class T, class E, class F,
  937. class U = decltype( std::declval<F>()( std::declval<T>() ) ),
  938. class En1 = typename std::enable_if<!detail::is_result<U>::value>::type,
  939. class En2 = typename std::enable_if<!std::is_void<U>::value>::type
  940. >
  941. result<U, E> operator&( result<T, E>&& r, F&& f )
  942. {
  943. if( r.has_error() )
  944. {
  945. return r.error();
  946. }
  947. else
  948. {
  949. return std::forward<F>( f )( *std::move( r ) );
  950. }
  951. }
  952. template<class T, class E, class F,
  953. class U = decltype( std::declval<F>()( std::declval<T const&>() ) ),
  954. class En = typename std::enable_if<std::is_void<U>::value>::type
  955. >
  956. result<U, E> operator&( result<T, E> const& r, F&& f )
  957. {
  958. if( r.has_error() )
  959. {
  960. return r.error();
  961. }
  962. else
  963. {
  964. std::forward<F>( f )( *r );
  965. return {};
  966. }
  967. }
  968. template<class T, class E, class F,
  969. class U = decltype( std::declval<F>()( std::declval<T>() ) ),
  970. class En = typename std::enable_if<std::is_void<U>::value>::type
  971. >
  972. result<U, E> operator&( result<T, E>&& r, F&& f )
  973. {
  974. if( r.has_error() )
  975. {
  976. return r.error();
  977. }
  978. else
  979. {
  980. std::forward<F>( f )( *std::move( r ) );
  981. return {};
  982. }
  983. }
  984. template<class E, class F,
  985. class U = decltype( std::declval<F>()() ),
  986. class En1 = typename std::enable_if<!detail::is_result<U>::value>::type,
  987. class En2 = typename std::enable_if<!std::is_void<U>::value>::type
  988. >
  989. result<U, E> operator&( result<void, E> const& r, F&& f )
  990. {
  991. if( r.has_error() )
  992. {
  993. return r.error();
  994. }
  995. else
  996. {
  997. return std::forward<F>( f )();
  998. }
  999. }
  1000. template<class E, class F,
  1001. class U = decltype( std::declval<F>()() ),
  1002. class En = typename std::enable_if<std::is_void<U>::value>::type
  1003. >
  1004. result<U, E> operator&( result<void, E> const& r, F&& f )
  1005. {
  1006. if( r.has_error() )
  1007. {
  1008. return r.error();
  1009. }
  1010. else
  1011. {
  1012. std::forward<F>( f )();
  1013. return {};
  1014. }
  1015. }
  1016. // result & unary-returning-result
  1017. template<class T, class E, class F,
  1018. class U = decltype( std::declval<F>()( std::declval<T const&>() ) ),
  1019. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  1020. class En2 = typename std::enable_if<std::is_convertible<E, typename U::error_type>::value>::type
  1021. >
  1022. U operator&( result<T, E> const& r, F&& f )
  1023. {
  1024. if( r.has_error() )
  1025. {
  1026. return r.error();
  1027. }
  1028. else
  1029. {
  1030. return std::forward<F>( f )( *r );
  1031. }
  1032. }
  1033. template<class T, class E, class F,
  1034. class U = decltype( std::declval<F>()( std::declval<T>() ) ),
  1035. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  1036. class En2 = typename std::enable_if<std::is_convertible<E, typename U::error_type>::value>::type
  1037. >
  1038. U operator&( result<T, E>&& r, F&& f )
  1039. {
  1040. if( r.has_error() )
  1041. {
  1042. return r.error();
  1043. }
  1044. else
  1045. {
  1046. return std::forward<F>( f )( *std::move( r ) );
  1047. }
  1048. }
  1049. template<class E, class F,
  1050. class U = decltype( std::declval<F>()() ),
  1051. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  1052. class En2 = typename std::enable_if<std::is_convertible<E, typename U::error_type>::value>::type
  1053. >
  1054. U operator&( result<void, E> const& r, F&& f )
  1055. {
  1056. if( r.has_error() )
  1057. {
  1058. return r.error();
  1059. }
  1060. else
  1061. {
  1062. return std::forward<F>( f )();
  1063. }
  1064. }
  1065. // operator&=
  1066. // result &= unary-returning-value
  1067. template<class T, class E, class F,
  1068. class U = decltype( std::declval<F>()( std::declval<T>() ) ),
  1069. class En1 = typename std::enable_if<!detail::is_result<U>::value>::type,
  1070. class En2 = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
  1071. >
  1072. result<T, E>& operator&=( result<T, E>& r, F&& f )
  1073. {
  1074. if( r )
  1075. {
  1076. r = std::forward<F>( f )( *std::move( r ) );
  1077. }
  1078. return r;
  1079. }
  1080. template<class E, class F,
  1081. class U = decltype( std::declval<F>()() ),
  1082. class En = typename std::enable_if<!detail::is_result<U>::value>::type
  1083. >
  1084. result<void, E>& operator&=( result<void, E>& r, F&& f )
  1085. {
  1086. if( r )
  1087. {
  1088. std::forward<F>( f )();
  1089. }
  1090. return r;
  1091. }
  1092. // result &= unary-returning-result
  1093. template<class T, class E, class F,
  1094. class U = decltype( std::declval<F>()( std::declval<T>() ) ),
  1095. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  1096. class En2 = typename std::enable_if<detail::is_value_convertible_to<typename U::value_type, T>::value>::type,
  1097. class En3 = typename std::enable_if<std::is_convertible<typename U::error_type, E>::value>::type
  1098. >
  1099. result<T, E>& operator&=( result<T, E>& r, F&& f )
  1100. {
  1101. if( r )
  1102. {
  1103. r = std::forward<F>( f )( *std::move( r ) );
  1104. }
  1105. return r;
  1106. }
  1107. template<class E, class F,
  1108. class U = decltype( std::declval<F>()() ),
  1109. class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
  1110. class En2 = typename std::enable_if<std::is_void<typename U::value_type>::value>::type,
  1111. class En3 = typename std::enable_if<std::is_convertible<typename U::error_type, E>::value>::type
  1112. >
  1113. result<void, E>& operator&=( result<void, E>& r, F&& f )
  1114. {
  1115. if( r )
  1116. {
  1117. r = std::forward<F>( f )();
  1118. }
  1119. return r;
  1120. }
  1121. } // namespace system
  1122. } // namespace boost
  1123. #endif // #ifndef BOOST_SYSTEM_RESULT_HPP_INCLUDED