Removing virtual methods...

This commit is contained in:
Benoit Blanchon
2014-11-03 12:32:47 +01:00
parent 507f809da0
commit f26f4263ea
7 changed files with 52 additions and 44 deletions

View File

@ -19,7 +19,8 @@ namespace ArduinoJson {
class JsonObject;
class JsonBuffer;
class JsonArray : public JsonPrintable, public Internals::ReferenceType {
class JsonArray : public JsonPrintable<JsonArray>,
public Internals::ReferenceType {
friend class JsonBuffer;
public:
@ -55,7 +56,8 @@ class JsonArray : public JsonPrintable, public Internals::ReferenceType {
static JsonArray &invalid() { return _invalid; }
virtual void writeTo(Internals::JsonWriter &writer) const;
template <typename T>
void writeTo(T &writer) const;
private:
// constructor is private: instance must be created via a JsonBuffer

View File

@ -20,7 +20,8 @@ namespace ArduinoJson {
class JsonArray;
class JsonBuffer;
class JsonObject : public JsonPrintable, public Internals::ReferenceType {
class JsonObject : public JsonPrintable<JsonObject>,
public Internals::ReferenceType {
friend class JsonBuffer;
public:
@ -55,7 +56,8 @@ class JsonObject : public JsonPrintable, public Internals::ReferenceType {
static JsonObject &invalid() { return _invalid; }
virtual void writeTo(Internals::JsonWriter &writer) const;
template <typename T>
void writeTo(T &writer) const;
private:
// constructor is private, instance must be created via JsonBuffer

View File

@ -7,24 +7,44 @@
#pragma once
#include "Arduino/Printable.hpp"
#include "Internals/StringBuilder.hpp"
#include "Internals/IndentedPrint.hpp"
namespace ArduinoJson {
namespace Internals {
class IndentedPrint;
class JsonWriter;
}
template <typename T>
class JsonPrintable : public Printable {
public:
size_t printTo(char *buffer, size_t bufferSize) const;
virtual size_t printTo(Print &print) const;
size_t printTo(Print &print) const {
CompactJsonWriter writer(&p);
downcast().writeTo(writer);
return writer.bytesWritten();
}
size_t printTo(char *buffer, size_t bufferSize) const {
Internals::StringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
size_t prettyPrintTo(char *buffer, size_t bufferSize) const;
size_t prettyPrintTo(Internals::IndentedPrint &print) const;
size_t prettyPrintTo(Print &print) const;
size_t prettyPrintTo(Internals::IndentedPrint &print) const {
PrettyJsonWriter writer(&p);
downcast().writeTo(writer);
return writer.bytesWritten();
}
protected:
virtual void writeTo(Internals::JsonWriter &) const = 0;
size_t prettyPrintTo(char *buffer, size_t bufferSize) const {
Internals::StringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb);
}
size_t prettyPrintTo(Print &print) const {
Internals::IndentedPrint indentedPrint = Internals::IndentedPrint(print);
return prettyPrintTo(indentedPrint);
}
private:
const T &downcast() { return *static_cast<const T *>(this); }
};
}

View File

@ -68,7 +68,8 @@ class JsonValue {
bool success() { return _type != Internals::JSON_INVALID; }
void writeTo(Internals::JsonWriter &writer) const;
template <typename T>
void writeTo(T &writer) const;
private:
JsonValue(Internals::JsonValueType type) : _type(type) {}

View File

@ -10,7 +10,8 @@
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
#include "ArduinoJson/Internals/CompactJsonWriter.hpp"
#include "ArduinoJson/Internals/PrettyJsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
@ -68,7 +69,8 @@ JsonObject &JsonArray::createNestedObject() {
return object;
}
void JsonArray::writeTo(JsonWriter &writer) const {
template <typename T>
void JsonArray::writeTo(T &writer) const {
JsonArrayNode *child = _firstNode;
if (child) {
@ -88,3 +90,6 @@ void JsonArray::writeTo(JsonWriter &writer) const {
writer.writeEmptyArray();
}
}
template void JsonArray::writeTo<CompactJsonWriter>(CompactJsonWriter &) const;
template void JsonArray::writeTo<PrettyJsonWriter>(PrettyJsonWriter &) const;

View File

@ -12,7 +12,8 @@
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
#include "ArduinoJson/Internals/CompactJsonWriter.hpp"
#include "ArduinoJson/Internals/PrettyJsonWriter.hpp"
#include "ArduinoJson/Internals/StringBuilder.hpp"
using namespace ArduinoJson;
@ -91,7 +92,8 @@ void JsonObject::removeNode(JsonObjectNode *nodeToRemove) {
}
}
void JsonObject::writeTo(JsonWriter &writer) const {
template <typename T>
void JsonObject::writeTo(T &writer) const {
JsonObjectNode *node = _firstNode;
if (node) {
@ -113,3 +115,6 @@ void JsonObject::writeTo(JsonWriter &writer) const {
writer.writeEmptyObject();
}
}
template void JsonObject::writeTo(CompactJsonWriter &writer) const;
template void JsonObject::writeTo(PrettyJsonWriter &writer) const;

View File

@ -13,30 +13,3 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
size_t JsonPrintable::printTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
size_t JsonPrintable::printTo(Print &p) const {
CompactJsonWriter writer(&p);
writeTo(writer);
return writer.bytesWritten();
}
size_t JsonPrintable::prettyPrintTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb);
}
size_t JsonPrintable::prettyPrintTo(IndentedPrint &p) const {
PrettyJsonWriter writer(&p);
writeTo(writer);
return writer.bytesWritten();
}
size_t JsonPrintable::prettyPrintTo(Print &print) const {
IndentedPrint indentedPrint = IndentedPrint(print);
return prettyPrintTo(indentedPrint);
}