unit List;
interface
uses
System.SysUtils,
Generics.Collections;
const
SID_MAIN = '
{BCA44697-4841-457B-8161-30A9BDC2A0A0}';
SID_CHILD1 = '
{C8527C3A-EB3C-46C1-94BB-3C9DFB12AF92}';
SID_CHILD2 = '
{A21F5DED-9BEC-4D25-BB7B-21AD3C46FB75}';
SID_CHILD3 = '
{8AE48CFE-2DDA-40C9-901A-660974B5DDA1}';
type
IMainInterface =
interface(IUnknown)
[SID_MAIN]
end;
IChildInterface1 =
interface(IUnknown)
[SID_CHILD1]
end;
IChildInterface2 =
interface(IUnknown)
[SID_CHILD2]
end;
IChildInterface3 =
interface(IUnknown)
[SID_CHILD3]
end;
TMyList =
class(TObject)
private
FInterfaceList : TDictionary<TGUID, IMainInterface>;
function GetInterfaceGUID(
const AInterfaceInstance : IMainInterface) : TGUID;
public
constructor Create;
destructor Destroy;
override;
function Add(
const AInterfaceInstance : IMainInterface) : Boolean;
function Remove(
const AType : TGUID;
out AInterfaceInstance : IMainInterface) : Boolean;
end;
implementation
function TMyList.GetInterfaceGUID(
const AInterfaceInstance : IMainInterface) : TGUID;
begin
Result := IMainInterface;
// wie geht das hier besser? Irgendwas mit GetTypeInfo von der RTTI oder sowas?
if Supports(AInterfaceInstance, IChildInterface1)
then
begin
Result := IChildInterface1;
end
else if Supports(AInterfaceInstance, IChildInterface2)
then
begin
Result := IChildInterface2;
end
else if Supports(AInterfaceInstance, IChildInterface3)
then
begin
Result := IChildInterface3;
end;
end;
constructor TMyList.Create;
begin
inherited Create;
FInterfaceList := TDictionary<TGUID, IMainInterface>.Create;
end;
destructor TMyList.Destroy;
begin
FInterfaceList.Free;
inherited Destroy;
end;
function TMyList.Remove(
const AType : TGUID;
out AInterfaceInstance : IMainInterface) : Boolean;
begin
Result := FInterfaceList.TryGetValue(AType, AInterfaceInstance);
if Result
then
begin
FInterfaceList.Remove(AType);
end;
end;
function TMyList.Add(
const AInterfaceInstance : IMainInterface) : Boolean;
begin
Result :=
not FInterfaceList.ContainsValue(AInterfaceInstance);
if Result
then
begin
FInterfaceList.Add(GetInterfaceGUID(AInterfaceInstance), AInterfaceInstance);
end;
end;
end.