blob: eeefb6485183e2e8742c6c529b9a0a0c6c7a90e2 [file] [log] [blame]
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -07001// See http://www.boost.org/libs/any for Documentation.
2
Jeff Thompson3d613fd2013-10-15 15:39:04 -07003#ifndef NDNBOOST_ANY_INCLUDED
4#define NDNBOOST_ANY_INCLUDED
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -07005
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
Jeff Thompson9939dcd2013-10-15 15:12:24 -070028// See ndnboost/python/type_id.hpp
Jeff Thompson3d613fd2013-10-15 15:39:04 -070029// TODO: add NDNBOOST_TYPEID_COMPARE_BY_NAME to config.hpp
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070030# 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))
Jeff Thompson3d613fd2013-10-15 15:39:04 -070035# define NDNBOOST_AUX_ANY_TYPE_ID_NAME
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070036#include <cstring>
37# endif
38
39namespace ndnboost
40{
41 class any
42 {
43 public: // structors
44
Jeff Thompson3d613fd2013-10-15 15:39:04 -070045 any() NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070046 : 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070061#ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070062 // Move constructor
Jeff Thompson3d613fd2013-10-15 15:39:04 -070063 any(any&& other) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070064 : 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070077 ~any() NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070078 {
79 delete content;
80 }
81
82 public: // modifiers
83
Jeff Thompson3d613fd2013-10-15 15:39:04 -070084 any & swap(any & rhs) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070085 {
86 std::swap(content, rhs.content);
87 return *this;
88 }
89
90
Jeff Thompson3d613fd2013-10-15 15:39:04 -070091#ifdef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -070092 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700113 any & operator=(any&& rhs) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700114 {
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700131 bool empty() const NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700132 {
133 return !content;
134 }
135
136 const std::type_info & type() const
137 {
138 return content ? content->type() : typeid(void);
139 }
140
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700141#ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700142 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700173#ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700174 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700199#ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700200
201 private: // representation
202
203 template<typename ValueType>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700204 friend ValueType * any_cast(any *) NDNBOOST_NOEXCEPT;
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700205
206 template<typename ValueType>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700207 friend ValueType * unsafe_any_cast(any *) NDNBOOST_NOEXCEPT;
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700208
209#else
210
211 public: // representation (public so any_cast can be non-friend)
212
213#endif
214
215 placeholder * content;
216
217 };
218
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700219 inline void swap(any & lhs, any & rhs) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700220 {
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>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700235 ValueType * any_cast(any * operand) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700236 {
237 return operand &&
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700238#ifdef NDNBOOST_AUX_ANY_TYPE_ID_NAME
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700239 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>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700248 inline const ValueType * any_cast(const any * operand) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700249 {
250 return any_cast<ValueType>(const_cast<any *>(operand));
251 }
252
253 template<typename ValueType>
254 ValueType any_cast(any & operand)
255 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700256 typedef NDNBOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700257
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700258#ifdef NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700259 // If 'nonref' is still reference type, it means the user has not
260 // specialized 'remove_reference'.
261
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700262 // Please use NDNBOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700263 // to generate specialization of remove_reference for your class
264 // See type traits library documentation for details
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700265 NDNBOOST_STATIC_ASSERT(!is_reference<nonref>::value);
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700266#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 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700277 typedef NDNBOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700278
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700279#ifdef NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700280 // The comment in the above version of 'any_cast' explains when this
281 // assert is fired and what to do.
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700282 NDNBOOST_STATIC_ASSERT(!is_reference<nonref>::value);
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700283#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>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700294 inline ValueType * unsafe_any_cast(any * operand) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700295 {
296 return &static_cast<any::holder<ValueType> *>(operand->content)->held;
297 }
298
299 template<typename ValueType>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700300 inline const ValueType * unsafe_any_cast(const any * operand) NDNBOOST_NOEXCEPT
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700301 {
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