optional.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2014 - 2021 Andrzej Krzemienski.
  3. //
  4. // Use, modification, and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/optional for documentation.
  9. //
  10. // You are welcome to contact the author at:
  11. // fernando_cacciola@hotmail.com
  12. //
  13. // Revisions:
  14. // 27 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen
  15. // 05 May 2014 (Added move semantics) Andrzej Krzemienski
  16. //
  17. #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
  18. #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
  19. #include <new>
  20. #ifndef BOOST_NO_IOSTREAM
  21. #include <iosfwd>
  22. #endif // BOOST_NO_IOSTREAM
  23. #include <boost/assert.hpp>
  24. #include <boost/core/addressof.hpp>
  25. #include <boost/core/enable_if.hpp>
  26. #include <boost/core/invoke_swap.hpp>
  27. #include <boost/core/launder.hpp>
  28. #include <boost/optional/bad_optional_access.hpp>
  29. #include <boost/throw_exception.hpp>
  30. #include <boost/type_traits/alignment_of.hpp>
  31. #include <boost/type_traits/conditional.hpp>
  32. #include <boost/type_traits/conjunction.hpp>
  33. #include <boost/type_traits/disjunction.hpp>
  34. #include <boost/type_traits/has_nothrow_constructor.hpp>
  35. #include <boost/type_traits/type_with_alignment.hpp>
  36. #include <boost/type_traits/remove_const.hpp>
  37. #include <boost/type_traits/remove_reference.hpp>
  38. #include <boost/type_traits/decay.hpp>
  39. #include <boost/type_traits/is_assignable.hpp>
  40. #include <boost/type_traits/is_base_of.hpp>
  41. #include <boost/type_traits/is_const.hpp>
  42. #include <boost/type_traits/is_constructible.hpp>
  43. #include <boost/type_traits/is_convertible.hpp>
  44. #include <boost/type_traits/is_lvalue_reference.hpp>
  45. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  46. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  47. #include <boost/type_traits/is_rvalue_reference.hpp>
  48. #include <boost/type_traits/is_same.hpp>
  49. #include <boost/type_traits/is_volatile.hpp>
  50. #include <boost/type_traits/is_scalar.hpp>
  51. #include <boost/none.hpp>
  52. #include <boost/optional/optional_fwd.hpp>
  53. #include <boost/optional/detail/optional_config.hpp>
  54. #include <boost/optional/detail/optional_factory_support.hpp>
  55. #include <boost/optional/detail/optional_aligned_storage.hpp>
  56. #include <boost/optional/detail/optional_hash.hpp>
  57. #include <boost/optional/detail/optional_utility.hpp>
  58. namespace boost { namespace optional_detail {
  59. template <typename T>
  60. struct optional_value_type
  61. {
  62. };
  63. template <typename U>
  64. struct optional_value_type< ::boost::optional<U> >
  65. {
  66. typedef U type;
  67. };
  68. template <typename T>
  69. T declval();
  70. // implementing my own result_of so that it works for C++11 (std::result_of)
  71. // and in C++20 (std::invoke_result).
  72. template <typename F, typename Ref, typename Rslt = decltype(declval<F>()(declval<Ref>()))>
  73. struct result_of
  74. {
  75. typedef Rslt type;
  76. };
  77. template <typename F, typename Ref, typename Rslt = typename optional_value_type<typename result_of<F, Ref>::type>::type>
  78. struct result_value_type
  79. {
  80. typedef Rslt type;
  81. };
  82. // optional<typename optional_detail::optional_value_type<decltype(optional_detail::declval<F>()(optional_detail::declval<reference_type>()))>::type>
  83. }} // namespace boost::optional_detail
  84. namespace boost {
  85. namespace optional_ns {
  86. // a tag for in-place initialization of contained value
  87. struct in_place_init_t
  88. {
  89. struct init_tag{};
  90. BOOST_CONSTEXPR explicit in_place_init_t(init_tag){}
  91. };
  92. BOOST_INLINE_CONSTEXPR in_place_init_t in_place_init ((in_place_init_t::init_tag()));
  93. // a tag for conditional in-place initialization of contained value
  94. struct in_place_init_if_t
  95. {
  96. struct init_tag{};
  97. BOOST_CONSTEXPR explicit in_place_init_if_t(init_tag){}
  98. };
  99. BOOST_INLINE_CONSTEXPR in_place_init_if_t in_place_init_if ((in_place_init_if_t::init_tag()));
  100. } // namespace optional_ns
  101. using optional_ns::in_place_init_t;
  102. using optional_ns::in_place_init;
  103. using optional_ns::in_place_init_if_t;
  104. using optional_ns::in_place_init_if;
  105. namespace optional_detail {
  106. struct init_value_tag {};
  107. struct optional_tag {};
  108. template<class T>
  109. class optional_base : public optional_tag
  110. {
  111. private :
  112. typedef aligned_storage<T> storage_type ;
  113. typedef optional_base<T> this_type ;
  114. protected :
  115. typedef T value_type ;
  116. typedef typename boost::remove_const<T>::type unqualified_value_type;
  117. protected:
  118. typedef T & reference_type ;
  119. typedef T const& reference_const_type ;
  120. typedef T && rval_reference_type ;
  121. typedef T && reference_type_of_temporary_wrapper ;
  122. typedef T * pointer_type ;
  123. typedef T const* pointer_const_type ;
  124. typedef T const& argument_type ;
  125. // Creates an optional<T> uninitialized.
  126. // No-throw
  127. optional_base()
  128. :
  129. m_initialized(false) {}
  130. // Creates an optional<T> uninitialized.
  131. // No-throw
  132. optional_base ( none_t )
  133. :
  134. m_initialized(false) {}
  135. // Creates an optional<T> initialized with 'val'.
  136. // Can throw if T::T(T const&) does
  137. optional_base ( init_value_tag, argument_type val )
  138. :
  139. m_initialized(false)
  140. {
  141. construct(val);
  142. }
  143. // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
  144. // Can throw if T::T(T&&) does
  145. optional_base ( init_value_tag, rval_reference_type val )
  146. :
  147. m_initialized(false)
  148. {
  149. construct( optional_detail::move(val) );
  150. }
  151. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional<T>.
  152. // Can throw if T::T(T const&) does
  153. optional_base ( bool cond, argument_type val )
  154. :
  155. m_initialized(false)
  156. {
  157. if ( cond )
  158. construct(val);
  159. }
  160. // Creates an optional<T> initialized with 'move(val)' IFF cond is true, otherwise creates an uninitialized optional<T>.
  161. // Can throw if T::T(T &&) does
  162. optional_base ( bool cond, rval_reference_type val )
  163. :
  164. m_initialized(false)
  165. {
  166. if ( cond )
  167. construct(optional_detail::move(val));
  168. }
  169. // Creates a deep copy of another optional<T>
  170. // Can throw if T::T(T const&) does
  171. optional_base ( optional_base const& rhs )
  172. :
  173. m_initialized(false)
  174. {
  175. if ( rhs.is_initialized() )
  176. construct(rhs.get_impl());
  177. }
  178. // Creates a deep move of another optional<T>
  179. // Can throw if T::T(T&&) does
  180. optional_base ( optional_base&& rhs )
  181. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  182. :
  183. m_initialized(false)
  184. {
  185. if ( rhs.is_initialized() )
  186. construct( optional_detail::move(rhs.get_impl()) );
  187. }
  188. template<class Expr, class PtrExpr>
  189. explicit optional_base ( Expr&& expr, PtrExpr const* tag )
  190. :
  191. m_initialized(false)
  192. {
  193. construct(optional_detail::forward<Expr>(expr),tag);
  194. }
  195. optional_base& operator= ( optional_base const& rhs )
  196. {
  197. this->assign(rhs);
  198. return *this;
  199. }
  200. optional_base& operator= ( optional_base && rhs )
  201. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  202. {
  203. this->assign(static_cast<optional_base&&>(rhs));
  204. return *this;
  205. }
  206. // No-throw (assuming T::~T() doesn't)
  207. ~optional_base() { destroy() ; }
  208. // Assigns from another optional<T> (deep-copies the rhs value)
  209. void assign ( optional_base const& rhs )
  210. {
  211. if (is_initialized())
  212. {
  213. if ( rhs.is_initialized() )
  214. assign_value(rhs.get_impl());
  215. else destroy();
  216. }
  217. else
  218. {
  219. if ( rhs.is_initialized() )
  220. construct(rhs.get_impl());
  221. }
  222. }
  223. // Assigns from another optional<T> (deep-moves the rhs value)
  224. void assign ( optional_base&& rhs )
  225. {
  226. if (is_initialized())
  227. {
  228. if ( rhs.is_initialized() )
  229. assign_value( optional_detail::move(rhs.get_impl()) );
  230. else destroy();
  231. }
  232. else
  233. {
  234. if ( rhs.is_initialized() )
  235. construct(optional_detail::move(rhs.get_impl()));
  236. }
  237. }
  238. // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
  239. template<class U>
  240. void assign ( optional<U> const& rhs )
  241. {
  242. if (is_initialized())
  243. {
  244. if ( rhs.is_initialized() )
  245. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  246. assign_value( rhs.get() );
  247. #else
  248. assign_value( static_cast<value_type>(rhs.get()) );
  249. #endif
  250. else destroy();
  251. }
  252. else
  253. {
  254. if ( rhs.is_initialized() )
  255. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  256. construct(rhs.get());
  257. #else
  258. construct(static_cast<value_type>(rhs.get()));
  259. #endif
  260. }
  261. }
  262. // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
  263. template<class U>
  264. void assign ( optional<U>&& rhs )
  265. {
  266. typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
  267. if (is_initialized())
  268. {
  269. if ( rhs.is_initialized() )
  270. assign_value( static_cast<ref_type>(rhs.get()) );
  271. else destroy();
  272. }
  273. else
  274. {
  275. if ( rhs.is_initialized() )
  276. construct(static_cast<ref_type>(rhs.get()));
  277. }
  278. }
  279. // Assigns from a T (deep-copies the rhs value)
  280. void assign ( argument_type val )
  281. {
  282. if (is_initialized())
  283. assign_value(val);
  284. else construct(val);
  285. }
  286. // Assigns from a T (deep-moves the rhs value)
  287. void assign ( rval_reference_type val )
  288. {
  289. if (is_initialized())
  290. assign_value( optional_detail::move(val) );
  291. else construct( optional_detail::move(val) );
  292. }
  293. // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
  294. // No-throw (assuming T::~T() doesn't)
  295. void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
  296. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  297. template<class Expr, class ExprPtr>
  298. void assign_expr ( Expr&& expr, ExprPtr const* tag )
  299. {
  300. if (is_initialized())
  301. assign_expr_to_initialized(optional_detail::forward<Expr>(expr),tag);
  302. else construct(optional_detail::forward<Expr>(expr),tag);
  303. }
  304. #endif
  305. public :
  306. // Destroys the current value, if any, leaving this UNINITIALIZED
  307. // No-throw (assuming T::~T() doesn't)
  308. void reset() BOOST_NOEXCEPT { destroy(); }
  309. // **DEPRECATED** Replaces the current value -if any- with 'val'
  310. void reset ( argument_type val ) { assign(val); }
  311. // Returns a pointer to the value if this is initialized, otherwise,
  312. // returns NULL.
  313. // No-throw
  314. pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
  315. pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
  316. bool is_initialized() const BOOST_NOEXCEPT { return m_initialized ; }
  317. protected :
  318. void construct ( argument_type val )
  319. {
  320. ::new (m_storage.address()) unqualified_value_type(val) ;
  321. m_initialized = true ;
  322. }
  323. void construct ( rval_reference_type val )
  324. {
  325. ::new (m_storage.address()) unqualified_value_type( optional_detail::move(val) ) ;
  326. m_initialized = true ;
  327. }
  328. // Constructs in-place
  329. // upon exception *this is always uninitialized
  330. template<class... Args>
  331. void construct ( in_place_init_t, Args&&... args )
  332. {
  333. ::new (m_storage.address()) unqualified_value_type( optional_detail::forward<Args>(args)... ) ;
  334. m_initialized = true ;
  335. }
  336. template<class... Args>
  337. void emplace_assign ( Args&&... args )
  338. {
  339. destroy();
  340. construct(in_place_init, optional_detail::forward<Args>(args)...);
  341. }
  342. template<class... Args>
  343. explicit optional_base ( in_place_init_t, Args&&... args )
  344. :
  345. m_initialized(false)
  346. {
  347. construct(in_place_init, optional_detail::forward<Args>(args)...);
  348. }
  349. template<class... Args>
  350. explicit optional_base ( in_place_init_if_t, bool cond, Args&&... args )
  351. :
  352. m_initialized(false)
  353. {
  354. if ( cond )
  355. construct(in_place_init, optional_detail::forward<Args>(args)...);
  356. }
  357. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  358. // Constructs in-place using the given factory
  359. template<class Expr>
  360. void construct ( Expr&& factory, in_place_factory_base const* )
  361. {
  362. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  363. m_initialized = true ;
  364. }
  365. // Constructs in-place using the given typed factory
  366. template<class Expr>
  367. void construct ( Expr&& factory, typed_in_place_factory_base const* )
  368. {
  369. factory.apply(m_storage.address()) ;
  370. m_initialized = true ;
  371. }
  372. template<class Expr>
  373. void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
  374. {
  375. destroy();
  376. construct(factory,tag);
  377. }
  378. // Constructs in-place using the given typed factory
  379. template<class Expr>
  380. void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
  381. {
  382. destroy();
  383. construct(factory,tag);
  384. }
  385. #endif
  386. // Constructs using any expression implicitly convertible to the single argument
  387. // of a one-argument T constructor.
  388. // Converting constructions of optional<T> from optional<U> uses this function with
  389. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  390. template<class Expr>
  391. void construct ( Expr&& expr, void const* )
  392. {
  393. new (m_storage.address()) unqualified_value_type(optional_detail::forward<Expr>(expr)) ;
  394. m_initialized = true ;
  395. }
  396. // Assigns using a form any expression implicitly convertible to the single argument
  397. // of a T's assignment operator.
  398. // Converting assignments of optional<T> from optional<U> uses this function with
  399. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  400. template<class Expr>
  401. void assign_expr_to_initialized ( Expr&& expr, void const* )
  402. {
  403. assign_value( optional_detail::forward<Expr>(expr) );
  404. }
  405. #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  406. // BCB5.64 (and probably lower versions) workaround.
  407. // The in-place factories are supported by means of catch-all constructors
  408. // and assignment operators (the functions are parameterized in terms of
  409. // an arbitrary 'Expr' type)
  410. // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
  411. // to the 'Expr'-taking functions even though explicit overloads are present for them.
  412. // Thus, the following overload is needed to properly handle the case when the 'lhs'
  413. // is another optional.
  414. //
  415. // For VC<=70 compilers this workaround doesn't work because the compiler issues and error
  416. // instead of choosing the wrong overload
  417. //
  418. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  419. template<class Expr>
  420. void construct ( Expr&& expr, optional_tag const* )
  421. {
  422. if ( expr.is_initialized() )
  423. {
  424. // An exception can be thrown here.
  425. // It it happens, THIS will be left uninitialized.
  426. new (m_storage.address()) unqualified_value_type(optional_detail::move(expr.get())) ;
  427. m_initialized = true ;
  428. }
  429. }
  430. #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  431. void assign_value ( argument_type val ) { get_impl() = val; }
  432. void assign_value ( rval_reference_type val ) { get_impl() = static_cast<rval_reference_type>(val); }
  433. void destroy()
  434. {
  435. if ( m_initialized )
  436. destroy_impl() ;
  437. }
  438. reference_const_type get_impl() const { return m_storage.ref() ; }
  439. reference_type get_impl() { return m_storage.ref() ; }
  440. pointer_const_type get_ptr_impl() const { return m_storage.ptr_ref(); }
  441. pointer_type get_ptr_impl() { return m_storage.ptr_ref(); }
  442. private :
  443. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1900))
  444. void destroy_impl ( ) { m_storage.ptr_ref()->~T() ; m_initialized = false ; }
  445. #else
  446. void destroy_impl ( ) { m_storage.ref().T::~T() ; m_initialized = false ; }
  447. #endif
  448. bool m_initialized ;
  449. storage_type m_storage ;
  450. } ;
  451. #include <boost/optional/detail/optional_trivially_copyable_base.hpp>
  452. // definition of metafunction is_optional_val_init_candidate
  453. template <typename U>
  454. struct is_optional_or_tag
  455. : boost::conditional< boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
  456. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, none_t>::value
  457. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_t>::value
  458. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_if_t>::value,
  459. boost::true_type, boost::false_type>::type
  460. {};
  461. template <typename T, typename U>
  462. struct has_dedicated_constructor
  463. : boost::disjunction<is_optional_or_tag<U>, boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<U>::type> >
  464. {};
  465. template <typename U>
  466. struct is_in_place_factory
  467. : boost::disjunction< boost::is_base_of<boost::in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>,
  468. boost::is_base_of<boost::typed_in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type> >
  469. {};
  470. #if !defined(BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT)
  471. template <typename T, typename U>
  472. struct is_factory_or_constructible_to_T
  473. : boost::disjunction< is_in_place_factory<U>, boost::is_constructible<T, U&&> >
  474. {};
  475. template <typename T, typename U>
  476. struct is_optional_constructible : boost::is_constructible<T, U>
  477. {};
  478. #else
  479. template <typename, typename>
  480. struct is_factory_or_constructible_to_T : boost::true_type
  481. {};
  482. template <typename T, typename U>
  483. struct is_optional_constructible : boost::true_type
  484. {};
  485. #endif // is_convertible condition
  486. #if !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  487. // for is_assignable
  488. // On some initial rvalue reference implementations GCC does it in a strange way,
  489. // preferring perfect-forwarding constructor to implicit copy constructor.
  490. template <typename T, typename U>
  491. struct is_opt_assignable
  492. : boost::conjunction<boost::is_convertible<U&&, T>, boost::is_assignable<T&, U&&> >
  493. {};
  494. #else
  495. template <typename T, typename U>
  496. struct is_opt_assignable : boost::is_convertible<U, T>
  497. {};
  498. #endif
  499. template <typename T, typename U>
  500. struct is_factory_or_opt_assignable_to_T
  501. : boost::disjunction< is_in_place_factory<U>, is_opt_assignable<T, U> >
  502. {};
  503. template <typename T, typename U, bool = has_dedicated_constructor<T, U>::value>
  504. struct is_optional_val_init_candidate
  505. : boost::false_type
  506. {};
  507. template <typename T, typename U>
  508. struct is_optional_val_init_candidate<T, U, false>
  509. : is_factory_or_constructible_to_T<T, U>
  510. {};
  511. template <typename T, typename U, bool = has_dedicated_constructor<T, U>::value>
  512. struct is_optional_val_assign_candidate
  513. : boost::false_type
  514. {};
  515. template <typename T, typename U>
  516. struct is_optional_val_assign_candidate<T, U, false>
  517. : is_factory_or_opt_assignable_to_T<T, U>
  518. {};
  519. } // namespace optional_detail
  520. namespace optional_config {
  521. template <typename T>
  522. struct optional_uses_direct_storage_for
  523. : boost::conditional<(boost::is_scalar<T>::value && !boost::is_const<T>::value && !boost::is_volatile<T>::value)
  524. , boost::true_type, boost::false_type>::type
  525. {};
  526. } // namespace optional_config
  527. #ifndef BOOST_OPTIONAL_DETAIL_NO_DIRECT_STORAGE_SPEC
  528. # define BOOST_OPTIONAL_BASE_TYPE(T) boost::conditional< optional_config::optional_uses_direct_storage_for<T>::value, \
  529. optional_detail::tc_optional_base<T>, \
  530. optional_detail::optional_base<T> \
  531. >::type
  532. #else
  533. # define BOOST_OPTIONAL_BASE_TYPE(T) optional_detail::optional_base<T>
  534. #endif
  535. template<class T>
  536. class optional
  537. : public BOOST_OPTIONAL_BASE_TYPE(T)
  538. {
  539. typedef typename BOOST_OPTIONAL_BASE_TYPE(T) base ;
  540. public :
  541. typedef optional<T> this_type ;
  542. typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
  543. typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
  544. typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
  545. typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
  546. typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
  547. typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
  548. typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
  549. typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
  550. // Creates an optional<T> uninitialized.
  551. // No-throw
  552. optional() BOOST_NOEXCEPT : base() {}
  553. // Creates an optional<T> uninitialized.
  554. // No-throw
  555. optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
  556. // Creates an optional<T> initialized with 'val'.
  557. // Can throw if T::T(T const&) does
  558. optional ( argument_type val ) : base(optional_detail::init_value_tag(), val) {}
  559. // Creates an optional<T> initialized with 'move(val)'.
  560. // Can throw if T::T(T &&) does
  561. optional ( rval_reference_type val ) : base(optional_detail::init_value_tag(), optional_detail::forward<T>(val))
  562. {}
  563. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  564. // Can throw if T::T(T const&) does
  565. optional ( bool cond, argument_type val ) : base(cond,val) {}
  566. /// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  567. // Can throw if T::T(T &&) does
  568. optional ( bool cond, rval_reference_type val ) : base( cond, optional_detail::forward<T>(val) )
  569. {}
  570. // NOTE: MSVC needs templated versions first
  571. // Creates a deep copy of another convertible optional<U>
  572. // Requires a valid conversion from U to T.
  573. // Can throw if T::T(U const&) does
  574. template<class U>
  575. explicit optional ( optional<U> const& rhs
  576. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  577. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U const&>, bool>::type = true
  578. #endif
  579. )
  580. :
  581. base()
  582. {
  583. if ( rhs.is_initialized() )
  584. this->construct(rhs.get());
  585. }
  586. // Creates a deep move of another convertible optional<U>
  587. // Requires a valid conversion from U to T.
  588. // Can throw if T::T(U&&) does
  589. template<class U>
  590. explicit optional ( optional<U> && rhs
  591. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  592. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U>, bool>::type = true
  593. #endif
  594. )
  595. :
  596. base()
  597. {
  598. if ( rhs.is_initialized() )
  599. this->construct( optional_detail::move(rhs.get()) );
  600. }
  601. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  602. // Creates an optional<T> with an expression which can be either
  603. // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
  604. // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
  605. // (c) Any expression implicitly convertible to the single type
  606. // of a one-argument T's constructor.
  607. // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
  608. // even though explicit overloads are present for these.
  609. // Depending on the above some T ctor is called.
  610. // Can throw if the resolved T ctor throws.
  611. template<class Expr>
  612. explicit optional ( Expr&& expr,
  613. BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true
  614. )
  615. : base(optional_detail::forward<Expr>(expr),boost::addressof(expr))
  616. {}
  617. #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  618. // Creates a deep copy of another optional<T>
  619. // Can throw if T::T(T const&) does
  620. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  621. optional ( optional const& ) = default;
  622. #else
  623. optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
  624. #endif
  625. // Creates a deep move of another optional<T>
  626. // Can throw if T::T(T&&) does
  627. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  628. optional ( optional && ) = default;
  629. #else
  630. optional ( optional && rhs )
  631. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  632. : base( optional_detail::move(rhs) )
  633. {}
  634. #endif
  635. #if BOOST_WORKAROUND(_MSC_VER, <= 1600)
  636. // On old MSVC compilers the implicitly declared dtor is not called
  637. ~optional() {}
  638. #endif
  639. #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  640. // Assigns from an expression. See corresponding constructor.
  641. // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
  642. template<class Expr>
  643. BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_assign_candidate<T, Expr>, optional&>::type
  644. operator= ( Expr&& expr )
  645. {
  646. this->assign_expr(optional_detail::forward<Expr>(expr),boost::addressof(expr));
  647. return *this ;
  648. }
  649. #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  650. // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
  651. // Requires a valid conversion from U to T.
  652. // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
  653. template<class U>
  654. optional& operator= ( optional<U> const& rhs )
  655. {
  656. this->assign(rhs);
  657. return *this ;
  658. }
  659. // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
  660. // Requires a valid conversion from U to T.
  661. // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
  662. template<class U>
  663. optional& operator= ( optional<U> && rhs )
  664. {
  665. this->assign(optional_detail::move(rhs));
  666. return *this ;
  667. }
  668. // Assigns from another optional<T> (deep-copies the rhs value)
  669. // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
  670. // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
  671. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  672. optional& operator= ( optional const& rhs ) = default;
  673. #else
  674. optional& operator= ( optional const& rhs )
  675. {
  676. this->assign( static_cast<base const&>(rhs) ) ;
  677. return *this ;
  678. }
  679. #endif
  680. // Assigns from another optional<T> (deep-moves the rhs value)
  681. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  682. optional& operator= ( optional && ) = default;
  683. #else
  684. optional& operator= ( optional && rhs )
  685. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  686. {
  687. this->assign( static_cast<base &&>(rhs) ) ;
  688. return *this ;
  689. }
  690. #endif
  691. #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  692. // Assigns from a T (deep-moves/copies the rhs value)
  693. template <typename T_>
  694. BOOST_DEDUCED_TYPENAME boost::enable_if<boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<T_>::type>, optional&>::type
  695. operator= ( T_&& val )
  696. {
  697. this->assign( optional_detail::forward<T_>(val) ) ;
  698. return *this ;
  699. }
  700. #else
  701. // Assigns from a T (deep-copies the rhs value)
  702. // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
  703. optional& operator= ( argument_type val )
  704. {
  705. this->assign( val ) ;
  706. return *this ;
  707. }
  708. // Assigns from a T (deep-moves the rhs value)
  709. optional& operator= ( rval_reference_type val )
  710. {
  711. this->assign( optional_detail::move(val) ) ;
  712. return *this ;
  713. }
  714. #endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  715. // Assigns from a "none"
  716. // Which destroys the current value, if any, leaving this UNINITIALIZED
  717. // No-throw (assuming T::~T() doesn't)
  718. optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
  719. {
  720. this->assign( none_ ) ;
  721. return *this ;
  722. }
  723. // Constructs in-place
  724. // upon exception *this is always uninitialized
  725. template<class... Args>
  726. void emplace ( Args&&... args )
  727. {
  728. this->emplace_assign( optional_detail::forward<Args>(args)... );
  729. }
  730. template<class... Args>
  731. explicit optional ( in_place_init_t, Args&&... args )
  732. : base( in_place_init, optional_detail::forward<Args>(args)... )
  733. {}
  734. template<class... Args>
  735. explicit optional ( in_place_init_if_t, bool cond, Args&&... args )
  736. : base( in_place_init_if, cond, optional_detail::forward<Args>(args)... )
  737. {}
  738. void swap( optional & arg )
  739. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  740. {
  741. // allow for Koenig lookup
  742. boost::core::invoke_swap(*this, arg);
  743. }
  744. // Returns a reference to the value if this is initialized, otherwise,
  745. // the behaviour is UNDEFINED
  746. // No-throw
  747. reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  748. reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  749. // Returns a copy of the value if this is initialized, 'v' otherwise
  750. reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
  751. reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
  752. // Returns a pointer to the value if this is initialized, otherwise,
  753. // the behaviour is UNDEFINED
  754. // No-throw
  755. pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  756. pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  757. // Returns a reference to the value if this is initialized, otherwise,
  758. // the behaviour is UNDEFINED
  759. // No-throw
  760. reference_const_type operator *() BOOST_OPTIONAL_CONST_REF_QUAL { return this->get() ; }
  761. reference_type operator *() BOOST_OPTIONAL_REF_QUAL { return this->get() ; }
  762. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  763. reference_type_of_temporary_wrapper operator *() && { return optional_detail::move(this->get()) ; }
  764. #endif
  765. reference_const_type value() BOOST_OPTIONAL_CONST_REF_QUAL
  766. {
  767. if (this->is_initialized())
  768. return this->get() ;
  769. else
  770. throw_exception(bad_optional_access());
  771. }
  772. reference_type value() BOOST_OPTIONAL_REF_QUAL
  773. {
  774. if (this->is_initialized())
  775. return this->get() ;
  776. else
  777. throw_exception(bad_optional_access());
  778. }
  779. template <class U>
  780. value_type value_or ( U&& v ) BOOST_OPTIONAL_CONST_REF_QUAL
  781. {
  782. if (this->is_initialized())
  783. return get();
  784. else
  785. return optional_detail::forward<U>(v);
  786. }
  787. template <typename F>
  788. value_type value_or_eval ( F f ) BOOST_OPTIONAL_CONST_REF_QUAL
  789. {
  790. if (this->is_initialized())
  791. return get();
  792. else
  793. return f();
  794. }
  795. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  796. reference_type_of_temporary_wrapper value() &&
  797. {
  798. if (this->is_initialized())
  799. return optional_detail::move(this->get()) ;
  800. else
  801. throw_exception(bad_optional_access());
  802. }
  803. template <class U>
  804. value_type value_or ( U&& v ) &&
  805. {
  806. if (this->is_initialized())
  807. return optional_detail::move(get());
  808. else
  809. return optional_detail::forward<U>(v);
  810. }
  811. template <typename F>
  812. value_type value_or_eval ( F f ) &&
  813. {
  814. if (this->is_initialized())
  815. return optional_detail::move(get());
  816. else
  817. return f();
  818. }
  819. #endif
  820. // Monadic interface
  821. template <typename F>
  822. optional<typename optional_detail::result_of<F, reference_type>::type> map(F f) BOOST_OPTIONAL_REF_QUAL
  823. {
  824. if (this->has_value())
  825. return f(get());
  826. else
  827. return none;
  828. }
  829. template <typename F>
  830. optional<typename optional_detail::result_of<F, reference_const_type>::type> map(F f) BOOST_OPTIONAL_CONST_REF_QUAL
  831. {
  832. if (this->has_value())
  833. return f(get());
  834. else
  835. return none;
  836. }
  837. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  838. template <typename F>
  839. optional<typename optional_detail::result_of<F, reference_type_of_temporary_wrapper>::type> map(F f) &&
  840. {
  841. if (this->has_value())
  842. return f(optional_detail::move(this->get()));
  843. else
  844. return none;
  845. }
  846. #endif
  847. template <typename F>
  848. optional<typename optional_detail::result_value_type<F, reference_type>::type>
  849. flat_map(F f) BOOST_OPTIONAL_REF_QUAL
  850. {
  851. if (this->has_value())
  852. return f(get());
  853. else
  854. return none;
  855. }
  856. template <typename F>
  857. optional<typename optional_detail::result_value_type<F, reference_const_type>::type>
  858. flat_map(F f) BOOST_OPTIONAL_CONST_REF_QUAL
  859. {
  860. if (this->has_value())
  861. return f(get());
  862. else
  863. return none;
  864. }
  865. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  866. template <typename F>
  867. optional<typename optional_detail::result_value_type<F, reference_type_of_temporary_wrapper>::type>
  868. flat_map(F f) &&
  869. {
  870. if (this->has_value())
  871. return f(optional_detail::move(get()));
  872. else
  873. return none;
  874. }
  875. #endif
  876. bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; }
  877. explicit operator bool() const BOOST_NOEXCEPT { return this->has_value() ; }
  878. } ;
  879. template<class T>
  880. class optional<T&&>
  881. {
  882. static_assert(sizeof(T) == 0, "Optional rvalue references are illegal.");
  883. } ;
  884. } // namespace boost
  885. #ifndef BOOST_OPTIONAL_CONFIG_DONT_SPECIALIZE_OPTIONAL_REFS
  886. # include <boost/optional/detail/optional_reference_spec.hpp>
  887. #endif
  888. namespace boost {
  889. template<class T>
  890. inline
  891. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( T && v )
  892. {
  893. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(optional_detail::forward<T>(v));
  894. }
  895. // Returns optional<T>(cond,v)
  896. template<class T>
  897. inline
  898. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( bool cond, T && v )
  899. {
  900. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(cond,optional_detail::forward<T>(v));
  901. }
  902. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  903. // No-throw
  904. template<class T>
  905. inline
  906. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  907. get ( optional<T> const& opt )
  908. {
  909. return opt.get() ;
  910. }
  911. template<class T>
  912. inline
  913. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  914. get ( optional<T>& opt )
  915. {
  916. return opt.get() ;
  917. }
  918. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  919. // No-throw
  920. template<class T>
  921. inline
  922. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  923. get ( optional<T> const* opt )
  924. {
  925. return opt->get_ptr() ;
  926. }
  927. template<class T>
  928. inline
  929. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  930. get ( optional<T>* opt )
  931. {
  932. return opt->get_ptr() ;
  933. }
  934. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  935. // No-throw
  936. template<class T>
  937. inline
  938. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  939. get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
  940. {
  941. return opt.get_value_or(v) ;
  942. }
  943. template<class T>
  944. inline
  945. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  946. get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
  947. {
  948. return opt.get_value_or(v) ;
  949. }
  950. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  951. // No-throw
  952. template<class T>
  953. inline
  954. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  955. get_pointer ( optional<T> const& opt )
  956. {
  957. return opt.get_ptr() ;
  958. }
  959. template<class T>
  960. inline
  961. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  962. get_pointer ( optional<T>& opt )
  963. {
  964. return opt.get_ptr() ;
  965. }
  966. } // namespace boost
  967. #ifndef BOOST_NO_IOSTREAM
  968. namespace boost {
  969. // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
  970. template<class CharType, class CharTrait>
  971. std::basic_ostream<CharType, CharTrait>&
  972. operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&)
  973. {
  974. static_assert(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
  975. return os;
  976. }
  977. } // namespace boost
  978. #endif // BOOST_NO_IOSTREAM
  979. #include <boost/optional/detail/optional_relops.hpp>
  980. #include <boost/optional/detail/optional_swap.hpp>
  981. #endif // header guard