2 * Copyright 2015 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 explicit TypeError(const std::string& expected,
64 dynamic::Type actual1, dynamic::Type actual2);
69 //////////////////////////////////////////////////////////////////////
73 // This helper is used in destroy() to be able to run destructors on
74 // types like "int64_t" without a compiler error.
76 template<class T> static void destroy(T* t) { t->~T(); }
80 * The enable_if junk here is necessary to avoid ambiguous
81 * conversions relating to bool and double when you implicitly
82 * convert an int or long to a dynamic.
84 template<class T, class Enable = void> struct ConversionHelper;
86 struct ConversionHelper<
88 typename std::enable_if<
89 std::is_integral<T>::value && !std::is_same<T,bool>::value
95 struct ConversionHelper<
97 typename std::enable_if<
98 (!std::is_integral<T>::value || std::is_same<T,bool>::value) &&
99 !std::is_same<T,std::nullptr_t>::value
105 struct ConversionHelper<
107 typename std::enable_if<
108 std::is_same<T,std::nullptr_t>::value
115 * Helper for implementing numeric conversions in operators on
116 * numbers. Just promotes to double when one of the arguments is
117 * double, or throws if either is not a numeric type.
119 template<template<class> class Op>
120 dynamic numericOp(dynamic const& a, dynamic const& b) {
121 if (!a.isNumber() || !b.isNumber()) {
122 throw TypeError("numeric", a.type(), b.type());
124 if (a.type() != b.type()) {
125 auto& integ = a.isInt() ? a : b;
126 auto& nonint = a.isInt() ? b : a;
127 return Op<double>()(to<double>(integ.asInt()), nonint.asDouble());
130 return Op<double>()(a.asDouble(), b.asDouble());
132 return Op<int64_t>()(a.asInt(), b.asInt());
137 //////////////////////////////////////////////////////////////////////
140 * We're doing this instead of a simple member typedef to avoid the
141 * undefined behavior of parameterizing std::unordered_map<> with an
144 * Note: Later we may add separate order tracking here (a multi-index
147 struct dynamic::ObjectImpl : std::unordered_map<dynamic, dynamic> {};
149 //////////////////////////////////////////////////////////////////////
151 // Helper object for creating objects conveniently. See object and
152 // the dynamic::dynamic(ObjectMaker&&) ctor.
153 struct dynamic::ObjectMaker {
154 friend struct dynamic;
156 explicit ObjectMaker() : val_(dynamic::object) {}
157 explicit ObjectMaker(dynamic const& key, dynamic val)
158 : val_(dynamic::object)
160 val_.insert(key, std::move(val));
162 explicit ObjectMaker(dynamic&& key, dynamic val)
163 : val_(dynamic::object)
165 val_.insert(std::move(key), std::move(val));
168 // Make sure no one tries to save one of these into an lvalue with
169 // auto or anything like that.
170 ObjectMaker(ObjectMaker&&) = default;
171 ObjectMaker(ObjectMaker const&) = delete;
172 ObjectMaker& operator=(ObjectMaker const&) = delete;
173 ObjectMaker& operator=(ObjectMaker&&) = delete;
175 // These return rvalue-references instead of lvalue-references to allow
176 // constructs like this to moved instead of copied:
177 // dynamic a = dynamic::object("a", "b")("c", "d")
178 ObjectMaker&& operator()(dynamic const& key, dynamic val) {
179 val_.insert(key, std::move(val));
180 return std::move(*this);
183 ObjectMaker&& operator()(dynamic&& key, dynamic val) {
184 val_.insert(std::move(key), std::move(val));
185 return std::move(*this);
192 // This looks like a case for perfect forwarding, but our use of
193 // std::initializer_list for constructing dynamic arrays makes it less
194 // functional than doing this manually.
195 inline dynamic::ObjectMaker dynamic::object() { return ObjectMaker(); }
196 inline dynamic::ObjectMaker dynamic::object(dynamic&& a, dynamic&& b) {
197 return ObjectMaker(std::move(a), std::move(b));
199 inline dynamic::ObjectMaker dynamic::object(dynamic const& a, dynamic&& b) {
200 return ObjectMaker(a, std::move(b));
202 inline dynamic::ObjectMaker dynamic::object(dynamic&& a, dynamic const& b) {
203 return ObjectMaker(std::move(a), b);
205 inline dynamic::ObjectMaker
206 dynamic::object(dynamic const& a, dynamic const& b) {
207 return ObjectMaker(a, b);
210 //////////////////////////////////////////////////////////////////////
212 struct dynamic::const_item_iterator
213 : boost::iterator_adaptor<dynamic::const_item_iterator,
214 dynamic::ObjectImpl::const_iterator> {
215 /* implicit */ const_item_iterator(base_type b) : iterator_adaptor_(b) { }
218 friend class boost::iterator_core_access;
221 struct dynamic::const_key_iterator
222 : boost::iterator_adaptor<dynamic::const_key_iterator,
223 dynamic::ObjectImpl::const_iterator,
225 /* implicit */ const_key_iterator(base_type b) : iterator_adaptor_(b) { }
228 dynamic const& dereference() const {
229 return base_reference()->first;
231 friend class boost::iterator_core_access;
234 struct dynamic::const_value_iterator
235 : boost::iterator_adaptor<dynamic::const_value_iterator,
236 dynamic::ObjectImpl::const_iterator,
238 /* implicit */ const_value_iterator(base_type b) : iterator_adaptor_(b) { }
241 dynamic const& dereference() const {
242 return base_reference()->second;
244 friend class boost::iterator_core_access;
247 //////////////////////////////////////////////////////////////////////
249 inline dynamic::dynamic(ObjectMaker (*)())
252 new (getAddress<ObjectImpl>()) ObjectImpl();
255 inline dynamic::dynamic(StringPiece s)
258 new (&u_.string) fbstring(s.data(), s.size());
261 inline dynamic::dynamic(char const* s)
264 new (&u_.string) fbstring(s);
267 inline dynamic::dynamic(std::string const& s)
270 new (&u_.string) fbstring(s);
273 inline dynamic::dynamic(fbstring const& s)
276 new (&u_.string) fbstring(s);
279 inline dynamic::dynamic(fbstring&& s)
282 new (&u_.string) fbstring(std::move(s));
285 inline dynamic& dynamic::operator=(std::initializer_list<dynamic> il) {
286 (*this) = dynamic(il);
290 inline dynamic::dynamic(std::initializer_list<dynamic> il)
293 new (&u_.array) Array(il.begin(), il.end());
296 inline dynamic::dynamic(ObjectMaker&& maker)
299 new (getAddress<ObjectImpl>())
300 ObjectImpl(std::move(*maker.val_.getAddress<ObjectImpl>()));
303 inline dynamic::dynamic(dynamic const& o)
309 inline dynamic::dynamic(dynamic&& o) noexcept
312 *this = std::move(o);
315 inline dynamic::~dynamic() noexcept { destroy(); }
318 dynamic::dynamic(T t) {
319 typedef typename detail::ConversionHelper<T>::type U;
320 type_ = TypeInfo<U>::type;
321 new (getAddress<U>()) U(std::move(t));
324 template<class Iterator>
325 dynamic::dynamic(Iterator first, Iterator last)
328 new (&u_.array) Array(first, last);
331 //////////////////////////////////////////////////////////////////////
333 inline dynamic::const_iterator dynamic::begin() const {
334 return get<Array>().begin();
336 inline dynamic::const_iterator dynamic::end() const {
337 return get<Array>().end();
341 struct dynamic::IterableProxy {
342 typedef It const_iterator;
343 typedef typename It::value_type value_type;
345 /* implicit */ IterableProxy(const dynamic::ObjectImpl* o) : o_(o) { }
356 const dynamic::ObjectImpl* o_;
359 inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys()
361 return &(get<ObjectImpl>());
364 inline dynamic::IterableProxy<dynamic::const_value_iterator> dynamic::values()
366 return &(get<ObjectImpl>());
369 inline dynamic::IterableProxy<dynamic::const_item_iterator> dynamic::items()
371 return &(get<ObjectImpl>());
374 inline bool dynamic::isString() const { return get_nothrow<fbstring>(); }
375 inline bool dynamic::isObject() const { return get_nothrow<ObjectImpl>(); }
376 inline bool dynamic::isBool() const { return get_nothrow<bool>(); }
377 inline bool dynamic::isArray() const { return get_nothrow<Array>(); }
378 inline bool dynamic::isDouble() const { return get_nothrow<double>(); }
379 inline bool dynamic::isInt() const { return get_nothrow<int64_t>(); }
380 inline bool dynamic::isNull() const { return get_nothrow<void*>(); }
381 inline bool dynamic::isNumber() const { return isInt() || isDouble(); }
383 inline dynamic::Type dynamic::type() const {
387 inline fbstring dynamic::asString() const { return asImpl<fbstring>(); }
388 inline double dynamic::asDouble() const { return asImpl<double>(); }
389 inline int64_t dynamic::asInt() const { return asImpl<int64_t>(); }
390 inline bool dynamic::asBool() const { return asImpl<bool>(); }
392 inline const fbstring& dynamic::getString() const& { return get<fbstring>(); }
393 inline double dynamic::getDouble() const& { return get<double>(); }
394 inline int64_t dynamic::getInt() const& { return get<int64_t>(); }
395 inline bool dynamic::getBool() const& { return get<bool>(); }
397 inline fbstring& dynamic::getString() & { return get<fbstring>(); }
398 inline double& dynamic::getDouble() & { return get<double>(); }
399 inline int64_t& dynamic::getInt() & { return get<int64_t>(); }
400 inline bool& dynamic::getBool() & { return get<bool>(); }
402 inline fbstring dynamic::getString() && { return std::move(get<fbstring>()); }
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>(); }
407 inline const char* dynamic::data() const& { return get<fbstring>().data(); }
408 inline const char* dynamic::c_str() const& { return get<fbstring>().c_str(); }
409 inline StringPiece dynamic::stringPiece() const { return get<fbstring>(); }
412 struct dynamic::CompareOp {
413 static bool comp(T const& a, T const& b) { return a < b; }
416 struct dynamic::CompareOp<dynamic::ObjectImpl> {
417 static bool comp(ObjectImpl const&, ObjectImpl const&) {
418 // This code never executes; it is just here for the compiler.
423 inline dynamic& dynamic::operator+=(dynamic const& o) {
424 if (type() == STRING && o.type() == STRING) {
425 *getAddress<fbstring>() += *o.getAddress<fbstring>();
428 *this = detail::numericOp<std::plus>(*this, o);
432 inline dynamic& dynamic::operator-=(dynamic const& o) {
433 *this = detail::numericOp<std::minus>(*this, o);
437 inline dynamic& dynamic::operator*=(dynamic const& o) {
438 *this = detail::numericOp<std::multiplies>(*this, o);
442 inline dynamic& dynamic::operator/=(dynamic const& o) {
443 *this = detail::numericOp<std::divides>(*this, o);
447 #define FB_DYNAMIC_INTEGER_OP(op) \
448 inline dynamic& dynamic::operator op(dynamic const& o) { \
449 if (!isInt() || !o.isInt()) { \
450 throw TypeError("int64", type(), o.type()); \
452 *getAddress<int64_t>() op o.asInt(); \
456 FB_DYNAMIC_INTEGER_OP(%=)
457 FB_DYNAMIC_INTEGER_OP(|=)
458 FB_DYNAMIC_INTEGER_OP(&=)
459 FB_DYNAMIC_INTEGER_OP(^=)
461 #undef FB_DYNAMIC_INTEGER_OP
463 inline dynamic& dynamic::operator++() {
468 inline dynamic& dynamic::operator--() {
473 inline dynamic const& dynamic::operator[](dynamic const& idx) const& {
477 inline dynamic dynamic::operator[](dynamic const& idx) && {
478 return std::move((*this)[idx]);
481 template<class K, class V> inline dynamic& dynamic::setDefault(K&& k, V&& v) {
482 auto& obj = get<ObjectImpl>();
483 return obj.insert(std::make_pair(std::forward<K>(k),
484 std::forward<V>(v))).first->second;
487 inline dynamic* dynamic::get_ptr(dynamic const& idx) & {
488 return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx));
491 inline dynamic& dynamic::at(dynamic const& idx) & {
492 return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx));
495 inline dynamic dynamic::at(dynamic const& idx) && {
496 return std::move(at(idx));
499 inline bool dynamic::empty() const {
506 inline std::size_t dynamic::count(dynamic const& key) const {
507 return find(key) != items().end();
510 inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
511 return get<ObjectImpl>().find(key);
514 template<class K, class V> inline void dynamic::insert(K&& key, V&& val) {
515 auto& obj = get<ObjectImpl>();
516 auto rv = obj.insert({ std::forward<K>(key), nullptr });
517 rv.first->second = std::forward<V>(val);
520 inline void dynamic::update(const dynamic& mergeObj) {
521 if (!isObject() || !mergeObj.isObject()) {
522 throw TypeError("object", type(), mergeObj.type());
525 for (const auto& pair : mergeObj.items()) {
526 (*this)[pair.first] = pair.second;
530 inline void dynamic::update_missing(const dynamic& mergeObj1) {
531 if (!isObject() || !mergeObj1.isObject()) {
532 throw TypeError("object", type(), mergeObj1.type());
535 // Only add if not already there
536 for (const auto& pair : mergeObj1.items()) {
537 if ((*this).find(pair.first) == (*this).items().end()) {
538 (*this)[pair.first] = pair.second;
543 inline dynamic dynamic::merge(
544 const dynamic& mergeObj1,
545 const dynamic& mergeObj2) {
547 // No checks on type needed here because they are done in update_missing
548 // Note that we do update_missing here instead of update() because
549 // it will prevent the extra writes that would occur with update()
550 auto ret = mergeObj2;
551 ret.update_missing(mergeObj1);
555 inline std::size_t dynamic::erase(dynamic const& key) {
556 auto& obj = get<ObjectImpl>();
557 return obj.erase(key);
560 inline dynamic::const_iterator dynamic::erase(const_iterator it) {
561 auto& arr = get<Array>();
562 // std::vector doesn't have an erase method that works on const iterators,
563 // even though the standard says it should, so this hack converts to a
564 // non-const iterator before calling erase.
565 return get<Array>().erase(arr.begin() + (it - arr.begin()));
568 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator it) {
569 return const_key_iterator(get<ObjectImpl>().erase(it.base()));
572 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator first,
573 const_key_iterator last) {
574 return const_key_iterator(get<ObjectImpl>().erase(first.base(),
578 inline dynamic::const_value_iterator dynamic::erase(const_value_iterator it) {
579 return const_value_iterator(get<ObjectImpl>().erase(it.base()));
582 inline dynamic::const_value_iterator dynamic::erase(const_value_iterator first,
583 const_value_iterator last) {
584 return const_value_iterator(get<ObjectImpl>().erase(first.base(),
588 inline dynamic::const_item_iterator dynamic::erase(const_item_iterator it) {
589 return const_item_iterator(get<ObjectImpl>().erase(it.base()));
592 inline dynamic::const_item_iterator dynamic::erase(const_item_iterator first,
593 const_item_iterator last) {
594 return const_item_iterator(get<ObjectImpl>().erase(first.base(),
598 inline void dynamic::resize(std::size_t sz, dynamic const& c) {
599 auto& array = get<Array>();
603 inline void dynamic::push_back(dynamic const& v) {
604 auto& array = get<Array>();
608 inline void dynamic::push_back(dynamic&& v) {
609 auto& array = get<Array>();
610 array.push_back(std::move(v));
613 inline void dynamic::pop_back() {
614 auto& array = get<Array>();
618 //////////////////////////////////////////////////////////////////////
620 #define FOLLY_DYNAMIC_DEC_TYPEINFO(T, str, val) \
621 template <> struct dynamic::TypeInfo<T> { \
622 static constexpr const char* name = str; \
623 static constexpr dynamic::Type type = val; \
627 FOLLY_DYNAMIC_DEC_TYPEINFO(void*, "null", dynamic::NULLT)
628 FOLLY_DYNAMIC_DEC_TYPEINFO(bool, "boolean", dynamic::BOOL)
629 FOLLY_DYNAMIC_DEC_TYPEINFO(fbstring, "string", dynamic::STRING)
630 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::Array, "array", dynamic::ARRAY)
631 FOLLY_DYNAMIC_DEC_TYPEINFO(double, "double", dynamic::DOUBLE)
632 FOLLY_DYNAMIC_DEC_TYPEINFO(int64_t, "int64", dynamic::INT64)
633 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::ObjectImpl, "object", dynamic::OBJECT)
635 #undef FOLLY_DYNAMIC_DEC_TYPEINFO
638 T dynamic::asImpl() const {
640 case INT64: return to<T>(*get_nothrow<int64_t>());
641 case DOUBLE: return to<T>(*get_nothrow<double>());
642 case BOOL: return to<T>(*get_nothrow<bool>());
643 case STRING: return to<T>(*get_nothrow<fbstring>());
645 throw TypeError("int/double/bool/string", type());
649 // Return a T* to our type, or null if we're not that type.
651 T* dynamic::get_nothrow() & noexcept {
652 if (type_ != TypeInfo<T>::type) {
655 return getAddress<T>();
659 T const* dynamic::get_nothrow() const& noexcept {
660 return const_cast<dynamic*>(this)->get_nothrow<T>();
663 // Return T* for where we can put a T, without type checking. (Memory
664 // might be uninitialized, even.)
666 T* dynamic::getAddress() noexcept {
667 return GetAddrImpl<T>::get(u_);
671 T const* dynamic::getAddress() const noexcept {
672 return const_cast<dynamic*>(this)->getAddress<T>();
675 template<class T> struct dynamic::GetAddrImpl {};
676 template<> struct dynamic::GetAddrImpl<void*> {
677 static void** get(Data& d) noexcept { return &d.nul; }
679 template<> struct dynamic::GetAddrImpl<dynamic::Array> {
680 static Array* get(Data& d) noexcept { return &d.array; }
682 template<> struct dynamic::GetAddrImpl<bool> {
683 static bool* get(Data& d) noexcept { return &d.boolean; }
685 template<> struct dynamic::GetAddrImpl<int64_t> {
686 static int64_t* get(Data& d) noexcept { return &d.integer; }
688 template<> struct dynamic::GetAddrImpl<double> {
689 static double* get(Data& d) noexcept { return &d.doubl; }
691 template<> struct dynamic::GetAddrImpl<fbstring> {
692 static fbstring* get(Data& d) noexcept { return &d.string; }
694 template<> struct dynamic::GetAddrImpl<dynamic::ObjectImpl> {
695 static_assert(sizeof(ObjectImpl) <= sizeof(Data::objectBuffer),
696 "In your implementation, std::unordered_map<> apparently takes different"
697 " amount of space depending on its template parameters. This is "
698 "weird. Make objectBuffer bigger if you want to compile dynamic.");
700 static ObjectImpl* get(Data& d) noexcept {
701 void* data = &d.objectBuffer;
702 return static_cast<ObjectImpl*>(data);
708 if (auto* p = get_nothrow<T>()) {
711 throw TypeError(TypeInfo<T>::name, type());
715 T const& dynamic::get() const {
716 return const_cast<dynamic*>(this)->get<T>();
719 //////////////////////////////////////////////////////////////////////
722 * Helper for implementing operator<<. Throws if the type shouldn't
726 struct dynamic::PrintImpl {
727 static void print(dynamic const&, std::ostream& out, T const& t) {
732 struct dynamic::PrintImpl<dynamic::ObjectImpl> {
733 static void print(dynamic const& d,
735 dynamic::ObjectImpl const&) {
736 d.print_as_pseudo_json(out);
740 struct dynamic::PrintImpl<dynamic::Array> {
741 static void print(dynamic const& d,
743 dynamic::Array const&) {
744 d.print_as_pseudo_json(out);
748 inline void dynamic::print(std::ostream& out) const {
749 #define FB_X(T) PrintImpl<T>::print(*this, out, *getAddress<T>())
750 FB_DYNAMIC_APPLY(type_, FB_X);
754 inline std::ostream& operator<<(std::ostream& out, dynamic const& d) {
759 //////////////////////////////////////////////////////////////////////
761 // Secialization of FormatValue so dynamic objects can be formatted
763 class FormatValue<dynamic> {
765 explicit FormatValue(const dynamic& val) : val_(val) { }
767 template <class FormatCallback>
768 void format(FormatArg& arg, FormatCallback& cb) const {
769 switch (val_.type()) {
771 FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
774 FormatValue<bool>(val_.asBool()).format(arg, cb);
777 FormatValue<int64_t>(val_.asInt()).format(arg, cb);
779 case dynamic::STRING:
780 FormatValue<fbstring>(val_.asString()).format(arg, cb);
782 case dynamic::DOUBLE:
783 FormatValue<double>(val_.asDouble()).format(arg, cb);
786 FormatValue(val_.at(arg.splitIntKey())).format(arg, cb);
788 case dynamic::OBJECT:
789 FormatValue(val_.at(arg.splitKey().toFbstring())).format(arg, cb);
799 class FormatValue<detail::DefaultValueWrapper<dynamic, V>> {
801 explicit FormatValue(
802 const detail::DefaultValueWrapper<dynamic, V>& val)
805 template <class FormatCallback>
806 void format(FormatArg& arg, FormatCallback& cb) const {
807 auto& c = val_.container;
812 case dynamic::STRING:
813 case dynamic::DOUBLE:
814 FormatValue<dynamic>(c).format(arg, cb);
818 int key = arg.splitIntKey();
819 if (key >= 0 && size_t(key) < c.size()) {
820 FormatValue<dynamic>(c.at(key)).format(arg, cb);
822 FormatValue<V>(val_.defaultValue).format(arg, cb);
826 case dynamic::OBJECT:
828 auto pos = c.find(arg.splitKey());
829 if (pos != c.items().end()) {
830 FormatValue<dynamic>(pos->second).format(arg, cb);
832 FormatValue<V>(val_.defaultValue).format(arg, cb);
840 const detail::DefaultValueWrapper<dynamic, V>& val_;
845 #undef FB_DYNAMIC_APPLY