IntrusivePtr.lock

Creates a new non weak IntrusivePtr that shares ownership of the managed object (must be IntrusivePtr.WeakType).

If there is no managed object, i.e. this is empty or this is expired, then the returned IntrusivePtr is empty. Method exists only if IntrusivePtr is isWeak

struct IntrusivePtr
scope
lock
()
()
if (
isCopyConstructable!(typeof(this), SharedType)
)

Examples

static struct Foo{
    ControlBlock!(int, int) c;
    int i;

    this(int i)pure nothrow @safe @nogc{
        this.i = i;
    }
}

{
    IntrusivePtr!Foo x = IntrusivePtr!Foo.make(123);

    auto w = x.weak;    //weak ptr

    IntrusivePtr!Foo y = w.lock;

    assert(x == y);
    assert(x.useCount == 2);
    assert(y.get.i == 123);
}

{
    IntrusivePtr!Foo x = IntrusivePtr!Foo.make(123);

    auto w = x.weak;    //weak ptr

    assert(w.expired == false);

    x = IntrusivePtr!Foo.make(321);

    assert(w.expired == true);

    IntrusivePtr!Foo y = w.lock;

    assert(y == null);
}

{
    shared IntrusivePtr!(shared Foo) x = IntrusivePtr!(shared Foo).make(123);

    shared IntrusivePtr!(shared Foo).WeakType w = x.load.weak;    //weak ptr

    assert(w.expired == false);

    x = IntrusivePtr!(shared Foo).make(321);

    assert(w.expired == true);

    IntrusivePtr!(shared Foo) y = w.load.lock;

    assert(y == null);
}

Meta