diff --git a/CHANGELOG.md b/CHANGELOG.md index e273add69..9a9890396 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Made `JsonBuffer` non-copyable (PR #524 by @luisrayas3) + v5.10.1 ------- diff --git a/src/ArduinoJson/Data/NonCopyable.hpp b/src/ArduinoJson/Data/NonCopyable.hpp new file mode 100644 index 000000000..98ebd8fb3 --- /dev/null +++ b/src/ArduinoJson/Data/NonCopyable.hpp @@ -0,0 +1,26 @@ +// Copyright Benoit Blanchon 2014-2017 +// MIT License +// +// Arduino JSON library +// https://bblanchon.github.io/ArduinoJson/ +// If you like this project, please add a star! + +#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/src/ArduinoJson/Data/ReferenceType.hpp b/src/ArduinoJson/Data/ReferenceType.hpp index ea394d5c9..bbc9046be 100644 --- a/src/ArduinoJson/Data/ReferenceType.hpp +++ b/src/ArduinoJson/Data/ReferenceType.hpp @@ -22,16 +22,6 @@ class ReferenceType { bool operator!=(const ReferenceType& other) const { return this != &other; } - - protected: - ReferenceType() {} - - private: - // copy constructor is private - ReferenceType(const ReferenceType&); - - // copy operator is private - ReferenceType& operator=(const ReferenceType&); }; } } diff --git a/src/ArduinoJson/JsonArray.hpp b/src/ArduinoJson/JsonArray.hpp index a87d25484..f6fdf9969 100644 --- a/src/ArduinoJson/JsonArray.hpp +++ b/src/ArduinoJson/JsonArray.hpp @@ -39,6 +39,7 @@ class JsonArraySubscript; // It can also be deserialized from a JSON string via JsonBuffer::parseArray(). class JsonArray : public Internals::JsonPrintable, public Internals::ReferenceType, + public Internals::NonCopyable, public Internals::List, public Internals::JsonBufferAllocated { public: diff --git a/src/ArduinoJson/JsonBuffer.hpp b/src/ArduinoJson/JsonBuffer.hpp index c669ae7e3..fafbb1bc4 100644 --- a/src/ArduinoJson/JsonBuffer.hpp +++ b/src/ArduinoJson/JsonBuffer.hpp @@ -11,6 +11,7 @@ #include // for uint8_t #include +#include "Data/NonCopyable.hpp" #include "JsonVariant.hpp" #include "TypeTraits/EnableIf.hpp" #include "TypeTraits/IsArray.hpp" @@ -34,7 +35,7 @@ class JsonObject; // Handle the memory management (done in derived classes) and calls the parser. // This abstract class is implemented by StaticJsonBuffer which implements a // fixed memory allocation. -class JsonBuffer { +class JsonBuffer : Internals::NonCopyable { public: // CAUTION: NO VIRTUAL DESTRUCTOR! // If we add a virtual constructor the Arduino compiler will add malloc() and diff --git a/src/ArduinoJson/JsonObject.hpp b/src/ArduinoJson/JsonObject.hpp index da61c8938..136bf4155 100644 --- a/src/ArduinoJson/JsonObject.hpp +++ b/src/ArduinoJson/JsonObject.hpp @@ -38,6 +38,7 @@ class JsonBuffer; // It can also be deserialized from a JSON string via JsonBuffer::parseObject(). class JsonObject : public Internals::JsonPrintable, public Internals::ReferenceType, + public Internals::NonCopyable, public Internals::List, public Internals::JsonBufferAllocated { public: diff --git a/test/DynamicJsonBuffer/alloc.cpp b/test/DynamicJsonBuffer/alloc.cpp index 349265ed4..278717519 100644 --- a/test/DynamicJsonBuffer/alloc.cpp +++ b/test/DynamicJsonBuffer/alloc.cpp @@ -36,10 +36,10 @@ TEST_CASE("DynamicJsonBuffer::alloc()") { SECTION("Alignment") { // make room for two but not three - buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1); + DynamicJsonBuffer tinyBuf(2 * sizeof(void*) + 1); - REQUIRE(isAligned(buffer.alloc(1))); // this on is aligned by design - REQUIRE(isAligned(buffer.alloc(1))); // this one fits in the first block - REQUIRE(isAligned(buffer.alloc(1))); // this one requires a new block + REQUIRE(isAligned(tinyBuf.alloc(1))); // this on is aligned by design + REQUIRE(isAligned(tinyBuf.alloc(1))); // this one fits in the first block + REQUIRE(isAligned(tinyBuf.alloc(1))); // this one requires a new block } }