Skip to content

Commit 5aeae3a

Browse files
committed
meta: split begin/cbegin and end/cend for meta seq containers, review vtable
1 parent 3f4f976 commit 5aeae3a

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

src/entt/meta/container.hpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,56 +64,52 @@ class basic_meta_sequence_container_traits {
6464
using size_type = typename meta_sequence_container::size_type;
6565
using iterator = typename meta_sequence_container::iterator;
6666

67-
static size_type basic_vtable(const operation op, const void *cvalue, void *value, iterator *it) {
67+
static size_type basic_vtable(const operation op, const void *container, const void *value, iterator *it) {
6868
switch(op) {
6969
case operation::size:
70-
return static_cast<const Type *>(cvalue)->size();
70+
return static_cast<const Type *>(container)->size();
7171
case operation::clear:
7272
if constexpr(internal::dynamic_sequence_container<Type>::value) {
73-
static_cast<Type *>(value)->clear();
73+
static_cast<Type *>(const_cast<void *>(container))->clear();
7474
return true;
7575
} else {
7676
break;
7777
}
7878
case operation::reserve:
7979
if constexpr(internal::reserve_aware_container<Type>::value) {
80-
static_cast<Type *>(value)->reserve(*static_cast<const size_type *>(cvalue));
80+
static_cast<Type *>(const_cast<void *>(container))->reserve(*static_cast<const size_type *>(value));
8181
return true;
8282
} else {
8383
break;
8484
}
8585
case operation::resize:
8686
if constexpr(internal::dynamic_sequence_container<Type>::value) {
87-
static_cast<Type *>(value)->resize(*static_cast<const size_type *>(cvalue));
87+
static_cast<Type *>(const_cast<void *>(container))->resize(*static_cast<const size_type *>(value));
8888
return true;
8989
} else {
9090
break;
9191
}
9292
case operation::begin:
93-
if(value) {
94-
it->rebind(static_cast<Type *>(value)->begin());
95-
} else {
96-
it->rebind(static_cast<const Type *>(cvalue)->begin());
97-
}
98-
93+
it->rebind(static_cast<Type *>(const_cast<void *>(container))->begin());
94+
return true;
95+
case operation::cbegin:
96+
it->rebind(static_cast<const Type *>(container)->begin());
9997
return true;
10098
case operation::end:
101-
if(value) {
102-
it->rebind(static_cast<Type *>(value)->end());
103-
} else {
104-
it->rebind(static_cast<const Type *>(cvalue)->end());
105-
}
106-
99+
it->rebind(static_cast<Type *>(const_cast<void *>(container))->end());
100+
return true;
101+
case operation::cend:
102+
it->rebind(static_cast<const Type *>(container)->end());
107103
return true;
108104
case operation::insert:
109105
if constexpr(internal::dynamic_sequence_container<Type>::value) {
110106
auto *const non_const = any_cast<typename Type::iterator>(&it->base());
111107
typename Type::const_iterator underlying{non_const ? *non_const : any_cast<const typename Type::const_iterator &>(it->base())};
112108

113109
// this abomination is necessary because only on macos value_type and const_reference are different types for std::vector<bool>
114-
if(auto &as_any = *static_cast<meta_any *>(const_cast<void *>(cvalue)); as_any.allow_cast<typename Type::const_reference>() || as_any.allow_cast<typename Type::value_type>()) {
110+
if(auto &as_any = *static_cast<meta_any *>(const_cast<void *>(value)); as_any.allow_cast<typename Type::const_reference>() || as_any.allow_cast<typename Type::value_type>()) {
115111
const auto *element = as_any.try_cast<std::remove_reference_t<typename Type::const_reference>>();
116-
it->rebind(static_cast<Type *>(value)->insert(underlying, element ? *element : as_any.cast<typename Type::value_type>()));
112+
it->rebind(static_cast<Type *>(const_cast<void *>(container))->insert(underlying, element ? *element : as_any.cast<typename Type::value_type>()));
117113
return true;
118114
}
119115
}
@@ -123,7 +119,7 @@ class basic_meta_sequence_container_traits {
123119
if constexpr(internal::dynamic_sequence_container<Type>::value) {
124120
auto *const non_const = any_cast<typename Type::iterator>(&it->base());
125121
typename Type::const_iterator underlying{non_const ? *non_const : any_cast<const typename Type::const_iterator &>(it->base())};
126-
it->rebind(static_cast<Type *>(value)->erase(underlying));
122+
it->rebind(static_cast<Type *>(const_cast<void *>(container))->erase(underlying));
127123
return true;
128124
} else {
129125
break;

src/entt/meta/meta.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class meta_sequence_container {
7575
private:
7676
const meta_ctx *ctx{};
7777
internal::meta_type_node (*value_type_node)(const internal::meta_context &){};
78-
size_type (*vtable)(const operation, const void *, void *, iterator *){};
78+
size_type (*vtable)(const operation, const void *, const void *, iterator *){};
7979
any storage{};
8080
};
8181

@@ -1782,15 +1782,15 @@ class meta_associative_container::meta_iterator final {
17821782
* @return True in case of success, false otherwise.
17831783
*/
17841784
inline bool meta_sequence_container::resize(const size_type sz) {
1785-
return (storage.policy() != any_policy::cref) && vtable(operation::resize, &sz, storage.data(), nullptr);
1785+
return (storage.policy() != any_policy::cref) && vtable(operation::resize, storage.data(), &sz, nullptr);
17861786
}
17871787

17881788
/**
17891789
* @brief Clears the content of a container.
17901790
* @return True in case of success, false otherwise.
17911791
*/
17921792
inline bool meta_sequence_container::clear() {
1793-
return (storage.policy() != any_policy::cref) && vtable(operation::clear, nullptr, storage.data(), nullptr);
1793+
return (storage.policy() != any_policy::cref) && vtable(operation::clear, storage.data(), nullptr, nullptr);
17941794
}
17951795

17961796
/**
@@ -1799,7 +1799,7 @@ inline bool meta_sequence_container::clear() {
17991799
* @return True in case of success, false otherwise.
18001800
*/
18011801
inline bool meta_sequence_container::reserve(const size_type sz) {
1802-
return (storage.policy() != any_policy::cref) && vtable(operation::reserve, &sz, storage.data(), nullptr);
1802+
return (storage.policy() != any_policy::cref) && vtable(operation::reserve, storage.data(), &sz, nullptr);
18031803
}
18041804

18051805
/**
@@ -1808,8 +1808,7 @@ inline bool meta_sequence_container::reserve(const size_type sz) {
18081808
*/
18091809
[[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::begin() {
18101810
iterator it{*ctx};
1811-
const void *data = std::as_const(storage).data();
1812-
vtable(operation::begin, data, storage.policy() == any_policy::cref ? nullptr : const_cast<void *>(data), &it);
1811+
vtable(storage.policy() == any_policy::cref ? operation::cbegin : operation::begin, std::as_const(storage).data(), nullptr, &it);
18131812
return it;
18141813
}
18151814

@@ -1819,8 +1818,7 @@ inline bool meta_sequence_container::reserve(const size_type sz) {
18191818
*/
18201819
[[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::end() {
18211820
iterator it{*ctx};
1822-
const void *data = std::as_const(storage).data();
1823-
vtable(operation::end, data, storage.policy() == any_policy::cref ? nullptr : const_cast<void *>(data), &it);
1821+
vtable(storage.policy() == any_policy::cref ? operation::cend : operation::end, std::as_const(storage).data(), nullptr, &it);
18241822
return it;
18251823
}
18261824

@@ -1831,7 +1829,7 @@ inline bool meta_sequence_container::reserve(const size_type sz) {
18311829
* @return A possibly invalid iterator to the inserted element.
18321830
*/
18331831
inline meta_sequence_container::iterator meta_sequence_container::insert(iterator it, meta_any value) {
1834-
return ((storage.policy() != any_policy::cref) && vtable(operation::insert, &value, storage.data(), &it)) ? it : iterator{*ctx};
1832+
return ((storage.policy() != any_policy::cref) && vtable(operation::insert, storage.data(), &value, &it)) ? it : iterator{*ctx};
18351833
}
18361834

18371835
/**
@@ -1840,7 +1838,7 @@ inline meta_sequence_container::iterator meta_sequence_container::insert(iterato
18401838
* @return A possibly invalid iterator following the last removed element.
18411839
*/
18421840
inline meta_sequence_container::iterator meta_sequence_container::erase(iterator it) {
1843-
return ((storage.policy() != any_policy::cref) && vtable(operation::erase, nullptr, storage.data(), &it)) ? it : iterator{*ctx};
1841+
return ((storage.policy() != any_policy::cref) && vtable(operation::erase, storage.data(), nullptr, &it)) ? it : iterator{*ctx};
18441842
}
18451843

18461844
/**

src/entt/meta/type_traits.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ enum class meta_sequence_container_operation {
1919
reserve,
2020
resize,
2121
begin,
22+
cbegin,
2223
end,
24+
cend,
2325
insert,
2426
erase
2527
};

0 commit comments

Comments
 (0)