Browse Source

Quick.Threads new class TThreadObjectList

Unknown 7 years ago
parent
commit
1e6b41f3e6
1 changed files with 115 additions and 0 deletions
  1. 115 0
      Quick.Threads.pas

+ 115 - 0
Quick.Threads.pas

@@ -26,12 +26,15 @@
   limitations under the License.
   limitations under the License.
 
 
  *************************************************************************** }
  *************************************************************************** }
+
 unit Quick.Threads;
 unit Quick.Threads;
 
 
 interface
 interface
 
 
 uses
 uses
   Classes,
   Classes,
+  Types,
+  System.RTLConsts,
   System.Generics.Collections,
   System.Generics.Collections,
   System.SyncObjs;
   System.SyncObjs;
 
 
@@ -114,6 +117,26 @@ type
     procedure Start;
     procedure Start;
   end;
   end;
 
 
+  TThreadObjectList<T: class> = class(TList<T>)
+    private
+      fList: TObjectList<T>;
+      fLock: TObject;
+      fDuplicates: TDuplicates;
+      function GetItem(aIndex : Integer) : T;
+      procedure SetItem(aIndex : Integer; aValue : T);
+    public
+      constructor Create(OwnedObjects : Boolean);
+      destructor Destroy; override;
+      property Items[Index : Integer] : T read GetItem write SetItem ; default;
+      procedure Add(const Item: T);
+      procedure Clear;
+      function LockList: TObjectList<T>;
+      procedure Remove(const Item: T); inline;
+      procedure RemoveItem(const Item: T; Direction: TDirection);
+      procedure UnlockList; inline;
+      property Duplicates: TDuplicates read fDuplicates write fDuplicates;
+  end;
+
 implementation
 implementation
 
 
 { TThreadedQueueCS<T> }
 { TThreadedQueueCS<T> }
@@ -384,4 +407,96 @@ begin
   fTaskQueue := TThreadedQueueCS<T>.Create(fMaxQueue,fInsertTimeout,fExtractTimeout);
   fTaskQueue := TThreadedQueueCS<T>.Create(fMaxQueue,fInsertTimeout,fExtractTimeout);
 end;
 end;
 
 
+{ TThreadObjectList<T> }
+
+procedure TThreadObjectList<T>.Add(const Item: T);
+begin
+  LockList;
+  try
+    if (Duplicates = dupAccept) or
+       (fList.IndexOf(Item) = -1) then
+      fList.Add(Item)
+    else if Duplicates = dupError then
+      raise EListError.CreateFmt(SDuplicateItem, [fList.ItemValue(Item)]);
+  finally
+    UnlockList;
+  end;
+end;
+
+procedure TThreadObjectList<T>.Clear;
+begin
+  LockList;
+  try
+    fList.Clear;
+  finally
+    UnlockList;
+  end;
+end;
+
+constructor TThreadObjectList<T>.Create(OwnedObjects : Boolean);
+begin
+  inherited Create;
+  fLock := TObject.Create;
+  fList := TObjectList<T>.Create;
+  fDuplicates := dupIgnore;
+end;
+
+destructor TThreadObjectList<T>.Destroy;
+begin
+  LockList;
+  try
+    fList.Free;
+    inherited Destroy;
+  finally
+    UnlockList;
+    fLock.Free;
+  end;
+end;
+
+function TThreadObjectList<T>.GetItem(aIndex: Integer): T;
+begin
+  LockList;
+  try
+    Result := fList[aIndex];
+  finally
+    UnlockList;
+  end;
+end;
+
+function TThreadObjectList<T>.LockList: TObjectList<T>;
+begin
+  System.TMonitor.Enter(fLock);
+  Result := fList;
+end;
+
+procedure TThreadObjectList<T>.Remove(const Item: T);
+begin
+  RemoveItem(Item, TDirection.FromBeginning);
+end;
+
+procedure TThreadObjectList<T>.RemoveItem(const Item: T; Direction: TDirection);
+begin
+  LockList;
+  try
+    fList.RemoveItem(Item, Direction);
+  finally
+    UnlockList;
+  end;
+end;
+
+procedure TThreadObjectList<T>.SetItem(aIndex: Integer; aValue: T);
+begin
+  LockList;
+  try
+    fList[aIndex] := aValue;
+  finally
+    UnlockList;
+  end;
+end;
+
+procedure TThreadObjectList<T>.UnlockList;
+begin
+  System.TMonitor.Exit(fLock);
+end;
+
 end.
 end.