Skip to content

Commit

Permalink
enforce unique constraint on property id
Browse files Browse the repository at this point in the history
  • Loading branch information
kr0ner committed Nov 26, 2024
1 parent 17d05cb commit 2e6a9ab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
15 changes: 15 additions & 0 deletions src/property.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "property.h"
#include <unordered_map>

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
41 changes: 17 additions & 24 deletions src/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint16_t, const Property*> map;
};

struct Property : public Mapper {
struct Property {
std::string_view name;
std::uint16_t id{0U};
Type type{Type::et_default};
Expand All @@ -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<std::uint16_t, Property> 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);
Expand Down
1 change: 1 addition & 0 deletions yaml/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e6a9ab

Please sign in to comment.