Skip to content

Commit 316b5b9

Browse files
committed
meta: split begin/cbegin and end/cend for meta assoc containers, review vtable
1 parent 5aeae3a commit 316b5b9

File tree

3 files changed

+31
-37
lines changed

3 files changed

+31
-37
lines changed

src/entt/meta/container.hpp

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -146,51 +146,45 @@ class basic_meta_associative_container_traits {
146146
using size_type = typename meta_associative_container::size_type;
147147
using iterator = typename meta_associative_container::iterator;
148148

149-
static size_type basic_vtable(const operation op, const void *cvalue, void *value, const void *key, iterator *it) {
149+
static size_type basic_vtable(const operation op, const void *container, const void *key, const void *value, iterator *it) {
150150
switch(op) {
151151
case operation::size:
152-
return static_cast<const Type *>(cvalue)->size();
152+
return static_cast<const Type *>(const_cast<void *>(container))->size();
153153
case operation::clear:
154-
static_cast<Type *>(value)->clear();
154+
static_cast<Type *>(const_cast<void *>(container))->clear();
155155
return true;
156156
case operation::reserve:
157157
if constexpr(internal::reserve_aware_container<Type>::value) {
158-
static_cast<Type *>(value)->reserve(*static_cast<const size_type *>(cvalue));
158+
static_cast<Type *>(const_cast<void *>(container))->reserve(*static_cast<const size_type *>(value));
159159
return true;
160160
} else {
161161
break;
162162
}
163163
case operation::begin:
164-
if(value) {
165-
it->rebind<key_only>(static_cast<Type *>(value)->begin());
166-
} else {
167-
it->rebind<key_only>(static_cast<const Type *>(cvalue)->begin());
168-
}
169-
164+
it->rebind<key_only>(static_cast<Type *>(const_cast<void *>(container))->begin());
165+
return true;
166+
case operation::cbegin:
167+
it->rebind<key_only>(static_cast<const Type *>(container)->begin());
170168
return true;
171169
case operation::end:
172-
if(value) {
173-
it->rebind<key_only>(static_cast<Type *>(value)->end());
174-
} else {
175-
it->rebind<key_only>(static_cast<const Type *>(cvalue)->end());
176-
}
177-
170+
it->rebind<key_only>(static_cast<Type *>(const_cast<void *>(container))->end());
171+
return true;
172+
case operation::cend:
173+
it->rebind<key_only>(static_cast<const Type *>(container)->end());
178174
return true;
179175
case operation::insert:
180176
if constexpr(key_only) {
181-
return static_cast<Type *>(value)->insert(*static_cast<const typename Type::key_type *>(key)).second;
177+
return static_cast<Type *>(const_cast<void *>(container))->insert(*static_cast<const typename Type::key_type *>(key)).second;
182178
} else {
183-
return static_cast<Type *>(value)->emplace(*static_cast<const typename Type::key_type *>(key), *static_cast<const typename Type::mapped_type *>(cvalue)).second;
179+
return static_cast<Type *>(const_cast<void *>(container))->emplace(*static_cast<const typename Type::key_type *>(key), *static_cast<const typename Type::mapped_type *>(value)).second;
184180
}
185181
case operation::erase:
186-
return static_cast<Type *>(value)->erase(*static_cast<const typename Type::key_type *>(key));
182+
return static_cast<Type *>(const_cast<void *>(container))->erase(*static_cast<const typename Type::key_type *>(key));
187183
case operation::find:
188-
if(value) {
189-
it->rebind<key_only>(static_cast<Type *>(value)->find(*static_cast<const typename Type::key_type *>(key)));
190-
} else {
191-
it->rebind<key_only>(static_cast<const Type *>(cvalue)->find(*static_cast<const typename Type::key_type *>(key)));
192-
}
193-
184+
it->rebind<key_only>(static_cast<Type *>(const_cast<void *>(container))->find(*static_cast<const typename Type::key_type *>(key)));
185+
return true;
186+
case operation::cfind:
187+
it->rebind<key_only>(static_cast<const Type *>(container)->find(*static_cast<const typename Type::key_type *>(key)));
194188
return true;
195189
}
196190

src/entt/meta/meta.hpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class meta_associative_container {
137137
internal::meta_type_node (*key_type_node)(const internal::meta_context &){};
138138
internal::meta_type_node (*mapped_type_node)(const internal::meta_context &){};
139139
internal::meta_type_node (*value_type_node)(const internal::meta_context &){};
140-
size_type (*vtable)(const operation, const void *, void *, const void *, iterator *){};
140+
size_type (*vtable)(const operation, const void *, const void *, const void *, iterator *){};
141141
any storage{};
142142
};
143143

@@ -1889,27 +1889,25 @@ inline meta_sequence_container::iterator meta_sequence_container::erase(iterator
18891889

18901890
/*! @copydoc meta_sequence_container::clear */
18911891
inline bool meta_associative_container::clear() {
1892-
return (storage.policy() != any_policy::cref) && vtable(operation::clear, nullptr, storage.data(), nullptr, nullptr);
1892+
return (storage.policy() != any_policy::cref) && vtable(operation::clear, storage.data(), nullptr, nullptr, nullptr);
18931893
}
18941894

18951895
/*! @copydoc meta_sequence_container::reserve */
18961896
inline bool meta_associative_container::reserve(const size_type sz) {
1897-
return (storage.policy() != any_policy::cref) && vtable(operation::reserve, &sz, storage.data(), nullptr, nullptr);
1897+
return (storage.policy() != any_policy::cref) && vtable(operation::reserve, storage.data(), nullptr, &sz, nullptr);
18981898
}
18991899

19001900
/*! @copydoc meta_sequence_container::begin */
19011901
[[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
19021902
iterator it{*ctx};
1903-
const void *data = std::as_const(storage).data();
1904-
vtable(operation::begin, data, storage.policy() == any_policy::cref ? nullptr : const_cast<void *>(data), nullptr, &it);
1903+
vtable(storage.policy() == any_policy::cref ? operation::cbegin : operation::begin, std::as_const(storage).data(), nullptr, nullptr, &it);
19051904
return it;
19061905
}
19071906

19081907
/*! @copydoc meta_sequence_container::end */
19091908
[[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
19101909
iterator it{*ctx};
1911-
const void *data = std::as_const(storage).data();
1912-
vtable(operation::end, data, storage.policy() == any_policy::cref ? nullptr : const_cast<void *>(data), nullptr, &it);
1910+
vtable(storage.policy() == any_policy::cref ? operation::cend : operation::end, std::as_const(storage).data(), nullptr, nullptr, &it);
19131911
return it;
19141912
}
19151913

@@ -1921,7 +1919,7 @@ inline bool meta_associative_container::reserve(const size_type sz) {
19211919
*/
19221920
inline bool meta_associative_container::insert(meta_any key, meta_any value = {}) {
19231921
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))}));
1924-
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);
1922+
return valid_key_value && (storage.policy() != any_policy::cref) && vtable(operation::insert, storage.data(), std::as_const(key).data(), std::as_const(value).data(), nullptr);
19251923
}
19261924

19271925
/**
@@ -1931,7 +1929,7 @@ inline bool meta_associative_container::insert(meta_any key, meta_any value = {}
19311929
*/
19321930
inline meta_associative_container::size_type meta_associative_container::erase(meta_any key) {
19331931
const bool valid_key = key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))});
1934-
return valid_key && (storage.policy() != any_policy::cref) && vtable(operation::erase, nullptr, storage.data(), std::as_const(key).data(), nullptr);
1932+
return valid_key && (storage.policy() != any_policy::cref) && vtable(operation::erase, storage.data(), std::as_const(key).data(), nullptr, nullptr);
19351933
}
19361934

19371935
/**
@@ -1941,8 +1939,7 @@ inline meta_associative_container::size_type meta_associative_container::erase(m
19411939
*/
19421940
[[nodiscard]] inline meta_associative_container::iterator meta_associative_container::find(meta_any key) {
19431941
iterator it{*ctx};
1944-
const void *data = std::as_const(storage).data();
1945-
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);
1942+
key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) && vtable(storage.policy() == any_policy::cref ? operation::cfind : operation::find, std::as_const(storage).data(), std::as_const(key).data(), nullptr, &it);
19461943
return it;
19471944
}
19481945

src/entt/meta/type_traits.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ enum class meta_associative_container_operation {
3131
clear,
3232
reserve,
3333
begin,
34+
cbegin,
3435
end,
36+
cend,
3537
insert,
3638
erase,
37-
find
39+
find,
40+
cfind
3841
};
3942

4043
} // namespace internal

0 commit comments

Comments
 (0)