Removed global new operator overload (issue #40, #45 and #46)

This commit is contained in:
Benoit Blanchon
2015-02-01 20:59:31 +01:00
parent dadd8986dc
commit 8db338ba14
10 changed files with 42 additions and 35 deletions

View File

@ -1,6 +1,12 @@
Arduino JSON: change log Arduino JSON: change log
======================== ========================
HEAD
----
* Removed global new operator overload (issue #40, #45 and #46)
* Added an example with EthernetServer
v4.1 v4.1
---- ----

View File

@ -0,0 +1,21 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include "../JsonBuffer.hpp"
namespace ArduinoJson {
namespace Internals {
class JsonBufferAllocated {
public:
void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() {
return jsonBuffer->alloc(n);
}
};
}
}

View File

@ -9,7 +9,6 @@
#include "../JsonBuffer.hpp" #include "../JsonBuffer.hpp"
#include "ListConstIterator.hpp" #include "ListConstIterator.hpp"
#include "ListIterator.hpp" #include "ListIterator.hpp"
#include "PlacementNew.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
@ -51,8 +50,7 @@ class List {
protected: protected:
node_type *createNode() { node_type *createNode() {
if (!_buffer) return NULL; if (!_buffer) return NULL;
void *ptr = _buffer->alloc(sizeof(node_type)); return new (_buffer) node_type();
return ptr ? new (ptr) node_type() : NULL;
} }
void addNode(node_type *nodeToAdd) { void addNode(node_type *nodeToAdd) {

View File

@ -8,16 +8,18 @@
#include <stddef.h> // for NULL #include <stddef.h> // for NULL
#include "JsonBufferAllocated.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
// A node for a singly-linked list. // A node for a singly-linked list.
// Used by List<T> and its iterators. // Used by List<T> and its iterators.
template <typename T> template <typename T>
struct ListNode { struct ListNode : public Internals::JsonBufferAllocated {
ListNode() : next(NULL) {} ListNode() : next(NULL) {}
ListNode<T>* next; ListNode<T> *next;
T content; T content;
}; };
} }

View File

@ -1,19 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#ifdef ARDUINO
// Declares the placement new as in <new>.
// This is required for Arduino IDE because it doesn't include the <new> header.
inline void *operator new(size_t, void *p) throw() { return p; }
#else
#include <new>
#endif

View File

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "Internals/JsonBufferAllocated.hpp"
#include "Internals/JsonPrintable.hpp" #include "Internals/JsonPrintable.hpp"
#include "Internals/List.hpp" #include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp" #include "Internals/ReferenceType.hpp"
@ -30,7 +31,8 @@ class JsonBuffer;
// It can also be deserialized from a JSON string via JsonBuffer::parseArray(). // It can also be deserialized from a JSON string via JsonBuffer::parseArray().
class JsonArray : public Internals::JsonPrintable<JsonArray>, class JsonArray : public Internals::JsonPrintable<JsonArray>,
public Internals::ReferenceType, public Internals::ReferenceType,
public Internals::List<JsonVariant> { public Internals::List<JsonVariant>,
public Internals::JsonBufferAllocated {
// JsonBuffer is a friend because it needs to call the private constructor. // JsonBuffer is a friend because it needs to call the private constructor.
friend class JsonBuffer; friend class JsonBuffer;

View File

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "Internals/JsonBufferAllocated.hpp"
#include "Internals/JsonPrintable.hpp" #include "Internals/JsonPrintable.hpp"
#include "Internals/List.hpp" #include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp" #include "Internals/ReferenceType.hpp"
@ -30,7 +31,8 @@ class JsonBuffer;
// It can also be deserialized from a JSON string via JsonBuffer::parseObject(). // It can also be deserialized from a JSON string via JsonBuffer::parseObject().
class JsonObject : public Internals::JsonPrintable<JsonObject>, class JsonObject : public Internals::JsonPrintable<JsonObject>,
public Internals::ReferenceType, public Internals::ReferenceType,
public Internals::List<JsonPair> { public Internals::List<JsonPair>,
public Internals::JsonBufferAllocated {
// JsonBuffer is a friend because it needs to call the private constructor. // JsonBuffer is a friend because it needs to call the private constructor.
friend class JsonBuffer; friend class JsonBuffer;

View File

@ -6,7 +6,6 @@
#include "../../include/ArduinoJson/Internals/List.hpp" #include "../../include/ArduinoJson/Internals/List.hpp"
#include "../../include/ArduinoJson/Internals/PlacementNew.hpp"
#include "../../include/ArduinoJson/JsonPair.hpp" #include "../../include/ArduinoJson/JsonPair.hpp"
#include "../../include/ArduinoJson/JsonVariant.hpp" #include "../../include/ArduinoJson/JsonVariant.hpp"

View File

@ -7,7 +7,6 @@
#include "../include/ArduinoJson/JsonBuffer.hpp" #include "../include/ArduinoJson/JsonBuffer.hpp"
#include "../include/ArduinoJson/Internals/JsonParser.hpp" #include "../include/ArduinoJson/Internals/JsonParser.hpp"
#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
#include "../include/ArduinoJson/JsonArray.hpp" #include "../include/ArduinoJson/JsonArray.hpp"
#include "../include/ArduinoJson/JsonObject.hpp" #include "../include/ArduinoJson/JsonObject.hpp"
@ -15,15 +14,13 @@ using namespace ArduinoJson;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
JsonArray &JsonBuffer::createArray() { JsonArray &JsonBuffer::createArray() {
void *ptr = alloc(sizeof(JsonArray)); JsonArray *ptr = new (this) JsonArray(this);
if (ptr) return *new (ptr) JsonArray(this); return ptr ? *ptr : JsonArray::invalid();
return JsonArray::invalid();
} }
JsonObject &JsonBuffer::createObject() { JsonObject &JsonBuffer::createObject() {
void *ptr = alloc(sizeof(JsonObject)); JsonObject *ptr = new (this) JsonObject(this);
if (ptr) return *new (ptr) JsonObject(this); return ptr ? *ptr : JsonObject::invalid();
return JsonObject::invalid();
} }
JsonArray &JsonBuffer::parseArray(char *json, uint8_t nestingLimit) { JsonArray &JsonBuffer::parseArray(char *json, uint8_t nestingLimit) {

View File

@ -8,7 +8,6 @@
#include <string.h> // for strcmp #include <string.h> // for strcmp
#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
#include "../include/ArduinoJson/Internals/StringBuilder.hpp" #include "../include/ArduinoJson/Internals/StringBuilder.hpp"
#include "../include/ArduinoJson/JsonArray.hpp" #include "../include/ArduinoJson/JsonArray.hpp"
#include "../include/ArduinoJson/JsonBuffer.hpp" #include "../include/ArduinoJson/JsonBuffer.hpp"