StaticJsonBuffer.hpp 776 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright Benoit Blanchon 2014-2015
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #pragma once
  7. #include "JsonBuffer.hpp"
  8. namespace ArduinoJson {
  9. // Implements a JsonBuffer with fixed memory allocation.
  10. // The template paramenter CAPACITY specifies the capacity of the buffer in
  11. // bytes.
  12. template <size_t CAPACITY>
  13. class StaticJsonBuffer : public JsonBuffer {
  14. public:
  15. explicit StaticJsonBuffer() : _size(0) {}
  16. size_t capacity() const { return CAPACITY; }
  17. size_t size() const { return _size; }
  18. protected:
  19. virtual void* alloc(size_t bytes) {
  20. if (_size + bytes > CAPACITY) return NULL;
  21. void* p = &_buffer[_size];
  22. _size += bytes;
  23. return p;
  24. }
  25. private:
  26. uint8_t _buffer[CAPACITY];
  27. size_t _size;
  28. };
  29. }