Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
map.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs {
9namespace _ {
10
11template <typename V>
12inline enable_if_t<is_pointer<V>::value, V>
13map_value_cast(ecs_map_val_t v) {
14 return reinterpret_cast<V>(static_cast<uintptr_t>(v));
15}
16
17template <typename V>
18inline enable_if_t<!is_pointer<V>::value, V>
19map_value_cast(ecs_map_val_t v) {
20 return static_cast<V>(v);
21}
22
23} // namespace _
24
25template <typename K, typename V>
26struct map_entry {
27 K first;
28 V second;
29};
30
31template <typename K, typename V>
34 : map_(nullptr)
35 , done_(true) { }
36
37 explicit map_iterator(const ecs_map_t *m)
38 : map_(m)
39 {
40 if (m) {
41 it_ = ecs_map_iter(m);
42 advance();
43 } else {
44 done_ = true;
45 }
46 }
47
48 bool operator!=(const map_iterator& other) const {
49 return done_ != other.done_;
50 }
51
52 const map_entry<K, V>& operator*() const {
53 return entry_;
54 }
55
56 const map_entry<K, V>* operator->() const {
57 return &entry_;
58 }
59
60 map_iterator& operator++() {
61 advance();
62 return *this;
63 }
64
65private:
66 void advance() {
67 if (ecs_map_next(&it_)) {
68 entry_.first = static_cast<K>(ecs_map_key(&it_));
69 entry_.second = _::map_value_cast<V>(ecs_map_value(&it_));
70 done_ = false;
71 } else {
72 done_ = true;
73 }
74 }
75
76 const ecs_map_t *map_;
77 ecs_map_iter_t it_ = {};
78 map_entry<K, V> entry_ = {};
79 bool done_ = false;
80};
81
82template <typename K, typename V>
83struct map {
84 explicit map(const ecs_map_t *m) : map_(m) { }
85
86 map_iterator<K, V> begin() const {
87 return map_iterator<K, V>(map_);
88 }
89
90 map_iterator<K, V> end() const {
91 return map_iterator<K, V>();
92 }
93
94private:
95 const ecs_map_t *map_;
96};
97
98}
Definition map.hpp:26