Loading...
Searching...
No Matches
EntityMap.hpp
1#pragma once
2#include <score/model/IdentifiedObjectMap.hpp>
3
4#include <nano_signal_slot.hpp>
5
6#include <utility>
7
8namespace score
9{
10template <typename T>
11class Entity;
12template <typename T, bool Ordered>
13class EntityMap;
14template <typename T, bool Ordered>
16{
17public:
18 void add(EntityMap<T, Ordered>& map, T* t);
19 void add_quiet(EntityMap<T, Ordered>& map, T* t);
20};
21
35template <typename T, bool Ordered = false>
37{
38public:
39 // The real interface starts here
40 using value_type = T;
41 OSSIA_INLINE auto begin() const INLINE_EXPORT { return m_map.begin(); }
42 // OSSIA_INLINE auto rbegin() const INLINE_EXPORT { return m_map.rbegin(); }
43 OSSIA_INLINE auto cbegin() const INLINE_EXPORT { return m_map.cbegin(); }
44 OSSIA_INLINE auto end() const INLINE_EXPORT { return m_map.end(); }
45 // OSSIA_INLINE auto rend() const INLINE_EXPORT { return m_map.rend(); }
46 OSSIA_INLINE auto cend() const INLINE_EXPORT { return m_map.cend(); }
47 OSSIA_INLINE auto size() const INLINE_EXPORT { return m_map.size(); }
48 OSSIA_INLINE bool empty() const INLINE_EXPORT { return m_map.empty(); }
49 OSSIA_INLINE auto& unsafe_map() INLINE_EXPORT { return m_map; }
50 OSSIA_INLINE const auto& map() const INLINE_EXPORT { return m_map; }
51 OSSIA_INLINE const auto& get() const INLINE_EXPORT { return m_map.m_map; }
52 T& at(const Id<T>& id) INLINE_EXPORT { return m_map.at(id); }
53 T& at(const Id<T>& id) const INLINE_EXPORT { return m_map.at(id); }
54 auto find(const Id<T>& id) const INLINE_EXPORT { return m_map.find(id); }
55
56 // public:
57 mutable Nano::Signal<void(T&)> mutable_added;
58 mutable Nano::Signal<void(const T&)> added;
59 mutable Nano::Signal<void(const T&)> removing;
60 mutable Nano::Signal<void(const T&)> removed;
61 mutable Nano::Signal<void()> orderChanged;
62 mutable Nano::Signal<void()> replaced;
63
64 void add(T* t) INLINE_EXPORT { EntityMapInserter<T, Ordered>{}.add(*this, t); }
65 void add_quiet(T* t) INLINE_EXPORT
66 {
67 EntityMapInserter<T, Ordered>{}.add_quiet(*this, t);
68 }
69
70 void erase(T& elt) INLINE_EXPORT
71 {
72 removing(elt);
73 m_map.remove(elt.id());
74 removed(elt);
75 }
76
77 void remove(T& elt) INLINE_EXPORT
78 {
79 removing(elt);
80 m_map.remove(elt.id());
81 removed(elt);
82 delete &elt;
83 }
84
85 void remove(T* elt) INLINE_EXPORT
86 {
87 removing(*elt);
88 m_map.remove(elt->id());
89 removed(*elt);
90 delete elt;
91 }
92
93 void remove(const Id<T>& id) INLINE_EXPORT
94 {
95 auto it = m_map.m_map.find(id);
96 if constexpr(Ordered)
97 {
98 auto& elt = *it->second.first;
99
100 removing(elt);
101 m_map.remove(it);
102 removed(elt);
103 delete &elt;
104 }
105 else
106 {
107 auto& elt = *it->second;
108
109 removing(elt);
110 m_map.remove(it);
111 removed(elt);
112 delete &elt;
113 }
114 }
115
116 void clear() INLINE_EXPORT
117 {
118 while(!m_map.empty())
119 {
120 remove(*m_map.begin());
121 }
122 }
123
124 void replace(IdContainer<T, T, Ordered>&& new_map)
125 {
126 for(T& m : m_map)
127 delete &m;
128
129 m_map.clear();
130 m_map.m_map = std::move(new_map.m_map);
131 if constexpr(Ordered)
132 {
133 m_map.m_order = std::move(new_map.m_order);
134 }
135
136 replaced();
137 }
138
139private:
141};
142
143template <typename T, bool O>
145{
146 SCORE_ASSERT(t);
147 map.unsafe_map().insert(t);
148
149 map.mutable_added(*t);
150 map.added(*t);
151}
152
153template <typename T, bool O>
154void EntityMapInserter<T, O>::add_quiet(EntityMap<T, O>& map, T* t)
155{
156 SCORE_ASSERT(t);
157 map.unsafe_map().insert(t);
158}
159}
A map to access child objects through their id.
Definition IdentifiedObjectMap.hpp:16
The id_base_t class.
Definition Identifier.hpp:59
The EntityMap class.
Definition EntityMap.hpp:37
Definition EntityMap.hpp:16
Base toolkit upon which the software is built.
Definition Application.cpp:99