Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enforce unique constraint on property id #66

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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