1 // See http://www.boost.org/libs/any for Documentation.
3 #ifndef BOOST_ANY_INCLUDED
4 #define BOOST_ANY_INCLUDED
6 // what: variant type boost::any
7 // who: contributed by Kevlin Henney,
8 // with features contributed and bugs found by
9 // Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
11 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
16 #include "boost/config.hpp"
17 #include "boost/throw_exception.hpp"
30 template<typename ValueType>
31 any(const ValueType & value)
32 : content(new holder<ValueType>(value))
36 any(const any & other)
37 : content(other.content ? other.content->clone() : 0)
50 std::swap(content, rhs.content);
54 template<typename ValueType>
55 any & operator=(const ValueType & rhs)
61 any & operator=(const any & rhs)
74 const std::type_info & type() const
76 return content ? content->type() : typeid(void);
79 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
82 public: // types (public so any_cast can be non-friend)
89 virtual ~placeholder()
95 virtual const std::type_info & type() const = 0;
97 virtual placeholder * clone() const = 0;
101 template<typename ValueType>
102 class holder : public placeholder
106 holder(const ValueType & value)
113 virtual const std::type_info & type() const
115 return typeid(ValueType);
118 virtual placeholder * clone() const
120 return new holder(held);
123 public: // representation
129 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
131 private: // representation
133 template<typename ValueType>
134 friend ValueType * any_cast(any *);
138 public: // representation (public so any_cast can be non-friend)
142 placeholder * content;
146 class bad_any_cast : public std::bad_cast
149 virtual const char * what() const throw()
151 return "boost::bad_any_cast: "
152 "failed conversion using boost::any_cast";
156 template<typename ValueType>
157 ValueType * any_cast(any * operand)
159 return operand && operand->type() == typeid(ValueType)
160 ? &static_cast<any::holder<ValueType> *>(operand->content)->held
164 template<typename ValueType>
165 const ValueType * any_cast(const any * operand)
167 return any_cast<ValueType>(const_cast<any *>(operand));
170 template<typename ValueType>
171 ValueType any_cast(const any & operand)
173 const ValueType * result = any_cast<ValueType>(&operand);
175 throw_exception(bad_any_cast());
181 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
183 // Permission to use, copy, modify, and distribute this software for any
184 // purpose is hereby granted without fee, provided that this copyright and
185 // permissions notice appear in all copies and derivatives.
187 // This software is provided "as is" without express or implied warranty.