Added namespace

This commit is contained in:
Benoît Blanchon
2014-07-03 13:54:27 +02:00
parent 538b15b400
commit bae5c36f41
12 changed files with 248 additions and 212 deletions

View File

@ -8,10 +8,14 @@
#include "JsonObjectBase.h" #include "JsonObjectBase.h"
#include "StringBuilder.h" #include "StringBuilder.h"
template<int N> namespace ArduinoJson
class JsonArray : public JsonObjectBase
{ {
public: namespace Generator
{
template<int N>
class JsonArray : public JsonObjectBase
{
public:
JsonArray() JsonArray()
{ {
itemCount = 0; itemCount = 0;
@ -23,7 +27,7 @@ public:
add(JsonValue(value)); add(JsonValue(value));
} }
void add(double value, int digits=2) void add(double value, int digits = 2)
{ {
add(JsonValue(value, digits)); add(JsonValue(value, digits));
} }
@ -38,7 +42,7 @@ public:
using JsonObjectBase::printTo; using JsonObjectBase::printTo;
private: private:
JsonValue items[N]; JsonValue items[N];
int itemCount; int itemCount;
@ -62,5 +66,6 @@ private:
return n; return n;
} }
}; };
}
}

View File

@ -7,10 +7,14 @@
#include "JsonObjectBase.h" #include "JsonObjectBase.h"
template<int N> namespace ArduinoJson
class JsonHashTable : public JsonObjectBase
{ {
public: namespace Generator
{
template<int N>
class JsonHashTable : public JsonObjectBase
{
public:
JsonHashTable() JsonHashTable()
{ {
@ -23,7 +27,7 @@ public:
add(key, JsonValue(value)); add(key, JsonValue(value));
} }
void add(const char* key, double value, int digits=2) void add(const char* key, double value, int digits = 2)
{ {
add(key, JsonValue(value, digits)); add(key, JsonValue(value, digits));
} }
@ -39,7 +43,7 @@ public:
using JsonObjectBase::printTo; using JsonObjectBase::printTo;
private: private:
struct KeyValuePair struct KeyValuePair
{ {
@ -74,5 +78,6 @@ private:
return n; return n;
} }
}; };
}
}

View File

@ -9,9 +9,13 @@
#include "Print.h" #include "Print.h"
#include "Printable.h" #include "Printable.h"
class JsonObjectBase : public Printable namespace ArduinoJson
{ {
public: namespace Generator
{
class JsonObjectBase : public Printable
{
public:
size_t printTo(char* buffer, size_t bufferSize) size_t printTo(char* buffer, size_t bufferSize)
{ {
@ -20,5 +24,6 @@ public:
} }
virtual size_t printTo(Print& p) const = 0; virtual size_t printTo(Print& p) const = 0;
}; };
}
}

View File

@ -5,6 +5,8 @@
#include "JsonValue.h" #include "JsonValue.h"
using namespace ArduinoJson::Generator;
size_t JsonValue::printBoolTo(Print& p) const size_t JsonValue::printBoolTo(Print& p) const
{ {
return p.print(content.asBool ? "true" : "false"); return p.print(content.asBool ? "true" : "false");

View File

@ -8,9 +8,13 @@
#include "Printable.h" #include "Printable.h"
#include "StringBuilder.h" #include "StringBuilder.h"
class JsonValue : public Printable namespace ArduinoJson
{ {
public: namespace Generator
{
class JsonValue : public Printable
{
public:
JsonValue() JsonValue()
{ {
@ -22,7 +26,7 @@ public:
content.asBool = value; content.asBool = value;
} }
JsonValue(double value, int digits=2) JsonValue(double value, int digits = 2)
: implementation(&JsonValue::printDoubleTo) : implementation(&JsonValue::printDoubleTo)
{ {
content.asDouble.value = value; content.asDouble.value = value;
@ -65,7 +69,7 @@ public:
return (this->*implementation)(p); return (this->*implementation)(p);
} }
private: private:
union Content union Content
{ {
@ -91,4 +95,6 @@ private:
size_t printLongTo(Print& p) const; size_t printLongTo(Print& p) const;
size_t printPrintableTo(Print& p) const; size_t printPrintableTo(Print& p) const;
size_t printStringTo(Print& p) const; size_t printStringTo(Print& p) const;
}; };
}
}

View File

@ -5,6 +5,8 @@
#include "StringBuilder.h" #include "StringBuilder.h"
using namespace ArduinoJson::Generator;
size_t StringBuilder::write(uint8_t c) size_t StringBuilder::write(uint8_t c)
{ {
if (length >= capacity) return 0; if (length >= capacity) return 0;

View File

@ -7,20 +7,25 @@
#include "Print.h" #include "Print.h"
class StringBuilder : public Print namespace ArduinoJson
{ {
public: namespace Generator
{
class StringBuilder : public Print
{
public:
StringBuilder(char* buf, int size) StringBuilder(char* buf, int size)
: buffer(buf), capacity(size-1), length(0) : buffer(buf), capacity(size - 1), length(0)
{ {
buffer[0] = 0; buffer[0] = 0;
} }
virtual size_t write(uint8_t c); virtual size_t write(uint8_t c);
private: private:
char* buffer; char* buffer;
int capacity; int capacity;
int length; int length;
}; };
}
}

View File

@ -3,6 +3,7 @@
#include "JsonHashTable.h" #include "JsonHashTable.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace ArduinoJson::Generator;
namespace JsonGeneratorTests namespace JsonGeneratorTests
{ {

View File

@ -3,6 +3,7 @@
#include "JsonHashTable.h" #include "JsonHashTable.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace ArduinoJson::Generator;
namespace JsonGeneratorTests namespace JsonGeneratorTests
{ {

View File

@ -3,6 +3,7 @@
#include "JsonValue.h" #include "JsonValue.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace ArduinoJson::Generator;
namespace JsonGeneratorTests namespace JsonGeneratorTests
{ {

View File

@ -2,6 +2,7 @@
#include "StringBuilder.h" #include "StringBuilder.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace ArduinoJson::Generator;
namespace JsonGeneratorTests namespace JsonGeneratorTests
{ {

View File

@ -5,6 +5,8 @@
#include <JsonGenerator.h> #include <JsonGenerator.h>
using namespace ArduinoJson::Generator;
void setup() void setup()
{ {
Serial.begin(9600); Serial.begin(9600);