#ifndef BOOST_ANY_INCLUDED
#define BOOST_ANY_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 <type_traits>
#include <boost/throw_exception.hpp>
#include <boost/type_index.hpp>
#endif
#include <boost/any/bad_any_cast.hpp>
#include <boost/any/fwd.hpp>
#include <boost/any/detail/placeholder.hpp>
namespace boost {
BOOST_ANY_BEGIN_MODULE_EXPORT
class any
{
public:
constexpr any() noexcept
: content(0)
{
}
template<typename ValueType>
any(const ValueType & value)
: content(new holder<
typename std::remove_cv<typename std::decay<const ValueType>::type>::type
>(value))
{
static_assert(
!anys::detail::is_basic_any<ValueType>::value,
"boost::any shall not be constructed from boost::anys::basic_any"
);
}
any(const any & other)
: content(other.content ? other.content->clone() : 0)
{
}
any(any&& other) noexcept
: content(other.content)
{
other.content = 0;
}
template<typename ValueType>
any(ValueType&& value
, typename std::enable_if<!std::is_same<any&, ValueType>::value >::type* = 0 , typename std::enable_if<!std::is_const<ValueType>::value >::type* = 0) : content(new holder< typename std::decay<ValueType>::type >(std::forward<ValueType>(value)))
{
static_assert(
!anys::detail::is_basic_any<typename std::decay<ValueType>::type>::value,
"boost::any shall not be constructed from boost::anys::basic_any"
);
}
~any() noexcept
{
delete content;
}
public:
any & swap(any & rhs) noexcept
{
placeholder* tmp = content;
content = rhs.content;
rhs.content = tmp;
return *this;
}
any & operator=(const any& rhs)
{
any(rhs).swap(*this);
return *this;
}
any & operator=(any&& rhs) noexcept
{
rhs.swap(*this);
any().swap(rhs);
return *this;
}
template <class ValueType>
any & operator=(ValueType&& rhs)
{
static_assert(
!anys::detail::is_basic_any<typename std::decay<ValueType>::type>::value,
"boost::anys::basic_any shall not be assigned into boost::any"
);
any(std::forward<ValueType>(rhs)).swap(*this);
return *this;
}
public:
bool empty() const noexcept
{
return !content;
}
void clear() noexcept
{
any().swap(*this);
}
const boost::typeindex::type_info& type() const noexcept
{
return content ? content->type() : boost::typeindex::type_id<void>().type_info();
}
private: class BOOST_SYMBOL_VISIBLE placeholder: public boost::anys::detail::placeholder
{
public:
virtual placeholder * clone() const = 0;
};
template<typename ValueType>
class holder final
: public placeholder
{
public:
holder(const ValueType & value)
: held(value)
{
}
holder(ValueType&& value)
: held(static_cast< ValueType&& >(value))
{
}
public:
const boost::typeindex::type_info& type() const noexcept override
{
return boost::typeindex::type_id<ValueType>().type_info();
}
placeholder * clone() const BOOST_OVERRIDE
{
return new holder(held);
}
public:
ValueType held;
private: holder & operator=(const holder &);
};
private: template<typename ValueType>
friend ValueType * unsafe_any_cast(any *) noexcept;
friend class boost::anys::unique_any;
placeholder * content;
};
inline void swap(any & lhs, any & rhs) noexcept
{
lhs.swap(rhs);
}
template<typename ValueType>
inline ValueType * unsafe_any_cast(any * operand) noexcept
{
return std::addressof(
static_cast<any::holder<ValueType> *>(operand->content)->held
);
}
template<typename ValueType>
inline const ValueType * unsafe_any_cast(const any * operand) noexcept
{
return boost::unsafe_any_cast<ValueType>(const_cast<any *>(operand));
}
template<typename ValueType>
ValueType * any_cast(any * operand) noexcept
{
return operand && operand->type() == boost::typeindex::type_id<ValueType>()
? boost::unsafe_any_cast<typename std::remove_cv<ValueType>::type>(operand)
: 0;
}
template<typename ValueType>
inline const ValueType * any_cast(const any * operand) noexcept
{
return boost::any_cast<ValueType>(const_cast<any *>(operand));
}
template<typename ValueType>
ValueType any_cast(any & operand)
{
using nonref = typename std::remove_reference<ValueType>::type;
nonref * result = boost::any_cast<nonref>(std::addressof(operand));
if(!result)
boost::throw_exception(bad_any_cast());
typedef typename std::conditional<
std::is_reference<ValueType>::value,
ValueType,
typename std::add_lvalue_reference<ValueType>::type
>::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 ValueType>
inline ValueType any_cast(const any & operand)
{
using nonref = typename std::remove_reference<ValueType>::type;
return boost::any_cast<const nonref &>(const_cast<any &>(operand));
}
template<typename ValueType>
inline ValueType any_cast(any&& operand)
{
static_assert(
std::is_rvalue_reference<ValueType&&>::value
|| std::is_const< typename std::remove_reference<ValueType>::type >::value,
"boost::any_cast shall not be used for getting nonconst references to temporary objects"
);
return boost::any_cast<ValueType>(operand);
}
BOOST_ANY_END_MODULE_EXPORT
}
#endif
#endif