TInterlocked.Exchange for interfaces?

TInterlocked.Exchange for interfaces?

Since there is no System.SyncObjs.TInterlocked.Exchange overload for interfaces (and the Exchange version is stupidly enough constrained to class - thanks copy paste from C# where the class constraint means any reference type!) I was wondering how to properly implement it for IInterface.

Is this correct?

class function TInterlockedHelper.Exchange(var Target: IInterface;
 const Value: IInterface): IInterface;
begin
 Result := IInterface(Exchange(Pointer(Target), Pointer(Value)));
 if Value <> nil then
  Value._AddRef;
 if Result <> nil then
  Result._Release;
end;

Comments