optional.hpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2014 - 2018 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. #include <iosfwd>
  21. #ifdef BOOST_OPTIONAL_DETAIL_USE_STD_TYPE_TRAITS
  22. # include <type_traits>
  23. #endif
  24. #include <boost/assert.hpp>
  25. #include <boost/core/addressof.hpp>
  26. #include <boost/core/enable_if.hpp>
  27. #include <boost/core/explicit_operator_bool.hpp>
  28. #include <boost/core/swap.hpp>
  29. #include <boost/optional/bad_optional_access.hpp>
  30. #include <boost/static_assert.hpp>
  31. #include <boost/throw_exception.hpp>
  32. #include <boost/type.hpp>
  33. #include <boost/type_traits/alignment_of.hpp>
  34. #include <boost/type_traits/conditional.hpp>
  35. #include <boost/type_traits/has_nothrow_constructor.hpp>
  36. #include <boost/type_traits/type_with_alignment.hpp>
  37. #include <boost/type_traits/remove_const.hpp>
  38. #include <boost/type_traits/remove_reference.hpp>
  39. #include <boost/type_traits/decay.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_lvalue_reference.hpp>
  44. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  45. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  46. #include <boost/type_traits/is_rvalue_reference.hpp>
  47. #include <boost/type_traits/is_same.hpp>
  48. #include <boost/type_traits/is_volatile.hpp>
  49. #include <boost/type_traits/is_scalar.hpp>
  50. #include <boost/move/utility.hpp>
  51. #include <boost/none.hpp>
  52. #include <boost/utility/compare_pointees.hpp>
  53. #include <boost/utility/result_of.hpp>
  54. #include <boost/optional/optional_fwd.hpp>
  55. #include <boost/optional/detail/optional_config.hpp>
  56. #include <boost/optional/detail/optional_factory_support.hpp>
  57. #include <boost/optional/detail/optional_aligned_storage.hpp>
  58. namespace boost { namespace optional_detail {
  59. template <typename T>
  60. struct optional_value_type
  61. {
  62. };
  63. template <typename T>
  64. struct optional_value_type< ::boost::optional<T> >
  65. {
  66. typedef T type;
  67. };
  68. }} // namespace boost::optional_detail
  69. #ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
  70. #include <boost/optional/detail/old_optional_implementation.hpp>
  71. #else
  72. namespace boost {
  73. namespace optional_ns {
  74. // a tag for in-place initialization of contained value
  75. struct in_place_init_t
  76. {
  77. struct init_tag{};
  78. explicit in_place_init_t(init_tag){}
  79. };
  80. const in_place_init_t in_place_init ((in_place_init_t::init_tag()));
  81. // a tag for conditional in-place initialization of contained value
  82. struct in_place_init_if_t
  83. {
  84. struct init_tag{};
  85. explicit in_place_init_if_t(init_tag){}
  86. };
  87. const in_place_init_if_t in_place_init_if ((in_place_init_if_t::init_tag()));
  88. } // namespace optional_ns
  89. using optional_ns::in_place_init_t;
  90. using optional_ns::in_place_init;
  91. using optional_ns::in_place_init_if_t;
  92. using optional_ns::in_place_init_if;
  93. namespace optional_detail {
  94. struct init_value_tag {};
  95. struct optional_tag {};
  96. template<class T>
  97. class optional_base : public optional_tag
  98. {
  99. private :
  100. typedef aligned_storage<T> storage_type ;
  101. typedef optional_base<T> this_type ;
  102. protected :
  103. typedef T value_type ;
  104. protected:
  105. typedef T & reference_type ;
  106. typedef T const& reference_const_type ;
  107. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  108. typedef T && rval_reference_type ;
  109. typedef T && reference_type_of_temporary_wrapper ;
  110. #endif
  111. typedef T * pointer_type ;
  112. typedef T const* pointer_const_type ;
  113. typedef T const& argument_type ;
  114. // Creates an optional<T> uninitialized.
  115. // No-throw
  116. optional_base()
  117. :
  118. m_initialized(false) {}
  119. // Creates an optional<T> uninitialized.
  120. // No-throw
  121. optional_base ( none_t )
  122. :
  123. m_initialized(false) {}
  124. // Creates an optional<T> initialized with 'val'.
  125. // Can throw if T::T(T const&) does
  126. optional_base ( init_value_tag, argument_type val )
  127. :
  128. m_initialized(false)
  129. {
  130. construct(val);
  131. }
  132. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  133. // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
  134. // Can throw if T::T(T&&) does
  135. optional_base ( init_value_tag, rval_reference_type val )
  136. :
  137. m_initialized(false)
  138. {
  139. construct( boost::move(val) );
  140. }
  141. #endif
  142. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional<T>.
  143. // Can throw if T::T(T const&) does
  144. optional_base ( bool cond, argument_type val )
  145. :
  146. m_initialized(false)
  147. {
  148. if ( cond )
  149. construct(val);
  150. }
  151. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  152. // Creates an optional<T> initialized with 'move(val)' IFF cond is true, otherwise creates an uninitialized optional<T>.
  153. // Can throw if T::T(T &&) does
  154. optional_base ( bool cond, rval_reference_type val )
  155. :
  156. m_initialized(false)
  157. {
  158. if ( cond )
  159. construct(boost::move(val));
  160. }
  161. #endif
  162. // Creates a deep copy of another optional<T>
  163. // Can throw if T::T(T const&) does
  164. optional_base ( optional_base const& rhs )
  165. :
  166. m_initialized(false)
  167. {
  168. if ( rhs.is_initialized() )
  169. construct(rhs.get_impl());
  170. }
  171. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  172. // Creates a deep move of another optional<T>
  173. // Can throw if T::T(T&&) does
  174. optional_base ( optional_base&& rhs )
  175. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  176. :
  177. m_initialized(false)
  178. {
  179. if ( rhs.is_initialized() )
  180. construct( boost::move(rhs.get_impl()) );
  181. }
  182. #endif
  183. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  184. template<class Expr, class PtrExpr>
  185. explicit optional_base ( Expr&& expr, PtrExpr const* tag )
  186. :
  187. m_initialized(false)
  188. {
  189. construct(boost::forward<Expr>(expr),tag);
  190. }
  191. #else
  192. // This is used for both converting and in-place constructions.
  193. // Derived classes use the 'tag' to select the appropriate
  194. // implementation (the correct 'construct()' overload)
  195. template<class Expr>
  196. explicit optional_base ( Expr const& expr, Expr const* tag )
  197. :
  198. m_initialized(false)
  199. {
  200. construct(expr,tag);
  201. }
  202. #endif
  203. optional_base& operator= ( optional_base const& rhs )
  204. {
  205. this->assign(rhs);
  206. return *this;
  207. }
  208. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  209. optional_base& operator= ( optional_base && rhs )
  210. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  211. {
  212. this->assign(static_cast<optional_base&&>(rhs));
  213. return *this;
  214. }
  215. #endif
  216. // No-throw (assuming T::~T() doesn't)
  217. ~optional_base() { destroy() ; }
  218. // Assigns from another optional<T> (deep-copies the rhs value)
  219. void assign ( optional_base const& rhs )
  220. {
  221. if (is_initialized())
  222. {
  223. if ( rhs.is_initialized() )
  224. assign_value(rhs.get_impl());
  225. else destroy();
  226. }
  227. else
  228. {
  229. if ( rhs.is_initialized() )
  230. construct(rhs.get_impl());
  231. }
  232. }
  233. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  234. // Assigns from another optional<T> (deep-moves the rhs value)
  235. void assign ( optional_base&& rhs )
  236. {
  237. if (is_initialized())
  238. {
  239. if ( rhs.is_initialized() )
  240. assign_value( boost::move(rhs.get_impl()) );
  241. else destroy();
  242. }
  243. else
  244. {
  245. if ( rhs.is_initialized() )
  246. construct(boost::move(rhs.get_impl()));
  247. }
  248. }
  249. #endif
  250. // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
  251. template<class U>
  252. void assign ( optional<U> const& rhs )
  253. {
  254. if (is_initialized())
  255. {
  256. if ( rhs.is_initialized() )
  257. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  258. assign_value( rhs.get() );
  259. #else
  260. assign_value( static_cast<value_type>(rhs.get()) );
  261. #endif
  262. else destroy();
  263. }
  264. else
  265. {
  266. if ( rhs.is_initialized() )
  267. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  268. construct(rhs.get());
  269. #else
  270. construct(static_cast<value_type>(rhs.get()));
  271. #endif
  272. }
  273. }
  274. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  275. // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
  276. template<class U>
  277. void assign ( optional<U>&& rhs )
  278. {
  279. typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
  280. if (is_initialized())
  281. {
  282. if ( rhs.is_initialized() )
  283. assign_value( static_cast<ref_type>(rhs.get()) );
  284. else destroy();
  285. }
  286. else
  287. {
  288. if ( rhs.is_initialized() )
  289. construct(static_cast<ref_type>(rhs.get()));
  290. }
  291. }
  292. #endif
  293. // Assigns from a T (deep-copies the rhs value)
  294. void assign ( argument_type val )
  295. {
  296. if (is_initialized())
  297. assign_value(val);
  298. else construct(val);
  299. }
  300. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  301. // Assigns from a T (deep-moves the rhs value)
  302. void assign ( rval_reference_type val )
  303. {
  304. if (is_initialized())
  305. assign_value( boost::move(val) );
  306. else construct( boost::move(val) );
  307. }
  308. #endif
  309. // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
  310. // No-throw (assuming T::~T() doesn't)
  311. void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
  312. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  313. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  314. template<class Expr, class ExprPtr>
  315. void assign_expr ( Expr&& expr, ExprPtr const* tag )
  316. {
  317. if (is_initialized())
  318. assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
  319. else construct(boost::forward<Expr>(expr),tag);
  320. }
  321. #else
  322. template<class Expr>
  323. void assign_expr ( Expr const& expr, Expr const* tag )
  324. {
  325. if (is_initialized())
  326. assign_expr_to_initialized(expr,tag);
  327. else construct(expr,tag);
  328. }
  329. #endif
  330. #endif
  331. public :
  332. // Destroys the current value, if any, leaving this UNINITIALIZED
  333. // No-throw (assuming T::~T() doesn't)
  334. void reset() BOOST_NOEXCEPT { destroy(); }
  335. // **DEPPRECATED** Replaces the current value -if any- with 'val'
  336. void reset ( argument_type val ) { assign(val); }
  337. // Returns a pointer to the value if this is initialized, otherwise,
  338. // returns NULL.
  339. // No-throw
  340. pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
  341. pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
  342. bool is_initialized() const BOOST_NOEXCEPT { return m_initialized ; }
  343. protected :
  344. void construct ( argument_type val )
  345. {
  346. ::new (m_storage.address()) value_type(val) ;
  347. m_initialized = true ;
  348. }
  349. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  350. void construct ( rval_reference_type val )
  351. {
  352. ::new (m_storage.address()) value_type( boost::move(val) ) ;
  353. m_initialized = true ;
  354. }
  355. #endif
  356. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  357. // Constructs in-place
  358. // upon exception *this is always uninitialized
  359. template<class... Args>
  360. void construct ( in_place_init_t, Args&&... args )
  361. {
  362. ::new (m_storage.address()) value_type( boost::forward<Args>(args)... ) ;
  363. m_initialized = true ;
  364. }
  365. template<class... Args>
  366. void emplace_assign ( Args&&... args )
  367. {
  368. destroy();
  369. construct(in_place_init, boost::forward<Args>(args)...);
  370. }
  371. template<class... Args>
  372. explicit optional_base ( in_place_init_t, Args&&... args )
  373. :
  374. m_initialized(false)
  375. {
  376. construct(in_place_init, boost::forward<Args>(args)...);
  377. }
  378. template<class... Args>
  379. explicit optional_base ( in_place_init_if_t, bool cond, Args&&... args )
  380. :
  381. m_initialized(false)
  382. {
  383. if ( cond )
  384. construct(in_place_init, boost::forward<Args>(args)...);
  385. }
  386. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  387. template<class Arg>
  388. void construct ( in_place_init_t, Arg&& arg )
  389. {
  390. ::new (m_storage.address()) value_type( boost::forward<Arg>(arg) );
  391. m_initialized = true ;
  392. }
  393. void construct ( in_place_init_t )
  394. {
  395. ::new (m_storage.address()) value_type();
  396. m_initialized = true ;
  397. }
  398. template<class Arg>
  399. void emplace_assign ( Arg&& arg )
  400. {
  401. destroy();
  402. construct(in_place_init, boost::forward<Arg>(arg)) ;
  403. }
  404. void emplace_assign ()
  405. {
  406. destroy();
  407. construct(in_place_init) ;
  408. }
  409. template<class Arg>
  410. explicit optional_base ( in_place_init_t, Arg&& arg )
  411. :
  412. m_initialized(false)
  413. {
  414. construct(in_place_init, boost::forward<Arg>(arg));
  415. }
  416. explicit optional_base ( in_place_init_t )
  417. :
  418. m_initialized(false)
  419. {
  420. construct(in_place_init);
  421. }
  422. template<class Arg>
  423. explicit optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
  424. :
  425. m_initialized(false)
  426. {
  427. if ( cond )
  428. construct(in_place_init, boost::forward<Arg>(arg));
  429. }
  430. explicit optional_base ( in_place_init_if_t, bool cond )
  431. :
  432. m_initialized(false)
  433. {
  434. if ( cond )
  435. construct(in_place_init);
  436. }
  437. #else
  438. template<class Arg>
  439. void construct ( in_place_init_t, const Arg& arg )
  440. {
  441. ::new (m_storage.address()) value_type( arg );
  442. m_initialized = true ;
  443. }
  444. template<class Arg>
  445. void construct ( in_place_init_t, Arg& arg )
  446. {
  447. ::new (m_storage.address()) value_type( arg );
  448. m_initialized = true ;
  449. }
  450. void construct ( in_place_init_t )
  451. {
  452. ::new (m_storage.address()) value_type();
  453. m_initialized = true ;
  454. }
  455. template<class Arg>
  456. void emplace_assign ( const Arg& arg )
  457. {
  458. destroy();
  459. construct(in_place_init, arg);
  460. }
  461. template<class Arg>
  462. void emplace_assign ( Arg& arg )
  463. {
  464. destroy();
  465. construct(in_place_init, arg);
  466. }
  467. void emplace_assign ()
  468. {
  469. destroy();
  470. construct(in_place_init);
  471. }
  472. template<class Arg>
  473. explicit optional_base ( in_place_init_t, const Arg& arg )
  474. : m_initialized(false)
  475. {
  476. construct(in_place_init, arg);
  477. }
  478. template<class Arg>
  479. explicit optional_base ( in_place_init_t, Arg& arg )
  480. : m_initialized(false)
  481. {
  482. construct(in_place_init, arg);
  483. }
  484. explicit optional_base ( in_place_init_t )
  485. : m_initialized(false)
  486. {
  487. construct(in_place_init);
  488. }
  489. template<class Arg>
  490. explicit optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
  491. : m_initialized(false)
  492. {
  493. if ( cond )
  494. construct(in_place_init, arg);
  495. }
  496. template<class Arg>
  497. explicit optional_base ( in_place_init_if_t, bool cond, Arg& arg )
  498. : m_initialized(false)
  499. {
  500. if ( cond )
  501. construct(in_place_init, arg);
  502. }
  503. explicit optional_base ( in_place_init_if_t, bool cond )
  504. : m_initialized(false)
  505. {
  506. if ( cond )
  507. construct(in_place_init);
  508. }
  509. #endif
  510. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  511. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  512. // Constructs in-place using the given factory
  513. template<class Expr>
  514. void construct ( Expr&& factory, in_place_factory_base const* )
  515. {
  516. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  517. m_initialized = true ;
  518. }
  519. // Constructs in-place using the given typed factory
  520. template<class Expr>
  521. void construct ( Expr&& factory, typed_in_place_factory_base const* )
  522. {
  523. factory.apply(m_storage.address()) ;
  524. m_initialized = true ;
  525. }
  526. template<class Expr>
  527. void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
  528. {
  529. destroy();
  530. construct(factory,tag);
  531. }
  532. // Constructs in-place using the given typed factory
  533. template<class Expr>
  534. void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
  535. {
  536. destroy();
  537. construct(factory,tag);
  538. }
  539. #else
  540. // Constructs in-place using the given factory
  541. template<class Expr>
  542. void construct ( Expr const& factory, in_place_factory_base const* )
  543. {
  544. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  545. m_initialized = true ;
  546. }
  547. // Constructs in-place using the given typed factory
  548. template<class Expr>
  549. void construct ( Expr const& factory, typed_in_place_factory_base const* )
  550. {
  551. factory.apply(m_storage.address()) ;
  552. m_initialized = true ;
  553. }
  554. template<class Expr>
  555. void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
  556. {
  557. destroy();
  558. construct(factory,tag);
  559. }
  560. // Constructs in-place using the given typed factory
  561. template<class Expr>
  562. void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
  563. {
  564. destroy();
  565. construct(factory,tag);
  566. }
  567. #endif
  568. #endif
  569. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  570. // Constructs using any expression implicitly convertible to the single argument
  571. // of a one-argument T constructor.
  572. // Converting constructions of optional<T> from optional<U> uses this function with
  573. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  574. template<class Expr>
  575. void construct ( Expr&& expr, void const* )
  576. {
  577. new (m_storage.address()) value_type(boost::forward<Expr>(expr)) ;
  578. m_initialized = true ;
  579. }
  580. // Assigns using a form any expression implicitly convertible to the single argument
  581. // of a T's assignment operator.
  582. // Converting assignments of optional<T> from optional<U> uses this function with
  583. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  584. template<class Expr>
  585. void assign_expr_to_initialized ( Expr&& expr, void const* )
  586. {
  587. assign_value( boost::forward<Expr>(expr) );
  588. }
  589. #else
  590. // Constructs using any expression implicitly convertible to the single argument
  591. // of a one-argument T constructor.
  592. // Converting constructions of optional<T> from optional<U> uses this function with
  593. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  594. template<class Expr>
  595. void construct ( Expr const& expr, void const* )
  596. {
  597. new (m_storage.address()) value_type(expr) ;
  598. m_initialized = true ;
  599. }
  600. // Assigns using a form any expression implicitly convertible to the single argument
  601. // of a T's assignment operator.
  602. // Converting assignments of optional<T> from optional<U> uses this function with
  603. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  604. template<class Expr>
  605. void assign_expr_to_initialized ( Expr const& expr, void const* )
  606. {
  607. assign_value(expr);
  608. }
  609. #endif
  610. #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  611. // BCB5.64 (and probably lower versions) workaround.
  612. // The in-place factories are supported by means of catch-all constructors
  613. // and assignment operators (the functions are parameterized in terms of
  614. // an arbitrary 'Expr' type)
  615. // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
  616. // to the 'Expr'-taking functions even though explicit overloads are present for them.
  617. // Thus, the following overload is needed to properly handle the case when the 'lhs'
  618. // is another optional.
  619. //
  620. // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
  621. // instead of choosing the wrong overload
  622. //
  623. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  624. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  625. template<class Expr>
  626. void construct ( Expr&& expr, optional_tag const* )
  627. {
  628. if ( expr.is_initialized() )
  629. {
  630. // An exception can be thrown here.
  631. // It it happens, THIS will be left uninitialized.
  632. new (m_storage.address()) value_type(boost::move(expr.get())) ;
  633. m_initialized = true ;
  634. }
  635. }
  636. #else
  637. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  638. template<class Expr>
  639. void construct ( Expr const& expr, optional_tag const* )
  640. {
  641. if ( expr.is_initialized() )
  642. {
  643. // An exception can be thrown here.
  644. // It it happens, THIS will be left uninitialized.
  645. new (m_storage.address()) value_type(expr.get()) ;
  646. m_initialized = true ;
  647. }
  648. }
  649. #endif
  650. #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  651. void assign_value ( argument_type val ) { get_impl() = val; }
  652. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  653. void assign_value ( rval_reference_type val ) { get_impl() = static_cast<rval_reference_type>(val); }
  654. #endif
  655. void destroy()
  656. {
  657. if ( m_initialized )
  658. destroy_impl() ;
  659. }
  660. reference_const_type get_impl() const { return m_storage.ref() ; }
  661. reference_type get_impl() { return m_storage.ref() ; }
  662. pointer_const_type get_ptr_impl() const { return m_storage.ptr_ref(); }
  663. pointer_type get_ptr_impl() { return m_storage.ptr_ref(); }
  664. private :
  665. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1900))
  666. void destroy_impl ( ) { m_storage.ptr_ref()->~T() ; m_initialized = false ; }
  667. #else
  668. void destroy_impl ( ) { m_storage.ref().T::~T() ; m_initialized = false ; }
  669. #endif
  670. bool m_initialized ;
  671. storage_type m_storage ;
  672. } ;
  673. #include <boost/optional/detail/optional_trivially_copyable_base.hpp>
  674. // definition of metafunction is_optional_val_init_candidate
  675. template <typename U>
  676. struct is_optional_related
  677. : boost::conditional< boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
  678. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, none_t>::value
  679. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_t>::value
  680. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_if_t>::value,
  681. boost::true_type, boost::false_type>::type
  682. {};
  683. #if !defined(BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT)
  684. template <typename T, typename U>
  685. struct is_convertible_to_T_or_factory
  686. : boost::conditional< boost::is_base_of<boost::in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
  687. || boost::is_base_of<boost::typed_in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
  688. || (boost::is_constructible<T, U&&>::value && !boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value)
  689. , boost::true_type, boost::false_type>::type
  690. {};
  691. template <typename T, typename U>
  692. struct is_optional_constructible : boost::is_constructible<T, U>
  693. {};
  694. #else
  695. template <typename, typename>
  696. struct is_convertible_to_T_or_factory : boost::true_type
  697. {};
  698. template <typename T, typename U>
  699. struct is_optional_constructible : boost::true_type
  700. {};
  701. #endif // is_convertible condition
  702. template <typename T, typename U, bool = is_optional_related<U>::value>
  703. struct is_optional_val_init_candidate
  704. : boost::false_type
  705. {};
  706. template <typename T, typename U>
  707. struct is_optional_val_init_candidate<T, U, false>
  708. : boost::conditional< is_convertible_to_T_or_factory<T, U>::value
  709. , boost::true_type, boost::false_type>::type
  710. {};
  711. } // namespace optional_detail
  712. namespace optional_config {
  713. template <typename T>
  714. struct optional_uses_direct_storage_for
  715. : boost::conditional<(boost::is_scalar<T>::value && !boost::is_const<T>::value && !boost::is_volatile<T>::value)
  716. , boost::true_type, boost::false_type>::type
  717. {};
  718. } // namespace optional_config
  719. #ifndef BOOST_OPTIONAL_DETAIL_NO_DIRECT_STORAGE_SPEC
  720. # define BOOST_OPTIONAL_BASE_TYPE(T) boost::conditional< optional_config::optional_uses_direct_storage_for<T>::value, \
  721. optional_detail::tc_optional_base<T>, \
  722. optional_detail::optional_base<T> \
  723. >::type
  724. #else
  725. # define BOOST_OPTIONAL_BASE_TYPE(T) optional_detail::optional_base<T>
  726. #endif
  727. template<class T>
  728. class optional
  729. : public BOOST_OPTIONAL_BASE_TYPE(T)
  730. {
  731. typedef typename BOOST_OPTIONAL_BASE_TYPE(T) base ;
  732. public :
  733. typedef optional<T> this_type ;
  734. typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
  735. typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
  736. typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
  737. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  738. typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
  739. typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
  740. #endif
  741. typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
  742. typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
  743. typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
  744. // Creates an optional<T> uninitialized.
  745. // No-throw
  746. optional() BOOST_NOEXCEPT : base() {}
  747. // Creates an optional<T> uninitialized.
  748. // No-throw
  749. optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
  750. // Creates an optional<T> initialized with 'val'.
  751. // Can throw if T::T(T const&) does
  752. optional ( argument_type val ) : base(optional_detail::init_value_tag(), val) {}
  753. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  754. // Creates an optional<T> initialized with 'move(val)'.
  755. // Can throw if T::T(T &&) does
  756. optional ( rval_reference_type val ) : base(optional_detail::init_value_tag(), boost::forward<T>(val))
  757. {}
  758. #endif
  759. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  760. // Can throw if T::T(T const&) does
  761. optional ( bool cond, argument_type val ) : base(cond,val) {}
  762. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  763. /// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  764. // Can throw if T::T(T &&) does
  765. optional ( bool cond, rval_reference_type val ) : base( cond, boost::forward<T>(val) )
  766. {}
  767. #endif
  768. // NOTE: MSVC needs templated versions first
  769. // Creates a deep copy of another convertible optional<U>
  770. // Requires a valid conversion from U to T.
  771. // Can throw if T::T(U const&) does
  772. template<class U>
  773. explicit optional ( optional<U> const& rhs
  774. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  775. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U const&>, bool>::type = true
  776. #endif
  777. )
  778. :
  779. base()
  780. {
  781. if ( rhs.is_initialized() )
  782. this->construct(rhs.get());
  783. }
  784. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  785. // Creates a deep move of another convertible optional<U>
  786. // Requires a valid conversion from U to T.
  787. // Can throw if T::T(U&&) does
  788. template<class U>
  789. explicit optional ( optional<U> && rhs
  790. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  791. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U>, bool>::type = true
  792. #endif
  793. )
  794. :
  795. base()
  796. {
  797. if ( rhs.is_initialized() )
  798. this->construct( boost::move(rhs.get()) );
  799. }
  800. #endif
  801. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  802. // Creates an optional<T> with an expression which can be either
  803. // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
  804. // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
  805. // (c) Any expression implicitly convertible to the single type
  806. // of a one-argument T's constructor.
  807. // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
  808. // even though explicit overloads are present for these.
  809. // Depending on the above some T ctor is called.
  810. // Can throw if the resolved T ctor throws.
  811. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  812. template<class Expr>
  813. explicit optional ( Expr&& expr,
  814. BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true
  815. )
  816. : base(boost::forward<Expr>(expr),boost::addressof(expr))
  817. {}
  818. #else
  819. template<class Expr>
  820. explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
  821. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  822. #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  823. // Creates a deep copy of another optional<T>
  824. // Can throw if T::T(T const&) does
  825. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  826. optional ( optional const& ) = default;
  827. #else
  828. optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
  829. #endif
  830. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  831. // Creates a deep move of another optional<T>
  832. // Can throw if T::T(T&&) does
  833. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  834. optional ( optional && ) = default;
  835. #else
  836. optional ( optional && rhs )
  837. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  838. : base( boost::move(rhs) )
  839. {}
  840. #endif
  841. #endif
  842. #if BOOST_WORKAROUND(_MSC_VER, <= 1600)
  843. // On old MSVC compilers the implicitly declared dtor is not called
  844. ~optional() {}
  845. #endif
  846. #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  847. // Assigns from an expression. See corresponding constructor.
  848. // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
  849. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  850. template<class Expr>
  851. BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_init_candidate<T, Expr>, optional&>::type
  852. operator= ( Expr&& expr )
  853. {
  854. this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
  855. return *this ;
  856. }
  857. #else
  858. template<class Expr>
  859. optional& operator= ( Expr const& expr )
  860. {
  861. this->assign_expr(expr,boost::addressof(expr));
  862. return *this ;
  863. }
  864. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  865. #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  866. // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
  867. // Requires a valid conversion from U to T.
  868. // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
  869. template<class U>
  870. optional& operator= ( optional<U> const& rhs )
  871. {
  872. this->assign(rhs);
  873. return *this ;
  874. }
  875. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  876. // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
  877. // Requires a valid conversion from U to T.
  878. // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
  879. template<class U>
  880. optional& operator= ( optional<U> && rhs )
  881. {
  882. this->assign(boost::move(rhs));
  883. return *this ;
  884. }
  885. #endif
  886. // Assigns from another optional<T> (deep-copies the rhs value)
  887. // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
  888. // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
  889. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  890. optional& operator= ( optional const& rhs ) = default;
  891. #else
  892. optional& operator= ( optional const& rhs )
  893. {
  894. this->assign( static_cast<base const&>(rhs) ) ;
  895. return *this ;
  896. }
  897. #endif
  898. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  899. // Assigns from another optional<T> (deep-moves the rhs value)
  900. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  901. optional& operator= ( optional && ) = default;
  902. #else
  903. optional& operator= ( optional && rhs )
  904. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  905. {
  906. this->assign( static_cast<base &&>(rhs) ) ;
  907. return *this ;
  908. }
  909. #endif
  910. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  911. #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  912. // Assigns from a T (deep-moves/copies the rhs value)
  913. template <typename T_>
  914. BOOST_DEDUCED_TYPENAME boost::enable_if<boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<T_>::type>, optional&>::type
  915. operator= ( T_&& val )
  916. {
  917. this->assign( boost::forward<T_>(val) ) ;
  918. return *this ;
  919. }
  920. #else
  921. // Assigns from a T (deep-copies the rhs value)
  922. // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
  923. optional& operator= ( argument_type val )
  924. {
  925. this->assign( val ) ;
  926. return *this ;
  927. }
  928. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  929. // Assigns from a T (deep-moves the rhs value)
  930. optional& operator= ( rval_reference_type val )
  931. {
  932. this->assign( boost::move(val) ) ;
  933. return *this ;
  934. }
  935. #endif
  936. #endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  937. // Assigns from a "none"
  938. // Which destroys the current value, if any, leaving this UNINITIALIZED
  939. // No-throw (assuming T::~T() doesn't)
  940. optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
  941. {
  942. this->assign( none_ ) ;
  943. return *this ;
  944. }
  945. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  946. // Constructs in-place
  947. // upon exception *this is always uninitialized
  948. template<class... Args>
  949. void emplace ( Args&&... args )
  950. {
  951. this->emplace_assign( boost::forward<Args>(args)... );
  952. }
  953. template<class... Args>
  954. explicit optional ( in_place_init_t, Args&&... args )
  955. : base( in_place_init, boost::forward<Args>(args)... )
  956. {}
  957. template<class... Args>
  958. explicit optional ( in_place_init_if_t, bool cond, Args&&... args )
  959. : base( in_place_init_if, cond, boost::forward<Args>(args)... )
  960. {}
  961. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  962. template<class Arg>
  963. void emplace ( Arg&& arg )
  964. {
  965. this->emplace_assign( boost::forward<Arg>(arg) );
  966. }
  967. void emplace ()
  968. {
  969. this->emplace_assign();
  970. }
  971. template<class Args>
  972. explicit optional ( in_place_init_t, Args&& args )
  973. : base( in_place_init, boost::forward<Args>(args) )
  974. {}
  975. explicit optional ( in_place_init_t )
  976. : base( in_place_init )
  977. {}
  978. template<class Args>
  979. explicit optional ( in_place_init_if_t, bool cond, Args&& args )
  980. : base( in_place_init_if, cond, boost::forward<Args>(args) )
  981. {}
  982. explicit optional ( in_place_init_if_t, bool cond )
  983. : base( in_place_init_if, cond )
  984. {}
  985. #else
  986. template<class Arg>
  987. void emplace ( const Arg& arg )
  988. {
  989. this->emplace_assign( arg );
  990. }
  991. template<class Arg>
  992. void emplace ( Arg& arg )
  993. {
  994. this->emplace_assign( arg );
  995. }
  996. void emplace ()
  997. {
  998. this->emplace_assign();
  999. }
  1000. template<class Arg>
  1001. explicit optional ( in_place_init_t, const Arg& arg )
  1002. : base( in_place_init, arg )
  1003. {}
  1004. template<class Arg>
  1005. explicit optional ( in_place_init_t, Arg& arg )
  1006. : base( in_place_init, arg )
  1007. {}
  1008. explicit optional ( in_place_init_t )
  1009. : base( in_place_init )
  1010. {}
  1011. template<class Arg>
  1012. explicit optional ( in_place_init_if_t, bool cond, const Arg& arg )
  1013. : base( in_place_init_if, cond, arg )
  1014. {}
  1015. template<class Arg>
  1016. explicit optional ( in_place_init_if_t, bool cond, Arg& arg )
  1017. : base( in_place_init_if, cond, arg )
  1018. {}
  1019. explicit optional ( in_place_init_if_t, bool cond )
  1020. : base( in_place_init_if, cond )
  1021. {}
  1022. #endif
  1023. void swap( optional & arg )
  1024. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  1025. {
  1026. // allow for Koenig lookup
  1027. boost::swap(*this, arg);
  1028. }
  1029. // Returns a reference to the value if this is initialized, otherwise,
  1030. // the behaviour is UNDEFINED
  1031. // No-throw
  1032. reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  1033. reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  1034. // Returns a copy of the value if this is initialized, 'v' otherwise
  1035. reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
  1036. reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
  1037. // Returns a pointer to the value if this is initialized, otherwise,
  1038. // the behaviour is UNDEFINED
  1039. // No-throw
  1040. pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  1041. pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  1042. // Returns a reference to the value if this is initialized, otherwise,
  1043. // the behaviour is UNDEFINED
  1044. // No-throw
  1045. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1046. reference_const_type operator *() const& { return this->get() ; }
  1047. reference_type operator *() & { return this->get() ; }
  1048. reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
  1049. #else
  1050. reference_const_type operator *() const { return this->get() ; }
  1051. reference_type operator *() { return this->get() ; }
  1052. #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
  1053. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1054. reference_const_type value() const&
  1055. {
  1056. if (this->is_initialized())
  1057. return this->get() ;
  1058. else
  1059. throw_exception(bad_optional_access());
  1060. }
  1061. reference_type value() &
  1062. {
  1063. if (this->is_initialized())
  1064. return this->get() ;
  1065. else
  1066. throw_exception(bad_optional_access());
  1067. }
  1068. reference_type_of_temporary_wrapper value() &&
  1069. {
  1070. if (this->is_initialized())
  1071. return boost::move(this->get()) ;
  1072. else
  1073. throw_exception(bad_optional_access());
  1074. }
  1075. #else
  1076. reference_const_type value() const
  1077. {
  1078. if (this->is_initialized())
  1079. return this->get() ;
  1080. else
  1081. throw_exception(bad_optional_access());
  1082. }
  1083. reference_type value()
  1084. {
  1085. if (this->is_initialized())
  1086. return this->get() ;
  1087. else
  1088. throw_exception(bad_optional_access());
  1089. }
  1090. #endif
  1091. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1092. template <class U>
  1093. value_type value_or ( U&& v ) const&
  1094. {
  1095. if (this->is_initialized())
  1096. return get();
  1097. else
  1098. return boost::forward<U>(v);
  1099. }
  1100. template <class U>
  1101. value_type value_or ( U&& v ) &&
  1102. {
  1103. if (this->is_initialized())
  1104. return boost::move(get());
  1105. else
  1106. return boost::forward<U>(v);
  1107. }
  1108. #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1109. template <class U>
  1110. value_type value_or ( U&& v ) const
  1111. {
  1112. if (this->is_initialized())
  1113. return get();
  1114. else
  1115. return boost::forward<U>(v);
  1116. }
  1117. #else
  1118. template <class U>
  1119. value_type value_or ( U const& v ) const
  1120. {
  1121. if (this->is_initialized())
  1122. return get();
  1123. else
  1124. return v;
  1125. }
  1126. template <class U>
  1127. value_type value_or ( U& v ) const
  1128. {
  1129. if (this->is_initialized())
  1130. return get();
  1131. else
  1132. return v;
  1133. }
  1134. #endif
  1135. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1136. template <typename F>
  1137. value_type value_or_eval ( F f ) const&
  1138. {
  1139. if (this->is_initialized())
  1140. return get();
  1141. else
  1142. return f();
  1143. }
  1144. template <typename F>
  1145. value_type value_or_eval ( F f ) &&
  1146. {
  1147. if (this->is_initialized())
  1148. return boost::move(get());
  1149. else
  1150. return f();
  1151. }
  1152. template <typename F>
  1153. optional<typename boost::result_of<F(reference_type)>::type> map(F f) &
  1154. {
  1155. if (this->has_value())
  1156. return f(get());
  1157. else
  1158. return none;
  1159. }
  1160. template <typename F>
  1161. optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const&
  1162. {
  1163. if (this->has_value())
  1164. return f(get());
  1165. else
  1166. return none;
  1167. }
  1168. template <typename F>
  1169. optional<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type> map(F f) &&
  1170. {
  1171. if (this->has_value())
  1172. return f(boost::move(this->get()));
  1173. else
  1174. return none;
  1175. }
  1176. template <typename F>
  1177. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f) &
  1178. {
  1179. if (this->has_value())
  1180. return f(get());
  1181. else
  1182. return none;
  1183. }
  1184. template <typename F>
  1185. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const&
  1186. {
  1187. if (this->has_value())
  1188. return f(get());
  1189. else
  1190. return none;
  1191. }
  1192. template <typename F>
  1193. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type>::type> flat_map(F f) &&
  1194. {
  1195. if (this->has_value())
  1196. return f(boost::move(get()));
  1197. else
  1198. return none;
  1199. }
  1200. #else
  1201. template <typename F>
  1202. value_type value_or_eval ( F f ) const
  1203. {
  1204. if (this->is_initialized())
  1205. return get();
  1206. else
  1207. return f();
  1208. }
  1209. template <typename F>
  1210. optional<typename boost::result_of<F(reference_type)>::type> map(F f)
  1211. {
  1212. if (this->has_value())
  1213. return f(get());
  1214. else
  1215. return none;
  1216. }
  1217. template <typename F>
  1218. optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const
  1219. {
  1220. if (this->has_value())
  1221. return f(get());
  1222. else
  1223. return none;
  1224. }
  1225. template <typename F>
  1226. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f)
  1227. {
  1228. if (this->has_value())
  1229. return f(get());
  1230. else
  1231. return none;
  1232. }
  1233. template <typename F>
  1234. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const
  1235. {
  1236. if (this->has_value())
  1237. return f(get());
  1238. else
  1239. return none;
  1240. }
  1241. #endif
  1242. bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; }
  1243. bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
  1244. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  1245. } ;
  1246. } // namespace boost
  1247. #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
  1248. namespace boost {
  1249. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1250. template<class T>
  1251. class optional<T&&>
  1252. {
  1253. BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal.");
  1254. } ;
  1255. #endif
  1256. } // namespace boost
  1257. #ifndef BOOST_OPTIONAL_CONFIG_DONT_SPECIALIZE_OPTIONAL_REFS
  1258. # include <boost/optional/detail/optional_reference_spec.hpp>
  1259. #endif
  1260. namespace boost {
  1261. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1262. template<class T>
  1263. inline
  1264. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( T && v )
  1265. {
  1266. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(boost::forward<T>(v));
  1267. }
  1268. // Returns optional<T>(cond,v)
  1269. template<class T>
  1270. inline
  1271. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( bool cond, T && v )
  1272. {
  1273. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(cond,boost::forward<T>(v));
  1274. }
  1275. #else
  1276. // Returns optional<T>(v)
  1277. template<class T>
  1278. inline
  1279. optional<T> make_optional ( T const& v )
  1280. {
  1281. return optional<T>(v);
  1282. }
  1283. // Returns optional<T>(cond,v)
  1284. template<class T>
  1285. inline
  1286. optional<T> make_optional ( bool cond, T const& v )
  1287. {
  1288. return optional<T>(cond,v);
  1289. }
  1290. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1291. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  1292. // No-throw
  1293. template<class T>
  1294. inline
  1295. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  1296. get ( optional<T> const& opt )
  1297. {
  1298. return opt.get() ;
  1299. }
  1300. template<class T>
  1301. inline
  1302. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  1303. get ( optional<T>& opt )
  1304. {
  1305. return opt.get() ;
  1306. }
  1307. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  1308. // No-throw
  1309. template<class T>
  1310. inline
  1311. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  1312. get ( optional<T> const* opt )
  1313. {
  1314. return opt->get_ptr() ;
  1315. }
  1316. template<class T>
  1317. inline
  1318. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  1319. get ( optional<T>* opt )
  1320. {
  1321. return opt->get_ptr() ;
  1322. }
  1323. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  1324. // No-throw
  1325. template<class T>
  1326. inline
  1327. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  1328. get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
  1329. {
  1330. return opt.get_value_or(v) ;
  1331. }
  1332. template<class T>
  1333. inline
  1334. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  1335. get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
  1336. {
  1337. return opt.get_value_or(v) ;
  1338. }
  1339. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  1340. // No-throw
  1341. template<class T>
  1342. inline
  1343. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  1344. get_pointer ( optional<T> const& opt )
  1345. {
  1346. return opt.get_ptr() ;
  1347. }
  1348. template<class T>
  1349. inline
  1350. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  1351. get_pointer ( optional<T>& opt )
  1352. {
  1353. return opt.get_ptr() ;
  1354. }
  1355. } // namespace boost
  1356. namespace boost {
  1357. // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
  1358. template<class CharType, class CharTrait>
  1359. std::basic_ostream<CharType, CharTrait>&
  1360. operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&)
  1361. {
  1362. BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
  1363. return os;
  1364. }
  1365. } // namespace boost
  1366. #include <boost/optional/detail/optional_relops.hpp>
  1367. #include <boost/optional/detail/optional_swap.hpp>
  1368. #endif // header guard