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/type_traits/remove_reference.hpp>
18 #include <boost/type_traits/is_reference.hpp>
19 #include <boost/throw_exception.hpp>
20 #include <boost/static_assert.hpp>
22 // See boost/python/type_id.hpp
23 // TODO: add BOOST_TYPEID_COMPARE_BY_NAME to config.hpp
24 # if (defined(__GNUC__) && __GNUC__ >= 3) \
26 || ( defined(__sgi) && defined(__host_mips)) \
27 || (defined(__hpux) && defined(__HP_aCC)) \
28 || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
29 # define BOOST_AUX_ANY_TYPE_ID_NAME
44 template<typename ValueType>
45 any(const ValueType & value)
46 : content(new holder<ValueType>(value))
50 any(const any & other)
51 : content(other.content ? other.content->clone() : 0)
64 std::swap(content, rhs.content);
68 template<typename ValueType>
69 any & operator=(const ValueType & rhs)
75 any & operator=(any rhs)
88 const std::type_info & type() const
90 return content ? content->type() : typeid(void);
93 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
96 public: // types (public so any_cast can be non-friend)
103 virtual ~placeholder()
109 virtual const std::type_info & type() const = 0;
111 virtual placeholder * clone() const = 0;
115 template<typename ValueType>
116 class holder : public placeholder
120 holder(const ValueType & value)
127 virtual const std::type_info & type() const
129 return typeid(ValueType);
132 virtual placeholder * clone() const
134 return new holder(held);
137 public: // representation
141 private: // intentionally left unimplemented
142 holder & operator=(const holder &);
145 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
147 private: // representation
149 template<typename ValueType>
150 friend ValueType * any_cast(any *);
152 template<typename ValueType>
153 friend ValueType * unsafe_any_cast(any *);
157 public: // representation (public so any_cast can be non-friend)
161 placeholder * content;
165 class bad_any_cast : public std::bad_cast
168 virtual const char * what() const throw()
170 return "boost::bad_any_cast: "
171 "failed conversion using boost::any_cast";
175 template<typename ValueType>
176 ValueType * any_cast(any * operand)
179 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
180 std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0
182 operand->type() == typeid(ValueType)
184 ? &static_cast<any::holder<ValueType> *>(operand->content)->held
188 template<typename ValueType>
189 inline const ValueType * any_cast(const any * operand)
191 return any_cast<ValueType>(const_cast<any *>(operand));
194 template<typename ValueType>
195 ValueType any_cast(any & operand)
197 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
199 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
200 // If 'nonref' is still reference type, it means the user has not
201 // specialized 'remove_reference'.
203 // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
204 // to generate specialization of remove_reference for your class
205 // See type traits library documentation for details
206 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
209 nonref * result = any_cast<nonref>(&operand);
211 boost::throw_exception(bad_any_cast());
215 template<typename ValueType>
216 inline ValueType any_cast(const any & operand)
218 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
220 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
221 // The comment in the above version of 'any_cast' explains when this
222 // assert is fired and what to do.
223 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
226 return any_cast<const nonref &>(const_cast<any &>(operand));
229 // Note: The "unsafe" versions of any_cast are not part of the
230 // public interface and may be removed at any time. They are
231 // required where we know what type is stored in the any and can't
232 // use typeid() comparison, e.g., when our types may travel across
233 // different shared libraries.
234 template<typename ValueType>
235 inline ValueType * unsafe_any_cast(any * operand)
237 return &static_cast<any::holder<ValueType> *>(operand->content)->held;
240 template<typename ValueType>
241 inline const ValueType * unsafe_any_cast(const any * operand)
243 return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
247 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
249 // Distributed under the Boost Software License, Version 1.0. (See
250 // accompanying file LICENSE_1_0.txt or copy at
251 // http://www.boost.org/LICENSE_1_0.txt)