contnrs.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 2002 by Florian Klaempfl
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifdef fpc}
  12. {$mode objfpc}
  13. {$endif}
  14. unit contnrs;
  15. interface
  16. uses
  17. Classes;
  18. Type
  19. TObjectList = class(TList)
  20. private
  21. ffreeobjects : boolean;
  22. Protected
  23. Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  24. function GetItem(Index: Integer): TObject;
  25. Procedure SetItem(Index: Integer; AObject: TObject);
  26. public
  27. constructor create;
  28. constructor create(freeobjects : boolean);
  29. function Add(AObject: TObject): Integer;
  30. function Extract(Item: TObject): TObject;
  31. function Remove(AObject: TObject): Integer;
  32. function IndexOf(AObject: TObject): Integer;
  33. function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
  34. Procedure Insert(Index: Integer; AObject: TObject);
  35. function First: TObject;
  36. Function Last: TObject;
  37. property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
  38. property Items[Index: Integer]: TObject read GetItem write SetItem; default;
  39. end;
  40. TComponentList = class(TObjectList)
  41. Private
  42. FNotifier : TComponent;
  43. Protected
  44. Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  45. Function GetItems(Index: Integer): TComponent;
  46. Procedure SetItems(Index: Integer; AComponent: TComponent);
  47. Procedure HandleFreeNotify(Sender: TObject; AComponent: TComponent);
  48. public
  49. destructor Destroy; override;
  50. Function Add(AComponent: TComponent): Integer;
  51. Function Extract(Item: TComponent): TComponent;
  52. Function Remove(AComponent: TComponent): Integer;
  53. Function IndexOf(AComponent: TComponent): Integer;
  54. Function First: TComponent;
  55. Function Last: TComponent;
  56. Procedure Insert(Index: Integer; AComponent: TComponent);
  57. property Items[Index: Integer]: TComponent read GetItems write SetItems; default;
  58. end;
  59. TClassList = class(TList)
  60. protected
  61. Function GetItems(Index: Integer): TClass;
  62. Procedure SetItems(Index: Integer; AClass: TClass);
  63. public
  64. Function Add(AClass: TClass): Integer;
  65. Function Extract(Item: TClass): TClass;
  66. Function Remove(AClass: TClass): Integer;
  67. Function IndexOf(AClass: TClass): Integer;
  68. Function First: TClass;
  69. Function Last: TClass;
  70. Procedure Insert(Index: Integer; AClass: TClass);
  71. property Items[Index: Integer]: TClass read GetItems write SetItems; default;
  72. end;
  73. TOrderedList = class(TObject)
  74. private
  75. FList: TList;
  76. protected
  77. Procedure PushItem(AItem: Pointer); virtual; abstract;
  78. Function PopItem: Pointer; virtual;
  79. Function PeekItem: Pointer; virtual;
  80. property List: TList read FList;
  81. public
  82. constructor Create;
  83. destructor Destroy; override;
  84. Function Count: Integer;
  85. Function AtLeast(ACount: Integer): Boolean;
  86. Function Push(AItem: Pointer): Pointer;
  87. Function Pop: Pointer;
  88. Function Peek: Pointer;
  89. end;
  90. { TStack class }
  91. TStack = class(TOrderedList)
  92. protected
  93. Procedure PushItem(AItem: Pointer); override;
  94. end;
  95. { TObjectStack class }
  96. TObjectStack = class(TStack)
  97. public
  98. Function Push(AObject: TObject): TObject;
  99. Function Pop: TObject;
  100. Function Peek: TObject;
  101. end;
  102. { TQueue class }
  103. TQueue = class(TOrderedList)
  104. protected
  105. Procedure PushItem(AItem: Pointer); override;
  106. end;
  107. { TObjectQueue class }
  108. TObjectQueue = class(TQueue)
  109. public
  110. Function Push(AObject: TObject): TObject;
  111. Function Pop: TObject;
  112. Function Peek: TObject;
  113. end;
  114. implementation
  115. constructor tobjectlist.create(freeobjects : boolean);
  116. begin
  117. inherited create;
  118. ffreeobjects:=freeobjects;
  119. end;
  120. Constructor tobjectlist.create;
  121. begin
  122. inherited create;
  123. ffreeobjects:=True;
  124. end;
  125. Procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
  126. begin
  127. if FFreeObjects then
  128. if (Action=lnDeleted) then
  129. TObject(Ptr).Free;
  130. inherited Notify(Ptr,Action);
  131. end;
  132. Function TObjectList.GetItem(Index: Integer): TObject;
  133. begin
  134. Result:=TObject(Inherited Get(Index));
  135. end;
  136. Procedure TObjectList.SetItem(Index: Integer; AObject: TObject);
  137. begin
  138. Put(Index,Pointer(AObject));
  139. end;
  140. Function TObjectList.Add(AObject: TObject): Integer;
  141. begin
  142. Result:=Inherited Add(Pointer(AObject));
  143. end;
  144. Function TObjectList.Extract(Item: TObject): TObject;
  145. begin
  146. Result:=Tobject(Inherited Extract(Pointer(Item)));
  147. end;
  148. Function TObjectList.Remove(AObject: TObject): Integer;
  149. begin
  150. Result:=Inherited Remove(Pointer(AObject));
  151. end;
  152. Function TObjectList.IndexOf(AObject: TObject): Integer;
  153. begin
  154. Result:=Inherited indexOF(Pointer(AObject));
  155. end;
  156. Function TObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
  157. Var
  158. I : Integer;
  159. begin
  160. I:=AStartAt;
  161. Result:=-1;
  162. If AExact then
  163. While (I<Count) and (Result=-1) do
  164. If Items[i].ClassType=AClass then
  165. Result:=I
  166. else
  167. Inc(I)
  168. else
  169. While (I<Count) and (Result=-1) do
  170. If Items[i].InheritsFrom(AClass) then
  171. Result:=I
  172. else
  173. Inc(I);
  174. end;
  175. Procedure TObjectList.Insert(Index: Integer; AObject: TObject);
  176. begin
  177. Inherited Insert(Index,Pointer(AObject));
  178. end;
  179. Function TObjectList.First: TObject;
  180. begin
  181. Result := TObject(Inherited First);
  182. end;
  183. Function TObjectList.Last: TObject;
  184. begin
  185. Result := TObject(Inherited Last);
  186. end;
  187. { TListComponent }
  188. Type
  189. TlistComponent = Class(TComponent)
  190. Private
  191. Flist : TComponentList;
  192. Public
  193. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  194. end;
  195. procedure TlistComponent.Notification(AComponent: TComponent;
  196. Operation: TOperation);
  197. begin
  198. If (Operation=opremove) then
  199. Flist.HandleFreeNotify(Self,AComponent);
  200. inherited;
  201. end;
  202. { TComponentList }
  203. Function TComponentList.Add(AComponent: TComponent): Integer;
  204. begin
  205. Inherited Add(AComponent);
  206. end;
  207. destructor TComponentList.Destroy;
  208. begin
  209. FNotifier.Free;
  210. inherited;
  211. end;
  212. Function TComponentList.Extract(Item: TComponent): TComponent;
  213. begin
  214. Result:=TComponent(Inherited Extract(Item));
  215. end;
  216. Function TComponentList.First: TComponent;
  217. begin
  218. Result:=TComponent(Inherited First);
  219. end;
  220. Function TComponentList.GetItems(Index: Integer): TComponent;
  221. begin
  222. Result:=TComponent(Inherited Items[Index]);
  223. end;
  224. Procedure TComponentList.HandleFreeNotify(Sender: TObject;
  225. AComponent: TComponent);
  226. begin
  227. Extract(Acomponent);
  228. end;
  229. Function TComponentList.IndexOf(AComponent: TComponent): Integer;
  230. begin
  231. Result:=Inherited IndexOf(AComponent);
  232. end;
  233. Procedure TComponentList.Insert(Index: Integer; AComponent: TComponent);
  234. begin
  235. Inherited Insert(Index,Acomponent)
  236. end;
  237. Function TComponentList.Last: TComponent;
  238. begin
  239. Result:=TComponent(Inherited Last);
  240. end;
  241. Procedure TComponentList.Notify(Ptr: Pointer; Action: TListNotification);
  242. begin
  243. If FNotifier=NIl then
  244. begin
  245. FNotifier:=TlistComponent.Create(nil);
  246. TlistComponent(FNotifier).FList:=Self;
  247. end;
  248. If Assigned(Ptr) then
  249. With TComponent(Ptr) do
  250. case Action of
  251. lnAdded : FreeNotification(FNotifier);
  252. lnExtracted, lnDeleted: RemoveFreeNotification(FNotifier);
  253. end;
  254. inherited Notify(Ptr, Action);
  255. end;
  256. Function TComponentList.Remove(AComponent: TComponent): Integer;
  257. begin
  258. Result:=Inherited Remove(AComponent);
  259. end;
  260. Procedure TComponentList.SetItems(Index: Integer; AComponent: TComponent);
  261. begin
  262. Put(Index,AComponent);
  263. end;
  264. { TClassList }
  265. Function TClassList.Add(AClass: TClass): Integer;
  266. begin
  267. Result:=Inherited Add(Pointer(AClass));
  268. end;
  269. Function TClassList.Extract(Item: TClass): TClass;
  270. begin
  271. Result:=TClass(Inherited Extract(Pointer(Item)));
  272. end;
  273. Function TClassList.First: TClass;
  274. begin
  275. Result:=TClass(Inherited First);
  276. end;
  277. Function TClassList.GetItems(Index: Integer): TClass;
  278. begin
  279. Result:=TClass(Inherited Items[Index]);
  280. end;
  281. Function TClassList.IndexOf(AClass: TClass): Integer;
  282. begin
  283. Result:=Inherited IndexOf(Pointer(AClass));
  284. end;
  285. Procedure TClassList.Insert(Index: Integer; AClass: TClass);
  286. begin
  287. Inherited Insert(index,Pointer(AClass));
  288. end;
  289. Function TClassList.Last: TClass;
  290. begin
  291. Result:=TClass(Inherited Last);
  292. end;
  293. Function TClassList.Remove(AClass: TClass): Integer;
  294. begin
  295. Result:=Inherited Remove(Pointer(AClass));
  296. end;
  297. Procedure TClassList.SetItems(Index: Integer; AClass: TClass);
  298. begin
  299. Put(Index,Pointer(Aclass));
  300. end;
  301. { TOrderedList }
  302. Function TOrderedList.AtLeast(ACount: Integer): Boolean;
  303. begin
  304. Result:=(FList.Count>=Acount)
  305. end;
  306. Function TOrderedList.Count: Integer;
  307. begin
  308. Result:=FList.Count;
  309. end;
  310. constructor TOrderedList.Create;
  311. begin
  312. FList:=Tlist.Create;
  313. end;
  314. destructor TOrderedList.Destroy;
  315. begin
  316. FList.Free;
  317. end;
  318. Function TOrderedList.Peek: Pointer;
  319. begin
  320. If AtLeast(1) then
  321. Result:=PeekItem
  322. else
  323. Result:=Nil;
  324. end;
  325. Function TOrderedList.PeekItem: Pointer;
  326. begin
  327. With Flist do
  328. Result:=Items[Count-1]
  329. end;
  330. Function TOrderedList.Pop: Pointer;
  331. begin
  332. If Atleast(1) then
  333. Result:=PopItem
  334. else
  335. Result:=Nil;
  336. end;
  337. Function TOrderedList.PopItem: Pointer;
  338. begin
  339. With FList do
  340. If Count>0 then
  341. begin
  342. Result:=Items[Count-1];
  343. Delete(Count-1);
  344. end
  345. else
  346. Result:=Nil;
  347. end;
  348. Function TOrderedList.Push(AItem: Pointer): Pointer;
  349. begin
  350. PushItem(Aitem);
  351. Result:=AItem;
  352. end;
  353. { TStack }
  354. Procedure TStack.PushItem(AItem: Pointer);
  355. begin
  356. FList.Add(Aitem);
  357. end;
  358. { TObjectStack }
  359. Function TObjectStack.Peek: TObject;
  360. begin
  361. Result:=TObject(Inherited Peek);
  362. end;
  363. Function TObjectStack.Pop: TObject;
  364. begin
  365. Result:=TObject(Inherited Pop);
  366. end;
  367. Function TObjectStack.Push(AObject: TObject): TObject;
  368. begin
  369. Result:=TObject(Inherited Push(Pointer(AObject)));
  370. end;
  371. { TQueue }
  372. Procedure TQueue.PushItem(AItem: Pointer);
  373. begin
  374. With Flist Do
  375. Insert(0,AItem);
  376. end;
  377. { TObjectQueue }
  378. Function TObjectQueue.Peek: TObject;
  379. begin
  380. Result:=TObject(Inherited Peek);
  381. end;
  382. Function TObjectQueue.Pop: TObject;
  383. begin
  384. Result:=TObject(Inherited Pop);
  385. end;
  386. Function TObjectQueue.Push(AObject: TObject): TObject;
  387. begin
  388. Result:=TObject(Inherited Push(Pointer(Aobject)));
  389. end;
  390. end.
  391. {
  392. $Log$
  393. Revision 1.6 2002-09-07 15:15:24 peter
  394. * old logs removed and tabs fixed
  395. Revision 1.5 2002/08/09 09:48:28 michael
  396. + mode directive added plus some types fixed
  397. Revision 1.4 2002/08/09 09:44:33 michael
  398. + Implemented stack and queue (untested)
  399. Revision 1.3 2002/07/26 11:26:26 michael
  400. + Initial implementation. Untested
  401. Revision 1.2 2002/07/21 12:04:49 michael
  402. + No optional parameters in 1.0.6
  403. Revision 1.1 2002/07/16 13:34:39 florian
  404. + skeleton for contnr.pp added
  405. }