Comparing records with operators

2019-07-25 12:46发布

问题:

I've created a record which encapsulates scenarios of version numbers in an integer array and is interchangeable as a string...

type
  TVersion = record
    Values: array of Integer;
    function Count: Integer;
    class operator implicit(aVersion: TVersion): String;
    class operator implicit(aVersion: String): TVersion;
  end;

class operator TVersion.implicit(aVersion: TVersion): String;
var
  X: Integer;
begin
  Result:= '';
  for X := 0 to Length(aVersion.Values)-1 do begin
    if X > 0 then Result:= Result + '.';
    Result:= Result + IntToStr(aVersion.Values[X]);
  end;
end;

class operator TVersion.implicit(aVersion: String): TVersion;
var
  S, T: String;
  I: Integer;
begin
  S:= aVersion + '.';
  SetLength(Result.Values, 0);
  while Length(S) > 0 do begin
    I:= Pos('.', S);
    T:= Copy(S, 1, I-1);
    Delete(S, 1, I);
    SetLength(Result.Values, Length(Result.Values)+1);
    Result.Values[Length(Result.Values)-1]:= StrToIntDef(T, 0);
  end;
end;

function TVersion.Count: Integer;
begin
  Result:= Length(Values);
end;

Usage:

var
  V: TVersion;
begin
  V:= '1.2.3.4';
  V.Values[3]:= 400;
  ShowMessage(V);
  SetLength(V.Values, 2);
  V.Values[0]:= 7;
  V.Values[1]:= 0;
  ShowMessage(V);
end;

What I would like to do is be able to compare these versions using standard operators, for example...

if V1 > V2 then ShowMessage('Version 1 is newer than version 2')
else if V1 < V2 then ShowMessage('Version 1 is older than version 2')
else if V1 = V2 then SHowMessage('Version 1 and version 2 are the same');

How can I do this, if possible?

回答1:

You have to implement some comparison operators like LessThan ('<'), Equal etc. Full list of overloadable operators is here.

Simple example of Equal implementation (without cases like 1.2.0 = 1.2)

class operator TVersion.Equal(a, b: TVersion): Boolean;
var
  la, lb: Integer;
begin
  la := Length(a.Values);
  lb := Length(b.Values);
  if la <> lb then
    Result := False
  else if la = 0 then
    Result := True
  else
    Result := CompareMem(a.Values, b.Values, la * SizeOf(Integer));
end;

About comparing two arrays of various sizes (not tested):

class operator TVersion.LessThan(a, b: TVersion): Boolean;
var
  la, lb, i: Integer;
begin
  la := Length(a.Values);
  lb := Length(b.Values);
  for i := 0 to Min(la, lb) - 1 do
    if a.Values[i] < b.Values[i] then   // 1.2.xxx < 1.3.xxx
      Exit(True)
    else if a.Values[i] > b.Values[i] then //1.3.xxx > 1.2.xxx
      Exit(False);

  //common parts are equal, check a rest
  //cases 1.2.3.xxx vs 1.2.3 or 1.2.3.xxx vs 1.2.3
  //and empty cases too
  Result := la < lb;
end;