ResourceArena.startCycle

Start a new cycle.

Each time a cycle is started, all expired resources are freed, making space for new resources. Resources that are kept alive, are moved to the start of the array, effectively changing their indices. Iterate on the return value to safely update your resources to use the new indices.

struct ResourceArena(T)
startCycle
(
scope void delegate
(
int index
,
ref T resource
)
@safe
moved = null
)

Parameters

moved void delegate
(
int index
,
ref T resource
)
@safe

A function that handles freeing and moving resources. It is called with a pair (index, resource) for every item that was freed or moved. For freed resources, the index is set to -1.

Examples

Example: Maintaining a hash map of resource IDs to indices.

ResourceArena!string arena;
int[string] indices;
indices["One"]   = arena.load("One");
indices["Two"]   = arena.load("Two");
indices["Three"] = arena.load("Three");

void nextCycle() {

    arena.startCycle((newIndex, ref resource) {

        // Freed
        if (newIndex == -1) {
            indices.remove(resource);
        }
        // Moved
        else {
            indices[resource] = newIndex;
        }

    });

}

// Start a cycle and remove "Two" from the arena
nextCycle();
arena.reload(indices["One"], "One");
arena.reload(indices["Three"], "Three");
assert(indices["One"]   == 0);
assert(indices["Two"]   == 1);
assert(indices["Three"] == 2);

// Next cycle the resource should be freed
nextCycle();
assert(indices["One"]   == 0);
assert(indices["Three"] == 1);

See Also

resourceLifetime

Meta