Add new CRC-32C checksum functions to folly
[folly.git] / folly / dynamic.h
index 88d23d9f5afdd4812db80391571f17ffec3bb0df..47740627c711b7fbbbea4137f7d2a77949a7e735 100644 (file)
@@ -253,13 +253,18 @@ public:
    */
   Type type() const;
 
+  /*
+   * Returns the type of this dynamic as a printable string.
+   */
+  const char* typeName() const;
+
   /*
    * Extract a value while trying to convert to the specified type.
    * Throws exceptions if we cannot convert from the real type to the
    * requested type.
    *
    * Note you can only use this to access integral types or strings,
-   * since arrays and objects are generally best delt with as a
+   * since arrays and objects are generally best dealt with as a
    * dynamic.
    */
   fbstring asString() const;
@@ -267,6 +272,15 @@ public:
   int64_t  asInt() const;
   bool     asBool() const;
 
+  /*
+   * It is occasionally useful to access a string's internal pointer
+   * directly, without the type conversion of `asString()`.
+   *
+   * These will throw a TypeError if the dynamic is not a string.
+   */
+  const char* data()  const;
+  const char* c_str() const;
+
   /*
    * Returns: true if this dynamic is null, an empty array, an empty
    * object, or an empty string.
@@ -311,6 +325,7 @@ public:
    */
   const_item_iterator find(dynamic const&) const;
 
+
   /*
    * If this is an object, returns whether it contains a field with
    * the given name.  Otherwise throws TypeError.
@@ -328,6 +343,20 @@ public:
   dynamic const& at(dynamic const&) const;
   dynamic&       at(dynamic const&);
 
+  /*
+   * Like 'at', above, except it returns either a pointer to the contained
+   * object or nullptr if it wasn't found. This allows a key to be tested for
+   * containment and retrieved in one operation. Example:
+   *
+   *   if (auto* found = d.get_ptr(key))
+   *     // use *found;
+   *
+   * Using these with dynamic objects that are not arrays or objects
+   * will throw a TypeError.
+   */
+  const dynamic* get_ptr(dynamic const&) const;
+  dynamic* get_ptr(dynamic const&);
+
   /*
    * This works for access to both objects and arrays.
    *
@@ -438,7 +467,6 @@ public:
 private:
   friend struct TypeError;
   struct ObjectImpl;
-  struct ObjectMaker;
   template<class T> struct TypeInfo;
   template<class T> struct CompareOp;
   template<class T> struct GetAddrImpl;