From 6cc0917afb4f583a96e43a4f63565ea4669462b4 Mon Sep 17 00:00:00 2001 From: kr0ner Date: Tue, 26 Nov 2024 14:35:28 +0100 Subject: [PATCH] enforce unique constraint on property id --- src/property.cpp | 15 +++++++++++++++ src/property.h | 41 +++++++++++++++++------------------------ yaml/common.yaml | 1 + 3 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 src/property.cpp diff --git a/src/property.cpp b/src/property.cpp new file mode 100644 index 0000000..c8a4fce --- /dev/null +++ b/src/property.cpp @@ -0,0 +1,15 @@ +#include "property.h" +#include + +namespace detail { +Property Mapper::getProperty(const std::uint16_t id) { + if (auto it = map.find(id); it != map.end()) { + return it->second; + } + return {"UNKNOWN", id}; +} + +Property Property::getProperty(const std::uint16_t id) { + return Mapper().getProperty(id); +} +} // namespace detail diff --git a/src/property.h b/src/property.h index 3af1ac7..1e13efe 100644 --- a/src/property.h +++ b/src/property.h @@ -8,18 +8,7 @@ #include "type.h" namespace detail { -struct Property; -struct Mapper { - Mapper(const Property* other); - - protected: - Mapper() = default; - - protected: - static inline std::unordered_map map; -}; - -struct Property : public Mapper { +struct Property { std::string_view name; std::uint16_t id{0U}; Type type{Type::et_default}; @@ -38,28 +27,32 @@ struct Property : public Mapper { } protected: - Property getProperty(const std::uint16_t id) { - if (auto it = map.find(id); it != map.end()) { - return *(it->second); - } - return {"UNKNOWN", id}; - } + Property getProperty(const std::uint16_t id); +}; + +class Mapper { + public: + Mapper() = default; + inline Mapper(const Property other) { map.insert({other.id, other}); } + + Property getProperty(const std::uint16_t id); + + private: + static inline std::unordered_map map; }; -inline Mapper::Mapper(const Property* other) { - map.insert({other->id, other}); -} } // namespace detail #define PROPERTY(NAME, VALUE, ...) \ static constexpr detail::Property k##NAME{#NAME, VALUE, ##__VA_ARGS__}; \ + static constexpr bool unique##VALUE{true}; \ static inline detail::Mapper k##NAME##_MAPPING { \ - &k##NAME \ + k##NAME \ } struct Property : public detail::Property { - Property(const Property& p) : detail::Property{p.name, p.id, p.type} {} - Property(const detail::Property& p) : detail::Property{p.name, p.id, p.type} {} + constexpr Property(const Property& p) : detail::Property{p.name, p.id, p.type} {} + constexpr Property(const detail::Property& p) : detail::Property{p.name, p.id, p.type} {} Property(const std::uint16_t _id) : detail::Property{getProperty(_id)} {} PROPERTY(INDEX_NOT_FOUND, 0x0000); diff --git a/yaml/common.yaml b/yaml/common.yaml index 9b74388..3ebfd6b 100644 --- a/yaml/common.yaml +++ b/yaml/common.yaml @@ -6,6 +6,7 @@ esphome: - OneESP32ToRuleThemAll/src/mapper.h - OneESP32ToRuleThemAll/src/mapper.cpp - OneESP32ToRuleThemAll/src/property.h + - OneESP32ToRuleThemAll/src/property.cpp - OneESP32ToRuleThemAll/src/simple_variant.h - OneESP32ToRuleThemAll/src/type.h - OneESP32ToRuleThemAll/src/type.cpp