1 // trivilally-copyable version of the storage
4 class tc_optional_base : public optional_tag
8 typedef tc_optional_base<T> this_type ;
12 typedef T value_type ;
15 typedef T & reference_type ;
16 typedef T const& reference_const_type ;
17 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
18 typedef T && rval_reference_type ;
19 typedef T && reference_type_of_temporary_wrapper ;
21 typedef T * pointer_type ;
22 typedef T const* pointer_const_type ;
23 typedef T const& argument_type ;
27 m_initialized(false) {}
29 tc_optional_base ( none_t )
31 m_initialized(false) {}
33 tc_optional_base ( init_value_tag, argument_type val )
35 m_initialized(true), m_storage(val) {}
37 tc_optional_base ( bool cond, argument_type val )
39 m_initialized(cond), m_storage(val) {}
41 // tc_optional_base ( tc_optional_base const& ) = default;
44 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
46 template<class Expr, class PtrExpr>
47 explicit tc_optional_base ( Expr&& expr, PtrExpr const* tag )
51 construct(boost::forward<Expr>(expr),tag);
55 // This is used for both converting and in-place constructions.
56 // Derived classes use the 'tag' to select the appropriate
57 // implementation (the correct 'construct()' overload)
59 explicit tc_optional_base ( Expr const& expr, Expr const* tag )
68 // tc_optional_base& operator= ( tc_optional_base const& ) = default;
69 // ~tc_optional_base() = default;
71 // Assigns from another optional<T> (deep-copies the rhs value)
72 void assign ( tc_optional_base const& rhs )
77 // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
79 void assign ( optional<U> const& rhs )
81 if ( rhs.is_initialized() )
82 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
83 m_storage = rhs.get();
85 m_storage = static_cast<value_type>(rhs.get());
88 m_initialized = rhs.is_initialized();
91 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
92 // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
94 void assign ( optional<U>&& rhs )
96 typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
97 if ( rhs.is_initialized() )
98 m_storage = static_cast<ref_type>(rhs.get());
99 m_initialized = rhs.is_initialized();
103 void assign ( argument_type val )
108 void assign ( none_t ) { destroy(); }
110 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
112 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
113 template<class Expr, class ExprPtr>
114 void assign_expr ( Expr&& expr, ExprPtr const* tag )
116 construct(boost::forward<Expr>(expr),tag);
120 void assign_expr ( Expr const& expr, Expr const* tag )
130 // Destroys the current value, if any, leaving this UNINITIALIZED
131 // No-throw (assuming T::~T() doesn't)
132 void reset() BOOST_NOEXCEPT { destroy(); }
134 // **DEPPRECATED** Replaces the current value -if any- with 'val'
135 void reset ( argument_type val ) BOOST_NOEXCEPT { assign(val); }
137 // Returns a pointer to the value if this is initialized, otherwise,
140 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
141 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
143 bool is_initialized() const { return m_initialized ; }
147 void construct ( argument_type val )
150 m_initialized = true ;
154 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
155 // Constructs in-place
156 // upon exception *this is always uninitialized
157 template<class... Args>
158 void construct ( in_place_init_t, Args&&... args )
160 m_storage = value_type( boost::forward<Args>(args)... ) ;
161 m_initialized = true ;
164 template<class... Args>
165 void emplace_assign ( Args&&... args )
167 construct(in_place_init, boost::forward<Args>(args)...);
170 template<class... Args>
171 explicit tc_optional_base ( in_place_init_t, Args&&... args )
175 construct(in_place_init, boost::forward<Args>(args)...);
178 template<class... Args>
179 explicit tc_optional_base ( in_place_init_if_t, bool cond, Args&&... args )
184 construct(in_place_init, boost::forward<Args>(args)...);
186 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
188 void construct ( in_place_init_t, Arg&& arg )
190 m_storage = value_type( boost::forward<Arg>(arg) );
191 m_initialized = true ;
194 void construct ( in_place_init_t )
196 m_storage = value_type();
197 m_initialized = true ;
201 void emplace_assign ( Arg&& arg )
203 construct(in_place_init, boost::forward<Arg>(arg)) ;
206 void emplace_assign ()
208 construct(in_place_init) ;
212 explicit tc_optional_base ( in_place_init_t, Arg&& arg )
216 construct(in_place_init, boost::forward<Arg>(arg));
219 explicit tc_optional_base ( in_place_init_t )
221 m_initialized(false), m_storage() {}
224 explicit tc_optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
229 construct(in_place_init, boost::forward<Arg>(arg));
232 explicit tc_optional_base ( in_place_init_if_t, bool cond )
237 construct(in_place_init);
243 void construct ( in_place_init_t, const Arg& arg )
245 m_storage = value_type( arg );
246 m_initialized = true ;
250 void construct ( in_place_init_t, Arg& arg )
252 m_storage = value_type( arg );
253 m_initialized = true ;
256 void construct ( in_place_init_t )
258 m_storage = value_type();
259 m_initialized = true ;
263 void emplace_assign ( const Arg& arg )
265 construct(in_place_init, arg);
269 void emplace_assign ( Arg& arg )
271 construct(in_place_init, arg);
274 void emplace_assign ()
276 construct(in_place_init);
280 explicit tc_optional_base ( in_place_init_t, const Arg& arg )
281 : m_initialized(false)
283 construct(in_place_init, arg);
287 explicit tc_optional_base ( in_place_init_t, Arg& arg )
288 : m_initialized(false)
290 construct(in_place_init, arg);
293 explicit tc_optional_base ( in_place_init_t )
294 : m_initialized(false)
296 construct(in_place_init);
300 explicit tc_optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
301 : m_initialized(false)
304 construct(in_place_init, arg);
308 explicit tc_optional_base ( in_place_init_if_t, bool cond, Arg& arg )
309 : m_initialized(false)
312 construct(in_place_init, arg);
315 explicit tc_optional_base ( in_place_init_if_t, bool cond )
316 : m_initialized(false)
319 construct(in_place_init);
323 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
325 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
326 // Constructs in-place using the given factory
328 void construct ( Expr&& factory, in_place_factory_base const* )
330 boost_optional_detail::construct<value_type>(factory, boost::addressof(m_storage));
331 m_initialized = true ;
334 // Constructs in-place using the given typed factory
336 void construct ( Expr&& factory, typed_in_place_factory_base const* )
338 factory.apply(boost::addressof(m_storage)) ;
339 m_initialized = true ;
343 void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
346 construct(factory,tag);
349 // Constructs in-place using the given typed factory
351 void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
354 construct(factory,tag);
358 // Constructs in-place using the given factory
360 void construct ( Expr const& factory, in_place_factory_base const* )
362 boost_optional_detail::construct<value_type>(factory, m_storage.address());
363 m_initialized = true ;
366 // Constructs in-place using the given typed factory
368 void construct ( Expr const& factory, typed_in_place_factory_base const* )
370 factory.apply(boost::addressof(m_storage)) ;
371 m_initialized = true ;
375 void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
378 construct(factory,tag);
381 // Constructs in-place using the given typed factory
383 void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
386 construct(factory,tag);
392 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
393 // Constructs using any expression implicitly convertible to the single argument
394 // of a one-argument T constructor.
395 // Converting constructions of optional<T> from optional<U> uses this function with
396 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
398 void construct ( Expr&& expr, void const* )
400 m_storage = value_type(boost::forward<Expr>(expr)) ;
401 m_initialized = true ;
404 // Assigns using a form any expression implicitly convertible to the single argument
405 // of a T's assignment operator.
406 // Converting assignments of optional<T> from optional<U> uses this function with
407 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
409 void assign_expr_to_initialized ( Expr&& expr, void const* )
411 assign_value( boost::forward<Expr>(expr) );
414 // Constructs using any expression implicitly convertible to the single argument
415 // of a one-argument T constructor.
416 // Converting constructions of optional<T> from optional<U> uses this function with
417 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
419 void construct ( Expr const& expr, void const* )
421 m_storage = value_type(expr) ;
422 m_initialized = true ;
425 // Assigns using a form any expression implicitly convertible to the single argument
426 // of a T's assignment operator.
427 // Converting assignments of optional<T> from optional<U> uses this function with
428 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
430 void assign_expr_to_initialized ( Expr const& expr, void const* )
437 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
438 // BCB5.64 (and probably lower versions) workaround.
439 // The in-place factories are supported by means of catch-all constructors
440 // and assignment operators (the functions are parameterized in terms of
441 // an arbitrary 'Expr' type)
442 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
443 // to the 'Expr'-taking functions even though explicit overloads are present for them.
444 // Thus, the following overload is needed to properly handle the case when the 'lhs'
445 // is another optional.
447 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
448 // instead of choosing the wrong overload
450 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
451 // Notice that 'Expr' will be optional<T> or optional<U> (but not tc_optional_base<..>)
453 void construct ( Expr&& expr, optional_tag const* )
455 if ( expr.is_initialized() )
457 // An exception can be thrown here.
458 // It it happens, THIS will be left uninitialized.
459 m_storage = value_type(boost::move(expr.get())) ;
460 m_initialized = true ;
464 // Notice that 'Expr' will be optional<T> or optional<U> (but not tc_optional_base<..>)
466 void construct ( Expr const& expr, optional_tag const* )
468 if ( expr.is_initialized() )
470 // An exception can be thrown here.
471 // It it happens, THIS will be left uninitialized.
472 m_storage = value_type(expr.get()) ;
473 m_initialized = true ;
477 #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
479 void assign_value ( argument_type val ) { m_storage = val; }
480 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
481 void assign_value ( rval_reference_type val ) { m_storage = static_cast<rval_reference_type>(val); }
486 m_initialized = false;
489 reference_const_type get_impl() const { return m_storage ; }
490 reference_type get_impl() { return m_storage ; }
492 pointer_const_type get_ptr_impl() const { return boost::addressof(m_storage); }
493 pointer_type get_ptr_impl() { return boost::addressof(m_storage); }