// Copyright Ruslan Arutyunyan, 2019-2021. // Copyright Antony Polukhin, 2021. // // Distributed under 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) // Contributed by Ruslan Arutyunyan #ifndef ACTIV_BOOST_ANYS_BASIC_ANY_HPP_INCLUDED #define ACTIV_BOOST_ANYS_BASIC_ANY_HPP_INCLUDED #include #ifdef ACTIV_BOOST_HAS_PRAGMA_ONCE # pragma once #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Boost { namespace anys { template class basic_any { ACTIV_BOOST_STATIC_ASSERT_MSG(OptimizeForSize > 0 && OptimizeForAlignment > 0, "Size and Align shall be positive values"); ACTIV_BOOST_STATIC_ASSERT_MSG(OptimizeForSize >= OptimizeForAlignment, "Size shall non less than Align"); ACTIV_BOOST_STATIC_ASSERT_MSG((OptimizeForAlignment & (OptimizeForAlignment - 1)) == 0, "Align shall be a power of 2"); ACTIV_BOOST_STATIC_ASSERT_MSG(OptimizeForSize % OptimizeForAlignment == 0, "Size shall be multiple of alignment"); private: enum operation { Destroy, Move, Copy, AnyCast, UnsafeCast, Typeinfo }; template static void* small_manager(operation op, basic_any& left, const basic_any* right, const Boost::typeindex::type_info* info) { switch (op) { case Destroy: ACTIV_BOOST_ASSERT(!left.empty()); reinterpret_cast(&left.content.small_value)->~ValueType(); break; case Move: { ACTIV_BOOST_ASSERT(left.empty()); ACTIV_BOOST_ASSERT(right); ACTIV_BOOST_ASSERT(!right->empty()); ACTIV_BOOST_ASSERT(right->type() == Boost::typeindex::type_id()); ValueType* value = reinterpret_cast(&const_cast(right)->content.small_value); #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES new (&left.content.small_value) ValueType(std::move(*value)); #else new (&left.content.small_value) ValueType(*value); #endif left.man = right->man; reinterpret_cast(&right->content.small_value)->~ValueType(); const_cast(right)->man = 0; }; break; case Copy: ACTIV_BOOST_ASSERT(left.empty()); ACTIV_BOOST_ASSERT(right); ACTIV_BOOST_ASSERT(!right->empty()); ACTIV_BOOST_ASSERT(right->type() == Boost::typeindex::type_id()); new (&left.content.small_value) ValueType(*reinterpret_cast(&right->content.small_value)); left.man = right->man; break; case AnyCast: ACTIV_BOOST_ASSERT(info); ACTIV_BOOST_ASSERT(!left.empty()); return Boost::typeindex::type_id() == *info ? reinterpret_cast::type *>(&left.content.small_value) : 0; case UnsafeCast: ACTIV_BOOST_ASSERT(!left.empty()); return reinterpret_cast::type *>(&left.content.small_value); case Typeinfo: return const_cast(static_cast(&Boost::typeindex::type_id().type_info())); } return 0; } template static void* large_manager(operation op, basic_any& left, const basic_any* right, const Boost::typeindex::type_info* info) { switch (op) { case Destroy: ACTIV_BOOST_ASSERT(!left.empty()); delete static_cast(left.content.large_value); break; case Move: ACTIV_BOOST_ASSERT(left.empty()); ACTIV_BOOST_ASSERT(right); ACTIV_BOOST_ASSERT(!right->empty()); ACTIV_BOOST_ASSERT(right->type() == Boost::typeindex::type_id()); left.content.large_value = right->content.large_value; left.man = right->man; const_cast(right)->content.large_value = 0; const_cast(right)->man = 0; break; case Copy: ACTIV_BOOST_ASSERT(left.empty()); ACTIV_BOOST_ASSERT(right); ACTIV_BOOST_ASSERT(!right->empty()); ACTIV_BOOST_ASSERT(right->type() == Boost::typeindex::type_id()); left.content.large_value = new ValueType(*static_cast(right->content.large_value)); left.man = right->man; break; case AnyCast: ACTIV_BOOST_ASSERT(info); ACTIV_BOOST_ASSERT(!left.empty()); return Boost::typeindex::type_id() == *info ? static_cast::type *>(left.content.large_value) : 0; case UnsafeCast: ACTIV_BOOST_ASSERT(!left.empty()); return reinterpret_cast::type *>(left.content.large_value); case Typeinfo: return const_cast(static_cast(&Boost::typeindex::type_id().type_info())); } return 0; } template struct is_small_object : Boost::integral_constant::value <= OptimizeForAlignment && Boost::is_nothrow_move_constructible::value> {}; template static void create(basic_any& any, const ValueType& value, Boost::true_type) { typedef typename Boost::decay::type DecayedType; any.man = &small_manager; new (&any.content.small_value) ValueType(value); } template static void create(basic_any& any, const ValueType& value, Boost::false_type) { typedef typename Boost::decay::type DecayedType; any.man = &large_manager; any.content.large_value = new DecayedType(value); } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template static void create(basic_any& any, ValueType&& value, Boost::true_type) { typedef typename Boost::decay::type DecayedType; any.man = &small_manager; new (&any.content.small_value) DecayedType(static_cast(value)); } template static void create(basic_any& any, ValueType&& value, Boost::false_type) { typedef typename Boost::decay::type DecayedType; any.man = &large_manager; any.content.large_value = new DecayedType(static_cast(value)); } #endif public: // non-type template parameters accessors static ACTIV_BOOST_CONSTEXPR_OR_CONST std::size_t buffer_size = OptimizeForSize; static ACTIV_BOOST_CONSTEXPR_OR_CONST std::size_t buffer_align = OptimizeForAlignment; public: // structors ACTIV_BOOST_CONSTEXPR basic_any() ACTIV_BOOST_NOEXCEPT : man(0), content() { } template basic_any(const ValueType & value) : man(0), content() { ACTIV_BOOST_STATIC_ASSERT_MSG( !(Boost::is_same::value), "Boost::anys::basic_any shall not be constructed from Boost::any" ); ACTIV_BOOST_STATIC_ASSERT_MSG( !anys::detail::is_basic_any::value, "Boost::anys::basic_any shall not be constructed from Boost::anys::basic_any" ); create(*this, value, is_small_object()); } basic_any(const basic_any & other) : man(0), content() { if (other.man) { other.man(Copy, *this, &other, 0); } } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES // Move constructor basic_any(basic_any&& other) ACTIV_BOOST_NOEXCEPT : man(0), content() { if (other.man) { other.man(Move, *this, &other, 0); } } // Perfect forwarding of ValueType template basic_any(ValueType&& value , typename Boost::disable_if >::type* = 0 // disable if value has type `basic_any&` , typename Boost::disable_if >::type* = 0) // disable if value has type `const ValueType&&` : man(0), content() { typedef typename Boost::decay::type DecayedType; ACTIV_BOOST_STATIC_ASSERT_MSG( !(Boost::is_same::value), "Boost::anys::basic_any shall not be constructed from Boost::any" ); ACTIV_BOOST_STATIC_ASSERT_MSG( !anys::detail::is_basic_any::value, "Boost::anys::basic_any shall not be constructed from Boost::anys::basic_any" ); create(*this, static_cast(value), is_small_object()); } #endif ~basic_any() ACTIV_BOOST_NOEXCEPT { if (man) { man(Destroy, *this, 0, 0); } } public: // modifiers basic_any & swap(basic_any & rhs) ACTIV_BOOST_NOEXCEPT { if (this == &rhs) { return *this; } if (man && rhs.man) { basic_any tmp; rhs.man(Move, tmp, &rhs, 0); man(Move, rhs, this, 0); tmp.man(Move, *this, &tmp, 0); } else if (man) { man(Move, rhs, this, 0); } else if (rhs.man) { rhs.man(Move, *this, &rhs, 0); } return *this; } #ifdef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template basic_any & operator=(const ValueType & rhs) { ACTIV_BOOST_STATIC_ASSERT_MSG( !(Boost::is_same::value), "Boost::any shall not be assigned into Boost::anys::basic_any" ); ACTIV_BOOST_STATIC_ASSERT_MSG( !anys::detail::is_basic_any::value, "Boost::anys::basic_any shall not be assigned into Boost::anys::basic_any" ); basic_any(rhs).swap(*this); return *this; } basic_any & operator=(basic_any rhs) { rhs.swap(*this); return *this; } #else basic_any & operator=(const basic_any& rhs) { basic_any(rhs).swap(*this); return *this; } // move assignment basic_any & operator=(basic_any&& rhs) ACTIV_BOOST_NOEXCEPT { rhs.swap(*this); basic_any().swap(rhs); return *this; } // Perfect forwarding of ValueType template basic_any & operator=(ValueType&& rhs) { typedef typename Boost::decay::type DecayedType; ACTIV_BOOST_STATIC_ASSERT_MSG( !(Boost::is_same::value), "Boost::any shall not be assigned into Boost::anys::basic_any" ); ACTIV_BOOST_STATIC_ASSERT_MSG( (!anys::detail::is_basic_any::value || Boost::is_same::value), "Boost::anys::basic_any shall not be assigned into Boost::anys::basic_any" ); basic_any(static_cast(rhs)).swap(*this); return *this; } #endif public: // queries bool empty() const ACTIV_BOOST_NOEXCEPT { return !man; } void clear() ACTIV_BOOST_NOEXCEPT { basic_any().swap(*this); } const Boost::typeindex::type_info& type() const ACTIV_BOOST_NOEXCEPT { return man ? *static_cast(man(Typeinfo, const_cast(*this), 0, 0)) : Boost::typeindex::type_id().type_info(); } private: // representation template friend ValueType * any_cast(basic_any *) ACTIV_BOOST_NOEXCEPT; template friend ValueType * unsafe_any_cast(basic_any *) ACTIV_BOOST_NOEXCEPT; typedef void*(*manager)(operation op, basic_any& left, const basic_any* right, const Boost::typeindex::type_info* info); manager man; union content { void * large_value; typename Boost::aligned_storage::type small_value; } content; }; template void swap(basic_any& lhs, basic_any& rhs) ACTIV_BOOST_NOEXCEPT { lhs.swap(rhs); } template ValueType * any_cast(basic_any * operand) ACTIV_BOOST_NOEXCEPT { return operand->man ? static_cast::type *>(operand->man(basic_any::AnyCast, *operand, 0, &Boost::typeindex::type_id().type_info())) : 0; } template inline const ValueType * any_cast(const basic_any * operand) ACTIV_BOOST_NOEXCEPT { return any_cast(const_cast *>(operand)); } template ValueType any_cast(basic_any & operand) { typedef typename remove_reference::type nonref; nonref * result = any_cast(Boost::addressof(operand)); if(!result) Boost::throw_exception(bad_any_cast()); // Attempt to avoid construction of a temporary object in cases when // `ValueType` is not a reference. Example: // `static_cast(*result);` // which is equal to `std::string(*result);` typedef typename Boost::conditional< Boost::is_reference::value, ValueType, typename Boost::add_reference::type >::type ref_type; #ifdef ACTIV_BOOST_MSVC # pragma warning(push) # pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local! #endif return static_cast(*result); #ifdef ACTIV_BOOST_MSVC # pragma warning(pop) #endif } template inline ValueType any_cast(const basic_any & operand) { typedef typename remove_reference::type nonref; return any_cast(const_cast &>(operand)); } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template inline ValueType any_cast(basic_any&& operand) { ACTIV_BOOST_STATIC_ASSERT_MSG( Boost::is_rvalue_reference::value /*true if ValueType is rvalue or just a value*/ || Boost::is_const< typename Boost::remove_reference::type >::value, "Boost::any_cast shall not be used for getting nonconst references to temporary objects" ); return any_cast(operand); } #endif // Note: The "unsafe" versions of any_cast are not part of the // public interface and may be removed at any time. They are // required where we know what type is stored in the any and can't // use typeid() comparison, e.g., when our types may travel across // different shared libraries. template inline ValueType * unsafe_any_cast(basic_any * operand) ACTIV_BOOST_NOEXCEPT { return static_cast(operand->man(basic_any::UnsafeCast, *operand, 0, 0)); } template inline const ValueType * unsafe_any_cast(const basic_any * operand) ACTIV_BOOST_NOEXCEPT { return unsafe_any_cast(const_cast *>(operand)); } } // namespace anys using Boost::anys::any_cast; using Boost::anys::unsafe_any_cast; } // namespace Boost #endif // #ifndef ACTIV_BOOST_ANYS_BASIC_ANY_HPP_INCLUDED