forked from bblanchon/ArduinoJson
Extracted storage_policy to improve coverage in string adapters
This commit is contained in:
@ -7,6 +7,7 @@
|
|||||||
#include <WString.h>
|
#include <WString.h>
|
||||||
|
|
||||||
#include <ArduinoJson/Polyfills/safe_strcmp.hpp>
|
#include <ArduinoJson/Polyfills/safe_strcmp.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
namespace ARDUINOJSON_NAMESPACE {
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
@ -39,17 +40,11 @@ class ArduinoStringAdapter {
|
|||||||
return compare(expected) == 0;
|
return compare(expected) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* data() const {
|
|
||||||
return _str->c_str();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _str->length();
|
return _str->length();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStatic() const {
|
typedef storage_policy::store_by_copy storage_policy;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const ::String* _str;
|
const ::String* _str;
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <string.h> // strcmp
|
#include <string.h> // strcmp
|
||||||
|
|
||||||
#include <ArduinoJson/Polyfills/safe_strcmp.hpp>
|
#include <ArduinoJson/Polyfills/safe_strcmp.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
namespace ARDUINOJSON_NAMESPACE {
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
@ -27,11 +28,6 @@ class ConstRamStringAdapter {
|
|||||||
return !_str;
|
return !_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TMemoryPool>
|
|
||||||
char* save(TMemoryPool*) const {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
if (!_str)
|
if (!_str)
|
||||||
return 0;
|
return 0;
|
||||||
@ -42,9 +38,7 @@ class ConstRamStringAdapter {
|
|||||||
return _str;
|
return _str;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStatic() const {
|
typedef storage_policy::store_by_address storage_policy;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const char* _str;
|
const char* _str;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Polyfills/pgmspace.hpp>
|
#include <ArduinoJson/Polyfills/pgmspace.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
namespace ARDUINOJSON_NAMESPACE {
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
@ -40,19 +41,13 @@ class FlashStringAdapter {
|
|||||||
return dup;
|
return dup;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* data() const {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
if (!_str)
|
if (!_str)
|
||||||
return 0;
|
return 0;
|
||||||
return strlen_P(reinterpret_cast<const char*>(_str));
|
return strlen_P(reinterpret_cast<const char*>(_str));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStatic() const {
|
typedef storage_policy::store_by_copy storage_policy;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const __FlashStringHelper* _str;
|
const __FlashStringHelper* _str;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
|
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
namespace ARDUINOJSON_NAMESPACE {
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
@ -22,9 +23,7 @@ class RamStringAdapter : public ConstRamStringAdapter {
|
|||||||
return dup;
|
return dup;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStatic() const {
|
typedef ARDUINOJSON_NAMESPACE::storage_policy::store_by_copy storage_policy;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Namespace.hpp>
|
#include <ArduinoJson/Namespace.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
namespace ARDUINOJSON_NAMESPACE {
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
@ -45,9 +46,7 @@ class SizedFlashStringAdapter {
|
|||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStatic() const {
|
typedef storage_policy::store_by_copy storage_policy;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const __FlashStringHelper* _str;
|
const __FlashStringHelper* _str;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Namespace.hpp>
|
#include <ArduinoJson/Namespace.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
#include <string.h> // strcmp
|
#include <string.h> // strcmp
|
||||||
|
|
||||||
@ -39,9 +40,7 @@ class SizedRamStringAdapter {
|
|||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStatic() const {
|
typedef storage_policy::store_by_copy storage_policy;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* _str;
|
const char* _str;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Namespace.hpp>
|
#include <ArduinoJson/Namespace.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -39,17 +40,11 @@ class StlStringAdapter {
|
|||||||
return *_str == expected;
|
return *_str == expected;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* data() const {
|
|
||||||
return _str->data();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _str->size();
|
return _str->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStatic() const {
|
typedef storage_policy::store_by_copy storage_policy;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const TString* _str;
|
const TString* _str;
|
||||||
|
15
src/ArduinoJson/Strings/StoragePolicy.hpp
Normal file
15
src/ArduinoJson/Strings/StoragePolicy.hpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// ArduinoJson - arduinojson.org
|
||||||
|
// Copyright Benoit Blanchon 2014-2020
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
|
namespace storage_policy {
|
||||||
|
struct store_by_address {};
|
||||||
|
struct store_by_copy {};
|
||||||
|
struct decide_at_runtime {};
|
||||||
|
} // namespace storage_policy
|
||||||
|
|
||||||
|
} // namespace ARDUINOJSON_NAMESPACE
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
|
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
namespace ARDUINOJSON_NAMESPACE {
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
@ -36,6 +37,8 @@ class String {
|
|||||||
return strcmp(lhs._data, rhs._data) == 0;
|
return strcmp(lhs._data, rhs._data) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef storage_policy::decide_at_runtime storage_policy;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* _data;
|
const char* _data;
|
||||||
bool _isStatic;
|
bool _isStatic;
|
||||||
|
@ -13,17 +13,39 @@ template <typename TAdaptedString>
|
|||||||
inline bool slotSetKey(VariantSlot* var, TAdaptedString key, MemoryPool* pool) {
|
inline bool slotSetKey(VariantSlot* var, TAdaptedString key, MemoryPool* pool) {
|
||||||
if (!var)
|
if (!var)
|
||||||
return false;
|
return false;
|
||||||
|
return slotSetKey(var, key, pool, typename TAdaptedString::storage_policy());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TAdaptedString>
|
||||||
|
inline bool slotSetKey(VariantSlot* var, TAdaptedString key, MemoryPool* pool,
|
||||||
|
storage_policy::decide_at_runtime) {
|
||||||
if (key.isStatic()) {
|
if (key.isStatic()) {
|
||||||
var->setLinkedKey(make_not_null(key.data()));
|
return slotSetKey(var, key, pool, storage_policy::store_by_address());
|
||||||
} else {
|
} else {
|
||||||
const char* dup = key.save(pool);
|
return slotSetKey(var, key, pool, storage_policy::store_by_copy());
|
||||||
if (!dup)
|
|
||||||
return false;
|
|
||||||
var->setOwnedKey(make_not_null(dup));
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename TAdaptedString>
|
||||||
|
inline bool slotSetKey(VariantSlot* var, TAdaptedString key, MemoryPool*,
|
||||||
|
storage_policy::store_by_address) {
|
||||||
|
ARDUINOJSON_ASSERT(var);
|
||||||
|
var->setLinkedKey(make_not_null(key.data()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TAdaptedString>
|
||||||
|
inline bool slotSetKey(VariantSlot* var, TAdaptedString key, MemoryPool* pool,
|
||||||
|
storage_policy::store_by_copy) {
|
||||||
|
const char* dup = key.save(pool);
|
||||||
|
if (!dup)
|
||||||
|
return false;
|
||||||
|
ARDUINOJSON_ASSERT(var);
|
||||||
|
var->setOwnedKey(make_not_null(dup));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
inline size_t slotSize(const VariantSlot* var) {
|
inline size_t slotSize(const VariantSlot* var) {
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
while (var) {
|
while (var) {
|
||||||
|
Reference in New Issue
Block a user