EventCountTest cleanups
[folly.git] / folly / dynamic.h
index 3defcfafc8ed7271c955105e81c91a1294f39d46..0e03f8f4791b1b472a59d509b3de5dff8f87a037 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #ifndef FOLLY_DYNAMIC_H_
 #define FOLLY_DYNAMIC_H_
 
-#include <unordered_map>
+#include <cstdint>
+#include <initializer_list>
 #include <memory>
-#include <string>
-#include <utility>
 #include <ostream>
+#include <string>
 #include <type_traits>
-#include <initializer_list>
+#include <unordered_map>
+#include <utility>
 #include <vector>
-#include <cstdint>
+
 #include <boost/operators.hpp>
 
-#include "folly/Traits.h"
-#include "folly/FBString.h"
+#include <folly/FBString.h>
+#include <folly/Range.h>
+#include <folly/Traits.h>
 
 namespace folly {
 
@@ -110,6 +112,7 @@ private:
   typedef std::vector<dynamic> Array;
 public:
   typedef Array::const_iterator const_iterator;
+  typedef dynamic value_type;
   struct const_key_iterator;
   struct const_value_iterator;
   struct const_item_iterator;
@@ -133,13 +136,20 @@ private:
   struct ObjectMaker;
 
 public:
-  template<class... Args> static ObjectMaker object(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&);
 
   /*
    * String compatibility constructors.
    */
+  /* implicit */ dynamic(StringPiece val);
   /* implicit */ dynamic(char const* val);
   /* implicit */ dynamic(std::string const& val);
+  /* implicit */ dynamic(fbstring const& val);
+  /* implicit */ dynamic(fbstring&& val);
 
   /*
    * This is part of the plumbing for object(), above.  Used to create
@@ -249,13 +259,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;
@@ -263,6 +278,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.
@@ -302,8 +326,8 @@ public:
    * AssociativeContainer-style find interface for objects.  Throws if
    * this is not an object.
    *
-   * Returns: end() if the key is not present, or an iterator pointing
-   * to the item.
+   * Returns: items().end() if the key is not present, or a
+   * const_item_iterator pointing to the item.
    */
   const_item_iterator find(dynamic const&) const;
 
@@ -324,6 +348,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.
    *
@@ -415,6 +453,14 @@ public:
   void push_back(dynamic const&);
   void push_back(dynamic&&);
 
+  /*
+   * Remove an element from the back of an array.  If this is not an array,
+   * throws TypeError.
+   *
+   * Does not invalidate iterators.
+   */
+  void pop_back();
+
   /*
    * Get a hash code.  This function is called by a std::hash<>
    * specialization, also.
@@ -426,7 +472,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;
@@ -479,6 +524,6 @@ private:
 
 }
 
-#include "folly/dynamic-inl.h"
+#include <folly/dynamic-inl.h>
 
 #endif