1 static struct Foo{ 2 ControlBlock!(int, int) c; 3 int i; 4 5 this(int i)pure nothrow @safe @nogc{ 6 this.i = i; 7 } 8 } 9 10 //lvalue exchange 11 { 12 shared x = IntrusivePtr!(shared Foo).make(123); 13 auto y = IntrusivePtr!(shared Foo).make(42); 14 15 auto z = x.exchange(y); 16 17 assert(x.load.get.i == 42); 18 assert(y.get.i == 42); 19 assert(z.get.i == 123); 20 } 21 22 //rvalue exchange 23 { 24 shared x = IntrusivePtr!(shared Foo).make(123); 25 auto y = IntrusivePtr!(shared Foo).make(42); 26 27 import core.lifetime : move; 28 auto z = x.exchange(move(y)); 29 30 assert(x.load.get.i == 42); 31 assert(y == null); 32 assert(z.get.i == 123); 33 } 34 35 //null exchange (same as move) 36 { 37 shared x = IntrusivePtr!(shared Foo).make(123); 38 39 auto z = x.exchange(null); 40 41 assert(x.load == null); 42 assert(z.get.i == 123); 43 } 44 45 //swap: 46 { 47 shared x = IntrusivePtr!(shared Foo).make(123); 48 auto y = IntrusivePtr!(shared Foo).make(42); 49 50 //opAssign is same as store 51 import core.lifetime : move; 52 y = x.exchange(move(y)); 53 54 assert(x.load.get.i == 42); 55 assert(y.get.i == 123); 56 }
Stores the non shared IntrusivePtr pointer ptr in the shared(IntrusivePtr) pointed to by this and returns the value formerly pointed-to by this, atomically or with mutex.