From 508f93631712e670420b58eb468ffe45072513bc Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 17 Jun 2017 14:17:01 +0200 Subject: [PATCH] Made `JsonBuffer` non-copyable (PR #524 by @luisrayas3) --- CHANGELOG.md | 5 +++++ src/ArduinoJson/Data/NonCopyable.hpp | 26 ++++++++++++++++++++++++++ src/ArduinoJson/Data/ReferenceType.hpp | 10 ---------- src/ArduinoJson/JsonArray.hpp | 1 + src/ArduinoJson/JsonBuffer.hpp | 3 ++- src/ArduinoJson/JsonObject.hpp | 1 + test/DynamicJsonBuffer/alloc.cpp | 8 ++++---- 7 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 src/ArduinoJson/Data/NonCopyable.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index e273add6..9a989039 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 00000000..98ebd8fb --- /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 ea394d5c..bbc9046b 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 a87d2548..f6fdf996 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 c669ae7e..fafbb1bc 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 da61c893..136bf415 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 349265ed..27871751 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 } }