123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #ifndef BOOST_ANY_INCLUDED
- #define BOOST_ANY_INCLUDED
- #include <algorithm>
- #include <typeinfo>
- #include "boost/config.hpp"
- #include <boost/type_traits/remove_reference.hpp>
- #include <boost/type_traits/is_reference.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/static_assert.hpp>
- namespace boost
- {
- class any
- {
- public:
- any()
- : content(0)
- {
- }
- template<typename ValueType>
- any(const ValueType & value)
- : content(new holder<ValueType>(value))
- {
- }
- any(const any & other)
- : content(other.content ? other.content->clone() : 0)
- {
- }
- ~any()
- {
- delete content;
- }
- public:
- any & swap(any & rhs)
- {
- std::swap(content, rhs.content);
- return *this;
- }
- template<typename ValueType>
- any & operator=(const ValueType & rhs)
- {
- any(rhs).swap(*this);
- return *this;
- }
- any & operator=(any rhs)
- {
- rhs.swap(*this);
- return *this;
- }
- public:
- bool empty() const
- {
- return !content;
- }
- const std::type_info & type() const
- {
- return content ? content->type() : typeid(void);
- }
- #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
- private:
- #else
- public:
- #endif
- class placeholder
- {
- public:
- virtual ~placeholder()
- {
- }
- public:
- virtual const std::type_info & type() const = 0;
- virtual placeholder * clone() const = 0;
- };
- template<typename ValueType>
- class holder : public placeholder
- {
- public:
- holder(const ValueType & value)
- : held(value)
- {
- }
- public:
- virtual const std::type_info & type() const
- {
- return typeid(ValueType);
- }
- virtual placeholder * clone() const
- {
- return new holder(held);
- }
- public:
- ValueType held;
- private:
- holder & operator=(const holder &);
- };
- #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
- private:
- template<typename ValueType>
- friend ValueType * any_cast(any *);
- template<typename ValueType>
- friend ValueType * unsafe_any_cast(any *);
- #else
- public:
- #endif
- placeholder * content;
- };
- class bad_any_cast : public std::bad_cast
- {
- public:
- virtual const char * what() const throw()
- {
- return "boost::bad_any_cast: "
- "failed conversion using boost::any_cast";
- }
- };
- template<typename ValueType>
- ValueType * any_cast(any * operand)
- {
- return operand && operand->type() == typeid(ValueType)
- ? &static_cast<any::holder<ValueType> *>(operand->content)->held
- : 0;
- }
- template<typename ValueType>
- inline const ValueType * any_cast(const any * operand)
- {
- return any_cast<ValueType>(const_cast<any *>(operand));
- }
- template<typename ValueType>
- ValueType any_cast(any & operand)
- {
- typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
- #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
-
-
-
-
-
- BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
- #endif
- nonref * result = any_cast<nonref>(&operand);
- if(!result)
- boost::throw_exception(bad_any_cast());
- return *result;
- }
- template<typename ValueType>
- inline ValueType any_cast(const any & operand)
- {
- typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
- #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
-
-
- BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
- #endif
- return any_cast<const nonref &>(const_cast<any &>(operand));
- }
-
-
-
-
-
- template<typename ValueType>
- inline ValueType * unsafe_any_cast(any * operand)
- {
- return &static_cast<any::holder<ValueType> *>(operand->content)->held;
- }
- template<typename ValueType>
- inline const ValueType * unsafe_any_cast(const any * operand)
- {
- return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
- }
- }
- #endif
|