2017-06-15 54 views
1

Howdey,德尔福 - 无法投TVirtualInterface到虚拟接口

我使用TVirtualInterface实现一些接口的基本接口。这些interfaes代表可以在数据库中找到的Keys。我使用定制的代码生成器生成接口定义。例如:

// Base code 

IKey = interface 
    function KeyFields : string; 
    function KeyValues : Variant; 
    function GetKeyValue(const aKeyName : string) : Variant; 
    procedure SetKeyValue(const aKeyName : string; Value : Variant); 
end; 

// Generated code 

ITable1Key = interface(IKey) 
end; 

ITable1Key1 = interface(ITable1Key) 
    procedure SetField1(const Value : string); 
    function GetField1 : string; 
    property Field1 : string read GetField1 write SetField1; 
end; 

ITable1Key2 = interface(ITable1Key) 
    procedure SetField1(const Value : string); 
    function GetField1 : string; 
    property Field1 : string read GetField1 write SetField1; 
    procedure SetField2(const Value : string); 
    function GetField2 : string; 
    property Field2 : string read GetField1 write SetField1; 
end; 

// Other generated declarations 

我使用TVirtualInterface来实现每个IKey接口,而不是逐个实现它们。

虽然,在我TVirtualInterface:

TKey = TVirtualInterface 
public 
    constructor Create(aType : PTypeInfo); 
    function Cast : IKey; 
end; 

TKey<T : IKey> 
public 
    constructor Create; reintroduce; 
    function Cast : T; 
end; 


constructor TKey.Create(aType : PTypeInfo) 
begin 
    inherited Create(aType, aHandlerMethod); 
end; 

function TKey.Cast; 
var 
    pInfo: PTypeInfo; 
begin 
    pInfo := TypeInfo(IKey); 
    if QueryInterface(GetTypeData(pInfo).Guid, Result) <> 0 then 
    begin 
    raise Exception.CreateFmt('Sorry, TKey is unable to cast %s to its interface ', [string(pInfo.Name)]); 
    end; 
end; 

constructor TKey<T>.Create; 
begin 
    inherited Create(TypeInfo(T)); 
end; 

function TKey<T>.Cast; 
var 
    pInfo: PTypeInfo; 
begin 
    pInfo := TypeInfo(T); 
    if QueryInterface(GetTypeData(pInfo).Guid, Result) <> 0 then 
    begin 
    raise Exception.CreateFmt('Sorry, TKey<T> is unable to cast %s to its interface ', [string(pInfo.Name)]); 
    end; 
end; 

我没有问题,铸造TKEY的虚拟接口使用TKey.Cast方法的T型,虽然TKey.Cast返回不支持错误接口。

我System.Rtti检查,这不是工作,我想它的方式部分:

function TVirtualInterface.QueryInterface(const IID: TGUID; out Obj): HResult; 
begin 
    if iid = FIID then 
    begin 
    _AddRef; 
    Pointer(Obj) := @VTable; 
    Result := S_OK; 
    end 
    else 
    Result := inherited 
end; 

现在,我怎么能强迫TVirtualInterface将自己转换为IID作为父FIID字段的界面?我是否必须为IKey界面创建另一个TVirtualInterface实例?

非常感谢。

回答

2

您错用了TVirtualInterface。它只是一个RTTI帮手,你根本不应该从中得到它。您应该从TInterfacedObject取而代之。

另外,两个TKey类都忽略传递给构造函数的PTypeInfo。非通用TKey.Cast()始终只查询IKey,从来没有后代接口。而通用TKey<T>.Cast总是重新查询T的RTTI以获取其IID。所以在构造函数中摆脱PTypeInfo,这是浪费。

由于非通用TKey仅仅是一个基类,实际上并不在所有实施任何派生接口,TKey.QueryInterface()总会失败比IKey本身以外的任何接口。至少通用TKey可以查询派生接口。

您的Cast功能无论如何都是多余的,因为您可以使用as运算符或SysUtils.Supports()函数将一个接口转换为另一个接口。这些是首选方法,不要手动使用QueryInterface()

在任何情况下,您的接口在其声明中缺少IID,因此无论如何您都不能在接口之间进行转换。

尝试更多的东西是这样的:

// Base code 

IKey = interface 
    ['{D6D212E0-C173-468C-8267-962CFC3FECF5}'] 
    function KeyFields : string; 
    function KeyValues : Variant; 
    function GetKeyValue(const aKeyName : string) : Variant; 
    procedure SetKeyValue(const aKeyName : string; Value : Variant); 
end; 

// Generated code 

ITable1Key = interface(IKey) 
    ['{B8E44C43-7248-442C-AE1B-6B9E426372C1}'] 
end; 

ITable1Key1 = interface(ITable1Key) 
    ['{0C86ECAA-A8E7-49EB-834F-77DE62BE1D28}'] 
    procedure SetField1(const Value : string); 
    function GetField1 : string; 
    property Field1 : string read GetField1 write SetField1; 
end; 

ITable1Key2 = interface(ITable1Key) 
    ['{82226DE9-221C-4268-B971-CD72617C19C7}'] 
    procedure SetField1(const Value : string); 
    function GetField1 : string; 
    property Field1 : string read GetField1 write SetField1; 
    procedure SetField2(const Value : string); 
    function GetField2 : string; 
    property Field2 : string read GetField1 write SetField1; 
end; 

// Other generated declarations 

type 
    TKey = class(TInterfacedObject, IKey) 
    public 
    function Cast : IKey; 
    // IKey methods... 
    end; 

    TKey<T : IKey> = class(TInterfacedObject, IKey, T) 
    public 
    function Cast : T; 
    end; 

    TTable1Key = class(TKey, IKey, ITable1Key) 
    end; 

    TTable1Key1 = class(TTable1Key, IKey, ITable1Key, ITable1Key1) 
    public 
    // ITable1Key1 methods... 
    end; 

    TTable1Key2 = class(TTable1Key, IKey, ITable1Key, ITable1Key2) 
    public 
    // Table1Key2 methods... 
    end; 

// and so on ... 

function TKey.Cast: IKey; 
begin 
    if not Supports(Self, IKey, Result) then 
    raise Exception.Create('Sorry, unable to cast to IKey'); 
end; 

function TKey<T>.Cast: T; 
begin 
    if not Supports(Self, GetTypeData(TypeInfo(T)).Guid, Result) then 
    raise Exception.CreateFmt('Sorry, unable to cast to %s', [string(TypeInfo(T).Name)]); 
end; 

// other class methods as needed ... 

还要注意派生类怎么也得重复由其基类实现的接口。这是一个已知的德尔福限制。派生类不继承基类接口。每个类都必须明确指定它实现的接口,即使实际实现在基类中。

+1

这是“限制”一个新问题吗?在Delphi中,你总是必须声明继承接口的实现,其中一个类直接实现派生接口和任何基接口,但接口实现是(或曾经是)从该基类自身声明的基类继承接口的实现。唯一的问题是如果基类实现了接口的方法而没有实际声明接口本身的实现。除非我的记忆力正在玩技巧(我目前没有德尔福交手测试)。 – Deltics

+0

@Deltics:“*这个”限制“是一个新问题吗?*” - 不,它一直在那里。接口方法的实现可以像预期的那样通过普通多态性从基类继承到派生类,但是必须在每个派生类声明中指定接口以便出现在每个类的内部接口表中,所以'TObject.GetInterface() (通过扩展,'IInterface.QueryInterface()',''as''和'as'运算符,'SysUtils。Supports()'等)可以看到它。不理想,C++没有这个限制。 –