mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 18:27:37 +02:00
Remove StoragePolicy
This commit is contained in:
@ -27,10 +27,14 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
|
||||
auto slot = pool->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
auto storedKey = storeString(pool, key);
|
||||
if (!storedKey)
|
||||
return nullptr;
|
||||
slot->setKey(storedKey);
|
||||
if (key.isLinked())
|
||||
slot->setKey(JsonString(key.data(), key.size(), JsonString::Linked));
|
||||
else {
|
||||
auto storedKey = pool->saveString(key);
|
||||
if (!storedKey)
|
||||
return nullptr;
|
||||
slot->setKey(JsonString(storedKey, key.size(), JsonString::Copied));
|
||||
}
|
||||
obj->add(slot);
|
||||
return slot->data();
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
|
||||
|
@ -10,7 +10,6 @@
|
||||
#include <ArduinoJson/Object/JsonObject.hpp>
|
||||
#include <ArduinoJson/Object/MemberProxy.hpp>
|
||||
#include <ArduinoJson/Polyfills/utility.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
#include <ArduinoJson/Variant/JsonVariantConst.hpp>
|
||||
#include <ArduinoJson/Variant/VariantTo.hpp>
|
||||
|
||||
|
@ -296,30 +296,4 @@ class MemoryPool {
|
||||
StringNode* strings_ = nullptr;
|
||||
};
|
||||
|
||||
template <typename TAdaptedString>
|
||||
JsonString storeString(MemoryPool* pool, TAdaptedString str,
|
||||
StringStoragePolicy::Copy) {
|
||||
return JsonString(pool->saveString(str), str.size(), JsonString::Copied);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
JsonString storeString(MemoryPool*, TAdaptedString str,
|
||||
StringStoragePolicy::Link) {
|
||||
return JsonString(str.data(), str.size(), JsonString::Linked);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
JsonString storeString(MemoryPool* pool, TAdaptedString str,
|
||||
StringStoragePolicy::LinkOrCopy policy) {
|
||||
if (policy.link)
|
||||
return storeString(pool, str, StringStoragePolicy::Link());
|
||||
else
|
||||
return storeString(pool, str, StringStoragePolicy::Copy());
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
JsonString storeString(MemoryPool* pool, TAdaptedString str) {
|
||||
return storeString(pool, str, str.storagePolicy());
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -26,6 +26,10 @@ class FlashString {
|
||||
return static_cast<char>(pgm_read_byte(str_ + i));
|
||||
}
|
||||
|
||||
const char* data() const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return size_;
|
||||
}
|
||||
@ -59,8 +63,8 @@ class FlashString {
|
||||
::memcpy_P(p, s.str_, n);
|
||||
}
|
||||
|
||||
StringStoragePolicy::Copy storagePolicy() const {
|
||||
return StringStoragePolicy::Copy();
|
||||
bool isLinked() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -15,9 +15,8 @@ class JsonStringAdapter : public SizedRamString {
|
||||
JsonStringAdapter(const JsonString& s)
|
||||
: SizedRamString(s.c_str(), s.size()), linked_(s.isLinked()) {}
|
||||
|
||||
StringStoragePolicy::LinkOrCopy storagePolicy() const {
|
||||
StringStoragePolicy::LinkOrCopy policy = {linked_};
|
||||
return policy;
|
||||
bool isLinked() const {
|
||||
return linked_;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -8,7 +8,6 @@
|
||||
#include <string.h> // strcmp
|
||||
|
||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapter.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
@ -53,8 +52,8 @@ class ZeroTerminatedRamString {
|
||||
return stringCompare(a, b) == 0;
|
||||
}
|
||||
|
||||
StringStoragePolicy::Copy storagePolicy() const {
|
||||
return StringStoragePolicy::Copy();
|
||||
bool isLinked() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
@ -83,8 +82,8 @@ class StaticStringAdapter : public ZeroTerminatedRamString {
|
||||
public:
|
||||
StaticStringAdapter(const char* str) : ZeroTerminatedRamString(str) {}
|
||||
|
||||
StringStoragePolicy::Link storagePolicy() const {
|
||||
return StringStoragePolicy::Link();
|
||||
bool isLinked() const {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@ -121,8 +120,8 @@ class SizedRamString {
|
||||
return str_;
|
||||
}
|
||||
|
||||
StringStoragePolicy::Copy storagePolicy() const {
|
||||
return StringStoragePolicy::Copy();
|
||||
bool isLinked() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -1,18 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
namespace StringStoragePolicy {
|
||||
|
||||
struct Link {};
|
||||
struct Copy {};
|
||||
struct LinkOrCopy {
|
||||
bool link;
|
||||
};
|
||||
} // namespace StringStoragePolicy
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||
#include <ArduinoJson/Polyfills/attributes.hpp>
|
||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/Visitor.hpp>
|
||||
|
||||
@ -59,18 +58,18 @@ inline bool variantCopyFrom(VariantData* dst, const VariantData* src,
|
||||
return collectionCopy(&dst->toObject(), src->asObject(), pool);
|
||||
case VALUE_IS_OWNED_STRING: {
|
||||
auto str = adaptString(src->asString());
|
||||
auto dup = storeString(pool, str, StringStoragePolicy::Copy());
|
||||
auto dup = pool->saveString(str);
|
||||
if (!dup)
|
||||
return false;
|
||||
dst->setString(dup);
|
||||
dst->setString(JsonString(dup, str.size(), JsonString::Copied));
|
||||
return true;
|
||||
}
|
||||
case VALUE_IS_RAW_STRING: {
|
||||
auto str = adaptString(src->asRawString());
|
||||
auto dup = storeString(pool, str, StringStoragePolicy::Copy());
|
||||
auto dup = pool->saveString(str);
|
||||
if (!dup)
|
||||
return false;
|
||||
dst->setRawString(dup.c_str(), str.size());
|
||||
dst->setRawString(dup, str.size());
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
@ -115,9 +114,20 @@ inline void variantSetString(VariantData* var, TAdaptedString value,
|
||||
if (!var)
|
||||
return;
|
||||
variantRelease(var, pool);
|
||||
JsonString str = storeString(pool, value);
|
||||
if (str)
|
||||
var->setString(str);
|
||||
|
||||
if (value.isNull()) {
|
||||
var->setNull();
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.isLinked()) {
|
||||
var->setString(JsonString(value.data(), value.size(), JsonString::Linked));
|
||||
return;
|
||||
}
|
||||
|
||||
auto dup = pool->saveString(value);
|
||||
if (dup)
|
||||
var->setString(JsonString(dup, value.size(), JsonString::Copied));
|
||||
else
|
||||
var->setNull();
|
||||
}
|
||||
|
Reference in New Issue
Block a user