X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fdynamic.h;h=0e03f8f4791b1b472a59d509b3de5dff8f87a037;hb=9b4749fd47c605c27b6973f317d7c4f3a0e101e9;hp=6334cf2a952fcb251e941fe07ec81affc7055a81;hpb=c8cbdd5fc46433e75cde2076b1db9a2e2bf003d2;p=folly.git diff --git a/folly/dynamic.h b/folly/dynamic.h index 6334cf2a..0e03f8f4 100644 --- a/folly/dynamic.h +++ b/folly/dynamic.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 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. @@ -63,19 +63,21 @@ #ifndef FOLLY_DYNAMIC_H_ #define FOLLY_DYNAMIC_H_ -#include +#include +#include #include -#include -#include #include +#include #include -#include +#include +#include #include -#include + #include -#include "folly/Traits.h" -#include "folly/FBString.h" +#include +#include +#include namespace folly { @@ -110,6 +112,7 @@ private: typedef std::vector Array; public: typedef Array::const_iterator const_iterator; + typedef dynamic value_type; struct const_key_iterator; struct const_value_iterator; struct const_item_iterator; @@ -142,8 +145,11 @@ public: /* * 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 @@ -264,7 +270,7 @@ public: * 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; @@ -272,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. @@ -333,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. * @@ -495,6 +524,6 @@ private: } -#include "folly/dynamic-inl.h" +#include #endif