// See http://www.boost.org/libs/any for Documentation. #ifndef ACTIV_BOOST_ANY_INCLUDED #define ACTIV_BOOST_ANY_INCLUDED #include #ifdef ACTIV_BOOST_HAS_PRAGMA_ONCE # pragma once #endif // what: variant type Boost::any // who: contributed by Kevlin Henney, // with features contributed and bugs found by // Antony Polukhin, Ed Brey, Mark Rodgers, // Peter Dimov, and James Curran // when: July 2001, April 2013 - 2020 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Boost { class any { public: // structors ACTIV_BOOST_CONSTEXPR any() ACTIV_BOOST_NOEXCEPT : content(0) { } template any(const ValueType & value) : content(new holder< ACTIV_BOOST_DEDUCED_TYPENAME remove_cv::type>::type >(value)) { ACTIV_BOOST_STATIC_ASSERT_MSG( !anys::detail::is_basic_any::value, "Boost::any shall not be constructed from Boost::anys::basic_any" ); } any(const any & other) : content(other.content ? other.content->clone() : 0) { } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES // Move constructor any(any&& other) ACTIV_BOOST_NOEXCEPT : content(other.content) { other.content = 0; } // Perfect forwarding of ValueType template any(ValueType&& value , typename Boost::disable_if >::type* = 0 // disable if value has type `any&` , typename Boost::disable_if >::type* = 0) // disable if value has type `const ValueType&&` : content(new holder< typename decay::type >(static_cast(value))) { ACTIV_BOOST_STATIC_ASSERT_MSG( !anys::detail::is_basic_any::type>::value, "Boost::any shall not be constructed from Boost::anys::basic_any" ); } #endif ~any() ACTIV_BOOST_NOEXCEPT { delete content; } public: // modifiers any & swap(any & rhs) ACTIV_BOOST_NOEXCEPT { placeholder* tmp = content; content = rhs.content; rhs.content = tmp; return *this; } #ifdef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template any & operator=(const ValueType & rhs) { ACTIV_BOOST_STATIC_ASSERT_MSG( !anys::detail::is_basic_any::value, "Boost::anys::basic_any shall not be assigned into Boost::any" ); any(rhs).swap(*this); return *this; } any & operator=(any rhs) { rhs.swap(*this); return *this; } #else any & operator=(const any& rhs) { any(rhs).swap(*this); return *this; } // move assignment any & operator=(any&& rhs) ACTIV_BOOST_NOEXCEPT { rhs.swap(*this); any().swap(rhs); return *this; } // Perfect forwarding of ValueType template any & operator=(ValueType&& rhs) { ACTIV_BOOST_STATIC_ASSERT_MSG( !anys::detail::is_basic_any::type>::value, "Boost::anys::basic_any shall not be assigned into Boost::any" ); any(static_cast(rhs)).swap(*this); return *this; } #endif public: // queries bool empty() const ACTIV_BOOST_NOEXCEPT { return !content; } void clear() ACTIV_BOOST_NOEXCEPT { any().swap(*this); } const Boost::typeindex::type_info& type() const ACTIV_BOOST_NOEXCEPT { return content ? content->type() : Boost::typeindex::type_id().type_info(); } #ifndef ACTIV_BOOST_NO_MEMBER_TEMPLATE_FRIENDS private: // types #else public: // types (public so any_cast can be non-friend) #endif class ACTIV_BOOST_SYMBOL_VISIBLE placeholder { public: // structors virtual ~placeholder() { } public: // queries virtual const Boost::typeindex::type_info& type() const ACTIV_BOOST_NOEXCEPT = 0; virtual placeholder * clone() const = 0; }; template class holder #ifndef ACTIV_BOOST_NO_CXX11_FINAL final #endif : public placeholder { public: // structors holder(const ValueType & value) : held(value) { } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES holder(ValueType&& value) : held(static_cast< ValueType&& >(value)) { } #endif public: // queries const Boost::typeindex::type_info& type() const ACTIV_BOOST_NOEXCEPT ACTIV_BOOST_OVERRIDE { return Boost::typeindex::type_id().type_info(); } placeholder * clone() const ACTIV_BOOST_OVERRIDE { return new holder(held); } public: // representation ValueType held; private: // intentionally left unimplemented holder & operator=(const holder &); }; #ifndef ACTIV_BOOST_NO_MEMBER_TEMPLATE_FRIENDS private: // representation template friend ValueType * any_cast(any *) ACTIV_BOOST_NOEXCEPT; template friend ValueType * unsafe_any_cast(any *) ACTIV_BOOST_NOEXCEPT; #else public: // representation (public so any_cast can be non-friend) #endif placeholder * content; }; inline void swap(any & lhs, any & rhs) ACTIV_BOOST_NOEXCEPT { lhs.swap(rhs); } template ValueType * any_cast(any * operand) ACTIV_BOOST_NOEXCEPT { return operand && operand->type() == Boost::typeindex::type_id() ? Boost::addressof( static_cast::type> *>(operand->content)->held ) : 0; } template inline const ValueType * any_cast(const any * operand) ACTIV_BOOST_NOEXCEPT { return any_cast(const_cast(operand)); } template ValueType any_cast(any & operand) { typedef ACTIV_BOOST_DEDUCED_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 ACTIV_BOOST_DEDUCED_TYPENAME Boost::conditional< Boost::is_reference::value, ValueType, ACTIV_BOOST_DEDUCED_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 any & operand) { typedef ACTIV_BOOST_DEDUCED_TYPENAME remove_reference::type nonref; return any_cast(const_cast(operand)); } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template inline ValueType any_cast(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(any * operand) ACTIV_BOOST_NOEXCEPT { return Boost::addressof( static_cast *>(operand->content)->held ); } template inline const ValueType * unsafe_any_cast(const any * operand) ACTIV_BOOST_NOEXCEPT { return unsafe_any_cast(const_cast(operand)); } } // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved. // Copyright Antony Polukhin, 2013-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) #endif