#ifndef BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED
#define BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED
#include <boost/any/detail/config.hpp>
#if !defined(BOOST_USE_MODULES) || defined(BOOST_ANY_INTERFACE_UNIT)
#ifndef BOOST_ANY_INTERFACE_UNIT
#include <boost/config.hpp>
#ifdef BOOST_HAS_PRAGMA_ONCE
# pragma once
#endif
#include <memory>
#include <utility>
#include <type_traits>
#include <boost/throw_exception.hpp>
#include <boost/type_index.hpp>
#endif
#include <boost/any/fwd.hpp>
#include <boost/any/bad_any_cast.hpp>
#include <boost/any/detail/placeholder.hpp>
namespace boost { namespace anys {
BOOST_ANY_BEGIN_MODULE_EXPORT
template <class T>
struct in_place_type_t
{
};
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
template <class T>
constexpr in_place_type_t<T> in_place_type{};
#endif
class unique_any {
public:
constexpr unique_any() noexcept = default;
unique_any(unique_any&& other) noexcept = default;
template<typename T>
unique_any(T&& value, typename std::enable_if<!std::is_same<T&&, boost::any&&>::value>::type* = nullptr)
: content(new holder< typename std::decay<T>::type >(std::forward<T>(value)))
{
static_assert(
!boost::anys::detail::is_basic_any< typename std::decay<T>::type >::value,
"boost::anys::unique_any could not be constructed from boost::anys::basic_any."
);
static_assert(
!std::is_same<unique_any, typename std::decay<T>::type >::value,
"boost::anys::unique_any could not be copied, only moved."
);
static_assert(
!std::is_same<boost::any, typename std::decay<T>::type >::value,
"boost::anys::unique_any could be constructed from an rvalue of boost::any, "
"not a lvalue."
);
}
template <class BoostAny>
unique_any(BoostAny&& value, typename std::enable_if<std::is_same<BoostAny&&, boost::any&&>::value>::type* = nullptr) noexcept
{
content.reset(value.content);
value.content = nullptr;
}
template<class T, class... Args>
explicit unique_any(in_place_type_t<T>, Args&&... args)
: content(new holder<typename std::decay<T>::type>(std::forward<Args>(args)...))
{
}
template <class T, class U, class... Args>
explicit unique_any(in_place_type_t<T>, std::initializer_list<U> il, Args&&... args)
: content(new holder<typename std::decay<T>::type>(il, std::forward<Args>(args)...))
{
}
~unique_any() noexcept = default;
unique_any & operator=(unique_any&& rhs) noexcept = default;
template <class T>
unique_any & operator=(T&& rhs)
{
unique_any(std::forward<T>(rhs)).swap(*this);
return *this;
}
template<class T, class... Args>
typename std::decay<T>::type& emplace(Args&&... args) {
auto* raw_ptr = new holder<typename std::decay<T>::type>(std::forward<Args>(args)...);
content = std::unique_ptr<boost::anys::detail::placeholder>(raw_ptr);
return raw_ptr->held;
}
template<class T, class U, class... Args>
typename std::decay<T>::type& emplace(std::initializer_list<U> il, Args&&... args) {
auto* raw_ptr = new holder<typename std::decay<T>::type>(il, std::forward<Args>(args)...);
content = std::unique_ptr<boost::anys::detail::placeholder>(raw_ptr);
return raw_ptr->held;
}
void reset() noexcept
{
content.reset();
}
void swap(unique_any& rhs) noexcept
{
content.swap(rhs.content);
}
bool has_value() const noexcept
{
return !!content;
}
const boost::typeindex::type_info& type() const noexcept
{
return content ? content->type() : boost::typeindex::type_id<void>().type_info();
}
private: template<typename T>
class holder final: public boost::anys::detail::placeholder
{
public:
template <class... Args>
holder(Args&&... args)
: held(std::forward<Args>(args)...)
{
}
template <class U, class... Args>
holder(std::initializer_list<U> il, Args&&... args)
: held(il, std::forward<Args>(args)...)
{
}
const boost::typeindex::type_info& type() const noexcept override
{
return boost::typeindex::type_id<T>().type_info();
}
public:
T held;
};
private: template<typename T>
friend T * unsafe_any_cast(unique_any *) noexcept;
std::unique_ptr<boost::anys::detail::placeholder> content;
};
inline void swap(unique_any & lhs, unique_any & rhs) noexcept
{
lhs.swap(rhs);
}
template<typename T>
inline T * unsafe_any_cast(unique_any * operand) noexcept
{
return std::addressof(
static_cast<unique_any::holder<T>&>(*operand->content).held
);
}
template<typename T>
inline const T * unsafe_any_cast(const unique_any * operand) noexcept
{
return anys::unsafe_any_cast<T>(const_cast<unique_any *>(operand));
}
template<typename T>
T * any_cast(unique_any * operand) noexcept
{
return operand && operand->type() == boost::typeindex::type_id<T>()
? anys::unsafe_any_cast<typename std::remove_cv<T>::type>(operand)
: nullptr;
}
template<typename T>
inline const T * any_cast(const unique_any * operand) noexcept
{
return anys::any_cast<T>(const_cast<unique_any *>(operand));
}
template<typename T>
T any_cast(unique_any & operand)
{
using nonref = typename std::remove_reference<T>::type;
nonref * result = anys::any_cast<nonref>(std::addressof(operand));
if(!result)
boost::throw_exception(bad_any_cast());
typedef typename std::conditional<
std::is_reference<T>::value,
T,
T&
>::type ref_type;
#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable: 4172)
#endif
return static_cast<ref_type>(*result);
#ifdef BOOST_MSVC
# pragma warning(pop)
#endif
}
template<typename T>
inline T any_cast(const unique_any & operand)
{
using nonref = typename std::remove_reference<T>::type;
return anys::any_cast<const nonref &>(const_cast<unique_any &>(operand));
}
template<typename T>
inline T any_cast(unique_any&& operand)
{
static_assert(
std::is_rvalue_reference<T&&>::value
|| std::is_const< typename std::remove_reference<T>::type >::value,
"boost::any_cast shall not be used for getting nonconst references to temporary objects"
);
return std::move(anys::any_cast<T&>(operand));
}
BOOST_ANY_END_MODULE_EXPORT
}
BOOST_ANY_BEGIN_MODULE_EXPORT
using boost::anys::any_cast;
using boost::anys::unsafe_any_cast;
BOOST_ANY_END_MODULE_EXPORT
}
#endif
#endif