EnTT  3.5.0
container.hpp
1 #ifndef ENTT_META_CONTAINER_HPP
2 #define ENTT_META_CONTAINER_HPP
3 
4 
5 #include <array>
6 #include <map>
7 #include <set>
8 #include <unordered_map>
9 #include <unordered_set>
10 #include <utility>
11 #include <vector>
12 #include "../config/config.h"
13 #include "type_traits.hpp"
14 
15 
16 namespace entt {
17 
18 
19 namespace internal {
20 
21 
22 template<typename Container, template<typename> class... Trait>
23 struct container_traits: public Trait<Container>... {};
24 
25 
30 template<typename Container>
33  using iterator = typename Container::iterator;
35  using size_type = typename Container::size_type;
37  using value_type = typename Container::value_type;
38 
44  [[nodiscard]] static size_type size(const Container &cont) ENTT_NOEXCEPT {
45  return cont.size();
46  }
47 
53  [[nodiscard]] static iterator begin(Container &cont) {
54  return cont.begin();
55  }
56 
62  [[nodiscard]] static iterator end(Container &cont) {
63  return cont.end();
64  }
65 };
66 
67 
72 template<typename Container>
75  using key_type = typename Container::key_type;
76 
84  [[nodiscard]] static typename Container::iterator find(Container &cont, const key_type &key) {
85  return cont.find(key);
86  }
87 };
88 
89 
94 template<typename Container>
101  [[nodiscard]] static bool clear(Container &cont) {
102  return cont.clear(), true;
103  }
104 };
105 
106 
111 template<typename Container>
119  [[nodiscard]] static bool erase(Container &cont, const typename Container::key_type &key) {
120  const auto sz = cont.size();
121  return cont.erase(key) != sz;
122  }
123 };
124 
125 
130 template<typename Container>
139  [[nodiscard]] static typename Container::value_type & get(Container &cont, typename Container::size_type pos) {
140  return cont[pos];
141  }
142 };
143 
144 
149 template<typename Container>
157  [[nodiscard]] static bool insert(Container &cont, const typename Container::key_type &key) {
158  return cont.insert(key).second;
159  }
160 };
161 
162 
167 template<typename Container>
176  [[nodiscard]] static bool insert(Container &cont, const typename Container::key_type &key, const typename Container::mapped_type &value) {
177  return cont.insert(std::make_pair(key, value)).second;
178  }
179 };
180 
181 
186 template<typename Container>
194  [[nodiscard]] static bool resize(Container &cont, typename Container::size_type sz) {
195  return (cont.resize(sz), true);
196  }
197 
206  [[nodiscard]] static std::pair<typename Container::iterator, bool> insert(Container &cont, typename Container::iterator it, const typename Container::value_type &value) {
207  return { cont.insert(it, value), true };
208  }
209 
218  [[nodiscard]] static std::pair<typename Container::iterator, bool> erase(Container &cont, typename Container::iterator it) {
219  return { cont.erase(it), true };
220  }
221 };
222 
223 
228 template<typename Container>
234  [[nodiscard]] static bool resize(const Container &, typename Container::size_type) {
235  return false;
236  }
237 
242  [[nodiscard]] static bool clear(const Container &) {
243  return false;
244  }
245 
251  [[nodiscard]] static std::pair<typename Container::iterator, bool> insert(const Container &, typename Container::iterator, const typename Container::value_type &) {
252  return { {}, false };
253  }
254 
260  [[nodiscard]] static std::pair<typename Container::iterator, bool> erase(const Container &, typename Container::iterator) {
261  return { {}, false };
262  }
263 };
264 
265 
266 }
267 
268 
274 template<typename Type, typename... Args>
275 struct meta_sequence_container_traits<std::vector<Type, Args...>>
277  std::vector<Type, Args...>,
278  internal::basic_container,
279  internal::basic_dynamic_container,
280  internal::basic_sequence_container,
281  internal::dynamic_sequence_container
282  >
283 {};
284 
285 
291 template<typename Type, auto N>
292 struct meta_sequence_container_traits<std::array<Type, N>>
294  std::array<Type, N>,
295  internal::basic_container,
296  internal::basic_sequence_container,
297  internal::fixed_sequence_container
298  >
299 {};
300 
301 
308 template<typename Key, typename Value, typename... Args>
309 struct meta_associative_container_traits<std::map<Key, Value, Args...>>
311  std::map<Key, Value, Args...>,
312  internal::basic_container,
313  internal::basic_associative_container,
314  internal::basic_dynamic_container,
315  internal::basic_dynamic_associative_container,
316  internal::dynamic_associative_key_value_container
317  >
318 {
320  using mapped_type = typename std::map<Key, Value, Args...>::mapped_type;
321 };
322 
323 
331 template<typename Key, typename Value, typename... Args>
332 struct meta_associative_container_traits<std::unordered_map<Key, Value, Args...>>
334  std::unordered_map<Key, Value, Args...>,
335  internal::basic_container,
336  internal::basic_associative_container,
337  internal::basic_dynamic_container,
338  internal::basic_dynamic_associative_container,
339  internal::dynamic_associative_key_value_container
340  >
341 {
343  using mapped_type = typename std::unordered_map<Key, Value, Args...>::mapped_type;
344 };
345 
346 
352 template<typename Key, typename... Args>
353 struct meta_associative_container_traits<std::set<Key, Args...>>
355  std::set<Key, Args...>,
356  internal::basic_container,
357  internal::basic_associative_container,
358  internal::basic_dynamic_container,
359  internal::basic_dynamic_associative_container,
360  internal::dynamic_associative_key_only_container
361  >
362 {};
363 
364 
371 template<typename Key, typename... Args>
372 struct meta_associative_container_traits<std::unordered_set<Key, Args...>>
374  std::unordered_set<Key, Args...>,
375  internal::basic_container,
376  internal::basic_associative_container,
377  internal::basic_dynamic_container,
378  internal::basic_dynamic_associative_container,
379  internal::dynamic_associative_key_only_container
380  >
381 {};
382 
383 
384 }
385 
386 
387 #endif
entt::internal::basic_container
Basic STL-compatible container traits.
Definition: container.hpp:31
entt::internal::fixed_sequence_container
STL-compatible fixed sequence container traits.
Definition: container.hpp:229
entt::internal::basic_dynamic_associative_container
Basic STL-compatible dynamic associative container traits.
Definition: container.hpp:112
entt::internal::container_traits
Definition: container.hpp:23
entt::internal::basic_container::begin
static iterator begin(Container &cont)
Returns an iterator to the first element of the given container.
Definition: container.hpp:53
entt::internal::basic_associative_container
Basic STL-compatible associative container traits.
Definition: container.hpp:73
entt::internal::dynamic_sequence_container::erase
static std::pair< typename Container::iterator, bool > erase(Container &cont, typename Container::iterator it)
Removes the element at the specified location from the given container.
Definition: container.hpp:218
entt::internal::basic_container::end
static iterator end(Container &cont)
Returns an iterator past the last element of the given container.
Definition: container.hpp:62
entt::internal::basic_dynamic_container
Basic STL-compatible dynamic container traits.
Definition: container.hpp:95
entt::internal::basic_sequence_container
Basic STL-compatible sequence container traits.
Definition: container.hpp:131
entt::internal::fixed_sequence_container::erase
static std::pair< typename Container::iterator, bool > erase(const Container &, typename Container::iterator)
Does nothing.
Definition: container.hpp:260
entt::meta_associative_container_traits< std::unordered_map< Key, Value, Args... > >::mapped_type
typename std::unordered_map< Key, Value, Args... >::mapped_type mapped_type
Mapped type of the sequence container.
Definition: container.hpp:343
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::internal::dynamic_associative_key_value_container::insert
static bool insert(Container &cont, const typename Container::key_type &key, const typename Container::mapped_type &value)
Inserts an element (a key/value pair) into the given container.
Definition: container.hpp:176
entt::internal::basic_sequence_container::get
static Container::value_type & get(Container &cont, typename Container::size_type pos)
Returns a reference to the element at the specified location of the given container (no bounds checki...
Definition: container.hpp:139
entt::internal::dynamic_sequence_container
STL-compatible dynamic sequence container traits.
Definition: container.hpp:187
entt::internal::dynamic_sequence_container::resize
static bool resize(Container &cont, typename Container::size_type sz)
Resizes the given container to contain the given number of elements.
Definition: container.hpp:194
entt::meta_associative_container_traits
Traits class template to be specialized to enable support for meta associative containers.
Definition: type_traits.hpp:24
entt::internal::basic_dynamic_associative_container::erase
static bool erase(Container &cont, const typename Container::key_type &key)
Removes the specified element from the given container.
Definition: container.hpp:119
entt::internal::basic_container< std::unordered_set< Key, Args... > >::value_type
typename Container::value_type value_type
Value type of the container.
Definition: container.hpp:37
entt::internal::fixed_sequence_container::resize
static bool resize(const Container &, typename Container::size_type)
Does nothing.
Definition: container.hpp:234
entt::meta_associative_container_traits< std::map< Key, Value, Args... > >::mapped_type
typename std::map< Key, Value, Args... >::mapped_type mapped_type
Mapped type of the sequence container.
Definition: container.hpp:320
entt::internal::basic_container< std::unordered_set< Key, Args... > >::iterator
typename Container::iterator iterator
Iterator type of the container.
Definition: container.hpp:33
entt::internal::basic_dynamic_container::clear
static bool clear(Container &cont)
Clears the content of the given container.
Definition: container.hpp:101
entt::internal::basic_container< std::unordered_set< Key, Args... > >::size_type
typename Container::size_type size_type
Unsigned integer type.
Definition: container.hpp:35
entt::meta_sequence_container_traits
Traits class template to be specialized to enable support for meta sequence containers.
Definition: type_traits.hpp:16
entt::internal::dynamic_sequence_container::insert
static std::pair< typename Container::iterator, bool > insert(Container &cont, typename Container::iterator it, const typename Container::value_type &value)
Inserts an element at the specified location of the given container.
Definition: container.hpp:206
entt::internal::fixed_sequence_container::insert
static std::pair< typename Container::iterator, bool > insert(const Container &, typename Container::iterator, const typename Container::value_type &)
Does nothing.
Definition: container.hpp:251
entt::internal::basic_associative_container::find
static Container::iterator find(Container &cont, const key_type &key)
Returns an iterator to the element with key equivalent to the given one, if any.
Definition: container.hpp:84
entt::internal::dynamic_associative_key_only_container::insert
static bool insert(Container &cont, const typename Container::key_type &key)
Inserts an element into the given container.
Definition: container.hpp:157
entt::internal::fixed_sequence_container::clear
static bool clear(const Container &)
Does nothing.
Definition: container.hpp:242
entt::internal::basic_container::size
static size_type size(const Container &cont) noexcept
Returns the size of the given container.
Definition: container.hpp:44
entt::internal::dynamic_associative_key_value_container
STL-compatible dynamic key-value associative container traits.
Definition: container.hpp:168
entt::internal::dynamic_associative_key_only_container
STL-compatible dynamic associative key-only container traits.
Definition: container.hpp:150
entt::internal::basic_associative_container::key_type
typename Container::key_type key_type
Key type of the sequence container.
Definition: container.hpp:75