2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_DYNAMIC_INL_H_
18 #define FOLLY_DYNAMIC_INL_H_
21 #include <boost/iterator/iterator_adaptor.hpp>
22 #include <boost/iterator/iterator_facade.hpp>
23 #include "folly/Likely.h"
24 #include "folly/Conv.h"
25 #include "folly/Format.h"
27 //////////////////////////////////////////////////////////////////////
32 struct hash< ::folly::dynamic> {
33 size_t operator()(::folly::dynamic const& d) const {
40 //////////////////////////////////////////////////////////////////////
42 // This is a higher-order preprocessor macro to aid going from runtime
43 // types to the compile time type system.
44 #define FB_DYNAMIC_APPLY(type, apply) do { \
46 case NULLT: apply(void*); break; \
47 case ARRAY: apply(Array); break; \
48 case BOOL: apply(bool); break; \
49 case DOUBLE: apply(double); break; \
50 case INT64: apply(int64_t); break; \
51 case OBJECT: apply(ObjectImpl); break; \
52 case STRING: apply(fbstring); break; \
53 default: CHECK(0); abort(); \
57 //////////////////////////////////////////////////////////////////////
61 struct TypeError : std::runtime_error {
62 explicit TypeError(const std::string& expected, dynamic::Type actual)
63 : std::runtime_error(to<std::string>("TypeError: expected dynamic "
64 "type `", expected, '\'', ", but had type `",
65 dynamic::typeName(actual), '\''))
67 explicit TypeError(const std::string& expected,
68 dynamic::Type actual1, dynamic::Type actual2)
69 : std::runtime_error(to<std::string>("TypeError: expected dynamic "
70 "types `", expected, '\'', ", but had types `",
71 dynamic::typeName(actual1), "' and `", dynamic::typeName(actual2),
77 //////////////////////////////////////////////////////////////////////
81 // This helper is used in destroy() to be able to run destructors on
82 // types like "int64_t" without a compiler error.
84 template<class T> static void destroy(T* t) { t->~T(); }
88 * The enable_if junk here is necessary to avoid ambiguous
89 * conversions relating to bool and double when you implicitly
90 * convert an int or long to a dynamic.
92 template<class T, class Enable = void> struct ConversionHelper;
94 struct ConversionHelper<
96 typename std::enable_if<
97 std::is_integral<T>::value && !std::is_same<T,bool>::value
100 typedef int64_t type;
103 struct ConversionHelper<
105 typename std::enable_if<
106 (!std::is_integral<T>::value || std::is_same<T,bool>::value) &&
107 !std::is_same<T,std::nullptr_t>::value
113 struct ConversionHelper<
115 typename std::enable_if<
116 std::is_same<T,std::nullptr_t>::value
123 * Helper for implementing numeric conversions in operators on
124 * numbers. Just promotes to double when one of the arguments is
125 * double, or throws if either is not a numeric type.
127 template<template<class> class Op>
128 dynamic numericOp(dynamic const& a, dynamic const& b) {
129 if (!a.isNumber() || !b.isNumber()) {
130 throw TypeError("numeric", a.type(), b.type());
132 if (a.type() != b.type()) {
133 auto& integ = a.isInt() ? a : b;
134 auto& nonint = a.isInt() ? b : a;
135 return Op<double>()(to<double>(integ.asInt()), nonint.asDouble());
138 return Op<double>()(a.asDouble(), b.asDouble());
140 return Op<int64_t>()(a.asInt(), b.asInt());
145 //////////////////////////////////////////////////////////////////////
148 * We're doing this instead of a simple member typedef to avoid the
149 * undefined behavior of parameterizing std::unordered_map<> with an
152 * Note: Later we may add separate order tracking here (a multi-index
155 struct dynamic::ObjectImpl : std::unordered_map<dynamic, dynamic> {};
157 //////////////////////////////////////////////////////////////////////
159 // Helper object for creating objects conveniently. See object and
160 // the dynamic::dynamic(ObjectMaker&&) ctor.
161 struct dynamic::ObjectMaker {
162 friend struct dynamic;
164 explicit ObjectMaker() : val_(dynamic::object) {}
165 explicit ObjectMaker(dynamic const& key, dynamic val)
166 : val_(dynamic::object)
168 val_.insert(key, std::move(val));
170 explicit ObjectMaker(dynamic&& key, dynamic val)
171 : val_(dynamic::object)
173 val_.insert(std::move(key), std::move(val));
176 // Make sure no one tries to save one of these into an lvalue with
177 // auto or anything like that.
178 ObjectMaker(ObjectMaker&&) = default;
179 ObjectMaker(ObjectMaker const&) = delete;
180 ObjectMaker& operator=(ObjectMaker const&) = delete;
181 ObjectMaker& operator=(ObjectMaker&&) = delete;
183 // These return rvalue-references instead of lvalue-references to allow
184 // constructs like this to moved instead of copied:
185 // dynamic a = dynamic::object("a", "b")("c", "d")
186 ObjectMaker&& operator()(dynamic const& key, dynamic val) {
187 val_.insert(key, std::move(val));
188 return std::move(*this);
191 ObjectMaker&& operator()(dynamic&& key, dynamic val) {
192 val_.insert(std::move(key), std::move(val));
193 return std::move(*this);
200 // This looks like a case for perfect forwarding, but our use of
201 // std::initializer_list for constructing dynamic arrays makes it less
202 // functional than doing this manually.
203 inline dynamic::ObjectMaker dynamic::object() { return ObjectMaker(); }
204 inline dynamic::ObjectMaker dynamic::object(dynamic&& a, dynamic&& b) {
205 return ObjectMaker(std::move(a), std::move(b));
207 inline dynamic::ObjectMaker dynamic::object(dynamic const& a, dynamic&& b) {
208 return ObjectMaker(a, std::move(b));
210 inline dynamic::ObjectMaker dynamic::object(dynamic&& a, dynamic const& b) {
211 return ObjectMaker(std::move(a), b);
213 inline dynamic::ObjectMaker
214 dynamic::object(dynamic const& a, dynamic const& b) {
215 return ObjectMaker(a, b);
218 //////////////////////////////////////////////////////////////////////
220 struct dynamic::const_item_iterator
221 : boost::iterator_adaptor<dynamic::const_item_iterator,
222 dynamic::ObjectImpl::const_iterator> {
223 /* implicit */ const_item_iterator(base_type b) : iterator_adaptor_(b) { }
226 friend class boost::iterator_core_access;
229 struct dynamic::const_key_iterator
230 : boost::iterator_adaptor<dynamic::const_key_iterator,
231 dynamic::ObjectImpl::const_iterator,
233 /* implicit */ const_key_iterator(base_type b) : iterator_adaptor_(b) { }
236 dynamic const& dereference() const {
237 return base_reference()->first;
239 friend class boost::iterator_core_access;
242 struct dynamic::const_value_iterator
243 : boost::iterator_adaptor<dynamic::const_value_iterator,
244 dynamic::ObjectImpl::const_iterator,
246 /* implicit */ const_value_iterator(base_type b) : iterator_adaptor_(b) { }
249 dynamic const& dereference() const {
250 return base_reference()->second;
252 friend class boost::iterator_core_access;
255 //////////////////////////////////////////////////////////////////////
257 inline dynamic::dynamic(ObjectMaker (*)())
260 new (getAddress<ObjectImpl>()) ObjectImpl();
263 inline dynamic::dynamic(StringPiece s)
266 new (&u_.string) fbstring(s.data(), s.size());
269 inline dynamic::dynamic(char const* s)
272 new (&u_.string) fbstring(s);
275 inline dynamic::dynamic(std::string const& s)
278 new (&u_.string) fbstring(s);
281 inline dynamic::dynamic(fbstring const& s)
284 new (&u_.string) fbstring(s);
287 inline dynamic::dynamic(fbstring&& s)
290 new (&u_.string) fbstring(std::move(s));
293 inline dynamic::dynamic(std::initializer_list<dynamic> il)
296 new (&u_.array) Array(il.begin(), il.end());
299 inline dynamic::dynamic(ObjectMaker&& maker)
302 new (getAddress<ObjectImpl>())
303 ObjectImpl(std::move(*maker.val_.getAddress<ObjectImpl>()));
306 inline dynamic::dynamic(dynamic const& o)
312 inline dynamic::dynamic(dynamic&& o)
315 *this = std::move(o);
318 inline dynamic::~dynamic() { destroy(); }
321 dynamic::dynamic(T t) {
322 typedef typename detail::ConversionHelper<T>::type U;
323 type_ = TypeInfo<U>::type;
324 new (getAddress<U>()) U(std::move(t));
327 template<class Iterator>
328 dynamic::dynamic(Iterator first, Iterator last)
331 new (&u_.array) Array(first, last);
334 //////////////////////////////////////////////////////////////////////
336 inline dynamic::const_iterator dynamic::begin() const {
337 return get<Array>().begin();
339 inline dynamic::const_iterator dynamic::end() const {
340 return get<Array>().end();
344 struct dynamic::IterableProxy {
345 typedef It const_iterator;
346 typedef typename It::value_type value_type;
348 /* implicit */ IterableProxy(const dynamic::ObjectImpl* o) : o_(o) { }
359 const dynamic::ObjectImpl* o_;
362 inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys()
364 return &(get<ObjectImpl>());
367 inline dynamic::IterableProxy<dynamic::const_value_iterator> dynamic::values()
369 return &(get<ObjectImpl>());
372 inline dynamic::IterableProxy<dynamic::const_item_iterator> dynamic::items()
374 return &(get<ObjectImpl>());
377 inline bool dynamic::isString() const { return get_nothrow<fbstring>(); }
378 inline bool dynamic::isObject() const { return get_nothrow<ObjectImpl>(); }
379 inline bool dynamic::isBool() const { return get_nothrow<bool>(); }
380 inline bool dynamic::isArray() const { return get_nothrow<Array>(); }
381 inline bool dynamic::isDouble() const { return get_nothrow<double>(); }
382 inline bool dynamic::isInt() const { return get_nothrow<int64_t>(); }
383 inline bool dynamic::isNull() const { return get_nothrow<void*>(); }
384 inline bool dynamic::isNumber() const { return isInt() || isDouble(); }
386 inline dynamic::Type dynamic::type() const {
390 inline fbstring dynamic::asString() const { return asImpl<fbstring>(); }
391 inline double dynamic::asDouble() const { return asImpl<double>(); }
392 inline int64_t dynamic::asInt() const { return asImpl<int64_t>(); }
393 inline bool dynamic::asBool() const { return asImpl<bool>(); }
395 inline const char* dynamic::data() const { return get<fbstring>().data(); }
396 inline const char* dynamic::c_str() const { return get<fbstring>().c_str(); }
399 struct dynamic::CompareOp {
400 static bool comp(T const& a, T const& b) { return a < b; }
403 struct dynamic::CompareOp<dynamic::ObjectImpl> {
404 static bool comp(ObjectImpl const& a, ObjectImpl const& b) {
405 // This code never executes; it is just here for the compiler.
410 inline bool dynamic::operator<(dynamic const& o) const {
411 if (UNLIKELY(type_ == OBJECT || o.type_ == OBJECT)) {
412 throw TypeError("object", type_);
414 if (type_ != o.type_) {
415 return type_ < o.type_;
418 #define FB_X(T) return CompareOp<T>::comp(*getAddress<T>(), \
420 FB_DYNAMIC_APPLY(type_, FB_X);
424 inline bool dynamic::operator==(dynamic const& o) const {
425 if (type() != o.type()) {
426 if (isNumber() && o.isNumber()) {
427 auto& integ = isInt() ? *this : o;
428 auto& doubl = isInt() ? o : *this;
429 return integ.asInt() == doubl.asDouble();
434 #define FB_X(T) return *getAddress<T>() == *o.getAddress<T>();
435 FB_DYNAMIC_APPLY(type_, FB_X);
439 inline dynamic& dynamic::operator+=(dynamic const& o) {
440 if (type() == STRING && o.type() == STRING) {
441 *getAddress<fbstring>() += *o.getAddress<fbstring>();
444 *this = detail::numericOp<std::plus>(*this, o);
448 inline dynamic& dynamic::operator-=(dynamic const& o) {
449 *this = detail::numericOp<std::minus>(*this, o);
453 inline dynamic& dynamic::operator*=(dynamic const& o) {
454 *this = detail::numericOp<std::multiplies>(*this, o);
458 inline dynamic& dynamic::operator/=(dynamic const& o) {
459 *this = detail::numericOp<std::divides>(*this, o);
463 #define FB_DYNAMIC_INTEGER_OP(op) \
464 inline dynamic& dynamic::operator op(dynamic const& o) { \
465 if (!isInt() || !o.isInt()) { \
466 throw TypeError("int64", type(), o.type()); \
468 *getAddress<int64_t>() op o.asInt(); \
472 FB_DYNAMIC_INTEGER_OP(%=)
473 FB_DYNAMIC_INTEGER_OP(|=)
474 FB_DYNAMIC_INTEGER_OP(&=)
475 FB_DYNAMIC_INTEGER_OP(^=)
477 #undef FB_DYNAMIC_INTEGER_OP
479 inline dynamic& dynamic::operator++() {
484 inline dynamic& dynamic::operator--() {
489 inline dynamic& dynamic::operator=(dynamic const& o) {
492 #define FB_X(T) new (getAddress<T>()) T(*o.getAddress<T>())
493 FB_DYNAMIC_APPLY(o.type_, FB_X);
500 inline dynamic& dynamic::operator=(dynamic&& o) {
503 #define FB_X(T) new (getAddress<T>()) T(std::move(*o.getAddress<T>()))
504 FB_DYNAMIC_APPLY(o.type_, FB_X);
511 inline dynamic& dynamic::operator[](dynamic const& k) {
512 if (!isObject() && !isArray()) {
513 throw TypeError("object/array", type());
518 auto& obj = get<ObjectImpl>();
519 auto ret = obj.insert({k, nullptr});
520 return ret.first->second;
523 inline dynamic const& dynamic::operator[](dynamic const& idx) const {
527 inline dynamic dynamic::getDefault(const dynamic& k, const dynamic& v) const {
528 auto& obj = get<ObjectImpl>();
529 auto it = obj.find(k);
530 return it == obj.end() ? v : it->second;
533 inline dynamic&& dynamic::getDefault(const dynamic& k, dynamic&& v) const {
534 auto& obj = get<ObjectImpl>();
535 auto it = obj.find(k);
536 if (it != obj.end()) {
543 template<class K, class V> inline dynamic& dynamic::setDefault(K&& k, V&& v) {
544 auto& obj = get<ObjectImpl>();
545 return obj.insert(std::make_pair(std::forward<K>(k),
546 std::forward<V>(v))).first->second;
549 inline dynamic* dynamic::get_ptr(dynamic const& idx) {
550 return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx));
553 inline const dynamic* dynamic::get_ptr(dynamic const& idx) const {
554 if (auto* parray = get_nothrow<Array>()) {
556 throw TypeError("int64", idx.type());
558 if (idx >= parray->size()) {
561 return &(*parray)[idx.asInt()];
562 } else if (auto* pobject = get_nothrow<ObjectImpl>()) {
563 auto it = pobject->find(idx);
564 if (it == pobject->end()) {
569 throw TypeError("object/array", type());
573 inline dynamic& dynamic::at(dynamic const& idx) {
574 return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx));
577 inline dynamic const& dynamic::at(dynamic const& idx) const {
578 if (auto* parray = get_nothrow<Array>()) {
580 throw TypeError("int64", idx.type());
582 if (idx >= parray->size()) {
583 throw std::out_of_range("out of range in dynamic array");
585 return (*parray)[idx.asInt()];
586 } else if (auto* pobject = get_nothrow<ObjectImpl>()) {
587 auto it = pobject->find(idx);
588 if (it == pobject->end()) {
589 throw std::out_of_range(to<std::string>(
590 "couldn't find key ", idx.asString(), " in dynamic object"));
594 throw TypeError("object/array", type());
598 inline bool dynamic::empty() const {
605 inline std::size_t dynamic::size() const {
606 if (auto* ar = get_nothrow<Array>()) {
609 if (auto* obj = get_nothrow<ObjectImpl>()) {
612 if (auto* str = get_nothrow<fbstring>()) {
615 throw TypeError("array/object", type());
618 inline std::size_t dynamic::count(dynamic const& key) const {
619 return find(key) != items().end();
622 inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
623 return get<ObjectImpl>().find(key);
626 template<class K, class V> inline void dynamic::insert(K&& key, V&& val) {
627 auto& obj = get<ObjectImpl>();
628 auto rv = obj.insert(std::make_pair(std::forward<K>(key),
629 std::forward<V>(val)));
631 // note, the second use of std:forward<V>(val) is only correct
632 // if the first one did not result in a move. obj[key] = val
633 // would be preferrable but doesn't compile because dynamic
634 // is (intentionally) not default constructable
635 rv.first->second = std::forward<V>(val);
639 inline std::size_t dynamic::erase(dynamic const& key) {
640 auto& obj = get<ObjectImpl>();
641 return obj.erase(key);
644 inline dynamic::const_iterator dynamic::erase(const_iterator it) {
645 auto& arr = get<Array>();
646 // std::vector doesn't have an erase method that works on const iterators,
647 // even though the standard says it should, so this hack converts to a
648 // non-const iterator before calling erase.
649 return get<Array>().erase(arr.begin() + (it - arr.begin()));
652 inline dynamic::const_iterator
653 dynamic::erase(const_iterator first, const_iterator last) {
654 auto& arr = get<Array>();
655 return get<Array>().erase(
656 arr.begin() + (first - arr.begin()),
657 arr.begin() + (last - arr.begin()));
660 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator it) {
661 return const_key_iterator(get<ObjectImpl>().erase(it.base()));
664 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator first,
665 const_key_iterator last) {
666 return const_key_iterator(get<ObjectImpl>().erase(first.base(),
670 inline dynamic::const_value_iterator dynamic::erase(const_value_iterator it) {
671 return const_value_iterator(get<ObjectImpl>().erase(it.base()));
674 inline dynamic::const_value_iterator dynamic::erase(const_value_iterator first,
675 const_value_iterator last) {
676 return const_value_iterator(get<ObjectImpl>().erase(first.base(),
680 inline dynamic::const_item_iterator dynamic::erase(const_item_iterator it) {
681 return const_item_iterator(get<ObjectImpl>().erase(it.base()));
684 inline dynamic::const_item_iterator dynamic::erase(const_item_iterator first,
685 const_item_iterator last) {
686 return const_item_iterator(get<ObjectImpl>().erase(first.base(),
690 inline void dynamic::resize(std::size_t sz, dynamic const& c) {
691 auto& array = get<Array>();
695 inline void dynamic::push_back(dynamic const& v) {
696 auto& array = get<Array>();
700 inline void dynamic::push_back(dynamic&& v) {
701 auto& array = get<Array>();
702 array.push_back(std::move(v));
705 inline void dynamic::pop_back() {
706 auto& array = get<Array>();
710 inline std::size_t dynamic::hash() const {
715 throw TypeError("not null/object/array", type());
717 return std::hash<int64_t>()(asInt());
719 return std::hash<double>()(asDouble());
721 return std::hash<bool>()(asBool());
723 return std::hash<fbstring>()(asString());
729 //////////////////////////////////////////////////////////////////////
731 template<class T> struct dynamic::TypeInfo {
732 static char const name[];
733 static Type const type;
736 #define FB_DEC_TYPE(T) \
737 template<> char const dynamic::TypeInfo<T>::name[]; \
738 template<> dynamic::Type const dynamic::TypeInfo<T>::type
742 FB_DEC_TYPE(fbstring);
743 FB_DEC_TYPE(dynamic::Array);
745 FB_DEC_TYPE(int64_t);
746 FB_DEC_TYPE(dynamic::ObjectImpl);
751 T dynamic::asImpl() const {
753 case INT64: return to<T>(*get_nothrow<int64_t>());
754 case DOUBLE: return to<T>(*get_nothrow<double>());
755 case BOOL: return to<T>(*get_nothrow<bool>());
756 case STRING: return to<T>(*get_nothrow<fbstring>());
758 throw TypeError("int/double/bool/string", type());
762 // Return a T* to our type, or null if we're not that type.
764 T* dynamic::get_nothrow() {
765 if (type_ != TypeInfo<T>::type) {
768 return getAddress<T>();
772 T const* dynamic::get_nothrow() const {
773 return const_cast<dynamic*>(this)->get_nothrow<T>();
776 // Return T* for where we can put a T, without type checking. (Memory
777 // might be uninitialized, even.)
779 T* dynamic::getAddress() {
780 return GetAddrImpl<T>::get(u_);
784 T const* dynamic::getAddress() const {
785 return const_cast<dynamic*>(this)->getAddress<T>();
788 template<class T> struct dynamic::GetAddrImpl {};
789 template<> struct dynamic::GetAddrImpl<void*> {
790 static void** get(Data& d) { return &d.nul; }
792 template<> struct dynamic::GetAddrImpl<dynamic::Array> {
793 static Array* get(Data& d) { return &d.array; }
795 template<> struct dynamic::GetAddrImpl<bool> {
796 static bool* get(Data& d) { return &d.boolean; }
798 template<> struct dynamic::GetAddrImpl<int64_t> {
799 static int64_t* get(Data& d) { return &d.integer; }
801 template<> struct dynamic::GetAddrImpl<double> {
802 static double* get(Data& d) { return &d.doubl; }
804 template<> struct dynamic::GetAddrImpl<fbstring> {
805 static fbstring* get(Data& d) { return &d.string; }
807 template<> struct dynamic::GetAddrImpl<dynamic::ObjectImpl> {
808 static_assert(sizeof(ObjectImpl) <= sizeof(Data::objectBuffer),
809 "In your implementation, std::unordered_map<> apparently takes different"
810 " amount of space depending on its template parameters. This is "
811 "weird. Make objectBuffer bigger if you want to compile dynamic.");
813 static ObjectImpl* get(Data& d) {
814 void* data = &d.objectBuffer;
815 return static_cast<ObjectImpl*>(data);
821 if (auto* p = get_nothrow<T>()) {
824 throw TypeError(TypeInfo<T>::name, type());
828 T const& dynamic::get() const {
829 return const_cast<dynamic*>(this)->get<T>();
832 inline char const* dynamic::typeName(Type t) {
833 #define FB_X(T) return TypeInfo<T>::name
834 FB_DYNAMIC_APPLY(t, FB_X);
838 inline void dynamic::destroy() {
839 // This short-circuit speeds up some microbenchmarks.
840 if (type_ == NULLT) return;
842 #define FB_X(T) detail::Destroy::destroy(getAddress<T>())
843 FB_DYNAMIC_APPLY(type_, FB_X);
849 //////////////////////////////////////////////////////////////////////
852 * Helper for implementing operator<<. Throws if the type shouldn't
856 struct dynamic::PrintImpl {
857 static void print(dynamic const&, std::ostream& out, T const& t) {
862 struct dynamic::PrintImpl<dynamic::ObjectImpl> {
863 static void print(dynamic const& d,
865 dynamic::ObjectImpl const&) {
866 d.print_as_pseudo_json(out);
870 struct dynamic::PrintImpl<dynamic::Array> {
871 static void print(dynamic const& d,
873 dynamic::Array const&) {
874 d.print_as_pseudo_json(out);
878 inline void dynamic::print(std::ostream& out) const {
879 #define FB_X(T) PrintImpl<T>::print(*this, out, *getAddress<T>())
880 FB_DYNAMIC_APPLY(type_, FB_X);
884 inline std::ostream& operator<<(std::ostream& out, dynamic const& d) {
889 //////////////////////////////////////////////////////////////////////
891 // Secialization of FormatValue so dynamic objects can be formatted
893 class FormatValue<dynamic> {
895 explicit FormatValue(const dynamic& val) : val_(val) { }
897 template <class FormatCallback>
898 void format(FormatArg& arg, FormatCallback& cb) const {
899 switch (val_.type()) {
901 FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
904 FormatValue<bool>(val_.asBool()).format(arg, cb);
907 FormatValue<int64_t>(val_.asInt()).format(arg, cb);
909 case dynamic::STRING:
910 FormatValue<fbstring>(val_.asString()).format(arg, cb);
912 case dynamic::DOUBLE:
913 FormatValue<double>(val_.asDouble()).format(arg, cb);
916 FormatValue(val_.at(arg.splitIntKey())).format(arg, cb);
918 case dynamic::OBJECT:
919 FormatValue(val_.at(arg.splitKey().toFbstring())).format(arg, cb);
929 class FormatValue<detail::DefaultValueWrapper<dynamic, V>> {
931 explicit FormatValue(
932 const detail::DefaultValueWrapper<dynamic, V>& val)
935 template <class FormatCallback>
936 void format(FormatArg& arg, FormatCallback& cb) const {
937 auto& c = val_.container;
942 case dynamic::STRING:
943 case dynamic::DOUBLE:
944 FormatValue<dynamic>(c).format(arg, cb);
948 int key = arg.splitIntKey();
949 if (key >= 0 && key < c.size()) {
950 FormatValue<dynamic>(c.at(key)).format(arg, cb);
952 FormatValue<V>(val_.defaultValue).format(arg, cb);
956 case dynamic::OBJECT:
958 auto pos = c.find(arg.splitKey());
959 if (pos != c.items().end()) {
960 FormatValue<dynamic>(pos->second).format(arg, cb);
962 FormatValue<V>(val_.defaultValue).format(arg, cb);
970 const detail::DefaultValueWrapper<dynamic, V>& val_;
975 #undef FB_DYNAMIC_APPLY