diff options
Diffstat (limited to 'include/lib/ArduinoJson/Data')
-rw-r--r-- | include/lib/ArduinoJson/Data/Encoding.hpp | 37 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp | 22 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/JsonFloat.hpp | 18 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/JsonInteger.hpp | 23 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/JsonVariantAs.hpp | 42 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/JsonVariantContent.hpp | 27 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/JsonVariantDefault.hpp | 23 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/JsonVariantType.hpp | 27 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/List.hpp | 94 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/ListConstIterator.hpp | 50 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/ListIterator.hpp | 60 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/ListNode.hpp | 24 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/NonCopyable.hpp | 23 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/ReferenceType.hpp | 24 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Data/ValueSaver.hpp | 52 |
15 files changed, 546 insertions, 0 deletions
diff --git a/include/lib/ArduinoJson/Data/Encoding.hpp b/include/lib/ArduinoJson/Data/Encoding.hpp new file mode 100644 index 0000000..a0efa2c --- /dev/null +++ b/include/lib/ArduinoJson/Data/Encoding.hpp @@ -0,0 +1,37 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +class Encoding { + public: + // Optimized for code size on a 8-bit AVR + static char escapeChar(char c) { + const char *p = escapeTable(false); + while (p[0] && p[1] != c) { + p += 2; + } + return p[0]; + } + + // Optimized for code size on a 8-bit AVR + static char unescapeChar(char c) { + const char *p = escapeTable(true); + for (;;) { + if (p[0] == '\0') return c; + if (p[0] == c) return p[1]; + p += 2; + } + } + + private: + static const char *escapeTable(bool excludeIdenticals) { + return &"\"\"\\\\b\bf\fn\nr\rt\t"[excludeIdenticals ? 4 : 0]; + } +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp b/include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp new file mode 100644 index 0000000..443aae4 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp @@ -0,0 +1,22 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonBuffer.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonBufferAllocated { + public: + void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() { + if (!jsonBuffer) return NULL; + return jsonBuffer->alloc(n); + } + + void operator delete(void *, JsonBuffer *)throw(); +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonFloat.hpp b/include/lib/ArduinoJson/Data/JsonFloat.hpp new file mode 100644 index 0000000..0ed4214 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonFloat.hpp @@ -0,0 +1,18 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" + +namespace ArduinoJson { +namespace Internals { + +#if ARDUINOJSON_USE_DOUBLE +typedef double JsonFloat; +#else +typedef float JsonFloat; +#endif +} +} diff --git a/include/lib/ArduinoJson/Data/JsonInteger.hpp b/include/lib/ArduinoJson/Data/JsonInteger.hpp new file mode 100644 index 0000000..c8ddd00 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonInteger.hpp @@ -0,0 +1,23 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" + +namespace ArduinoJson { +namespace Internals { + +#if ARDUINOJSON_USE_LONG_LONG +typedef long long JsonInteger; +typedef unsigned long long JsonUInt; +#elif ARDUINOJSON_USE_INT64 +typedef __int64 JsonInteger; +typedef unsigned _int64 JsonUInt; +#else +typedef long JsonInteger; +typedef unsigned long JsonUInt; +#endif +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantAs.hpp b/include/lib/ArduinoJson/Data/JsonVariantAs.hpp new file mode 100644 index 0000000..8f202c5 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantAs.hpp @@ -0,0 +1,42 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A metafunction that returns the type of the value returned by +// JsonVariant::as<T>() +template <typename T> +struct JsonVariantAs { + typedef T type; +}; + +template <> +struct JsonVariantAs<char*> { + typedef const char* type; +}; + +template <> +struct JsonVariantAs<JsonArray> { + typedef JsonArray& type; +}; + +template <> +struct JsonVariantAs<const JsonArray> { + typedef const JsonArray& type; +}; + +template <> +struct JsonVariantAs<JsonObject> { + typedef JsonObject& type; +}; + +template <> +struct JsonVariantAs<const JsonObject> { + typedef const JsonObject& type; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantContent.hpp b/include/lib/ArduinoJson/Data/JsonVariantContent.hpp new file mode 100644 index 0000000..c525a60 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantContent.hpp @@ -0,0 +1,27 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonFloat.hpp" +#include "JsonInteger.hpp" + +namespace ArduinoJson { + +// Forward declarations +class JsonArray; +class JsonObject; + +namespace Internals { +// A union that defines the actual content of a JsonVariant. +// The enum JsonVariantType determines which member is in use. +union JsonVariantContent { + JsonFloat asFloat; // used for double and float + JsonUInt asInteger; // used for bool, char, short, int and longs + const char* asString; // asString can be null + JsonArray* asArray; // asArray cannot be null + JsonObject* asObject; // asObject cannot be null +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantDefault.hpp b/include/lib/ArduinoJson/Data/JsonVariantDefault.hpp new file mode 100644 index 0000000..57ecc83 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantDefault.hpp @@ -0,0 +1,23 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +template <typename T> +struct JsonVariantDefault { + static T get() { + return T(); + } +}; + +template <typename T> +struct JsonVariantDefault<const T> : JsonVariantDefault<T> {}; + +template <typename T> +struct JsonVariantDefault<T&> : JsonVariantDefault<T> {}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantType.hpp b/include/lib/ArduinoJson/Data/JsonVariantType.hpp new file mode 100644 index 0000000..21f890e --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantType.hpp @@ -0,0 +1,27 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +class JsonArray; +class JsonObject; + +namespace Internals { + +// Enumerated type to know the current type of a JsonVariant. +// The value determines which member of JsonVariantContent is used. +enum JsonVariantType { + JSON_UNDEFINED, // JsonVariant has not been initialized + JSON_UNPARSED, // JsonVariant contains an unparsed string + JSON_STRING, // JsonVariant stores a const char* + JSON_BOOLEAN, // JsonVariant stores a bool + JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt + JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated + JSON_ARRAY, // JsonVariant stores a pointer to a JsonArray + JSON_OBJECT, // JsonVariant stores a pointer to a JsonObject + JSON_FLOAT // JsonVariant stores a JsonFloat +}; +} +} diff --git a/include/lib/ArduinoJson/Data/List.hpp b/include/lib/ArduinoJson/Data/List.hpp new file mode 100644 index 0000000..506308c --- /dev/null +++ b/include/lib/ArduinoJson/Data/List.hpp @@ -0,0 +1,94 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonBuffer.hpp" +#include "ListConstIterator.hpp" +#include "ListIterator.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A singly linked list of T. +// The linked list is composed of ListNode<T>. +// It is derived by JsonArray and JsonObject +template <typename T> +class List { + public: + typedef T value_type; + typedef ListNode<T> node_type; + typedef ListIterator<T> iterator; + typedef ListConstIterator<T> const_iterator; + + // Creates an empty List<T> attached to a JsonBuffer. + // The JsonBuffer allows to allocate new nodes. + // When buffer is NULL, the List is not able to grow and success() returns + // false. This is used to identify bad memory allocations and parsing + // failures. + explicit List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} + + // Returns true if the object is valid + // Would return false in the following situation: + // - the memory allocation failed (StaticJsonBuffer was too small) + // - the JSON parsing failed + bool success() const { + return _buffer != NULL; + } + + // Returns the numbers of elements in the list. + // For a JsonObject, it would return the number of key-value pairs + size_t size() const { + size_t nodeCount = 0; + for (node_type *node = _firstNode; node; node = node->next) nodeCount++; + return nodeCount; + } + + iterator add() { + node_type *newNode = new (_buffer) node_type(); + + if (_firstNode) { + node_type *lastNode = _firstNode; + while (lastNode->next) lastNode = lastNode->next; + lastNode->next = newNode; + } else { + _firstNode = newNode; + } + + return iterator(newNode); + } + + iterator begin() { + return iterator(_firstNode); + } + iterator end() { + return iterator(NULL); + } + + const_iterator begin() const { + return const_iterator(_firstNode); + } + const_iterator end() const { + return const_iterator(NULL); + } + + void remove(iterator it) { + node_type *nodeToRemove = it._node; + if (!nodeToRemove) return; + if (nodeToRemove == _firstNode) { + _firstNode = nodeToRemove->next; + } else { + for (node_type *node = _firstNode; node; node = node->next) + if (node->next == nodeToRemove) node->next = nodeToRemove->next; + } + } + + protected: + JsonBuffer *_buffer; + + private: + node_type *_firstNode; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ListConstIterator.hpp b/include/lib/ArduinoJson/Data/ListConstIterator.hpp new file mode 100644 index 0000000..a6af685 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ListConstIterator.hpp @@ -0,0 +1,50 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "ListNode.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A read-only forward itertor for List<T> +template <typename T> +class ListConstIterator { + public: + explicit ListConstIterator(const ListNode<T> *node = NULL) : _node(node) {} + + const T &operator*() const { + return _node->content; + } + const T *operator->() { + return &_node->content; + } + + bool operator==(const ListConstIterator<T> &other) const { + return _node == other._node; + } + + bool operator!=(const ListConstIterator<T> &other) const { + return _node != other._node; + } + + ListConstIterator<T> &operator++() { + if (_node) _node = _node->next; + return *this; + } + + ListConstIterator<T> &operator+=(size_t distance) { + while (_node && distance) { + _node = _node->next; + --distance; + } + return *this; + } + + private: + const ListNode<T> *_node; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ListIterator.hpp b/include/lib/ArduinoJson/Data/ListIterator.hpp new file mode 100644 index 0000000..01fa287 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ListIterator.hpp @@ -0,0 +1,60 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "ListConstIterator.hpp" +#include "ListNode.hpp" + +namespace ArduinoJson { +namespace Internals { + +template <typename T> +class List; + +// A read-write forward iterator for List<T> +template <typename T> +class ListIterator { + friend class List<T>; + + public: + explicit ListIterator(ListNode<T> *node = NULL) : _node(node) {} + + T &operator*() const { + return _node->content; + } + T *operator->() { + return &_node->content; + } + + bool operator==(const ListIterator<T> &other) const { + return _node == other._node; + } + + bool operator!=(const ListIterator<T> &other) const { + return _node != other._node; + } + + ListIterator<T> &operator++() { + if (_node) _node = _node->next; + return *this; + } + + ListIterator<T> &operator+=(size_t distance) { + while (_node && distance) { + _node = _node->next; + --distance; + } + return *this; + } + + operator ListConstIterator<T>() const { + return ListConstIterator<T>(_node); + } + + private: + ListNode<T> *_node; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ListNode.hpp b/include/lib/ArduinoJson/Data/ListNode.hpp new file mode 100644 index 0000000..c090712 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ListNode.hpp @@ -0,0 +1,24 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include <stddef.h> // for NULL + +#include "JsonBufferAllocated.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A node for a singly-linked list. +// Used by List<T> and its iterators. +template <typename T> +struct ListNode : public Internals::JsonBufferAllocated { + ListNode() throw() : next(NULL) {} + + ListNode<T> *next; + T content; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/NonCopyable.hpp b/include/lib/ArduinoJson/Data/NonCopyable.hpp new file mode 100644 index 0000000..73f3d8e --- /dev/null +++ b/include/lib/ArduinoJson/Data/NonCopyable.hpp @@ -0,0 +1,23 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A type that cannot be copied +class NonCopyable { + protected: + NonCopyable() {} + + private: + // copy constructor is private + NonCopyable(const NonCopyable&); + + // copy operator is private + NonCopyable& operator=(const NonCopyable&); +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ReferenceType.hpp b/include/lib/ArduinoJson/Data/ReferenceType.hpp new file mode 100644 index 0000000..1e49117 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ReferenceType.hpp @@ -0,0 +1,24 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A type that is meant to be used by reference only (JsonArray and JsonObject) +class ReferenceType { + public: + bool operator==(const ReferenceType& other) const { + // two JsonArray are equal if they are the same instance + // (we don't compare the content) + return this == &other; + } + + bool operator!=(const ReferenceType& other) const { + return this != &other; + } +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ValueSaver.hpp b/include/lib/ArduinoJson/Data/ValueSaver.hpp new file mode 100644 index 0000000..9750f1a --- /dev/null +++ b/include/lib/ArduinoJson/Data/ValueSaver.hpp @@ -0,0 +1,52 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonBuffer.hpp" +#include "../JsonVariant.hpp" +#include "../StringTraits/StringTraits.hpp" +#include "../TypeTraits/EnableIf.hpp" + +namespace ArduinoJson { +namespace Internals { + +template <typename Source, typename Enable = void> +struct ValueSaver { + template <typename Destination> + static bool save(JsonBuffer*, Destination& destination, Source source) { + destination = source; + return true; + } +}; + +template <typename Source> +struct ValueSaver< + Source, typename EnableIf<StringTraits<Source>::should_duplicate>::type> { + template <typename Destination> + static bool save(JsonBuffer* buffer, Destination& dest, Source source) { + if (!StringTraits<Source>::is_null(source)) { + typename StringTraits<Source>::duplicate_t dup = + StringTraits<Source>::duplicate(source, buffer); + if (!dup) return false; + dest = dup; + } else { + dest = reinterpret_cast<const char*>(0); + } + return true; + } +}; + +// const char*, const signed char*, const unsigned char* +template <typename Char> +struct ValueSaver< + Char*, typename EnableIf<!StringTraits<Char*>::should_duplicate>::type> { + template <typename Destination> + static bool save(JsonBuffer*, Destination& dest, Char* source) { + dest = reinterpret_cast<const char*>(source); + return true; + } +}; +} +} |