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 <ArduinoJson/Polyfills/safe_strcmp.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -39,17 +40,11 @@ class ArduinoStringAdapter {
|
||||
return compare(expected) == 0;
|
||||
}
|
||||
|
||||
const char* data() const {
|
||||
return _str->c_str();
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return _str->length();
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return false;
|
||||
}
|
||||
typedef storage_policy::store_by_copy storage_policy;
|
||||
|
||||
private:
|
||||
const ::String* _str;
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <string.h> // strcmp
|
||||
|
||||
#include <ArduinoJson/Polyfills/safe_strcmp.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -27,11 +28,6 @@ class ConstRamStringAdapter {
|
||||
return !_str;
|
||||
}
|
||||
|
||||
template <typename TMemoryPool>
|
||||
char* save(TMemoryPool*) const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
if (!_str)
|
||||
return 0;
|
||||
@ -42,9 +38,7 @@ class ConstRamStringAdapter {
|
||||
return _str;
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return true;
|
||||
}
|
||||
typedef storage_policy::store_by_address storage_policy;
|
||||
|
||||
protected:
|
||||
const char* _str;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Polyfills/pgmspace.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -40,19 +41,13 @@ class FlashStringAdapter {
|
||||
return dup;
|
||||
}
|
||||
|
||||
const char* data() const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
if (!_str)
|
||||
return 0;
|
||||
return strlen_P(reinterpret_cast<const char*>(_str));
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return false;
|
||||
}
|
||||
typedef storage_policy::store_by_copy storage_policy;
|
||||
|
||||
private:
|
||||
const __FlashStringHelper* _str;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -22,9 +23,7 @@ class RamStringAdapter : public ConstRamStringAdapter {
|
||||
return dup;
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return false;
|
||||
}
|
||||
typedef ARDUINOJSON_NAMESPACE::storage_policy::store_by_copy storage_policy;
|
||||
};
|
||||
|
||||
template <typename TChar>
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -45,9 +46,7 @@ class SizedFlashStringAdapter {
|
||||
return _size;
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return false;
|
||||
}
|
||||
typedef storage_policy::store_by_copy storage_policy;
|
||||
|
||||
private:
|
||||
const __FlashStringHelper* _str;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
#include <string.h> // strcmp
|
||||
|
||||
@ -39,9 +40,7 @@ class SizedRamStringAdapter {
|
||||
return _size;
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return false;
|
||||
}
|
||||
typedef storage_policy::store_by_copy storage_policy;
|
||||
|
||||
private:
|
||||
const char* _str;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
@ -39,17 +40,11 @@ class StlStringAdapter {
|
||||
return *_str == expected;
|
||||
}
|
||||
|
||||
const char* data() const {
|
||||
return _str->data();
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return _str->size();
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return false;
|
||||
}
|
||||
typedef storage_policy::store_by_copy storage_policy;
|
||||
|
||||
private:
|
||||
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
|
||||
|
||||
#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -36,6 +37,8 @@ class String {
|
||||
return strcmp(lhs._data, rhs._data) == 0;
|
||||
}
|
||||
|
||||
typedef storage_policy::decide_at_runtime storage_policy;
|
||||
|
||||
private:
|
||||
const char* _data;
|
||||
bool _isStatic;
|
||||
|
@ -13,17 +13,39 @@ template <typename TAdaptedString>
|
||||
inline bool slotSetKey(VariantSlot* var, TAdaptedString key, MemoryPool* pool) {
|
||||
if (!var)
|
||||
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()) {
|
||||
var->setLinkedKey(make_not_null(key.data()));
|
||||
return slotSetKey(var, key, pool, storage_policy::store_by_address());
|
||||
} else {
|
||||
const char* dup = key.save(pool);
|
||||
if (!dup)
|
||||
return false;
|
||||
var->setOwnedKey(make_not_null(dup));
|
||||
return slotSetKey(var, key, pool, storage_policy::store_by_copy());
|
||||
}
|
||||
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) {
|
||||
size_t n = 0;
|
||||
while (var) {
|
||||
|
Reference in New Issue
Block a user