fix doc example typo
[boost.git] / boost / any.hpp
blob47773dcabc030a0d6ef43495f8fc8ce21e34c407
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
10 // when: July 2001
11 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
13 #include <algorithm>
14 #include <typeinfo>
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 namespace boost
24 class any
26 public: // structors
28 any()
29 : content(0)
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)
44 ~any()
46 delete content;
49 public: // modifiers
51 any & swap(any & rhs)
53 std::swap(content, rhs.content);
54 return *this;
57 template<typename ValueType>
58 any & operator=(const ValueType & rhs)
60 any(rhs).swap(*this);
61 return *this;
64 any & operator=(any rhs)
66 rhs.swap(*this);
67 return *this;
70 public: // queries
72 bool empty() const
74 return !content;
77 const std::type_info & type() const
79 return content ? content->type() : typeid(void);
82 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
83 private: // types
84 #else
85 public: // types (public so any_cast can be non-friend)
86 #endif
88 class placeholder
90 public: // structors
92 virtual ~placeholder()
96 public: // queries
98 virtual const std::type_info & type() const = 0;
100 virtual placeholder * clone() const = 0;
104 template<typename ValueType>
105 class holder : public placeholder
107 public: // structors
109 holder(const ValueType & value)
110 : held(value)
114 public: // queries
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
128 ValueType held;
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 *);
144 #else
146 public: // representation (public so any_cast can be non-friend)
148 #endif
150 placeholder * content;
154 class bad_any_cast : public std::bad_cast
156 public:
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
169 : 0;
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);
191 #endif
193 nonref * result = any_cast<nonref>(&operand);
194 if(!result)
195 boost::throw_exception(bad_any_cast());
196 return *result;
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);
208 #endif
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)
237 #endif