RefCountedTrusted

Adapted RefCounted with a @trusted destructor.

Destructor

~this
~this()

Can be @trusted as long as this type is used correctly.

Postblit

A postblit is present on this object, but not explicitly documented in the source.

Members

Properties

refCountedPayload
T refCountedPayload [@property getter]
inout(T) refCountedPayload [@property getter]

These are the only ways to access the refcounted payload (but the store MUST be initialized)

refCountedStore
inout(RefCountedStore) refCountedStore [@property getter]

Accesses the refcounted store, usually to check if (or ensure that) it is initialized.

Structs

RefCountedStore
struct RefCountedStore

Protects a pointer to some reference counted payload.

Examples

static uint nDestroyed = 0;
struct Resource {
    string name;
    this(string name) { this.name = name; }
    @disable this(this);
    ~this() { debug ++nDestroyed; }
}

{
    auto rc1 = RefCountedTrusted!Resource("thing");
    assert(rc1.refCountedStore.isInitialized);
    rc1.refCountedStore.ensureInitialized();

    {
        auto rc2 = rc1;
        RefCountedTrusted!Resource rc3;
        rc3 = rc2;
        () @trusted { assert(rc3.refCountedPayload.name == "thing"); }();
    }

    // ~rc2 and ~rc3 didn't destroy the resource: there's still one ref
    debug assert(nDestroyed == 0);
}

// now the last reference is removed and the resource is destroyed
debug assert(nDestroyed == 1);

Meta

Version

XXX: See related bug reports and pull requests.