Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 1 | // See http://www.boost.org/libs/any for Documentation. |
| 2 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 3 | #ifndef NDNBOOST_ANY_INCLUDED |
| 4 | #define NDNBOOST_ANY_INCLUDED |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 9939dcd | 2013-10-15 15:12:24 -0700 | [diff] [blame] | 28 | // See ndnboost/python/type_id.hpp |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 29 | // TODO: add NDNBOOST_TYPEID_COMPARE_BY_NAME to config.hpp |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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)) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 35 | # define NDNBOOST_AUX_ANY_TYPE_ID_NAME |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 36 | #include <cstring> |
| 37 | # endif |
| 38 | |
| 39 | namespace ndnboost |
| 40 | { |
| 41 | class any |
| 42 | { |
| 43 | public: // structors |
| 44 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 45 | any() NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 61 | #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 62 | // Move constructor |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 63 | any(any&& other) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 77 | ~any() NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 78 | { |
| 79 | delete content; |
| 80 | } |
| 81 | |
| 82 | public: // modifiers |
| 83 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 84 | any & swap(any & rhs) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 85 | { |
| 86 | std::swap(content, rhs.content); |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 91 | #ifdef NDNBOOST_NO_CXX11_RVALUE_REFERENCES |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 113 | any & operator=(any&& rhs) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 131 | bool empty() const NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 132 | { |
| 133 | return !content; |
| 134 | } |
| 135 | |
| 136 | const std::type_info & type() const |
| 137 | { |
| 138 | return content ? content->type() : typeid(void); |
| 139 | } |
| 140 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 141 | #ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 173 | #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 199 | #ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 200 | |
| 201 | private: // representation |
| 202 | |
| 203 | template<typename ValueType> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 204 | friend ValueType * any_cast(any *) NDNBOOST_NOEXCEPT; |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 205 | |
| 206 | template<typename ValueType> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 207 | friend ValueType * unsafe_any_cast(any *) NDNBOOST_NOEXCEPT; |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 219 | inline void swap(any & lhs, any & rhs) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 235 | ValueType * any_cast(any * operand) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 236 | { |
| 237 | return operand && |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 238 | #ifdef NDNBOOST_AUX_ANY_TYPE_ID_NAME |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 248 | inline const ValueType * any_cast(const any * operand) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 249 | { |
| 250 | return any_cast<ValueType>(const_cast<any *>(operand)); |
| 251 | } |
| 252 | |
| 253 | template<typename ValueType> |
| 254 | ValueType any_cast(any & operand) |
| 255 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 256 | typedef NDNBOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref; |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 257 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 258 | #ifdef NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 259 | // If 'nonref' is still reference type, it means the user has not |
| 260 | // specialized 'remove_reference'. |
| 261 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 262 | // Please use NDNBOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 263 | // to generate specialization of remove_reference for your class |
| 264 | // See type traits library documentation for details |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 265 | NDNBOOST_STATIC_ASSERT(!is_reference<nonref>::value); |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 277 | typedef NDNBOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref; |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 278 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 279 | #ifdef NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 280 | // The comment in the above version of 'any_cast' explains when this |
| 281 | // assert is fired and what to do. |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 282 | NDNBOOST_STATIC_ASSERT(!is_reference<nonref>::value); |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 294 | inline ValueType * unsafe_any_cast(any * operand) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 295 | { |
| 296 | return &static_cast<any::holder<ValueType> *>(operand->content)->held; |
| 297 | } |
| 298 | |
| 299 | template<typename ValueType> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 300 | inline const ValueType * unsafe_any_cast(const any * operand) NDNBOOST_NOEXCEPT |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 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 |