1 // See http://www.boost.org/libs/any for Documentation.
3 #ifndef BOOST_ANY_INCLUDED
4 #define BOOST_ANY_INCLUDED
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 // what: variant type boost::any
11 // who: contributed by Kevlin Henney,
12 // with features contributed and bugs found by
13 // Antony Polukhin, Ed Brey, Mark Rodgers,
14 // Peter Dimov, and James Curran
15 // when: July 2001, April 2013 - May 2013
20 #include "boost/config.hpp"
21 #include <boost/type_traits/remove_reference.hpp>
22 #include <boost/type_traits/decay.hpp>
23 #include <boost/type_traits/add_reference.hpp>
24 #include <boost/type_traits/is_reference.hpp>
25 #include <boost/type_traits/is_const.hpp>
26 #include <boost/throw_exception.hpp>
27 #include <boost/static_assert.hpp>
28 #include <boost/utility/enable_if.hpp>
29 #include <boost/type_traits/is_same.hpp>
30 #include <boost/type_traits/is_const.hpp>
32 // See boost/python/type_id.hpp
33 // TODO: add BOOST_TYPEID_COMPARE_BY_NAME to config.hpp
34 # if (defined(__GNUC__) && __GNUC__ >= 3) \
36 || ( defined(__sgi) && defined(__host_mips)) \
37 || (defined(__hpux) && defined(__HP_aCC)) \
38 || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
39 # define BOOST_AUX_ANY_TYPE_ID_NAME
45 #pragma warning(disable: 4172) // Mistakenly warns: returning address of local variable or temporary
59 template<typename ValueType>
60 any(const ValueType & value)
61 : content(new holder<BOOST_DEDUCED_TYPENAME decay<const ValueType>::type>(value))
65 any(const any & other)
66 : content(other.content ? other.content->clone() : 0)
70 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
72 any(any&& other) BOOST_NOEXCEPT
73 : content(other.content)
78 // Perfect forwarding of ValueType
79 template<typename ValueType>
81 , typename boost::disable_if<boost::is_same<any&, ValueType> >::type* = 0 // disable if value has type `any&`
82 , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
83 : content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
95 any & swap(any & rhs) BOOST_NOEXCEPT
97 std::swap(content, rhs.content);
102 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
103 template<typename ValueType>
104 any & operator=(const ValueType & rhs)
106 any(rhs).swap(*this);
110 any & operator=(any rhs)
112 any(rhs).swap(*this);
117 any & operator=(const any& rhs)
119 any(rhs).swap(*this);
124 any & operator=(any&& rhs) BOOST_NOEXCEPT
131 // Perfect forwarding of ValueType
132 template <class ValueType>
133 any & operator=(ValueType&& rhs)
135 any(static_cast<ValueType&&>(rhs)).swap(*this);
142 bool empty() const BOOST_NOEXCEPT
147 void clear() BOOST_NOEXCEPT
152 const std::type_info & type() const BOOST_NOEXCEPT
154 return content ? content->type() : typeid(void);
157 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
160 public: // types (public so any_cast can be non-friend)
167 virtual ~placeholder()
173 virtual const std::type_info & type() const BOOST_NOEXCEPT = 0;
175 virtual placeholder * clone() const = 0;
179 template<typename ValueType>
180 class holder : public placeholder
184 holder(const ValueType & value)
189 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
190 holder(ValueType&& value)
191 : held(static_cast< ValueType&& >(value))
197 virtual const std::type_info & type() const BOOST_NOEXCEPT
199 return typeid(ValueType);
202 virtual placeholder * clone() const
204 return new holder(held);
207 public: // representation
211 private: // intentionally left unimplemented
212 holder & operator=(const holder &);
215 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
217 private: // representation
219 template<typename ValueType>
220 friend ValueType * any_cast(any *) BOOST_NOEXCEPT;
222 template<typename ValueType>
223 friend ValueType * unsafe_any_cast(any *) BOOST_NOEXCEPT;
227 public: // representation (public so any_cast can be non-friend)
231 placeholder * content;
235 inline void swap(any & lhs, any & rhs) BOOST_NOEXCEPT
240 class BOOST_SYMBOL_VISIBLE bad_any_cast : public std::bad_cast
243 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
245 return "boost::bad_any_cast: "
246 "failed conversion using boost::any_cast";
250 template<typename ValueType>
251 ValueType * any_cast(any * operand) BOOST_NOEXCEPT
254 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
255 std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0
257 operand->type() == typeid(ValueType)
259 ? &static_cast<any::holder<ValueType> *>(operand->content)->held
263 template<typename ValueType>
264 inline const ValueType * any_cast(const any * operand) BOOST_NOEXCEPT
266 return any_cast<ValueType>(const_cast<any *>(operand));
269 template<typename ValueType>
270 ValueType any_cast(any & operand)
272 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
274 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
275 // If 'nonref' is still reference type, it means the user has not
276 // specialized 'remove_reference'.
278 // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
279 // to generate specialization of remove_reference for your class
280 // See type traits library documentation for details
281 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
284 nonref * result = any_cast<nonref>(&operand);
286 boost::throw_exception(bad_any_cast());
288 // Attempt to avoid construction of a temporary object in cases when
289 // `ValueType` is not a reference. Example:
290 // `static_cast<std::string>(*result);`
291 // which is equal to `std::string(*result);`
292 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
293 boost::is_reference<ValueType>,
295 BOOST_DEDUCED_TYPENAME boost::add_reference<ValueType>::type
298 return static_cast<ref_type>(*result);
301 template<typename ValueType>
302 inline ValueType any_cast(const any & operand)
304 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
306 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
307 // The comment in the above version of 'any_cast' explains when this
308 // assert is fired and what to do.
309 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
312 return any_cast<const nonref &>(const_cast<any &>(operand));
315 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
316 template<typename ValueType>
317 inline ValueType&& any_cast(any&& operand)
319 BOOST_STATIC_ASSERT_MSG(
320 boost::is_rvalue_reference<ValueType&&>::value
321 || boost::is_const< typename boost::remove_reference<ValueType>::type >::value,
322 "boost::any_cast shall not be used for getting nonconst references to temporary objects"
324 return any_cast<ValueType&&>(operand);
329 // Note: The "unsafe" versions of any_cast are not part of the
330 // public interface and may be removed at any time. They are
331 // required where we know what type is stored in the any and can't
332 // use typeid() comparison, e.g., when our types may travel across
333 // different shared libraries.
334 template<typename ValueType>
335 inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
337 return &static_cast<any::holder<ValueType> *>(operand->content)->held;
340 template<typename ValueType>
341 inline const ValueType * unsafe_any_cast(const any * operand) BOOST_NOEXCEPT
343 return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
347 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
349 // Distributed under the Boost Software License, Version 1.0. (See
350 // accompanying file LICENSE_1_0.txt or copy at
351 // http://www.boost.org/LICENSE_1_0.txt)
353 #if defined(_MSC_VER)