optional.hpp 37 KB

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