Skip to content

Commit

Permalink
Made JsonBuffer non-copyable (PR #524 by @luisrayas3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jun 17, 2017
1 parent e9d88dd commit 508f936
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
ArduinoJson: change log
=======================

HEAD
----

* Made `JsonBuffer` non-copyable (PR #524 by @luisrayas3)

v5.10.1
-------

Expand Down
26 changes: 26 additions & 0 deletions 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&);
};
}
}
10 changes: 0 additions & 10 deletions src/ArduinoJson/Data/ReferenceType.hpp
Expand Up @@ -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&);
};
}
}
1 change: 1 addition & 0 deletions src/ArduinoJson/JsonArray.hpp
Expand Up @@ -39,6 +39,7 @@ class JsonArraySubscript;
// It can also be deserialized from a JSON string via JsonBuffer::parseArray().
class JsonArray : public Internals::JsonPrintable<JsonArray>,
public Internals::ReferenceType,
public Internals::NonCopyable,
public Internals::List<JsonVariant>,
public Internals::JsonBufferAllocated {
public:
Expand Down
3 changes: 2 additions & 1 deletion src/ArduinoJson/JsonBuffer.hpp
Expand Up @@ -11,6 +11,7 @@
#include <stdint.h> // for uint8_t
#include <string.h>

#include "Data/NonCopyable.hpp"
#include "JsonVariant.hpp"
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsArray.hpp"
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/ArduinoJson/JsonObject.hpp
Expand Up @@ -38,6 +38,7 @@ class JsonBuffer;
// It can also be deserialized from a JSON string via JsonBuffer::parseObject().
class JsonObject : public Internals::JsonPrintable<JsonObject>,
public Internals::ReferenceType,
public Internals::NonCopyable,
public Internals::List<JsonPair>,
public Internals::JsonBufferAllocated {
public:
Expand Down
8 changes: 4 additions & 4 deletions test/DynamicJsonBuffer/alloc.cpp
Expand Up @@ -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
}
}

0 comments on commit 508f936

Please sign in to comment.