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>
33 template<typename ValueType
>
34 any(const ValueType
& value
)
35 : content(new holder
<ValueType
>(value
))
39 any(const any
& other
)
40 : content(other
.content
? other
.content
->clone() : 0)
53 std::swap(content
, rhs
.content
);
57 template<typename ValueType
>
58 any
& operator=(const ValueType
& rhs
)
64 any
& operator=(any rhs
)
77 const std::type_info
& type() const
79 return content
? content
->type() : typeid(void);
82 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
85 public: // types (public so any_cast can be non-friend)
92 virtual ~placeholder()
98 virtual const std::type_info
& type() const = 0;
100 virtual placeholder
* clone() const = 0;
104 template<typename ValueType
>
105 class holder
: public placeholder
109 holder(const ValueType
& value
)
116 virtual const std::type_info
& type() const
118 return typeid(ValueType
);
121 virtual placeholder
* clone() const
123 return new holder(held
);
126 public: // representation
130 private: // intentionally left unimplemented
131 holder
& operator=(const holder
&);
134 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
136 private: // representation
138 template<typename ValueType
>
139 friend ValueType
* any_cast(any
*);
141 template<typename ValueType
>
142 friend ValueType
* unsafe_any_cast(any
*);
146 public: // representation (public so any_cast can be non-friend)
150 placeholder
* content
;
154 class bad_any_cast
: public std::bad_cast
157 virtual const char * what() const throw()
159 return "boost::bad_any_cast: "
160 "failed conversion using boost::any_cast";
164 template<typename ValueType
>
165 ValueType
* any_cast(any
* operand
)
167 return operand
&& operand
->type() == typeid(ValueType
)
168 ? &static_cast<any::holder
<ValueType
> *>(operand
->content
)->held
172 template<typename ValueType
>
173 inline const ValueType
* any_cast(const any
* operand
)
175 return any_cast
<ValueType
>(const_cast<any
*>(operand
));
178 template<typename ValueType
>
179 ValueType
any_cast(any
& operand
)
181 typedef BOOST_DEDUCED_TYPENAME remove_reference
<ValueType
>::type nonref
;
183 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
184 // If 'nonref' is still reference type, it means the user has not
185 // specialized 'remove_reference'.
187 // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
188 // to generate specialization of remove_reference for your class
189 // See type traits library documentation for details
190 BOOST_STATIC_ASSERT(!is_reference
<nonref
>::value
);
193 nonref
* result
= any_cast
<nonref
>(&operand
);
195 boost::throw_exception(bad_any_cast());
199 template<typename ValueType
>
200 inline ValueType
any_cast(const any
& operand
)
202 typedef BOOST_DEDUCED_TYPENAME remove_reference
<ValueType
>::type nonref
;
204 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
205 // The comment in the above version of 'any_cast' explains when this
206 // assert is fired and what to do.
207 BOOST_STATIC_ASSERT(!is_reference
<nonref
>::value
);
210 return any_cast
<const nonref
&>(const_cast<any
&>(operand
));
213 // Note: The "unsafe" versions of any_cast are not part of the
214 // public interface and may be removed at any time. They are
215 // required where we know what type is stored in the any and can't
216 // use typeid() comparison, e.g., when our types may travel across
217 // different shared libraries.
218 template<typename ValueType
>
219 inline ValueType
* unsafe_any_cast(any
* operand
)
221 return &static_cast<any::holder
<ValueType
> *>(operand
->content
)->held
;
224 template<typename ValueType
>
225 inline const ValueType
* unsafe_any_cast(const any
* operand
)
227 return unsafe_any_cast
<ValueType
>(const_cast<any
*>(operand
));
231 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
233 // Distributed under the Boost Software License, Version 1.0. (See
234 // accompanying file LICENSE_1_0.txt or copy at
235 // http://www.boost.org/LICENSE_1_0.txt)