// Copyright Antony Polukhin, 2020-2022. // // 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) // See http://www.boost.org/libs/any for Documentation. #ifndef BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED #define BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED #include #ifdef BOOST_HAS_PRAGMA_ONCE # pragma once #endif #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES #error Header requires C++11 compatible compiler with move semantics #endif #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS #error Header requires C++11 compatible compiler with defaulted functions #endif #ifdef BOOST_NO_CXX11_SMART_PTR #error Header requires C++11 compatible standard library with std::unique_ptr #endif #include #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST #include #endif #include #include #include #include namespace boost { namespace anys { template struct in_place_type_t { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template constexpr in_place_type_t in_place_type{}; #endif class unique_any { public: BOOST_CONSTEXPR unique_any() BOOST_NOEXCEPT = default; unique_any(unique_any&& other) BOOST_NOEXCEPT = default; // Perfect forwarding of T template unique_any(T&& value , typename boost::disable_if >::type* = 0 // disable if value has type `unique_any&` , typename boost::disable_if >::type* = 0) // disable if value has type `const T&&` : content(new holder< typename boost::decay::type >(std::forward(value))) { } template explicit unique_any(in_place_type_t, Args&&... args) : content(new holder::type>(std::forward(args)...)) { } #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST template explicit unique_any(in_place_type_t, std::initializer_list il, Args&&... args) : content(new holder::type>(il, std::forward(args)...)) { } #endif ~unique_any() BOOST_NOEXCEPT = default; unique_any & operator=(unique_any&& rhs) BOOST_NOEXCEPT = default; template unique_any & operator=(T&& rhs) { unique_any(std::forward(rhs)).swap(*this); return *this; } template typename boost::decay::type& emplace(Args&&... args) { content = std::unique_ptr( new holder::type>(std::forward(args)...) ); } #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST template typename boost::decay::type& emplace(initializer_list il, Args&&... args) { content = std::unique_ptr( new holder::type>(il, std::forward(args)...) ); } #endif void reset() BOOST_NOEXCEPT { content.reset(); } unique_any& swap(unique_any& rhs) BOOST_NOEXCEPT { content.swap(rhs.content); return *this; } bool has_value() const BOOST_NOEXCEPT { return !content; } const boost::typeindex::type_info& type() const BOOST_NOEXCEPT { return content ? content->type() : boost::typeindex::type_id().type_info(); } private: // types class BOOST_SYMBOL_VISIBLE placeholder { virtual ~placeholder() { } virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0; }; template class holder BOOST_FINAL: public placeholder { public: // structors template holder(Args&&... args) : held(std::forward(args)...) { } #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST template holder(std::initializer_list il, Args&&... args) : held(il, std::forward(args)...) { } #endif const boost::typeindex::type_info& type() const BOOST_NOEXCEPT BOOST_OVERRIDE { return boost::typeindex::type_id().type_info(); } public: // representation T held; }; private: // representation template friend T * any_cast(unique_any *) BOOST_NOEXCEPT; template friend T * unsafe_any_cast(unique_any *) BOOST_NOEXCEPT; std::unique_ptr content; }; inline void swap(unique_any & lhs, unique_any & rhs) BOOST_NOEXCEPT { lhs.swap(rhs); } template T * any_cast(unique_any * operand) BOOST_NOEXCEPT { return operand && operand->type() == boost::typeindex::type_id() ? boost::addressof( static_cast::type>&>(*operand->content).held ) : 0; } template inline const T * any_cast(const unique_any * operand) BOOST_NOEXCEPT { return any_cast(const_cast(operand)); } template T any_cast(unique_any & operand) { typedef 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 // `T` is not a reference. Example: // `static_cast(*result);` // which is equal to `std::string(*result);` typedef BOOST_DEDUCED_TYPENAME boost::conditional< boost::is_reference::value, T, BOOST_DEDUCED_TYPENAME boost::add_reference::type >::type ref_type; #ifdef 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 BOOST_MSVC # pragma warning(pop) #endif } template inline T any_cast(const unique_any & operand) { typedef BOOST_DEDUCED_TYPENAME remove_reference::type nonref; return any_cast(const_cast(operand)); } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES template inline T any_cast(unique_any&& operand) { BOOST_STATIC_ASSERT_MSG( boost::is_rvalue_reference::value /*true if T 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 T * unsafe_any_cast(unique_any * operand) BOOST_NOEXCEPT { return boost::addressof( static_cast&>(*operand->content)->held ); } template inline const T * unsafe_any_cast(const unique_any * operand) 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 // BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED