result.hpp 34 KB

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