ReusableBlock

A reusable memory block for placing objects on.

The elements are added only at the back either by copying or by moving. They are removed only from the front. The elements are never moved to a different memory location inside the buffer.

Constructors

this
this(ubyte[] buffer)

Construct an object that will use the provided memory

Members

Functions

capacity
size_t capacity()

Total capacity of the block

clear
void clear()

The same as calling this.removeFrontN(this.length)

copyBack
void copyBack(const(SourceT) element)

Calls ~= to copy the specified _element

emplaceBack
void emplaceBack(Args args)

Construct a new element after the existing elements

empty
bool empty()

Whether the block has no elements at all

freeCapacity
size_t freeCapacity()

Number of elements the block currently has room for

length
size_t length()

Current number of elements in the block

moveEmplace
void moveEmplace(SourceT element)

Move the specified _element to the back of existing elements

opDollar
size_t opDollar()

Number of elements in the block

opIndex
inout(T) opIndex(size_t index)

Return a reference to an element

opOpAssign
void opOpAssign(SourceT element)

Add an _element after the existing elements; lvalues are copied, rvalues are moved.

opSlice
inout(T)[] opSlice(size_t from, size_t to)

A slice providing access _to the specified elements

opSlice
inout(T)[] opSlice()

A slice to all elements; the same as [0..$]

ptr
inout(T)* ptr()

Pointer to the beginning of the block

removeFrontN
void removeFrontN(size_t n)

Remove elements from the head of the block. If n == length, freeCapacity will be equal to capacity after this call. Whether the destructors are called on the removed elements is determined by the dtors template parameter.

toString
void toString(void delegate(in char[]) sink)

String representation of this object mostly for debugging

Parameters

T

the type of the elements that will be stored on the block

dtors

whether to execute destructors of elements upon removing them

Examples

// Constructing a block on a piece of memory
ubyte[100] buffer;
auto b = ReusableBlock!int(buffer);

// Depending on the alignment of the elements, the capacity may be less than
// the requested amount
assert(b.capacity <= buffer.length / int.sizeof);

// Add 2 elements
b ~= 0;
b ~= 1;

b.length.shouldBe(2);
b[0].shouldBe(0);
b[1].shouldBe(1);
b.freeCapacity.shouldBe(b.capacity - 2);

// Remove the first one
b.removeFrontN(1);
b.length.shouldBe(1);
b[0].shouldBe(1);
// Note that free capacity does not increase:
b.freeCapacity.shouldBe(b.capacity - 2);

// Remove all elements and reset the block
b.clear();
b.length.shouldBe(0);
// This time all capacity is free
b.freeCapacity.shouldBe(b.capacity);

Meta