optional.hpp 47 KB

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