Sfoglia il codice sorgente

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

Benoit Blanchon 11 anni fa
parent
commit
8db338ba14

+ 6 - 0
CHANGELOG.md

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

+ 21 - 0
include/ArduinoJson/Internals/JsonBufferAllocated.hpp

@@ -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);
+  }
+};
+}
+}

+ 1 - 3
include/ArduinoJson/Internals/List.hpp

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

+ 4 - 2
include/ArduinoJson/Internals/ListNode.hpp

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

+ 0 - 19
include/ArduinoJson/Internals/PlacementNew.hpp

@@ -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

+ 3 - 1
include/ArduinoJson/JsonArray.hpp

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include "Internals/JsonBufferAllocated.hpp"
 #include "Internals/JsonPrintable.hpp"
 #include "Internals/List.hpp"
 #include "Internals/ReferenceType.hpp"
@@ -30,7 +31,8 @@ class JsonBuffer;
 // It can also be deserialized from a JSON string via JsonBuffer::parseArray().
 class JsonArray : public Internals::JsonPrintable<JsonArray>,
                   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.
   friend class JsonBuffer;
 

+ 3 - 1
include/ArduinoJson/JsonObject.hpp

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include "Internals/JsonBufferAllocated.hpp"
 #include "Internals/JsonPrintable.hpp"
 #include "Internals/List.hpp"
 #include "Internals/ReferenceType.hpp"
@@ -30,7 +31,8 @@ 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::List<JsonPair> {
+                   public Internals::List<JsonPair>,
+                   public Internals::JsonBufferAllocated {
   // JsonBuffer is a friend because it needs to call the private constructor.
   friend class JsonBuffer;
 

+ 0 - 1
src/Internals/List.cpp

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

+ 4 - 7
src/JsonBuffer.cpp

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

+ 0 - 1
src/JsonObject.cpp

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