Function SharedPtr.exchange

Stores the non shared SharedPtr pointer ptr in the shared(SharedPtr) pointed to by this and returns the value formerly pointed-to by this, atomically or with mutex.

SharedPtr exchange(MemoryOrder order = MemoryOrder.seq, This) (
  typeof(null)
) scope
if (isMutable!This);

SharedPtr exchange(MemoryOrder order = MemoryOrder.seq, Rhs, This) (
  scope Rhs ptr
) scope
if (isSharedPtr!Rhs && isMoveAssignable!(Rhs, This) && !is(Rhs == shared));

Examples

//lvalue exchange
{
	shared x = SharedPtr!(shared long).make(123);
	auto y = SharedPtr!(shared long).make(42);

	auto z = x.exchange(y);

	assert(x.load.get == 42);
	assert(y.get == 42);
	assert(z.get == 123);
}

//rvalue exchange
{
	shared x = SharedPtr!(shared long).make(123);
	auto y = SharedPtr!(shared long).make(42);

	import core.lifetime : move;
	auto z = x.exchange(move(y));

	assert(x.load.get == 42);
	assert(y == null);
	assert(z.get == 123);
}

//null exchange (same as move)
{
	shared x = SharedPtr!(shared long).make(123);

	auto z = x.exchange(null);

	assert(x.load == null);
	assert(z.get == 123);
}

//swap:
{
	shared x = SharedPtr!(shared long).make(123);
	auto y = SharedPtr!(shared long).make(42);

	//opAssign is same as store
	import core.lifetime : move;
	y = x.exchange(move(y));

	assert(x.load.get == 42);
	assert(y.get == 123);
}