Sen API
Sen Libraries
Loading...
Searching...
No Matches
object_list.h
Go to the documentation of this file.
1// === object_list.h ===================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_OBJ_OBJECT_LIST_H
9#define SEN_CORE_OBJ_OBJECT_LIST_H
10
11// sen
15
16// std
17#include <list>
18
19namespace sen
20{
21
24
27template <typename T>
29{
30public:
31 SEN_MOVE_ONLY(ObjectList)
32
33 static constexpr std::size_t defaultListSizeHint = 10U;
34
35public: // types
42
43 using TypedObjectList = std::list<T*>;
44 using UntypedObjectList = std::list<std::shared_ptr<Object>>;
45
46 struct Iterators
47 {
48 typename TypedObjectList::iterator typedBegin;
49 typename TypedObjectList::iterator typedEnd;
50 typename UntypedObjectList::iterator untypedBegin;
51 typename UntypedObjectList::iterator untypedEnd;
52 };
53
54 using Callback = std::function<void(const Iterators& iterators)>;
55
56public: // special members
60 explicit ObjectList(std::size_t sizeHint = defaultListSizeHint) { iteratorMap_.reserve(sizeHint); }
61
63 ObjectList(ObjectProvider& provider, std::size_t sizeHint): ObjectList(sizeHint) { provider.addListener(this, true); }
64
65 ~ObjectList() override = default;
66
67public:
71 [[nodiscard]] Callback onAdded(Callback&& function) noexcept;
72
76 [[nodiscard]] Callback onRemoved(Callback&& function) noexcept
77 {
78 return std::exchange(onRemoved_, std::move(function));
79 }
80
81public: // multiple object lookup
83 [[nodiscard]] const std::list<T*>& getObjects() const noexcept { return typedObjects_; }
84
86 [[nodiscard]] const std::list<std::shared_ptr<Object>>& getUntypedObjects() const noexcept { return untypedObjects_; }
87
88protected: // implements ObjectProviderListener
89 void onObjectsAdded(const ObjectAdditionList& additions) override;
90 void onObjectsRemoved(const ObjectRemovalList& removals) override;
91
92private:
93 [[nodiscard]] T* getCastedObject(Object* object);
94
95private:
96 struct IteratorPair
97 {
98 typename TypedObjectList::const_iterator typed;
99 typename UntypedObjectList::const_iterator untyped;
100 };
101
102private:
103 TypedObjectList typedObjects_;
104 UntypedObjectList untypedObjects_;
105 std::unordered_map<ObjectId, IteratorPair> iteratorMap_;
106 Callback onAdded_ = nullptr;
107 Callback onRemoved_ = nullptr;
108};
109
111
112//----------------------------------------------------------------------------------------------------------------------
113// Inline implementation
114//----------------------------------------------------------------------------------------------------------------------
115
116template <typename T>
117inline typename ObjectList<T>::Callback ObjectList<T>::onAdded(Callback&& function) noexcept
118{
119 auto previous = std::exchange(onAdded_, std::move(function));
120
121 if (onAdded_ && !untypedObjects_.empty())
122 {
123 onAdded_({typedObjects_.begin(), typedObjects_.end(), untypedObjects_.begin(), untypedObjects_.end()});
124 }
125
126 return previous;
127}
128
129template <typename T>
130inline T* ObjectList<T>::getCastedObject(Object* object)
131{
132 if constexpr (!std::is_same_v<T, Object>)
133 {
134 auto* castedObject = dynamic_cast<T*>(object);
135 if (castedObject == nullptr)
136 {
137 std::string err;
138 err.append("error casting object named '");
139 err.append(object->getName());
140 err.append("' of class '");
141 err.append(object->getClass()->getQualifiedName());
142 err.append("' to a native type '");
143 err.append(typeid(T).name());
144 err.append("' within an ObjectList");
146 }
147
148 return castedObject;
149 }
150 else
151 {
152 return object;
153 }
154}
155
156template <typename T>
158{
159 Iterators iterators;
160 bool added = false;
161
162 for (const auto& addition: additions)
163 {
164 // only react to discovered instances
165 auto* instance = getObjectInstance(addition);
166 if (instance == nullptr)
167 {
168 continue;
169 }
170
171 auto castedObject = getCastedObject(instance);
172 auto lastTyped = typedObjects_.insert(typedObjects_.end(), castedObject);
173 auto lastUntyped = untypedObjects_.insert(untypedObjects_.end(), instance->shared_from_this());
174
175 if (!added)
176 {
177 iterators.untypedBegin = lastUntyped;
178 iterators.typedBegin = lastTyped;
179 added = true;
180 }
181
182 // store the object
183 iteratorMap_.insert({instance->getId(), {lastTyped, lastUntyped}});
184 }
185
186 if (!added)
187 {
188 return;
189 }
190
191 if (onAdded_)
192 {
193 iterators.typedEnd = typedObjects_.end();
194 iterators.untypedEnd = untypedObjects_.end();
195 onAdded_(iterators);
196 }
197}
198
199template <typename T>
201{
202 TypedObjectList typedRemovals;
203 UntypedObjectList untypedRemovals;
204
205 for (const auto& removal: removals)
206 {
207 auto itr = iteratorMap_.find(removal.objectid);
208 if (itr != iteratorMap_.end())
209 {
210 auto castedObject = *(itr->second.typed);
211 auto nonCastedObject = *(itr->second.untyped);
212
213 untypedObjects_.erase(itr->second.untyped);
214 typedObjects_.erase(itr->second.typed);
215 iteratorMap_.erase(itr);
216
217 if (onRemoved_)
218 {
219 typedRemovals.push_back(castedObject);
220 untypedRemovals.push_back(nonCastedObject);
221 }
222 }
223 }
224
225 if (onRemoved_ && !typedRemovals.empty())
226 {
227 Iterators iterators;
228 iterators.typedBegin = typedRemovals.begin();
229 iterators.typedEnd = typedRemovals.end();
230 iterators.untypedBegin = untypedRemovals.begin();
231 iterators.untypedEnd = untypedRemovals.end();
232
233 onRemoved_(iterators);
234 }
235}
236
237} // namespace sen
238
239#endif // SEN_CORE_OBJ_OBJECT_LIST_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...
Base class for event or method callbacks. It stores the queue where to push the response....
Definition callback.h:146
A sen object.
Definition object.h:76
virtual const std::string & getName() const noexcept=0
The name given to the object upon construction.
virtual ConstTypeHandle< ClassType > getClass() const noexcept=0
Reflection information.
TypedObjectList::iterator typedBegin
Definition object_list.h:48
std::list< T * > TypedObjectList
Definition object_list.h:43
SearchMode
How to search for objects.
Definition object_list.h:38
@ ignoreSubClasses
Definition object_list.h:40
@ includeSubClasses
Definition object_list.h:39
Callback onAdded(Callback &&function) noexcept
Installs a function to be called when objects are added / discovered. This function will be called du...
Definition object_list.h:117
const std::list< T * > & getObjects() const noexcept
Gets the list of currently-registered objects.
Definition object_list.h:83
static constexpr std::size_t defaultListSizeHint
Definition object_list.h:33
ObjectList(ObjectProvider &provider, std::size_t sizeHint)
Automatically adds itself as a listener to the provider.
Definition object_list.h:63
UntypedObjectList::iterator untypedEnd
Definition object_list.h:51
std::list< std::shared_ptr< Object > > UntypedObjectList
Definition object_list.h:44
std::function< void(const Iterators &iterators)> Callback
Definition object_list.h:54
void onObjectsRemoved(const ObjectRemovalList &removals) override
Called when objects will be removed from a source.
Definition object_list.h:200
const std::list< std::shared_ptr< Object > > & getUntypedObjects() const noexcept
Gets the list of currently-registered objects.
Definition object_list.h:86
UntypedObjectList::iterator untypedBegin
Definition object_list.h:50
void onObjectsAdded(const ObjectAdditionList &additions) override
Called when objects are been added to a source.
Definition object_list.h:157
~ObjectList() override=default
TypedObjectList::iterator typedEnd
Definition object_list.h:49
ObjectList(std::size_t sizeHint=defaultListSizeHint)
The sizeHint reserves space the internal containers to prevent memory reallocation in the initial ite...
Definition object_list.h:60
Callback onRemoved(Callback &&function) noexcept
Installs a function to be called when objects are added / discovered. This function will be called du...
Definition object_list.h:76
Definition object_list.h:47
virtual void addListener(ObjectProviderListener *listener, bool notifyAboutExistingObjects)
Registers an event listener.
friend class ObjectProvider
Definition object_provider.h:128
void throwRuntimeError(const std::string &err)
Throws std::exception that attempts to collect the stack trace. We also wrap it to avoid including st...
Object * getObjectInstance(const ObjectAddition &discovery)
Definition object_provider.h:97
std::vector< ObjectAddition > ObjectAdditionList
Sequence of object additions.
Definition object_provider.h:71
std::vector< ObjectRemoval > ObjectRemovalList
Sequence of object removals.
Definition object_provider.h:74
Definition assert.h:17
STL namespace.