| 1 | /* |
| 2 | * Copyright 2011-present Facebook, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include <functional> |
| 19 | |
| 20 | #include <folly/CPortability.h> |
| 21 | #include <folly/Conv.h> |
| 22 | #include <folly/Format.h> |
| 23 | #include <folly/Likely.h> |
| 24 | #include <folly/detail/Iterators.h> |
| 25 | #include <folly/lang/Exception.h> |
| 26 | |
| 27 | namespace folly { |
| 28 | namespace detail { |
| 29 | struct DynamicHasher { |
| 30 | using is_transparent = void; |
| 31 | |
| 32 | size_t operator()(dynamic const& d) const { |
| 33 | return d.hash(); |
| 34 | } |
| 35 | |
| 36 | template <typename T> |
| 37 | std::enable_if_t<std::is_convertible<T, StringPiece>::value, size_t> |
| 38 | operator()(T const& val) const { |
| 39 | // keep consistent with dynamic::hash() for strings |
| 40 | return Hash()(static_cast<StringPiece>(val)); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | struct DynamicKeyEqual { |
| 45 | using is_transparent = void; |
| 46 | |
| 47 | bool operator()(const dynamic& lhs, const dynamic& rhs) const { |
| 48 | return std::equal_to<dynamic>()(lhs, rhs); |
| 49 | } |
| 50 | |
| 51 | // Dynamic objects contains a map<dynamic, dynamic>. At least one of the |
| 52 | // operands should be a dynamic. Hence, an operator() where both operands are |
| 53 | // convertible to StringPiece is unnecessary. |
| 54 | template <typename A, typename B> |
| 55 | std::enable_if_t< |
| 56 | std::is_convertible<A, StringPiece>::value && |
| 57 | std::is_convertible<B, StringPiece>::value, |
| 58 | bool> |
| 59 | operator()(A const& lhs, B const& rhs) const = delete; |
| 60 | |
| 61 | template <typename A> |
| 62 | std::enable_if_t<std::is_convertible<A, StringPiece>::value, bool> operator()( |
| 63 | A const& lhs, |
| 64 | dynamic const& rhs) const { |
| 65 | return FOLLY_LIKELY(rhs.type() == dynamic::Type::STRING) && |
| 66 | std::equal_to<StringPiece>()(lhs, rhs.stringPiece()); |
| 67 | } |
| 68 | |
| 69 | template <typename B> |
| 70 | std::enable_if_t<std::is_convertible<B, StringPiece>::value, bool> operator()( |
| 71 | dynamic const& lhs, |
| 72 | B const& rhs) const { |
| 73 | return FOLLY_LIKELY(lhs.type() == dynamic::Type::STRING) && |
| 74 | std::equal_to<StringPiece>()(lhs.stringPiece(), rhs); |
| 75 | } |
| 76 | }; |
| 77 | } // namespace detail |
| 78 | } // namespace folly |
| 79 | |
| 80 | ////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | namespace std { |
| 83 | |
| 84 | template <> |
| 85 | struct hash<::folly::dynamic> { |
| 86 | size_t operator()(::folly::dynamic const& d) const { |
| 87 | return d.hash(); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | } // namespace std |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | // This is a higher-order preprocessor macro to aid going from runtime |
| 96 | // types to the compile time type system. |
| 97 | #define FB_DYNAMIC_APPLY(type, apply) \ |
| 98 | do { \ |
| 99 | switch ((type)) { \ |
| 100 | case NULLT: \ |
| 101 | apply(std::nullptr_t); \ |
| 102 | break; \ |
| 103 | case ARRAY: \ |
| 104 | apply(Array); \ |
| 105 | break; \ |
| 106 | case BOOL: \ |
| 107 | apply(bool); \ |
| 108 | break; \ |
| 109 | case DOUBLE: \ |
| 110 | apply(double); \ |
| 111 | break; \ |
| 112 | case INT64: \ |
| 113 | apply(int64_t); \ |
| 114 | break; \ |
| 115 | case OBJECT: \ |
| 116 | apply(ObjectImpl); \ |
| 117 | break; \ |
| 118 | case STRING: \ |
| 119 | apply(std::string); \ |
| 120 | break; \ |
| 121 | default: \ |
| 122 | CHECK(0); \ |
| 123 | abort(); \ |
| 124 | } \ |
| 125 | } while (0) |
| 126 | |
| 127 | ////////////////////////////////////////////////////////////////////// |
| 128 | |
| 129 | namespace folly { |
| 130 | |
| 131 | struct FOLLY_EXPORT TypeError : std::runtime_error { |
| 132 | explicit TypeError(const std::string& expected, dynamic::Type actual); |
| 133 | explicit TypeError( |
| 134 | const std::string& expected, |
| 135 | dynamic::Type actual1, |
| 136 | dynamic::Type actual2); |
| 137 | // TODO: noexcept calculation required through gcc-v4.9; remove once upgrading |
| 138 | // to gcc-v5. |
| 139 | TypeError(const TypeError&) noexcept( |
| 140 | std::is_nothrow_copy_constructible<std::runtime_error>::value); |
| 141 | TypeError& operator=(const TypeError&) noexcept( |
| 142 | std::is_nothrow_copy_assignable<std::runtime_error>::value); |
| 143 | TypeError(TypeError&&) noexcept( |
| 144 | std::is_nothrow_move_constructible<std::runtime_error>::value); |
| 145 | TypeError& operator=(TypeError&&) noexcept( |
| 146 | std::is_nothrow_move_assignable<std::runtime_error>::value); |
| 147 | ~TypeError() override; |
| 148 | }; |
| 149 | |
| 150 | ////////////////////////////////////////////////////////////////////// |
| 151 | |
| 152 | namespace detail { |
| 153 | |
| 154 | // This helper is used in destroy() to be able to run destructors on |
| 155 | // types like "int64_t" without a compiler error. |
| 156 | struct Destroy { |
| 157 | template <class T> |
| 158 | static void destroy(T* t) { |
| 159 | t->~T(); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | /* |
| 164 | * Helper for implementing numeric conversions in operators on |
| 165 | * numbers. Just promotes to double when one of the arguments is |
| 166 | * double, or throws if either is not a numeric type. |
| 167 | */ |
| 168 | template <template <class> class Op> |
| 169 | dynamic numericOp(dynamic const& a, dynamic const& b) { |
| 170 | if (!a.isNumber() || !b.isNumber()) { |
| 171 | throw_exception<TypeError>("numeric" , a.type(), b.type()); |
| 172 | } |
| 173 | if (a.type() != b.type()) { |
| 174 | auto& integ = a.isInt() ? a : b; |
| 175 | auto& nonint = a.isInt() ? b : a; |
| 176 | return Op<double>()(to<double>(integ.asInt()), nonint.asDouble()); |
| 177 | } |
| 178 | if (a.isDouble()) { |
| 179 | return Op<double>()(a.asDouble(), b.asDouble()); |
| 180 | } |
| 181 | return Op<int64_t>()(a.asInt(), b.asInt()); |
| 182 | } |
| 183 | |
| 184 | } // namespace detail |
| 185 | |
| 186 | ////////////////////////////////////////////////////////////////////// |
| 187 | |
| 188 | /* |
| 189 | * We're doing this instead of a simple member typedef to avoid the |
| 190 | * undefined behavior of parameterizing F14NodeMap<> with an |
| 191 | * incomplete type. |
| 192 | * |
| 193 | * Note: Later we may add separate order tracking here (a multi-index |
| 194 | * type of thing.) |
| 195 | */ |
| 196 | struct dynamic::ObjectImpl : F14NodeMap< |
| 197 | dynamic, |
| 198 | dynamic, |
| 199 | detail::DynamicHasher, |
| 200 | detail::DynamicKeyEqual> {}; |
| 201 | |
| 202 | ////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | // Helper object for creating objects conveniently. See object and |
| 205 | // the dynamic::dynamic(ObjectMaker&&) ctor. |
| 206 | struct dynamic::ObjectMaker { |
| 207 | friend struct dynamic; |
| 208 | |
| 209 | explicit ObjectMaker() : val_(dynamic::object) {} |
| 210 | explicit ObjectMaker(dynamic key, dynamic val) : val_(dynamic::object) { |
| 211 | val_.insert(std::move(key), std::move(val)); |
| 212 | } |
| 213 | |
| 214 | // Make sure no one tries to save one of these into an lvalue with |
| 215 | // auto or anything like that. |
| 216 | ObjectMaker(ObjectMaker&&) = default; |
| 217 | ObjectMaker(ObjectMaker const&) = delete; |
| 218 | ObjectMaker& operator=(ObjectMaker const&) = delete; |
| 219 | ObjectMaker& operator=(ObjectMaker&&) = delete; |
| 220 | |
| 221 | // This returns an rvalue-reference instead of an lvalue-reference |
| 222 | // to allow constructs like this to moved instead of copied: |
| 223 | // dynamic a = dynamic::object("a", "b")("c", "d") |
| 224 | ObjectMaker&& operator()(dynamic key, dynamic val) { |
| 225 | val_.insert(std::move(key), std::move(val)); |
| 226 | return std::move(*this); |
| 227 | } |
| 228 | |
| 229 | private: |
| 230 | dynamic val_; |
| 231 | }; |
| 232 | |
| 233 | inline void dynamic::array(EmptyArrayTag) {} |
| 234 | |
| 235 | template <class... Args> |
| 236 | inline dynamic dynamic::array(Args&&... args) { |
| 237 | return dynamic(Array{std::forward<Args>(args)...}); |
| 238 | } |
| 239 | |
| 240 | inline dynamic::ObjectMaker dynamic::object() { |
| 241 | return ObjectMaker(); |
| 242 | } |
| 243 | inline dynamic::ObjectMaker dynamic::object(dynamic a, dynamic b) { |
| 244 | return ObjectMaker(std::move(a), std::move(b)); |
| 245 | } |
| 246 | |
| 247 | ////////////////////////////////////////////////////////////////////// |
| 248 | |
| 249 | struct dynamic::item_iterator : detail::IteratorAdaptor< |
| 250 | dynamic::item_iterator, |
| 251 | dynamic::ObjectImpl::iterator, |
| 252 | std::pair<dynamic const, dynamic>, |
| 253 | std::forward_iterator_tag> { |
| 254 | using Super = detail::IteratorAdaptor< |
| 255 | dynamic::item_iterator, |
| 256 | dynamic::ObjectImpl::iterator, |
| 257 | std::pair<dynamic const, dynamic>, |
| 258 | std::forward_iterator_tag>; |
| 259 | /* implicit */ item_iterator(dynamic::ObjectImpl::iterator b) : Super(b) {} |
| 260 | |
| 261 | using object_type = dynamic::ObjectImpl; |
| 262 | }; |
| 263 | |
| 264 | struct dynamic::value_iterator : detail::IteratorAdaptor< |
| 265 | dynamic::value_iterator, |
| 266 | dynamic::ObjectImpl::iterator, |
| 267 | dynamic, |
| 268 | std::forward_iterator_tag> { |
| 269 | using Super = detail::IteratorAdaptor< |
| 270 | dynamic::value_iterator, |
| 271 | dynamic::ObjectImpl::iterator, |
| 272 | dynamic, |
| 273 | std::forward_iterator_tag>; |
| 274 | /* implicit */ value_iterator(dynamic::ObjectImpl::iterator b) : Super(b) {} |
| 275 | |
| 276 | using object_type = dynamic::ObjectImpl; |
| 277 | |
| 278 | dynamic& dereference() const { |
| 279 | return base()->second; |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | struct dynamic::const_item_iterator |
| 284 | : detail::IteratorAdaptor< |
| 285 | dynamic::const_item_iterator, |
| 286 | dynamic::ObjectImpl::const_iterator, |
| 287 | std::pair<dynamic const, dynamic> const, |
| 288 | std::forward_iterator_tag> { |
| 289 | using Super = detail::IteratorAdaptor< |
| 290 | dynamic::const_item_iterator, |
| 291 | dynamic::ObjectImpl::const_iterator, |
| 292 | std::pair<dynamic const, dynamic> const, |
| 293 | std::forward_iterator_tag>; |
| 294 | /* implicit */ const_item_iterator(dynamic::ObjectImpl::const_iterator b) |
| 295 | : Super(b) {} |
| 296 | /* implicit */ const_item_iterator(const_item_iterator const& i) |
| 297 | : Super(i.base()) {} |
| 298 | /* implicit */ const_item_iterator(item_iterator i) : Super(i.base()) {} |
| 299 | |
| 300 | using object_type = dynamic::ObjectImpl const; |
| 301 | }; |
| 302 | |
| 303 | struct dynamic::const_key_iterator : detail::IteratorAdaptor< |
| 304 | dynamic::const_key_iterator, |
| 305 | dynamic::ObjectImpl::const_iterator, |
| 306 | dynamic const, |
| 307 | std::forward_iterator_tag> { |
| 308 | using Super = detail::IteratorAdaptor< |
| 309 | dynamic::const_key_iterator, |
| 310 | dynamic::ObjectImpl::const_iterator, |
| 311 | dynamic const, |
| 312 | std::forward_iterator_tag>; |
| 313 | /* implicit */ const_key_iterator(dynamic::ObjectImpl::const_iterator b) |
| 314 | : Super(b) {} |
| 315 | |
| 316 | using object_type = dynamic::ObjectImpl const; |
| 317 | |
| 318 | dynamic const& dereference() const { |
| 319 | return base()->first; |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | struct dynamic::const_value_iterator : detail::IteratorAdaptor< |
| 324 | dynamic::const_value_iterator, |
| 325 | dynamic::ObjectImpl::const_iterator, |
| 326 | dynamic const, |
| 327 | std::forward_iterator_tag> { |
| 328 | using Super = detail::IteratorAdaptor< |
| 329 | dynamic::const_value_iterator, |
| 330 | dynamic::ObjectImpl::const_iterator, |
| 331 | dynamic const, |
| 332 | std::forward_iterator_tag>; |
| 333 | /* implicit */ const_value_iterator(dynamic::ObjectImpl::const_iterator b) |
| 334 | : Super(b) {} |
| 335 | /* implicit */ const_value_iterator(value_iterator i) : Super(i.base()) {} |
| 336 | /* implicit */ const_value_iterator(dynamic::ObjectImpl::iterator i) |
| 337 | : Super(i) {} |
| 338 | |
| 339 | using object_type = dynamic::ObjectImpl const; |
| 340 | |
| 341 | dynamic const& dereference() const { |
| 342 | return base()->second; |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | ////////////////////////////////////////////////////////////////////// |
| 347 | |
| 348 | inline dynamic::dynamic() : dynamic(nullptr) {} |
| 349 | |
| 350 | inline dynamic::dynamic(std::nullptr_t) : type_(NULLT) {} |
| 351 | |
| 352 | inline dynamic::dynamic(void (*)(EmptyArrayTag)) : type_(ARRAY) { |
| 353 | new (&u_.array) Array(); |
| 354 | } |
| 355 | |
| 356 | inline dynamic::dynamic(ObjectMaker (*)()) : type_(OBJECT) { |
| 357 | new (getAddress<ObjectImpl>()) ObjectImpl(); |
| 358 | } |
| 359 | |
| 360 | inline dynamic::dynamic(StringPiece s) : type_(STRING) { |
| 361 | new (&u_.string) std::string(s.data(), s.size()); |
| 362 | } |
| 363 | |
| 364 | inline dynamic::dynamic(char const* s) : type_(STRING) { |
| 365 | new (&u_.string) std::string(s); |
| 366 | } |
| 367 | |
| 368 | inline dynamic::dynamic(std::string s) : type_(STRING) { |
| 369 | new (&u_.string) std::string(std::move(s)); |
| 370 | } |
| 371 | |
| 372 | inline dynamic::dynamic(ObjectMaker&& maker) : type_(OBJECT) { |
| 373 | new (getAddress<ObjectImpl>()) |
| 374 | ObjectImpl(std::move(*maker.val_.getAddress<ObjectImpl>())); |
| 375 | } |
| 376 | |
| 377 | inline dynamic::dynamic(dynamic const& o) : type_(NULLT) { |
| 378 | *this = o; |
| 379 | } |
| 380 | |
| 381 | inline dynamic::dynamic(dynamic&& o) noexcept : type_(NULLT) { |
| 382 | *this = std::move(o); |
| 383 | } |
| 384 | |
| 385 | inline dynamic::~dynamic() noexcept { |
| 386 | destroy(); |
| 387 | } |
| 388 | |
| 389 | // Integral types except bool convert to int64_t, float types to double. |
| 390 | template <class T> |
| 391 | struct dynamic::NumericTypeHelper< |
| 392 | T, |
| 393 | typename std::enable_if<std::is_integral<T>::value>::type> { |
| 394 | static_assert( |
| 395 | !kIsObjC || sizeof(T) > sizeof(char), |
| 396 | "char-sized types are ambiguous in objc; cast to bool or wider type" ); |
| 397 | using type = int64_t; |
| 398 | }; |
| 399 | template <> |
| 400 | struct dynamic::NumericTypeHelper<bool> { |
| 401 | using type = bool; |
| 402 | }; |
| 403 | template <> |
| 404 | struct dynamic::NumericTypeHelper<float> { |
| 405 | using type = double; |
| 406 | }; |
| 407 | template <> |
| 408 | struct dynamic::NumericTypeHelper<double> { |
| 409 | using type = double; |
| 410 | }; |
| 411 | |
| 412 | inline dynamic::dynamic(std::vector<bool>::reference b) |
| 413 | : dynamic(static_cast<bool>(b)) {} |
| 414 | inline dynamic::dynamic(VectorBoolConstRefCtorType b) |
| 415 | : dynamic(static_cast<bool>(b)) {} |
| 416 | |
| 417 | template < |
| 418 | class T, |
| 419 | class NumericType /* = typename NumericTypeHelper<T>::type */> |
| 420 | dynamic::dynamic(T t) { |
| 421 | type_ = TypeInfo<NumericType>::type; |
| 422 | new (getAddress<NumericType>()) NumericType(NumericType(t)); |
| 423 | } |
| 424 | |
| 425 | template <class Iterator> |
| 426 | dynamic::dynamic(Iterator first, Iterator last) : type_(ARRAY) { |
| 427 | new (&u_.array) Array(first, last); |
| 428 | } |
| 429 | |
| 430 | ////////////////////////////////////////////////////////////////////// |
| 431 | |
| 432 | inline dynamic::const_iterator dynamic::begin() const { |
| 433 | return get<Array>().begin(); |
| 434 | } |
| 435 | inline dynamic::const_iterator dynamic::end() const { |
| 436 | return get<Array>().end(); |
| 437 | } |
| 438 | |
| 439 | inline dynamic::iterator dynamic::begin() { |
| 440 | return get<Array>().begin(); |
| 441 | } |
| 442 | inline dynamic::iterator dynamic::end() { |
| 443 | return get<Array>().end(); |
| 444 | } |
| 445 | |
| 446 | template <class It> |
| 447 | struct dynamic::IterableProxy { |
| 448 | typedef It iterator; |
| 449 | typedef typename It::value_type value_type; |
| 450 | typedef typename It::object_type object_type; |
| 451 | |
| 452 | /* implicit */ IterableProxy(object_type* o) : o_(o) {} |
| 453 | |
| 454 | It begin() const { |
| 455 | return o_->begin(); |
| 456 | } |
| 457 | |
| 458 | It end() const { |
| 459 | return o_->end(); |
| 460 | } |
| 461 | |
| 462 | private: |
| 463 | object_type* o_; |
| 464 | }; |
| 465 | |
| 466 | inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys() |
| 467 | const { |
| 468 | return &(get<ObjectImpl>()); |
| 469 | } |
| 470 | |
| 471 | inline dynamic::IterableProxy<dynamic::const_value_iterator> dynamic::values() |
| 472 | const { |
| 473 | return &(get<ObjectImpl>()); |
| 474 | } |
| 475 | |
| 476 | inline dynamic::IterableProxy<dynamic::const_item_iterator> dynamic::items() |
| 477 | const { |
| 478 | return &(get<ObjectImpl>()); |
| 479 | } |
| 480 | |
| 481 | inline dynamic::IterableProxy<dynamic::value_iterator> dynamic::values() { |
| 482 | return &(get<ObjectImpl>()); |
| 483 | } |
| 484 | |
| 485 | inline dynamic::IterableProxy<dynamic::item_iterator> dynamic::items() { |
| 486 | return &(get<ObjectImpl>()); |
| 487 | } |
| 488 | |
| 489 | inline bool dynamic::isString() const { |
| 490 | return get_nothrow<std::string>() != nullptr; |
| 491 | } |
| 492 | inline bool dynamic::isObject() const { |
| 493 | return get_nothrow<ObjectImpl>() != nullptr; |
| 494 | } |
| 495 | inline bool dynamic::isBool() const { |
| 496 | return get_nothrow<bool>() != nullptr; |
| 497 | } |
| 498 | inline bool dynamic::isArray() const { |
| 499 | return get_nothrow<Array>() != nullptr; |
| 500 | } |
| 501 | inline bool dynamic::isDouble() const { |
| 502 | return get_nothrow<double>() != nullptr; |
| 503 | } |
| 504 | inline bool dynamic::isInt() const { |
| 505 | return get_nothrow<int64_t>() != nullptr; |
| 506 | } |
| 507 | inline bool dynamic::isNull() const { |
| 508 | return get_nothrow<std::nullptr_t>() != nullptr; |
| 509 | } |
| 510 | inline bool dynamic::isNumber() const { |
| 511 | return isInt() || isDouble(); |
| 512 | } |
| 513 | |
| 514 | inline dynamic::Type dynamic::type() const { |
| 515 | return type_; |
| 516 | } |
| 517 | |
| 518 | inline std::string dynamic::asString() const { |
| 519 | return asImpl<std::string>(); |
| 520 | } |
| 521 | inline double dynamic::asDouble() const { |
| 522 | return asImpl<double>(); |
| 523 | } |
| 524 | inline int64_t dynamic::asInt() const { |
| 525 | return asImpl<int64_t>(); |
| 526 | } |
| 527 | inline bool dynamic::asBool() const { |
| 528 | return asImpl<bool>(); |
| 529 | } |
| 530 | |
| 531 | inline const std::string& dynamic::getString() const& { |
| 532 | return get<std::string>(); |
| 533 | } |
| 534 | inline double dynamic::getDouble() const& { |
| 535 | return get<double>(); |
| 536 | } |
| 537 | inline int64_t dynamic::getInt() const& { |
| 538 | return get<int64_t>(); |
| 539 | } |
| 540 | inline bool dynamic::getBool() const& { |
| 541 | return get<bool>(); |
| 542 | } |
| 543 | |
| 544 | inline std::string& dynamic::getString() & { |
| 545 | return get<std::string>(); |
| 546 | } |
| 547 | inline double& dynamic::getDouble() & { |
| 548 | return get<double>(); |
| 549 | } |
| 550 | inline int64_t& dynamic::getInt() & { |
| 551 | return get<int64_t>(); |
| 552 | } |
| 553 | inline bool& dynamic::getBool() & { |
| 554 | return get<bool>(); |
| 555 | } |
| 556 | |
| 557 | inline std::string&& dynamic::getString() && { |
| 558 | return std::move(get<std::string>()); |
| 559 | } |
| 560 | inline double dynamic::getDouble() && { |
| 561 | return get<double>(); |
| 562 | } |
| 563 | inline int64_t dynamic::getInt() && { |
| 564 | return get<int64_t>(); |
| 565 | } |
| 566 | inline bool dynamic::getBool() && { |
| 567 | return get<bool>(); |
| 568 | } |
| 569 | |
| 570 | inline const char* dynamic::data() const& { |
| 571 | return get<std::string>().data(); |
| 572 | } |
| 573 | inline const char* dynamic::c_str() const& { |
| 574 | return get<std::string>().c_str(); |
| 575 | } |
| 576 | inline StringPiece dynamic::stringPiece() const { |
| 577 | return get<std::string>(); |
| 578 | } |
| 579 | |
| 580 | template <class T> |
| 581 | struct dynamic::CompareOp { |
| 582 | static bool comp(T const& a, T const& b) { |
| 583 | return a < b; |
| 584 | } |
| 585 | }; |
| 586 | template <> |
| 587 | struct dynamic::CompareOp<dynamic::ObjectImpl> { |
| 588 | static bool comp(ObjectImpl const&, ObjectImpl const&) { |
| 589 | // This code never executes; it is just here for the compiler. |
| 590 | return false; |
| 591 | } |
| 592 | }; |
| 593 | template <> |
| 594 | struct dynamic::CompareOp<std::nullptr_t> { |
| 595 | static bool comp(std::nullptr_t const&, std::nullptr_t const&) { |
| 596 | return true; |
| 597 | } |
| 598 | }; |
| 599 | |
| 600 | inline dynamic& dynamic::operator+=(dynamic const& o) { |
| 601 | if (type() == STRING && o.type() == STRING) { |
| 602 | *getAddress<std::string>() += *o.getAddress<std::string>(); |
| 603 | return *this; |
| 604 | } |
| 605 | *this = detail::numericOp<std::plus>(*this, o); |
| 606 | return *this; |
| 607 | } |
| 608 | |
| 609 | inline dynamic& dynamic::operator-=(dynamic const& o) { |
| 610 | *this = detail::numericOp<std::minus>(*this, o); |
| 611 | return *this; |
| 612 | } |
| 613 | |
| 614 | inline dynamic& dynamic::operator*=(dynamic const& o) { |
| 615 | *this = detail::numericOp<std::multiplies>(*this, o); |
| 616 | return *this; |
| 617 | } |
| 618 | |
| 619 | inline dynamic& dynamic::operator/=(dynamic const& o) { |
| 620 | *this = detail::numericOp<std::divides>(*this, o); |
| 621 | return *this; |
| 622 | } |
| 623 | |
| 624 | #define FB_DYNAMIC_INTEGER_OP(op) \ |
| 625 | inline dynamic& dynamic::operator op(dynamic const& o) { \ |
| 626 | if (!isInt() || !o.isInt()) { \ |
| 627 | throw_exception<TypeError>("int64", type(), o.type()); \ |
| 628 | } \ |
| 629 | *getAddress<int64_t>() op o.asInt(); \ |
| 630 | return *this; \ |
| 631 | } |
| 632 | |
| 633 | FB_DYNAMIC_INTEGER_OP(%=) |
| 634 | FB_DYNAMIC_INTEGER_OP(|=) |
| 635 | FB_DYNAMIC_INTEGER_OP(&=) |
| 636 | FB_DYNAMIC_INTEGER_OP(^=) |
| 637 | |
| 638 | #undef FB_DYNAMIC_INTEGER_OP |
| 639 | |
| 640 | inline dynamic& dynamic::operator++() { |
| 641 | ++get<int64_t>(); |
| 642 | return *this; |
| 643 | } |
| 644 | |
| 645 | inline dynamic& dynamic::operator--() { |
| 646 | --get<int64_t>(); |
| 647 | return *this; |
| 648 | } |
| 649 | |
| 650 | template <typename K> |
| 651 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic const&> dynamic::operator[]( |
| 652 | K&& idx) const& { |
| 653 | return at(std::forward<K>(idx)); |
| 654 | } |
| 655 | |
| 656 | template <typename K> |
| 657 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic&> dynamic::operator[]( |
| 658 | K&& idx) & { |
| 659 | if (!isObject() && !isArray()) { |
| 660 | throw_exception<TypeError>("object/array" , type()); |
| 661 | } |
| 662 | if (isArray()) { |
| 663 | return at(std::forward<K>(idx)); |
| 664 | } |
| 665 | auto& obj = get<ObjectImpl>(); |
| 666 | auto ret = obj.emplace(std::forward<K>(idx), nullptr); |
| 667 | return ret.first->second; |
| 668 | } |
| 669 | |
| 670 | template <typename K> |
| 671 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic&&> dynamic::operator[]( |
| 672 | K&& idx) && { |
| 673 | return std::move((*this)[std::forward<K>(idx)]); |
| 674 | } |
| 675 | |
| 676 | inline dynamic const& dynamic::operator[](StringPiece k) const& { |
| 677 | return at(k); |
| 678 | } |
| 679 | |
| 680 | inline dynamic&& dynamic::operator[](StringPiece k) && { |
| 681 | return std::move((*this)[k]); |
| 682 | } |
| 683 | |
| 684 | template <typename K> |
| 685 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic> dynamic::getDefault( |
| 686 | K&& k, |
| 687 | const dynamic& v) const& { |
| 688 | auto& obj = get<ObjectImpl>(); |
| 689 | auto it = obj.find(std::forward<K>(k)); |
| 690 | return it == obj.end() ? v : it->second; |
| 691 | } |
| 692 | |
| 693 | template <typename K> |
| 694 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic> dynamic::getDefault( |
| 695 | K&& k, |
| 696 | dynamic&& v) const& { |
| 697 | auto& obj = get<ObjectImpl>(); |
| 698 | auto it = obj.find(std::forward<K>(k)); |
| 699 | // Avoid clang bug with ternary |
| 700 | if (it == obj.end()) { |
| 701 | return std::move(v); |
| 702 | } else { |
| 703 | return it->second; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | template <typename K> |
| 708 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic> dynamic::getDefault( |
| 709 | K&& k, |
| 710 | const dynamic& v) && { |
| 711 | auto& obj = get<ObjectImpl>(); |
| 712 | auto it = obj.find(std::forward<K>(k)); |
| 713 | // Avoid clang bug with ternary |
| 714 | if (it == obj.end()) { |
| 715 | return v; |
| 716 | } else { |
| 717 | return std::move(it->second); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | template <typename K> |
| 722 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic> dynamic::getDefault( |
| 723 | K&& k, |
| 724 | dynamic&& v) && { |
| 725 | auto& obj = get<ObjectImpl>(); |
| 726 | auto it = obj.find(std::forward<K>(k)); |
| 727 | return std::move(it == obj.end() ? v : it->second); |
| 728 | } |
| 729 | |
| 730 | template <typename K, typename V> |
| 731 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic&> dynamic::setDefault( |
| 732 | K&& k, |
| 733 | V&& v) { |
| 734 | auto& obj = get<ObjectImpl>(); |
| 735 | return obj.emplace(std::forward<K>(k), std::forward<V>(v)).first->second; |
| 736 | } |
| 737 | |
| 738 | template <typename K> |
| 739 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic&> dynamic::setDefault( |
| 740 | K&& k, |
| 741 | dynamic&& v) { |
| 742 | auto& obj = get<ObjectImpl>(); |
| 743 | return obj.emplace(std::forward<K>(k), std::move(v)).first->second; |
| 744 | } |
| 745 | |
| 746 | template <typename K> |
| 747 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic&> dynamic::setDefault( |
| 748 | K&& k, |
| 749 | const dynamic& v) { |
| 750 | auto& obj = get<ObjectImpl>(); |
| 751 | return obj.emplace(std::forward<K>(k), v).first->second; |
| 752 | } |
| 753 | |
| 754 | template <typename V> |
| 755 | dynamic& dynamic::setDefault(StringPiece k, V&& v) { |
| 756 | auto& obj = get<ObjectImpl>(); |
| 757 | return obj.emplace(k, std::forward<V>(v)).first->second; |
| 758 | } |
| 759 | |
| 760 | inline dynamic& dynamic::setDefault(StringPiece k, dynamic&& v) { |
| 761 | auto& obj = get<ObjectImpl>(); |
| 762 | return obj.emplace(k, std::move(v)).first->second; |
| 763 | } |
| 764 | |
| 765 | inline dynamic& dynamic::setDefault(StringPiece k, const dynamic& v) { |
| 766 | auto& obj = get<ObjectImpl>(); |
| 767 | return obj.emplace(k, v).first->second; |
| 768 | } |
| 769 | |
| 770 | template <typename K> |
| 771 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic const*> dynamic::get_ptr( |
| 772 | K&& k) const& { |
| 773 | return get_ptrImpl(std::forward<K>(k)); |
| 774 | } |
| 775 | |
| 776 | template <typename K> |
| 777 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic*> dynamic::get_ptr( |
| 778 | K&& idx) & { |
| 779 | return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx)); |
| 780 | } |
| 781 | |
| 782 | inline dynamic* dynamic::get_ptr(StringPiece idx) & { |
| 783 | return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx)); |
| 784 | } |
| 785 | |
| 786 | // clang-format off |
| 787 | inline |
| 788 | dynamic::resolved_json_pointer<dynamic> |
| 789 | dynamic::try_get_ptr(json_pointer const& jsonPtr) & { |
| 790 | auto ret = const_cast<dynamic const*>(this)->try_get_ptr(jsonPtr); |
| 791 | if (ret.hasValue()) { |
| 792 | return json_pointer_resolved_value<dynamic>{ |
| 793 | const_cast<dynamic*>(ret.value().parent), |
| 794 | const_cast<dynamic*>(ret.value().value), |
| 795 | ret.value().parent_key, ret.value().parent_index}; |
| 796 | } else { |
| 797 | return makeUnexpected( |
| 798 | json_pointer_resolution_error<dynamic>{ |
| 799 | ret.error().error_code, |
| 800 | ret.error().index, |
| 801 | const_cast<dynamic*>(ret.error().context)} |
| 802 | ); |
| 803 | } |
| 804 | } |
| 805 | // clang-format on |
| 806 | |
| 807 | inline dynamic* dynamic::get_ptr(json_pointer const& jsonPtr) & { |
| 808 | return const_cast<dynamic*>( |
| 809 | const_cast<dynamic const*>(this)->get_ptr(jsonPtr)); |
| 810 | } |
| 811 | |
| 812 | template <typename K> |
| 813 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic const&> dynamic::at( |
| 814 | K&& k) const& { |
| 815 | return atImpl(std::forward<K>(k)); |
| 816 | } |
| 817 | |
| 818 | template <typename K> |
| 819 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic&> dynamic::at(K&& idx) & { |
| 820 | return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx)); |
| 821 | } |
| 822 | |
| 823 | template <typename K> |
| 824 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic&&> dynamic::at(K&& idx) && { |
| 825 | return std::move(at(idx)); |
| 826 | } |
| 827 | |
| 828 | inline dynamic& dynamic::at(StringPiece idx) & { |
| 829 | return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx)); |
| 830 | } |
| 831 | |
| 832 | inline dynamic&& dynamic::at(StringPiece idx) && { |
| 833 | return std::move(at(idx)); |
| 834 | } |
| 835 | |
| 836 | inline bool dynamic::empty() const { |
| 837 | if (isNull()) { |
| 838 | return true; |
| 839 | } |
| 840 | return !size(); |
| 841 | } |
| 842 | |
| 843 | template <typename K> |
| 844 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic::const_item_iterator> |
| 845 | dynamic::find(K&& key) const { |
| 846 | return get<ObjectImpl>().find(std::forward<K>(key)); |
| 847 | } |
| 848 | |
| 849 | template <typename K> |
| 850 | dynamic::IfIsNonStringDynamicConvertible<K, dynamic::item_iterator> |
| 851 | dynamic::find(K&& key) { |
| 852 | return get<ObjectImpl>().find(std::forward<K>(key)); |
| 853 | } |
| 854 | |
| 855 | inline dynamic::const_item_iterator dynamic::find(StringPiece key) const { |
| 856 | return get<ObjectImpl>().find(key); |
| 857 | } |
| 858 | |
| 859 | inline dynamic::item_iterator dynamic::find(StringPiece key) { |
| 860 | return get<ObjectImpl>().find(key); |
| 861 | } |
| 862 | |
| 863 | template <typename K> |
| 864 | dynamic::IfIsNonStringDynamicConvertible<K, std::size_t> dynamic::count( |
| 865 | K&& key) const { |
| 866 | return find(std::forward<K>(key)) != items().end() ? 1u : 0u; |
| 867 | } |
| 868 | |
| 869 | inline std::size_t dynamic::count(StringPiece key) const { |
| 870 | return find(key) != items().end() ? 1u : 0u; |
| 871 | } |
| 872 | |
| 873 | template <class K, class V> |
| 874 | inline dynamic::IfNotIterator<K, void> dynamic::insert(K&& key, V&& val) { |
| 875 | auto& obj = get<ObjectImpl>(); |
| 876 | obj[std::forward<K>(key)] = std::forward<V>(val); |
| 877 | } |
| 878 | |
| 879 | template <class T> |
| 880 | inline dynamic::iterator dynamic::insert(const_iterator pos, T&& value) { |
| 881 | auto& arr = get<Array>(); |
| 882 | return arr.insert(pos, std::forward<T>(value)); |
| 883 | } |
| 884 | |
| 885 | inline void dynamic::update(const dynamic& mergeObj) { |
| 886 | if (!isObject() || !mergeObj.isObject()) { |
| 887 | throw_exception<TypeError>("object" , type(), mergeObj.type()); |
| 888 | } |
| 889 | |
| 890 | for (const auto& pair : mergeObj.items()) { |
| 891 | (*this)[pair.first] = pair.second; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | inline void dynamic::update_missing(const dynamic& mergeObj1) { |
| 896 | if (!isObject() || !mergeObj1.isObject()) { |
| 897 | throw_exception<TypeError>("object" , type(), mergeObj1.type()); |
| 898 | } |
| 899 | |
| 900 | // Only add if not already there |
| 901 | for (const auto& pair : mergeObj1.items()) { |
| 902 | if ((*this).find(pair.first) == (*this).items().end()) { |
| 903 | (*this)[pair.first] = pair.second; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | inline void dynamic::merge_patch(const dynamic& patch) { |
| 909 | auto& self = *this; |
| 910 | if (!patch.isObject()) { |
| 911 | self = patch; |
| 912 | return; |
| 913 | } |
| 914 | // if we are not an object, erase all contents, reset to object |
| 915 | if (!isObject()) { |
| 916 | self = object; |
| 917 | } |
| 918 | for (const auto& pair : patch.items()) { |
| 919 | if (pair.second.isNull()) { |
| 920 | // if name could be found in current object, remove it |
| 921 | auto it = self.find(pair.first); |
| 922 | if (it != self.items().end()) { |
| 923 | self.erase(it); |
| 924 | } |
| 925 | } else { |
| 926 | self[pair.first].merge_patch(pair.second); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | inline dynamic dynamic::merge( |
| 932 | const dynamic& mergeObj1, |
| 933 | const dynamic& mergeObj2) { |
| 934 | // No checks on type needed here because they are done in update_missing |
| 935 | // Note that we do update_missing here instead of update() because |
| 936 | // it will prevent the extra writes that would occur with update() |
| 937 | auto ret = mergeObj2; |
| 938 | ret.update_missing(mergeObj1); |
| 939 | return ret; |
| 940 | } |
| 941 | |
| 942 | template <typename K> |
| 943 | dynamic::IfIsNonStringDynamicConvertible<K, std::size_t> dynamic::erase( |
| 944 | K&& key) { |
| 945 | auto& obj = get<ObjectImpl>(); |
| 946 | return obj.erase(std::forward<K>(key)); |
| 947 | } |
| 948 | inline std::size_t dynamic::erase(StringPiece key) { |
| 949 | auto& obj = get<ObjectImpl>(); |
| 950 | return obj.erase(key); |
| 951 | } |
| 952 | |
| 953 | inline dynamic::iterator dynamic::erase(const_iterator it) { |
| 954 | auto& arr = get<Array>(); |
| 955 | // std::vector doesn't have an erase method that works on const iterators, |
| 956 | // even though the standard says it should, so this hack converts to a |
| 957 | // non-const iterator before calling erase. |
| 958 | return get<Array>().erase(arr.begin() + (it - arr.begin())); |
| 959 | } |
| 960 | |
| 961 | inline dynamic::const_key_iterator dynamic::erase(const_key_iterator it) { |
| 962 | return const_key_iterator(get<ObjectImpl>().erase(it.base())); |
| 963 | } |
| 964 | |
| 965 | inline dynamic::const_key_iterator dynamic::erase( |
| 966 | const_key_iterator first, |
| 967 | const_key_iterator last) { |
| 968 | return const_key_iterator(get<ObjectImpl>().erase(first.base(), last.base())); |
| 969 | } |
| 970 | |
| 971 | inline dynamic::value_iterator dynamic::erase(const_value_iterator it) { |
| 972 | return value_iterator(get<ObjectImpl>().erase(it.base())); |
| 973 | } |
| 974 | |
| 975 | inline dynamic::value_iterator dynamic::erase( |
| 976 | const_value_iterator first, |
| 977 | const_value_iterator last) { |
| 978 | return value_iterator(get<ObjectImpl>().erase(first.base(), last.base())); |
| 979 | } |
| 980 | |
| 981 | inline dynamic::item_iterator dynamic::erase(const_item_iterator it) { |
| 982 | return item_iterator(get<ObjectImpl>().erase(it.base())); |
| 983 | } |
| 984 | |
| 985 | inline dynamic::item_iterator dynamic::erase( |
| 986 | const_item_iterator first, |
| 987 | const_item_iterator last) { |
| 988 | return item_iterator(get<ObjectImpl>().erase(first.base(), last.base())); |
| 989 | } |
| 990 | |
| 991 | inline void dynamic::resize(std::size_t sz, dynamic const& c) { |
| 992 | auto& arr = get<Array>(); |
| 993 | arr.resize(sz, c); |
| 994 | } |
| 995 | |
| 996 | inline void dynamic::push_back(dynamic const& v) { |
| 997 | auto& arr = get<Array>(); |
| 998 | arr.push_back(v); |
| 999 | } |
| 1000 | |
| 1001 | inline void dynamic::push_back(dynamic&& v) { |
| 1002 | auto& arr = get<Array>(); |
| 1003 | arr.push_back(std::move(v)); |
| 1004 | } |
| 1005 | |
| 1006 | inline void dynamic::pop_back() { |
| 1007 | auto& arr = get<Array>(); |
| 1008 | arr.pop_back(); |
| 1009 | } |
| 1010 | |
| 1011 | ////////////////////////////////////////////////////////////////////// |
| 1012 | |
| 1013 | inline dynamic::dynamic(Array&& r) : type_(ARRAY) { |
| 1014 | new (&u_.array) Array(std::move(r)); |
| 1015 | } |
| 1016 | |
| 1017 | #define FOLLY_DYNAMIC_DEC_TYPEINFO(T, str, val) \ |
| 1018 | template <> \ |
| 1019 | struct dynamic::TypeInfo<T> { \ |
| 1020 | static constexpr const char* name = str; \ |
| 1021 | static constexpr dynamic::Type type = val; \ |
| 1022 | }; \ |
| 1023 | // |
| 1024 | |
| 1025 | FOLLY_DYNAMIC_DEC_TYPEINFO(std::nullptr_t, "null" , dynamic::NULLT) |
| 1026 | FOLLY_DYNAMIC_DEC_TYPEINFO(bool, "boolean" , dynamic::BOOL) |
| 1027 | FOLLY_DYNAMIC_DEC_TYPEINFO(std::string, "string" , dynamic::STRING) |
| 1028 | FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::Array, "array" , dynamic::ARRAY) |
| 1029 | FOLLY_DYNAMIC_DEC_TYPEINFO(double, "double" , dynamic::DOUBLE) |
| 1030 | FOLLY_DYNAMIC_DEC_TYPEINFO(int64_t, "int64" , dynamic::INT64) |
| 1031 | FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::ObjectImpl, "object" , dynamic::OBJECT) |
| 1032 | |
| 1033 | #undef FOLLY_DYNAMIC_DEC_TYPEINFO |
| 1034 | |
| 1035 | template <class T> |
| 1036 | T dynamic::asImpl() const { |
| 1037 | switch (type()) { |
| 1038 | case INT64: |
| 1039 | return to<T>(*get_nothrow<int64_t>()); |
| 1040 | case DOUBLE: |
| 1041 | return to<T>(*get_nothrow<double>()); |
| 1042 | case BOOL: |
| 1043 | return to<T>(*get_nothrow<bool>()); |
| 1044 | case STRING: |
| 1045 | return to<T>(*get_nothrow<std::string>()); |
| 1046 | default: |
| 1047 | throw_exception<TypeError>("int/double/bool/string" , type()); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // Return a T* to our type, or null if we're not that type. |
| 1052 | // clang-format off |
| 1053 | template <class T> |
| 1054 | T* dynamic::get_nothrow() & noexcept { |
| 1055 | if (type_ != TypeInfo<T>::type) { |
| 1056 | return nullptr; |
| 1057 | } |
| 1058 | return getAddress<T>(); |
| 1059 | } |
| 1060 | // clang-format on |
| 1061 | |
| 1062 | template <class T> |
| 1063 | T const* dynamic::get_nothrow() const& noexcept { |
| 1064 | return const_cast<dynamic*>(this)->get_nothrow<T>(); |
| 1065 | } |
| 1066 | |
| 1067 | // Return T* for where we can put a T, without type checking. (Memory |
| 1068 | // might be uninitialized, even.) |
| 1069 | template <class T> |
| 1070 | T* dynamic::getAddress() noexcept { |
| 1071 | return GetAddrImpl<T>::get(u_); |
| 1072 | } |
| 1073 | |
| 1074 | template <class T> |
| 1075 | T const* dynamic::getAddress() const noexcept { |
| 1076 | return const_cast<dynamic*>(this)->getAddress<T>(); |
| 1077 | } |
| 1078 | |
| 1079 | template <class T> |
| 1080 | struct dynamic::GetAddrImpl {}; |
| 1081 | template <> |
| 1082 | struct dynamic::GetAddrImpl<std::nullptr_t> { |
| 1083 | static std::nullptr_t* get(Data& d) noexcept { |
| 1084 | return &d.nul; |
| 1085 | } |
| 1086 | }; |
| 1087 | template <> |
| 1088 | struct dynamic::GetAddrImpl<dynamic::Array> { |
| 1089 | static Array* get(Data& d) noexcept { |
| 1090 | return &d.array; |
| 1091 | } |
| 1092 | }; |
| 1093 | template <> |
| 1094 | struct dynamic::GetAddrImpl<bool> { |
| 1095 | static bool* get(Data& d) noexcept { |
| 1096 | return &d.boolean; |
| 1097 | } |
| 1098 | }; |
| 1099 | template <> |
| 1100 | struct dynamic::GetAddrImpl<int64_t> { |
| 1101 | static int64_t* get(Data& d) noexcept { |
| 1102 | return &d.integer; |
| 1103 | } |
| 1104 | }; |
| 1105 | template <> |
| 1106 | struct dynamic::GetAddrImpl<double> { |
| 1107 | static double* get(Data& d) noexcept { |
| 1108 | return &d.doubl; |
| 1109 | } |
| 1110 | }; |
| 1111 | template <> |
| 1112 | struct dynamic::GetAddrImpl<std::string> { |
| 1113 | static std::string* get(Data& d) noexcept { |
| 1114 | return &d.string; |
| 1115 | } |
| 1116 | }; |
| 1117 | template <> |
| 1118 | struct dynamic::GetAddrImpl<dynamic::ObjectImpl> { |
| 1119 | static_assert( |
| 1120 | sizeof(ObjectImpl) <= sizeof(Data::objectBuffer), |
| 1121 | "In your implementation, F14NodeMap<> apparently takes different" |
| 1122 | " amount of space depending on its template parameters. This is " |
| 1123 | "weird. Make objectBuffer bigger if you want to compile dynamic." ); |
| 1124 | |
| 1125 | static ObjectImpl* get(Data& d) noexcept { |
| 1126 | void* data = &d.objectBuffer; |
| 1127 | return static_cast<ObjectImpl*>(data); |
| 1128 | } |
| 1129 | }; |
| 1130 | |
| 1131 | template <class T> |
| 1132 | T& dynamic::get() { |
| 1133 | if (auto* p = get_nothrow<T>()) { |
| 1134 | return *p; |
| 1135 | } |
| 1136 | throw_exception<TypeError>(TypeInfo<T>::name, type()); |
| 1137 | } |
| 1138 | |
| 1139 | template <class T> |
| 1140 | T const& dynamic::get() const { |
| 1141 | return const_cast<dynamic*>(this)->get<T>(); |
| 1142 | } |
| 1143 | |
| 1144 | ////////////////////////////////////////////////////////////////////// |
| 1145 | |
| 1146 | /* |
| 1147 | * Helper for implementing operator<<. Throws if the type shouldn't |
| 1148 | * support it. |
| 1149 | */ |
| 1150 | template <class T> |
| 1151 | struct dynamic::PrintImpl { |
| 1152 | static void print(dynamic const&, std::ostream& out, T const& t) { |
| 1153 | out << t; |
| 1154 | } |
| 1155 | }; |
| 1156 | // Otherwise, null, being (void*)0, would print as 0. |
| 1157 | template <> |
| 1158 | struct dynamic::PrintImpl<std::nullptr_t> { |
| 1159 | static void |
| 1160 | print(dynamic const& /* d */, std::ostream& out, std::nullptr_t const&) { |
| 1161 | out << "null" ; |
| 1162 | } |
| 1163 | }; |
| 1164 | template <> |
| 1165 | struct dynamic::PrintImpl<dynamic::ObjectImpl> { |
| 1166 | static void |
| 1167 | print(dynamic const& d, std::ostream& out, dynamic::ObjectImpl const&) { |
| 1168 | d.print_as_pseudo_json(out); |
| 1169 | } |
| 1170 | }; |
| 1171 | template <> |
| 1172 | struct dynamic::PrintImpl<dynamic::Array> { |
| 1173 | static void |
| 1174 | print(dynamic const& d, std::ostream& out, dynamic::Array const&) { |
| 1175 | d.print_as_pseudo_json(out); |
| 1176 | } |
| 1177 | }; |
| 1178 | |
| 1179 | inline void dynamic::print(std::ostream& out) const { |
| 1180 | #define FB_X(T) PrintImpl<T>::print(*this, out, *getAddress<T>()) |
| 1181 | FB_DYNAMIC_APPLY(type_, FB_X); |
| 1182 | #undef FB_X |
| 1183 | } |
| 1184 | |
| 1185 | inline std::ostream& operator<<(std::ostream& out, dynamic const& d) { |
| 1186 | d.print(out); |
| 1187 | return out; |
| 1188 | } |
| 1189 | |
| 1190 | ////////////////////////////////////////////////////////////////////// |
| 1191 | |
| 1192 | // Secialization of FormatValue so dynamic objects can be formatted |
| 1193 | template <> |
| 1194 | class FormatValue<dynamic> { |
| 1195 | public: |
| 1196 | explicit FormatValue(const dynamic& val) : val_(val) {} |
| 1197 | |
| 1198 | template <class FormatCallback> |
| 1199 | void format(FormatArg& arg, FormatCallback& cb) const { |
| 1200 | switch (val_.type()) { |
| 1201 | case dynamic::NULLT: |
| 1202 | FormatValue<std::nullptr_t>(nullptr).format(arg, cb); |
| 1203 | break; |
| 1204 | case dynamic::BOOL: |
| 1205 | FormatValue<bool>(val_.asBool()).format(arg, cb); |
| 1206 | break; |
| 1207 | case dynamic::INT64: |
| 1208 | FormatValue<int64_t>(val_.asInt()).format(arg, cb); |
| 1209 | break; |
| 1210 | case dynamic::STRING: |
| 1211 | FormatValue<std::string>(val_.asString()).format(arg, cb); |
| 1212 | break; |
| 1213 | case dynamic::DOUBLE: |
| 1214 | FormatValue<double>(val_.asDouble()).format(arg, cb); |
| 1215 | break; |
| 1216 | case dynamic::ARRAY: |
| 1217 | FormatValue(val_.at(arg.splitIntKey())).format(arg, cb); |
| 1218 | break; |
| 1219 | case dynamic::OBJECT: |
| 1220 | FormatValue(val_.at(arg.splitKey().toString())).format(arg, cb); |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | private: |
| 1226 | const dynamic& val_; |
| 1227 | }; |
| 1228 | |
| 1229 | template <class V> |
| 1230 | class FormatValue<detail::DefaultValueWrapper<dynamic, V>> { |
| 1231 | public: |
| 1232 | explicit FormatValue(const detail::DefaultValueWrapper<dynamic, V>& val) |
| 1233 | : val_(val) {} |
| 1234 | |
| 1235 | template <class FormatCallback> |
| 1236 | void format(FormatArg& arg, FormatCallback& cb) const { |
| 1237 | auto& c = val_.container; |
| 1238 | switch (c.type()) { |
| 1239 | case dynamic::NULLT: |
| 1240 | case dynamic::BOOL: |
| 1241 | case dynamic::INT64: |
| 1242 | case dynamic::STRING: |
| 1243 | case dynamic::DOUBLE: |
| 1244 | FormatValue<dynamic>(c).format(arg, cb); |
| 1245 | break; |
| 1246 | case dynamic::ARRAY: { |
| 1247 | int key = arg.splitIntKey(); |
| 1248 | if (key >= 0 && size_t(key) < c.size()) { |
| 1249 | FormatValue<dynamic>(c.at(key)).format(arg, cb); |
| 1250 | } else { |
| 1251 | FormatValue<V>(val_.defaultValue).format(arg, cb); |
| 1252 | } |
| 1253 | break; |
| 1254 | } |
| 1255 | case dynamic::OBJECT: { |
| 1256 | auto pos = c.find(arg.splitKey()); |
| 1257 | if (pos != c.items().end()) { |
| 1258 | FormatValue<dynamic>(pos->second).format(arg, cb); |
| 1259 | } else { |
| 1260 | FormatValue<V>(val_.defaultValue).format(arg, cb); |
| 1261 | } |
| 1262 | break; |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | private: |
| 1268 | const detail::DefaultValueWrapper<dynamic, V>& val_; |
| 1269 | }; |
| 1270 | |
| 1271 | } // namespace folly |
| 1272 | |
| 1273 | #undef FB_DYNAMIC_APPLY |
| 1274 | |