From 729bf0afd284b3302255c62631e9c00719987dcf Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 18 Jul 2017 22:00:06 +0200 Subject: [PATCH] Made JsonBuffer destructor protected --- src/ArduinoJson/JsonBuffer.hpp | 28 +++++----------------------- src/ArduinoJson/JsonBufferBase.hpp | 21 +++------------------ src/ArduinoJson/StaticJsonBuffer.hpp | 23 +++++++++++++---------- 3 files changed, 21 insertions(+), 51 deletions(-) diff --git a/src/ArduinoJson/JsonBuffer.hpp b/src/ArduinoJson/JsonBuffer.hpp index fafbb1bc..06bda0ef 100644 --- a/src/ArduinoJson/JsonBuffer.hpp +++ b/src/ArduinoJson/JsonBuffer.hpp @@ -16,16 +16,6 @@ #include "TypeTraits/EnableIf.hpp" #include "TypeTraits/IsArray.hpp" -#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 - namespace ArduinoJson { class JsonArray; class JsonObject; @@ -37,11 +27,6 @@ class JsonObject; // fixed memory allocation. class JsonBuffer : Internals::NonCopyable { public: - // CAUTION: NO VIRTUAL DESTRUCTOR! - // If we add a virtual constructor the Arduino compiler will add malloc() and - // free() to the binary, adding 706 useless bytes. - // virtual ~JsonBuffer() {} - // Allocates an empty JsonArray. // // Returns a reference to the new JsonArray or JsonArray::invalid() if the @@ -77,6 +62,11 @@ class JsonBuffer : Internals::NonCopyable { virtual void *alloc(size_t size) = 0; protected: + // CAUTION: NO VIRTUAL DESTRUCTOR! + // If we add a virtual constructor the Arduino compiler will add malloc() + // and free() to the binary, adding 706 useless bytes. + ~JsonBuffer() {} + // Preserve aligment if necessary static FORCE_INLINE size_t round_size_up(size_t bytes) { #if ARDUINOJSON_ENABLE_ALIGNMENT @@ -88,11 +78,3 @@ class JsonBuffer : Internals::NonCopyable { } }; } - -#if defined(__clang__) -#pragma clang diagnostic pop -#elif defined(__GNUC__) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -#pragma GCC diagnostic pop -#endif -#endif diff --git a/src/ArduinoJson/JsonBufferBase.hpp b/src/ArduinoJson/JsonBufferBase.hpp index 630d6afb..480cd830 100644 --- a/src/ArduinoJson/JsonBufferBase.hpp +++ b/src/ArduinoJson/JsonBufferBase.hpp @@ -9,16 +9,6 @@ #include "Deserialization/JsonParser.hpp" -#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 - namespace ArduinoJson { template class JsonBufferBase : public JsonBuffer { @@ -127,17 +117,12 @@ class JsonBufferBase : public JsonBuffer { return Internals::makeParser(that(), json, nestingLimit).parseVariant(); } + protected: + ~JsonBufferBase() {} + private: TDerived *that() { return static_cast(this); } }; } - -#if defined(__clang__) -#pragma clang diagnostic pop -#elif defined(__GNUC__) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -#pragma GCC diagnostic pop -#endif -#endif diff --git a/src/ArduinoJson/StaticJsonBuffer.hpp b/src/ArduinoJson/StaticJsonBuffer.hpp index 7d848524..8eaa4bb5 100644 --- a/src/ArduinoJson/StaticJsonBuffer.hpp +++ b/src/ArduinoJson/StaticJsonBuffer.hpp @@ -9,16 +9,6 @@ #include "JsonBufferBase.hpp" -#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 - namespace ArduinoJson { class StaticJsonBufferBase : public JsonBufferBase { @@ -81,6 +71,9 @@ class StaticJsonBufferBase : public JsonBufferBase { return String(this); } + protected: + ~StaticJsonBufferBase() {} + private: void alignNextAlloc() { _size = round_size_up(_size); @@ -101,6 +94,16 @@ class StaticJsonBufferBase : public JsonBufferBase { 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.