1 // See http://www.boost.org/libs/any for Documentation.
3 #ifndef BOOST_ANY_INCLUDED
4 #define BOOST_ANY_INCLUDED
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
19 #include "boost/config.hpp"
20 #include <boost/type_index.hpp>
21 #include <boost/type_traits/remove_reference.hpp>
22 #include <boost/type_traits/decay.hpp>
23 #include <boost/type_traits/remove_cv.hpp>
24 #include <boost/type_traits/add_reference.hpp>
25 #include <boost/type_traits/is_reference.hpp>
26 #include <boost/type_traits/is_const.hpp>
27 #include <boost/throw_exception.hpp>
28 #include <boost/static_assert.hpp>
29 #include <boost/utility/enable_if.hpp>
30 #include <boost/type_traits/is_same.hpp>
31 #include <boost/type_traits/is_const.hpp>
32 #include <boost/mpl/if.hpp>
45 template<typename ValueType>
46 any(const ValueType & value)
48 BOOST_DEDUCED_TYPENAME remove_cv<BOOST_DEDUCED_TYPENAME decay<const ValueType>::type>::type
53 any(const any & other)
54 : content(other.content ? other.content->clone() : 0)
58 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
60 any(any&& other) BOOST_NOEXCEPT
61 : content(other.content)
66 // Perfect forwarding of ValueType
67 template<typename ValueType>
69 , typename boost::disable_if<boost::is_same<any&, ValueType> >::type* = 0 // disable if value has type `any&`
70 , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
71 : content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
83 any & swap(any & rhs) BOOST_NOEXCEPT
85 std::swap(content, rhs.content);
90 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
91 template<typename ValueType>
92 any & operator=(const ValueType & rhs)
98 any & operator=(any rhs)
100 any(rhs).swap(*this);
105 any & operator=(const any& rhs)
107 any(rhs).swap(*this);
112 any & operator=(any&& rhs) BOOST_NOEXCEPT
119 // Perfect forwarding of ValueType
120 template <class ValueType>
121 any & operator=(ValueType&& rhs)
123 any(static_cast<ValueType&&>(rhs)).swap(*this);
130 bool empty() const BOOST_NOEXCEPT
135 void clear() BOOST_NOEXCEPT
140 const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
142 return content ? content->type() : boost::typeindex::type_id<void>().type_info();
145 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
148 public: // types (public so any_cast can be non-friend)
155 virtual ~placeholder()
161 virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0;
163 virtual placeholder * clone() const = 0;
167 template<typename ValueType>
168 class holder : public placeholder
172 holder(const ValueType & value)
177 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
178 holder(ValueType&& value)
179 : held(static_cast< ValueType&& >(value))
185 virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
187 return boost::typeindex::type_id<ValueType>().type_info();
190 virtual placeholder * clone() const
192 return new holder(held);
195 public: // representation
199 private: // intentionally left unimplemented
200 holder & operator=(const holder &);
203 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
205 private: // representation
207 template<typename ValueType>
208 friend ValueType * any_cast(any *) BOOST_NOEXCEPT;
210 template<typename ValueType>
211 friend ValueType * unsafe_any_cast(any *) BOOST_NOEXCEPT;
215 public: // representation (public so any_cast can be non-friend)
219 placeholder * content;
223 inline void swap(any & lhs, any & rhs) BOOST_NOEXCEPT
228 class BOOST_SYMBOL_VISIBLE bad_any_cast :
229 #ifndef BOOST_NO_RTTI
232 public std::exception
236 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
238 return "boost::bad_any_cast: "
239 "failed conversion using boost::any_cast";
243 template<typename ValueType>
244 ValueType * any_cast(any * operand) BOOST_NOEXCEPT
246 return operand && operand->type() == boost::typeindex::type_id<ValueType>()
247 ? &static_cast<any::holder<BOOST_DEDUCED_TYPENAME remove_cv<ValueType>::type> *>(operand->content)->held
251 template<typename ValueType>
252 inline const ValueType * any_cast(const any * operand) BOOST_NOEXCEPT
254 return any_cast<ValueType>(const_cast<any *>(operand));
257 template<typename ValueType>
258 ValueType any_cast(any & operand)
260 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
263 nonref * result = any_cast<nonref>(&operand);
265 boost::throw_exception(bad_any_cast());
267 // Attempt to avoid construction of a temporary object in cases when
268 // `ValueType` is not a reference. Example:
269 // `static_cast<std::string>(*result);`
270 // which is equal to `std::string(*result);`
271 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
272 boost::is_reference<ValueType>,
274 BOOST_DEDUCED_TYPENAME boost::add_reference<ValueType>::type
277 return static_cast<ref_type>(*result);
280 template<typename ValueType>
281 inline ValueType any_cast(const any & operand)
283 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
284 return any_cast<const nonref &>(const_cast<any &>(operand));
287 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
288 template<typename ValueType>
289 inline ValueType any_cast(any&& operand)
291 BOOST_STATIC_ASSERT_MSG(
292 boost::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
293 || boost::is_const< typename boost::remove_reference<ValueType>::type >::value,
294 "boost::any_cast shall not be used for getting nonconst references to temporary objects"
296 return any_cast<ValueType>(operand);
301 // Note: The "unsafe" versions of any_cast are not part of the
302 // public interface and may be removed at any time. They are
303 // required where we know what type is stored in the any and can't
304 // use typeid() comparison, e.g., when our types may travel across
305 // different shared libraries.
306 template<typename ValueType>
307 inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
309 return &static_cast<any::holder<ValueType> *>(operand->content)->held;
312 template<typename ValueType>
313 inline const ValueType * unsafe_any_cast(const any * operand) BOOST_NOEXCEPT
315 return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
319 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
321 // Distributed under the Boost Software License, Version 1.0. (See
322 // accompanying file LICENSE_1_0.txt or copy at
323 // http://www.boost.org/LICENSE_1_0.txt)