optional_trivially_copyable_base.hpp 14 KB

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