tiOPF (delphi @ www.tiopf.com のオブジェクト永続性フレームワーク) で使用する汎用リスト クラスを作成しようとしています。具体的には、既存のジェネリック クラス (TtiObjectList) を取得して、TtiObject の子孫を使用するジェネリック バージョンを作成しようとしています。
D7 - D2009 および Free Pascal でコンパイルする必要があるため、基本クラスを変更する範囲は限られています。既存の永続化メカニズムを機能させ続けるには、TtiObjectList から派生する必要があります。
// base class
type
TtiObjectList = class(TtiObject)
...
protected
function GetItems(i: integer): TtiObject; virtual;
procedure SetItems(i: integer; const AValue: TtiObject); virtual;
...
public
function Add(const AObject : TtiObject): integer; overload; virtual;
...
end;
私のクラスは次のように定義されています。
TtiGenericObjectList<T: TtiObject> = class(TtiObjectList)
protected
function GetItems(i:integer): T; reintroduce;
procedure SetItems(i:integer; const Value: T); reintroduce;
public
function Add(const AObject: T): integer; reintroduce;
property Items[i:integer]: T read GetItems write SetItems; default;
end;
implementation
{ TtiGenericObjectList<T> }
function TtiGenericObjectList<T>.Add(const AObject: T): integer;
var obj: TtiObject;
begin
obj:= TtiObject(AObject); /// Invalid typecast
result:= inherited Add(obj);
end;
// alternate add, also fails
function TtiGenericObjectList<T>.Add(const AObject: T): integer;
begin
result:= inherited Add(AObject); /// **There is no overloaded version**
/// **of 'Add' that can be called with these arguments**
end;
function TtiGenericObjectList<T>.GetItems(i: integer): T;
begin
result:= T(inherited GetItems(i)); /// **Invalid typecast **
end;
procedure TtiGenericObjectList<T>.SetItems(i: integer; const Value: T);
begin
inherited SetItems(i, Value);
end;
私が抱えている問題は、デルファイが T を TtiObject の子孫として認識していないことです。次のようなことをすると、無効な型キャスト エラーが発生します。
function TtiGenericObjectList<T>.Add(const AObject: T): integer;
var obj: TtiObject;
begin
obj:= TtiObject(AObject); /// **Invalid typecast***
result:= inherited Add(obj);
end;
型キャストを行わないと、上記のリストに示されているように、代わりにオーバーロード エラーが発生します。
私が間違っているアイデアはありますか?
ショーン