EnTT 3.14.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
61template<typename Type>
63 static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Unexpected type");
64
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(
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
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>
196 static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Unexpected type");
197
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>
309 : basic_meta_sequence_container_traits<std::vector<Args...>> {};
310
316template<typename Type, auto N>
318 : basic_meta_sequence_container_traits<std::array<Type, N>> {};
319
324template<typename... Args>
326 : basic_meta_sequence_container_traits<std::list<Args...>> {};
327
332template<typename... Args>
334 : basic_meta_sequence_container_traits<std::deque<Args...>> {};
335
340template<typename... Args>
341struct meta_associative_container_traits<std::map<Args...>>
342 : basic_meta_associative_container_traits<std::map<Args...>> {};
343
349template<typename... Args>
351 : basic_meta_associative_container_traits<std::unordered_map<Args...>> {};
352
357template<typename... Args>
358struct meta_associative_container_traits<std::set<Args...>>
359 : basic_meta_associative_container_traits<std::set<Args...>> {};
360
366template<typename... Args>
368 : basic_meta_associative_container_traits<std::unordered_set<Args...>> {};
369
374template<typename... Args>
377
382template<typename... Args>
385
386} // namespace entt
387
388#endif
Associative container for key-value pairs with unique keys.
Associative container for unique objects of a given type.
std::size_t size_type
Unsigned integer type.
Definition meta.hpp:105
meta_iterator iterator
Meta iterator type.
Definition meta.hpp:107
Opaque meta context type.
Definition context.hpp:34
meta_iterator iterator
Meta iterator type.
Definition meta.hpp:38
std::size_t size_type
Unsigned integer type.
Definition meta.hpp:36
EnTT default namespace.
Definition dense_map.hpp:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
@ cref
Const aliasing mode, the object points to a const element.
General purpose implementation of meta associative container traits.
static iterator begin(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the beginning.
static iterator find(const meta_ctx &area, void *container, const void *as_const, const void *key)
Finds an element with a given key.
static iterator end(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the end.
static bool reserve(void *container, const size_type sz)
Increases the capacity of a container.
typename meta_associative_container::iterator iterator
Meta iterator type.
static constexpr bool key_only
True in case of key-only containers, false otherwise.
static size_type size(const void *container)
Returns the number of elements in a container.
typename meta_associative_container::size_type size_type
Unsigned integer type.
static bool clear(void *container)
Clears a container.
static bool insert(void *container, const void *key, const void *value)
Inserts an element into a container, if the key does not exist.
static size_type erase(void *container, const void *key)
Removes an element from a container.
General purpose implementation of meta sequence container traits.
Definition container.hpp:62
typename meta_sequence_container::size_type size_type
Unsigned integer type.
Definition container.hpp:66
typename meta_sequence_container::iterator iterator
Meta iterator type.
Definition container.hpp:68
static iterator begin(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the beginning.
static size_type size(const void *container)
Returns the number of elements in a container.
Definition container.hpp:78
static iterator end(const meta_ctx &area, void *container, const void *as_const)
Returns a possibly const iterator to the end.
static iterator erase(const meta_ctx &area, void *container, const iterator &it)
Erases an element from a container.
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.
static bool resize(void *container, const size_type sz)
Resizes a container.
static bool reserve(void *container, const size_type sz)
Increases the capacity of a container.
static constexpr bool fixed_size
True in case of key-only containers, false otherwise.
Definition container.hpp:71
static bool clear(void *container)
Clears a container.
Definition container.hpp:87
Traits class template to be specialized to enable support for meta associative containers.
Traits class template to be specialized to enable support for meta sequence containers.