Removed the indirection via StringSlot

This commit is contained in:
Benoit Blanchon
2018-11-27 17:28:19 +01:00
parent 45f4e5ac20
commit a60162ba76
23 changed files with 105 additions and 164 deletions

View File

@@ -12,13 +12,12 @@ class ArduinoString {
public:
ArduinoString(const ::String& str) : _str(&str) {}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool* memoryPool) const {
char* save(MemoryPool* memoryPool) const {
if (isNull()) return NULL;
size_t n = _str->length() + 1;
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy(slot->value, _str->c_str(), n);
return slot;
char* dup = memoryPool->allocFrozenString(n);
if (dup) memcpy(dup, _str->c_str(), n);
return dup;
}
bool isNull() const {

View File

@@ -21,12 +21,11 @@ class FixedSizeFlashString {
return !_str;
}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool* memoryPool) const {
char* save(MemoryPool* memoryPool) const {
if (!_str) return NULL;
StringSlot* slot = memoryPool->allocFrozenString(_size);
if (!slot) memcpy_P(slot->value, (const char*)_str, _size);
return slot;
char* dup = memoryPool->allocFrozenString(_size);
if (!dup) memcpy_P(dup, (const char*)_str, _size);
return dup;
}
size_t size() const {

View File

@@ -23,11 +23,11 @@ class FixedSizeRamString {
}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool* memoryPool) const {
char* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
StringSlot* slot = memoryPool->allocFrozenString(_size);
if (slot) memcpy(slot->value, _str, _size);
return slot;
char* dup = memoryPool->allocFrozenString(_size);
if (dup) memcpy(dup, _str, _size);
return dup;
}
size_t size() const {

View File

@@ -12,12 +12,11 @@ class StlString {
public:
StlString(const std::string& str) : _str(&str) {}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool* memoryPool) const {
char* save(MemoryPool* memoryPool) const {
size_t n = _str->length() + 1;
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy(slot->value, _str->c_str(), n);
return slot;
char* dup = memoryPool->allocFrozenString(n);
if (dup) memcpy(dup, _str->c_str(), n);
return dup;
}
bool isNull() const {

View File

@@ -5,44 +5,35 @@
#pragma once
#include <string.h>
#include "../Memory/StringSlot.hpp"
#include "../Memory/MemoryPool.hpp"
namespace ARDUINOJSON_NAMESPACE {
class StringInMemoryPool {
public:
StringInMemoryPool(StringSlot* s = 0) : _slot(s) {}
StringInMemoryPool(char* s = 0) : _value(s) {}
bool equals(const char* expected) const {
if (!_slot) return expected == 0;
const char* actual = _slot->value;
if (!_value) return expected == 0;
const char* actual = _value;
if (actual == expected) return true;
return strcmp(actual, expected) == 0;
}
char* save(void*) {
return _value;
}
bool isNull() const {
return !_slot;
}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool*) const {
return _slot;
}
size_t size() const {
return _slot->size;
}
StringSlot* slot() const {
return _slot;
return !_value;
}
const char* c_str() const {
return _slot->value;
return _value;
}
protected:
StringSlot* _slot;
char* _value;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -20,13 +20,12 @@ class ZeroTerminatedFlashString {
return !_str;
}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool* memoryPool) const {
char* save(MemoryPool* memoryPool) const {
if (!_str) return NULL;
size_t n = size() + 1; // copy the terminator
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy_P(slot->value, reinterpret_cast<const char*>(_str), n);
return slot;
char* dup = memoryPool->allocFrozenString(n);
if (dup) memcpy_P(dup, reinterpret_cast<const char*>(_str), n);
return dup;
}
size_t size() const {

View File

@@ -14,12 +14,12 @@ class ZeroTerminatedRamString : public ZeroTerminatedRamStringConst {
: ZeroTerminatedRamStringConst(str) {}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool* memoryPool) const {
char* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
size_t n = size() + 1;
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy(slot->value, _str, n);
return slot;
char* dup = memoryPool->allocFrozenString(n);
if (dup) memcpy(dup, _str, n);
return dup;
}
};