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/StaticJsonBuffer.hpp | 126 +++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 include/lib/ArduinoJson/StaticJsonBuffer.hpp (limited to 'include/lib/ArduinoJson/StaticJsonBuffer.hpp') diff --git a/include/lib/ArduinoJson/StaticJsonBuffer.hpp b/include/lib/ArduinoJson/StaticJsonBuffer.hpp new file mode 100644 index 0000000..267d9d0 --- /dev/null +++ b/include/lib/ArduinoJson/StaticJsonBuffer.hpp @@ -0,0 +1,126 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonBufferBase.hpp" + +namespace ArduinoJson { +namespace Internals { + +class StaticJsonBufferBase : public JsonBufferBase { + public: + class String { + public: + String(StaticJsonBufferBase* parent) : _parent(parent) { + _start = parent->_buffer + parent->_size; + } + + void append(char c) { + if (_parent->canAlloc(1)) { + char* last = static_cast(_parent->doAlloc(1)); + *last = c; + } + } + + const char* c_str() const { + if (_parent->canAlloc(1)) { + char* last = static_cast(_parent->doAlloc(1)); + *last = '\0'; + return _start; + } else { + return NULL; + } + } + + private: + StaticJsonBufferBase* _parent; + char* _start; + }; + + StaticJsonBufferBase(char* buffer, size_t capa) + : _buffer(buffer), _capacity(capa), _size(0) {} + + // Gets the capacity of the buffer in bytes + size_t capacity() const { + return _capacity; + } + + // Gets the current usage of the buffer in bytes + size_t size() const { + return _size; + } + + // Allocates the specified amount of bytes in the buffer + virtual void* alloc(size_t bytes) { + alignNextAlloc(); + if (!canAlloc(bytes)) return NULL; + return doAlloc(bytes); + } + + // Resets the buffer. + // USE WITH CAUTION: this invalidates all previously allocated data + void clear() { + _size = 0; + } + + String startString() { + return String(this); + } + + protected: + ~StaticJsonBufferBase() {} + + private: + void alignNextAlloc() { + _size = round_size_up(_size); + } + + bool canAlloc(size_t bytes) const { + return _size + bytes <= _capacity; + } + + void* doAlloc(size_t bytes) { + void* p = &_buffer[_size]; + _size += bytes; + return p; + } + + char* _buffer; + size_t _capacity; + size_t _size; +}; +} + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnon-virtual-dtor" +#elif defined(__GNUC__) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#pragma GCC diagnostic push +#endif +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#endif + +// Implements a JsonBuffer with fixed memory allocation. +// The template paramenter CAPACITY specifies the capacity of the buffer in +// bytes. +template +class StaticJsonBuffer : public Internals::StaticJsonBufferBase { + public: + explicit StaticJsonBuffer() + : Internals::StaticJsonBufferBase(_buffer, CAPACITY) {} + + private: + char _buffer[CAPACITY]; +}; +} + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#pragma GCC diagnostic pop +#endif +#endif -- cgit v1.2.3