123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- #ifndef BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
- #define BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
- #include <boost/config.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- # pragma once
- #endif
- #include <boost/any/bad_any_cast.hpp>
- #include <boost/any/fwd.hpp>
- #include <boost/assert.hpp>
- #include <boost/type_index.hpp>
- #include <boost/throw_exception.hpp>
- #include <memory> // for std::addressof
- #include <type_traits>
- namespace boost {
- namespace anys {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
- class basic_any
- {
- static_assert(OptimizeForSize > 0 && OptimizeForAlignment > 0, "Size and Align shall be positive values");
- static_assert(OptimizeForSize >= OptimizeForAlignment, "Size shall non less than Align");
- static_assert((OptimizeForAlignment & (OptimizeForAlignment - 1)) == 0, "Align shall be a power of 2");
- static_assert(OptimizeForSize % OptimizeForAlignment == 0, "Size shall be multiple of alignment");
- private:
-
- enum operation
- {
- Destroy,
- Move,
- Copy,
- AnyCast,
- UnsafeCast,
- Typeinfo
- };
- template <typename ValueType>
- static void* small_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
- {
- switch (op)
- {
- case Destroy:
- BOOST_ASSERT(!left.empty());
- reinterpret_cast<ValueType*>(&left.content.small_value)->~ValueType();
- break;
- case Move: {
- BOOST_ASSERT(left.empty());
- BOOST_ASSERT(right);
- BOOST_ASSERT(!right->empty());
- BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
- ValueType* value = reinterpret_cast<ValueType*>(&const_cast<basic_any*>(right)->content.small_value);
- new (&left.content.small_value) ValueType(std::move(*value));
- left.man = right->man;
- reinterpret_cast<ValueType const*>(&right->content.small_value)->~ValueType();
- const_cast<basic_any*>(right)->man = 0;
- };
- break;
- case Copy:
- BOOST_ASSERT(left.empty());
- BOOST_ASSERT(right);
- BOOST_ASSERT(!right->empty());
- BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
- new (&left.content.small_value) ValueType(*reinterpret_cast<const ValueType*>(&right->content.small_value));
- left.man = right->man;
- break;
- case AnyCast:
- BOOST_ASSERT(info);
- BOOST_ASSERT(!left.empty());
- return boost::typeindex::type_id<ValueType>() == *info ?
- reinterpret_cast<typename std::remove_cv<ValueType>::type *>(&left.content.small_value) : 0;
- case UnsafeCast:
- BOOST_ASSERT(!left.empty());
- return reinterpret_cast<typename std::remove_cv<ValueType>::type *>(&left.content.small_value);
- case Typeinfo:
- return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
- }
- return 0;
- }
- template <typename ValueType>
- static void* large_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
- {
- switch (op)
- {
- case Destroy:
- BOOST_ASSERT(!left.empty());
- delete static_cast<ValueType*>(left.content.large_value);
- break;
- case Move:
- BOOST_ASSERT(left.empty());
- BOOST_ASSERT(right);
- BOOST_ASSERT(!right->empty());
- BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
- left.content.large_value = right->content.large_value;
- left.man = right->man;
- const_cast<basic_any*>(right)->content.large_value = 0;
- const_cast<basic_any*>(right)->man = 0;
- break;
- case Copy:
- BOOST_ASSERT(left.empty());
- BOOST_ASSERT(right);
- BOOST_ASSERT(!right->empty());
- BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
- left.content.large_value = new ValueType(*static_cast<const ValueType*>(right->content.large_value));
- left.man = right->man;
- break;
- case AnyCast:
- BOOST_ASSERT(info);
- BOOST_ASSERT(!left.empty());
- return boost::typeindex::type_id<ValueType>() == *info ?
- static_cast<typename std::remove_cv<ValueType>::type *>(left.content.large_value) : 0;
- case UnsafeCast:
- BOOST_ASSERT(!left.empty());
- return reinterpret_cast<typename std::remove_cv<ValueType>::type *>(left.content.large_value);
- case Typeinfo:
- return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
- }
- return 0;
- }
- template <typename ValueType>
- struct is_small_object : std::integral_constant<bool, sizeof(ValueType) <= OptimizeForSize &&
- alignof(ValueType) <= OptimizeForAlignment &&
- std::is_nothrow_move_constructible<ValueType>::value>
- {};
- template <typename ValueType>
- static void create(basic_any& any, const ValueType& value, std::true_type)
- {
- typedef typename std::decay<const ValueType>::type DecayedType;
- any.man = &small_manager<DecayedType>;
- new (&any.content.small_value) ValueType(value);
- }
- template <typename ValueType>
- static void create(basic_any& any, const ValueType& value, std::false_type)
- {
- typedef typename std::decay<const ValueType>::type DecayedType;
- any.man = &large_manager<DecayedType>;
- any.content.large_value = new DecayedType(value);
- }
- template <typename ValueType>
- static void create(basic_any& any, ValueType&& value, std::true_type)
- {
- typedef typename std::decay<const ValueType>::type DecayedType;
- any.man = &small_manager<DecayedType>;
- new (&any.content.small_value) DecayedType(std::forward<ValueType>(value));
- }
- template <typename ValueType>
- static void create(basic_any& any, ValueType&& value, std::false_type)
- {
- typedef typename std::decay<const ValueType>::type DecayedType;
- any.man = &large_manager<DecayedType>;
- any.content.large_value = new DecayedType(std::forward<ValueType>(value));
- }
-
- public:
- static constexpr std::size_t buffer_size = OptimizeForSize;
- static constexpr std::size_t buffer_align = OptimizeForAlignment;
- public:
-
- constexpr basic_any() noexcept
- : man(0), content()
- {
- }
-
-
-
-
-
-
-
-
-
-
- template<typename ValueType>
- basic_any(const ValueType & value)
- : man(0), content()
- {
- static_assert(
- !std::is_same<ValueType, boost::any>::value,
- "boost::anys::basic_any shall not be constructed from boost::any"
- );
- static_assert(
- !anys::detail::is_basic_any<ValueType>::value,
- "boost::anys::basic_any<A, B> shall not be constructed from boost::anys::basic_any<C, D>"
- );
- create(*this, value, is_small_object<ValueType>());
- }
-
-
-
-
-
-
-
-
- basic_any(const basic_any & other)
- : man(0), content()
- {
- if (other.man)
- {
- other.man(Copy, *this, &other, 0);
- }
- }
-
-
-
-
-
- basic_any(basic_any&& other) noexcept
- : man(0), content()
- {
- if (other.man)
- {
- other.man(Move, *this, &other, 0);
- }
- }
-
-
-
-
-
-
-
-
-
-
- template<typename ValueType>
- basic_any(ValueType&& value
- , typename std::enable_if<!std::is_same<basic_any&, ValueType>::value >::type* = 0
- , typename std::enable_if<!std::is_const<ValueType>::value >::type* = 0)
- : man(0), content()
- {
- typedef typename std::decay<ValueType>::type DecayedType;
- static_assert(
- !std::is_same<DecayedType, boost::any>::value,
- "boost::anys::basic_any shall not be constructed from boost::any"
- );
- static_assert(
- !anys::detail::is_basic_any<DecayedType>::value,
- "boost::anys::basic_any<A, B> shall not be constructed from boost::anys::basic_any<C, D>"
- );
- create(*this, static_cast<ValueType&&>(value), is_small_object<DecayedType>());
- }
-
-
-
- ~basic_any() noexcept
- {
- if (man)
- {
- man(Destroy, *this, 0, 0);
- }
- }
- public:
-
-
-
-
- basic_any & swap(basic_any & rhs) noexcept
- {
- if (this == &rhs)
- {
- return *this;
- }
- if (man && rhs.man)
- {
- basic_any tmp;
- rhs.man(Move, tmp, &rhs, 0);
- man(Move, rhs, this, 0);
- tmp.man(Move, *this, &tmp, 0);
- }
- else if (man)
- {
- man(Move, rhs, this, 0);
- }
- else if (rhs.man)
- {
- rhs.man(Move, *this, &rhs, 0);
- }
- return *this;
- }
-
-
-
-
-
-
-
-
-
- basic_any & operator=(const basic_any& rhs)
- {
- basic_any(rhs).swap(*this);
- return *this;
- }
-
-
-
-
-
-
-
-
- basic_any & operator=(basic_any&& rhs) noexcept
- {
- rhs.swap(*this);
- basic_any().swap(rhs);
- return *this;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <class ValueType>
- basic_any & operator=(ValueType&& rhs)
- {
- typedef typename std::decay<ValueType>::type DecayedType;
- static_assert(
- !std::is_same<DecayedType, boost::any>::value,
- "boost::any shall not be assigned into boost::anys::basic_any"
- );
- static_assert(
- !anys::detail::is_basic_any<DecayedType>::value || std::is_same<DecayedType, basic_any>::value,
- "boost::anys::basic_any<A, B> shall not be assigned into boost::anys::basic_any<C, D>"
- );
- basic_any(std::forward<ValueType>(rhs)).swap(*this);
- return *this;
- }
- public:
-
-
- bool empty() const noexcept
- {
- return !man;
- }
-
- void clear() noexcept
- {
- basic_any().swap(*this);
- }
-
-
-
-
-
-
- const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
- {
- return man
- ? *static_cast<const boost::typeindex::type_info*>(man(Typeinfo, const_cast<basic_any&>(*this), 0, 0))
- : boost::typeindex::type_id<void>().type_info();
- }
- private:
-
- template<typename ValueType, std::size_t Size, std::size_t Alignment>
- friend ValueType * any_cast(basic_any<Size, Alignment> *) noexcept;
- template<typename ValueType, std::size_t Size, std::size_t Alignment>
- friend ValueType * unsafe_any_cast(basic_any<Size, Alignment> *) noexcept;
- typedef void*(*manager)(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info);
- manager man;
- union content {
- void * large_value;
- alignas(OptimizeForAlignment) unsigned char small_value[OptimizeForSize];
- } content;
-
- };
-
-
- template<std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
- void swap(basic_any<OptimizeForSize, OptimizeForAlignment>& lhs, basic_any<OptimizeForSize, OptimizeForAlignment>& rhs) noexcept
- {
- lhs.swap(rhs);
- }
-
-
- template<typename ValueType, std::size_t Size, std::size_t Alignment>
- ValueType * any_cast(basic_any<Size, Alignment> * operand) noexcept
- {
- return operand->man ?
- static_cast<typename std::remove_cv<ValueType>::type *>(operand->man(basic_any<Size, Alignment>::AnyCast, *operand, 0, &boost::typeindex::type_id<ValueType>().type_info()))
- : 0;
- }
-
-
- template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
- inline const ValueType * any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> * operand) noexcept
- {
- return boost::anys::any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
- }
-
-
-
- template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
- ValueType any_cast(basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
- {
- typedef typename std::remove_reference<ValueType>::type nonref;
- nonref * result = boost::anys::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, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
- inline ValueType any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
- {
- typedef typename std::remove_reference<ValueType>::type nonref;
- return boost::anys::any_cast<const nonref &>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> &>(operand));
- }
-
-
-
- template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
- inline ValueType any_cast(basic_any<OptimizeForSize, OptimizeForAlignment>&& 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::anys::any_cast<ValueType>(operand);
- }
-
-
-
-
-
-
- template<typename ValueType, std::size_t OptimizedForSize, std::size_t OptimizeForAlignment>
- inline ValueType * unsafe_any_cast(basic_any<OptimizedForSize, OptimizeForAlignment> * operand) noexcept
- {
- return static_cast<ValueType*>(operand->man(basic_any<OptimizedForSize, OptimizeForAlignment>::UnsafeCast, *operand, 0, 0));
- }
- template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
- inline const ValueType * unsafe_any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> * operand) noexcept
- {
- return boost::anys::unsafe_any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
- }
-
- }
- using boost::anys::any_cast;
- using boost::anys::unsafe_any_cast;
- }
- #endif
|