mirror of
https://github.com/esphome/esphome.git
synced 2025-10-23 04:03:52 +01:00
[mapping] Use custom allocator (#9972)
This commit is contained in:
@@ -10,7 +10,8 @@ from esphome.loader import get_component
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
MULTI_CONF = True
|
||||
|
||||
map_ = cg.std_ns.class_("map")
|
||||
mapping_ns = cg.esphome_ns.namespace("mapping")
|
||||
mapping_class = mapping_ns.class_("Mapping")
|
||||
|
||||
CONF_ENTRIES = "entries"
|
||||
CONF_CLASS = "class"
|
||||
@@ -29,7 +30,11 @@ class IndexType:
|
||||
|
||||
INDEX_TYPES = {
|
||||
"int": IndexType(cv.int_, cg.int_, int),
|
||||
"string": IndexType(cv.string, cg.std_string, str),
|
||||
"string": IndexType(
|
||||
cv.string,
|
||||
cg.std_string,
|
||||
str,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +52,7 @@ def to_schema(value):
|
||||
|
||||
BASE_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(map_),
|
||||
cv.Required(CONF_ID): cv.declare_id(mapping_class),
|
||||
cv.Required(CONF_FROM): cv.one_of(*INDEX_TYPES, lower=True),
|
||||
cv.Required(CONF_TO): cv.string,
|
||||
},
|
||||
@@ -123,12 +128,15 @@ async def to_code(config):
|
||||
if list(entries.values())[0].op != ".":
|
||||
value_type = value_type.operator("ptr")
|
||||
varid = config[CONF_ID]
|
||||
varid.type = map_.template(index_type, value_type)
|
||||
varid.type = mapping_class.template(
|
||||
index_type,
|
||||
value_type,
|
||||
)
|
||||
var = MockObj(varid, ".")
|
||||
decl = VariableDeclarationExpression(varid.type, "", varid)
|
||||
add_global(decl)
|
||||
CORE.register_variable(varid, var)
|
||||
|
||||
for key, value in entries.items():
|
||||
cg.add(var.insert((key, value)))
|
||||
cg.add(var.set(key, value))
|
||||
return var
|
||||
|
69
esphome/components/mapping/mapping.h
Normal file
69
esphome/components/mapping/mapping.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace esphome::mapping {
|
||||
|
||||
using alloc_string_t = std::basic_string<char, std::char_traits<char>, RAMAllocator<char>>;
|
||||
|
||||
/**
|
||||
*
|
||||
* Mapping class with custom allocator.
|
||||
* Additionally, when std::string is used as key or value, it will be replaced with a custom string type
|
||||
* that uses RAMAllocator.
|
||||
* @tparam K The type of the key in the mapping.
|
||||
* @tparam V The type of the value in the mapping. Should be a basic type or pointer.
|
||||
*/
|
||||
|
||||
static const char *const TAG = "mapping";
|
||||
|
||||
template<typename K, typename V> class Mapping {
|
||||
public:
|
||||
// Constructor
|
||||
Mapping() = default;
|
||||
|
||||
using key_t = const std::conditional_t<std::is_same_v<K, std::string>,
|
||||
alloc_string_t, // if K is std::string, custom string type
|
||||
K>;
|
||||
using value_t = std::conditional_t<std::is_same_v<V, std::string>,
|
||||
alloc_string_t, // if V is std::string, custom string type
|
||||
V>;
|
||||
|
||||
void set(const K &key, const V &value) { this->map_[key_t{key}] = value; }
|
||||
|
||||
V get(const K &key) const {
|
||||
auto it = this->map_.find(key_t{key});
|
||||
if (it != this->map_.end()) {
|
||||
return V{it->second};
|
||||
}
|
||||
if constexpr (std::is_pointer_v<K>) {
|
||||
esph_log_e(TAG, "Key '%p' not found in mapping", key);
|
||||
} else if constexpr (std::is_same_v<K, std::string>) {
|
||||
esph_log_e(TAG, "Key '%s' not found in mapping", key.c_str());
|
||||
} else {
|
||||
esph_log_e(TAG, "Key '%s' not found in mapping", to_string(key).c_str());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// index map overload
|
||||
V operator[](K key) { return this->get(key); }
|
||||
|
||||
// convenience function for strings to get a C-style string
|
||||
template<typename T = V, std::enable_if_t<std::is_same_v<T, std::string>, int> = 0>
|
||||
const char *operator[](K key) const {
|
||||
auto it = this->map_.find(key_t{key});
|
||||
if (it != this->map_.end()) {
|
||||
return it->second.c_str(); // safe since value remains in map
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
protected:
|
||||
std::map<key_t, value_t, std::less<key_t>, RAMAllocator<std::pair<key_t, value_t>>> map_;
|
||||
};
|
||||
|
||||
} // namespace esphome::mapping
|
Reference in New Issue
Block a user