optional.hpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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 metafunciton 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>
  703. struct is_optional_val_init_candidate
  704. : boost::conditional< !is_optional_related<U>::value && is_convertible_to_T_or_factory<T, U>::value
  705. , boost::true_type, boost::false_type>::type
  706. {};
  707. } // namespace optional_detail
  708. namespace optional_config {
  709. template <typename T>
  710. struct optional_uses_direct_storage_for
  711. : boost::conditional<(boost::is_scalar<T>::value && !boost::is_const<T>::value && !boost::is_volatile<T>::value)
  712. , boost::true_type, boost::false_type>::type
  713. {};
  714. } // namespace optional_config
  715. #ifndef BOOST_OPTIONAL_DETAIL_NO_DIRECT_STORAGE_SPEC
  716. # define BOOST_OPTIONAL_BASE_TYPE(T) boost::conditional< optional_config::optional_uses_direct_storage_for<T>::value, \
  717. optional_detail::tc_optional_base<T>, \
  718. optional_detail::optional_base<T> \
  719. >::type
  720. #else
  721. # define BOOST_OPTIONAL_BASE_TYPE(T) optional_detail::optional_base<T>
  722. #endif
  723. template<class T>
  724. class optional
  725. : public BOOST_OPTIONAL_BASE_TYPE(T)
  726. {
  727. typedef typename BOOST_OPTIONAL_BASE_TYPE(T) base ;
  728. public :
  729. typedef optional<T> this_type ;
  730. typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
  731. typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
  732. typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
  733. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  734. typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
  735. typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
  736. #endif
  737. typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
  738. typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
  739. typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
  740. // Creates an optional<T> uninitialized.
  741. // No-throw
  742. optional() BOOST_NOEXCEPT : base() {}
  743. // Creates an optional<T> uninitialized.
  744. // No-throw
  745. optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
  746. // Creates an optional<T> initialized with 'val'.
  747. // Can throw if T::T(T const&) does
  748. optional ( argument_type val ) : base(optional_detail::init_value_tag(), val) {}
  749. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  750. // Creates an optional<T> initialized with 'move(val)'.
  751. // Can throw if T::T(T &&) does
  752. optional ( rval_reference_type val ) : base(optional_detail::init_value_tag(), boost::forward<T>(val))
  753. {}
  754. #endif
  755. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  756. // Can throw if T::T(T const&) does
  757. optional ( bool cond, argument_type val ) : base(cond,val) {}
  758. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  759. /// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  760. // Can throw if T::T(T &&) does
  761. optional ( bool cond, rval_reference_type val ) : base( cond, boost::forward<T>(val) )
  762. {}
  763. #endif
  764. // NOTE: MSVC needs templated versions first
  765. // Creates a deep copy of another convertible optional<U>
  766. // Requires a valid conversion from U to T.
  767. // Can throw if T::T(U const&) does
  768. template<class U>
  769. explicit optional ( optional<U> const& rhs
  770. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  771. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U const&>, bool>::type = true
  772. #endif
  773. )
  774. :
  775. base()
  776. {
  777. if ( rhs.is_initialized() )
  778. this->construct(rhs.get());
  779. }
  780. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  781. // Creates a deep move of another convertible optional<U>
  782. // Requires a valid conversion from U to T.
  783. // Can throw if T::T(U&&) does
  784. template<class U>
  785. explicit optional ( optional<U> && rhs
  786. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  787. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U>, bool>::type = true
  788. #endif
  789. )
  790. :
  791. base()
  792. {
  793. if ( rhs.is_initialized() )
  794. this->construct( boost::move(rhs.get()) );
  795. }
  796. #endif
  797. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  798. // Creates an optional<T> with an expression which can be either
  799. // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
  800. // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
  801. // (c) Any expression implicitly convertible to the single type
  802. // of a one-argument T's constructor.
  803. // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
  804. // even though explicit overloads are present for these.
  805. // Depending on the above some T ctor is called.
  806. // Can throw if the resolved T ctor throws.
  807. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  808. template<class Expr>
  809. explicit optional ( Expr&& expr,
  810. BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true
  811. )
  812. : base(boost::forward<Expr>(expr),boost::addressof(expr))
  813. {}
  814. #else
  815. template<class Expr>
  816. explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
  817. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  818. #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  819. // Creates a deep copy of another optional<T>
  820. // Can throw if T::T(T const&) does
  821. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  822. optional ( optional const& ) = default;
  823. #else
  824. optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
  825. #endif
  826. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  827. // Creates a deep move of another optional<T>
  828. // Can throw if T::T(T&&) does
  829. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  830. optional ( optional && rhs ) = default;
  831. #else
  832. optional ( optional && rhs )
  833. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  834. : base( boost::move(rhs) )
  835. {}
  836. #endif
  837. #endif
  838. #if BOOST_WORKAROUND(_MSC_VER, <= 1600)
  839. // On old MSVC compilers the implicitly declared dtor is not called
  840. ~optional() {}
  841. #endif
  842. #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  843. // Assigns from an expression. See corresponding constructor.
  844. // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
  845. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  846. template<class Expr>
  847. BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_init_candidate<T, Expr>, optional&>::type
  848. operator= ( Expr&& expr )
  849. {
  850. this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
  851. return *this ;
  852. }
  853. #else
  854. template<class Expr>
  855. optional& operator= ( Expr const& expr )
  856. {
  857. this->assign_expr(expr,boost::addressof(expr));
  858. return *this ;
  859. }
  860. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  861. #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  862. // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
  863. // Requires a valid conversion from U to T.
  864. // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
  865. template<class U>
  866. optional& operator= ( optional<U> const& rhs )
  867. {
  868. this->assign(rhs);
  869. return *this ;
  870. }
  871. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  872. // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
  873. // Requires a valid conversion from U to T.
  874. // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
  875. template<class U>
  876. optional& operator= ( optional<U> && rhs )
  877. {
  878. this->assign(boost::move(rhs));
  879. return *this ;
  880. }
  881. #endif
  882. // Assigns from another optional<T> (deep-copies the rhs value)
  883. // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
  884. // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
  885. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  886. optional& operator= ( optional const& rhs ) = default;
  887. #else
  888. optional& operator= ( optional const& rhs )
  889. {
  890. this->assign( static_cast<base const&>(rhs) ) ;
  891. return *this ;
  892. }
  893. #endif
  894. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  895. // Assigns from another optional<T> (deep-moves the rhs value)
  896. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  897. optional& operator= ( optional && ) = default;
  898. #else
  899. optional& operator= ( optional && rhs )
  900. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  901. {
  902. this->assign( static_cast<base &&>(rhs) ) ;
  903. return *this ;
  904. }
  905. #endif
  906. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  907. #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  908. // Assigns from a T (deep-moves/copies the rhs value)
  909. template <typename T_>
  910. BOOST_DEDUCED_TYPENAME boost::enable_if<boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<T_>::type>, optional&>::type
  911. operator= ( T_&& val )
  912. {
  913. this->assign( boost::forward<T_>(val) ) ;
  914. return *this ;
  915. }
  916. #else
  917. // Assigns from a T (deep-copies the rhs value)
  918. // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
  919. optional& operator= ( argument_type val )
  920. {
  921. this->assign( val ) ;
  922. return *this ;
  923. }
  924. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  925. // Assigns from a T (deep-moves the rhs value)
  926. optional& operator= ( rval_reference_type val )
  927. {
  928. this->assign( boost::move(val) ) ;
  929. return *this ;
  930. }
  931. #endif
  932. #endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  933. // Assigns from a "none"
  934. // Which destroys the current value, if any, leaving this UNINITIALIZED
  935. // No-throw (assuming T::~T() doesn't)
  936. optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
  937. {
  938. this->assign( none_ ) ;
  939. return *this ;
  940. }
  941. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  942. // Constructs in-place
  943. // upon exception *this is always uninitialized
  944. template<class... Args>
  945. void emplace ( Args&&... args )
  946. {
  947. this->emplace_assign( boost::forward<Args>(args)... );
  948. }
  949. template<class... Args>
  950. explicit optional ( in_place_init_t, Args&&... args )
  951. : base( in_place_init, boost::forward<Args>(args)... )
  952. {}
  953. template<class... Args>
  954. explicit optional ( in_place_init_if_t, bool cond, Args&&... args )
  955. : base( in_place_init_if, cond, boost::forward<Args>(args)... )
  956. {}
  957. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  958. template<class Arg>
  959. void emplace ( Arg&& arg )
  960. {
  961. this->emplace_assign( boost::forward<Arg>(arg) );
  962. }
  963. void emplace ()
  964. {
  965. this->emplace_assign();
  966. }
  967. template<class Args>
  968. explicit optional ( in_place_init_t, Args&& args )
  969. : base( in_place_init, boost::forward<Args>(args) )
  970. {}
  971. explicit optional ( in_place_init_t )
  972. : base( in_place_init )
  973. {}
  974. template<class Args>
  975. explicit optional ( in_place_init_if_t, bool cond, Args&& args )
  976. : base( in_place_init_if, cond, boost::forward<Args>(args) )
  977. {}
  978. explicit optional ( in_place_init_if_t, bool cond )
  979. : base( in_place_init_if, cond )
  980. {}
  981. #else
  982. template<class Arg>
  983. void emplace ( const Arg& arg )
  984. {
  985. this->emplace_assign( arg );
  986. }
  987. template<class Arg>
  988. void emplace ( Arg& arg )
  989. {
  990. this->emplace_assign( arg );
  991. }
  992. void emplace ()
  993. {
  994. this->emplace_assign();
  995. }
  996. template<class Arg>
  997. explicit optional ( in_place_init_t, const Arg& arg )
  998. : base( in_place_init, arg )
  999. {}
  1000. template<class Arg>
  1001. explicit optional ( in_place_init_t, Arg& arg )
  1002. : base( in_place_init, arg )
  1003. {}
  1004. explicit optional ( in_place_init_t )
  1005. : base( in_place_init )
  1006. {}
  1007. template<class Arg>
  1008. explicit optional ( in_place_init_if_t, bool cond, const Arg& arg )
  1009. : base( in_place_init_if, cond, arg )
  1010. {}
  1011. template<class Arg>
  1012. explicit optional ( in_place_init_if_t, bool cond, Arg& arg )
  1013. : base( in_place_init_if, cond, arg )
  1014. {}
  1015. explicit optional ( in_place_init_if_t, bool cond )
  1016. : base( in_place_init_if, cond )
  1017. {}
  1018. #endif
  1019. void swap( optional & arg )
  1020. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  1021. {
  1022. // allow for Koenig lookup
  1023. boost::swap(*this, arg);
  1024. }
  1025. // Returns a reference to the value if this is initialized, otherwise,
  1026. // the behaviour is UNDEFINED
  1027. // No-throw
  1028. reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  1029. reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  1030. // Returns a copy of the value if this is initialized, 'v' otherwise
  1031. reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
  1032. reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
  1033. // Returns a pointer to the value if this is initialized, otherwise,
  1034. // the behaviour is UNDEFINED
  1035. // No-throw
  1036. pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  1037. pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  1038. // Returns a reference to the value if this is initialized, otherwise,
  1039. // the behaviour is UNDEFINED
  1040. // No-throw
  1041. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1042. reference_const_type operator *() const& { return this->get() ; }
  1043. reference_type operator *() & { return this->get() ; }
  1044. reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
  1045. #else
  1046. reference_const_type operator *() const { return this->get() ; }
  1047. reference_type operator *() { return this->get() ; }
  1048. #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
  1049. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1050. reference_const_type value() const&
  1051. {
  1052. if (this->is_initialized())
  1053. return this->get() ;
  1054. else
  1055. throw_exception(bad_optional_access());
  1056. }
  1057. reference_type value() &
  1058. {
  1059. if (this->is_initialized())
  1060. return this->get() ;
  1061. else
  1062. throw_exception(bad_optional_access());
  1063. }
  1064. reference_type_of_temporary_wrapper value() &&
  1065. {
  1066. if (this->is_initialized())
  1067. return boost::move(this->get()) ;
  1068. else
  1069. throw_exception(bad_optional_access());
  1070. }
  1071. #else
  1072. reference_const_type value() const
  1073. {
  1074. if (this->is_initialized())
  1075. return this->get() ;
  1076. else
  1077. throw_exception(bad_optional_access());
  1078. }
  1079. reference_type value()
  1080. {
  1081. if (this->is_initialized())
  1082. return this->get() ;
  1083. else
  1084. throw_exception(bad_optional_access());
  1085. }
  1086. #endif
  1087. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1088. template <class U>
  1089. value_type value_or ( U&& v ) const&
  1090. {
  1091. if (this->is_initialized())
  1092. return get();
  1093. else
  1094. return boost::forward<U>(v);
  1095. }
  1096. template <class U>
  1097. value_type value_or ( U&& v ) &&
  1098. {
  1099. if (this->is_initialized())
  1100. return boost::move(get());
  1101. else
  1102. return boost::forward<U>(v);
  1103. }
  1104. #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1105. template <class U>
  1106. value_type value_or ( U&& v ) const
  1107. {
  1108. if (this->is_initialized())
  1109. return get();
  1110. else
  1111. return boost::forward<U>(v);
  1112. }
  1113. #else
  1114. template <class U>
  1115. value_type value_or ( U const& v ) const
  1116. {
  1117. if (this->is_initialized())
  1118. return get();
  1119. else
  1120. return v;
  1121. }
  1122. template <class U>
  1123. value_type value_or ( U& v ) const
  1124. {
  1125. if (this->is_initialized())
  1126. return get();
  1127. else
  1128. return v;
  1129. }
  1130. #endif
  1131. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1132. template <typename F>
  1133. value_type value_or_eval ( F f ) const&
  1134. {
  1135. if (this->is_initialized())
  1136. return get();
  1137. else
  1138. return f();
  1139. }
  1140. template <typename F>
  1141. value_type value_or_eval ( F f ) &&
  1142. {
  1143. if (this->is_initialized())
  1144. return boost::move(get());
  1145. else
  1146. return f();
  1147. }
  1148. template <typename F>
  1149. optional<typename boost::result_of<F(reference_type)>::type> map(F f) &
  1150. {
  1151. if (this->has_value())
  1152. return f(get());
  1153. else
  1154. return none;
  1155. }
  1156. template <typename F>
  1157. optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const&
  1158. {
  1159. if (this->has_value())
  1160. return f(get());
  1161. else
  1162. return none;
  1163. }
  1164. template <typename F>
  1165. optional<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type> map(F f) &&
  1166. {
  1167. if (this->has_value())
  1168. return f(boost::move(this->get()));
  1169. else
  1170. return none;
  1171. }
  1172. template <typename F>
  1173. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f) &
  1174. {
  1175. if (this->has_value())
  1176. return f(get());
  1177. else
  1178. return none;
  1179. }
  1180. template <typename F>
  1181. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const&
  1182. {
  1183. if (this->has_value())
  1184. return f(get());
  1185. else
  1186. return none;
  1187. }
  1188. template <typename F>
  1189. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type>::type> flat_map(F f) &&
  1190. {
  1191. if (this->has_value())
  1192. return f(boost::move(get()));
  1193. else
  1194. return none;
  1195. }
  1196. #else
  1197. template <typename F>
  1198. value_type value_or_eval ( F f ) const
  1199. {
  1200. if (this->is_initialized())
  1201. return get();
  1202. else
  1203. return f();
  1204. }
  1205. template <typename F>
  1206. optional<typename boost::result_of<F(reference_type)>::type> map(F f)
  1207. {
  1208. if (this->has_value())
  1209. return f(get());
  1210. else
  1211. return none;
  1212. }
  1213. template <typename F>
  1214. optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const
  1215. {
  1216. if (this->has_value())
  1217. return f(get());
  1218. else
  1219. return none;
  1220. }
  1221. template <typename F>
  1222. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f)
  1223. {
  1224. if (this->has_value())
  1225. return f(get());
  1226. else
  1227. return none;
  1228. }
  1229. template <typename F>
  1230. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const
  1231. {
  1232. if (this->has_value())
  1233. return f(get());
  1234. else
  1235. return none;
  1236. }
  1237. #endif
  1238. bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; }
  1239. bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
  1240. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  1241. } ;
  1242. } // namespace boost
  1243. #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
  1244. namespace boost {
  1245. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1246. template<class T>
  1247. class optional<T&&>
  1248. {
  1249. BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal.");
  1250. } ;
  1251. #endif
  1252. } // namespace boost
  1253. #ifndef BOOST_OPTIONAL_CONFIG_DONT_SPECIALIZE_OPTIONAL_REFS
  1254. # include <boost/optional/detail/optional_reference_spec.hpp>
  1255. #endif
  1256. namespace boost {
  1257. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1258. template<class T>
  1259. inline
  1260. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( T && v )
  1261. {
  1262. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(boost::forward<T>(v));
  1263. }
  1264. // Returns optional<T>(cond,v)
  1265. template<class T>
  1266. inline
  1267. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( bool cond, T && v )
  1268. {
  1269. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(cond,boost::forward<T>(v));
  1270. }
  1271. #else
  1272. // Returns optional<T>(v)
  1273. template<class T>
  1274. inline
  1275. optional<T> make_optional ( T const& v )
  1276. {
  1277. return optional<T>(v);
  1278. }
  1279. // Returns optional<T>(cond,v)
  1280. template<class T>
  1281. inline
  1282. optional<T> make_optional ( bool cond, T const& v )
  1283. {
  1284. return optional<T>(cond,v);
  1285. }
  1286. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1287. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  1288. // No-throw
  1289. template<class T>
  1290. inline
  1291. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  1292. get ( optional<T> const& opt )
  1293. {
  1294. return opt.get() ;
  1295. }
  1296. template<class T>
  1297. inline
  1298. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  1299. get ( optional<T>& opt )
  1300. {
  1301. return opt.get() ;
  1302. }
  1303. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  1304. // No-throw
  1305. template<class T>
  1306. inline
  1307. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  1308. get ( optional<T> const* opt )
  1309. {
  1310. return opt->get_ptr() ;
  1311. }
  1312. template<class T>
  1313. inline
  1314. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  1315. get ( optional<T>* opt )
  1316. {
  1317. return opt->get_ptr() ;
  1318. }
  1319. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  1320. // No-throw
  1321. template<class T>
  1322. inline
  1323. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  1324. get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
  1325. {
  1326. return opt.get_value_or(v) ;
  1327. }
  1328. template<class T>
  1329. inline
  1330. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  1331. get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
  1332. {
  1333. return opt.get_value_or(v) ;
  1334. }
  1335. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  1336. // No-throw
  1337. template<class T>
  1338. inline
  1339. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  1340. get_pointer ( optional<T> const& opt )
  1341. {
  1342. return opt.get_ptr() ;
  1343. }
  1344. template<class T>
  1345. inline
  1346. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  1347. get_pointer ( optional<T>& opt )
  1348. {
  1349. return opt.get_ptr() ;
  1350. }
  1351. } // namespace boost
  1352. namespace boost {
  1353. // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
  1354. template<class CharType, class CharTrait>
  1355. std::basic_ostream<CharType, CharTrait>&
  1356. operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&)
  1357. {
  1358. BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
  1359. return os;
  1360. }
  1361. } // namespace boost
  1362. #include <boost/optional/detail/optional_relops.hpp>
  1363. #include <boost/optional/detail/optional_swap.hpp>
  1364. #endif // header guard