The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,261 other subscribers

TInterlockedHelper for Delphi interfaces: Spring.Reactive.pas

Posted by jpluimers on 2020/02/25

Reminder so self: [WayBacksglienke / Spring4D / source / Source / Reactive / Spring.Reactive.pas — Bitbucket fragments:

type
  TInterlocked = SyncObjs.TInterlocked;
  TInterlockedHelper = class helper for TInterlocked // TODO: move to Spring.pas
    class function CompareExchange<T: IInterface>(var Target: T; const Value, Comparand: T): T; overload; static;
    class function Exchange<T: IInterface>(var Target: T; const Value: T): T; overload; static;
  end;

{$REGION 'TInterlockedHelper'}

class function TInterlockedHelper.CompareExchange<T>(var Target: T;
  const Value, Comparand: T): T;
begin
  Result := Default(T);
  PPointer(@Result)^ := CompareExchange(PPointer(@Target)^, PPointer(@Value)^, PPointer(@Comparand)^);
  if PPointer(@Result)^ = PPointer(@Comparand)^ then
  begin
    if Assigned(Value) then
      Value._AddRef;
  end
  else
    if Assigned(Result) then
      Result._AddRef;
end;

class function TInterlockedHelper.Exchange<T>(var Target: T;
  const Value: T): T;
begin
  Result := Default(T);
  PPointer(@Result)^ := Exchange(PPointer(@Target)^, PPointer(@Value)^);
  if Assigned(Value) then
    Value._AddRef;
end;

{$ENDREGION}a

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.