Function dynCastMove
Dynamic cast for shared pointers if ElementType
is class with D linkage.
{null} dynCastMove(T, Ptr)()
if (isRcPtr!Ptr && !is(Ptr == shared) && !Ptr .isWeak && isClassOrInterface!T && (__traits(getLinkage, T) == "D") && isClassOrInterface!(Ptr .ElementType) && (__traits(getLinkage, Ptr .ElementType) == "D"));
Creates a new instance of RcPtr
whose stored pointer is obtained from ptr
's stored pointer using a dynaic cast expression.
If ptr
is null or dynamic cast fail then result RcPtr
is null.
Otherwise, the new RcPtr
will share ownership with the initial value of ptr
.
Example
static class Foo{
int i;
this(int i)pure nothrow @safe @nogc{
this .i = i;
}
}
static class Bar : Foo{
double d;
this(int i, double d)pure nothrow @safe @nogc{
super(i);
this .d = d;
}
}
static class Zee{
}
{
RcPtr!(const Foo) foo = RcPtr!Bar .make(42, 3.14);
assert(foo .get .i == 42);
auto bar = dynCast!Bar(foo);
assert(bar != null);
assert(bar .get .d == 3.14);
static assert(is(typeof(bar) == RcPtr!(const Bar)));
auto zee = dynCast!Zee(foo);
assert(zee == null);
static assert(is(typeof(zee) == RcPtr!(const Zee)));
}
{
RcPtr!(const Foo) foo = RcPtr!Bar .make(42, 3.14);
assert(foo .get .i == 42);
import core .lifetime : move;
auto bar = dynCast!Bar(foo .move);
assert(bar != null);
assert(bar .get .d == 3.14);
static assert(is(typeof(bar) == RcPtr!(const Bar)));
}
{
RcPtr!(const Foo) foo = RcPtr!Bar .make(42, 3.14);
assert(foo .get .i == 42);
auto bar = dynCastMove!Bar(foo);
assert(foo == null);
assert(bar != null);
assert(bar .get .d == 3.14);
static assert(is(typeof(bar) == RcPtr!(const Bar)));
}