meta: further reduce the number of instantiations and allocations
This commit is contained in:
@@ -28,28 +28,36 @@ namespace entt {
|
||||
|
||||
namespace internal {
|
||||
|
||||
inline internal::meta_base_node &meta_extend(internal::meta_type_node &owner, const id_type id, internal::meta_base_node node) {
|
||||
return owner.details->base.insert_or_assign(id, std::move(node)).first->second;
|
||||
inline decltype(auto) owner(const type_info &info) {
|
||||
auto &&context = meta_context::from(locator<meta_ctx>::value_or()).value;
|
||||
ENTT_ASSERT(context.contains(info.hash()), "Type not available");
|
||||
return context[info.hash()];
|
||||
}
|
||||
|
||||
inline internal::meta_conv_node &meta_extend(internal::meta_type_node &owner, const id_type id, internal::meta_conv_node node) {
|
||||
return owner.details->conv.insert_or_assign(id, std::move(node)).first->second;
|
||||
inline meta_base_node &meta_extend(const type_info &info, const id_type id, meta_base_node node) {
|
||||
return owner(info).details->base.insert_or_assign(id, std::move(node)).first->second;
|
||||
}
|
||||
|
||||
inline internal::meta_ctor_node &meta_extend(internal::meta_type_node &owner, const id_type id, internal::meta_ctor_node node) {
|
||||
return owner.details->ctor.insert_or_assign(id, std::move(node)).first->second;
|
||||
inline meta_conv_node &meta_extend(const type_info &info, const id_type id, meta_conv_node node) {
|
||||
return owner(info).details->conv.insert_or_assign(id, std::move(node)).first->second;
|
||||
}
|
||||
|
||||
inline internal::meta_dtor_node &meta_extend(internal::meta_type_node &owner, internal::meta_dtor_node node) {
|
||||
return (owner.dtor = std::move(node));
|
||||
inline meta_ctor_node &meta_extend(const type_info &info, const id_type id, meta_ctor_node node) {
|
||||
return owner(info).details->ctor.insert_or_assign(id, std::move(node)).first->second;
|
||||
}
|
||||
|
||||
inline internal::meta_data_node &meta_extend(internal::meta_type_node &owner, const id_type id, internal::meta_data_node node) {
|
||||
return owner.details->data.insert_or_assign(id, std::move(node)).first->second;
|
||||
inline meta_dtor_node &meta_extend(const type_info &info, meta_dtor_node node) {
|
||||
return (owner(info).dtor = std::move(node));
|
||||
}
|
||||
|
||||
inline internal::meta_func_node &meta_extend(internal::meta_type_node &owner, const id_type id, internal::meta_func_node node) {
|
||||
if(auto it = owner.details->func.find(id); it != owner.details->func.end()) {
|
||||
inline meta_data_node &meta_extend(const type_info &info, const id_type id, meta_data_node node) {
|
||||
return owner(info).details->data.insert_or_assign(id, std::move(node)).first->second;
|
||||
}
|
||||
|
||||
inline meta_func_node &meta_extend(const type_info &info, const id_type id, meta_func_node node) {
|
||||
auto &&elem = owner(info);
|
||||
|
||||
if(auto it = elem.details->func.find(id); it != elem.details->func.end()) {
|
||||
for(auto *curr = &it->second; curr; curr = curr->next.get()) {
|
||||
if(curr->invoke == node.invoke) {
|
||||
node.next = std::move(curr->next);
|
||||
@@ -59,19 +67,14 @@ inline internal::meta_func_node &meta_extend(internal::meta_type_node &owner, co
|
||||
}
|
||||
|
||||
// locally overloaded function
|
||||
node.next = std::make_shared<internal::meta_func_node>(std::move(owner.details->func[id]));
|
||||
node.next = std::make_shared<meta_func_node>(std::move(elem.details->func[id]));
|
||||
}
|
||||
|
||||
return owner.details->func.insert_or_assign(id, std::move(node)).first->second;
|
||||
return elem.details->func.insert_or_assign(id, std::move(node)).first->second;
|
||||
}
|
||||
|
||||
template<typename Owner>
|
||||
inline internal::meta_prop_node &meta_extend(Owner &owner, const id_type id, internal::meta_prop_node node) {
|
||||
if(!owner.details) {
|
||||
owner.details = std::make_shared<typename decltype(owner.details)::element_type>();
|
||||
}
|
||||
|
||||
return (owner.details->prop[id] = std::move(node));
|
||||
inline meta_prop_node &meta_extend(dense_map<id_type, meta_prop_node, identity> &prop, const id_type id, meta_prop_node node) {
|
||||
return (prop[id] = std::move(node));
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
@@ -81,84 +84,20 @@ inline internal::meta_prop_node &meta_extend(Owner &owner, const id_type id, int
|
||||
* @endcond
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Meta factory to be used for reflection purposes.
|
||||
*
|
||||
* The meta factory is an utility class used to reflect types, data members and
|
||||
* functions of all sorts. This class ensures that the underlying web of types
|
||||
* is built correctly and performs some checks in debug mode to ensure that
|
||||
* there are no subtle errors at runtime.
|
||||
*/
|
||||
template<typename...>
|
||||
class meta_factory;
|
||||
|
||||
/**
|
||||
* @brief Extended meta factory to be used for reflection purposes.
|
||||
* @tparam Type Reflected type for which the factory was created.
|
||||
* @tparam Node Node for which to create properties, if any.
|
||||
*/
|
||||
template<typename Type, typename Node>
|
||||
class meta_factory<Type, Node>: public meta_factory<Type> {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructs an extended factory from a given node.
|
||||
* @param target The node for which to store properties, if any.
|
||||
*/
|
||||
meta_factory(Node &target) noexcept
|
||||
: parent{&target} {}
|
||||
|
||||
/**
|
||||
* @brief Assigns a property to the last meta object created.
|
||||
*
|
||||
* Both the key and the value (if any) must be at least copy constructible.
|
||||
*
|
||||
* @tparam Value Optional type of the property value.
|
||||
* @param id Property key.
|
||||
* @param value Optional property value.
|
||||
* @return An extended meta factory for the given type.
|
||||
*/
|
||||
template<typename... Value>
|
||||
meta_factory prop(id_type id, [[maybe_unused]] Value &&...value) {
|
||||
if constexpr(sizeof...(Value) == 0u) {
|
||||
internal::meta_extend(
|
||||
*parent,
|
||||
id,
|
||||
internal::meta_prop_node{
|
||||
&internal::resolve<void>});
|
||||
} else {
|
||||
internal::meta_extend(
|
||||
*parent,
|
||||
id,
|
||||
internal::meta_prop_node{
|
||||
&internal::resolve<std::decay_t<Value>>...,
|
||||
std::make_shared<std::decay_t<Value>>(std::forward<Value>(value))...});
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
Node *parent;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Basic meta factory to be used for reflection purposes.
|
||||
* @tparam Type Reflected type for which the factory was created.
|
||||
*/
|
||||
template<typename Type>
|
||||
class meta_factory<Type> {
|
||||
decltype(auto) owner() {
|
||||
return internal::meta_context::from(locator<meta_ctx>::value_or()).value[type_id<Type>().hash()];
|
||||
}
|
||||
|
||||
class meta_factory {
|
||||
template<typename Setter, auto Getter, typename Policy, std::size_t... Index>
|
||||
auto data(const id_type id, std::index_sequence<Index...>) noexcept {
|
||||
void data(const id_type id, std::index_sequence<Index...>) noexcept {
|
||||
using data_type = std::invoke_result_t<decltype(Getter), Type &>;
|
||||
using args_type = type_list<typename meta_function_helper_t<Type, decltype(value_list_element_v<Index, Setter>)>::args_type...>;
|
||||
static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
|
||||
|
||||
auto &&elem = internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
id,
|
||||
internal::meta_data_node{
|
||||
/* this is never static */
|
||||
@@ -169,15 +108,21 @@ class meta_factory<Type> {
|
||||
+[](meta_handle instance, meta_any value) { return (meta_setter<Type, value_list_element_v<Index, Setter>>(*instance.operator->(), value.as_ref()) || ...); },
|
||||
&meta_getter<Type, Getter, Policy>});
|
||||
|
||||
return meta_factory<Type, internal::meta_data_node>{elem};
|
||||
bucket = &elem.prop;
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Default constructor. */
|
||||
meta_factory() noexcept {
|
||||
if(const auto it = internal::meta_context::from(locator<meta_ctx>::value_or()).value.try_emplace(type_id<Type>().hash(), internal::resolve<Type>()).first; !it->second.details) {
|
||||
it->second.details = std::make_shared<internal::meta_type_descriptor>();
|
||||
meta_factory() noexcept
|
||||
: bucket{},
|
||||
info{&type_id<Type>()} {
|
||||
auto &&elem = internal::owner(*info);
|
||||
|
||||
if(!elem.details) {
|
||||
elem.details = std::make_shared<internal::meta_type_descriptor>();
|
||||
}
|
||||
|
||||
bucket = &elem.details->prop;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,10 +131,11 @@ public:
|
||||
* @return An extended meta factory for the given type.
|
||||
*/
|
||||
auto type(const id_type id) noexcept {
|
||||
auto &&elem = owner();
|
||||
auto &&elem = internal::owner(*info);
|
||||
ENTT_ASSERT(elem.id == id || !resolve(id), "Duplicate identifier");
|
||||
bucket = &elem.details->prop;
|
||||
elem.id = id;
|
||||
return meta_factory<Type, internal::meta_type_node>{elem};
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,7 +151,7 @@ public:
|
||||
static_assert(!std::is_same_v<Type, Base> && std::is_base_of_v<Base, Type>, "Invalid base type");
|
||||
|
||||
internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
type_id<Base>().hash(),
|
||||
internal::meta_base_node{
|
||||
&internal::resolve<Base>,
|
||||
@@ -213,6 +159,7 @@ public:
|
||||
return static_cast<const void *>(static_cast<const Base *>(static_cast<const Type *>(instance)));
|
||||
}});
|
||||
|
||||
bucket = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -233,13 +180,14 @@ public:
|
||||
using conv_type = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<decltype(Candidate), Type &>>>;
|
||||
|
||||
internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
type_id<conv_type>().hash(),
|
||||
internal::meta_conv_node{
|
||||
+[](const void *instance) {
|
||||
return forward_as_meta(std::invoke(Candidate, *static_cast<const Type *>(instance)));
|
||||
}});
|
||||
|
||||
bucket = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -257,13 +205,14 @@ public:
|
||||
using conv_type = std::remove_cv_t<std::remove_reference_t<To>>;
|
||||
|
||||
internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
type_id<conv_type>().hash(),
|
||||
internal::meta_conv_node{
|
||||
+[](const void *instance) {
|
||||
return forward_as_meta(static_cast<To>(*static_cast<const Type *>(instance)));
|
||||
}});
|
||||
|
||||
bucket = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -287,13 +236,14 @@ public:
|
||||
static_assert(std::is_same_v<std::remove_cv_t<std::remove_reference_t<typename descriptor::return_type>>, Type>, "The function doesn't return an object of the required type");
|
||||
|
||||
internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
type_id<typename descriptor::args_type>().hash(),
|
||||
internal::meta_ctor_node{
|
||||
descriptor::args_type::size,
|
||||
&meta_arg<typename descriptor::args_type>,
|
||||
&meta_construct<Type, Candidate, Policy>});
|
||||
|
||||
bucket = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -312,13 +262,14 @@ public:
|
||||
using descriptor = meta_function_helper_t<Type, Type (*)(Args...)>;
|
||||
|
||||
internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
type_id<typename descriptor::args_type>().hash(),
|
||||
internal::meta_ctor_node{
|
||||
descriptor::args_type::size,
|
||||
&meta_arg<typename descriptor::args_type>,
|
||||
&meta_construct<Type, Args...>});
|
||||
|
||||
bucket = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -345,10 +296,11 @@ public:
|
||||
static_assert(std::is_invocable_v<decltype(Func), Type &>, "The function doesn't accept an object of the type provided");
|
||||
|
||||
internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
internal::meta_dtor_node{
|
||||
+[](void *instance) { std::invoke(Func, *static_cast<Type *>(instance)); }});
|
||||
|
||||
bucket = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -371,7 +323,7 @@ public:
|
||||
using data_type = std::remove_reference_t<std::invoke_result_t<decltype(Data), Type &>>;
|
||||
|
||||
auto &&elem = internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
id,
|
||||
internal::meta_data_node{
|
||||
/* this is never static */
|
||||
@@ -382,12 +334,12 @@ public:
|
||||
&meta_setter<Type, Data>,
|
||||
&meta_getter<Type, Data, Policy>});
|
||||
|
||||
return meta_factory<Type, internal::meta_data_node>{elem};
|
||||
bucket = &elem.prop;
|
||||
} else {
|
||||
using data_type = std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>;
|
||||
|
||||
auto &&elem = internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
id,
|
||||
internal::meta_data_node{
|
||||
((std::is_same_v<Type, std::remove_const_t<data_type>> || std::is_const_v<data_type>) ? internal::meta_traits::is_const : internal::meta_traits::is_none) | internal::meta_traits::is_static,
|
||||
@@ -397,8 +349,10 @@ public:
|
||||
&meta_setter<Type, Data>,
|
||||
&meta_getter<Type, Data, Policy>});
|
||||
|
||||
return meta_factory<Type, internal::meta_data_node>{elem};
|
||||
bucket = &elem.prop;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -428,7 +382,7 @@ public:
|
||||
|
||||
if constexpr(std::is_same_v<decltype(Setter), std::nullptr_t>) {
|
||||
auto &&elem = internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
id,
|
||||
internal::meta_data_node{
|
||||
/* this is never static */
|
||||
@@ -439,12 +393,12 @@ public:
|
||||
&meta_setter<Type, Setter>,
|
||||
&meta_getter<Type, Getter, Policy>});
|
||||
|
||||
return meta_factory<Type, internal::meta_data_node>{elem};
|
||||
bucket = &elem.prop;
|
||||
} else {
|
||||
using args_type = typename meta_function_helper_t<Type, decltype(Setter)>::args_type;
|
||||
|
||||
auto &&elem = internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
id,
|
||||
internal::meta_data_node{
|
||||
/* this is never static nor const */
|
||||
@@ -455,8 +409,10 @@ public:
|
||||
&meta_setter<Type, Setter>,
|
||||
&meta_getter<Type, Getter, Policy>});
|
||||
|
||||
return meta_factory<Type, internal::meta_data_node>{elem};
|
||||
bucket = &elem.prop;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -478,7 +434,8 @@ public:
|
||||
*/
|
||||
template<typename Setter, auto Getter, typename Policy = as_is_t>
|
||||
auto data(const id_type id) noexcept {
|
||||
return data<Setter, Getter, Policy>(id, std::make_index_sequence<Setter::size>{});
|
||||
data<Setter, Getter, Policy>(id, std::make_index_sequence<Setter::size>{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -500,7 +457,7 @@ public:
|
||||
static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
|
||||
|
||||
auto &&elem = internal::meta_extend(
|
||||
owner(),
|
||||
*info,
|
||||
id,
|
||||
internal::meta_func_node{
|
||||
(descriptor::is_const ? internal::meta_traits::is_const : internal::meta_traits::is_none) | (descriptor::is_static ? internal::meta_traits::is_static : internal::meta_traits::is_none),
|
||||
@@ -509,8 +466,45 @@ public:
|
||||
&meta_arg<typename descriptor::args_type>,
|
||||
&meta_invoke<Type, Candidate, Policy>});
|
||||
|
||||
return meta_factory<Type, internal::meta_func_node>{elem};
|
||||
bucket = &elem.prop;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a property to the last meta object created.
|
||||
*
|
||||
* Both the key and the value (if any) must be at least copy constructible.
|
||||
*
|
||||
* @tparam Value Optional type of the property value.
|
||||
* @param id Property key.
|
||||
* @param value Optional property value.
|
||||
* @return An extended meta factory for the given type.
|
||||
*/
|
||||
template<typename... Value>
|
||||
meta_factory prop(id_type id, [[maybe_unused]] Value &&...value) {
|
||||
ENTT_ASSERT(bucket != nullptr, "Meta object does not support properties");
|
||||
|
||||
if constexpr(sizeof...(Value) == 0u) {
|
||||
internal::meta_extend(
|
||||
*bucket,
|
||||
id,
|
||||
internal::meta_prop_node{
|
||||
&internal::resolve<void>});
|
||||
} else {
|
||||
internal::meta_extend(
|
||||
*bucket,
|
||||
id,
|
||||
internal::meta_prop_node{
|
||||
&internal::resolve<std::decay_t<Value>>...,
|
||||
std::make_shared<std::decay_t<Value>>(std::forward<Value>(value))...});
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
dense_map<id_type, internal::meta_prop_node, identity> *bucket;
|
||||
const type_info *info;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -526,9 +520,9 @@ public:
|
||||
*/
|
||||
template<typename Type>
|
||||
[[nodiscard]] auto meta() noexcept {
|
||||
auto res = internal::meta_context::from(locator<meta_ctx>::value_or()).value.try_emplace(type_id<Type>().hash(), internal::resolve<Type>());
|
||||
// extended meta factory to allow assigning properties to opaque meta types
|
||||
return meta_factory<Type, internal::meta_type_node>{res.first->second};
|
||||
// make sure the type exists in the context before returning a factory
|
||||
internal::meta_context::from(locator<meta_ctx>::value_or()).value.try_emplace(type_id<Type>().hash(), internal::resolve<Type>());
|
||||
return meta_factory<Type>{};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -644,19 +644,23 @@ struct meta_prop {
|
||||
/*! @brief Node type. */
|
||||
using node_type = internal::meta_prop_node;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
meta_prop() noexcept
|
||||
: node{} {}
|
||||
|
||||
/**
|
||||
* @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 = {}) noexcept
|
||||
: node{curr} {}
|
||||
meta_prop(const node_type &curr) noexcept
|
||||
: node{&curr} {}
|
||||
|
||||
/**
|
||||
* @brief Returns the stored value by copy.
|
||||
* @return A wrapper containing the value stored with the property.
|
||||
*/
|
||||
[[nodiscard]] meta_any value() const {
|
||||
return node.value ? node.type().from_void(nullptr, node.value.get()) : meta_any{};
|
||||
return node->value ? node->type().from_void(nullptr, node->value.get()) : meta_any{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -664,63 +668,37 @@ struct meta_prop {
|
||||
* @return True if the object is valid, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] explicit operator bool() const noexcept {
|
||||
return !(node.type == nullptr);
|
||||
return (node != nullptr);
|
||||
}
|
||||
|
||||
private:
|
||||
node_type node;
|
||||
const node_type *node;
|
||||
};
|
||||
|
||||
/**
|
||||
* @cond TURN_OFF_DOXYGEN
|
||||
* Internal details not to be documented.
|
||||
*/
|
||||
|
||||
namespace internal {
|
||||
|
||||
struct meta_common {
|
||||
template<typename Type, auto Member, typename Node>
|
||||
[[nodiscard]] auto range(const std::shared_ptr<Node> &node) const noexcept {
|
||||
using return_type = meta_range<Type, typename std::decay_t<decltype(((*node).*Member))>::const_iterator>;
|
||||
return node ? return_type{((*node).*Member).cbegin(), ((*node).*Member).cend()} : return_type{};
|
||||
}
|
||||
|
||||
template<typename Type, auto Member, typename Node>
|
||||
[[nodiscard]] auto find(const std::shared_ptr<Node> &node, const id_type key) const {
|
||||
if(node) {
|
||||
if(const auto it = ((*node).*Member).find(key); it != ((*node).*Member).cend()) {
|
||||
return Type{it->second};
|
||||
}
|
||||
}
|
||||
|
||||
return Type{};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
/**
|
||||
* Internal details not to be documented.
|
||||
* @endcond
|
||||
*/
|
||||
|
||||
/*! @brief Opaque wrapper for data members. */
|
||||
struct meta_data: private internal::meta_common {
|
||||
struct meta_data {
|
||||
/*! @brief Node type. */
|
||||
using node_type = internal::meta_data_node;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename node_type::size_type;
|
||||
|
||||
/*! @copydoc meta_prop::meta_prop */
|
||||
meta_data(const node_type &curr = {}) noexcept
|
||||
: node{curr} {}
|
||||
/*! @brief Default constructor. */
|
||||
meta_data() noexcept
|
||||
: node{} {}
|
||||
|
||||
/**
|
||||
* @brief Constructs an instance from a given node.
|
||||
* @param curr The underlying node with which to construct the instance.
|
||||
*/
|
||||
meta_data(const node_type &curr) noexcept
|
||||
: node{&curr} {}
|
||||
|
||||
/**
|
||||
* @brief Returns the number of setters available.
|
||||
* @return The number of setters available.
|
||||
*/
|
||||
[[nodiscard]] size_type arity() const noexcept {
|
||||
return node.arity;
|
||||
return node->arity;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -728,7 +706,7 @@ struct meta_data: private internal::meta_common {
|
||||
* @return True if the data member is constant, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool is_const() const noexcept {
|
||||
return static_cast<bool>(node.traits & internal::meta_traits::is_const);
|
||||
return static_cast<bool>(node->traits & internal::meta_traits::is_const);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -736,7 +714,7 @@ struct meta_data: private internal::meta_common {
|
||||
* @return True if the data member is static, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool is_static() const noexcept {
|
||||
return static_cast<bool>(node.traits & internal::meta_traits::is_static);
|
||||
return static_cast<bool>(node->traits & internal::meta_traits::is_static);
|
||||
}
|
||||
|
||||
/*! @copydoc meta_any::type */
|
||||
@@ -757,7 +735,7 @@ struct meta_data: private internal::meta_common {
|
||||
*/
|
||||
template<typename Type>
|
||||
bool set(meta_handle instance, Type &&value) const {
|
||||
return node.set && node.set(std::move(instance), std::forward<Type>(value));
|
||||
return node->set && node->set(std::move(instance), std::forward<Type>(value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -770,7 +748,7 @@ struct meta_data: private internal::meta_common {
|
||||
* @return A wrapper containing the value of the underlying variable.
|
||||
*/
|
||||
[[nodiscard]] meta_any get(meta_handle instance) const {
|
||||
return node.get(std::move(instance));
|
||||
return node->get(std::move(instance));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -784,8 +762,8 @@ struct meta_data: private internal::meta_common {
|
||||
* @brief Returns a range to visit registered meta properties.
|
||||
* @return An iterable range to visit registered meta properties.
|
||||
*/
|
||||
[[nodiscard]] meta_range<meta_prop, typename decltype(internal::meta_prop_map::prop)::const_iterator> prop() const noexcept {
|
||||
return meta_common::range<meta_prop, &internal::meta_prop_map::prop>(node.details);
|
||||
[[nodiscard]] meta_range<meta_prop, typename decltype(internal::meta_data_node::prop)::const_iterator> prop() const noexcept {
|
||||
return {node->prop.cbegin(), node->prop.cend()};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -794,7 +772,8 @@ struct meta_data: private internal::meta_common {
|
||||
* @return The registered meta property for the given key, if any.
|
||||
*/
|
||||
[[nodiscard]] meta_prop prop(const id_type key) const {
|
||||
return meta_common::find<meta_prop, &internal::meta_prop_map::prop>(node.details, key);
|
||||
const auto it = node->prop.find(key);
|
||||
return it != node->prop.cend() ? meta_prop{it->second} : meta_prop{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -802,30 +781,37 @@ struct meta_data: private internal::meta_common {
|
||||
* @return True if the object is valid, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] explicit operator bool() const noexcept {
|
||||
return !(node.type == nullptr);
|
||||
return (node != nullptr);
|
||||
}
|
||||
|
||||
private:
|
||||
node_type node;
|
||||
const node_type *node;
|
||||
};
|
||||
|
||||
/*! @brief Opaque wrapper for member functions. */
|
||||
struct meta_func: private internal::meta_common {
|
||||
struct meta_func {
|
||||
/*! @brief Node type. */
|
||||
using node_type = internal::meta_func_node;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename node_type::size_type;
|
||||
|
||||
/*! @copydoc meta_prop::meta_prop */
|
||||
meta_func(const node_type &curr = {}) noexcept
|
||||
: node{curr} {}
|
||||
/*! @brief Default constructor. */
|
||||
meta_func() noexcept
|
||||
: node{} {}
|
||||
|
||||
/**
|
||||
* @brief Constructs an instance from a given node.
|
||||
* @param curr The underlying node with which to construct the instance.
|
||||
*/
|
||||
meta_func(const node_type &curr) noexcept
|
||||
: node{&curr} {}
|
||||
|
||||
/**
|
||||
* @brief Returns the number of arguments accepted by a member function.
|
||||
* @return The number of arguments accepted by the member function.
|
||||
*/
|
||||
[[nodiscard]] size_type arity() const noexcept {
|
||||
return node.arity;
|
||||
return node->arity;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -833,7 +819,7 @@ struct meta_func: private internal::meta_common {
|
||||
* @return True if the member function is constant, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool is_const() const noexcept {
|
||||
return static_cast<bool>(node.traits & internal::meta_traits::is_const);
|
||||
return static_cast<bool>(node->traits & internal::meta_traits::is_const);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -841,7 +827,7 @@ struct meta_func: private internal::meta_common {
|
||||
* @return True if the member function is static, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool is_static() const noexcept {
|
||||
return static_cast<bool>(node.traits & internal::meta_traits::is_static);
|
||||
return static_cast<bool>(node->traits & internal::meta_traits::is_static);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -872,7 +858,7 @@ struct meta_func: private internal::meta_common {
|
||||
* @return A wrapper containing the returned value, if any.
|
||||
*/
|
||||
meta_any invoke(meta_handle instance, meta_any *const args, const size_type sz) const {
|
||||
return sz == arity() ? node.invoke(std::move(instance), args) : meta_any{};
|
||||
return sz == arity() ? node->invoke(std::move(instance), args) : meta_any{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -892,8 +878,8 @@ struct meta_func: private internal::meta_common {
|
||||
}
|
||||
|
||||
/*! @copydoc meta_data::prop */
|
||||
[[nodiscard]] meta_range<meta_prop, typename decltype(internal::meta_prop_map::prop)::const_iterator> prop() const noexcept {
|
||||
return meta_common::range<meta_prop, &internal::meta_prop_map::prop>(node.details);
|
||||
[[nodiscard]] meta_range<meta_prop, typename decltype(internal::meta_func_node::prop)::const_iterator> prop() const noexcept {
|
||||
return {node->prop.cbegin(), node->prop.cend()};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -902,7 +888,8 @@ struct meta_func: private internal::meta_common {
|
||||
* @return The registered meta property for the given key, if any.
|
||||
*/
|
||||
[[nodiscard]] meta_prop prop(const id_type key) const {
|
||||
return meta_common::find<meta_prop, &internal::meta_prop_map::prop>(node.details, key);
|
||||
const auto it = node->prop.find(key);
|
||||
return it != node->prop.cend() ? meta_prop{it->second} : meta_prop{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -910,7 +897,7 @@ struct meta_func: private internal::meta_common {
|
||||
* @return The next overload of the given function, if any.
|
||||
*/
|
||||
[[nodiscard]] meta_func next() const {
|
||||
return node.next ? meta_func{*node.next} : meta_func{};
|
||||
return node->next ? meta_func{*node->next} : meta_func{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -918,15 +905,15 @@ struct meta_func: private internal::meta_common {
|
||||
* @return True if the object is valid, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] explicit operator bool() const noexcept {
|
||||
return !(node.invoke == nullptr);
|
||||
return (node != nullptr);
|
||||
}
|
||||
|
||||
private:
|
||||
node_type node;
|
||||
const node_type *node;
|
||||
};
|
||||
|
||||
/*! @brief Opaque wrapper for types. */
|
||||
class meta_type: private internal::meta_common {
|
||||
class meta_type {
|
||||
template<typename Func>
|
||||
[[nodiscard]] auto lookup(meta_any *const args, const typename internal::meta_type_node::size_type sz, Func next) const {
|
||||
decltype(next()) candidate = nullptr;
|
||||
@@ -984,7 +971,10 @@ public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename node_type::size_type;
|
||||
|
||||
/*! @copydoc meta_prop::meta_prop */
|
||||
/**
|
||||
* @brief Constructs an instance from a given node.
|
||||
* @param curr The underlying node with which to construct the instance.
|
||||
*/
|
||||
meta_type(const node_type &curr = {}) noexcept
|
||||
: node{curr} {}
|
||||
|
||||
@@ -1153,7 +1143,8 @@ public:
|
||||
* @return An iterable range to visit registered top-level base meta types.
|
||||
*/
|
||||
[[nodiscard]] meta_range<meta_type, typename decltype(internal::meta_type_descriptor::base)::const_iterator> base() const noexcept {
|
||||
return meta_common::range<meta_type, &internal::meta_type_descriptor::base>(node.details);
|
||||
using range_type = meta_range<meta_type, typename decltype(internal::meta_type_descriptor::base)::const_iterator>;
|
||||
return node.details ? range_type{node.details->base.cbegin(), node.details->base.cend()} : range_type{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1161,7 +1152,8 @@ public:
|
||||
* @return An iterable range to visit registered top-level meta data.
|
||||
*/
|
||||
[[nodiscard]] meta_range<meta_data, typename decltype(internal::meta_type_descriptor::data)::const_iterator> data() const noexcept {
|
||||
return meta_common::range<meta_data, &internal::meta_type_descriptor::data>(node.details);
|
||||
using range_type = meta_range<meta_data, typename decltype(internal::meta_type_descriptor::data)::const_iterator>;
|
||||
return node.details ? range_type{node.details->data.cbegin(), node.details->data.cend()} : range_type{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1173,8 +1165,10 @@ public:
|
||||
* @return The registered meta data for the given identifier, if any.
|
||||
*/
|
||||
[[nodiscard]] meta_data data(const id_type id) const {
|
||||
if(auto elem = meta_common::find<meta_data, &internal::meta_type_descriptor::data>(node.details, id)) {
|
||||
return elem;
|
||||
if(node.details) {
|
||||
if(const auto it = node.details->data.find(id); it != node.details->data.cend()) {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
return forward_to_bases([id](auto &&type) { return type.data(id); });
|
||||
@@ -1200,8 +1194,10 @@ public:
|
||||
* @return The registered meta function for the given identifier, if any.
|
||||
*/
|
||||
[[nodiscard]] meta_func func(const id_type id) const {
|
||||
if(auto elem = meta_common::find<meta_func, &internal::meta_type_descriptor::func>(node.details, id)) {
|
||||
return elem;
|
||||
if(node.details) {
|
||||
if(const auto it = node.details->func.find(id); it != node.details->func.cend()) {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
return forward_to_bases([id](auto &&type) { return type.func(id); });
|
||||
@@ -1352,7 +1348,8 @@ public:
|
||||
* @return An iterable range to visit registered top-level meta properties.
|
||||
*/
|
||||
[[nodiscard]] meta_range<meta_prop, typename decltype(internal::meta_type_descriptor::prop)::const_iterator> prop() const noexcept {
|
||||
return meta_common::range<meta_prop, &internal::meta_type_descriptor::prop>(node.details);
|
||||
using range_type = meta_range<meta_prop, typename decltype(internal::meta_type_descriptor::prop)::const_iterator>;
|
||||
return node.details ? range_type{node.details->prop.cbegin(), node.details->prop.cend()} : range_type{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1364,8 +1361,10 @@ public:
|
||||
* @return The registered meta property for the given key, if any.
|
||||
*/
|
||||
[[nodiscard]] meta_prop prop(const id_type key) const {
|
||||
if(auto elem = meta_common::find<meta_prop, &internal::meta_type_descriptor::prop>(node.details, key)) {
|
||||
return elem;
|
||||
if(node.details) {
|
||||
if(const auto it = node.details->prop.find(key); it != node.details->prop.cend()) {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
return forward_to_bases([key](auto &&type) { return type.prop(key); });
|
||||
@@ -1474,19 +1473,19 @@ inline bool meta_any::assign(meta_any &&other) {
|
||||
}
|
||||
|
||||
[[nodiscard]] inline meta_type meta_data::type() const noexcept {
|
||||
return node.type();
|
||||
return node->type();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline meta_type meta_data::arg(const size_type index) const noexcept {
|
||||
return index < arity() ? node.arg(index) : meta_type{};
|
||||
return index < arity() ? node->arg(index) : meta_type{};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline meta_type meta_func::ret() const noexcept {
|
||||
return node.ret();
|
||||
return node->ret();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline meta_type meta_func::arg(const size_type index) const noexcept {
|
||||
return index < arity() ? node.arg(index) : meta_type{};
|
||||
return index < arity() ? node->arg(index) : meta_type{};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -52,10 +52,6 @@ struct meta_prop_node {
|
||||
std::shared_ptr<void> value{};
|
||||
};
|
||||
|
||||
struct meta_prop_map {
|
||||
dense_map<id_type, meta_prop_node, identity> prop{};
|
||||
};
|
||||
|
||||
struct meta_base_node {
|
||||
meta_type_node (*type)() noexcept {};
|
||||
const void *(*cast)(const void *) noexcept {};
|
||||
@@ -86,7 +82,7 @@ struct meta_data_node {
|
||||
meta_type (*arg)(const size_type) noexcept {};
|
||||
bool (*set)(meta_handle, meta_any){};
|
||||
meta_any (*get)(meta_handle){};
|
||||
std::shared_ptr<meta_prop_map> details{};
|
||||
dense_map<id_type, meta_prop_node, identity> prop{};
|
||||
};
|
||||
|
||||
struct meta_func_node {
|
||||
@@ -98,7 +94,7 @@ struct meta_func_node {
|
||||
meta_type (*arg)(const size_type) noexcept {};
|
||||
meta_any (*invoke)(meta_handle, meta_any *const){};
|
||||
std::shared_ptr<meta_func_node> next{};
|
||||
std::shared_ptr<meta_prop_map> details{};
|
||||
dense_map<id_type, meta_prop_node, identity> prop{};
|
||||
};
|
||||
|
||||
struct meta_template_node {
|
||||
@@ -109,12 +105,13 @@ struct meta_template_node {
|
||||
meta_type_node (*arg)(const size_type) noexcept {};
|
||||
};
|
||||
|
||||
struct meta_type_descriptor: meta_prop_map {
|
||||
struct meta_type_descriptor {
|
||||
dense_map<id_type, meta_ctor_node, identity> ctor{};
|
||||
dense_map<id_type, meta_base_node, identity> base{};
|
||||
dense_map<id_type, meta_conv_node, identity> conv{};
|
||||
dense_map<id_type, meta_data_node, identity> data{};
|
||||
dense_map<id_type, meta_func_node, identity> func{};
|
||||
dense_map<id_type, meta_prop_node, identity> prop{};
|
||||
};
|
||||
|
||||
struct meta_type_node {
|
||||
|
||||
Reference in New Issue
Block a user