SharedPtr.this - multiple declarations

Function SharedPtr.this

Constructs a SharedPtr without managed object. Same as SharedPtr.init

this(This) (
  typeof(null) nil
) pure nothrow @nogc @safe;

Examples

SharedPtr!long x = null;

assert(x == null);
assert(x == SharedPtr!long.init);

Function SharedPtr.this

Forward constructor (merge move and copy constructor).

this(Rhs, This) (
  auto scope ref Rhs rhs,
  Forward
) @trusted
if (isSmartPtr!Rhs && !is(Rhs == shared));

Function SharedPtr.this

Constructs a SharedPtr which shares ownership of the object managed by rhs and pointing to element.

this(Rhs, Elm, This) (
  auto scope ref Rhs rhs,
  Elm element
) @trusted
if (isSharedPtr!Rhs && is(Elm : GetElementReferenceType!This) && isAliasable!(Rhs, This) && !weakLock!(Rhs, This) && !is(Rhs == shared));

The aliasing constructor: constructs a SharedPtr which shares ownership information with the initial value of rhs, but holds an unrelated and unmanaged pointer ptr. If this SharedPtr is the last of the group to go out of scope, it will call the stored deleter for the object originally managed by rhs. However, calling get() or ptr() on this SharedPtr will always return a copy of element. It is the responsibility of the programmer to make sure that this ptr remains valid as long as this SharedPtr exists, such as in the typical use cases where element is a member of the object managed by rhs or is an alias (e.g., downcast) of rhs.get().

Examples

static struct Foo{
	int i;
	double d;
}
SharedPtr!Foo foo = SharedPtr!Foo.make(42, 3.14);

auto x = SharedPtr!double(foo, &foo.d);
assert(foo.useCount == 2);
assert(foo.get == 3.14);

Function SharedPtr.this

Constructs a SharedPtr which shares ownership of the object managed by rhs.

this(Rhs, This) (
  auto scope ref Rhs rhs
) @trusted
if (isSmartPtr!Rhs && !is(Rhs == shared) && !isMoveCtor!(This, rhs));

If rhs manages no object, this manages no object too. If rhs if rvalue then ownership is moved. The template overload doesn't participate in overload resolution if ElementType of typeof(rhs) is not implicitly convertible to ElementType. If rhs if WeakType then this ctor is equivalent to this(rhs.lock()).

Examples

{
	SharedPtr!long x = SharedPtr!long.make(123);
	assert(x.useCount == 1);

	SharedPtr!long a = x;	      //lvalue copy ctor
	assert(a == x);

	const SharedPtr!long b = x;   //lvalue copy ctor
	assert(b == x);

	SharedPtr!(const long) c = x; //lvalue ctor
	assert(c == x);

	const SharedPtr!long d = b;   //lvalue ctor
	assert(d == x);

	assert(x.useCount == 5);
}

{
	import core.lifetime : move;
	SharedPtr!long x = SharedPtr!long.make(123);
	assert(x.useCount == 1);

	SharedPtr!long a = move(x);        //rvalue copy ctor
	assert(a.useCount == 1);

	const SharedPtr!long b = move(a);  //rvalue copy ctor
	assert(b.useCount == 1);

	SharedPtr!(const long) c = b.load;  //rvalue ctor
	assert(c.useCount == 2);

	const SharedPtr!long d = move(c);  //rvalue ctor
	assert(d.useCount == 2);
}

{
	import core.lifetime : move;
	auto u = UniquePtr!(long, SharedControlBlock).make(123);

	SharedPtr!long s = move(u);        //rvalue copy ctor
	assert(s != null);
	assert(s.useCount == 1);

	SharedPtr!long s2 = UniquePtr!(long, SharedControlBlock).init;
	assert(s2 == null);
}

{
	import core.lifetime : move;
	auto rc = RcPtr!(long).make(123);
	assert(rc.useCount == 1);

	SharedPtr!long s = rc;
	assert(s != null);
	assert(s.useCount == 2);
	assert(rc.useCount == 2);

	SharedPtr!long s2 = RcPtr!(long).init;
	assert(s2 == null);
}