Thank you Albert
Yep, I saw, now I have a new version using that, and is working fine:
PInterfaceItem = ^TInterfaceItem;
TInterfaceItem = record
Item: IInterface;
Name1: PChar;
Name2: PChar;
end;
TNamedInterfaceList = class(TInterfacedObject, INamedInterfaceList)
private
FDoubleNamed: boolean;
FCaseSensitive: Boolean;
FHashList1: TStringHash;
FHashList2: TStringHash;
FItems: TList;
FLock: TCriticalSection;
function GetCaseSensitive: Boolean;
function GetCount: Integer;
function GetItem(const Name: string): IInterface;
function GetItems(const Index: Integer): IInterface;
procedure SetCaseSensitive(const Value: Boolean);
procedure SetItem(const Name: string; const Value: IInterface);
procedure SetItems(const Index: Integer; const Value: IInterface);
procedure ReHash;
protected
function Add(const Name: string; const Item: IInterface): Integer;
overload;
function Add(const Name1, Name2: string; const Item: IInterface):
Integer; overload;
function IndexOf(const Item: IInterface): Integer; overload;
function IndexOf(const Name: string; const Name2: boolean = False):
Integer; overload;
procedure Exchange(const Index1, Index2: Integer);
procedure Clear;
procedure Delete(const Index: Integer); overload;
procedure Delete(const Name: string; const Name2: boolean = False);
overload;
public
constructor Create(const DoubleNamed: boolean = False; const
HashSize: Integer = 389);
destructor Destroy; override;
procedure Lock;
procedure Unlock;
property CaseSensitive: Boolean read GetCaseSensitive write
SetCaseSensitive;
property Count: Integer read GetCount;
property Item[const Name: string]: IInterface read GetItem write
SetItem;
property Items[const Index: Integer]: IInterface read GetItems write
SetItems; default;
end;
Albert Zeyer escreveu:
I don't think so.
You have another problem with your current implementation: The
reference counting of your interfaces will not work. You have to call
manually the _AddRef and _Release of the interface after adding them
to the list and removing them. You also can't use FList.Clear, because
then the reference counting doen't work either.
Am Samstag, den 09.09.2006, 20:10 -0300 schrieb Cesar Romero:
Can someone suggest a better way to do that?
Now Im using:
THashedInterfaceList = class(TInterfacedObject, IHashedInterfaceList)
private
FList: THashedStringList;
protected
function Add(const Name: string; const Item: IInterface): Integer;
function GetCount: Integer;
function GetItem(const Name: string): IInterface;
function GetItems(const Index: Integer): IInterface;
function IndexOf(const Name: string): Integer;
function IndexOfItem(const Item: IInterface): Integer;
procedure Clear;
procedure Delete(const Index: Integer);
property Item[const Name: string]: IInterface read GetItem;
property Items[const Index: Integer]: IInterface read GetItems; default;
property Count: Integer read GetCount;
public
constructor Create(const SortList: boolean = False); virtual;
destructor Destroy; override;
end;
function THashedInterfaceList.Add(const Name: string;
const Item: IInterface): Integer;
begin
Result:= FList.AddObject(Name, Pointer(Item));
end;
procedure THashedInterfaceList.Clear;
begin
FList.Clear;
end;
constructor THashedInterfaceList.Create(const SortList: boolean);
begin
inherited Create;
FList:= THashedStringList.Create;
FList.Duplicates:= dupError;
FList.Sorted:= SortList;
end;
procedure THashedInterfaceList.Delete(const Index: Integer);
begin
FList.Delete(Index);
end;
destructor THashedInterfaceList.Destroy;
begin
FList.Free;
inherited;
end;
function THashedInterfaceList.GetCount: Integer;
begin
Result:= FList.Count;
end;
function THashedInterfaceList.GetItem(const Name: string): IInterface;
begin
Result:= Items[IndexOf(Name)];
end;
function THashedInterfaceList.GetItems(const Index: Integer): IInterface;
var
LItem: Pointer;
begin
if (Index <> NotFound) and (Index < Count) then
LItem:= FList.Objects[Index]
else
LItem:= nil;
Result:= IInterface(LItem);
end;
function THashedInterfaceList.IndexOf(const Name: string): Integer;
begin
Result:= FList.IndexOf(Name);
end;
function THashedInterfaceList.IndexOfItem(const Item: IInterface): Integer;
begin
Result:= FList.IndexOfObject(Pointer(Item));
end;
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives