optional.hpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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