*/
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;
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.
*/
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.
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.
*
private:
friend struct TypeError;
struct ObjectImpl;
- struct ObjectMaker;
template<class T> struct TypeInfo;
template<class T> struct CompareOp;
template<class T> struct GetAddrImpl;