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"
29 template<typename ValueType>
30 any(const ValueType & value)
31 : content(new holder<ValueType>(value))
35 any(const any & other)
36 : content(other.content ? other.content->clone() : 0)
49 std::swap(content, rhs.content);
53 template<typename ValueType>
54 any & operator=(const ValueType & rhs)
60 any & operator=(const any & rhs)
73 const std::type_info & type() const
75 return content ? content->type() : typeid(void);
78 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
81 public: // types (public so any_cast can be non-friend)
88 virtual ~placeholder()
94 virtual const std::type_info & type() const = 0;
96 virtual placeholder * clone() const = 0;
100 template<typename ValueType>
101 class holder : public placeholder
105 holder(const ValueType & value)
112 virtual const std::type_info & type() const
114 return typeid(ValueType);
117 virtual placeholder * clone() const
119 return new holder(held);
122 public: // representation
128 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
130 private: // representation
132 template<typename ValueType>
133 friend ValueType * any_cast(any *);
137 public: // representation (public so any_cast can be non-friend)
141 placeholder * content;
145 class bad_any_cast : public std::bad_cast
148 virtual const char * what() const throw()
150 return "boost::bad_any_cast: "
151 "failed conversion using boost::any_cast";
155 template<typename ValueType>
156 ValueType * any_cast(any * operand)
158 return operand && operand->type() == typeid(ValueType)
159 ? &static_cast<any::holder<ValueType> *>(operand->content)->held
163 template<typename ValueType>
164 const ValueType * any_cast(const any * operand)
166 return any_cast<ValueType>(const_cast<any *>(operand));
169 template<typename ValueType>
170 ValueType any_cast(const any & operand)
172 const ValueType * result = any_cast<ValueType>(&operand);
174 throw bad_any_cast();
180 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
182 // Permission to use, copy, modify, and distribute this software for any
183 // purpose is hereby granted without fee, provided that this copyright and
184 // permissions notice appear in all copies and derivatives.
186 // This software is provided "as is" without express or implied warranty.