Browse Source

Add Exchange<T> that works like std::exchange in C++.

Rika Ichinose 1 year ago
parent
commit
05b7ebd34e
2 changed files with 7 additions and 0 deletions
  1. 1 0
      rtl/objpas/sysutils/sysutilh.inc
  2. 6 0
      rtl/objpas/sysutils/sysutils.inc

+ 1 - 0
rtl/objpas/sysutils/sysutilh.inc

@@ -297,6 +297,7 @@ type
 
 {$IFNDEF VER3_0}
 generic function IfThen<T>(val:boolean;const iftrue:T; const iffalse:T) :T; inline; overload;
+generic function Exchange<T>(var target:T; const newvalue:T) :T; inline;
 {$ENDIF}
 
 Var

+ 6 - 0
rtl/objpas/sysutils/sysutils.inc

@@ -878,6 +878,12 @@ begin
   else
     Result:=ifFalse;
 end;
+
+generic function Exchange<T>(var target:T; const newvalue:T) :T;
+begin
+  Result := target;
+  target := newvalue;
+end;
 {$ENDIF}
 
 Function ArrayOfConstToStrArray(Args: array of const) : TUTF8StringDynArray;