Skip to content

Commit

Permalink
meta: static_cast only vtable for meta associative containers
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Aug 7, 2023
1 parent 8f211b7 commit cf7b18f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/entt/meta/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class basic_meta_associative_container_traits {
using size_type = typename meta_associative_container::size_type;
using iterator = typename meta_associative_container::iterator;

static size_type basic_vtable(const operation op, const void *cvalue, void *value, const meta_any *key, iterator *it) {
static size_type basic_vtable(const operation op, const void *cvalue, void *value, const void *key, iterator *it) {
switch(op) {
case operation::size:
return static_cast<const Type *>(cvalue)->size();
Expand Down Expand Up @@ -179,17 +179,17 @@ class basic_meta_associative_container_traits {
return true;
case operation::insert:
if constexpr(key_only) {
return static_cast<Type *>(value)->insert(key->cast<const typename Type::key_type &>()).second;
return static_cast<Type *>(value)->insert(*static_cast<const typename Type::key_type *>(key)).second;
} else {
return static_cast<Type *>(value)->emplace(key->cast<const typename Type::key_type &>(), static_cast<meta_any *>(const_cast<void *>(cvalue))->cast<const typename Type::mapped_type &>()).second;
return static_cast<Type *>(value)->emplace(*static_cast<const typename Type::key_type *>(key), *static_cast<const typename Type::mapped_type *>(cvalue)).second;
}
case operation::erase:
return static_cast<Type *>(value)->erase(key->cast<const typename Type::key_type &>());
return static_cast<Type *>(value)->erase(*static_cast<const typename Type::key_type *>(key));
case operation::find:
if(value) {
it->rebind<key_only>(static_cast<Type *>(value)->find(key->cast<const typename Type::key_type &>()));
it->rebind<key_only>(static_cast<Type *>(value)->find(*static_cast<const typename Type::key_type *>(key)));
} else {
it->rebind<key_only>(static_cast<const Type *>(cvalue)->find(key->cast<const typename Type::key_type &>()));
it->rebind<key_only>(static_cast<const Type *>(cvalue)->find(*static_cast<const typename Type::key_type *>(key)));
}

return true;
Expand Down
10 changes: 6 additions & 4 deletions src/entt/meta/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class meta_associative_container {
internal::meta_type_node (*key_type_node)(const internal::meta_context &){};
internal::meta_type_node (*mapped_type_node)(const internal::meta_context &){};
internal::meta_type_node (*value_type_node)(const internal::meta_context &){};
size_type (*vtable)(const operation, const void *, void *, const meta_any *, iterator *){};
size_type (*vtable)(const operation, const void *, void *, const void *, iterator *){};
any storage{};
};

Expand Down Expand Up @@ -1922,7 +1922,8 @@ inline bool meta_associative_container::reserve(const size_type sz) {
* @return A bool denoting whether the insertion took place.
*/
inline bool meta_associative_container::insert(meta_any key, meta_any value = {}) {
return (storage.policy() != any_policy::cref) && key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) && (!mapped_type_node || value.allow_cast(meta_type{*ctx, mapped_type_node(internal::meta_context::from(*ctx))})) && vtable(operation::insert, &value, storage.data(), &key, nullptr);
const bool valid_key_value = key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) && (!mapped_type_node || value.allow_cast(meta_type{*ctx, mapped_type_node(internal::meta_context::from(*ctx))}));
return valid_key_value && (storage.policy() != any_policy::cref) && vtable(operation::insert, std::as_const(value).data(), storage.data(), std::as_const(key).data(), nullptr);
}

/**
Expand All @@ -1931,7 +1932,8 @@ inline bool meta_associative_container::insert(meta_any key, meta_any value = {}
* @return A bool denoting whether the removal took place.
*/
inline meta_associative_container::size_type meta_associative_container::erase(meta_any key) {
return (storage.policy() != any_policy::cref) && key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) && vtable(operation::erase, nullptr, storage.data(), &key, nullptr);
const bool valid_key = key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))});
return valid_key && (storage.policy() != any_policy::cref) && vtable(operation::erase, nullptr, storage.data(), std::as_const(key).data(), nullptr);
}

/**
Expand All @@ -1942,7 +1944,7 @@ inline meta_associative_container::size_type meta_associative_container::erase(m
[[nodiscard]] inline meta_associative_container::iterator meta_associative_container::find(meta_any key) {
iterator it{*ctx};
const void *data = std::as_const(storage).data();
key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) && vtable(operation::find, data, storage.policy() == any_policy::cref ? nullptr : const_cast<void *>(data), &key, &it);
key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) && vtable(operation::find, data, storage.policy() == any_policy::cref ? nullptr : const_cast<void *>(data), std::as_const(key).data(), &it);
return it;
}

Expand Down

0 comments on commit cf7b18f

Please sign in to comment.