StaticJsonBuffer.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #pragma once
  8. #include "JsonBuffer.hpp"
  9. #if defined(__clang__)
  10. #pragma clang diagnostic push
  11. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  12. #elif defined(__GNUC__)
  13. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  14. #pragma GCC diagnostic push
  15. #endif
  16. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  17. #endif
  18. namespace ArduinoJson {
  19. // Implements a JsonBuffer with fixed memory allocation.
  20. // The template paramenter CAPACITY specifies the capacity of the buffer in
  21. // bytes.
  22. template <size_t CAPACITY>
  23. class StaticJsonBuffer : public JsonBuffer {
  24. public:
  25. explicit StaticJsonBuffer() : _size(0) {}
  26. size_t capacity() const {
  27. return CAPACITY;
  28. }
  29. size_t size() const {
  30. return _size;
  31. }
  32. virtual void* alloc(size_t bytes) {
  33. if (_size + bytes > CAPACITY) return NULL;
  34. void* p = &_buffer[_size];
  35. _size += round_size_up(bytes);
  36. return p;
  37. }
  38. private:
  39. uint8_t _buffer[CAPACITY];
  40. size_t _size;
  41. };
  42. }
  43. #if defined(__clang__)
  44. #pragma clang diagnostic pop
  45. #elif defined(__GNUC__)
  46. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  47. #pragma GCC diagnostic pop
  48. #endif
  49. #endif