SharedPtr.opAssign - multiple declarations
Function SharedPtr.opAssign
Releases the ownership of the managed object, if any.
void opAssign(MemoryOrder order = MemoryOrder .seq, This)
(
typeof(null) nil
) scope
if (isMutable!This);
After the call, this manages no object.
Examples
{
SharedPtr!long x = SharedPtr!long .make(1);
assert(x .useCount == 1);
x = null;
assert(x .useCount == 0);
assert(x == null);
}
{
SharedPtr!(shared long) x = SharedPtr!(shared long) .make(1);
assert(x .useCount == 1);
x = null;
assert(x .useCount == 0);
assert(x == null);
}
{
shared SharedPtr!(long) x = SharedPtr!(shared long) .make(1);
assert(x .useCount == 1);
x = null;
assert(x .useCount == 0);
assert(x == null);
}
Function SharedPtr.opAssign
Shares ownership of the object managed by rhs
.
void opAssign(MemoryOrder order = MemoryOrder .seq, Rhs, This)
(
auto scope ref Rhs desired
) scope
if (isSmartPtr!Rhs && !is(Rhs == shared));
If rhs
manages no object, this
manages no object too.
If rhs
is rvalue then move-assigns a SharedPtr
from rhs
Examples
{
SharedPtr!long px1 = SharedPtr!long .make(1);
SharedPtr!long px2 = SharedPtr!long .make(2);
assert(px2 .useCount == 1);
px1 = px2;
assert(*px1 == 2);
assert(px2 .useCount == 2);
}
{
SharedPtr!long px = SharedPtr!long .make(1);
SharedPtr!(const long) pcx = SharedPtr!long .make(2);
assert(px .useCount == 1);
pcx = px;
assert(*pcx == 1);
assert(pcx .useCount == 2);
}
{
const SharedPtr!long cpx = SharedPtr!long .make(1);
SharedPtr!(const long) pcx = SharedPtr!long .make(2);
assert(pcx .useCount == 1);
pcx = cpx;
assert(*pcx == 1);
assert(pcx .useCount == 2);
}
{
SharedPtr!(immutable long) pix = SharedPtr!(immutable long) .make(123);
SharedPtr!(const long) pcx = SharedPtr!long .make(2);
assert(pix .useCount == 1);
pcx = pix;
assert(*pcx == 123);
assert(pcx .useCount == 2);
}
import btl .autoptr .rc_ptr;
{
SharedPtr!long sp = SharedPtr!long .make(10);
RcPtr!long rc = RcPtr!long .make(20);
assert(sp .useCount == 1);
assert(rc .useCount == 1);
sp = rc;
assert(sp .get == 20);
assert(rc .useCount == 2);
}
import btl .autoptr .intrusive_ptr;
{
static struct Foo{
private SharedControlBlock control;
int i;
this(int i)pure nothrow @safe @nogc{
this .i = i;
}
}
SharedPtr!Foo sp = SharedPtr!Foo .make(10);
IntrusivePtr!Foo ip = IntrusivePtr!Foo .make(20);
assert(sp .useCount == 1);
assert(ip .useCount == 1);
sp = ip;
assert(sp .get .i== 20);
assert(ip .useCount == 2);
}