如何将泛型用于两个不同的类,但其中一个来自VCL

时间:2018-11-24 23:22:15

标签: delphi

我正在维护和扩展可与8位位图一起使用的旧版应用程序,但出于几个原因,我从头开始做了自己的位图类(TMyBitmap),实现了一些明显的功能,例如Width,Height和ScanLine属性。请注意,TBitmap和TMyBitmap之间没有关系(TMyBitmap不会从TBitmap继承)。

现在,我想使用多种方法来创建实用程序类,以使用标准TBitmap或我自己的TMyBitmap。对于TBitmap或TMyBitmap,这些方法的实现相同,因为它们仅使用“公共”属性(Width,Height和ScanLine),因此泛型看起来像是合适的解决方案:

TBmpUtility<T> = class
strict private
  FBmp: T;
public
  constructor Create(Bmp: T);
  procedure Fill(Y: Integer; Code: Byte); //example of an utility method
end;

procedure TBmpUtility<T>.Fill(Y: Integer; Code: Byte);
var
  i: Integer;
  Line: PByteArray;
begin
  Line := Self.FBmp.ScanLine[Y];
  for i := 0 to Self.FBmp.Width - 1 do
    Line[i] := Code;
end;

这里明显的问题是,我无法真正调用Self.FBmp.ScanLine[Y]Self.FBmp.Width,因为此时编译器对TBitmap或TMyBitmap一无所知。然后我声明了一个接口,并在TMyBitmap上实现了它:

  IBitmap  = interface
  ['{029D42A0-132F-4C6E-BE72-648E1361C0A4}']
    function GetWidth: Integer;
    procedure SetWidth(Value: Integer);
    function GetHeight: Integer;
    procedure SetHeight(Value: Integer);
    function GetScanLine(Y: Integer): Pointer;
    property Width: Integer read GetWidth write SetWidth;
    property Height: Integer read GetHeight write SetHeight;
    property ScanLine[Y: Integer]: Pointer read GetScanLine;
  end;

  TMyBitmap = class (TSinglentonImplementation, IBitmap)
  ...
  end;

然后,我可以在泛型声明上添加类型约束,以便能够通过实用工具方法实现中的Self.FBmp调用这些方法。

TBmpUtility<T: IBitmap> = class

这里的问题是:标准TBitmap没有实现我的IBitmap接口。因此,我无法将实用程序类与如下的标准位图一起使用:

Bmp := TBitmap.Create();
BmpUtil := TBmpUtility<TBitmap>.Create(Bmp);

我可以做这样的事情:

TCustomBitmap = class (TBitmap, IBitmap)
...
end;

但是项目代码在各处都使用TBitmap,而不是TCustomBitmap,所以我不能互换这些类型,因此这不是一个选择。

我尝试了一个辅助类。像这样的东西:

TBitmapHelper = class (IBitmap) helper for TBitmap
...
end;

但显然,它不起作用

有什么办法解决这种情况吗?从一开始就记住目标:只有一个实现代码块,用于实用程序方法同时处理TBitmap和TMyBitmap。如果泛型是正确的解决方案,则应如下所示:

Bmp := TBitmap.Create();
BmpUtil := TBmpUtility<TBitmap>.Create(Bmp);
BmpUtil.Fill(10, 0);
//or
MyBmp := TMyBitmap.Create();
BmpUtil := TBmpUtility<TMyBitmap>.Create(MyBmp);
BmpUtil.Fill(10, 0);

(仅供参考:我正在使用Delphi 10.2.3 [Tokyo])

1 个答案:

答案 0 :(得分:3)

按照David的建议,您可以创建一个适配器类。此类可以实现您的接口,并在大多数情况下充当对实际位图类的简单传递。用于TBitmap的一个可能看起来像这样(为简洁起见,省略了一些方法):

type
  TBitmapAdapter = class(TInterfacedObject, IBitmap)
  private
    FBitmap: TBitmap;
    function GetWidth: Integer;
    procedure SetWidth(Value: Integer);
    // And everything else from IBitmap
  public
    constructor Create(ABitmap: TBitmap);
  end;

function TBitmapAdapter.GetWidth: Integer;
begin
  Result := FBitmap.Width;
end;

procedure TBitmapAdapter.SetWidth(Value: Integer);
begin
  FBitmap.Width := Value;
end;

您可以创建一个同时处理两种类型的类,但这将需要在每个方法中使用ifs。我将创建两个单独的类,因此您只需创建一个TBitmapAdapter或TMyBitmapAdapter。

您可以简单地构造该类并将其作为接口传递。如果您的TBmpUtility接受一个I​​Bitmap,则可以这样构造它:

TBmpUtility.Create(TBitmapAdapter.Create(Self))

您确实可以为TBitmap和TMyBitmap编写类帮助程序,以为您创建正确的包装器。再次是TBitmap版本:

type
  TBitmapHelper = class helper for TBitmap
    function AsIBitmap: IBitmap;
  end;

function TBitmapHelper.AsIBitmap: IBitmap;
begin
  Result := TBitmapAdapter.Create(Self);
end;

然后您可以像这样简单地使用它:

TBmpUtility.Create(Bitmap.AsIBitmap)

或者只是将其传递给实用程序类的方法,您可以拥有一个可以处理任何位图的实用程序实例:

BmpUtility.Fill(Bitmap.AsIBitmap);

可以临时存储对IBitmap的引用,以防止在每次调用时创建和销毁适配器:

var b: IBitmap;
begin
  b := Bitmap.AsIBitmap;
  BmpUtility.SetHeight(b, 100);
  BmpUtility.SetWidth(b, 100);
  BmpUtility.Fill(b);
end;

Bitmap.AsIBitmap的结果是适配器类的IBitmap接口,但是使用它的代码甚至都不知道,并且接口被重新计数,因此一旦超出范围,适配器将被释放。