Chris@16
|
1 // See http://www.boost.org/libs/any for Documentation.
|
Chris@16
|
2
|
Chris@16
|
3 #ifndef BOOST_ANY_INCLUDED
|
Chris@16
|
4 #define BOOST_ANY_INCLUDED
|
Chris@16
|
5
|
Chris@101
|
6 #if defined(_MSC_VER)
|
Chris@16
|
7 # pragma once
|
Chris@16
|
8 #endif
|
Chris@16
|
9
|
Chris@16
|
10 // what: variant type boost::any
|
Chris@16
|
11 // who: contributed by Kevlin Henney,
|
Chris@16
|
12 // with features contributed and bugs found by
|
Chris@16
|
13 // Antony Polukhin, Ed Brey, Mark Rodgers,
|
Chris@16
|
14 // Peter Dimov, and James Curran
|
Chris@16
|
15 // when: July 2001, April 2013 - May 2013
|
Chris@16
|
16
|
Chris@16
|
17 #include <algorithm>
|
Chris@16
|
18
|
Chris@16
|
19 #include "boost/config.hpp"
|
Chris@101
|
20 #include <boost/type_index.hpp>
|
Chris@16
|
21 #include <boost/type_traits/remove_reference.hpp>
|
Chris@16
|
22 #include <boost/type_traits/decay.hpp>
|
Chris@101
|
23 #include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
24 #include <boost/type_traits/add_reference.hpp>
|
Chris@16
|
25 #include <boost/type_traits/is_reference.hpp>
|
Chris@16
|
26 #include <boost/type_traits/is_const.hpp>
|
Chris@16
|
27 #include <boost/throw_exception.hpp>
|
Chris@16
|
28 #include <boost/static_assert.hpp>
|
Chris@16
|
29 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
30 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
31 #include <boost/type_traits/is_const.hpp>
|
Chris@101
|
32 #include <boost/mpl/if.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 namespace boost
|
Chris@16
|
35 {
|
Chris@16
|
36 class any
|
Chris@16
|
37 {
|
Chris@16
|
38 public: // structors
|
Chris@16
|
39
|
Chris@16
|
40 any() BOOST_NOEXCEPT
|
Chris@16
|
41 : content(0)
|
Chris@16
|
42 {
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 template<typename ValueType>
|
Chris@16
|
46 any(const ValueType & value)
|
Chris@101
|
47 : content(new holder<
|
Chris@101
|
48 BOOST_DEDUCED_TYPENAME remove_cv<BOOST_DEDUCED_TYPENAME decay<const ValueType>::type>::type
|
Chris@101
|
49 >(value))
|
Chris@16
|
50 {
|
Chris@16
|
51 }
|
Chris@16
|
52
|
Chris@16
|
53 any(const any & other)
|
Chris@16
|
54 : content(other.content ? other.content->clone() : 0)
|
Chris@16
|
55 {
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
59 // Move constructor
|
Chris@16
|
60 any(any&& other) BOOST_NOEXCEPT
|
Chris@16
|
61 : content(other.content)
|
Chris@16
|
62 {
|
Chris@16
|
63 other.content = 0;
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 // Perfect forwarding of ValueType
|
Chris@16
|
67 template<typename ValueType>
|
Chris@16
|
68 any(ValueType&& value
|
Chris@16
|
69 , typename boost::disable_if<boost::is_same<any&, ValueType> >::type* = 0 // disable if value has type `any&`
|
Chris@16
|
70 , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
|
Chris@16
|
71 : content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
|
Chris@16
|
72 {
|
Chris@16
|
73 }
|
Chris@16
|
74 #endif
|
Chris@16
|
75
|
Chris@16
|
76 ~any() BOOST_NOEXCEPT
|
Chris@16
|
77 {
|
Chris@16
|
78 delete content;
|
Chris@16
|
79 }
|
Chris@16
|
80
|
Chris@16
|
81 public: // modifiers
|
Chris@16
|
82
|
Chris@16
|
83 any & swap(any & rhs) BOOST_NOEXCEPT
|
Chris@16
|
84 {
|
Chris@16
|
85 std::swap(content, rhs.content);
|
Chris@16
|
86 return *this;
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89
|
Chris@16
|
90 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
91 template<typename ValueType>
|
Chris@16
|
92 any & operator=(const ValueType & rhs)
|
Chris@16
|
93 {
|
Chris@16
|
94 any(rhs).swap(*this);
|
Chris@16
|
95 return *this;
|
Chris@16
|
96 }
|
Chris@16
|
97
|
Chris@16
|
98 any & operator=(any rhs)
|
Chris@16
|
99 {
|
Chris@16
|
100 any(rhs).swap(*this);
|
Chris@16
|
101 return *this;
|
Chris@16
|
102 }
|
Chris@16
|
103
|
Chris@16
|
104 #else
|
Chris@16
|
105 any & operator=(const any& rhs)
|
Chris@16
|
106 {
|
Chris@16
|
107 any(rhs).swap(*this);
|
Chris@16
|
108 return *this;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 // move assignement
|
Chris@16
|
112 any & operator=(any&& rhs) BOOST_NOEXCEPT
|
Chris@16
|
113 {
|
Chris@16
|
114 rhs.swap(*this);
|
Chris@16
|
115 any().swap(rhs);
|
Chris@16
|
116 return *this;
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@16
|
119 // Perfect forwarding of ValueType
|
Chris@16
|
120 template <class ValueType>
|
Chris@16
|
121 any & operator=(ValueType&& rhs)
|
Chris@16
|
122 {
|
Chris@16
|
123 any(static_cast<ValueType&&>(rhs)).swap(*this);
|
Chris@16
|
124 return *this;
|
Chris@16
|
125 }
|
Chris@16
|
126 #endif
|
Chris@16
|
127
|
Chris@16
|
128 public: // queries
|
Chris@16
|
129
|
Chris@16
|
130 bool empty() const BOOST_NOEXCEPT
|
Chris@16
|
131 {
|
Chris@16
|
132 return !content;
|
Chris@16
|
133 }
|
Chris@16
|
134
|
Chris@16
|
135 void clear() BOOST_NOEXCEPT
|
Chris@16
|
136 {
|
Chris@16
|
137 any().swap(*this);
|
Chris@16
|
138 }
|
Chris@16
|
139
|
Chris@101
|
140 const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
|
Chris@16
|
141 {
|
Chris@101
|
142 return content ? content->type() : boost::typeindex::type_id<void>().type_info();
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
Chris@16
|
146 private: // types
|
Chris@16
|
147 #else
|
Chris@16
|
148 public: // types (public so any_cast can be non-friend)
|
Chris@16
|
149 #endif
|
Chris@16
|
150
|
Chris@16
|
151 class placeholder
|
Chris@16
|
152 {
|
Chris@16
|
153 public: // structors
|
Chris@16
|
154
|
Chris@16
|
155 virtual ~placeholder()
|
Chris@16
|
156 {
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 public: // queries
|
Chris@16
|
160
|
Chris@101
|
161 virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0;
|
Chris@16
|
162
|
Chris@16
|
163 virtual placeholder * clone() const = 0;
|
Chris@16
|
164
|
Chris@16
|
165 };
|
Chris@16
|
166
|
Chris@16
|
167 template<typename ValueType>
|
Chris@16
|
168 class holder : public placeholder
|
Chris@16
|
169 {
|
Chris@16
|
170 public: // structors
|
Chris@16
|
171
|
Chris@16
|
172 holder(const ValueType & value)
|
Chris@16
|
173 : held(value)
|
Chris@16
|
174 {
|
Chris@16
|
175 }
|
Chris@16
|
176
|
Chris@16
|
177 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
178 holder(ValueType&& value)
|
Chris@16
|
179 : held(static_cast< ValueType&& >(value))
|
Chris@16
|
180 {
|
Chris@16
|
181 }
|
Chris@16
|
182 #endif
|
Chris@16
|
183 public: // queries
|
Chris@16
|
184
|
Chris@101
|
185 virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
|
Chris@16
|
186 {
|
Chris@101
|
187 return boost::typeindex::type_id<ValueType>().type_info();
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 virtual placeholder * clone() const
|
Chris@16
|
191 {
|
Chris@16
|
192 return new holder(held);
|
Chris@16
|
193 }
|
Chris@16
|
194
|
Chris@16
|
195 public: // representation
|
Chris@16
|
196
|
Chris@16
|
197 ValueType held;
|
Chris@16
|
198
|
Chris@16
|
199 private: // intentionally left unimplemented
|
Chris@16
|
200 holder & operator=(const holder &);
|
Chris@16
|
201 };
|
Chris@16
|
202
|
Chris@16
|
203 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
Chris@16
|
204
|
Chris@16
|
205 private: // representation
|
Chris@16
|
206
|
Chris@16
|
207 template<typename ValueType>
|
Chris@16
|
208 friend ValueType * any_cast(any *) BOOST_NOEXCEPT;
|
Chris@16
|
209
|
Chris@16
|
210 template<typename ValueType>
|
Chris@16
|
211 friend ValueType * unsafe_any_cast(any *) BOOST_NOEXCEPT;
|
Chris@16
|
212
|
Chris@16
|
213 #else
|
Chris@16
|
214
|
Chris@16
|
215 public: // representation (public so any_cast can be non-friend)
|
Chris@16
|
216
|
Chris@16
|
217 #endif
|
Chris@16
|
218
|
Chris@16
|
219 placeholder * content;
|
Chris@16
|
220
|
Chris@16
|
221 };
|
Chris@16
|
222
|
Chris@16
|
223 inline void swap(any & lhs, any & rhs) BOOST_NOEXCEPT
|
Chris@16
|
224 {
|
Chris@16
|
225 lhs.swap(rhs);
|
Chris@16
|
226 }
|
Chris@16
|
227
|
Chris@101
|
228 class BOOST_SYMBOL_VISIBLE bad_any_cast :
|
Chris@101
|
229 #ifndef BOOST_NO_RTTI
|
Chris@101
|
230 public std::bad_cast
|
Chris@101
|
231 #else
|
Chris@101
|
232 public std::exception
|
Chris@101
|
233 #endif
|
Chris@16
|
234 {
|
Chris@16
|
235 public:
|
Chris@16
|
236 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@16
|
237 {
|
Chris@16
|
238 return "boost::bad_any_cast: "
|
Chris@16
|
239 "failed conversion using boost::any_cast";
|
Chris@16
|
240 }
|
Chris@16
|
241 };
|
Chris@16
|
242
|
Chris@16
|
243 template<typename ValueType>
|
Chris@16
|
244 ValueType * any_cast(any * operand) BOOST_NOEXCEPT
|
Chris@16
|
245 {
|
Chris@101
|
246 return operand && operand->type() == boost::typeindex::type_id<ValueType>()
|
Chris@101
|
247 ? &static_cast<any::holder<BOOST_DEDUCED_TYPENAME remove_cv<ValueType>::type> *>(operand->content)->held
|
Chris@16
|
248 : 0;
|
Chris@16
|
249 }
|
Chris@16
|
250
|
Chris@16
|
251 template<typename ValueType>
|
Chris@16
|
252 inline const ValueType * any_cast(const any * operand) BOOST_NOEXCEPT
|
Chris@16
|
253 {
|
Chris@16
|
254 return any_cast<ValueType>(const_cast<any *>(operand));
|
Chris@16
|
255 }
|
Chris@16
|
256
|
Chris@16
|
257 template<typename ValueType>
|
Chris@16
|
258 ValueType any_cast(any & operand)
|
Chris@16
|
259 {
|
Chris@16
|
260 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
|
Chris@16
|
261
|
Chris@16
|
262
|
Chris@16
|
263 nonref * result = any_cast<nonref>(&operand);
|
Chris@16
|
264 if(!result)
|
Chris@16
|
265 boost::throw_exception(bad_any_cast());
|
Chris@16
|
266
|
Chris@16
|
267 // Attempt to avoid construction of a temporary object in cases when
|
Chris@16
|
268 // `ValueType` is not a reference. Example:
|
Chris@16
|
269 // `static_cast<std::string>(*result);`
|
Chris@16
|
270 // which is equal to `std::string(*result);`
|
Chris@16
|
271 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
|
Chris@16
|
272 boost::is_reference<ValueType>,
|
Chris@16
|
273 ValueType,
|
Chris@16
|
274 BOOST_DEDUCED_TYPENAME boost::add_reference<ValueType>::type
|
Chris@16
|
275 >::type ref_type;
|
Chris@16
|
276
|
Chris@16
|
277 return static_cast<ref_type>(*result);
|
Chris@16
|
278 }
|
Chris@16
|
279
|
Chris@16
|
280 template<typename ValueType>
|
Chris@16
|
281 inline ValueType any_cast(const any & operand)
|
Chris@16
|
282 {
|
Chris@16
|
283 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
|
Chris@16
|
284 return any_cast<const nonref &>(const_cast<any &>(operand));
|
Chris@16
|
285 }
|
Chris@16
|
286
|
Chris@16
|
287 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
288 template<typename ValueType>
|
Chris@101
|
289 inline ValueType any_cast(any&& operand)
|
Chris@16
|
290 {
|
Chris@16
|
291 BOOST_STATIC_ASSERT_MSG(
|
Chris@101
|
292 boost::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
|
Chris@16
|
293 || boost::is_const< typename boost::remove_reference<ValueType>::type >::value,
|
Chris@16
|
294 "boost::any_cast shall not be used for getting nonconst references to temporary objects"
|
Chris@16
|
295 );
|
Chris@101
|
296 return any_cast<ValueType>(operand);
|
Chris@16
|
297 }
|
Chris@16
|
298 #endif
|
Chris@16
|
299
|
Chris@16
|
300
|
Chris@16
|
301 // Note: The "unsafe" versions of any_cast are not part of the
|
Chris@16
|
302 // public interface and may be removed at any time. They are
|
Chris@16
|
303 // required where we know what type is stored in the any and can't
|
Chris@16
|
304 // use typeid() comparison, e.g., when our types may travel across
|
Chris@16
|
305 // different shared libraries.
|
Chris@16
|
306 template<typename ValueType>
|
Chris@16
|
307 inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
|
Chris@16
|
308 {
|
Chris@16
|
309 return &static_cast<any::holder<ValueType> *>(operand->content)->held;
|
Chris@16
|
310 }
|
Chris@16
|
311
|
Chris@16
|
312 template<typename ValueType>
|
Chris@16
|
313 inline const ValueType * unsafe_any_cast(const any * operand) BOOST_NOEXCEPT
|
Chris@16
|
314 {
|
Chris@16
|
315 return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
|
Chris@16
|
316 }
|
Chris@16
|
317 }
|
Chris@16
|
318
|
Chris@16
|
319 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
|
Chris@16
|
320 //
|
Chris@16
|
321 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
322 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
323 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
324
|
Chris@16
|
325 #endif
|