From 4d5a7114c1416bf82b3448484786c0467f1965f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Tue, 1 Jul 2014 13:44:36 +0200 Subject: [PATCH] Added interface Printable. --- JsonGeneratorTests/JsonArray.h | 2 +- JsonGeneratorTests/JsonGeneratorTests.vcxproj | 1 + .../JsonGeneratorTests.vcxproj.filters | 3 +++ JsonGeneratorTests/JsonHashTable.h | 2 +- JsonGeneratorTests/JsonObjectBase.h | 5 ++-- JsonGeneratorTests/JsonValue.cpp | 8 +++--- JsonGeneratorTests/JsonValue.h | 27 +++++++++---------- JsonGeneratorTests/Printable.h | 16 +++++++++++ 8 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 JsonGeneratorTests/Printable.h diff --git a/JsonGeneratorTests/JsonArray.h b/JsonGeneratorTests/JsonArray.h index 9d612684..cf1bea97 100644 --- a/JsonGeneratorTests/JsonArray.h +++ b/JsonGeneratorTests/JsonArray.h @@ -37,7 +37,7 @@ private: JsonValue items[N]; int itemCount; - virtual size_t writeTo(Print& p) + virtual size_t writeTo(Print& p) const { size_t n = 0; diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj b/JsonGeneratorTests/JsonGeneratorTests.vcxproj index 3de0894e..67807102 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj @@ -94,6 +94,7 @@ + diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters index db203657..0ac6b8d9 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters @@ -50,5 +50,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/JsonGeneratorTests/JsonHashTable.h b/JsonGeneratorTests/JsonHashTable.h index aca50bf7..f2894d73 100644 --- a/JsonGeneratorTests/JsonHashTable.h +++ b/JsonGeneratorTests/JsonHashTable.h @@ -45,7 +45,7 @@ private: KeyValuePair items[N]; int itemCount; - virtual size_t writeTo(Print& p) + virtual size_t writeTo(Print& p) const { size_t n = 0; diff --git a/JsonGeneratorTests/JsonObjectBase.h b/JsonGeneratorTests/JsonObjectBase.h index f8ffd3f9..7daad792 100644 --- a/JsonGeneratorTests/JsonObjectBase.h +++ b/JsonGeneratorTests/JsonObjectBase.h @@ -7,8 +7,9 @@ #include "JsonValue.h" #include "Print.h" +#include "Printable.h" -class JsonObjectBase +class JsonObjectBase : public Printable { public: @@ -18,6 +19,6 @@ public: return writeTo(sb); } - virtual size_t writeTo(Print& p) = 0; + virtual size_t writeTo(Print& p) const = 0; }; diff --git a/JsonGeneratorTests/JsonValue.cpp b/JsonGeneratorTests/JsonValue.cpp index 284fef47..5a23e9b0 100644 --- a/JsonGeneratorTests/JsonValue.cpp +++ b/JsonGeneratorTests/JsonValue.cpp @@ -8,12 +8,12 @@ #include #include -size_t JsonValue::writeBooleanTo(Print& p) +size_t JsonValue::writeBooleanTo(Print& p) const { return p.write(content.boolean ? "true" : "false"); } -size_t JsonValue::writeNumberTo(Print& p) +size_t JsonValue::writeNumberTo(Print& p) const { char tmp[16]; @@ -22,7 +22,7 @@ size_t JsonValue::writeNumberTo(Print& p) return p.write(tmp); } -size_t JsonValue::writeObjectTo(Print& p) +size_t JsonValue::writeObjectTo(Print& p) const { if (content.object) return ((JsonObjectBase*)content.object)->writeTo(p); @@ -30,7 +30,7 @@ size_t JsonValue::writeObjectTo(Print& p) return p.write("null"); } -size_t JsonValue::writeStringTo(Print& p) +size_t JsonValue::writeStringTo(Print& p) const { auto s = content.string; diff --git a/JsonGeneratorTests/JsonValue.h b/JsonGeneratorTests/JsonValue.h index 9f98c4dd..5fb506b6 100644 --- a/JsonGeneratorTests/JsonValue.h +++ b/JsonGeneratorTests/JsonValue.h @@ -5,11 +5,10 @@ #pragma once +#include "Printable.h" #include "StringBuilder.h" -class JsonObjectBase; - -class JsonValue +class JsonValue : public Printable { public: @@ -35,13 +34,13 @@ public: content.boolean = value; } - JsonValue(JsonObjectBase& value) + JsonValue(Printable& value) : implementation(&JsonValue::writeObjectTo) { content.object = &value; } - size_t writeTo(Print& p) + virtual size_t writeTo(Print& p) const { // handmade polymorphism return (this->*implementation)(p); @@ -51,18 +50,18 @@ private: union Content { - bool boolean; - double number; - JsonObjectBase* object; - const char* string; + bool boolean; + double number; + Printable* object; + const char* string; }; Content content; - size_t(JsonValue::*implementation)(Print& p); + size_t(JsonValue::*implementation)(Print& p)const; - size_t writeBooleanTo(Print& p); - size_t writeNumberTo(Print& p); - size_t writeObjectTo(Print& p); - size_t writeStringTo(Print& p); + size_t writeBooleanTo(Print& p) const; + size_t writeNumberTo(Print& p) const; + size_t writeObjectTo(Print& p) const; + size_t writeStringTo(Print& p) const; }; \ No newline at end of file diff --git a/JsonGeneratorTests/Printable.h b/JsonGeneratorTests/Printable.h new file mode 100644 index 00000000..136aac7d --- /dev/null +++ b/JsonGeneratorTests/Printable.h @@ -0,0 +1,16 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +class Print; + +class Printable +{ +public: + + virtual size_t writeTo(Print& p) const = 0; +}; +