mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-14 17:29:33 +02:00
Fix for Mantis #30453: add Delphi compatible TThreadList<>
git-svn-id: trunk@34278 -
This commit is contained in:
parent
5e120982e5
commit
7678f5ecf0
@ -230,6 +230,25 @@ type
|
||||
property Items[Index: SizeInt]: T read GetItem write SetItem; default;
|
||||
end;
|
||||
|
||||
TThreadList<T> = class
|
||||
private
|
||||
FList: TList<T>;
|
||||
FDuplicates: TDuplicates;
|
||||
FLock: TRTLCriticalSection;
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
|
||||
procedure Add(constref AValue: T);
|
||||
procedure Remove(constref AValue: T);
|
||||
procedure Clear;
|
||||
|
||||
function LockList: TList<T>;
|
||||
procedure UnlockList; inline;
|
||||
|
||||
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
|
||||
end;
|
||||
|
||||
TQueue<T> = class(TCustomList<T>)
|
||||
protected
|
||||
// bug #24287 - workaround for generics type name conflict (Identifier not found)
|
||||
@ -980,6 +999,72 @@ begin
|
||||
Result := TArrayHelperBugHack.BinarySearch(FItems, AItem, AIndex, AComparer);
|
||||
end;
|
||||
|
||||
{ TThreadList<T> }
|
||||
|
||||
constructor TThreadList<T>.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
FDuplicates:=dupIgnore;
|
||||
InitCriticalSection(FLock);
|
||||
FList := TList<T>.Create;
|
||||
end;
|
||||
|
||||
destructor TThreadList<T>.Destroy;
|
||||
begin
|
||||
LockList;
|
||||
try
|
||||
FList.Free;
|
||||
inherited Destroy;
|
||||
finally
|
||||
UnlockList;
|
||||
DoneCriticalSection(FLock);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TThreadList<T>.Add(constref AValue: T);
|
||||
begin
|
||||
LockList;
|
||||
try
|
||||
if (Duplicates = dupAccept) or (FList.IndexOf(AValue) = -1) then
|
||||
FList.Add(AValue)
|
||||
else if Duplicates = dupError then
|
||||
raise EArgumentException.CreateRes(@SDuplicatesNotAllowed);
|
||||
finally
|
||||
UnlockList;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TThreadList<T>.Remove(constref AValue: T);
|
||||
begin
|
||||
LockList;
|
||||
try
|
||||
FList.Remove(AValue);
|
||||
finally
|
||||
UnlockList;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TThreadList<T>.Clear;
|
||||
begin
|
||||
LockList;
|
||||
try
|
||||
FList.Clear;
|
||||
finally
|
||||
UnlockList;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TThreadList<T>.LockList: TList<T>;
|
||||
begin
|
||||
Result:=FList;
|
||||
System.EnterCriticalSection(FLock);
|
||||
end;
|
||||
|
||||
procedure TThreadList<T>.UnlockList;
|
||||
begin
|
||||
System.LeaveCriticalSection(FLock);
|
||||
end;
|
||||
|
||||
{ TQueue<T>.TEnumerator }
|
||||
|
||||
constructor TQueue<T>.TEnumerator.Create(AQueue: TQueue<T>);
|
||||
|
Loading…
Reference in New Issue
Block a user