X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Fdynamic.h;h=a62805090dd9058938830571b6112c77bf273624;hp=2dc49434cbf32c386e7bf734533efacf3e7ec1b4;hb=e9c1c0434713e921c98288e932c05f228a734886;hpb=d458b0a14486b9d80d5644c7b4de60640c891df1 diff --git a/folly/dynamic.h b/folly/dynamic.h index 2dc49434..a6280509 100644 --- a/folly/dynamic.h +++ b/folly/dynamic.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,16 +45,6 @@ * Also see folly/json.h for the serialization and deserialization * functions for JSON. * - * Note: dynamic is not DefaultConstructible. Rationale: - * - * - The intuitive thing to initialize a defaulted dynamic to would - * be nullptr. - * - * - However, the expression dynamic d = {} is required to call the - * default constructor by the standard, which is confusing - * behavior for dynamic unless the default constructor creates an - * empty array. - * * Additional documentation is in folly/docs/Dynamic.md. * * @author Jordan DeLong @@ -95,6 +85,7 @@ struct dynamic : private boost::operators { OBJECT, STRING, }; + template struct NumericTypeHelper; /* * We support direct iteration of arrays, and indirect iteration of objects. @@ -108,12 +99,17 @@ struct dynamic : private boost::operators { private: typedef std::vector Array; public: + typedef Array::iterator iterator; typedef Array::const_iterator const_iterator; typedef dynamic value_type; + struct const_key_iterator; struct const_value_iterator; struct const_item_iterator; + struct value_iterator; + struct item_iterator; + /* * Creation routines for making dynamic objects and arrays. Objects * are maps from key to value (so named due to json-related origins @@ -143,18 +139,20 @@ public: static dynamic array(Args&& ...args); static ObjectMaker object(); - static ObjectMaker object(dynamic&&, dynamic&&); - static ObjectMaker object(dynamic const&, dynamic&&); - static ObjectMaker object(dynamic&&, dynamic const&); - static ObjectMaker object(dynamic const&, dynamic const&); + static ObjectMaker object(dynamic, dynamic); + + /** + * Default constructor, initializes with nullptr. + */ + dynamic(); /* * String compatibility constructors. */ + /* implicit */ dynamic(std::nullptr_t); /* implicit */ dynamic(StringPiece val); /* implicit */ dynamic(char const* val); - /* implicit */ dynamic(std::string const& val); - /* implicit */ dynamic(std::string&& val); + /* implicit */ dynamic(std::string val); /* * This is part of the plumbing for array() and object(), above. @@ -166,15 +164,18 @@ public: /* implicit */ dynamic(ObjectMaker&&); /* - * Conversion constructors from most of the other types. + * Constructors for integral and float types. + * Other types are SFINAEd out with NumericTypeHelper. */ - template /* implicit */ dynamic(T t); + template::type> + /* implicit */ dynamic(T t); /* * Create a dynamic that is an array of the values from the supplied * iterator range. */ - template dynamic(Iterator first, Iterator last); + template + explicit dynamic(Iterator first, Iterator last); dynamic(dynamic const&); dynamic(dynamic&&) noexcept; @@ -324,6 +325,8 @@ public: */ const_iterator begin() const; const_iterator end() const; + iterator begin(); + iterator end(); private: /* @@ -339,6 +342,8 @@ public: IterableProxy keys() const; IterableProxy values() const; IterableProxy items() const; + IterableProxy values(); + IterableProxy items(); /* * AssociativeContainer-style find interface for objects. Throws if @@ -348,6 +353,7 @@ public: * const_item_iterator pointing to the item. */ const_item_iterator find(dynamic const&) const; + item_iterator find(dynamic const&); /* * If this is an object, returns whether it contains a field with @@ -479,19 +485,17 @@ public: * removed, or end() if there are none. (The iteration order does * not change.) */ - const_iterator erase(const_iterator it); - const_iterator erase(const_iterator first, const_iterator last); + iterator erase(const_iterator it); + iterator erase(const_iterator first, const_iterator last); const_key_iterator erase(const_key_iterator it); const_key_iterator erase(const_key_iterator first, const_key_iterator last); - const_value_iterator erase(const_value_iterator it); - const_value_iterator erase(const_value_iterator first, - const_value_iterator last); + value_iterator erase(const_value_iterator it); + value_iterator erase(const_value_iterator first, const_value_iterator last); - const_item_iterator erase(const_item_iterator it); - const_item_iterator erase(const_item_iterator first, - const_item_iterator last); + item_iterator erase(const_item_iterator it); + item_iterator erase(const_item_iterator first, const_item_iterator last); /* * Append elements to an array. If this is not an array, throws * TypeError. @@ -548,9 +552,7 @@ private: explicit Data() : nul(nullptr) {} ~Data() {} - // XXX: gcc does an ICE if we use std::nullptr_t instead of void* - // here. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50361 - void* nul; + std::nullptr_t nul; Array array; bool boolean; double doubl;