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