1 static foreach(enum bool weak; [true, false]){ 2 //fail 3 { 4 RcPtr!long a = RcPtr!long.make(123); 5 RcPtr!long b = RcPtr!long.make(42); 6 RcPtr!long c = RcPtr!long.make(666); 7 8 static if(weak)a.compareExchangeWeak(b, c); 9 else a.compareExchangeStrong(b, c); 10 11 assert(*a == 123); 12 assert(*b == 123); 13 assert(*c == 666); 14 15 } 16 17 //success 18 { 19 RcPtr!long a = RcPtr!long.make(123); 20 RcPtr!long b = a; 21 RcPtr!long c = RcPtr!long.make(666); 22 23 static if(weak)a.compareExchangeWeak(b, c); 24 else a.compareExchangeStrong(b, c); 25 26 assert(*a == 666); 27 assert(*b == 123); 28 assert(*c == 666); 29 } 30 31 //shared fail 32 { 33 shared RcPtr!(shared long) a = RcPtr!(shared long).make(123); 34 RcPtr!(shared long) b = RcPtr!(shared long).make(42); 35 RcPtr!(shared long) c = RcPtr!(shared long).make(666); 36 37 static if(weak)a.compareExchangeWeak(b, c); 38 else a.compareExchangeStrong(b, c); 39 40 auto tmp = a.exchange(null); 41 assert(*tmp == 123); 42 assert(*b == 123); 43 assert(*c == 666); 44 } 45 46 //shared success 47 { 48 RcPtr!(shared long) b = RcPtr!(shared long).make(123); 49 shared RcPtr!(shared long) a = b; 50 RcPtr!(shared long) c = RcPtr!(shared long).make(666); 51 52 static if(weak)a.compareExchangeWeak(b, c); 53 else a.compareExchangeStrong(b, c); 54 55 auto tmp = a.exchange(null); 56 assert(*tmp == 666); 57 assert(*b == 123); 58 assert(*c == 666); 59 } 60 }
Compares the RcPtr pointers pointed-to by this and expected.
If they are equivalent (store the same pointer value, and either share ownership of the same object or are both empty), assigns desired into this using the memory ordering constraints specified by success and returns true. If they are not equivalent, assigns this into expected using the memory ordering constraints specified by failure and returns false.
More info in c++ std::atomic<std::shared_ptr>.