home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Source / Vcl / contnrs.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  10KB  |  415 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       Borland Delphi Visual Component Library         }
  4. {                                                       }
  5. {       Copyright (c) 1995,99 Inprise Corporation       }
  6. {                                                       }
  7. {*******************************************************}
  8.  
  9. unit Contnrs;
  10.  
  11. interface
  12.  
  13. uses
  14.   SysUtils, Classes;
  15.  
  16. type
  17.  
  18. { TObjectList class }
  19.  
  20.   TObjectList = class(TList)
  21.   private
  22.     FOwnsObjects: Boolean;
  23.   protected
  24.     procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  25.     function GetItem(Index: Integer): TObject;
  26.     procedure SetItem(Index: Integer; AObject: TObject);
  27.   public
  28.     constructor Create; overload;
  29.     constructor Create(AOwnsObjects: Boolean); overload;
  30.  
  31.     function Add(AObject: TObject): Integer;
  32.     function Remove(AObject: TObject): Integer;
  33.     function IndexOf(AObject: TObject): Integer;
  34.     function FindInstanceOf(AClass: TClass; AExact: Boolean = True; AStartAt: Integer = 0): Integer;
  35.     procedure Insert(Index: Integer; AObject: TObject);
  36.     property OwnsObjects: Boolean read FOwnsObjects write FOwnsObjects;
  37.     property Items[Index: Integer]: TObject read GetItem write SetItem; default;
  38.   end;
  39.  
  40. { TComponentList class }
  41.  
  42.   TComponentList = class(TObjectList)
  43.   private
  44.     FNexus: TComponent;
  45.   protected
  46.     procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  47.     function GetItems(Index: Integer): TComponent;
  48.     procedure SetItems(Index: Integer; AComponent: TComponent);
  49.     procedure HandleFreeNotify(Sender: TObject; AComponent: TComponent);
  50.   public
  51.     destructor Destroy; override;
  52.  
  53.     function Add(AComponent: TComponent): Integer;
  54.     function Remove(AComponent: TComponent): Integer;
  55.     function IndexOf(AComponent: TComponent): Integer;
  56.     procedure Insert(Index: Integer; AComponent: TComponent);
  57.     property Items[Index: Integer]: TComponent read GetItems write SetItems; default;
  58.   end;
  59.  
  60. { TClassList class }
  61.  
  62.   TClassList = class(TList)
  63.   protected
  64.     function GetItems(Index: Integer): TClass;
  65.     procedure SetItems(Index: Integer; AClass: TClass);
  66.   public
  67.     function Add(aClass: TClass): Integer;
  68.     function Remove(aClass: TClass): Integer;
  69.     function IndexOf(aClass: TClass): Integer;
  70.     procedure Insert(Index: Integer; aClass: TClass);
  71.     property Items[Index: Integer]: TClass read GetItems write SetItems; default;
  72.   end;
  73.  
  74. { TOrdered class }
  75.  
  76.   TOrderedList = class(TObject)
  77.   private
  78.     FList: TList;
  79.   protected
  80.     procedure PushItem(AItem: Pointer); virtual; abstract;
  81.     function PopItem: Pointer; virtual;
  82.     function PeekItem: Pointer; virtual;
  83.     property List: TList read FList;
  84.   public
  85.     constructor Create;
  86.     destructor Destroy; override;
  87.  
  88.     function Count: Integer;
  89.     function AtLeast(ACount: Integer): Boolean;
  90.     procedure Push(AItem: Pointer);
  91.     function Pop: Pointer;
  92.     function Peek: Pointer;
  93.   end;
  94.  
  95. { TStack class }
  96.  
  97.   TStack = class(TOrderedList)
  98.   protected
  99.     procedure PushItem(AItem: Pointer); override;
  100.   end;
  101.  
  102. { TObjectStack class }
  103.  
  104.   TObjectStack = class(TStack)
  105.   public
  106.     procedure Push(AObject: TObject);
  107.     function Pop: TObject;
  108.     function Peek: TObject;
  109.   end;
  110.  
  111. { TQueue class }
  112.  
  113.   TQueue = class(TOrderedList)
  114.   protected
  115.     procedure PushItem(AItem: Pointer); override;
  116.   end;
  117.  
  118. { TObjectQueue class }
  119.  
  120.   TObjectQueue = class(TQueue)
  121.   public
  122.     procedure Push(AObject: TObject);
  123.     function Pop: TObject;
  124.     function Peek: TObject;
  125.   end;
  126.  
  127. implementation
  128.  
  129. { TObjectList }
  130.  
  131. function TObjectList.Add(AObject: TObject): Integer;
  132. begin
  133.   Result := inherited Add(AObject);
  134. end;
  135.  
  136. constructor TObjectList.Create;
  137. begin
  138.   inherited Create;
  139.   FOwnsObjects := True;
  140. end;
  141.  
  142. constructor TObjectList.Create(AOwnsObjects: Boolean);
  143. begin
  144.   inherited Create;
  145.   FOwnsObjects := AOwnsObjects;
  146. end;
  147.  
  148. function TObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean;
  149.   AStartAt: Integer): Integer;
  150. var
  151.   I: Integer;
  152. begin
  153.   Result := -1;
  154.   for I := AStartAt to Count - 1 do
  155.     if (AExact and
  156.         (Items[I].ClassType = AClass)) or
  157.        (not AExact and
  158.         Items[I].InheritsFrom(AClass)) then
  159.     begin
  160.       Result := I;
  161.       break;
  162.     end;
  163. end;
  164.  
  165. function TObjectList.GetItem(Index: Integer): TObject;
  166. begin
  167.   Result := inherited Items[Index];
  168. end;
  169.  
  170. function TObjectList.IndexOf(AObject: TObject): Integer;
  171. begin
  172.   Result := inherited IndexOf(AObject);
  173. end;
  174.  
  175. procedure TObjectList.Insert(Index: Integer; AObject: TObject);
  176. begin
  177.   inherited Insert(Index, AObject);
  178. end;
  179.  
  180. procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
  181. begin
  182.   if OwnsObjects then
  183.     if Action = lnDeleted then
  184.       TObject(Ptr).Free;
  185.   inherited Notify(Ptr, Action);
  186. end;
  187.  
  188. function TObjectList.Remove(AObject: TObject): Integer;
  189. begin
  190.   Result := inherited Remove(AObject);
  191. end;
  192.  
  193. procedure TObjectList.SetItem(Index: Integer; AObject: TObject);
  194. begin
  195.   inherited Items[Index] := AObject;
  196. end;
  197.  
  198.  
  199. { TComponentListNexus }
  200. { used by TComponentList to get free notification }
  201.  
  202. type
  203.   TComponentListNexusEvent = procedure(Sender: TObject; AComponent: TComponent) of object;
  204.   TComponentListNexus = class(TComponent)
  205.   private
  206.     FOnFreeNotify: TComponentListNexusEvent;
  207.   protected
  208.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  209.   public
  210.     property OnFreeNotify: TComponentListNexusEvent read FOnFreeNotify write FOnFreeNotify;
  211.   end;
  212.  
  213. { TComponentListNexus }
  214.  
  215. procedure TComponentListNexus.Notification(AComponent: TComponent; Operation: TOperation);
  216. begin
  217.   if (Operation = opRemove) and Assigned(FOnFreeNotify) then
  218.     FOnFreeNotify(Self, AComponent);
  219.   inherited Notification(AComponent, Operation);
  220. end;
  221.  
  222. { TComponentList }
  223.  
  224. function TComponentList.Add(AComponent: TComponent): Integer;
  225. begin
  226.   Result := inherited Add(AComponent);
  227. end;
  228.  
  229. destructor TComponentList.Destroy;
  230. begin
  231.   FNexus.Free;
  232.   inherited Destroy;
  233. end;
  234.  
  235. function TComponentList.GetItems(Index: Integer): TComponent;
  236. begin
  237.   Result := TComponent(inherited Items[Index]);
  238. end;
  239.  
  240. procedure TComponentList.HandleFreeNotify(Sender: TObject; AComponent: TComponent);
  241. begin
  242.   Extract(AComponent);
  243. end;
  244.  
  245. function TComponentList.IndexOf(AComponent: TComponent): Integer;
  246. begin
  247.   Result := inherited IndexOf(AComponent);
  248. end;
  249.  
  250. procedure TComponentList.Insert(Index: Integer; AComponent: TComponent);
  251. begin
  252.   inherited Insert(Index, AComponent);
  253. end;
  254.  
  255. procedure TComponentList.Notify(Ptr: Pointer; Action: TListNotification);
  256. begin
  257.   if not Assigned(FNexus) then
  258.   begin
  259.     FNexus := TComponentListNexus.Create(nil);
  260.     TComponentListNexus(FNexus).OnFreeNotify := HandleFreeNotify;
  261.   end;
  262.   case Action of
  263.     lnAdded:
  264.       if Ptr <> nil then
  265.         TComponent(Ptr).FreeNotification(FNexus);
  266.     lnExtracted,
  267.     lnDeleted:
  268.       if Ptr <> nil then
  269.         TComponent(Ptr).RemoveFreeNotification(FNexus);
  270.   end;
  271.   inherited Notify(Ptr, Action);
  272. end;
  273.  
  274. function TComponentList.Remove(AComponent: TComponent): Integer;
  275. begin
  276.   Result := inherited Remove(AComponent);
  277. end;
  278.  
  279. procedure TComponentList.SetItems(Index: Integer; AComponent: TComponent);
  280. begin
  281.   inherited Items[Index] := AComponent;
  282. end;
  283.  
  284. { TClassList }
  285.  
  286. function TClassList.Add(AClass: TClass): Integer;
  287. begin
  288.   Result := inherited Add(AClass);
  289. end;
  290.  
  291. function TClassList.GetItems(Index: Integer): TClass;
  292. begin
  293.   Result := TClass(inherited Items[Index]);
  294. end;
  295.  
  296. function TClassList.IndexOf(AClass: TClass): Integer;
  297. begin
  298.   Result := inherited IndexOf(AClass);
  299. end;
  300.  
  301. procedure TClassList.Insert(Index: Integer; AClass: TClass);
  302. begin
  303.   inherited Insert(Index, AClass);
  304. end;
  305.  
  306. function TClassList.Remove(AClass: TClass): Integer;
  307. begin
  308.   Result := inherited Remove(AClass);
  309. end;
  310.  
  311. procedure TClassList.SetItems(Index: Integer; AClass: TClass);
  312. begin
  313.   inherited Items[Index] := AClass;
  314. end;
  315.  
  316. { TOrderedList }
  317.  
  318. function TOrderedList.AtLeast(ACount: integer): boolean;
  319. begin
  320.   Result := List.Count >= ACount;
  321. end;
  322.  
  323. function TOrderedList.Peek: Pointer;
  324. begin
  325.   Result := PeekItem;
  326. end;
  327.  
  328. function TOrderedList.Pop: Pointer;
  329. begin
  330.   Result := PopItem;
  331. end;
  332.  
  333. procedure TOrderedList.Push(AItem: Pointer);
  334. begin
  335.   PushItem(AItem);
  336. end;
  337.  
  338. function TOrderedList.Count: Integer;
  339. begin
  340.   Result := List.Count;
  341. end;
  342.  
  343. constructor TOrderedList.Create;
  344. begin
  345.   inherited Create;
  346.   FList := TList.Create;
  347. end;
  348.  
  349. destructor TOrderedList.Destroy;
  350. begin
  351.   List.Free;
  352.   inherited Destroy;
  353. end;
  354.  
  355. function TOrderedList.PeekItem: Pointer;
  356. begin
  357.   Result := List[List.Count-1];
  358. end;
  359.  
  360. function TOrderedList.PopItem: Pointer;
  361. begin
  362.   Result := PeekItem;
  363.   List.Delete(List.Count-1);
  364. end;
  365.  
  366. { TStack }
  367.  
  368. procedure TStack.PushItem(AItem: Pointer);
  369. begin
  370.   List.Add(AItem);
  371. end;
  372.  
  373. { TObjectStack }
  374.  
  375. function TObjectStack.Peek: TObject;
  376. begin
  377.   Result := TObject(inherited Peek);
  378. end;
  379.  
  380. function TObjectStack.Pop: TObject;
  381. begin
  382.   Result := TObject(inherited Pop);
  383. end;
  384.  
  385. procedure TObjectStack.Push(AObject: TObject);
  386. begin
  387.   inherited Push(AObject);
  388. end;
  389.  
  390. { TQueue }
  391.  
  392. procedure TQueue.PushItem(AItem: Pointer);
  393. begin
  394.   List.Insert(0, AItem);
  395. end;
  396.  
  397. { TObjectQueue }
  398.  
  399. function TObjectQueue.Peek: TObject;
  400. begin
  401.   Result := TObject(inherited Peek);
  402. end;
  403.  
  404. function TObjectQueue.Pop: TObject;
  405. begin
  406.   Result := TObject(inherited Pop);
  407. end;
  408.  
  409. procedure TObjectQueue.Push(AObject: TObject);
  410. begin
  411.   inherited Push(AObject);
  412. end;
  413.  
  414. end.
  415.