From 4f6253aa9fec99260b8bb7b9b2e9003f5259b600 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 17 Sep 2018 10:02:07 +0200 Subject: Import arduinojson and ubjson. Only partially working at the moment --- include/lib/ArduinoJson/Data/Encoding.hpp | 37 +++++++++ .../lib/ArduinoJson/Data/JsonBufferAllocated.hpp | 22 +++++ include/lib/ArduinoJson/Data/JsonFloat.hpp | 18 +++++ include/lib/ArduinoJson/Data/JsonInteger.hpp | 23 ++++++ include/lib/ArduinoJson/Data/JsonVariantAs.hpp | 42 ++++++++++ .../lib/ArduinoJson/Data/JsonVariantContent.hpp | 27 +++++++ .../lib/ArduinoJson/Data/JsonVariantDefault.hpp | 23 ++++++ include/lib/ArduinoJson/Data/JsonVariantType.hpp | 27 +++++++ include/lib/ArduinoJson/Data/List.hpp | 94 ++++++++++++++++++++++ include/lib/ArduinoJson/Data/ListConstIterator.hpp | 50 ++++++++++++ include/lib/ArduinoJson/Data/ListIterator.hpp | 60 ++++++++++++++ include/lib/ArduinoJson/Data/ListNode.hpp | 24 ++++++ include/lib/ArduinoJson/Data/NonCopyable.hpp | 23 ++++++ include/lib/ArduinoJson/Data/ReferenceType.hpp | 24 ++++++ include/lib/ArduinoJson/Data/ValueSaver.hpp | 52 ++++++++++++ 15 files changed, 546 insertions(+) create mode 100644 include/lib/ArduinoJson/Data/Encoding.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonFloat.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonInteger.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantAs.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantContent.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantDefault.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantType.hpp create mode 100644 include/lib/ArduinoJson/Data/List.hpp create mode 100644 include/lib/ArduinoJson/Data/ListConstIterator.hpp create mode 100644 include/lib/ArduinoJson/Data/ListIterator.hpp create mode 100644 include/lib/ArduinoJson/Data/ListNode.hpp create mode 100644 include/lib/ArduinoJson/Data/NonCopyable.hpp create mode 100644 include/lib/ArduinoJson/Data/ReferenceType.hpp create mode 100644 include/lib/ArduinoJson/Data/ValueSaver.hpp (limited to 'include/lib/ArduinoJson/Data') 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() +template +struct JsonVariantAs { + typedef T type; +}; + +template <> +struct JsonVariantAs { + typedef const char* type; +}; + +template <> +struct JsonVariantAs { + typedef JsonArray& type; +}; + +template <> +struct JsonVariantAs { + typedef const JsonArray& type; +}; + +template <> +struct JsonVariantAs { + typedef JsonObject& type; +}; + +template <> +struct JsonVariantAs { + 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 +struct JsonVariantDefault { + static T get() { + return T(); + } +}; + +template +struct JsonVariantDefault : JsonVariantDefault {}; + +template +struct JsonVariantDefault : JsonVariantDefault {}; +} +} 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. +// It is derived by JsonArray and JsonObject +template +class List { + public: + typedef T value_type; + typedef ListNode node_type; + typedef ListIterator iterator; + typedef ListConstIterator const_iterator; + + // Creates an empty List 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 +template +class ListConstIterator { + public: + explicit ListConstIterator(const ListNode *node = NULL) : _node(node) {} + + const T &operator*() const { + return _node->content; + } + const T *operator->() { + return &_node->content; + } + + bool operator==(const ListConstIterator &other) const { + return _node == other._node; + } + + bool operator!=(const ListConstIterator &other) const { + return _node != other._node; + } + + ListConstIterator &operator++() { + if (_node) _node = _node->next; + return *this; + } + + ListConstIterator &operator+=(size_t distance) { + while (_node && distance) { + _node = _node->next; + --distance; + } + return *this; + } + + private: + const ListNode *_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 +class List; + +// A read-write forward iterator for List +template +class ListIterator { + friend class List; + + public: + explicit ListIterator(ListNode *node = NULL) : _node(node) {} + + T &operator*() const { + return _node->content; + } + T *operator->() { + return &_node->content; + } + + bool operator==(const ListIterator &other) const { + return _node == other._node; + } + + bool operator!=(const ListIterator &other) const { + return _node != other._node; + } + + ListIterator &operator++() { + if (_node) _node = _node->next; + return *this; + } + + ListIterator &operator+=(size_t distance) { + while (_node && distance) { + _node = _node->next; + --distance; + } + return *this; + } + + operator ListConstIterator() const { + return ListConstIterator(_node); + } + + private: + ListNode *_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 // for NULL + +#include "JsonBufferAllocated.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A node for a singly-linked list. +// Used by List and its iterators. +template +struct ListNode : public Internals::JsonBufferAllocated { + ListNode() throw() : next(NULL) {} + + ListNode *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 +struct ValueSaver { + template + static bool save(JsonBuffer*, Destination& destination, Source source) { + destination = source; + return true; + } +}; + +template +struct ValueSaver< + Source, typename EnableIf::should_duplicate>::type> { + template + static bool save(JsonBuffer* buffer, Destination& dest, Source source) { + if (!StringTraits::is_null(source)) { + typename StringTraits::duplicate_t dup = + StringTraits::duplicate(source, buffer); + if (!dup) return false; + dest = dup; + } else { + dest = reinterpret_cast(0); + } + return true; + } +}; + +// const char*, const signed char*, const unsigned char* +template +struct ValueSaver< + Char*, typename EnableIf::should_duplicate>::type> { + template + static bool save(JsonBuffer*, Destination& dest, Char* source) { + dest = reinterpret_cast(source); + return true; + } +}; +} +} -- cgit v1.2.3