123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- // Copyright (C) 2017 Andrzej Krzemienski.
- //
- // Use, modification, and distribution is subject to the Boost Software
- // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- //
- // See http://www.boost.org/libs/optional for documentation.
- //
- // You are welcome to contact the author at:
- // akrzemi1@gmail.com
- // trivially-copyable version of the storage
- template<class T>
- class tc_optional_base : public optional_tag
- {
- private :
- typedef tc_optional_base<T> this_type ;
- protected :
- typedef T value_type ;
- protected:
- typedef T & reference_type ;
- typedef T const& reference_const_type ;
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- typedef T && rval_reference_type ;
- typedef T && reference_type_of_temporary_wrapper ;
- #endif
- typedef T * pointer_type ;
- typedef T const* pointer_const_type ;
- typedef T const& argument_type ;
- tc_optional_base()
- :
- m_initialized(false) {}
- tc_optional_base ( none_t )
- :
- m_initialized(false) {}
- tc_optional_base ( init_value_tag, argument_type val )
- :
- m_initialized(true), m_storage(val) {}
- tc_optional_base ( bool cond, argument_type val )
- :
- m_initialized(cond), m_storage(val) {}
- // tc_optional_base ( tc_optional_base const& ) = default;
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- template<class Expr, class PtrExpr>
- explicit tc_optional_base ( Expr&& expr, PtrExpr const* tag )
- :
- m_initialized(false)
- {
- construct(boost::forward<Expr>(expr),tag);
- }
- #else
- // This is used for both converting and in-place constructions.
- // Derived classes use the 'tag' to select the appropriate
- // implementation (the correct 'construct()' overload)
- template<class Expr>
- explicit tc_optional_base ( Expr const& expr, Expr const* tag )
- :
- m_initialized(false)
- {
- construct(expr,tag);
- }
- #endif
- // tc_optional_base& operator= ( tc_optional_base const& ) = default;
- // ~tc_optional_base() = default;
- // Assigns from another optional<T> (deep-copies the rhs value)
- void assign ( tc_optional_base const& rhs )
- {
- *this = rhs;
- }
- // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
- template<class U>
- void assign ( optional<U> const& rhs )
- {
- if ( rhs.is_initialized() )
- #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
- m_storage = rhs.get();
- #else
- m_storage = static_cast<value_type>(rhs.get());
- #endif
- m_initialized = rhs.is_initialized();
- }
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
- template<class U>
- void assign ( optional<U>&& rhs )
- {
- typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
- if ( rhs.is_initialized() )
- m_storage = static_cast<ref_type>(rhs.get());
- m_initialized = rhs.is_initialized();
- }
- #endif
- void assign ( argument_type val )
- {
- construct(val);
- }
- void assign ( none_t ) { destroy(); }
- #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- template<class Expr, class ExprPtr>
- void assign_expr ( Expr&& expr, ExprPtr const* tag )
- {
- construct(boost::forward<Expr>(expr),tag);
- }
- #else
- template<class Expr>
- void assign_expr ( Expr const& expr, Expr const* tag )
- {
- construct(expr,tag);
- }
- #endif
- #endif
- public :
- // Destroys the current value, if any, leaving this UNINITIALIZED
- // No-throw (assuming T::~T() doesn't)
- void reset() BOOST_NOEXCEPT { destroy(); }
- // **DEPRECATED** Replaces the current value -if any- with 'val'
- void reset ( argument_type val ) BOOST_NOEXCEPT { assign(val); }
- // Returns a pointer to the value if this is initialized, otherwise,
- // returns NULL.
- // No-throw
- pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
- pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
- bool is_initialized() const { return m_initialized ; }
- protected :
- void construct ( argument_type val )
- {
- m_storage = val ;
- m_initialized = true ;
- }
- #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
- // Constructs in-place
- // upon exception *this is always uninitialized
- template<class... Args>
- void construct ( in_place_init_t, Args&&... args )
- {
- m_storage = value_type( boost::forward<Args>(args)... ) ;
- m_initialized = true ;
- }
- template<class... Args>
- void emplace_assign ( Args&&... args )
- {
- construct(in_place_init, boost::forward<Args>(args)...);
- }
- template<class... Args>
- explicit tc_optional_base ( in_place_init_t, Args&&... args )
- :
- m_initialized(false)
- {
- construct(in_place_init, boost::forward<Args>(args)...);
- }
- template<class... Args>
- explicit tc_optional_base ( in_place_init_if_t, bool cond, Args&&... args )
- :
- m_initialized(false)
- {
- if ( cond )
- construct(in_place_init, boost::forward<Args>(args)...);
- }
- #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
- template<class Arg>
- void construct ( in_place_init_t, Arg&& arg )
- {
- m_storage = value_type( boost::forward<Arg>(arg) );
- m_initialized = true ;
- }
- void construct ( in_place_init_t )
- {
- m_storage = value_type();
- m_initialized = true ;
- }
- template<class Arg>
- void emplace_assign ( Arg&& arg )
- {
- construct(in_place_init, boost::forward<Arg>(arg)) ;
- }
- void emplace_assign ()
- {
- construct(in_place_init) ;
- }
- template<class Arg>
- explicit tc_optional_base ( in_place_init_t, Arg&& arg )
- :
- m_initialized(false)
- {
- construct(in_place_init, boost::forward<Arg>(arg));
- }
- explicit tc_optional_base ( in_place_init_t )
- :
- m_initialized(false), m_storage() {}
- template<class Arg>
- explicit tc_optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
- :
- m_initialized(false)
- {
- if ( cond )
- construct(in_place_init, boost::forward<Arg>(arg));
- }
- explicit tc_optional_base ( in_place_init_if_t, bool cond )
- :
- m_initialized(false)
- {
- if ( cond )
- construct(in_place_init);
- }
- #else
- template<class Arg>
- void construct ( in_place_init_t, const Arg& arg )
- {
- m_storage = value_type( arg );
- m_initialized = true ;
- }
- template<class Arg>
- void construct ( in_place_init_t, Arg& arg )
- {
- m_storage = value_type( arg );
- m_initialized = true ;
- }
- void construct ( in_place_init_t )
- {
- m_storage = value_type();
- m_initialized = true ;
- }
- template<class Arg>
- void emplace_assign ( const Arg& arg )
- {
- construct(in_place_init, arg);
- }
- template<class Arg>
- void emplace_assign ( Arg& arg )
- {
- construct(in_place_init, arg);
- }
- void emplace_assign ()
- {
- construct(in_place_init);
- }
- template<class Arg>
- explicit tc_optional_base ( in_place_init_t, const Arg& arg )
- : m_initialized(false)
- {
- construct(in_place_init, arg);
- }
- template<class Arg>
- explicit tc_optional_base ( in_place_init_t, Arg& arg )
- : m_initialized(false)
- {
- construct(in_place_init, arg);
- }
- explicit tc_optional_base ( in_place_init_t )
- : m_initialized(false)
- {
- construct(in_place_init);
- }
- template<class Arg>
- explicit tc_optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
- : m_initialized(false)
- {
- if ( cond )
- construct(in_place_init, arg);
- }
- template<class Arg>
- explicit tc_optional_base ( in_place_init_if_t, bool cond, Arg& arg )
- : m_initialized(false)
- {
- if ( cond )
- construct(in_place_init, arg);
- }
- explicit tc_optional_base ( in_place_init_if_t, bool cond )
- : m_initialized(false)
- {
- if ( cond )
- construct(in_place_init);
- }
- #endif
- #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- // Constructs in-place using the given factory
- template<class Expr>
- void construct ( Expr&& factory, in_place_factory_base const* )
- {
- boost_optional_detail::construct<value_type>(factory, boost::addressof(m_storage));
- m_initialized = true ;
- }
- // Constructs in-place using the given typed factory
- template<class Expr>
- void construct ( Expr&& factory, typed_in_place_factory_base const* )
- {
- factory.apply(boost::addressof(m_storage)) ;
- m_initialized = true ;
- }
- template<class Expr>
- void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
- {
- destroy();
- construct(factory,tag);
- }
- // Constructs in-place using the given typed factory
- template<class Expr>
- void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
- {
- destroy();
- construct(factory,tag);
- }
- #else
- // Constructs in-place using the given factory
- template<class Expr>
- void construct ( Expr const& factory, in_place_factory_base const* )
- {
- boost_optional_detail::construct<value_type>(factory, boost::addressof(m_storage));
- m_initialized = true ;
- }
- // Constructs in-place using the given typed factory
- template<class Expr>
- void construct ( Expr const& factory, typed_in_place_factory_base const* )
- {
- factory.apply(boost::addressof(m_storage)) ;
- m_initialized = true ;
- }
- template<class Expr>
- void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
- {
- destroy();
- construct(factory,tag);
- }
- // Constructs in-place using the given typed factory
- template<class Expr>
- void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
- {
- destroy();
- construct(factory,tag);
- }
- #endif
- #endif
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- // Constructs using any expression implicitly convertible to the single argument
- // of a one-argument T constructor.
- // Converting constructions of optional<T> from optional<U> uses this function with
- // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
- template<class Expr>
- void construct ( Expr&& expr, void const* )
- {
- m_storage = value_type(boost::forward<Expr>(expr)) ;
- m_initialized = true ;
- }
- // Assigns using a form any expression implicitly convertible to the single argument
- // of a T's assignment operator.
- // Converting assignments of optional<T> from optional<U> uses this function with
- // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
- template<class Expr>
- void assign_expr_to_initialized ( Expr&& expr, void const* )
- {
- assign_value( boost::forward<Expr>(expr) );
- }
- #else
- // Constructs using any expression implicitly convertible to the single argument
- // of a one-argument T constructor.
- // Converting constructions of optional<T> from optional<U> uses this function with
- // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
- template<class Expr>
- void construct ( Expr const& expr, void const* )
- {
- m_storage = value_type(expr) ;
- m_initialized = true ;
- }
- // Assigns using a form any expression implicitly convertible to the single argument
- // of a T's assignment operator.
- // Converting assignments of optional<T> from optional<U> uses this function with
- // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
- template<class Expr>
- void assign_expr_to_initialized ( Expr const& expr, void const* )
- {
- assign_value(expr);
- }
- #endif
- #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
- // BCB5.64 (and probably lower versions) workaround.
- // The in-place factories are supported by means of catch-all constructors
- // and assignment operators (the functions are parameterized in terms of
- // an arbitrary 'Expr' type)
- // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
- // to the 'Expr'-taking functions even though explicit overloads are present for them.
- // Thus, the following overload is needed to properly handle the case when the 'lhs'
- // is another optional.
- //
- // For VC<=70 compilers this workaround doesn't work because the compiler issues and error
- // instead of choosing the wrong overload
- //
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- // Notice that 'Expr' will be optional<T> or optional<U> (but not tc_optional_base<..>)
- template<class Expr>
- void construct ( Expr&& expr, optional_tag const* )
- {
- if ( expr.is_initialized() )
- {
- // An exception can be thrown here.
- // It it happens, THIS will be left uninitialized.
- m_storage = value_type(boost::move(expr.get())) ;
- m_initialized = true ;
- }
- }
- #else
- // Notice that 'Expr' will be optional<T> or optional<U> (but not tc_optional_base<..>)
- template<class Expr>
- void construct ( Expr const& expr, optional_tag const* )
- {
- if ( expr.is_initialized() )
- {
- // An exception can be thrown here.
- // It it happens, THIS will be left uninitialized.
- m_storage = value_type(expr.get()) ;
- m_initialized = true ;
- }
- }
- #endif
- #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
- void assign_value ( argument_type val ) { m_storage = val; }
- #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
- void assign_value ( rval_reference_type val ) { m_storage = static_cast<rval_reference_type>(val); }
- #endif
- void destroy()
- {
- m_initialized = false;
- }
- reference_const_type get_impl() const { return m_storage ; }
- reference_type get_impl() { return m_storage ; }
- pointer_const_type get_ptr_impl() const { return boost::addressof(m_storage); }
- pointer_type get_ptr_impl() { return boost::addressof(m_storage); }
- private :
- bool m_initialized ;
- T m_storage ;
- } ;
|