Pool

Represents a pool which takes in a key of type ValueType. If a corresponding EntryType is mapped-to via said key then it is returned, else it is constructed on the spot, stored and then finally returned

If your EntryType is to have its constructor called _without_ taking in a single argument of type ValueType then set the entryTakesValue_onCtor to false. It is true by default.

The result of pooling an EntryType which is a struct-type is that it will have a pointer returned, i.e. an EntryType*

struct Pool (
EntryType
ValueType
bool entryTakesValue_onCtor = true
) if (
isClassType!(EntryType) ||
isStructType!(EntryType)
) {}

Members

Functions

clear
bool clear(ValueType v)

Clears the entry that would be mapped to by the given key

Mixins

__anonymous
mixin Methods!(EntryType)
Undocumented in source.

Mixed In Members

From mixin Methods!(EntryType)

pool
EntryType pool(ValueType v)

Pools a new entry by the given value. If no such pool entry exists then it is constructed, stored and returned, else the existing entry is returned.

pool
EntryType* pool(ValueType v)

Pools a new entry by the given value. If no such pool entry exists then it is constructed, stored and returned, else the existing entry is returned.

Examples

General usage of the pool

Pool!(DNode, int) d;
static assert(__traits(compiles, Pool!(DNode, int)()));


// Initial pool creates the node
DNode p1 = d.pool(1);
assert(p1 !is null);

// Pooling again by said value
// should now return the same
// exact object
DNode p2 = d.pool(1);
assert(p2 is p1);

// Any other value makes an entirely
// new node
DNode p3 = d.pool(2);
assert(p3 !is p2);

// Class-based type without a `this(ValueType)` ctor
static assert(__traits(compiles, Pool!(ClassWithoutSimpleThis, int)()) == false);

// Only class types and struct types are supported
static assert(__traits(compiles, Pool!(int, int)()) == false);

Tests out the clearing of an entry by its key

Pool!(Thing, int) p;
Thing t1 = p.pool(1);
assert(t1);

Thing t2 = p.pool(1);
assert(t2 is t1);

// Clear, now pooling should give different address
p.clear(1);

Thing t3 = p.pool(1);
assert(t3 !is t2);

Tests usage of a type that consumes no ValueType on construction of itself (EntryType has a zero-arity constructor)

static assert(__traits(compiles,  Pool!(ArglessThing, int, false)));

Pool!(ArglessThing, int, false) p;
ArglessThing t1 = p.pool(1);
assert(t1);

ArglessThing t2 = p.pool(1);
assert(t2 is t1);

Tests pooling with an EntryType which is a struct type

// Struct-based types are supported
struct P {}
static assert(__traits(compiles, Pool!(P, int, false)()) == true);

Pool!(Person, int) p;
Person* t1 = p.pool(1);
assert(t1);

Person* t2 = p.pool(1);
assert(t2 is t1);
assert(t2.uniq == 1);

Person* t3 = p.pool(2);
assert(t3);
assert(t3.uniq == 2);
assert(t3 !is t2);

Meta