The OpenD Programming Language

Unique

A unique pointer similar to C++'s std::unique_ptr.

Constructors

this
this(Args args)

The allocator is global, so no need to pass it in to the constructor

this
this(Allocator allocator, Args args)

Non-singleton allocator, must be passed in

this
this(Unique!(T, Allocator) other)

Destructor

~this
~this()

Postblit

this(this)
this(this)

Members

Functions

allocator
Allocator allocator()
borrow
auto borrow()

Borrow the owned pointer. Can be @safe with DIP1000 and if used in a scope fashion.

opAssign
void opAssign(Unique!(T, Allocator) other)

Move from another smart pointer

opCast
bool opCast()

"Truthiness" cast

release
Pointer release()

release ownership

unique
Unique unique()

Releases ownership and transfers it to the returned Unique object.

Static functions

construct
typeof(this) construct(Args args)

Factory method so can construct with zero args.

construct
typeof(this) construct(Allocator allocator, Args args)

Factory method. Not necessary with non-global allocator but included for symmetry.

Examples

struct S {
    private ulong zeroArgsCtorTest = 3;
}
auto s = Unique!S.construct();
static assert(is(typeof(s) == Unique!S));
assert(s._object !is null);
assert(s.zeroArgsCtorTest == 3);
import std.experimental.allocator: dispose;
import core.exception: AssertError;

try {
    auto allocator = TestAllocator();
    auto ptr = Unique!(Struct, TestAllocator*)(&allocator, 42);
    ptr.release;
    assert(Struct.numStructs == 1);
} catch(AssertError e) { // TestAllocator should throw due to memory leak
    version(unitThreadedLight) {}
    else
        "Memory leak in TestAllocator".should.be in e.msg;
    return;
}

assert(0); // should throw above

Meta