EnTT: src/entt/meta/container.hpp Source File

EnTT 3.15.0

Loading...
Searching...
No Matches
container.hpp
1// IWYU pragma: always_keep
2
3#ifndef ENTT_META_CONTAINER_HPP
4#define ENTT_META_CONTAINER_HPP
5
6#include <array>
7#include <deque>
8#include <iterator>
9#include <list>
10#include <map>
11#include <set>
12#include <type_traits>
13#include <unordered_map>
14#include <unordered_set>
15#include <vector>
16#include "../container/dense_map.hpp"
17#include "../container/dense_set.hpp"
18#include "context.hpp"
19#include "meta.hpp"
20#include "type_traits.hpp"
21
22namespace entt {
23
25namespace internal {
26
27template<typename, typename = void>
28struct fixed_size_sequence_container: std::true_type {};
29
30template<typename Type>
31struct fixed_size_sequence_container<Type, std::void_t<decltype(&Type::clear)>>: std::false_type {};
32
33template<typename Type>
34inline constexpr bool fixed_size_sequence_container_v = fixed_size_sequence_container<Type>::value;
35
36template<typename, typename = void>
37struct key_only_associative_container: std::true_type {};
38
39template<typename Type>
40struct key_only_associative_container<Type, std::void_t<typename Type::mapped_type>>: std::false_type {};
41
42template<typename Type>
43inline constexpr bool key_only_associative_container_v = key_only_associative_container<Type>::value;
44
45template<typename, typename = void>
46struct reserve_aware_container: std::false_type {};
47
48template<typename Type>
49struct reserve_aware_container<Type, std::void_t<decltype(&Type::reserve)>>: std::true_type {};
50
51template<typename Type>
52inline constexpr bool reserve_aware_container_v = reserve_aware_container<Type>::value;
53
54} // namespace internal
56
61template<typename Type>
62 struct basic_meta_sequence_container_traits {
63 static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Unexpected type");
64
66 using size_type = typename meta_sequence_container::size_type;
68 using iterator = typename meta_sequence_container::iterator;
69
71 static constexpr bool fixed_size = internal::fixed_size_sequence_container_v<Type>;
72
78 [[nodiscard]] static size_type size(const void *container) {
79 return static_cast<const Type *>(container)->size();
80 }
81
87 [[nodiscard]] static bool clear([[maybe_unused]] void *container) {
88 if constexpr(fixed_size) {
89 return false;
90 } else {
91 static_cast<Type *>(container)->clear();
92 return true;
93 }
94 }
95
102 [[nodiscard]] static bool reserve([[maybe_unused]] void *container, [[maybe_unused]] const size_type sz) {
103 if constexpr(internal::reserve_aware_container_v<Type>) {
104 static_cast<Type *>(container)->reserve(sz);
105 return true;
106 } else {
107 return false;
108 }
109 }
110
117 [[nodiscard]] static bool resize([[maybe_unused]] void *container, [[maybe_unused]] const size_type sz) {
118 if constexpr(fixed_size || !std::is_default_constructible_v<typename Type::value_type>) {
119 return false;
120 } else {
121 static_cast<Type *>(container)->resize(sz);
122 return true;
123 }
124 }
125
133 static iterator begin(const meta_ctx &area, void *container, const void *as_const) {
134 return (container != nullptr) ? iterator{area, static_cast<Type *>(container)->begin()}
135 : iterator{area, static_cast<const Type *>(as_const)->begin()};
136 }
137
145 static iterator end(const meta_ctx &area, void *container, const void *as_const) {
146 return (container != nullptr) ? iterator{area, static_cast<Type *>(container)->end()}
147 : iterator{area, static_cast<const Type *>(as_const)->end()};
148 }
149
162 [[nodiscard]] static iterator insert([[maybe_unused]] const meta_ctx &area, [[maybe_unused]] void *container, [[maybe_unused]] const void *value, [[maybe_unused]] const void *cref, [[maybe_unused]] const iterator &it) {
163 if constexpr(fixed_size) {
164 return iterator{};
165 } else {
166 auto *const non_const = any_cast<typename Type::iterator>(&it.base());
167 return {area, static_cast<Type *>(container)->insert(
168 non_const ? *non_const : any_cast<const typename Type::const_iterator &>(it.base()),
169 (value != nullptr) ? *static_cast<const typename Type::value_type *>(value) : *static_cast<const std::remove_reference_t<typename Type::const_reference> *>(cref))};
170 }
171 }
172
180 [[nodiscard]] static iterator erase([[maybe_unused]] const meta_ctx &area, [[maybe_unused]] void *container, [[maybe_unused]] const iterator &it) {
181 if constexpr(fixed_size) {
182 return iterator{};
183 } else {
184 auto *const non_const = any_cast<typename Type::iterator>(&it.base());
185 return {area, static_cast<Type *>(container)->erase(non_const ? *non_const : any_cast<const typename Type::const_iterator &>(it.base()))};
186 }
187 }
188};
189
194template<typename Type>
195 struct basic_meta_associative_container_traits {
196 static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Unexpected type");
197
199 using size_type = typename meta_associative_container::size_type;
201 using iterator = typename meta_associative_container::iterator;
202
204 static constexpr bool key_only = internal::key_only_associative_container_v<Type>;
205
211 [[nodiscard]] static size_type size(const void *container) {
212 return static_cast<const Type *>(container)->size();
213 }
214
220 [[nodiscard]] static bool clear(void *container) {
221 static_cast<Type *>(container)->clear();
222 return true;
223 }
224
231 [[nodiscard]] static bool reserve([[maybe_unused]] void *container, [[maybe_unused]] const size_type sz) {
232 if constexpr(internal::reserve_aware_container_v<Type>) {
233 static_cast<Type *>(container)->reserve(sz);
234 return true;
235 } else {
236 return false;
237 }
238 }
239
247 static iterator begin(const meta_ctx &area, void *container, const void *as_const) {
248 return (container != nullptr) ? iterator{area, std::bool_constant<key_only>{}, static_cast<Type *>(container)->begin()}
249 : iterator{area, std::bool_constant<key_only>{}, static_cast<const Type *>(as_const)->begin()};
250 }
251
259 static iterator end(const meta_ctx &area, void *container, const void *as_const) {
260 return (container != nullptr) ? iterator{area, std::bool_constant<key_only>{}, static_cast<Type *>(container)->end()}
261 : iterator{area, std::bool_constant<key_only>{}, static_cast<const Type *>(as_const)->end()};
262 }
263
271 [[nodiscard]] static bool insert(void *container, const void *key, [[maybe_unused]] const void *value) {
272 if constexpr(key_only) {
273 return static_cast<Type *>(container)->insert(*static_cast<const typename Type::key_type *>(key)).second;
274 } else {
275 return static_cast<Type *>(container)->emplace(*static_cast<const typename Type::key_type *>(key), *static_cast<const typename Type::mapped_type *>(value)).second;
276 }
277 }
278
285 [[nodiscard]] static size_type erase(void *container, const void *key) {
286 return static_cast<Type *>(container)->erase(*static_cast<const typename Type::key_type *>(key));
287 }
288
297 static iterator find(const meta_ctx &area, void *container, const void *as_const, const void *key) {
298 return (container != nullptr) ? iterator{area, std::bool_constant<key_only>{}, static_cast<Type *>(container)->find(*static_cast<const typename Type::key_type *>(key))}
299 : iterator{area, std::bool_constant<key_only>{}, static_cast<const Type *>(as_const)->find(*static_cast<const typename Type::key_type *>(key))};
300 }
301};
302
307template<typename... Args>
308 struct meta_sequence_container_traits<std::vector<Args...>>
309 : basic_meta_sequence_container_traits<std::vector<Args...>> {};
310
316template<typename Type, auto N>
317 struct meta_sequence_container_traits<std::array<Type, N>>
318 : basic_meta_sequence_container_traits<std::array<Type, N>> {};
319
324template<typename... Args>
325 struct meta_sequence_container_traits<std::list<Args...>>
326 : basic_meta_sequence_container_traits<std::list<Args...>> {};
327
332template<typename... Args>
333 struct meta_sequence_container_traits<std::deque<Args...>>
334 : basic_meta_sequence_container_traits<std::deque<Args...>> {};
335
340template<typename... Args>
341 struct meta_associative_container_traits<std::map<Args...>>
342 : basic_meta_associative_container_traits<std::map<Args...>> {};
343
349template<typename... Args>
350 struct meta_associative_container_traits<std::unordered_map<Args...>>
351 : basic_meta_associative_container_traits<std::unordered_map<Args...>> {};
352
357template<typename... Args>
358 struct meta_associative_container_traits<std::set<Args...>>
359 : basic_meta_associative_container_traits<std::set<Args...>> {};
360
366template<typename... Args>
367 struct meta_associative_container_traits<std::unordered_set<Args...>>
368 : basic_meta_associative_container_traits<std::unordered_set<Args...>> {};
369
374template<typename... Args>
375 struct meta_associative_container_traits<dense_map<Args...>>
376 : basic_meta_associative_container_traits<dense_map<Args...>> {};
377
382template<typename... Args>
383 struct meta_associative_container_traits<dense_set<Args...>>
384 : basic_meta_associative_container_traits<dense_set<Args...>> {};
385
386} // namespace entt
387
388#endif
entt::dense_map
Associative container for key-value pairs with unique keys.
Definition dense_map.hpp:260
entt::dense_set
Associative container for unique objects of a given type.
Definition dense_set.hpp:220
entt::meta_associative_container::size_type
std::size_t size_type
Unsigned integer type.
Definition meta.hpp:99
entt::meta_associative_container::iterator
meta_iterator iterator
Meta iterator type.
Definition meta.hpp:101
entt::meta_ctx
Opaque meta context type.
Definition context.hpp:34
entt::meta_sequence_container::iterator
meta_iterator iterator
Meta iterator type.
Definition meta.hpp:38
entt::meta_sequence_container::size_type
std::size_t size_type
Unsigned integer type.
Definition meta.hpp:36
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::any_cast
std::remove_const_t< Type > any_cast(const basic_any< Len, Align > &data) noexcept
Performs type-safe access to the contained object.
Definition any.hpp:464
entt::any_policy::cref
@ cref
Const aliasing mode, the object points to a const element.
Definition fwd.hpp:21
entt::basic_meta_associative_container_traits
General purpose implementation of meta associative container traits.
Definition container.hpp:195
entt::basic_meta_associative_container_traits::begin
static iterator begin(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the beginning.
Definition container.hpp:247
entt::basic_meta_associative_container_traits::find
static iterator find(const meta_ctx &area, void *container, const void *as_const, const void *key)
Finds an element with a given key.
Definition container.hpp:297
entt::basic_meta_associative_container_traits::end
static iterator end(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the end.
Definition container.hpp:259
entt::basic_meta_associative_container_traits::reserve
static bool reserve(void *container, const size_type sz)
Increases the capacity of a container.
Definition container.hpp:231
entt::basic_meta_associative_container_traits::iterator
typename meta_associative_container::iterator iterator
Meta iterator type.
Definition container.hpp:201
entt::basic_meta_associative_container_traits::key_only
static constexpr bool key_only
True in case of key-only containers, false otherwise.
Definition container.hpp:204
entt::basic_meta_associative_container_traits::size
static size_type size(const void *container)
Returns the number of elements in a container.
Definition container.hpp:211
entt::basic_meta_associative_container_traits::size_type
typename meta_associative_container::size_type size_type
Unsigned integer type.
Definition container.hpp:199
entt::basic_meta_associative_container_traits::clear
static bool clear(void *container)
Clears a container.
Definition container.hpp:220
entt::basic_meta_associative_container_traits::insert
static bool insert(void *container, const void *key, const void *value)
Inserts an element into a container, if the key does not exist.
Definition container.hpp:271
entt::basic_meta_associative_container_traits::erase
static size_type erase(void *container, const void *key)
Removes an element from a container.
Definition container.hpp:285
entt::basic_meta_sequence_container_traits
General purpose implementation of meta sequence container traits.
Definition container.hpp:62
entt::basic_meta_sequence_container_traits::size_type
typename meta_sequence_container::size_type size_type
Unsigned integer type.
Definition container.hpp:66
entt::basic_meta_sequence_container_traits::iterator
typename meta_sequence_container::iterator iterator
Meta iterator type.
Definition container.hpp:68
entt::basic_meta_sequence_container_traits::begin
static iterator begin(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the beginning.
Definition container.hpp:133
entt::basic_meta_sequence_container_traits::size
static size_type size(const void *container)
Returns the number of elements in a container.
Definition container.hpp:78
entt::basic_meta_sequence_container_traits::end
static iterator end(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the end.
Definition container.hpp:145
entt::basic_meta_sequence_container_traits::erase
static iterator erase(const meta_ctx &area, void *container, const iterator &it)
Erases an element from a container.
Definition container.hpp:180
entt::basic_meta_sequence_container_traits::insert
static iterator insert(const meta_ctx &area, void *container, const void *value, const void *cref, const iterator &it)
Assigns one element to a container and constructs its object from a given opaque instance.
Definition container.hpp:162
entt::basic_meta_sequence_container_traits::resize
static bool resize(void *container, const size_type sz)
Resizes a container.
Definition container.hpp:117
entt::basic_meta_sequence_container_traits::reserve
static bool reserve(void *container, const size_type sz)
Increases the capacity of a container.
Definition container.hpp:102
entt::basic_meta_sequence_container_traits::fixed_size
static constexpr bool fixed_size
True in case of key-only containers, false otherwise.
Definition container.hpp:71
entt::basic_meta_sequence_container_traits::clear
static bool clear(void *container)
Clears a container.
Definition container.hpp:87
entt::meta_associative_container_traits
Traits class template to be specialized to enable support for meta associative containers.
Definition type_traits.hpp:28
entt::meta_sequence_container_traits
Traits class template to be specialized to enable support for meta sequence containers.
Definition type_traits.hpp:21