optional_trivially_copyable_base.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // Copyright (C) 2017 Andrzej Krzemienski.
  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/libs/optional for documentation.
  8. //
  9. // You are welcome to contact the author at:
  10. // akrzemi1@gmail.com
  11. // trivially-copyable version of the storage
  12. template<class T>
  13. class tc_optional_base : public optional_tag
  14. {
  15. private :
  16. typedef tc_optional_base<T> this_type ;
  17. protected :
  18. typedef T value_type ;
  19. protected:
  20. typedef T & reference_type ;
  21. typedef T const& reference_const_type ;
  22. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  23. typedef T && rval_reference_type ;
  24. typedef T && reference_type_of_temporary_wrapper ;
  25. #endif
  26. typedef T * pointer_type ;
  27. typedef T const* pointer_const_type ;
  28. typedef T const& argument_type ;
  29. tc_optional_base()
  30. :
  31. m_initialized(false) {}
  32. tc_optional_base ( none_t )
  33. :
  34. m_initialized(false) {}
  35. tc_optional_base ( init_value_tag, argument_type val )
  36. :
  37. m_initialized(true), m_storage(val) {}
  38. tc_optional_base ( bool cond, argument_type val )
  39. :
  40. m_initialized(cond), m_storage(val) {}
  41. // tc_optional_base ( tc_optional_base const& ) = default;
  42. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  43. template<class Expr, class PtrExpr>
  44. explicit tc_optional_base ( Expr&& expr, PtrExpr const* tag )
  45. :
  46. m_initialized(false)
  47. {
  48. construct(boost::forward<Expr>(expr),tag);
  49. }
  50. #else
  51. // This is used for both converting and in-place constructions.
  52. // Derived classes use the 'tag' to select the appropriate
  53. // implementation (the correct 'construct()' overload)
  54. template<class Expr>
  55. explicit tc_optional_base ( Expr const& expr, Expr const* tag )
  56. :
  57. m_initialized(false)
  58. {
  59. construct(expr,tag);
  60. }
  61. #endif
  62. // tc_optional_base& operator= ( tc_optional_base const& ) = default;
  63. // ~tc_optional_base() = default;
  64. // Assigns from another optional<T> (deep-copies the rhs value)
  65. void assign ( tc_optional_base const& rhs )
  66. {
  67. *this = rhs;
  68. }
  69. // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
  70. template<class U>
  71. void assign ( optional<U> const& rhs )
  72. {
  73. if ( rhs.is_initialized() )
  74. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  75. m_storage = rhs.get();
  76. #else
  77. m_storage = static_cast<value_type>(rhs.get());
  78. #endif
  79. m_initialized = rhs.is_initialized();
  80. }
  81. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  82. // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
  83. template<class U>
  84. void assign ( optional<U>&& rhs )
  85. {
  86. typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
  87. if ( rhs.is_initialized() )
  88. m_storage = static_cast<ref_type>(rhs.get());
  89. m_initialized = rhs.is_initialized();
  90. }
  91. #endif
  92. void assign ( argument_type val )
  93. {
  94. construct(val);
  95. }
  96. void assign ( none_t ) { destroy(); }
  97. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  98. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  99. template<class Expr, class ExprPtr>
  100. void assign_expr ( Expr&& expr, ExprPtr const* tag )
  101. {
  102. construct(boost::forward<Expr>(expr),tag);
  103. }
  104. #else
  105. template<class Expr>
  106. void assign_expr ( Expr const& expr, Expr const* tag )
  107. {
  108. construct(expr,tag);
  109. }
  110. #endif
  111. #endif
  112. public :
  113. // Destroys the current value, if any, leaving this UNINITIALIZED
  114. // No-throw (assuming T::~T() doesn't)
  115. void reset() BOOST_NOEXCEPT { destroy(); }
  116. // **DEPRECATED** Replaces the current value -if any- with 'val'
  117. void reset ( argument_type val ) BOOST_NOEXCEPT { assign(val); }
  118. // Returns a pointer to the value if this is initialized, otherwise,
  119. // returns NULL.
  120. // No-throw
  121. pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
  122. pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
  123. bool is_initialized() const { return m_initialized ; }
  124. protected :
  125. void construct ( argument_type val )
  126. {
  127. m_storage = val ;
  128. m_initialized = true ;
  129. }
  130. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  131. // Constructs in-place
  132. // upon exception *this is always uninitialized
  133. template<class... Args>
  134. void construct ( in_place_init_t, Args&&... args )
  135. {
  136. m_storage = value_type( boost::forward<Args>(args)... ) ;
  137. m_initialized = true ;
  138. }
  139. template<class... Args>
  140. void emplace_assign ( Args&&... args )
  141. {
  142. construct(in_place_init, boost::forward<Args>(args)...);
  143. }
  144. template<class... Args>
  145. explicit tc_optional_base ( in_place_init_t, Args&&... args )
  146. :
  147. m_initialized(false)
  148. {
  149. construct(in_place_init, boost::forward<Args>(args)...);
  150. }
  151. template<class... Args>
  152. explicit tc_optional_base ( in_place_init_if_t, bool cond, Args&&... args )
  153. :
  154. m_initialized(false)
  155. {
  156. if ( cond )
  157. construct(in_place_init, boost::forward<Args>(args)...);
  158. }
  159. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  160. template<class Arg>
  161. void construct ( in_place_init_t, Arg&& arg )
  162. {
  163. m_storage = value_type( boost::forward<Arg>(arg) );
  164. m_initialized = true ;
  165. }
  166. void construct ( in_place_init_t )
  167. {
  168. m_storage = value_type();
  169. m_initialized = true ;
  170. }
  171. template<class Arg>
  172. void emplace_assign ( Arg&& arg )
  173. {
  174. construct(in_place_init, boost::forward<Arg>(arg)) ;
  175. }
  176. void emplace_assign ()
  177. {
  178. construct(in_place_init) ;
  179. }
  180. template<class Arg>
  181. explicit tc_optional_base ( in_place_init_t, Arg&& arg )
  182. :
  183. m_initialized(false)
  184. {
  185. construct(in_place_init, boost::forward<Arg>(arg));
  186. }
  187. explicit tc_optional_base ( in_place_init_t )
  188. :
  189. m_initialized(false), m_storage() {}
  190. template<class Arg>
  191. explicit tc_optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
  192. :
  193. m_initialized(false)
  194. {
  195. if ( cond )
  196. construct(in_place_init, boost::forward<Arg>(arg));
  197. }
  198. explicit tc_optional_base ( in_place_init_if_t, bool cond )
  199. :
  200. m_initialized(false)
  201. {
  202. if ( cond )
  203. construct(in_place_init);
  204. }
  205. #else
  206. template<class Arg>
  207. void construct ( in_place_init_t, const Arg& arg )
  208. {
  209. m_storage = value_type( arg );
  210. m_initialized = true ;
  211. }
  212. template<class Arg>
  213. void construct ( in_place_init_t, Arg& arg )
  214. {
  215. m_storage = value_type( arg );
  216. m_initialized = true ;
  217. }
  218. void construct ( in_place_init_t )
  219. {
  220. m_storage = value_type();
  221. m_initialized = true ;
  222. }
  223. template<class Arg>
  224. void emplace_assign ( const Arg& arg )
  225. {
  226. construct(in_place_init, arg);
  227. }
  228. template<class Arg>
  229. void emplace_assign ( Arg& arg )
  230. {
  231. construct(in_place_init, arg);
  232. }
  233. void emplace_assign ()
  234. {
  235. construct(in_place_init);
  236. }
  237. template<class Arg>
  238. explicit tc_optional_base ( in_place_init_t, const Arg& arg )
  239. : m_initialized(false)
  240. {
  241. construct(in_place_init, arg);
  242. }
  243. template<class Arg>
  244. explicit tc_optional_base ( in_place_init_t, Arg& arg )
  245. : m_initialized(false)
  246. {
  247. construct(in_place_init, arg);
  248. }
  249. explicit tc_optional_base ( in_place_init_t )
  250. : m_initialized(false)
  251. {
  252. construct(in_place_init);
  253. }
  254. template<class Arg>
  255. explicit tc_optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
  256. : m_initialized(false)
  257. {
  258. if ( cond )
  259. construct(in_place_init, arg);
  260. }
  261. template<class Arg>
  262. explicit tc_optional_base ( in_place_init_if_t, bool cond, Arg& arg )
  263. : m_initialized(false)
  264. {
  265. if ( cond )
  266. construct(in_place_init, arg);
  267. }
  268. explicit tc_optional_base ( in_place_init_if_t, bool cond )
  269. : m_initialized(false)
  270. {
  271. if ( cond )
  272. construct(in_place_init);
  273. }
  274. #endif
  275. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  276. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  277. // Constructs in-place using the given factory
  278. template<class Expr>
  279. void construct ( Expr&& factory, in_place_factory_base const* )
  280. {
  281. boost_optional_detail::construct<value_type>(factory, boost::addressof(m_storage));
  282. m_initialized = true ;
  283. }
  284. // Constructs in-place using the given typed factory
  285. template<class Expr>
  286. void construct ( Expr&& factory, typed_in_place_factory_base const* )
  287. {
  288. factory.apply(boost::addressof(m_storage)) ;
  289. m_initialized = true ;
  290. }
  291. template<class Expr>
  292. void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
  293. {
  294. destroy();
  295. construct(factory,tag);
  296. }
  297. // Constructs in-place using the given typed factory
  298. template<class Expr>
  299. void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
  300. {
  301. destroy();
  302. construct(factory,tag);
  303. }
  304. #else
  305. // Constructs in-place using the given factory
  306. template<class Expr>
  307. void construct ( Expr const& factory, in_place_factory_base const* )
  308. {
  309. boost_optional_detail::construct<value_type>(factory, boost::addressof(m_storage));
  310. m_initialized = true ;
  311. }
  312. // Constructs in-place using the given typed factory
  313. template<class Expr>
  314. void construct ( Expr const& factory, typed_in_place_factory_base const* )
  315. {
  316. factory.apply(boost::addressof(m_storage)) ;
  317. m_initialized = true ;
  318. }
  319. template<class Expr>
  320. void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
  321. {
  322. destroy();
  323. construct(factory,tag);
  324. }
  325. // Constructs in-place using the given typed factory
  326. template<class Expr>
  327. void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
  328. {
  329. destroy();
  330. construct(factory,tag);
  331. }
  332. #endif
  333. #endif
  334. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  335. // Constructs using any expression implicitly convertible to the single argument
  336. // of a one-argument T constructor.
  337. // Converting constructions of optional<T> from optional<U> uses this function with
  338. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  339. template<class Expr>
  340. void construct ( Expr&& expr, void const* )
  341. {
  342. m_storage = value_type(boost::forward<Expr>(expr)) ;
  343. m_initialized = true ;
  344. }
  345. // Assigns using a form any expression implicitly convertible to the single argument
  346. // of a T's assignment operator.
  347. // Converting assignments of optional<T> from optional<U> uses this function with
  348. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  349. template<class Expr>
  350. void assign_expr_to_initialized ( Expr&& expr, void const* )
  351. {
  352. assign_value( boost::forward<Expr>(expr) );
  353. }
  354. #else
  355. // Constructs using any expression implicitly convertible to the single argument
  356. // of a one-argument T constructor.
  357. // Converting constructions of optional<T> from optional<U> uses this function with
  358. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  359. template<class Expr>
  360. void construct ( Expr const& expr, void const* )
  361. {
  362. m_storage = value_type(expr) ;
  363. m_initialized = true ;
  364. }
  365. // Assigns using a form any expression implicitly convertible to the single argument
  366. // of a T's assignment operator.
  367. // Converting assignments of optional<T> from optional<U> uses this function with
  368. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  369. template<class Expr>
  370. void assign_expr_to_initialized ( Expr const& expr, void const* )
  371. {
  372. assign_value(expr);
  373. }
  374. #endif
  375. #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  376. // BCB5.64 (and probably lower versions) workaround.
  377. // The in-place factories are supported by means of catch-all constructors
  378. // and assignment operators (the functions are parameterized in terms of
  379. // an arbitrary 'Expr' type)
  380. // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
  381. // to the 'Expr'-taking functions even though explicit overloads are present for them.
  382. // Thus, the following overload is needed to properly handle the case when the 'lhs'
  383. // is another optional.
  384. //
  385. // For VC<=70 compilers this workaround doesn't work because the compiler issues and error
  386. // instead of choosing the wrong overload
  387. //
  388. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  389. // Notice that 'Expr' will be optional<T> or optional<U> (but not tc_optional_base<..>)
  390. template<class Expr>
  391. void construct ( Expr&& expr, optional_tag const* )
  392. {
  393. if ( expr.is_initialized() )
  394. {
  395. // An exception can be thrown here.
  396. // It it happens, THIS will be left uninitialized.
  397. m_storage = value_type(boost::move(expr.get())) ;
  398. m_initialized = true ;
  399. }
  400. }
  401. #else
  402. // Notice that 'Expr' will be optional<T> or optional<U> (but not tc_optional_base<..>)
  403. template<class Expr>
  404. void construct ( Expr const& expr, optional_tag const* )
  405. {
  406. if ( expr.is_initialized() )
  407. {
  408. // An exception can be thrown here.
  409. // It it happens, THIS will be left uninitialized.
  410. m_storage = value_type(expr.get()) ;
  411. m_initialized = true ;
  412. }
  413. }
  414. #endif
  415. #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  416. void assign_value ( argument_type val ) { m_storage = val; }
  417. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  418. void assign_value ( rval_reference_type val ) { m_storage = static_cast<rval_reference_type>(val); }
  419. #endif
  420. void destroy()
  421. {
  422. m_initialized = false;
  423. }
  424. reference_const_type get_impl() const { return m_storage ; }
  425. reference_type get_impl() { return m_storage ; }
  426. pointer_const_type get_ptr_impl() const { return boost::addressof(m_storage); }
  427. pointer_type get_ptr_impl() { return boost::addressof(m_storage); }
  428. private :
  429. bool m_initialized ;
  430. T m_storage ;
  431. } ;