The OpenD Programming Language

createRC

template createRC(T)
createRC
(
Args...
)
(
auto ref Args args
)
if (
!is(T == interface) &&
!__traits(isAbstractClass, T)
)

Members

Functions

createRC
mir_rcptr!T createRC(Args args)

Examples

auto a = createRC!double(10);
auto b = a;
assert(*b == 10);
*b = 100;
assert(*a == 100);

Classes with empty constructor

static class C
{
    int index = 34;

    override size_t toHash() const scope @safe pure nothrow @nogc
    {
        return index;
    }
}
assert(createRC!C.index == 34);
static interface I {
    ref double bar() @safe pure nothrow @nogc;
    size_t toHash() @trusted scope const pure nothrow @nogc;
}
static abstract class D
{
    int index;

    override size_t toHash() const scope @safe pure nothrow @nogc
    {
        return index;
    }
}
static class C : D, I
{
    double value;
    ref double bar() @safe pure nothrow @nogc { return value; }
    this(double d){ value = d; }

    override size_t toHash() const scope @safe pure nothrow @nogc
    {
        return hashOf(value, super.toHash);
    }
}
auto a = createRC!C(10);
assert(a._counter == 1);
auto b = a;
assert(a._counter == 2);
assert((*b).value == 10);
b.value = 100; // access via alias this syntax
assert(a.value == 100);
assert(a._counter == 2);

auto d = a.castTo!D; //RCPtr!D
assert(d._counter == 3);
d.index = 234;
assert(a.index == 234);
auto i = a.castTo!I; //RCPtr!I
assert(i.bar == 100);
assert(i._counter == 4);

auto v = a.shareMember!"value"; //RCPtr!double
auto w = a.shareMember!"bar"; //RCPtr!double
assert(i._counter == 6);
assert(*v == 100);
()@trusted{assert(&*w is &*v);}();

'Alias This' support

struct S
{
    double e;
}
struct C
{
    int i;
    S s;
    // 'alias' should be accesable by reference
    // or a class/interface
    alias s this;
}

auto a = createRC!C(10, S(3));
auto s = a.castTo!S; // RCPtr!S
assert(s._counter == 2);
assert(s.e == 3);

Meta