Function RcPtr.compareExchangeStrong

Compares the RcPtr pointers pointed-to by this and expected.

bool compareExchangeStrong(MemoryOrder success = MemoryOrder.seq, MemoryOrder failure = success, E, D, This) (
  scope ref E expected,
  scope D desired
) scope
if (isRcPtr!E && !is(E == shared) && isRcPtr!D && !is(D == shared) && isMoveAssignable!(D, This) && isCopyAssignable!(This, E) && (This.isWeak == D.isWeak) && (This.isWeak == E.isWeak));

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.

Examples

static foreach(enum bool weak; [true, false]){
    //fail
    {
        RcPtr!long a = RcPtr!long.make(123);
        RcPtr!long b = RcPtr!long.make(42);
        RcPtr!long c = RcPtr!long.make(666);

        static if(weak)a.compareExchangeWeak(b, c);
        else a.compareExchangeStrong(b, c);

        assert(*a == 123);
        assert(*b == 123);
        assert(*c == 666);

    }

    //success
    {
        RcPtr!long a = RcPtr!long.make(123);
        RcPtr!long b = a;
        RcPtr!long c = RcPtr!long.make(666);

        static if(weak)a.compareExchangeWeak(b, c);
        else a.compareExchangeStrong(b, c);

        assert(*a == 666);
        assert(*b == 123);
        assert(*c == 666);
    }

    //shared fail
    {
        shared RcPtr!(shared long) a = RcPtr!(shared long).make(123);
        RcPtr!(shared long) b = RcPtr!(shared long).make(42);
        RcPtr!(shared long) c = RcPtr!(shared long).make(666);

        static if(weak)a.compareExchangeWeak(b, c);
        else a.compareExchangeStrong(b, c);

        auto tmp = a.exchange(null);
        assert(*tmp == 123);
        assert(*b == 123);
        assert(*c == 666);
    }

    //shared success
    {
        RcPtr!(shared long) b = RcPtr!(shared long).make(123);
        shared RcPtr!(shared long) a = b;
        RcPtr!(shared long) c = RcPtr!(shared long).make(666);

        static if(weak)a.compareExchangeWeak(b, c);
        else a.compareExchangeStrong(b, c);

        auto tmp = a.exchange(null);
        assert(*tmp == 666);
        assert(*b == 123);
        assert(*c == 666);
    }
}