CRTP base that gives a manager safe deferred-operation support. More...
#include <DeferredOperationsManager.h>
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 = 0 |
| 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. | |
CRTP base that gives a manager safe deferred-operation support.
Several Malena managers own collections that are iterated during event firing, update ticks, or draw calls. If user code (e.g., a click callback) tries to remove an element from the collection while it is being iterated, the result is undefined behavior — typically a crash.
DeferredOperationsManager solves this with a simple pattern:
beginBusy() / endBusy().deferOrExecute().endBusy() decrements the depth counter to zero, all queued operations are flushed automatically.Because the pattern uses CRTP, each derived manager gets its own independent static busyDepth counter and pendingOperations queue. EventsManager, MessageManager, CoreManager, and PluginManager all inherit from this class for exactly that reason.
Nested calls are fully supported: beginBusy() increments the depth and endBusy() only flushes when the depth returns to zero.
| Derived | The concrete manager class (CRTP parameter). |
Definition at line 65 of file DeferredOperationsManager.h.
|
staticprotected |
Signal that iteration has begun.
Increments the busy-depth counter. Multiple nested calls are safe — operations remain deferred until the outermost endBusy() is reached.
|
static |
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).
|
staticprotected |
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.
| operation | The callable to execute or defer. |
|
staticprotected |
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.
|
static |
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.
true when busyDepth > 0.
|
static |
Flush all pending operations immediately.
Normally called automatically by endBusy(). Provided as a public escape hatch for unusual teardown sequences.
isBusy() is true can cause iterator invalidation in the currently running loop. Only call manually when you are certain it is safe.
|
inlinestaticprotected |
Iteration nesting depth. Operations are deferred while this is > 0.
Definition at line 69 of file DeferredOperationsManager.h.
|
inlinestaticprotected |
Queue of operations pending until the current iteration completes.
Definition at line 72 of file DeferredOperationsManager.h.