Redesigned JsonVariant to leverage converting constructors instead of assignment operators

This commit is contained in:
Benoit Blanchon
2015-05-23 15:32:50 +02:00
parent 08d05df00e
commit 756c279cdc
44 changed files with 934 additions and 649 deletions

View File

@@ -22,6 +22,7 @@ namespace ArduinoJson {
// Forward declarations
class JsonObject;
class JsonBuffer;
class JsonArraySubscript;
// An array of JsonVariant.
//
@@ -33,35 +34,35 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
public Internals::ReferenceType,
public Internals::List<JsonVariant>,
public Internals::JsonBufferAllocated {
// JsonBuffer is a friend because it needs to call the private constructor.
friend class JsonBuffer;
public:
// Returns the JsonVariant at the specified index (synonym for operator[])
JsonVariant &at(int index) const;
// Create an empty JsonArray attached to the specified JsonBuffer.
// You should not call this constructor directly.
// Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray().
explicit JsonArray(JsonBuffer *buffer)
: Internals::List<JsonVariant>(buffer) {}
// Returns the JsonVariant at the specified index (synonym for at())
JsonVariant &operator[](int index) const { return at(index); }
// Gets the value at the specified index
JSON_FORCE_INLINE const JsonArraySubscript operator[](size_t index) const;
// Adds an uninitialized JsonVariant at the end of the array.
// Return a reference or JsonVariant::invalid() if allocation fails.
JsonVariant &add();
// Gets or sets the value at specified index
JSON_FORCE_INLINE JsonArraySubscript operator[](size_t index);
// Adds the specified value at the end of the array.
JSON_FORCE_INLINE bool add(const JsonVariant value);
// Sets the value at specified index.
JSON_FORCE_INLINE void set(size_t index, const JsonVariant value);
// Gets the value at the specified index.
JSON_FORCE_INLINE JsonVariant get(size_t index) const;
// Gets the value at the specified index.
template <typename T>
void add(T value) {
add().set(value);
}
JSON_FORCE_INLINE T get(size_t index) const;
// Adds the specified double value at the end of the array.
// The value will be printed with the specified number of decimal digits.
void add(double value, uint8_t decimals) { add().set(value, decimals); }
// Adds a reference to the specified JsonArray at the end of the array.
void add(JsonArray &array) { add().set(array); }
// Adds a reference to the specified JsonObject at the end of the array.
void add(JsonObject &obejct) { add().set(obejct); }
// Check the type of the value at specified index.
template <typename T>
JSON_FORCE_INLINE T is(size_t index) const;
// Creates a JsonArray and adds a reference at the end of the array.
// It's a shortcut for JsonBuffer::createArray() and JsonArray::add()
@@ -72,7 +73,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
JsonObject &createNestedObject();
// Removes element at specified index.
void removeAt(int index);
void removeAt(size_t index);
// Returns a reference an invalid JsonArray.
// This object is meant to replace a NULL pointer.
@@ -83,13 +84,11 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
void writeTo(Internals::JsonWriter &writer) const;
private:
// Create an empty JsonArray attached to the specified JsonBuffer.
explicit JsonArray(JsonBuffer *buffer)
: Internals::List<JsonVariant>(buffer) {}
node_type *getNodeAt(int index) const;
node_type *getNodeAt(size_t index) const;
// The instance returned by JsonArray::invalid()
static JsonArray _invalid;
};
}
#include "JsonArray.ipp"