blob: 2419c6da28e3b4d236bbf32821048e62890d97b6 [file] [log] [blame]
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -07001// See http://www.boost.org/libs/any for Documentation.
2
3#ifndef BOOST_ANY_INCLUDED
4#define BOOST_ANY_INCLUDED
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10// what: variant type ndnboost::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, Aplril 2013
16
17#include <algorithm>
18#include <typeinfo>
19
20#include "ndnboost/config.hpp"
21#include <ndnboost/type_traits/remove_reference.hpp>
22#include <ndnboost/type_traits/is_reference.hpp>
23#include <ndnboost/throw_exception.hpp>
24#include <ndnboost/static_assert.hpp>
25#include <ndnboost/utility/enable_if.hpp>
26#include <ndnboost/type_traits/is_same.hpp>
27
28// See boost/python/type_id.hpp
29// TODO: add BOOST_TYPEID_COMPARE_BY_NAME to config.hpp
30# if (defined(__GNUC__) && __GNUC__ >= 3) \
31 || defined(_AIX) \
32 || ( defined(__sgi) && defined(__host_mips)) \
33 || (defined(__hpux) && defined(__HP_aCC)) \
34 || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
35# define BOOST_AUX_ANY_TYPE_ID_NAME
36#include <cstring>
37# endif
38
39namespace ndnboost
40{
41 class any
42 {
43 public: // structors
44
45 any() BOOST_NOEXCEPT
46 : content(0)
47 {
48 }
49
50 template<typename ValueType>
51 any(const ValueType & value)
52 : content(new holder<ValueType>(value))
53 {
54 }
55
56 any(const any & other)
57 : content(other.content ? other.content->clone() : 0)
58 {
59 }
60
61#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
62 // Move constructor
63 any(any&& other) BOOST_NOEXCEPT
64 : content(other.content)
65 {
66 other.content = 0;
67 }
68
69 // Perfect forwarding of ValueType
70 template<typename ValueType>
71 any(ValueType&& value, typename ndnboost::disable_if<ndnboost::is_same<any&, ValueType> >::type* = 0)
72 : content(new holder< typename remove_reference<ValueType>::type >(static_cast<ValueType&&>(value)))
73 {
74 }
75#endif
76
77 ~any() BOOST_NOEXCEPT
78 {
79 delete content;
80 }
81
82 public: // modifiers
83
84 any & swap(any & rhs) BOOST_NOEXCEPT
85 {
86 std::swap(content, rhs.content);
87 return *this;
88 }
89
90
91#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
92 template<typename ValueType>
93 any & operator=(const ValueType & rhs)
94 {
95 any(rhs).swap(*this);
96 return *this;
97 }
98
99 any & operator=(any rhs)
100 {
101 any(rhs).swap(*this);
102 return *this;
103 }
104
105#else
106 any & operator=(const any& rhs)
107 {
108 any(rhs).swap(*this);
109 return *this;
110 }
111
112 // move assignement
113 any & operator=(any&& rhs) BOOST_NOEXCEPT
114 {
115 rhs.swap(*this);
116 any().swap(rhs);
117 return *this;
118 }
119
120 // Perfect forwarding of ValueType
121 template <class ValueType>
122 any & operator=(ValueType&& rhs)
123 {
124 any(static_cast<ValueType&&>(rhs)).swap(*this);
125 return *this;
126 }
127#endif
128
129 public: // queries
130
131 bool empty() const BOOST_NOEXCEPT
132 {
133 return !content;
134 }
135
136 const std::type_info & type() const
137 {
138 return content ? content->type() : typeid(void);
139 }
140
141#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
142 private: // types
143#else
144 public: // types (public so any_cast can be non-friend)
145#endif
146
147 class placeholder
148 {
149 public: // structors
150
151 virtual ~placeholder()
152 {
153 }
154
155 public: // queries
156
157 virtual const std::type_info & type() const = 0;
158
159 virtual placeholder * clone() const = 0;
160
161 };
162
163 template<typename ValueType>
164 class holder : public placeholder
165 {
166 public: // structors
167
168 holder(const ValueType & value)
169 : held(value)
170 {
171 }
172
173#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
174 holder(ValueType&& value)
175 : held(static_cast< ValueType&& >(value))
176 {
177 }
178#endif
179 public: // queries
180
181 virtual const std::type_info & type() const
182 {
183 return typeid(ValueType);
184 }
185
186 virtual placeholder * clone() const
187 {
188 return new holder(held);
189 }
190
191 public: // representation
192
193 ValueType held;
194
195 private: // intentionally left unimplemented
196 holder & operator=(const holder &);
197 };
198
199#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
200
201 private: // representation
202
203 template<typename ValueType>
204 friend ValueType * any_cast(any *) BOOST_NOEXCEPT;
205
206 template<typename ValueType>
207 friend ValueType * unsafe_any_cast(any *) BOOST_NOEXCEPT;
208
209#else
210
211 public: // representation (public so any_cast can be non-friend)
212
213#endif
214
215 placeholder * content;
216
217 };
218
219 inline void swap(any & lhs, any & rhs) BOOST_NOEXCEPT
220 {
221 lhs.swap(rhs);
222 }
223
224 class bad_any_cast : public std::bad_cast
225 {
226 public:
227 virtual const char * what() const throw()
228 {
229 return "ndnboost::bad_any_cast: "
230 "failed conversion using ndnboost::any_cast";
231 }
232 };
233
234 template<typename ValueType>
235 ValueType * any_cast(any * operand) BOOST_NOEXCEPT
236 {
237 return operand &&
238#ifdef BOOST_AUX_ANY_TYPE_ID_NAME
239 std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0
240#else
241 operand->type() == typeid(ValueType)
242#endif
243 ? &static_cast<any::holder<ValueType> *>(operand->content)->held
244 : 0;
245 }
246
247 template<typename ValueType>
248 inline const ValueType * any_cast(const any * operand) BOOST_NOEXCEPT
249 {
250 return any_cast<ValueType>(const_cast<any *>(operand));
251 }
252
253 template<typename ValueType>
254 ValueType any_cast(any & operand)
255 {
256 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
257
258#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
259 // If 'nonref' is still reference type, it means the user has not
260 // specialized 'remove_reference'.
261
262 // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
263 // to generate specialization of remove_reference for your class
264 // See type traits library documentation for details
265 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
266#endif
267
268 nonref * result = any_cast<nonref>(&operand);
269 if(!result)
270 ndnboost::throw_exception(bad_any_cast());
271 return *result;
272 }
273
274 template<typename ValueType>
275 inline ValueType any_cast(const any & operand)
276 {
277 typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
278
279#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
280 // The comment in the above version of 'any_cast' explains when this
281 // assert is fired and what to do.
282 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
283#endif
284
285 return any_cast<const nonref &>(const_cast<any &>(operand));
286 }
287
288 // Note: The "unsafe" versions of any_cast are not part of the
289 // public interface and may be removed at any time. They are
290 // required where we know what type is stored in the any and can't
291 // use typeid() comparison, e.g., when our types may travel across
292 // different shared libraries.
293 template<typename ValueType>
294 inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
295 {
296 return &static_cast<any::holder<ValueType> *>(operand->content)->held;
297 }
298
299 template<typename ValueType>
300 inline const ValueType * unsafe_any_cast(const any * operand) BOOST_NOEXCEPT
301 {
302 return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
303 }
304}
305
306// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
307//
308// Distributed under the Boost Software License, Version 1.0. (See
309// accompanying file LICENSE_1_0.txt or copy at
310// http://www.boost.org/LICENSE_1_0.txt)
311
312#endif