optional_trivially_copyable_base.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. typedef T && rval_reference_type ;
  23. typedef T && reference_type_of_temporary_wrapper ;
  24. typedef T * pointer_type ;
  25. typedef T const* pointer_const_type ;
  26. typedef T const& argument_type ;
  27. tc_optional_base()
  28. :
  29. m_initialized(false), m_storage() {}
  30. tc_optional_base ( none_t )
  31. :
  32. m_initialized(false), m_storage() {}
  33. tc_optional_base ( init_value_tag, argument_type val )
  34. :
  35. m_initialized(true), m_storage(val) {}
  36. tc_optional_base ( bool cond, argument_type val )
  37. :
  38. m_initialized(cond), m_storage(val) {}
  39. // tc_optional_base ( tc_optional_base const& ) = default;
  40. template<class Expr, class PtrExpr>
  41. explicit tc_optional_base ( Expr&& expr, PtrExpr const* tag )
  42. :
  43. m_initialized(false)
  44. {
  45. construct(optional_detail::forward<Expr>(expr),tag);
  46. }
  47. // tc_optional_base& operator= ( tc_optional_base const& ) = default;
  48. // ~tc_optional_base() = default;
  49. // Assigns from another optional<T> (deep-copies the rhs value)
  50. void assign ( tc_optional_base const& rhs )
  51. {
  52. *this = rhs;
  53. }
  54. // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
  55. template<class U>
  56. void assign ( optional<U> const& rhs )
  57. {
  58. if ( rhs.is_initialized() )
  59. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  60. m_storage = rhs.get();
  61. #else
  62. m_storage = static_cast<value_type>(rhs.get());
  63. #endif
  64. m_initialized = rhs.is_initialized();
  65. }
  66. // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
  67. template<class U>
  68. void assign ( optional<U>&& rhs )
  69. {
  70. typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
  71. if ( rhs.is_initialized() )
  72. m_storage = static_cast<ref_type>(rhs.get());
  73. m_initialized = rhs.is_initialized();
  74. }
  75. void assign ( argument_type val )
  76. {
  77. construct(val);
  78. }
  79. void assign ( none_t ) { destroy(); }
  80. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  81. template<class Expr, class ExprPtr>
  82. void assign_expr ( Expr&& expr, ExprPtr const* tag )
  83. {
  84. construct(optional_detail::forward<Expr>(expr),tag);
  85. }
  86. #endif
  87. public :
  88. // Destroys the current value, if any, leaving this UNINITIALIZED
  89. // No-throw (assuming T::~T() doesn't)
  90. void reset() BOOST_NOEXCEPT { destroy(); }
  91. // **DEPRECATED** Replaces the current value -if any- with 'val'
  92. void reset ( argument_type val ) BOOST_NOEXCEPT { assign(val); }
  93. // Returns a pointer to the value if this is initialized, otherwise,
  94. // returns NULL.
  95. // No-throw
  96. pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
  97. pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
  98. bool is_initialized() const { return m_initialized ; }
  99. protected :
  100. void construct ( argument_type val )
  101. {
  102. m_storage = val ;
  103. m_initialized = true ;
  104. }
  105. // Constructs in-place
  106. // upon exception *this is always uninitialized
  107. template<class... Args>
  108. void construct ( in_place_init_t, Args&&... args )
  109. {
  110. m_storage = value_type( optional_detail::forward<Args>(args)... ) ;
  111. m_initialized = true ;
  112. }
  113. template<class... Args>
  114. void emplace_assign ( Args&&... args )
  115. {
  116. construct(in_place_init, optional_detail::forward<Args>(args)...);
  117. }
  118. template<class... Args>
  119. explicit tc_optional_base ( in_place_init_t, Args&&... args )
  120. :
  121. m_initialized(false)
  122. {
  123. construct(in_place_init, optional_detail::forward<Args>(args)...);
  124. }
  125. template<class... Args>
  126. explicit tc_optional_base ( in_place_init_if_t, bool cond, Args&&... args )
  127. :
  128. m_initialized(false)
  129. {
  130. if ( cond )
  131. construct(in_place_init, optional_detail::forward<Args>(args)...);
  132. }
  133. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  134. // Constructs in-place using the given factory
  135. template<class Expr>
  136. void construct ( Expr&& factory, in_place_factory_base const* )
  137. {
  138. boost_optional_detail::construct<value_type>(factory, boost::addressof(m_storage));
  139. m_initialized = true ;
  140. }
  141. // Constructs in-place using the given typed factory
  142. template<class Expr>
  143. void construct ( Expr&& factory, typed_in_place_factory_base const* )
  144. {
  145. factory.apply(boost::addressof(m_storage)) ;
  146. m_initialized = true ;
  147. }
  148. template<class Expr>
  149. void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
  150. {
  151. destroy();
  152. construct(factory,tag);
  153. }
  154. // Constructs in-place using the given typed factory
  155. template<class Expr>
  156. void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
  157. {
  158. destroy();
  159. construct(factory,tag);
  160. }
  161. #endif
  162. // Constructs using any expression implicitly convertible to the single argument
  163. // of a one-argument T constructor.
  164. // Converting constructions of optional<T> from optional<U> uses this function with
  165. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  166. template<class Expr>
  167. void construct ( Expr&& expr, void const* )
  168. {
  169. m_storage = value_type(optional_detail::forward<Expr>(expr)) ;
  170. m_initialized = true ;
  171. }
  172. // Assigns using a form any expression implicitly convertible to the single argument
  173. // of a T's assignment operator.
  174. // Converting assignments of optional<T> from optional<U> uses this function with
  175. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  176. template<class Expr>
  177. void assign_expr_to_initialized ( Expr&& expr, void const* )
  178. {
  179. assign_value( optional_detail::forward<Expr>(expr) );
  180. }
  181. #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  182. // BCB5.64 (and probably lower versions) workaround.
  183. // The in-place factories are supported by means of catch-all constructors
  184. // and assignment operators (the functions are parameterized in terms of
  185. // an arbitrary 'Expr' type)
  186. // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
  187. // to the 'Expr'-taking functions even though explicit overloads are present for them.
  188. // Thus, the following overload is needed to properly handle the case when the 'lhs'
  189. // is another optional.
  190. //
  191. // For VC<=70 compilers this workaround doesn't work because the compiler issues and error
  192. // instead of choosing the wrong overload
  193. //
  194. // Notice that 'Expr' will be optional<T> or optional<U> (but not tc_optional_base<..>)
  195. template<class Expr>
  196. void construct ( Expr&& expr, optional_tag const* )
  197. {
  198. if ( expr.is_initialized() )
  199. {
  200. // An exception can be thrown here.
  201. // It it happens, THIS will be left uninitialized.
  202. m_storage = value_type(optional_detail::move(expr.get())) ;
  203. m_initialized = true ;
  204. }
  205. }
  206. #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  207. void assign_value ( argument_type val ) { m_storage = val; }
  208. void assign_value ( rval_reference_type val ) { m_storage = static_cast<rval_reference_type>(val); }
  209. void destroy()
  210. {
  211. m_initialized = false;
  212. }
  213. reference_const_type get_impl() const { return m_storage ; }
  214. reference_type get_impl() { return m_storage ; }
  215. pointer_const_type get_ptr_impl() const { return boost::addressof(m_storage); }
  216. pointer_type get_ptr_impl() { return boost::addressof(m_storage); }
  217. private :
  218. bool m_initialized ;
  219. T m_storage ;
  220. } ;