optional.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
  2. //
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/lib/optional for documentation.
  8. //
  9. // You are welcome to contact the author at:
  10. // fernando_cacciola@hotmail.com
  11. //
  12. #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
  13. #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
  14. #include<new>
  15. #include<algorithm>
  16. #include "boost/config.hpp"
  17. #include "boost/assert.hpp"
  18. #include "boost/type.hpp"
  19. #include "boost/type_traits/alignment_of.hpp"
  20. #include "boost/type_traits/type_with_alignment.hpp"
  21. #include "boost/type_traits/remove_reference.hpp"
  22. #include "boost/type_traits/is_reference.hpp"
  23. #include "boost/mpl/if.hpp"
  24. #include "boost/mpl/bool.hpp"
  25. #include "boost/mpl/not.hpp"
  26. #include "boost/detail/reference_content.hpp"
  27. #include "boost/none.hpp"
  28. #include "boost/utility/compare_pointees.hpp"
  29. #include "boost/optional/optional_fwd.hpp"
  30. #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
  31. // VC6.0 has the following bug:
  32. // When a templated assignment operator exist, an implicit conversion
  33. // constructing an optional<T> is used when assigment of the form:
  34. // optional<T> opt ; opt = T(...);
  35. // is compiled.
  36. // However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
  37. // Therefore, for VC6.0 templated assignment is disabled.
  38. //
  39. #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
  40. #endif
  41. #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
  42. // VC7.0 has the following bug:
  43. // When both a non-template and a template copy-ctor exist
  44. // and the templated version is made 'explicit', the explicit is also
  45. // given to the non-templated version, making the class non-implicitely-copyable.
  46. //
  47. #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
  48. #endif
  49. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700)
  50. // AFAICT only VC7.1 correctly resolves the overload set
  51. // that includes the in-place factory taking functions,
  52. // so for the other VC versions, in-place factory support
  53. // is disabled
  54. #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  55. #endif
  56. #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
  57. // BCB (5.5.1) cannot parse the nested template struct in an inplace factory.
  58. #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  59. #endif
  60. #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \
  61. && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581) )
  62. // BCB (up to 5.64) has the following bug:
  63. // If there is a member function/operator template of the form
  64. // template<class Expr> mfunc( Expr expr ) ;
  65. // some calls are resolved to this even if there are other better matches.
  66. // The effect of this bug is that calls to converting ctors and assignments
  67. // are incrorrectly sink to this general catch-all member function template as shown above.
  68. #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  69. #endif
  70. // Daniel Wallin discovered that bind/apply.hpp badly interacts with the apply<>
  71. // member template of a factory as used in the optional<> implementation.
  72. // He proposed this simple fix which is to move the call to apply<> outside
  73. // namespace boost.
  74. namespace boost_optional_detail
  75. {
  76. template <class T, class Factory>
  77. void construct(Factory const& factory, void* address)
  78. {
  79. factory.BOOST_NESTED_TEMPLATE apply<T>(address);
  80. }
  81. }
  82. namespace boost {
  83. class in_place_factory_base ;
  84. class typed_in_place_factory_base ;
  85. namespace optional_detail {
  86. // This local class is used instead of that in "aligned_storage.hpp"
  87. // because I've found the 'official' class to ICE BCB5.5
  88. // when some types are used with optional<>
  89. // (due to sizeof() passed down as a non-type template parameter)
  90. template <class T>
  91. class aligned_storage
  92. {
  93. // Borland ICEs if unnamed unions are used for this!
  94. union dummy_u
  95. {
  96. char data[ sizeof(T) ];
  97. BOOST_DEDUCED_TYPENAME type_with_alignment<
  98. ::boost::alignment_of<T>::value >::type aligner_;
  99. } dummy_ ;
  100. public:
  101. void const* address() const { return &dummy_.data[0]; }
  102. void * address() { return &dummy_.data[0]; }
  103. } ;
  104. template<class T>
  105. struct types_when_isnt_ref
  106. {
  107. typedef T const& reference_const_type ;
  108. typedef T & reference_type ;
  109. typedef T const* pointer_const_type ;
  110. typedef T * pointer_type ;
  111. typedef T const& argument_type ;
  112. } ;
  113. template<class T>
  114. struct types_when_is_ref
  115. {
  116. typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
  117. typedef raw_type& reference_const_type ;
  118. typedef raw_type& reference_type ;
  119. typedef raw_type* pointer_const_type ;
  120. typedef raw_type* pointer_type ;
  121. typedef raw_type& argument_type ;
  122. } ;
  123. struct optional_tag {} ;
  124. template<class T>
  125. class optional_base : public optional_tag
  126. {
  127. private :
  128. typedef
  129. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  130. BOOST_DEDUCED_TYPENAME
  131. #endif
  132. ::boost::detail::make_reference_content<T>::type internal_type ;
  133. typedef aligned_storage<internal_type> storage_type ;
  134. typedef types_when_isnt_ref<T> types_when_not_ref ;
  135. typedef types_when_is_ref<T> types_when_ref ;
  136. typedef optional_base<T> this_type ;
  137. protected :
  138. typedef T value_type ;
  139. typedef mpl::true_ is_reference_tag ;
  140. typedef mpl::false_ is_not_reference_tag ;
  141. typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
  142. typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
  143. typedef bool (this_type::*unspecified_bool_type)() const;
  144. typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
  145. typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
  146. typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
  147. typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
  148. typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
  149. // Creates an optional<T> uninitialized.
  150. // No-throw
  151. optional_base()
  152. :
  153. m_initialized(false) {}
  154. // Creates an optional<T> uninitialized.
  155. // No-throw
  156. optional_base ( none_t )
  157. :
  158. m_initialized(false) {}
  159. // Creates an optional<T> initialized with 'val'.
  160. // Can throw if T::T(T const&) does
  161. optional_base ( argument_type val )
  162. :
  163. m_initialized(false)
  164. {
  165. construct(val);
  166. }
  167. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional<T>.
  168. // Can throw if T::T(T const&) does
  169. optional_base ( bool cond, argument_type val )
  170. :
  171. m_initialized(false)
  172. {
  173. if ( cond )
  174. construct(val);
  175. }
  176. // Creates a deep copy of another optional<T>
  177. // Can throw if T::T(T const&) does
  178. optional_base ( optional_base const& rhs )
  179. :
  180. m_initialized(false)
  181. {
  182. if ( rhs.is_initialized() )
  183. construct(rhs.get_impl());
  184. }
  185. // This is used for both converting and in-place constructions.
  186. // Derived classes use the 'tag' to select the appropriate
  187. // implementation (the correct 'construct()' overload)
  188. template<class Expr>
  189. explicit optional_base ( Expr const& expr, Expr const* tag )
  190. :
  191. m_initialized(false)
  192. {
  193. construct(expr,tag);
  194. }
  195. // No-throw (assuming T::~T() doesn't)
  196. ~optional_base() { destroy() ; }
  197. // Assigns from another optional<T> (deep-copies the rhs value)
  198. void assign ( optional_base const& rhs )
  199. {
  200. if (is_initialized())
  201. {
  202. if ( rhs.is_initialized() )
  203. assign_value(rhs.get_impl(), is_reference_predicate() );
  204. else destroy();
  205. }
  206. else
  207. {
  208. if ( rhs.is_initialized() )
  209. construct(rhs.get_impl());
  210. }
  211. }
  212. // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
  213. template<class U>
  214. void assign ( optional<U> const& rhs )
  215. {
  216. if (is_initialized())
  217. {
  218. if ( rhs.is_initialized() )
  219. assign_value(static_cast<value_type>(rhs.get()), is_reference_predicate() );
  220. else destroy();
  221. }
  222. else
  223. {
  224. if ( rhs.is_initialized() )
  225. construct(static_cast<value_type>(rhs.get()));
  226. }
  227. }
  228. // Assigns from a T (deep-copies the rhs value)
  229. void assign ( argument_type val )
  230. {
  231. if (is_initialized())
  232. assign_value(val, is_reference_predicate() );
  233. else construct(val);
  234. }
  235. // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
  236. // No-throw (assuming T::~T() doesn't)
  237. void assign ( none_t ) { destroy(); }
  238. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  239. template<class Expr>
  240. void assign_expr ( Expr const& expr, Expr const* tag )
  241. {
  242. if (is_initialized())
  243. assign_expr_to_initialized(expr,tag);
  244. else construct(expr,tag);
  245. }
  246. #endif
  247. public :
  248. // Destroys the current value, if any, leaving this UNINITIALIZED
  249. // No-throw (assuming T::~T() doesn't)
  250. void reset() { destroy(); }
  251. // Replaces the current value -if any- with 'val'
  252. void reset ( argument_type val ) { assign(val); }
  253. // Returns a pointer to the value if this is initialized, otherwise,
  254. // returns NULL.
  255. // No-throw
  256. pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
  257. pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
  258. bool is_initialized() const { return m_initialized ; }
  259. protected :
  260. void construct ( argument_type val )
  261. {
  262. new (m_storage.address()) internal_type(val) ;
  263. m_initialized = true ;
  264. }
  265. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  266. // Constructs in-place using the given factory
  267. template<class Expr>
  268. void construct ( Expr const& factory, in_place_factory_base const* )
  269. {
  270. BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
  271. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  272. m_initialized = true ;
  273. }
  274. // Constructs in-place using the given typed factory
  275. template<class Expr>
  276. void construct ( Expr const& factory, typed_in_place_factory_base const* )
  277. {
  278. BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
  279. factory.apply(m_storage.address()) ;
  280. m_initialized = true ;
  281. }
  282. template<class Expr>
  283. void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
  284. {
  285. destroy();
  286. construct(factory,tag);
  287. }
  288. // Constructs in-place using the given typed factory
  289. template<class Expr>
  290. void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
  291. {
  292. destroy();
  293. construct(factory,tag);
  294. }
  295. #endif
  296. // Constructs using any expression implicitely convertible to the single argument
  297. // of a one-argument T constructor.
  298. // Converting constructions of optional<T> from optional<U> uses this function with
  299. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  300. template<class Expr>
  301. void construct ( Expr const& expr, void const* )
  302. {
  303. new (m_storage.address()) internal_type(expr) ;
  304. m_initialized = true ;
  305. }
  306. // Assigns using a form any expression implicitely convertible to the single argument
  307. // of a T's assignment operator.
  308. // Converting assignments of optional<T> from optional<U> uses this function with
  309. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  310. template<class Expr>
  311. void assign_expr_to_initialized ( Expr const& expr, void const* )
  312. {
  313. assign_value(expr, is_reference_predicate());
  314. }
  315. #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  316. // BCB5.64 (and probably lower versions) workaround.
  317. // The in-place factories are supported by means of catch-all constructors
  318. // and assignment operators (the functions are parameterized in terms of
  319. // an arbitrary 'Expr' type)
  320. // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
  321. // to the 'Expr'-taking functions even though explicit overloads are present for them.
  322. // Thus, the following overload is needed to properly handle the case when the 'lhs'
  323. // is another optional.
  324. //
  325. // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
  326. // instead of choosing the wrong overload
  327. //
  328. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  329. template<class Expr>
  330. void construct ( Expr const& expr, optional_tag const* )
  331. {
  332. if ( expr.is_initialized() )
  333. {
  334. // An exception can be thrown here.
  335. // It it happens, THIS will be left uninitialized.
  336. new (m_storage.address()) internal_type(expr.get()) ;
  337. m_initialized = true ;
  338. }
  339. }
  340. #endif
  341. void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
  342. void assign_value ( argument_type val, is_reference_tag ) { construct(val); }
  343. void destroy()
  344. {
  345. if ( m_initialized )
  346. destroy_impl(is_reference_predicate()) ;
  347. }
  348. unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
  349. reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
  350. reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
  351. pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
  352. pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
  353. private :
  354. // internal_type can be either T or reference_content<T>
  355. internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
  356. internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
  357. // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
  358. reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
  359. reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
  360. reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
  361. reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
  362. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  363. void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
  364. #else
  365. void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->T::~T() ; m_initialized = false ; }
  366. #endif
  367. void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
  368. // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
  369. // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
  370. // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
  371. pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
  372. pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; }
  373. pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; }
  374. pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; }
  375. bool m_initialized ;
  376. storage_type m_storage ;
  377. } ;
  378. } // namespace optional_detail
  379. template<class T>
  380. class optional : public optional_detail::optional_base<T>
  381. {
  382. typedef optional_detail::optional_base<T> base ;
  383. typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ;
  384. public :
  385. typedef optional<T> this_type ;
  386. typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
  387. typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
  388. typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
  389. typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
  390. typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
  391. typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
  392. // Creates an optional<T> uninitialized.
  393. // No-throw
  394. optional() : base() {}
  395. // Creates an optional<T> uninitialized.
  396. // No-throw
  397. optional( none_t none_ ) : base(none_) {}
  398. // Creates an optional<T> initialized with 'val'.
  399. // Can throw if T::T(T const&) does
  400. optional ( argument_type val ) : base(val) {}
  401. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  402. // Can throw if T::T(T const&) does
  403. optional ( bool cond, argument_type val ) : base(cond,val) {}
  404. #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
  405. // NOTE: MSVC needs templated versions first
  406. // Creates a deep copy of another convertible optional<U>
  407. // Requires a valid conversion from U to T.
  408. // Can throw if T::T(U const&) does
  409. template<class U>
  410. explicit optional ( optional<U> const& rhs )
  411. :
  412. base()
  413. {
  414. if ( rhs.is_initialized() )
  415. this->construct(rhs.get());
  416. }
  417. #endif
  418. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  419. // Creates an optional<T> with an expression which can be either
  420. // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
  421. // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
  422. // (c) Any expression implicitely convertible to the single type
  423. // of a one-argument T's constructor.
  424. // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
  425. // even though explicit overloads are present for these.
  426. // Depending on the above some T ctor is called.
  427. // Can throw is the resolved T ctor throws.
  428. template<class Expr>
  429. explicit optional ( Expr const& expr ) : base(expr,&expr) {}
  430. #endif
  431. // Creates a deep copy of another optional<T>
  432. // Can throw if T::T(T const&) does
  433. optional ( optional const& rhs ) : base(rhs) {}
  434. // No-throw (assuming T::~T() doesn't)
  435. ~optional() {}
  436. #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  437. // Assigns from an expression. See corresponding constructor.
  438. // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
  439. template<class Expr>
  440. optional& operator= ( Expr expr )
  441. {
  442. this->assign_expr(expr,&expr);
  443. return *this ;
  444. }
  445. #endif
  446. #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
  447. // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
  448. // Requires a valid conversion from U to T.
  449. // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
  450. template<class U>
  451. optional& operator= ( optional<U> const& rhs )
  452. {
  453. this->assign(rhs);
  454. return *this ;
  455. }
  456. #endif
  457. // Assigns from another optional<T> (deep-copies the rhs value)
  458. // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
  459. // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
  460. optional& operator= ( optional const& rhs )
  461. {
  462. this->assign( rhs ) ;
  463. return *this ;
  464. }
  465. // Assigns from a T (deep-copies the rhs value)
  466. // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
  467. optional& operator= ( argument_type val )
  468. {
  469. this->assign( val ) ;
  470. return *this ;
  471. }
  472. // Assigns from a "none"
  473. // Which destroys the current value, if any, leaving this UNINITIALIZED
  474. // No-throw (assuming T::~T() doesn't)
  475. optional& operator= ( none_t none_ )
  476. {
  477. this->assign( none_ ) ;
  478. return *this ;
  479. }
  480. // Returns a reference to the value if this is initialized, otherwise,
  481. // the behaviour is UNDEFINED
  482. // No-throw
  483. reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  484. reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  485. // Returns a copy of the value if this is initialized, 'v' otherwise
  486. reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
  487. reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
  488. // Returns a pointer to the value if this is initialized, otherwise,
  489. // the behaviour is UNDEFINED
  490. // No-throw
  491. pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  492. pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  493. // Returns a reference to the value if this is initialized, otherwise,
  494. // the behaviour is UNDEFINED
  495. // No-throw
  496. reference_const_type operator *() const { return this->get() ; }
  497. reference_type operator *() { return this->get() ; }
  498. // implicit conversion to "bool"
  499. // No-throw
  500. operator unspecified_bool_type() const { return this->safe_bool() ; }
  501. // This is provided for those compilers which don't like the conversion to bool
  502. // on some contexts.
  503. bool operator!() const { return !this->is_initialized() ; }
  504. } ;
  505. // Returns optional<T>(v)
  506. template<class T>
  507. inline
  508. optional<T> make_optional ( T const& v )
  509. {
  510. return optional<T>(v);
  511. }
  512. // Returns optional<T>(cond,v)
  513. template<class T>
  514. inline
  515. optional<T> make_optional ( bool cond, T const& v )
  516. {
  517. return optional<T>(cond,v);
  518. }
  519. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  520. // No-throw
  521. template<class T>
  522. inline
  523. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  524. get ( optional<T> const& opt )
  525. {
  526. return opt.get() ;
  527. }
  528. template<class T>
  529. inline
  530. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  531. get ( optional<T>& opt )
  532. {
  533. return opt.get() ;
  534. }
  535. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  536. // No-throw
  537. template<class T>
  538. inline
  539. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  540. get ( optional<T> const* opt )
  541. {
  542. return opt->get_ptr() ;
  543. }
  544. template<class T>
  545. inline
  546. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  547. get ( optional<T>* opt )
  548. {
  549. return opt->get_ptr() ;
  550. }
  551. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  552. // No-throw
  553. template<class T>
  554. inline
  555. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  556. get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
  557. {
  558. return opt.get_value_or(v) ;
  559. }
  560. template<class T>
  561. inline
  562. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  563. get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
  564. {
  565. return opt.get_value_or(v) ;
  566. }
  567. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  568. // No-throw
  569. template<class T>
  570. inline
  571. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  572. get_pointer ( optional<T> const& opt )
  573. {
  574. return opt.get_ptr() ;
  575. }
  576. template<class T>
  577. inline
  578. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  579. get_pointer ( optional<T>& opt )
  580. {
  581. return opt.get_ptr() ;
  582. }
  583. // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
  584. // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
  585. //
  586. // optional<T> vs optional<T> cases
  587. //
  588. template<class T>
  589. inline
  590. bool operator == ( optional<T> const& x, optional<T> const& y )
  591. { return equal_pointees(x,y); }
  592. template<class T>
  593. inline
  594. bool operator < ( optional<T> const& x, optional<T> const& y )
  595. { return less_pointees(x,y); }
  596. template<class T>
  597. inline
  598. bool operator != ( optional<T> const& x, optional<T> const& y )
  599. { return !( x == y ) ; }
  600. template<class T>
  601. inline
  602. bool operator > ( optional<T> const& x, optional<T> const& y )
  603. { return y < x ; }
  604. template<class T>
  605. inline
  606. bool operator <= ( optional<T> const& x, optional<T> const& y )
  607. { return !( y < x ) ; }
  608. template<class T>
  609. inline
  610. bool operator >= ( optional<T> const& x, optional<T> const& y )
  611. { return !( x < y ) ; }
  612. //
  613. // optional<T> vs T cases
  614. //
  615. template<class T>
  616. inline
  617. bool operator == ( optional<T> const& x, T const& y )
  618. { return equal_pointees(x, optional<T>(y)); }
  619. template<class T>
  620. inline
  621. bool operator < ( optional<T> const& x, T const& y )
  622. { return less_pointees(x, optional<T>(y)); }
  623. template<class T>
  624. inline
  625. bool operator != ( optional<T> const& x, T const& y )
  626. { return !( x == y ) ; }
  627. template<class T>
  628. inline
  629. bool operator > ( optional<T> const& x, T const& y )
  630. { return y < x ; }
  631. template<class T>
  632. inline
  633. bool operator <= ( optional<T> const& x, T const& y )
  634. { return !( y < x ) ; }
  635. template<class T>
  636. inline
  637. bool operator >= ( optional<T> const& x, T const& y )
  638. { return !( x < y ) ; }
  639. //
  640. // T vs optional<T> cases
  641. //
  642. template<class T>
  643. inline
  644. bool operator == ( T const& x, optional<T> const& y )
  645. { return equal_pointees( optional<T>(x), y ); }
  646. template<class T>
  647. inline
  648. bool operator < ( T const& x, optional<T> const& y )
  649. { return less_pointees( optional<T>(x), y ); }
  650. template<class T>
  651. inline
  652. bool operator != ( T const& x, optional<T> const& y )
  653. { return !( x == y ) ; }
  654. template<class T>
  655. inline
  656. bool operator > ( T const& x, optional<T> const& y )
  657. { return y < x ; }
  658. template<class T>
  659. inline
  660. bool operator <= ( T const& x, optional<T> const& y )
  661. { return !( y < x ) ; }
  662. template<class T>
  663. inline
  664. bool operator >= ( T const& x, optional<T> const& y )
  665. { return !( x < y ) ; }
  666. //
  667. // optional<T> vs none cases
  668. //
  669. template<class T>
  670. inline
  671. bool operator == ( optional<T> const& x, none_t )
  672. { return equal_pointees(x, optional<T>() ); }
  673. template<class T>
  674. inline
  675. bool operator < ( optional<T> const& x, none_t )
  676. { return less_pointees(x,optional<T>() ); }
  677. template<class T>
  678. inline
  679. bool operator != ( optional<T> const& x, none_t y )
  680. { return !( x == y ) ; }
  681. template<class T>
  682. inline
  683. bool operator > ( optional<T> const& x, none_t y )
  684. { return y < x ; }
  685. template<class T>
  686. inline
  687. bool operator <= ( optional<T> const& x, none_t y )
  688. { return !( y < x ) ; }
  689. template<class T>
  690. inline
  691. bool operator >= ( optional<T> const& x, none_t y )
  692. { return !( x < y ) ; }
  693. //
  694. // none vs optional<T> cases
  695. //
  696. template<class T>
  697. inline
  698. bool operator == ( none_t x, optional<T> const& y )
  699. { return equal_pointees(optional<T>() ,y); }
  700. template<class T>
  701. inline
  702. bool operator < ( none_t x, optional<T> const& y )
  703. { return less_pointees(optional<T>() ,y); }
  704. template<class T>
  705. inline
  706. bool operator != ( none_t x, optional<T> const& y )
  707. { return !( x == y ) ; }
  708. template<class T>
  709. inline
  710. bool operator > ( none_t x, optional<T> const& y )
  711. { return y < x ; }
  712. template<class T>
  713. inline
  714. bool operator <= ( none_t x, optional<T> const& y )
  715. { return !( y < x ) ; }
  716. template<class T>
  717. inline
  718. bool operator >= ( none_t x, optional<T> const& y )
  719. { return !( x < y ) ; }
  720. //
  721. // The following swap implementation follows the GCC workaround as found in
  722. // "boost/detail/compressed_pair.hpp"
  723. //
  724. namespace optional_detail {
  725. // GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
  726. #if BOOST_WORKAROUND(__GNUC__, < 3) \
  727. || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
  728. using std::swap;
  729. #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
  730. #endif
  731. // optional's swap:
  732. // If both are initialized, calls swap(T&, T&). If this swap throws, both will remain initialized but their values are now unspecified.
  733. // If only one is initialized, calls U.reset(*I), THEN I.reset().
  734. // If U.reset(*I) throws, both are left UNCHANGED (U is kept uinitialized and I is never reset)
  735. // If both are uninitialized, do nothing (no-throw)
  736. template<class T>
  737. inline
  738. void optional_swap ( optional<T>& x, optional<T>& y )
  739. {
  740. if ( !x && !!y )
  741. {
  742. x.reset(*y);
  743. y.reset();
  744. }
  745. else if ( !!x && !y )
  746. {
  747. y.reset(*x);
  748. x.reset();
  749. }
  750. else if ( !!x && !!y )
  751. {
  752. // GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
  753. #ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
  754. // allow for Koenig lookup
  755. using std::swap ;
  756. #endif
  757. swap(*x,*y);
  758. }
  759. }
  760. } // namespace optional_detail
  761. template<class T> inline void swap ( optional<T>& x, optional<T>& y )
  762. {
  763. optional_detail::optional_swap(x,y);
  764. }
  765. } // namespace boost
  766. #endif