Loading...
Searching...
No Matches
ml::CoreManager< T > Class Template Reference

Static, type-safe collection manager for Core-derived objects. More...

#include <Malena/Core/CoreManager.h>

Inheritance diagram for ml::CoreManager< T >:
[legend]

Public Member Functions

virtual ~CoreManager ()
void addComponent (T &component)
 Register a T object with this manager.
void clear ()
 Remove all registered objects.
const std::vector< T * > & getComponents () const
 Return a read-only view of all currently registered objects.
bool removeComponent (T &component)
 Unregister a T object by reference.
bool removeComponent (T *component)
 Unregister a T object by pointer.

Static Public Member Functions

static void clearPending ()
 Discard all pending operations without executing them.
static bool isBusy ()
 Return true if the manager is currently iterating.
static void processPending ()
 Flush all pending operations immediately.

Static Protected Member Functions

static void beginBusy ()
 Signal that iteration has begun.
static void deferOrExecute (std::function< void()> operation)
 Execute operation now if safe, otherwise queue it.
static void endBusy ()
 Signal that iteration has ended; flush pending operations.

Static Protected Attributes

static int busyDepth
 Iteration nesting depth. Operations are deferred while this is > 0.
static std::vector< std::function< void()> > pendingOperations
 Queue of operations pending until the current iteration completes.

Detailed Description

template<typename T>
class ml::CoreManager< T >

Static, type-safe collection manager for Core-derived objects.

CoreManager<T> holds a per-instance list of T* raw pointers and provides add, remove, and query operations. It inherits DeferredOperationsManager so that removals requested while the collection is being iterated are queued and applied safely once iteration completes — preventing iterator invalidation crashes.

Each CoreManager<T> instance maintains its own independent list. AppManager, Panel, and any other class inheriting CoreManager<Core> each manage their own separate component sets.

Typical use

// Add a component to the managed collection (instance method)
myManager.addComponent(myRect);
// Remove safely from anywhere, including inside an event callback
myManager.removeComponent(myRect);

Safety contract

  • addComponent and removeComponent are safe to call at any time, including from within an event or update callback that is currently iterating the collection.
  • Do not copy or move a T object after it has been registered. The collection stores raw pointers; moving the object invalidates them.
Template Parameters
TA Core-derived type whose instances will be managed.
See also
DeferredOperationsManager, Core, Application

Definition at line 55 of file CoreManager.h.

Constructor & Destructor Documentation

◆ ~CoreManager()

template<typename T>
virtual ml::CoreManager< T >::~CoreManager ( )
virtual

Member Function Documentation

◆ addComponent()

template<typename T>
void ml::CoreManager< T >::addComponent ( T & component)

Register a T object with this manager.

After registration, the object will receive update() and draw() calls each frame (for managers that drive those methods).

Parameters
componentThe object to register. Must outlive its registration.

◆ beginBusy()

void ml::DeferredOperationsManager< CoreManager< T > >::beginBusy ( )
staticprotectedinherited

Signal that iteration has begun.

Increments the busy-depth counter. Multiple nested calls are safe — operations remain deferred until the outermost endBusy() is reached.

◆ clear()

template<typename T>
void ml::CoreManager< T >::clear ( )

Remove all registered objects.

If the collection is currently being iterated, the clear is deferred. After this call (or after the deferred clear executes), getComponents() returns an empty vector.

◆ clearPending()

void ml::DeferredOperationsManager< CoreManager< T > >::clearPending ( )
staticinherited

Discard all pending operations without executing them.

Use during application shutdown or full reset when executing the queued operations would be unsafe (e.g., the objects they reference have already been destroyed).

◆ deferOrExecute()

void ml::DeferredOperationsManager< CoreManager< T > >::deferOrExecute ( std::function< void()> operation)
staticprotectedinherited

Execute operation now if safe, otherwise queue it.

If busyDepth > 0 the operation is appended to pendingOperations and will run when the current iteration completes. If the manager is idle the operation executes immediately.

Parameters
operationThe callable to execute or defer.

◆ endBusy()

void ml::DeferredOperationsManager< CoreManager< T > >::endBusy ( )
staticprotectedinherited

Signal that iteration has ended; flush pending operations.

Decrements the busy-depth counter. When the counter reaches zero, all operations in pendingOperations are executed in FIFO order and the queue is cleared.

◆ getComponents()

template<typename T>
const std::vector< T * > & ml::CoreManager< T >::getComponents ( ) const
nodiscard

Return a read-only view of all currently registered objects.

The returned reference is valid until the next structural modification (add or remove). Do not store the reference across frames.

Returns
Const reference to the internal component pointer vector.

◆ isBusy()

bool ml::DeferredOperationsManager< CoreManager< T > >::isBusy ( )
staticinherited

Return true if the manager is currently iterating.

Can be used by external code that needs to know whether a destructive operation will be deferred.

Returns
true when busyDepth > 0.

◆ processPending()

void ml::DeferredOperationsManager< CoreManager< T > >::processPending ( )
staticinherited

Flush all pending operations immediately.

Normally called automatically by endBusy(). Provided as a public escape hatch for unusual teardown sequences.

Warning
Calling this while isBusy() is true can cause iterator invalidation in the currently running loop. Only call manually when you are certain it is safe.

◆ removeComponent() [1/2]

template<typename T>
bool ml::CoreManager< T >::removeComponent ( T & component)

Unregister a T object by reference.

Safe to call from inside an event or update callback. If the collection is currently being iterated, the removal is deferred until iteration completes.

Parameters
componentThe object to remove.
Returns
true if the object was found and removed (or queued for removal); false if it was not registered.

◆ removeComponent() [2/2]

template<typename T>
bool ml::CoreManager< T >::removeComponent ( T * component)

Unregister a T object by pointer.

Equivalent to the reference overload. Passing nullptr is safe and returns false.

Parameters
componentPointer to the object to remove.
Returns
true if found and removed (or queued); false otherwise.

Member Data Documentation

◆ busyDepth

int ml::DeferredOperationsManager< CoreManager< T > >::busyDepth
inlinestaticprotectedinherited

Iteration nesting depth. Operations are deferred while this is > 0.

Definition at line 70 of file DeferredOperationsManager.h.

◆ pendingOperations

std::vector<std::function<void()> > ml::DeferredOperationsManager< CoreManager< T > >::pendingOperations
inlinestaticprotectedinherited

Queue of operations pending until the current iteration completes.

Definition at line 73 of file DeferredOperationsManager.h.


The documentation for this class was generated from the following file: