8#ifndef SEN_CORE_OBJ_OBJECT_LIST_H
9#define SEN_CORE_OBJ_OBJECT_LIST_H
55 [[nodiscard]]
typename TypedObjectList::iterator
begin()
const {
return typedBegin; }
56 [[nodiscard]]
typename TypedObjectList::iterator
end()
const {
return typedEnd; }
89 return std::exchange(onRemoved_, std::move(function));
94 [[nodiscard]]
const std::list<T*>&
getObjects() const noexcept {
return typedObjects_; }
97 [[nodiscard]]
const std::list<std::shared_ptr<Object>>&
getUntypedObjects() const noexcept {
return untypedObjects_; }
104 [[nodiscard]] T* getCastedObject(
Object*
object);
109 typename TypedObjectList::const_iterator typed;
110 typename UntypedObjectList::const_iterator untyped;
116 std::unordered_map<ObjectId, IteratorPair> iteratorMap_;
130 auto previous = std::exchange(onAdded_, std::move(function));
132 if (onAdded_ && !untypedObjects_.empty())
134 onAdded_({typedObjects_.begin(), typedObjects_.end(), untypedObjects_.begin(), untypedObjects_.end()});
141inline T* ObjectList<T>::getCastedObject(
Object*
object)
143 if constexpr (!std::is_same_v<T, Object>)
145 auto* castedObject =
dynamic_cast<T*
>(object);
146 if (castedObject ==
nullptr)
149 err.append(
"error casting object named '");
151 err.append(
"' of class '");
152 err.append(object->
getClass()->getQualifiedName());
153 err.append(
"' to a native type '");
154 err.append(
typeid(T).name());
155 err.append(
"' within an ObjectList");
173 for (
const auto& addition: additions)
177 if (instance ==
nullptr)
182 auto castedObject = getCastedObject(instance);
183 auto lastTyped = typedObjects_.insert(typedObjects_.end(), castedObject);
184 auto lastUntyped = untypedObjects_.insert(untypedObjects_.end(), instance->shared_from_this());
194 iteratorMap_.insert({instance->getId(), {lastTyped, lastUntyped}});
204 iterators.
typedEnd = typedObjects_.end();
216 for (
const auto& removal: removals)
218 auto itr = iteratorMap_.find(removal.objectid);
219 if (itr != iteratorMap_.end())
221 auto castedObject = *(itr->second.typed);
222 auto nonCastedObject = *(itr->second.untyped);
224 untypedObjects_.erase(itr->second.untyped);
225 typedObjects_.erase(itr->second.typed);
226 iteratorMap_.erase(itr);
230 typedRemovals.push_back(castedObject);
231 untypedRemovals.push_back(nonCastedObject);
236 if (onRemoved_ && !typedRemovals.empty())
240 iterators.
typedEnd = typedRemovals.end();
244 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.
std::list< T * > TypedObjectList
Definition object_list.h:44
SearchMode
How to search for objects.
Definition object_list.h:39
@ ignoreSubClasses
Definition object_list.h:41
@ includeSubClasses
Definition object_list.h:40
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:128
const std::list< T * > & getObjects() const noexcept
Gets the list of currently-registered objects.
Definition object_list.h:94
static constexpr std::size_t defaultListSizeHint
Definition object_list.h:34
ObjectList(ObjectProvider &provider, std::size_t sizeHint)
Automatically adds itself as a listener to the provider.
Definition object_list.h:74
std::list< std::shared_ptr< Object > > UntypedObjectList
Definition object_list.h:45
std::function< void(const Iterators &iterators)> Callback
Definition object_list.h:65
void onObjectsRemoved(const ObjectRemovalList &removals) override
Called when objects will be removed from a source.
Definition object_list.h:211
const std::list< std::shared_ptr< Object > > & getUntypedObjects() const noexcept
Gets the list of currently-registered objects.
Definition object_list.h:97
void onObjectsAdded(const ObjectAdditionList &additions) override
Called when objects are been added to a source.
Definition object_list.h:168
~ObjectList() override=default
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:71
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:87
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
IteratorRange< IteratorType > makeRange(IteratorType begin, IteratorType end)
Create a simple range for the two iterators.
Definition iterator_adapters.h:52
Definition object_list.h:48
TypedObjectList::iterator typedBegin
Definition object_list.h:49
UntypedObjectList::iterator untypedEnd
Definition object_list.h:52
auto untyped() const
Range-for support iterates over untyped objects.
Definition object_list.h:59
TypedObjectList::iterator begin() const
Range-for support iterates over typed objects.
Definition object_list.h:55
UntypedObjectList::iterator untypedBegin
Definition object_list.h:51
TypedObjectList::iterator end() const
Definition object_list.h:56
TypedObjectList::iterator typedEnd
Definition object_list.h:50
auto typed() const
Range-for support iterates over untype objects (here for simmetry with untyped()).
Definition object_list.h:62