old_optional_implementation.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2014-2016 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 maintainer at:
  11. // akrzemi1@gmail.com
  12. #ifndef BOOST_OPTIONAL_DETAIL_OLD_OPTIONAL_IMPLEMENTATION_AJK_28JAN2015_HPP
  13. #define BOOST_OPTIONAL_DETAIL_OLD_OPTIONAL_IMPLEMENTATION_AJK_28JAN2015_HPP
  14. #include <boost/detail/reference_content.hpp>
  15. #include <boost/type_traits/is_reference.hpp>
  16. #include <boost/type_traits/integral_constant.hpp>
  17. #include <boost/type_traits/conditional.hpp>
  18. #include <boost/core/invoke_swap.hpp>
  19. namespace boost {
  20. namespace optional_detail {
  21. template<class T>
  22. struct types_when_isnt_ref
  23. {
  24. typedef T const& reference_const_type ;
  25. typedef T & reference_type ;
  26. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  27. typedef T && rval_reference_type ;
  28. typedef T && reference_type_of_temporary_wrapper;
  29. #ifdef BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES
  30. // GCC 4.4 has support for an early draft of rvalue references. The conforming version below
  31. // causes warnings about returning references to a temporary.
  32. static T&& move(T&& r) { return r; }
  33. #else
  34. static rval_reference_type move(reference_type r) { return boost::move(r); }
  35. #endif
  36. #endif
  37. typedef T const* pointer_const_type ;
  38. typedef T * pointer_type ;
  39. typedef T const& argument_type ;
  40. } ;
  41. template<class T>
  42. struct types_when_is_ref
  43. {
  44. typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
  45. typedef raw_type& reference_const_type ;
  46. typedef raw_type& reference_type ;
  47. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  48. typedef BOOST_DEDUCED_TYPENAME remove_const<raw_type>::type&& rval_reference_type ;
  49. typedef raw_type& reference_type_of_temporary_wrapper;
  50. static reference_type move(reference_type r) { return r; }
  51. #endif
  52. typedef raw_type* pointer_const_type ;
  53. typedef raw_type* pointer_type ;
  54. typedef raw_type& argument_type ;
  55. } ;
  56. template <class To, class From>
  57. void prevent_binding_rvalue_ref_to_optional_lvalue_ref()
  58. {
  59. #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
  60. BOOST_STATIC_ASSERT_MSG(
  61. !boost::is_lvalue_reference<To>::value || !boost::is_rvalue_reference<From>::value,
  62. "binding rvalue references to optional lvalue references is disallowed");
  63. #endif
  64. }
  65. struct optional_tag {} ;
  66. template<class T>
  67. class optional_base : public optional_tag
  68. {
  69. private :
  70. typedef
  71. #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
  72. BOOST_DEDUCED_TYPENAME
  73. #endif
  74. ::boost::detail::make_reference_content<T>::type internal_type ;
  75. typedef aligned_storage<internal_type> storage_type ;
  76. typedef types_when_isnt_ref<T> types_when_not_ref ;
  77. typedef types_when_is_ref<T> types_when_ref ;
  78. typedef optional_base<T> this_type ;
  79. protected :
  80. typedef T value_type ;
  81. typedef true_type is_reference_tag ;
  82. typedef false_type is_not_reference_tag ;
  83. typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
  84. public:
  85. typedef BOOST_DEDUCED_TYPENAME conditional<is_reference_predicate::value,types_when_ref,types_when_not_ref>::type types ;
  86. protected:
  87. typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
  88. typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
  89. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  90. typedef BOOST_DEDUCED_TYPENAME types::rval_reference_type rval_reference_type ;
  91. typedef BOOST_DEDUCED_TYPENAME types::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
  92. #endif
  93. typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
  94. typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
  95. typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
  96. // Creates an optional<T> uninitialized.
  97. // No-throw
  98. optional_base()
  99. :
  100. m_initialized(false) {}
  101. // Creates an optional<T> uninitialized.
  102. // No-throw
  103. optional_base ( none_t )
  104. :
  105. m_initialized(false) {}
  106. // Creates an optional<T> initialized with 'val'.
  107. // Can throw if T::T(T const&) does
  108. optional_base ( argument_type val )
  109. :
  110. m_initialized(false)
  111. {
  112. construct(val);
  113. }
  114. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  115. // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
  116. // Can throw if T::T(T&&) does
  117. optional_base ( rval_reference_type val )
  118. :
  119. m_initialized(false)
  120. {
  121. construct( boost::move(val) );
  122. }
  123. #endif
  124. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional<T>.
  125. // Can throw if T::T(T const&) does
  126. optional_base ( bool cond, argument_type val )
  127. :
  128. m_initialized(false)
  129. {
  130. if ( cond )
  131. construct(val);
  132. }
  133. // Creates a deep copy of another optional<T>
  134. // Can throw if T::T(T const&) does
  135. optional_base ( optional_base const& rhs )
  136. :
  137. m_initialized(false)
  138. {
  139. if ( rhs.is_initialized() )
  140. construct(rhs.get_impl());
  141. }
  142. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  143. // Creates a deep move of another optional<T>
  144. // Can throw if T::T(T&&) does
  145. optional_base ( optional_base&& rhs )
  146. :
  147. m_initialized(false)
  148. {
  149. if ( rhs.is_initialized() )
  150. construct( boost::move(rhs.get_impl()) );
  151. }
  152. #endif
  153. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  154. template<class Expr, class PtrExpr>
  155. explicit optional_base ( Expr&& expr, PtrExpr const* tag )
  156. :
  157. m_initialized(false)
  158. {
  159. construct(boost::forward<Expr>(expr),tag);
  160. }
  161. #else
  162. // This is used for both converting and in-place constructions.
  163. // Derived classes use the 'tag' to select the appropriate
  164. // implementation (the correct 'construct()' overload)
  165. template<class Expr>
  166. explicit optional_base ( Expr const& expr, Expr const* tag )
  167. :
  168. m_initialized(false)
  169. {
  170. construct(expr,tag);
  171. }
  172. #endif
  173. // No-throw (assuming T::~T() doesn't)
  174. ~optional_base() { destroy() ; }
  175. // Assigns from another optional<T> (deep-copies the rhs value)
  176. void assign ( optional_base const& rhs )
  177. {
  178. if (is_initialized())
  179. {
  180. if ( rhs.is_initialized() )
  181. assign_value(rhs.get_impl(), is_reference_predicate() );
  182. else destroy();
  183. }
  184. else
  185. {
  186. if ( rhs.is_initialized() )
  187. construct(rhs.get_impl());
  188. }
  189. }
  190. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  191. // Assigns from another optional<T> (deep-moves the rhs value)
  192. void assign ( optional_base&& rhs )
  193. {
  194. if (is_initialized())
  195. {
  196. if ( rhs.is_initialized() )
  197. assign_value(boost::move(rhs.get_impl()), is_reference_predicate() );
  198. else destroy();
  199. }
  200. else
  201. {
  202. if ( rhs.is_initialized() )
  203. construct(boost::move(rhs.get_impl()));
  204. }
  205. }
  206. #endif
  207. // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
  208. template<class U>
  209. void assign ( optional<U> const& rhs )
  210. {
  211. if (is_initialized())
  212. {
  213. if ( rhs.is_initialized() )
  214. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  215. assign_value(rhs.get(), is_reference_predicate() );
  216. #else
  217. assign_value(static_cast<value_type>(rhs.get()), is_reference_predicate() );
  218. #endif
  219. else destroy();
  220. }
  221. else
  222. {
  223. if ( rhs.is_initialized() )
  224. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  225. construct(rhs.get());
  226. #else
  227. construct(static_cast<value_type>(rhs.get()));
  228. #endif
  229. }
  230. }
  231. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  232. // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
  233. template<class U>
  234. void assign ( optional<U>&& rhs )
  235. {
  236. typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
  237. if (is_initialized())
  238. {
  239. if ( rhs.is_initialized() )
  240. assign_value(static_cast<ref_type>(rhs.get()), is_reference_predicate() );
  241. else destroy();
  242. }
  243. else
  244. {
  245. if ( rhs.is_initialized() )
  246. construct(static_cast<ref_type>(rhs.get()));
  247. }
  248. }
  249. #endif
  250. // Assigns from a T (deep-copies the rhs value)
  251. void assign ( argument_type val )
  252. {
  253. if (is_initialized())
  254. assign_value(val, is_reference_predicate() );
  255. else construct(val);
  256. }
  257. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  258. // Assigns from a T (deep-moves the rhs value)
  259. void assign ( rval_reference_type val )
  260. {
  261. if (is_initialized())
  262. assign_value( boost::move(val), is_reference_predicate() );
  263. else construct( boost::move(val) );
  264. }
  265. #endif
  266. // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
  267. // No-throw (assuming T::~T() doesn't)
  268. void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
  269. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  270. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  271. template<class Expr, class ExprPtr>
  272. void assign_expr ( Expr&& expr, ExprPtr const* tag )
  273. {
  274. if (is_initialized())
  275. assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
  276. else construct(boost::forward<Expr>(expr),tag);
  277. }
  278. #else
  279. template<class Expr>
  280. void assign_expr ( Expr const& expr, Expr const* tag )
  281. {
  282. if (is_initialized())
  283. assign_expr_to_initialized(expr,tag);
  284. else construct(expr,tag);
  285. }
  286. #endif
  287. #endif
  288. public :
  289. // Destroys the current value, if any, leaving this UNINITIALIZED
  290. // No-throw (assuming T::~T() doesn't)
  291. void reset() BOOST_NOEXCEPT { destroy(); }
  292. // **DEPRECATED** Replaces the current value -if any- with 'val'
  293. void reset ( argument_type val ) { assign(val); }
  294. // Returns a pointer to the value if this is initialized, otherwise,
  295. // returns NULL.
  296. // No-throw
  297. pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
  298. pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
  299. bool is_initialized() const { return m_initialized ; }
  300. protected :
  301. void construct ( argument_type val )
  302. {
  303. ::new (m_storage.address()) internal_type(val) ;
  304. m_initialized = true ;
  305. }
  306. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  307. void construct ( rval_reference_type val )
  308. {
  309. ::new (m_storage.address()) internal_type( types::move(val) ) ;
  310. m_initialized = true ;
  311. }
  312. #endif
  313. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  314. // Constructs in-place
  315. // upon exception *this is always uninitialized
  316. template<class... Args>
  317. void emplace_assign ( Args&&... args )
  318. {
  319. destroy();
  320. ::new (m_storage.address()) internal_type( boost::forward<Args>(args)... );
  321. m_initialized = true ;
  322. }
  323. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  324. template<class Arg>
  325. void emplace_assign ( Arg&& arg )
  326. {
  327. destroy();
  328. ::new (m_storage.address()) internal_type( boost::forward<Arg>(arg) );
  329. m_initialized = true ;
  330. }
  331. void emplace_assign ()
  332. {
  333. destroy();
  334. ::new (m_storage.address()) internal_type();
  335. m_initialized = true ;
  336. }
  337. #else
  338. template<class Arg>
  339. void emplace_assign ( const Arg& arg )
  340. {
  341. destroy();
  342. ::new (m_storage.address()) internal_type( arg );
  343. m_initialized = true ;
  344. }
  345. template<class Arg>
  346. void emplace_assign ( Arg& arg )
  347. {
  348. destroy();
  349. ::new (m_storage.address()) internal_type( arg );
  350. m_initialized = true ;
  351. }
  352. void emplace_assign ()
  353. {
  354. destroy();
  355. ::new (m_storage.address()) internal_type();
  356. m_initialized = true ;
  357. }
  358. #endif
  359. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  360. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  361. // Constructs in-place using the given factory
  362. template<class Expr>
  363. void construct ( Expr&& factory, in_place_factory_base const* )
  364. {
  365. BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
  366. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  367. m_initialized = true ;
  368. }
  369. // Constructs in-place using the given typed factory
  370. template<class Expr>
  371. void construct ( Expr&& factory, typed_in_place_factory_base const* )
  372. {
  373. BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
  374. factory.apply(m_storage.address()) ;
  375. m_initialized = true ;
  376. }
  377. template<class Expr>
  378. void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
  379. {
  380. destroy();
  381. construct(factory,tag);
  382. }
  383. // Constructs in-place using the given typed factory
  384. template<class Expr>
  385. void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
  386. {
  387. destroy();
  388. construct(factory,tag);
  389. }
  390. #else
  391. // Constructs in-place using the given factory
  392. template<class Expr>
  393. void construct ( Expr const& factory, in_place_factory_base const* )
  394. {
  395. BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
  396. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  397. m_initialized = true ;
  398. }
  399. // Constructs in-place using the given typed factory
  400. template<class Expr>
  401. void construct ( Expr const& factory, typed_in_place_factory_base const* )
  402. {
  403. BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
  404. factory.apply(m_storage.address()) ;
  405. m_initialized = true ;
  406. }
  407. template<class Expr>
  408. void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
  409. {
  410. destroy();
  411. construct(factory,tag);
  412. }
  413. // Constructs in-place using the given typed factory
  414. template<class Expr>
  415. void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
  416. {
  417. destroy();
  418. construct(factory,tag);
  419. }
  420. #endif
  421. #endif
  422. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  423. // Constructs using any expression implicitly convertible to the single argument
  424. // of a one-argument T constructor.
  425. // Converting constructions of optional<T> from optional<U> uses this function with
  426. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  427. template<class Expr>
  428. void construct ( Expr&& expr, void const* )
  429. {
  430. new (m_storage.address()) internal_type(boost::forward<Expr>(expr)) ;
  431. m_initialized = true ;
  432. }
  433. // Assigns using a form any expression implicitly convertible to the single argument
  434. // of a T's assignment operator.
  435. // Converting assignments of optional<T> from optional<U> uses this function with
  436. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  437. template<class Expr>
  438. void assign_expr_to_initialized ( Expr&& expr, void const* )
  439. {
  440. assign_value(boost::forward<Expr>(expr), is_reference_predicate());
  441. }
  442. #else
  443. // Constructs using any expression implicitly convertible to the single argument
  444. // of a one-argument T constructor.
  445. // Converting constructions of optional<T> from optional<U> uses this function with
  446. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  447. template<class Expr>
  448. void construct ( Expr const& expr, void const* )
  449. {
  450. new (m_storage.address()) internal_type(expr) ;
  451. m_initialized = true ;
  452. }
  453. // Assigns using a form any expression implicitly convertible to the single argument
  454. // of a T's assignment operator.
  455. // Converting assignments of optional<T> from optional<U> uses this function with
  456. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  457. template<class Expr>
  458. void assign_expr_to_initialized ( Expr const& expr, void const* )
  459. {
  460. assign_value(expr, is_reference_predicate());
  461. }
  462. #endif
  463. #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  464. // BCB5.64 (and probably lower versions) workaround.
  465. // The in-place factories are supported by means of catch-all constructors
  466. // and assignment operators (the functions are parameterized in terms of
  467. // an arbitrary 'Expr' type)
  468. // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
  469. // to the 'Expr'-taking functions even though explicit overloads are present for them.
  470. // Thus, the following overload is needed to properly handle the case when the 'lhs'
  471. // is another optional.
  472. //
  473. // For VC<=70 compilers this workaround doesn't work because the compiler issues and error
  474. // instead of choosing the wrong overload
  475. //
  476. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  477. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  478. template<class Expr>
  479. void construct ( Expr&& expr, optional_tag const* )
  480. {
  481. if ( expr.is_initialized() )
  482. {
  483. // An exception can be thrown here.
  484. // It it happens, THIS will be left uninitialized.
  485. new (m_storage.address()) internal_type(types::move(expr.get())) ;
  486. m_initialized = true ;
  487. }
  488. }
  489. #else
  490. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  491. template<class Expr>
  492. void construct ( Expr const& expr, optional_tag const* )
  493. {
  494. if ( expr.is_initialized() )
  495. {
  496. // An exception can be thrown here.
  497. // It it happens, THIS will be left uninitialized.
  498. new (m_storage.address()) internal_type(expr.get()) ;
  499. m_initialized = true ;
  500. }
  501. }
  502. #endif
  503. #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  504. void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
  505. void assign_value ( argument_type val, is_reference_tag ) { construct(val); }
  506. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  507. void assign_value ( rval_reference_type val, is_not_reference_tag ) { get_impl() = static_cast<rval_reference_type>(val); }
  508. void assign_value ( rval_reference_type val, is_reference_tag ) { construct( static_cast<rval_reference_type>(val) ); }
  509. #endif
  510. void destroy()
  511. {
  512. if ( m_initialized )
  513. destroy_impl(is_reference_predicate()) ;
  514. }
  515. reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
  516. reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
  517. pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
  518. pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
  519. private :
  520. // internal_type can be either T or reference_content<T>
  521. #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS)
  522. // This workaround is supposed to silence GCC warnings about broken strict aliasing rules
  523. internal_type const* get_object() const
  524. {
  525. union { void const* ap_pvoid; internal_type const* as_ptype; } caster = { m_storage.address() };
  526. return caster.as_ptype;
  527. }
  528. internal_type * get_object()
  529. {
  530. union { void* ap_pvoid; internal_type* as_ptype; } caster = { m_storage.address() };
  531. return caster.as_ptype;
  532. }
  533. #else
  534. internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
  535. internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
  536. #endif
  537. // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
  538. reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
  539. reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
  540. reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
  541. reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
  542. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x581))
  543. void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
  544. #else
  545. void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->~T() ; m_initialized = false ; }
  546. #endif
  547. void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
  548. // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
  549. // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
  550. // the following overloads are used to filter out the case and guarantee an error in case of T being a reference.
  551. pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
  552. pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; }
  553. pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; }
  554. pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; }
  555. bool m_initialized ;
  556. storage_type m_storage ;
  557. } ;
  558. } // namespace optional_detail
  559. template<class T>
  560. class optional : public optional_detail::optional_base<T>
  561. {
  562. typedef optional_detail::optional_base<T> base ;
  563. public :
  564. typedef optional<T> this_type ;
  565. typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
  566. typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
  567. typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
  568. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  569. typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
  570. typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
  571. #endif
  572. typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
  573. typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
  574. typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
  575. // Creates an optional<T> uninitialized.
  576. // No-throw
  577. optional() BOOST_NOEXCEPT : base() {}
  578. // Creates an optional<T> uninitialized.
  579. // No-throw
  580. optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
  581. // Creates an optional<T> initialized with 'val'.
  582. // Can throw if T::T(T const&) does
  583. optional ( argument_type val ) : base(val) {}
  584. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  585. // Creates an optional<T> initialized with 'move(val)'.
  586. // Can throw if T::T(T &&) does
  587. optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
  588. {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, rval_reference_type>();}
  589. #endif
  590. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  591. // Can throw if T::T(T const&) does
  592. optional ( bool cond, argument_type val ) : base(cond,val) {}
  593. // NOTE: MSVC needs templated versions first
  594. // Creates a deep copy of another convertible optional<U>
  595. // Requires a valid conversion from U to T.
  596. // Can throw if T::T(U const&) does
  597. template<class U>
  598. explicit optional ( optional<U> const& rhs )
  599. :
  600. base()
  601. {
  602. if ( rhs.is_initialized() )
  603. this->construct(rhs.get());
  604. }
  605. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  606. // Creates a deep move of another convertible optional<U>
  607. // Requires a valid conversion from U to T.
  608. // Can throw if T::T(U&&) does
  609. template<class U>
  610. explicit optional ( optional<U> && rhs )
  611. :
  612. base()
  613. {
  614. if ( rhs.is_initialized() )
  615. this->construct( boost::move(rhs.get()) );
  616. }
  617. #endif
  618. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  619. // Creates an optional<T> with an expression which can be either
  620. // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
  621. // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
  622. // (c) Any expression implicitly convertible to the single type
  623. // of a one-argument T's constructor.
  624. // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
  625. // even though explicit overloads are present for these.
  626. // Depending on the above some T ctor is called.
  627. // Can throw if the resolved T ctor throws.
  628. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  629. template<class Expr>
  630. explicit optional ( Expr&& expr,
  631. BOOST_DEDUCED_TYPENAME boost::disable_if_c<
  632. (boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type>::value) ||
  633. boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type, none_t>::value, bool >::type = true
  634. )
  635. : base(boost::forward<Expr>(expr),boost::addressof(expr))
  636. {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, Expr&&>();}
  637. #else
  638. template<class Expr>
  639. explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
  640. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  641. #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  642. // Creates a deep copy of another optional<T>
  643. // Can throw if T::T(T const&) does
  644. optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
  645. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  646. // Creates a deep move of another optional<T>
  647. // Can throw if T::T(T&&) does
  648. optional ( optional && rhs )
  649. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  650. : base( boost::move(rhs) )
  651. {}
  652. #endif
  653. // No-throw (assuming T::~T() doesn't)
  654. ~optional() {}
  655. #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  656. // Assigns from an expression. See corresponding constructor.
  657. // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
  658. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  659. template<class Expr>
  660. BOOST_DEDUCED_TYPENAME boost::disable_if_c<
  661. boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type>::value ||
  662. boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type, none_t>::value,
  663. optional&
  664. >::type
  665. operator= ( Expr&& expr )
  666. {
  667. optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, Expr&&>();
  668. this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
  669. return *this ;
  670. }
  671. #else
  672. template<class Expr>
  673. optional& operator= ( Expr const& expr )
  674. {
  675. this->assign_expr(expr,boost::addressof(expr));
  676. return *this ;
  677. }
  678. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  679. #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  680. // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
  681. // Requires a valid conversion from U to T.
  682. // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
  683. template<class U>
  684. optional& operator= ( optional<U> const& rhs )
  685. {
  686. this->assign(rhs);
  687. return *this ;
  688. }
  689. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  690. // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
  691. // Requires a valid conversion from U to T.
  692. // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
  693. template<class U>
  694. optional& operator= ( optional<U> && rhs )
  695. {
  696. this->assign(boost::move(rhs));
  697. return *this ;
  698. }
  699. #endif
  700. // Assigns from another optional<T> (deep-copies the rhs value)
  701. // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
  702. // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
  703. optional& operator= ( optional const& rhs )
  704. {
  705. this->assign( static_cast<base const&>(rhs) ) ;
  706. return *this ;
  707. }
  708. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  709. // Assigns from another optional<T> (deep-moves the rhs value)
  710. optional& operator= ( optional && rhs )
  711. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  712. {
  713. this->assign( static_cast<base &&>(rhs) ) ;
  714. return *this ;
  715. }
  716. #endif
  717. // Assigns from a T (deep-copies the rhs value)
  718. // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
  719. optional& operator= ( argument_type val )
  720. {
  721. this->assign( val ) ;
  722. return *this ;
  723. }
  724. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  725. // Assigns from a T (deep-moves the rhs value)
  726. optional& operator= ( rval_reference_type val )
  727. {
  728. optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, rval_reference_type>();
  729. this->assign( boost::move(val) ) ;
  730. return *this ;
  731. }
  732. #endif
  733. // Assigns from a "none"
  734. // Which destroys the current value, if any, leaving this UNINITIALIZED
  735. // No-throw (assuming T::~T() doesn't)
  736. optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
  737. {
  738. this->assign( none_ ) ;
  739. return *this ;
  740. }
  741. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  742. // Constructs in-place
  743. // upon exception *this is always uninitialized
  744. template<class... Args>
  745. void emplace ( Args&&... args )
  746. {
  747. this->emplace_assign( boost::forward<Args>(args)... );
  748. }
  749. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  750. template<class Arg>
  751. void emplace ( Arg&& arg )
  752. {
  753. this->emplace_assign( boost::forward<Arg>(arg) );
  754. }
  755. void emplace ()
  756. {
  757. this->emplace_assign();
  758. }
  759. #else
  760. template<class Arg>
  761. void emplace ( const Arg& arg )
  762. {
  763. this->emplace_assign( arg );
  764. }
  765. template<class Arg>
  766. void emplace ( Arg& arg )
  767. {
  768. this->emplace_assign( arg );
  769. }
  770. void emplace ()
  771. {
  772. this->emplace_assign();
  773. }
  774. #endif
  775. void swap( optional & arg )
  776. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  777. {
  778. // allow for Koenig lookup
  779. boost::core::invoke_swap(*this, arg);
  780. }
  781. // Returns a reference to the value if this is initialized, otherwise,
  782. // the behaviour is UNDEFINED
  783. // No-throw
  784. reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  785. reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  786. // Returns a copy of the value if this is initialized, 'v' otherwise
  787. reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
  788. reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
  789. // Returns a pointer to the value if this is initialized, otherwise,
  790. // the behaviour is UNDEFINED
  791. // No-throw
  792. pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  793. pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  794. // Returns a reference to the value if this is initialized, otherwise,
  795. // the behaviour is UNDEFINED
  796. // No-throw
  797. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  798. reference_const_type operator *() const& { return this->get() ; }
  799. reference_type operator *() & { return this->get() ; }
  800. reference_type_of_temporary_wrapper operator *() && { return base::types::move(this->get()) ; }
  801. #else
  802. reference_const_type operator *() const { return this->get() ; }
  803. reference_type operator *() { return this->get() ; }
  804. #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
  805. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  806. reference_const_type value() const&
  807. {
  808. if (this->is_initialized())
  809. return this->get() ;
  810. else
  811. throw_exception(bad_optional_access());
  812. }
  813. reference_type value() &
  814. {
  815. if (this->is_initialized())
  816. return this->get() ;
  817. else
  818. throw_exception(bad_optional_access());
  819. }
  820. reference_type_of_temporary_wrapper value() &&
  821. {
  822. if (this->is_initialized())
  823. return base::types::move(this->get()) ;
  824. else
  825. throw_exception(bad_optional_access());
  826. }
  827. #else
  828. reference_const_type value() const
  829. {
  830. if (this->is_initialized())
  831. return this->get() ;
  832. else
  833. throw_exception(bad_optional_access());
  834. }
  835. reference_type value()
  836. {
  837. if (this->is_initialized())
  838. return this->get() ;
  839. else
  840. throw_exception(bad_optional_access());
  841. }
  842. #endif
  843. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  844. template <class U>
  845. value_type value_or ( U&& v ) const&
  846. {
  847. if (this->is_initialized())
  848. return get();
  849. else
  850. return boost::forward<U>(v);
  851. }
  852. template <class U>
  853. value_type value_or ( U&& v ) &&
  854. {
  855. if (this->is_initialized())
  856. return base::types::move(get());
  857. else
  858. return boost::forward<U>(v);
  859. }
  860. #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  861. template <class U>
  862. value_type value_or ( U&& v ) const
  863. {
  864. if (this->is_initialized())
  865. return get();
  866. else
  867. return boost::forward<U>(v);
  868. }
  869. #else
  870. template <class U>
  871. value_type value_or ( U const& v ) const
  872. {
  873. if (this->is_initialized())
  874. return get();
  875. else
  876. return v;
  877. }
  878. template <class U>
  879. value_type value_or ( U& v ) const
  880. {
  881. if (this->is_initialized())
  882. return get();
  883. else
  884. return v;
  885. }
  886. #endif
  887. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  888. template <typename F>
  889. value_type value_or_eval ( F f ) const&
  890. {
  891. if (this->is_initialized())
  892. return get();
  893. else
  894. return f();
  895. }
  896. template <typename F>
  897. value_type value_or_eval ( F f ) &&
  898. {
  899. if (this->is_initialized())
  900. return base::types::move(get());
  901. else
  902. return f();
  903. }
  904. #else
  905. template <typename F>
  906. value_type value_or_eval ( F f ) const
  907. {
  908. if (this->is_initialized())
  909. return get();
  910. else
  911. return f();
  912. }
  913. #endif
  914. bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
  915. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  916. } ;
  917. } // namespace boost
  918. #endif // header guard