Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.rc.ptr

Thread-safe reference-counted shared pointers

.
struct mir_rcptr(T);
Thread safe reference counting array.
__xdtor if any is used to destruct objects.
The implementation never adds roots into the GC.
template opUnary(string op : "*")
ditto
inout @property scope inout(T) _get_value();
pure nothrow @nogc @safe void proxySwap(ref typeof(this) rhs);
auto _shareMember(string member, Args...)(auto ref Args args);
@trusted .mir_rcptr!R _shareAs(R)()
if (isImplicitlyConvertible!(T, R));

const @trusted .mir_rcptr!(const(R)) _shareAs(R)()
if (isImplicitlyConvertible!(const(T), const(R)));

immutable @trusted .mir_rcptr!(immutable(R)) _shareAs(R)()
if (isImplicitlyConvertible!(immutable(T), immutable(R)));
Construct a shared pointer of a required type with a current context. Provides polymorphism abilities for classes and structures with alias this syntax.
const @system .mir_rcptr!R _withContext(R)(return R value) return
if (is(R == class) || is(R == interface));

const @system .mir_rcptr!R _withContext(R)(return ref R value) return
if (!is(R == class) && !is(R == interface));
Returns:
shared pointer constructed with current context.
alias RCPtr = mir_rcptr(T);
template createRC(T)
Examples:
auto a = createRC!double(10);
auto b = a;
assert(*b == 10);
*b = 100;
assert(*a == 100);
Examples:
static interface I { ref double bar() @safe pure nothrow @nogc; }
static abstract class D { int index; }
static class C : D, I
{
    double value;
    ref double bar() @safe pure nothrow @nogc { return value; }
    this(double d) { value = d; }
}
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);

auto d = a._shareAs!D; //RCPtr!D
import std.stdio;
assert(d._counter == 3);
d.index = 234;
assert(a.index == 234);
auto i = a._shareAs!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);}();
Examples:
'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._shareAs!S; // RCPtr!S
assert(s._counter == 2);
assert(s.e == 3);
mir_rcptr!T createRC(Args...)(auto ref Args args);