Avoid std::initializer_list ctor in folly::dynamic::array
[folly.git] / folly / dynamic-inl.h
1 /*
2  * Copyright 2016 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
17 #pragma once
18
19 #include <functional>
20 #include <boost/iterator/iterator_adaptor.hpp>
21 #include <boost/iterator/iterator_facade.hpp>
22 #include <folly/Likely.h>
23 #include <folly/Conv.h>
24 #include <folly/Format.h>
25
26 //////////////////////////////////////////////////////////////////////
27
28 namespace std {
29
30 template<>
31 struct hash< ::folly::dynamic> {
32   size_t operator()(::folly::dynamic const& d) const {
33     return d.hash();
34   }
35 };
36
37 }
38
39 //////////////////////////////////////////////////////////////////////
40
41 // This is a higher-order preprocessor macro to aid going from runtime
42 // types to the compile time type system.
43 #define FB_DYNAMIC_APPLY(type, apply) \
44   do {                                \
45     switch ((type)) {                 \
46       case NULLT:                     \
47         apply(void*);                 \
48         break;                        \
49       case ARRAY:                     \
50         apply(Array);                 \
51         break;                        \
52       case BOOL:                      \
53         apply(bool);                  \
54         break;                        \
55       case DOUBLE:                    \
56         apply(double);                \
57         break;                        \
58       case INT64:                     \
59         apply(int64_t);               \
60         break;                        \
61       case OBJECT:                    \
62         apply(ObjectImpl);            \
63         break;                        \
64       case STRING:                    \
65         apply(std::string);           \
66         break;                        \
67       default:                        \
68         CHECK(0);                     \
69         abort();                      \
70     }                                 \
71   } while (0)
72
73 //////////////////////////////////////////////////////////////////////
74
75 namespace folly {
76
77 struct TypeError : std::runtime_error {
78   explicit TypeError(const std::string& expected, dynamic::Type actual);
79   explicit TypeError(const std::string& expected,
80     dynamic::Type actual1, dynamic::Type actual2);
81   ~TypeError();
82 };
83
84
85 //////////////////////////////////////////////////////////////////////
86
87 namespace detail {
88
89   // This helper is used in destroy() to be able to run destructors on
90   // types like "int64_t" without a compiler error.
91   struct Destroy {
92     template<class T> static void destroy(T* t) { t->~T(); }
93   };
94
95   /*
96    * The enable_if junk here is necessary to avoid ambiguous
97    * conversions relating to bool and double when you implicitly
98    * convert an int or long to a dynamic.
99    */
100   template<class T, class Enable = void> struct ConversionHelper;
101   template<class T>
102   struct ConversionHelper<
103     T,
104     typename std::enable_if<
105       std::is_integral<T>::value && !std::is_same<T,bool>::value
106     >::type
107   > {
108     typedef int64_t type;
109   };
110   template<class T>
111   struct ConversionHelper<
112     T,
113     typename std::enable_if<
114       (!std::is_integral<T>::value || std::is_same<T,bool>::value) &&
115       !std::is_same<T,std::nullptr_t>::value
116     >::type
117   > {
118     typedef T type;
119   };
120   template<class T>
121   struct ConversionHelper<
122     T,
123     typename std::enable_if<
124       std::is_same<T,std::nullptr_t>::value
125     >::type
126   > {
127     typedef void* type;
128   };
129
130   /*
131    * Helper for implementing numeric conversions in operators on
132    * numbers.  Just promotes to double when one of the arguments is
133    * double, or throws if either is not a numeric type.
134    */
135   template<template<class> class Op>
136   dynamic numericOp(dynamic const& a, dynamic const& b) {
137     if (!a.isNumber() || !b.isNumber()) {
138       throw TypeError("numeric", a.type(), b.type());
139     }
140     if (a.type() != b.type()) {
141       auto& integ  = a.isInt() ? a : b;
142       auto& nonint = a.isInt() ? b : a;
143       return Op<double>()(to<double>(integ.asInt()), nonint.asDouble());
144     }
145     if (a.isDouble()) {
146       return Op<double>()(a.asDouble(), b.asDouble());
147     }
148     return Op<int64_t>()(a.asInt(), b.asInt());
149   }
150
151 }
152
153 //////////////////////////////////////////////////////////////////////
154
155 /*
156  * We're doing this instead of a simple member typedef to avoid the
157  * undefined behavior of parameterizing std::unordered_map<> with an
158  * incomplete type.
159  *
160  * Note: Later we may add separate order tracking here (a multi-index
161  * type of thing.)
162  */
163 struct dynamic::ObjectImpl : std::unordered_map<dynamic, dynamic> {};
164
165 //////////////////////////////////////////////////////////////////////
166
167 // Helper object for creating objects conveniently.  See object and
168 // the dynamic::dynamic(ObjectMaker&&) ctor.
169 struct dynamic::ObjectMaker {
170   friend struct dynamic;
171
172   explicit ObjectMaker() : val_(dynamic::object) {}
173   explicit ObjectMaker(dynamic const& key, dynamic val)
174     : val_(dynamic::object)
175   {
176     val_.insert(key, std::move(val));
177   }
178   explicit ObjectMaker(dynamic&& key, dynamic val)
179     : val_(dynamic::object)
180   {
181     val_.insert(std::move(key), std::move(val));
182   }
183
184   // Make sure no one tries to save one of these into an lvalue with
185   // auto or anything like that.
186   ObjectMaker(ObjectMaker&&) = default;
187   ObjectMaker(ObjectMaker const&) = delete;
188   ObjectMaker& operator=(ObjectMaker const&) = delete;
189   ObjectMaker& operator=(ObjectMaker&&) = delete;
190
191   // These return rvalue-references instead of lvalue-references to allow
192   // constructs like this to moved instead of copied:
193   //  dynamic a = dynamic::object("a", "b")("c", "d")
194   ObjectMaker&& operator()(dynamic const& key, dynamic val) {
195     val_.insert(key, std::move(val));
196     return std::move(*this);
197   }
198
199   ObjectMaker&& operator()(dynamic&& key, dynamic val) {
200     val_.insert(std::move(key), std::move(val));
201     return std::move(*this);
202   }
203
204 private:
205   dynamic val_;
206 };
207
208 inline void dynamic::array(EmptyArrayTag) {}
209
210 template <class... Args>
211 inline dynamic dynamic::array(Args&& ...args) {
212   return dynamic(Array{std::forward<Args>(args)...}, PrivateTag());
213 }
214
215 // This looks like a case for perfect forwarding, but our use of
216 // std::initializer_list for constructing dynamic arrays makes it less
217 // functional than doing this manually.
218
219 // TODO(ott, 10300209): When the initializer_list constructor is gone,
220 // simplify this.
221 inline dynamic::ObjectMaker dynamic::object() { return ObjectMaker(); }
222 inline dynamic::ObjectMaker dynamic::object(dynamic&& a, dynamic&& b) {
223   return ObjectMaker(std::move(a), std::move(b));
224 }
225 inline dynamic::ObjectMaker dynamic::object(dynamic const& a, dynamic&& b) {
226   return ObjectMaker(a, std::move(b));
227 }
228 inline dynamic::ObjectMaker dynamic::object(dynamic&& a, dynamic const& b) {
229   return ObjectMaker(std::move(a), b);
230 }
231 inline dynamic::ObjectMaker
232 dynamic::object(dynamic const& a, dynamic const& b) {
233   return ObjectMaker(a, b);
234 }
235
236 //////////////////////////////////////////////////////////////////////
237
238 struct dynamic::const_item_iterator
239   : boost::iterator_adaptor<dynamic::const_item_iterator,
240                             dynamic::ObjectImpl::const_iterator> {
241   /* implicit */ const_item_iterator(base_type b) : iterator_adaptor_(b) { }
242
243  private:
244   friend class boost::iterator_core_access;
245 };
246
247 struct dynamic::const_key_iterator
248   : boost::iterator_adaptor<dynamic::const_key_iterator,
249                             dynamic::ObjectImpl::const_iterator,
250                             dynamic const> {
251   /* implicit */ const_key_iterator(base_type b) : iterator_adaptor_(b) { }
252
253  private:
254   dynamic const& dereference() const {
255     return base_reference()->first;
256   }
257   friend class boost::iterator_core_access;
258 };
259
260 struct dynamic::const_value_iterator
261   : boost::iterator_adaptor<dynamic::const_value_iterator,
262                             dynamic::ObjectImpl::const_iterator,
263                             dynamic const> {
264   /* implicit */ const_value_iterator(base_type b) : iterator_adaptor_(b) { }
265
266  private:
267   dynamic const& dereference() const {
268     return base_reference()->second;
269   }
270   friend class boost::iterator_core_access;
271 };
272
273 //////////////////////////////////////////////////////////////////////
274
275 inline dynamic::dynamic(void (*)(EmptyArrayTag))
276   : type_(ARRAY)
277 {
278   new (&u_.array) Array();
279 }
280
281 inline dynamic::dynamic(ObjectMaker (*)())
282   : type_(OBJECT)
283 {
284   new (getAddress<ObjectImpl>()) ObjectImpl();
285 }
286
287 inline dynamic::dynamic(StringPiece s)
288   : type_(STRING)
289 {
290   new (&u_.string) std::string(s.data(), s.size());
291 }
292
293 inline dynamic::dynamic(char const* s)
294   : type_(STRING)
295 {
296   new (&u_.string) std::string(s);
297 }
298
299 inline dynamic::dynamic(std::string const& s)
300   : type_(STRING)
301 {
302   new (&u_.string) std::string(s);
303 }
304
305 inline dynamic::dynamic(std::string&& s) : type_(STRING) {
306   new (&u_.string) std::string(std::move(s));
307 }
308
309 inline dynamic::dynamic(std::initializer_list<dynamic> il)
310     : dynamic(Array(std::move(il)), PrivateTag()) {}
311
312 inline dynamic& dynamic::operator=(std::initializer_list<dynamic> il) {
313   (*this) = dynamic(Array(std::move(il)), PrivateTag());
314   return *this;
315 }
316
317 inline dynamic::dynamic(ObjectMaker&& maker)
318   : type_(OBJECT)
319 {
320   new (getAddress<ObjectImpl>())
321     ObjectImpl(std::move(*maker.val_.getAddress<ObjectImpl>()));
322 }
323
324 inline dynamic::dynamic(dynamic const& o)
325   : type_(NULLT)
326 {
327   *this = o;
328 }
329
330 inline dynamic::dynamic(dynamic&& o) noexcept
331   : type_(NULLT)
332 {
333   *this = std::move(o);
334 }
335
336 inline dynamic::~dynamic() noexcept { destroy(); }
337
338 template<class T>
339 dynamic::dynamic(T t) {
340   typedef typename detail::ConversionHelper<T>::type U;
341   type_ = TypeInfo<U>::type;
342   new (getAddress<U>()) U(std::move(t));
343 }
344
345 template<class Iterator>
346 dynamic::dynamic(Iterator first, Iterator last)
347   : type_(ARRAY)
348 {
349   new (&u_.array) Array(first, last);
350 }
351
352 //////////////////////////////////////////////////////////////////////
353
354 inline dynamic::const_iterator dynamic::begin() const {
355   return get<Array>().begin();
356 }
357 inline dynamic::const_iterator dynamic::end() const {
358   return get<Array>().end();
359 }
360
361 template <class It>
362 struct dynamic::IterableProxy {
363   typedef It const_iterator;
364   typedef typename It::value_type value_type;
365
366   /* implicit */ IterableProxy(const dynamic::ObjectImpl* o) : o_(o) { }
367
368   It begin() const {
369     return o_->begin();
370   }
371
372   It end() const {
373     return o_->end();
374   }
375
376  private:
377   const dynamic::ObjectImpl* o_;
378 };
379
380 inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys()
381   const {
382   return &(get<ObjectImpl>());
383 }
384
385 inline dynamic::IterableProxy<dynamic::const_value_iterator> dynamic::values()
386   const {
387   return &(get<ObjectImpl>());
388 }
389
390 inline dynamic::IterableProxy<dynamic::const_item_iterator> dynamic::items()
391   const {
392   return &(get<ObjectImpl>());
393 }
394
395 inline bool dynamic::isString() const {
396   return get_nothrow<std::string>();
397 }
398 inline bool dynamic::isObject() const { return get_nothrow<ObjectImpl>(); }
399 inline bool dynamic::isBool()   const { return get_nothrow<bool>(); }
400 inline bool dynamic::isArray()  const { return get_nothrow<Array>(); }
401 inline bool dynamic::isDouble() const { return get_nothrow<double>(); }
402 inline bool dynamic::isInt()    const { return get_nothrow<int64_t>(); }
403 inline bool dynamic::isNull()   const { return get_nothrow<void*>(); }
404 inline bool dynamic::isNumber() const { return isInt() || isDouble(); }
405
406 inline dynamic::Type dynamic::type() const {
407   return type_;
408 }
409
410 inline std::string dynamic::asString() const {
411   return asImpl<std::string>();
412 }
413 inline double dynamic::asDouble() const {
414   return asImpl<double>();
415 }
416 inline int64_t dynamic::asInt() const {
417   return asImpl<int64_t>();
418 }
419 inline bool dynamic::asBool() const {
420   return asImpl<bool>();
421 }
422
423 inline const std::string& dynamic::getString() const& {
424   return get<std::string>();
425 }
426 inline double          dynamic::getDouble() const& { return get<double>(); }
427 inline int64_t         dynamic::getInt()    const& { return get<int64_t>(); }
428 inline bool            dynamic::getBool()   const& { return get<bool>(); }
429
430 inline std::string& dynamic::getString()& {
431   return get<std::string>();
432 }
433 inline double&   dynamic::getDouble() & { return get<double>(); }
434 inline int64_t&  dynamic::getInt()    & { return get<int64_t>(); }
435 inline bool&     dynamic::getBool()   & { return get<bool>(); }
436
437 inline std::string dynamic::getString()&& {
438   return std::move(get<std::string>());
439 }
440 inline double   dynamic::getDouble() && { return get<double>(); }
441 inline int64_t  dynamic::getInt()    && { return get<int64_t>(); }
442 inline bool     dynamic::getBool()   && { return get<bool>(); }
443
444 inline const char* dynamic::data() const& {
445   return get<std::string>().data();
446 }
447 inline const char* dynamic::c_str() const& {
448   return get<std::string>().c_str();
449 }
450 inline StringPiece dynamic::stringPiece() const {
451   return get<std::string>();
452 }
453
454 template<class T>
455 struct dynamic::CompareOp {
456   static bool comp(T const& a, T const& b) { return a < b; }
457 };
458 template<>
459 struct dynamic::CompareOp<dynamic::ObjectImpl> {
460   static bool comp(ObjectImpl const&, ObjectImpl const&) {
461     // This code never executes; it is just here for the compiler.
462     return false;
463   }
464 };
465
466 inline dynamic& dynamic::operator+=(dynamic const& o) {
467   if (type() == STRING && o.type() == STRING) {
468     *getAddress<std::string>() += *o.getAddress<std::string>();
469     return *this;
470   }
471   *this = detail::numericOp<std::plus>(*this, o);
472   return *this;
473 }
474
475 inline dynamic& dynamic::operator-=(dynamic const& o) {
476   *this = detail::numericOp<std::minus>(*this, o);
477   return *this;
478 }
479
480 inline dynamic& dynamic::operator*=(dynamic const& o) {
481   *this = detail::numericOp<std::multiplies>(*this, o);
482   return *this;
483 }
484
485 inline dynamic& dynamic::operator/=(dynamic const& o) {
486   *this = detail::numericOp<std::divides>(*this, o);
487   return *this;
488 }
489
490 #define FB_DYNAMIC_INTEGER_OP(op)                           \
491   inline dynamic& dynamic::operator op(dynamic const& o) {  \
492     if (!isInt() || !o.isInt()) {                           \
493       throw TypeError("int64", type(), o.type());           \
494     }                                                       \
495     *getAddress<int64_t>() op o.asInt();                    \
496     return *this;                                           \
497   }
498
499 FB_DYNAMIC_INTEGER_OP(%=)
500 FB_DYNAMIC_INTEGER_OP(|=)
501 FB_DYNAMIC_INTEGER_OP(&=)
502 FB_DYNAMIC_INTEGER_OP(^=)
503
504 #undef FB_DYNAMIC_INTEGER_OP
505
506 inline dynamic& dynamic::operator++() {
507   ++get<int64_t>();
508   return *this;
509 }
510
511 inline dynamic& dynamic::operator--() {
512   --get<int64_t>();
513   return *this;
514 }
515
516 inline dynamic const& dynamic::operator[](dynamic const& idx) const& {
517   return at(idx);
518 }
519
520 inline dynamic dynamic::operator[](dynamic const& idx) && {
521   return std::move((*this)[idx]);
522 }
523
524 template<class K, class V> inline dynamic& dynamic::setDefault(K&& k, V&& v) {
525   auto& obj = get<ObjectImpl>();
526   return obj.insert(std::make_pair(std::forward<K>(k),
527                                    std::forward<V>(v))).first->second;
528 }
529
530 inline dynamic* dynamic::get_ptr(dynamic const& idx) & {
531   return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx));
532 }
533
534 inline dynamic& dynamic::at(dynamic const& idx) & {
535   return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx));
536 }
537
538 inline dynamic dynamic::at(dynamic const& idx) && {
539   return std::move(at(idx));
540 }
541
542 inline bool dynamic::empty() const {
543   if (isNull()) {
544     return true;
545   }
546   return !size();
547 }
548
549 inline std::size_t dynamic::count(dynamic const& key) const {
550   return find(key) != items().end();
551 }
552
553 inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
554   return get<ObjectImpl>().find(key);
555 }
556
557 template<class K, class V> inline void dynamic::insert(K&& key, V&& val) {
558   auto& obj = get<ObjectImpl>();
559   auto rv = obj.insert({ std::forward<K>(key), nullptr });
560   rv.first->second = std::forward<V>(val);
561 }
562
563 inline void dynamic::update(const dynamic& mergeObj) {
564   if (!isObject() || !mergeObj.isObject()) {
565     throw TypeError("object", type(), mergeObj.type());
566   }
567
568   for (const auto& pair : mergeObj.items()) {
569     (*this)[pair.first] = pair.second;
570   }
571 }
572
573 inline void dynamic::update_missing(const dynamic& mergeObj1) {
574   if (!isObject() || !mergeObj1.isObject()) {
575     throw TypeError("object", type(), mergeObj1.type());
576   }
577
578   // Only add if not already there
579   for (const auto& pair : mergeObj1.items()) {
580     if ((*this).find(pair.first) == (*this).items().end()) {
581       (*this)[pair.first] = pair.second;
582     }
583   }
584 }
585
586 inline dynamic dynamic::merge(
587     const dynamic& mergeObj1,
588     const dynamic& mergeObj2) {
589
590   // No checks on type needed here because they are done in update_missing
591   // Note that we do update_missing here instead of update() because
592   // it will prevent the extra writes that would occur with update()
593   auto ret = mergeObj2;
594   ret.update_missing(mergeObj1);
595   return ret;
596 }
597
598 inline std::size_t dynamic::erase(dynamic const& key) {
599   auto& obj = get<ObjectImpl>();
600   return obj.erase(key);
601 }
602
603 inline dynamic::const_iterator dynamic::erase(const_iterator it) {
604   auto& arr = get<Array>();
605   // std::vector doesn't have an erase method that works on const iterators,
606   // even though the standard says it should, so this hack converts to a
607   // non-const iterator before calling erase.
608   return get<Array>().erase(arr.begin() + (it - arr.begin()));
609 }
610
611 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator it) {
612   return const_key_iterator(get<ObjectImpl>().erase(it.base()));
613 }
614
615 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator first,
616                                                   const_key_iterator last) {
617   return const_key_iterator(get<ObjectImpl>().erase(first.base(),
618                                                     last.base()));
619 }
620
621 inline dynamic::const_value_iterator dynamic::erase(const_value_iterator it) {
622   return const_value_iterator(get<ObjectImpl>().erase(it.base()));
623 }
624
625 inline dynamic::const_value_iterator dynamic::erase(const_value_iterator first,
626                                                     const_value_iterator last) {
627   return const_value_iterator(get<ObjectImpl>().erase(first.base(),
628                                                       last.base()));
629 }
630
631 inline dynamic::const_item_iterator dynamic::erase(const_item_iterator it) {
632   return const_item_iterator(get<ObjectImpl>().erase(it.base()));
633 }
634
635 inline dynamic::const_item_iterator dynamic::erase(const_item_iterator first,
636                                                    const_item_iterator last) {
637   return const_item_iterator(get<ObjectImpl>().erase(first.base(),
638                                                      last.base()));
639 }
640
641 inline void dynamic::resize(std::size_t sz, dynamic const& c) {
642   auto& arr = get<Array>();
643   arr.resize(sz, c);
644 }
645
646 inline void dynamic::push_back(dynamic const& v) {
647   auto& arr = get<Array>();
648   arr.push_back(v);
649 }
650
651 inline void dynamic::push_back(dynamic&& v) {
652   auto& arr = get<Array>();
653   arr.push_back(std::move(v));
654 }
655
656 inline void dynamic::pop_back() {
657   auto& arr = get<Array>();
658   arr.pop_back();
659 }
660
661 //////////////////////////////////////////////////////////////////////
662
663 inline dynamic::dynamic(Array&& r, PrivateTag) : type_(ARRAY) {
664   new (&u_.array) Array(std::move(r));
665 }
666
667 #define FOLLY_DYNAMIC_DEC_TYPEINFO(T, str, val) \
668   template <> struct dynamic::TypeInfo<T> { \
669     static constexpr const char* name = str; \
670     static constexpr dynamic::Type type = val; \
671   }; \
672   //
673
674 FOLLY_DYNAMIC_DEC_TYPEINFO(void*,               "null",    dynamic::NULLT)
675 FOLLY_DYNAMIC_DEC_TYPEINFO(bool,                "boolean", dynamic::BOOL)
676 FOLLY_DYNAMIC_DEC_TYPEINFO(std::string, "string", dynamic::STRING)
677 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::Array,      "array",   dynamic::ARRAY)
678 FOLLY_DYNAMIC_DEC_TYPEINFO(double,              "double",  dynamic::DOUBLE)
679 FOLLY_DYNAMIC_DEC_TYPEINFO(int64_t,             "int64",   dynamic::INT64)
680 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::ObjectImpl, "object",  dynamic::OBJECT)
681
682 #undef FOLLY_DYNAMIC_DEC_TYPEINFO
683
684 template<class T>
685 T dynamic::asImpl() const {
686   switch (type()) {
687   case INT64:    return to<T>(*get_nothrow<int64_t>());
688   case DOUBLE:   return to<T>(*get_nothrow<double>());
689   case BOOL:     return to<T>(*get_nothrow<bool>());
690   case STRING:
691     return to<T>(*get_nothrow<std::string>());
692   default:
693     throw TypeError("int/double/bool/string", type());
694   }
695 }
696
697 // Return a T* to our type, or null if we're not that type.
698 template<class T>
699 T* dynamic::get_nothrow() & noexcept {
700   if (type_ != TypeInfo<T>::type) {
701     return nullptr;
702   }
703   return getAddress<T>();
704 }
705
706 template<class T>
707 T const* dynamic::get_nothrow() const& noexcept {
708   return const_cast<dynamic*>(this)->get_nothrow<T>();
709 }
710
711 // Return T* for where we can put a T, without type checking.  (Memory
712 // might be uninitialized, even.)
713 template<class T>
714 T* dynamic::getAddress() noexcept {
715   return GetAddrImpl<T>::get(u_);
716 }
717
718 template<class T>
719 T const* dynamic::getAddress() const noexcept {
720   return const_cast<dynamic*>(this)->getAddress<T>();
721 }
722
723 template<class T> struct dynamic::GetAddrImpl {};
724 template<> struct dynamic::GetAddrImpl<void*> {
725   static void** get(Data& d) noexcept { return &d.nul; }
726 };
727 template<> struct dynamic::GetAddrImpl<dynamic::Array> {
728   static Array* get(Data& d) noexcept { return &d.array; }
729 };
730 template<> struct dynamic::GetAddrImpl<bool> {
731   static bool* get(Data& d) noexcept { return &d.boolean; }
732 };
733 template<> struct dynamic::GetAddrImpl<int64_t> {
734   static int64_t* get(Data& d) noexcept { return &d.integer; }
735 };
736 template<> struct dynamic::GetAddrImpl<double> {
737   static double* get(Data& d) noexcept { return &d.doubl; }
738 };
739 template <>
740 struct dynamic::GetAddrImpl<std::string> {
741   static std::string* get(Data& d) noexcept {
742     return &d.string;
743   }
744 };
745 template<> struct dynamic::GetAddrImpl<dynamic::ObjectImpl> {
746   static_assert(sizeof(ObjectImpl) <= sizeof(Data::objectBuffer),
747     "In your implementation, std::unordered_map<> apparently takes different"
748     " amount of space depending on its template parameters.  This is "
749     "weird.  Make objectBuffer bigger if you want to compile dynamic.");
750
751   static ObjectImpl* get(Data& d) noexcept {
752     void* data = &d.objectBuffer;
753     return static_cast<ObjectImpl*>(data);
754   }
755 };
756
757 template<class T>
758 T& dynamic::get() {
759   if (auto* p = get_nothrow<T>()) {
760     return *p;
761   }
762   throw TypeError(TypeInfo<T>::name, type());
763 }
764
765 template<class T>
766 T const& dynamic::get() const {
767   return const_cast<dynamic*>(this)->get<T>();
768 }
769
770 //////////////////////////////////////////////////////////////////////
771
772 /*
773  * Helper for implementing operator<<.  Throws if the type shouldn't
774  * support it.
775  */
776 template<class T>
777 struct dynamic::PrintImpl {
778   static void print(dynamic const&, std::ostream& out, T const& t) {
779     out << t;
780   }
781 };
782 // Otherwise, null, being (void*)0, would print as 0.
783 template <>
784 struct dynamic::PrintImpl<void*> {
785   static void print(dynamic const& /* d */,
786                     std::ostream& out,
787                     void* const& nul) {
788     DCHECK_EQ((void*)0, nul);
789     out << "null";
790   }
791 };
792 template<>
793 struct dynamic::PrintImpl<dynamic::ObjectImpl> {
794   static void print(dynamic const& d,
795                     std::ostream& out,
796                     dynamic::ObjectImpl const&) {
797     d.print_as_pseudo_json(out);
798   }
799 };
800 template<>
801 struct dynamic::PrintImpl<dynamic::Array> {
802   static void print(dynamic const& d,
803                     std::ostream& out,
804                     dynamic::Array const&) {
805     d.print_as_pseudo_json(out);
806   }
807 };
808
809 inline void dynamic::print(std::ostream& out) const {
810 #define FB_X(T) PrintImpl<T>::print(*this, out, *getAddress<T>())
811   FB_DYNAMIC_APPLY(type_, FB_X);
812 #undef FB_X
813 }
814
815 inline std::ostream& operator<<(std::ostream& out, dynamic const& d) {
816   d.print(out);
817   return out;
818 }
819
820 //////////////////////////////////////////////////////////////////////
821
822 // Secialization of FormatValue so dynamic objects can be formatted
823 template <>
824 class FormatValue<dynamic> {
825  public:
826   explicit FormatValue(const dynamic& val) : val_(val) { }
827
828   template <class FormatCallback>
829   void format(FormatArg& arg, FormatCallback& cb) const {
830     switch (val_.type()) {
831     case dynamic::NULLT:
832       FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
833       break;
834     case dynamic::BOOL:
835       FormatValue<bool>(val_.asBool()).format(arg, cb);
836       break;
837     case dynamic::INT64:
838       FormatValue<int64_t>(val_.asInt()).format(arg, cb);
839       break;
840     case dynamic::STRING:
841       FormatValue<std::string>(val_.asString()).format(arg, cb);
842       break;
843     case dynamic::DOUBLE:
844       FormatValue<double>(val_.asDouble()).format(arg, cb);
845       break;
846     case dynamic::ARRAY:
847       FormatValue(val_.at(arg.splitIntKey())).format(arg, cb);
848       break;
849     case dynamic::OBJECT:
850       FormatValue(val_.at(arg.splitKey().toString())).format(arg, cb);
851       break;
852     }
853   }
854
855  private:
856   const dynamic& val_;
857 };
858
859 template <class V>
860 class FormatValue<detail::DefaultValueWrapper<dynamic, V>> {
861  public:
862   explicit FormatValue(
863       const detail::DefaultValueWrapper<dynamic, V>& val)
864     : val_(val) { }
865
866   template <class FormatCallback>
867   void format(FormatArg& arg, FormatCallback& cb) const {
868     auto& c = val_.container;
869     switch (c.type()) {
870     case dynamic::NULLT:
871     case dynamic::BOOL:
872     case dynamic::INT64:
873     case dynamic::STRING:
874     case dynamic::DOUBLE:
875       FormatValue<dynamic>(c).format(arg, cb);
876       break;
877     case dynamic::ARRAY:
878       {
879         int key = arg.splitIntKey();
880         if (key >= 0 && size_t(key) < c.size()) {
881           FormatValue<dynamic>(c.at(key)).format(arg, cb);
882         } else{
883           FormatValue<V>(val_.defaultValue).format(arg, cb);
884         }
885       }
886       break;
887     case dynamic::OBJECT:
888       {
889         auto pos = c.find(arg.splitKey());
890         if (pos != c.items().end()) {
891           FormatValue<dynamic>(pos->second).format(arg, cb);
892         } else {
893           FormatValue<V>(val_.defaultValue).format(arg, cb);
894         }
895       }
896       break;
897     }
898   }
899
900  private:
901   const detail::DefaultValueWrapper<dynamic, V>& val_;
902 };
903
904 }  // namespaces
905
906 #undef FB_DYNAMIC_APPLY