2 * Copyright 2012 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.
18 * This is a runtime dynamically typed value. It holds types from a
19 * specific predetermined set of types (ints, bools, arrays, etc). In
20 * particular, it can be used as a convenient in-memory representation
21 * for complete json objects.
23 * In general you can try to use these objects as if they were the
24 * type they represent (although in some cases with a slightly less
25 * complete interface than the raw type), and it'll just throw a
26 * TypeError if it is used in an illegal way.
30 * dynamic twelve = 12;
31 * dynamic str = "string";
32 * dynamic map = dynamic::object;
34 * map[str + "another_str"] = { "array", "of", 4, "elements" };
35 * map.insert("null_element", nullptr);
37 * assert(map[str] == 13);
39 * // Building a complex object with a sub array inline:
40 * dynamic d = dynamic::object
42 * ("key2", { "a", "array" })
45 * Also see folly/json.h for the serialization and deserialization
48 * Note: dynamic is not DefaultConstructible. Rationale:
50 * - The intuitive thing to initialize a defaulted dynamic to would
53 * - However, the expression dynamic d = {} is required to call the
54 * default constructor by the standard, which is confusing
55 * behavior for dynamic unless the default constructor creates an
58 * Additional documentation is in folly/docs/Dynamic.md.
60 * @author Jordan DeLong <delong.j@fb.com>
63 #ifndef FOLLY_DYNAMIC_H_
64 #define FOLLY_DYNAMIC_H_
66 #include <unordered_map>
71 #include <type_traits>
72 #include <initializer_list>
75 #include <boost/operators.hpp>
77 #include "folly/Traits.h"
78 #include "folly/FBString.h"
82 //////////////////////////////////////////////////////////////////////
87 //////////////////////////////////////////////////////////////////////
89 struct dynamic : private boost::operators<dynamic> {
101 * We support direct iteration of arrays, and indirect iteration of objects.
102 * See begin(), end(), keys(), values(), and items() for more.
104 * Array iterators dereference as the elements in the array.
105 * Object key iterators dereference as the keys in the object.
106 * Object value iterators dereference as the values in the object.
107 * Object item iterators dereference as pairs of (key, value).
110 typedef std::vector<dynamic> Array;
112 typedef Array::const_iterator const_iterator;
113 struct const_key_iterator;
114 struct const_value_iterator;
115 struct const_item_iterator;
118 * Creation routines for making dynamic objects. Objects are maps
119 * from key to value (so named due to json-related origins here).
123 * // Make a fairly complex dynamic:
124 * dynamic d = dynamic::object("key", "value1")
125 * ("key2", { "value", "with", 4, "words" });
127 * // Build an object in a few steps:
128 * dynamic d = dynamic::object;
130 * d["something_else"] = { 1, 2, 3, nullptr };
136 template<class... Args> static ObjectMaker object(Args&&...);
139 * String compatibility constructors.
141 /* implicit */ dynamic(char const* val);
142 /* implicit */ dynamic(std::string const& val);
145 * This is part of the plumbing for object(), above. Used to create
146 * a new object dynamic.
148 /* implicit */ dynamic(ObjectMaker (*)());
149 /* implicit */ dynamic(ObjectMaker const&) = delete;
150 /* implicit */ dynamic(ObjectMaker&&);
153 * Create a new array from an initializer list.
157 * dynamic v = { 1, 2, 3, "foo" };
159 /* implicit */ dynamic(std::initializer_list<dynamic> il);
162 * Conversion constructors from most of the other types.
164 template<class T> /* implicit */ dynamic(T t);
167 * Create a dynamic that is an array of the values from the supplied
170 template<class Iterator> dynamic(Iterator first, Iterator last);
172 dynamic(dynamic const&);
177 * "Deep" equality comparison. This will compare all the way down
178 * an object or array, and is potentially expensive.
180 bool operator==(dynamic const& o) const;
183 * For all types except object this returns the natural ordering on
184 * those types. For objects, we throw TypeError.
186 bool operator<(dynamic const& o) const;
191 * These throw TypeError when used with types or type combinations
192 * that don't support them.
194 * These functions may also throw if you use 64-bit integers with
195 * doubles when the integers are too big to fit in a double.
197 dynamic& operator+=(dynamic const&);
198 dynamic& operator-=(dynamic const&);
199 dynamic& operator*=(dynamic const&);
200 dynamic& operator/=(dynamic const&);
201 dynamic& operator%=(dynamic const&);
202 dynamic& operator|=(dynamic const&);
203 dynamic& operator&=(dynamic const&);
204 dynamic& operator^=(dynamic const&);
205 dynamic& operator++();
206 dynamic& operator--();
209 * Assignment from other dynamics. Because of the implicit conversion
210 * to dynamic from its potential types, you can use this to change the
211 * type pretty intuitively.
213 * Basic guarantee only.
215 dynamic& operator=(dynamic const&);
216 dynamic& operator=(dynamic&&);
219 * For simple dynamics (not arrays or objects), this prints the
220 * value to an std::ostream in the expected way. Respects the
221 * formatting manipulators that have been sent to the stream
224 * If the dynamic holds an object or array, this prints them in a
225 * format very similar to JSON. (It will in fact actually be JSON
226 * as long as the dynamic validly represents a JSON object---i.e. it
227 * can't have non-string keys.)
229 friend std::ostream& operator<<(std::ostream&, dynamic const&);
232 * Returns true if this dynamic is of the specified type.
234 bool isString() const;
235 bool isObject() const;
238 bool isArray() const;
239 bool isDouble() const;
243 * Returns: isInt() || isDouble().
245 bool isNumber() const;
248 * Returns the type of this dynamic.
253 * Extract a value while trying to convert to the specified type.
254 * Throws exceptions if we cannot convert from the real type to the
257 * Note you can only use this to access integral types or strings,
258 * since arrays and objects are generally best delt with as a
261 fbstring asString() const;
262 double asDouble() const;
263 int64_t asInt() const;
267 * Returns: true if this dynamic is null, an empty array, an empty
268 * object, or an empty string.
273 * If this is an array or an object, returns the number of elements
274 * contained. If it is a string, returns the length. Otherwise
277 std::size_t size() const;
280 * You can iterate over the values of the array. Calling these on
281 * non-arrays will throw a TypeError.
283 const_iterator begin() const;
284 const_iterator end() const;
288 * Helper object returned by keys(), values(), and items().
290 template <class T> struct IterableProxy;
294 * You can iterate over the keys, values, or items (std::pair of key and
295 * value) in an object. Calling these on non-objects will throw a TypeError.
297 IterableProxy<const_key_iterator> keys() const;
298 IterableProxy<const_value_iterator> values() const;
299 IterableProxy<const_item_iterator> items() const;
302 * AssociativeContainer-style find interface for objects. Throws if
303 * this is not an object.
305 * Returns: end() if the key is not present, or an iterator pointing
308 const_item_iterator find(dynamic const&) const;
311 * If this is an object, returns whether it contains a field with
312 * the given name. Otherwise throws TypeError.
314 std::size_t count(dynamic const&) const;
317 * For objects or arrays, provides access to sub-fields by index or
320 * Using these with dynamic objects that are not arrays or objects
321 * will throw a TypeError. Using an index that is out of range or
322 * object-element that's not present throws std::out_of_range.
324 dynamic const& at(dynamic const&) const;
325 dynamic& at(dynamic const&);
328 * This works for access to both objects and arrays.
330 * In the case of an array, the index must be an integer, and this will throw
331 * std::out_of_range if it is less than zero or greater than size().
333 * In the case of an object, the non-const overload inserts a null
334 * value if the key isn't present. The const overload will throw
335 * std::out_of_range if the key is not present.
337 * These functions do not invalidate iterators.
339 dynamic& operator[](dynamic const&);
340 dynamic const& operator[](dynamic const&) const;
343 * Only defined for objects, throws TypeError otherwise.
345 * getDefault will return the value associated with the supplied key, the
346 * supplied default otherwise. setDefault will set the key to the supplied
347 * default if it is not yet set, otherwise leaving it. setDefault returns
348 * a reference to the existing value if present, the new value otherwise.
351 getDefault(const dynamic& k, const dynamic& v = dynamic::object) const;
352 dynamic&& getDefault(const dynamic& k, dynamic&& v) const;
353 template<class K, class V = dynamic>
354 dynamic& setDefault(K&& k, V&& v = dynamic::object);
357 * Resizes an array so it has at n elements, using the supplied
358 * default to fill new elements. Throws TypeError if this dynamic
361 * May invalidate iterators.
365 void resize(std::size_t n, dynamic const& = nullptr);
368 * Inserts the supplied key-value pair to an object, or throws if
369 * it's not an object.
371 * Invalidates iterators.
373 template<class K, class V> void insert(K&&, V&& val);
376 * Erase an element from a dynamic object, by key.
378 * Invalidates iterators to the element being erased.
380 * Returns the number of elements erased (i.e. 1 or 0).
382 std::size_t erase(dynamic const& key);
385 * Erase an element from a dynamic object or array, using an
386 * iterator or an iterator range.
388 * In arrays, invalidates iterators to elements after the element
389 * being erased. In objects, invalidates iterators to the elements
392 * Returns a new iterator to the first element beyond any elements
393 * removed, or end() if there are none. (The iteration order does
396 const_iterator erase(const_iterator it);
397 const_iterator erase(const_iterator first, const_iterator last);
399 const_key_iterator erase(const_key_iterator it);
400 const_key_iterator erase(const_key_iterator first, const_key_iterator last);
402 const_value_iterator erase(const_value_iterator it);
403 const_value_iterator erase(const_value_iterator first,
404 const_value_iterator last);
406 const_item_iterator erase(const_item_iterator it);
407 const_item_iterator erase(const_item_iterator first,
408 const_item_iterator last);
410 * Append elements to an array. If this is not an array, throws
413 * Invalidates iterators.
415 void push_back(dynamic const&);
416 void push_back(dynamic&&);
419 * Get a hash code. This function is called by a std::hash<>
420 * specialization, also.
422 * Throws TypeError if this is an object, array, or null.
424 std::size_t hash() const;
427 friend struct TypeError;
430 template<class T> struct TypeInfo;
431 template<class T> struct CompareOp;
432 template<class T> struct GetAddrImpl;
433 template<class T> struct PrintImpl;
435 template<class T> T const& get() const;
436 template<class T> T& get();
437 template<class T> T* get_nothrow();
438 template<class T> T const* get_nothrow() const;
439 template<class T> T* getAddress();
440 template<class T> T const* getAddress() const;
442 template<class T> T asImpl() const;
444 static char const* typeName(Type);
446 void print(std::ostream&) const;
447 void print_as_pseudo_json(std::ostream&) const; // see json.cpp
452 explicit Data() : nul(nullptr) {}
455 // XXX: gcc does an ICE if we use std::nullptr_t instead of void*
456 // here. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50361
465 * Objects are placement new'd here. We have to use a char buffer
466 * because we don't know the type here (std::unordered_map<> with
467 * dynamic would be parameterizing a std:: template with an
468 * incomplete type right now). (Note that in contrast we know it
469 * is ok to do this with fbvector because we own it.)
471 typename std::aligned_storage<
472 sizeof(std::unordered_map<int,int>),
473 alignof(std::unordered_map<int,int>)
474 >::type objectBuffer;
478 //////////////////////////////////////////////////////////////////////
482 #include "folly/dynamic-inl.h"