User can now use a JsonString as a key or a value

This commit is contained in:
Benoit Blanchon
2019-01-29 17:00:11 +01:00
parent 6f55d1e58f
commit b184af6d00
29 changed files with 500 additions and 376 deletions

View File

@@ -8,9 +8,9 @@
namespace ARDUINOJSON_NAMESPACE {
class ArduinoStringWrapper {
class ArduinoStringAdapter {
public:
ArduinoStringWrapper(const ::String& str) : _str(&str) {}
ArduinoStringAdapter(const ::String& str) : _str(&str) {}
char* save(MemoryPool* pool) const {
if (isNull()) return NULL;
@@ -40,6 +40,10 @@ class ArduinoStringWrapper {
return _str->length();
}
bool isStatic() const {
return false;
}
private:
const ::String* _str;
};
@@ -50,8 +54,8 @@ struct IsString< ::String> : true_type {};
template <>
struct IsString< ::StringSumHelper> : true_type {};
inline ArduinoStringWrapper wrapString(const ::String& str) {
return ArduinoStringWrapper(str);
inline ArduinoStringAdapter adaptString(const ::String& str) {
return ArduinoStringAdapter(str);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -9,9 +9,9 @@
namespace ARDUINOJSON_NAMESPACE {
class ConstRamStringWrapper {
class ConstRamStringAdapter {
public:
ConstRamStringWrapper(const char* str = 0) : _str(str) {}
ConstRamStringAdapter(const char* str = 0) : _str(str) {}
bool equals(const char* expected) const {
const char* actual = _str;
@@ -23,25 +23,29 @@ class ConstRamStringWrapper {
return !_str;
}
// template <typename TMemoryPool>
// const char* save(TMemoryPool*) const {
// return _str;
// }
template <typename TMemoryPool>
char* save(TMemoryPool*) const {
return 0;
}
size_t size() const {
return strlen(_str);
}
const char* c_str() const {
const char* data() const {
return _str;
}
bool isStatic() const {
return true;
}
protected:
const char* _str;
};
inline ConstRamStringWrapper wrapString(const char* str) {
return ConstRamStringWrapper(str);
inline ConstRamStringAdapter adaptString(const char* str) {
return ConstRamStringAdapter(str);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -6,9 +6,9 @@
namespace ARDUINOJSON_NAMESPACE {
class FlashStringWrapper {
class FlashStringAdapter {
public:
FlashStringWrapper(const __FlashStringHelper* str) : _str(str) {}
FlashStringAdapter(const __FlashStringHelper* str) : _str(str) {}
bool equals(const char* expected) const {
const char* actual = reinterpret_cast<const char*>(_str);
@@ -28,16 +28,24 @@ class FlashStringWrapper {
return dup;
}
const char* data() const {
return 0;
}
size_t size() const {
return strlen_P(reinterpret_cast<const char*>(_str));
}
bool isStatic() const {
return false;
}
private:
const __FlashStringHelper* _str;
};
inline FlashStringWrapper wrapString(const __FlashStringHelper* str) {
return FlashStringWrapper(str);
inline FlashStringAdapter adaptString(const __FlashStringHelper* str) {
return FlashStringAdapter(str);
}
template <>

View File

@@ -4,13 +4,13 @@
#pragma once
#include "ConstRamStringWrapper.hpp"
#include "ConstRamStringAdapter.hpp"
namespace ARDUINOJSON_NAMESPACE {
class RamStringWrapper : public ConstRamStringWrapper {
class RamStringAdapter : public ConstRamStringAdapter {
public:
RamStringWrapper(const char* str) : ConstRamStringWrapper(str) {}
RamStringAdapter(const char* str) : ConstRamStringAdapter(str) {}
char* save(MemoryPool* pool) const {
if (!_str) return NULL;
@@ -19,15 +19,19 @@ class RamStringWrapper : public ConstRamStringWrapper {
if (dup) memcpy(dup, _str, n);
return dup;
}
bool isStatic() const {
return false;
}
};
template <typename TChar>
inline RamStringWrapper wrapString(const TChar* str) {
return RamStringWrapper(reinterpret_cast<const char*>(str));
inline RamStringAdapter adaptString(const TChar* str) {
return RamStringAdapter(reinterpret_cast<const char*>(str));
}
inline RamStringWrapper wrapString(char* str) {
return RamStringWrapper(str);
inline RamStringAdapter adaptString(char* str) {
return RamStringAdapter(str);
}
template <typename TChar>

View File

@@ -6,9 +6,9 @@
namespace ARDUINOJSON_NAMESPACE {
class SizedFlashStringWrapper {
class SizedFlashStringAdapter {
public:
SizedFlashStringWrapper(const __FlashStringHelper* str, size_t sz)
SizedFlashStringAdapter(const __FlashStringHelper* str, size_t sz)
: _str(str), _size(sz) {}
bool equals(const char* expected) const {
@@ -32,13 +32,17 @@ class SizedFlashStringWrapper {
return strlen_P(reinterpret_cast<const char*>(_str));
}
bool isStatic() const {
return false;
}
private:
const __FlashStringHelper* _str;
size_t _size;
};
inline SizedFlashStringWrapper wrapString(const __FlashStringHelper* str,
size_t sz) {
return SizedFlashStringWrapper(str, sz);
inline SizedFlashStringAdapter adaptString(const __FlashStringHelper* str,
size_t sz) {
return SizedFlashStringAdapter(str, sz);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -8,9 +8,9 @@
namespace ARDUINOJSON_NAMESPACE {
class SizedRamStringWrapper {
class SizedRamStringAdapter {
public:
SizedRamStringWrapper(const char* str, size_t n) : _str(str), _size(n) {}
SizedRamStringAdapter(const char* str, size_t n) : _str(str), _size(n) {}
bool equals(const char* expected) const {
const char* actual = reinterpret_cast<const char*>(_str);
@@ -33,14 +33,18 @@ class SizedRamStringWrapper {
return strlen(reinterpret_cast<const char*>(_str));
}
bool isStatic() const {
return false;
}
private:
const char* _str;
size_t _size;
};
template <typename TChar>
inline SizedRamStringWrapper wrapString(const TChar* str, size_t size) {
return SizedRamStringWrapper(reinterpret_cast<const char*>(str), size);
inline SizedRamStringAdapter adaptString(const TChar* str, size_t size) {
return SizedRamStringAdapter(reinterpret_cast<const char*>(str), size);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -8,9 +8,9 @@
namespace ARDUINOJSON_NAMESPACE {
class StlStringWrapper {
class StlStringAdapter {
public:
StlStringWrapper(const std::string& str) : _str(&str) {}
StlStringAdapter(const std::string& str) : _str(&str) {}
char* save(MemoryPool* pool) const {
size_t n = _str->length() + 1;
@@ -36,6 +36,10 @@ class StlStringWrapper {
return _str->size();
}
bool isStatic() const {
return false;
}
private:
const std::string* _str;
};
@@ -43,8 +47,8 @@ class StlStringWrapper {
template <>
struct IsString<std::string> : true_type {};
inline StlStringWrapper wrapString(const std::string& str) {
return StlStringWrapper(str);
inline StlStringAdapter adaptString(const std::string& str) {
return StlStringAdapter(str);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -4,12 +4,15 @@
#pragma once
#include "ConstRamStringAdapter.hpp"
namespace ARDUINOJSON_NAMESPACE {
class String {
public:
String() : _data(0) {}
String(const char* slot) : _data(slot) {}
String() : _data(0), _isStatic(true) {}
String(const char* data, bool isStaticData = true)
: _data(data), _isStatic(isStaticData) {}
const char* c_str() const {
return _data;
@@ -19,6 +22,10 @@ class String {
return !_data;
}
bool isStatic() const {
return _isStatic;
}
friend bool operator==(String lhs, String rhs) {
if (lhs._data == rhs._data) return true;
if (!lhs._data) return false;
@@ -28,5 +35,31 @@ class String {
private:
const char* _data;
bool _isStatic;
};
class StringAdapter : public RamStringAdapter {
public:
StringAdapter(const String& str)
: RamStringAdapter(str.c_str()), _isStatic(str.isStatic()) {}
bool isStatic() const {
return _isStatic;
}
/* const char* save(MemoryPool* pool) const {
if (_isStatic) return c_str();
return RamStringAdapter::save(pool);
}*/
private:
bool _isStatic;
};
template <>
struct IsString<String> : true_type {};
inline StringAdapter adaptString(const String& str) {
return StringAdapter(str);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -18,19 +18,19 @@ template <typename T>
struct IsString<T&> : IsString<T> {};
} // namespace ARDUINOJSON_NAMESPACE
#include "ConstRamStringWrapper.hpp"
#include "RamStringWrapper.hpp"
#include "SizedRamStringWrapper.hpp"
#include "ConstRamStringAdapter.hpp"
#include "RamStringAdapter.hpp"
#include "SizedRamStringAdapter.hpp"
#if ARDUINOJSON_ENABLE_STD_STRING
#include "StlStringWrapper.hpp"
#include "StlStringAdapter.hpp"
#endif
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
#include "ArduinoStringWrapper.hpp"
#include "ArduinoStringAdapter.hpp"
#endif
#if ARDUINOJSON_ENABLE_PROGMEM
#include "FlashStringWrapper.hpp"
#include "SizedFlashStringWrapper.hpp"
#include "FlashStringAdapter.hpp"
#include "SizedFlashStringAdapter.hpp"
#endif