1 #ifndef BOOST_ANY_INCLUDED
2 #define BOOST_ANY_INCLUDED
4 // what: variant type boost::any
5 // who: contributed by Kevlin Henney,
6 // with features contributed and bugs found by
7 // Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
9 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
14 #include "boost/config.hpp"
27 template<typename ValueType>
28 any(const ValueType & value)
29 : content(new holder<ValueType>(value))
33 any(const any & other)
34 : content(other.content ? other.content->clone() : 0)
47 std::swap(content, rhs.content);
51 template<typename ValueType>
52 any & operator=(const ValueType & rhs)
58 any & operator=(const any & rhs)
71 const std::type_info & type() const
73 return content ? content->type() : typeid(void);
76 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
79 public: // types (public so any_cast can be non-friend)
86 virtual ~placeholder()
92 virtual const std::type_info & type() const = 0;
94 virtual placeholder * clone() const = 0;
98 template<typename ValueType>
99 class holder : public placeholder
103 holder(const ValueType & value)
110 virtual const std::type_info & type() const
112 return typeid(ValueType);
115 virtual placeholder * clone() const
117 return new holder(held);
120 public: // representation
126 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
128 private: // representation
130 template<typename ValueType>
131 friend ValueType * any_cast(any *);
135 public: // representation (public so any_cast can be non-friend)
139 placeholder * content;
143 class bad_any_cast : public std::bad_cast
146 virtual const char * what() const throw()
148 return "boost::bad_any_cast: "
149 "failed conversion using boost::any_cast";
153 template<typename ValueType>
154 ValueType * any_cast(any * operand)
156 return operand && operand->type() == typeid(ValueType)
157 ? &static_cast<any::holder<ValueType> *>(operand->content)->held
161 template<typename ValueType>
162 const ValueType * any_cast(const any * operand)
164 return any_cast<ValueType>(const_cast<any *>(operand));
167 template<typename ValueType>
168 ValueType any_cast(const any & operand)
170 const ValueType * result = any_cast<ValueType>(&operand);
172 throw bad_any_cast();
178 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
180 // Permission to use, copy, modify, and distribute this software for any
181 // purpose is hereby granted without fee, provided that this copyright and
182 // permissions notice appear in all copies and derivatives.
184 // This software is provided "as is" without express or implied warranty.