Fixed object keys not being duplicated

This commit is contained in:
Benoit Blanchon
2018-10-04 11:16:23 +02:00
parent 527dc19794
commit 6b985b2d1f
30 changed files with 368 additions and 218 deletions

View File

@@ -12,16 +12,16 @@ class ArduinoString {
public:
ArduinoString(const ::String& str) : _str(&str) {}
template <typename Buffer>
const char* save(Buffer* memoryPool) const {
if (is_null()) return NULL;
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
if (isNull()) return NULL;
size_t n = _str->length() + 1;
void* dup = memoryPool->alloc(n);
if (dup != NULL) memcpy(dup, _str->c_str(), n);
return static_cast<const char*>(dup);
}
bool is_null() const {
bool isNull() const {
// Arduino's String::c_str() can return NULL
return !_str->c_str();
}

View File

@@ -17,12 +17,12 @@ class FixedSizeFlashString {
return strncmp_P(expected, actual, _size) == 0;
}
bool is_null() const {
bool isNull() const {
return !_str;
}
template <typename Buffer>
const char* save(Buffer* memoryPool) const {
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
void* dup = memoryPool->alloc(_size);
if (dup != NULL) memcpy_P(dup, (const char*)_str, _size);

View File

@@ -18,12 +18,12 @@ class FixedSizeRamString {
return strcmp(actual, expected) == 0;
}
bool is_null() const {
bool isNull() const {
return !_str;
}
template <typename Buffer>
const char* save(Buffer* memoryPool) const {
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
void* dup = memoryPool->alloc(_size);
if (!dup) return NULL;

View File

@@ -12,15 +12,15 @@ class StlString {
public:
StlString(const std::string& str) : _str(&str) {}
template <typename Buffer>
const char* save(Buffer* memoryPool) const {
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
size_t n = _str->length() + 1;
void* dup = memoryPool->alloc(n);
if (dup != NULL) memcpy(dup, _str->c_str(), n);
return static_cast<const char*>(dup);
}
bool is_null() const {
bool isNull() const {
return false;
}

View File

@@ -0,0 +1,16 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "ZeroTerminatedRamStringConst.hpp"
namespace ARDUINOJSON_NAMESPACE {
class StringInMemoryPool : public ZeroTerminatedRamStringConst {
public:
StringInMemoryPool(const char* str = 0) : ZeroTerminatedRamStringConst(str) {}
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -18,7 +18,9 @@ struct IsString<T&> : IsString<T> {};
} // namespace ARDUINOJSON_NAMESPACE
#include "FixedSizeRamString.hpp"
#include "StringInMemoryPool.hpp"
#include "ZeroTerminatedRamString.hpp"
#include "ZeroTerminatedRamStringConst.hpp"
#if ARDUINOJSON_ENABLE_STD_STRING
#include "StlString.hpp"

View File

@@ -16,12 +16,12 @@ class ZeroTerminatedFlashString {
return strcmp_P(expected, actual) == 0;
}
bool is_null() const {
bool isNull() const {
return !_str;
}
template <typename Buffer>
const char* save(Buffer* memoryPool) const {
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
size_t n = size() + 1; // copy the terminator
void* dup = memoryPool->alloc(n);

View File

@@ -4,24 +4,17 @@
#pragma once
#include "ZeroTerminatedRamStringConst.hpp"
namespace ARDUINOJSON_NAMESPACE {
class ZeroTerminatedRamString {
class ZeroTerminatedRamString : public ZeroTerminatedRamStringConst {
public:
ZeroTerminatedRamString(const char* str) : _str(str) {}
ZeroTerminatedRamString(const char* str)
: ZeroTerminatedRamStringConst(str) {}
bool equals(const char* expected) const {
const char* actual = reinterpret_cast<const char*>(_str);
if (!actual || !expected) return actual == expected;
return strcmp(actual, expected) == 0;
}
bool is_null() const {
return !_str;
}
template <typename Buffer>
const char* save(Buffer* memoryPool) const {
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
size_t n = size() + 1;
void* dup = memoryPool->alloc(n);
@@ -29,13 +22,6 @@ class ZeroTerminatedRamString {
memcpy(dup, _str, n);
return static_cast<const char*>(dup);
}
size_t size() const {
return strlen(reinterpret_cast<const char*>(_str));
}
private:
const char* _str;
};
template <typename TChar>
@@ -43,6 +29,10 @@ inline ZeroTerminatedRamString makeString(const TChar* str) {
return ZeroTerminatedRamString(reinterpret_cast<const char*>(str));
}
inline ZeroTerminatedRamString makeString(char* str) {
return ZeroTerminatedRamString(str);
}
template <typename TChar>
struct IsString<TChar*> {
static const bool value = sizeof(TChar) == 1;

View File

@@ -0,0 +1,43 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include <stdlib.h> // size_t
#include <string.h> // strcmp
namespace ARDUINOJSON_NAMESPACE {
class ZeroTerminatedRamStringConst {
public:
ZeroTerminatedRamStringConst(const char* str) : _str(str) {}
bool equals(const char* expected) const {
const char* actual = _str;
if (!actual || !expected) return actual == expected;
return strcmp(actual, expected) == 0;
}
bool isNull() const {
return !_str;
}
template <typename TMemoryPool>
const char* save(TMemoryPool*) const {
return _str;
}
size_t size() const {
return strlen(_str);
}
protected:
const char* _str;
};
inline ZeroTerminatedRamStringConst makeString(const char* str) {
return ZeroTerminatedRamStringConst(str);
}
} // namespace ARDUINOJSON_NAMESPACE