8#ifndef SEN_CORE_OBJ_OBJECT_LIST_H
9#define SEN_CORE_OBJ_OBJECT_LIST_H
78 return std::exchange(onRemoved_, std::move(function));
83 [[nodiscard]]
const std::list<T*>&
getObjects() const noexcept {
return typedObjects_; }
86 [[nodiscard]]
const std::list<std::shared_ptr<Object>>&
getUntypedObjects() const noexcept {
return untypedObjects_; }
93 [[nodiscard]] T* getCastedObject(
Object*
object);
98 typename TypedObjectList::const_iterator typed;
99 typename UntypedObjectList::const_iterator untyped;
105 std::unordered_map<ObjectId, IteratorPair> iteratorMap_;
119 auto previous = std::exchange(onAdded_, std::move(function));
121 if (onAdded_ && !untypedObjects_.empty())
123 onAdded_({typedObjects_.begin(), typedObjects_.end(), untypedObjects_.begin(), untypedObjects_.end()});
130inline T* ObjectList<T>::getCastedObject(
Object*
object)
132 if constexpr (!std::is_same_v<T, Object>)
134 if (
object ==
nullptr)
139 auto* castedObject =
dynamic_cast<T*
>(object);
140 if (castedObject ==
nullptr)
143 err.append(
"error casting object named '");
145 err.append(
"' of class '");
146 err.append(object->
getClass()->getQualifiedName());
147 err.append(
"' to a native type '");
148 err.append(
typeid(T).name());
149 err.append(
"' within an ObjectList");
167 for (
const auto& addition: additions)
171 if (instance ==
nullptr)
176 auto castedObject = getCastedObject(instance);
177 auto lastTyped = typedObjects_.insert(typedObjects_.end(), castedObject);
178 auto lastUntyped = untypedObjects_.insert(untypedObjects_.end(), instance->shared_from_this());
188 iteratorMap_.insert({instance->getId(), {lastTyped, lastUntyped}});
198 iterators.
typedEnd = typedObjects_.end();
210 for (
const auto& removal: removals)
212 auto itr = iteratorMap_.find(removal.objectid);
213 if (itr != iteratorMap_.end())
215 auto castedObject = *(itr->second.typed);
216 auto nonCastedObject = *(itr->second.untyped);
218 untypedObjects_.erase(itr->second.untyped);
219 typedObjects_.erase(itr->second.typed);
220 iteratorMap_.erase(itr);
224 typedRemovals.push_back(castedObject);
225 untypedRemovals.push_back(nonCastedObject);
234 iterators.
typedEnd = typedRemovals.end();
238 onRemoved_(iterators);
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:205
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:162
~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.
ObjectProviderListener()=default
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