// Copyright Antony Polukhin, 2020-2024. // // 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 /// \file boost/any/unique_any.hpp /// \brief \copybrief boost::anys::unique_any #include // for std::unique_ptr #include #include #include #include #include #include #include namespace boost { namespace anys { /// Helper type for providing emplacement type to the constructor. template struct in_place_type_t { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template constexpr in_place_type_t in_place_type{}; #endif /// \brief A class whose instances can hold instances of any /// type (including non-copyable and non-movable types). class unique_any { public: /// \post this->has_value() is false. constexpr unique_any() noexcept = default; /// Move constructor that moves content of /// `other` into new instance and leaves `other` empty. /// /// \post other->has_value() is false. /// \throws Nothing. unique_any(unique_any&& other) noexcept = default; /// Forwards `value`, so /// that the content of the new instance has type `std::decay_t` /// and value is the `value` before the forward. /// /// \throws std::bad_alloc or any exceptions arising from the move or /// copy constructor of the contained type. template unique_any(T&& value, typename std::enable_if::value>::type* = nullptr) : content(new holder< typename std::decay::type >(std::forward(value))) { static_assert( !boost::anys::detail::is_basic_any< typename std::decay::type >::value, "boost::anys::unique_any could not be constructed from boost::anys::basic_any." ); static_assert( !std::is_same::type >::value, "boost::anys::unique_any could not be copied, only moved." ); static_assert( !std::is_same::type >::value, "boost::anys::unique_any could be constructed from an rvalue of boost::any, " "not a lvalue." ); } /// Moves the content of `boost::any` into *this. /// /// \throws Nothing. /// \post `value.empty()` is true. template unique_any(BoostAny&& value, typename std::enable_if::value>::type* = nullptr) noexcept { content.reset(value.content); value.content = nullptr; } /// Inplace constructs `T` from forwarded `args...`, /// so that the content of `*this` is equivalent /// in type to `std::decay_t`. /// /// \throws std::bad_alloc or any exceptions arising from the move or /// copy constructor of the contained type. template explicit unique_any(in_place_type_t, Args&&... args) : content(new holder::type>(std::forward(args)...)) { } /// Inplace constructs `T` from `li` and forwarded `args...`, /// so that the initial content of `*this` is equivalent /// in type to `std::decay_t`. /// /// \throws std::bad_alloc or any exceptions arising from the move or /// copy constructor of the contained type. template explicit unique_any(in_place_type_t, std::initializer_list il, Args&&... args) : content(new holder::type>(il, std::forward(args)...)) { } /// Releases any and all resources used in management of instance. /// /// \throws Nothing. ~unique_any() noexcept = default; /// Moves content of `rhs` into /// current instance, discarding previous content, so that the /// new content is equivalent in both type and value to the /// content of rhs before move, or empty if `rhs.empty()`. /// /// \post `rhs->empty()` is true /// \throws Nothing. unique_any & operator=(unique_any&& rhs) noexcept = default; /// Forwards `rhs`, /// discarding previous content, so that the new content of is /// equivalent in both type and value to `rhs` before forward. /// /// \throws std::bad_alloc /// or any exceptions arising from the move or copy constructor of the /// contained type. Assignment satisfies the strong guarantee /// of exception safety. template unique_any & operator=(T&& rhs) { unique_any(std::forward(rhs)).swap(*this); return *this; } /// Inplace constructs `T` from forwarded `args...`, discarding previous /// content, so that the content of `*this` is equivalent /// in type to `std::decay_t`. /// /// \returns reference to the content of `*this`. /// \throws std::bad_alloc or any exceptions arising from the move or /// copy constructor of the contained type. template typename std::decay::type& emplace(Args&&... args) { auto* raw_ptr = new holder::type>(std::forward(args)...); content = std::unique_ptr(raw_ptr); return raw_ptr->held; } /// Inplace constructs `T` from `li` and forwarded `args...`, discarding /// previous content, so that the content of `*this` is equivalent /// in type to `std::decay_t`. /// /// \returns reference to the content of `*this`. /// \throws std::bad_alloc or any exceptions arising from the move or /// copy constructor of the contained type. template typename std::decay::type& emplace(std::initializer_list il, Args&&... args) { auto* raw_ptr = new holder::type>(il, std::forward(args)...); content = std::unique_ptr(raw_ptr); return raw_ptr->held; } /// \post this->has_value() is false. void reset() noexcept { content.reset(); } /// Exchange of the contents of `*this` and `rhs`. /// /// \returns `*this` /// \throws Nothing. void swap(unique_any& rhs) noexcept { content.swap(rhs.content); } /// \returns `true` if instance is not empty, otherwise `false`. /// \throws Nothing. bool has_value() const noexcept { return !!content; } /// \returns the `typeid` of the /// contained value if instance is non-empty, otherwise /// `typeid(void)`. /// /// Useful for querying against types known either at compile time or /// only at runtime. const boost::typeindex::type_info& type() const noexcept { return content ? content->type() : boost::typeindex::type_id().type_info(); } private: // types /// @cond template class holder final: public boost::anys::detail::placeholder { public: template holder(Args&&... args) : held(std::forward(args)...) { } template holder(std::initializer_list il, Args&&... args) : held(il, std::forward(args)...) { } const boost::typeindex::type_info& type() const noexcept override { return boost::typeindex::type_id().type_info(); } public: T held; }; private: // representation template friend T * unsafe_any_cast(unique_any *) noexcept; std::unique_ptr content; /// @endcond }; /// Exchange of the contents of `lhs` and `rhs`. /// \throws Nothing. inline void swap(unique_any & lhs, unique_any & rhs) noexcept { lhs.swap(rhs); } /// @cond // 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) noexcept { return std::addressof( static_cast&>(*operand->content).held ); } template inline const T * unsafe_any_cast(const unique_any * operand) noexcept { return anys::unsafe_any_cast(const_cast(operand)); } /// @endcond /// \returns Pointer to a `T` stored in `operand`, nullptr if /// `operand` does not contain specified `T`. template T * any_cast(unique_any * operand) noexcept { return operand && operand->type() == boost::typeindex::type_id() ? anys::unsafe_any_cast::type>(operand) : nullptr; } /// \returns Const pointer to a `T` stored in `operand`, nullptr if /// `operand` does not contain specified `T`. template inline const T * any_cast(const unique_any * operand) noexcept { return anys::any_cast(const_cast(operand)); } /// \returns `T` stored in `operand` /// \throws boost::bad_any_cast if `operand` does not contain specified `T`. template T any_cast(unique_any & operand) { typedef typename std::remove_reference::type nonref; nonref * result = anys::any_cast(std::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 typename std::conditional< std::is_reference::value, T, T& >::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 } /// \returns `T` stored in `operand` /// \throws boost::bad_any_cast if `operand` does not contain specified `T`. template inline T any_cast(const unique_any & operand) { typedef typename std::remove_reference::type nonref; return anys::any_cast(const_cast(operand)); } /// \returns `T` stored in `operand` /// \throws boost::bad_any_cast if `operand` does not contain specified `T`. template inline T any_cast(unique_any&& operand) { static_assert( std::is_rvalue_reference::value /*true if T is rvalue or just a value*/ || std::is_const< typename std::remove_reference::type >::value, "boost::any_cast shall not be used for getting nonconst references to temporary objects" ); return std::move(anys::any_cast(operand)); } } // namespace anys using boost::anys::any_cast; using boost::anys::unsafe_any_cast; } // namespace boost #endif // BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED