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