meta: use the hot/cold model with meta_prop_node

This commit is contained in:
Michele Caini
2022-09-06 11:49:24 +02:00
parent a30cecdf36
commit 10755275ea
3 changed files with 23 additions and 15 deletions

View File

@@ -78,12 +78,16 @@ public:
* @return An extended meta factory for the given type.
*/
template<typename... Value>
meta_factory prop(id_type key, Value &&...value) {
meta_factory prop(id_type key, [[maybe_unused]] Value &&...value) {
internal::meta_details_setup(*node);
node->details->prop[key] = internal::meta_prop_node{
&internal::resolve<std::decay_t<Value>>...,
std::make_shared<std::decay_t<Value>>(std::forward<Value>(value))...};
if constexpr(sizeof...(Value) == 0u) {
node->details->prop[key] = internal::meta_prop_node{&internal::resolve<void>};
} else {
node->details->prop[key] = internal::meta_prop_node{
&internal::resolve<std::decay_t<Value>>...,
std::make_shared<std::decay_t<Value>>(std::forward<Value>(value))...};
}
return *this;
}

View File

@@ -661,7 +661,7 @@ struct meta_prop {
* @brief Constructs an instance from a given node.
* @param curr The underlying node with which to construct the instance.
*/
meta_prop(const node_type *curr = nullptr) noexcept
meta_prop(const node_type &curr = {}) noexcept
: node{curr} {}
/**
@@ -669,7 +669,7 @@ struct meta_prop {
* @return A wrapper containing the value stored with the property.
*/
[[nodiscard]] meta_any value() const {
return (node && node->type) ? node->type()->from_void(nullptr, node->value.get()) : meta_any{};
return node.value ? node.type()->from_void(nullptr, node.value.get()) : meta_any{};
}
/**
@@ -677,11 +677,11 @@ struct meta_prop {
* @return True if the object is valid, false otherwise.
*/
[[nodiscard]] explicit operator bool() const noexcept {
return !(node == nullptr);
return !(node.type == nullptr);
}
private:
const node_type *node;
node_type node;
};
/*! @brief Opaque wrapper for data members. */
@@ -780,11 +780,11 @@ struct meta_data {
[[nodiscard]] meta_prop prop(const id_type key) const {
if(node->details) {
if(auto it = node->details->prop.find(key); it != node->details->prop.cend()) {
return &it->second;
return it->second;
}
}
return nullptr;
return {};
}
/**
@@ -898,11 +898,11 @@ struct meta_func {
[[nodiscard]] meta_prop prop(const id_type key) const {
if(node->details) {
if(auto it = node->details->prop.find(key); it != node->details->prop.cend()) {
return &it->second;
return it->second;
}
}
return nullptr;
return {};
}
/**
@@ -1394,7 +1394,7 @@ public:
[[nodiscard]] meta_prop prop(const id_type key) const {
if(node->details) {
if(auto it = node->details->prop.find(key); it != node->details->prop.cend()) {
return &it->second;
return it->second;
}
}
@@ -1404,7 +1404,7 @@ public:
}
}
return nullptr;
return {};
}
/**

View File

@@ -25,7 +25,11 @@ class meta_range_iterator final {
template<typename Value>
Type to_value(char, const Value &value) const {
return &value;
if constexpr(std::is_same_v<std::decay_t<Value>, internal::meta_prop_node>) {
return value;
} else {
return &value;
}
}
public: