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