123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- {
- $Id$
- This file is part of the Free Component Library (FCL)
- Copyright (c) 2002 by Florian Klaempfl
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- {$ifdef fpc}
- {$mode objfpc}
- {$endif}
- unit contnrs;
- interface
- uses
- Classes;
- Type
- TObjectList = class(TList)
- private
- ffreeobjects : boolean;
- Protected
- Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
- function GetItem(Index: Integer): TObject;
- Procedure SetItem(Index: Integer; AObject: TObject);
- public
- constructor create;
- constructor create(freeobjects : boolean);
- function Add(AObject: TObject): Integer;
- function Extract(Item: TObject): TObject;
- function Remove(AObject: TObject): Integer;
- function IndexOf(AObject: TObject): Integer;
- function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
- Procedure Insert(Index: Integer; AObject: TObject);
- function First: TObject;
- Function Last: TObject;
- property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
- property Items[Index: Integer]: TObject read GetItem write SetItem; default;
- end;
- TComponentList = class(TObjectList)
- Private
- FNotifier : TComponent;
- Protected
- Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
- Function GetItems(Index: Integer): TComponent;
- Procedure SetItems(Index: Integer; AComponent: TComponent);
- Procedure HandleFreeNotify(Sender: TObject; AComponent: TComponent);
- public
- destructor Destroy; override;
- Function Add(AComponent: TComponent): Integer;
- Function Extract(Item: TComponent): TComponent;
- Function Remove(AComponent: TComponent): Integer;
- Function IndexOf(AComponent: TComponent): Integer;
- Function First: TComponent;
- Function Last: TComponent;
- Procedure Insert(Index: Integer; AComponent: TComponent);
- property Items[Index: Integer]: TComponent read GetItems write SetItems; default;
- end;
- TClassList = class(TList)
- protected
- Function GetItems(Index: Integer): TClass;
- Procedure SetItems(Index: Integer; AClass: TClass);
- public
- Function Add(AClass: TClass): Integer;
- Function Extract(Item: TClass): TClass;
- Function Remove(AClass: TClass): Integer;
- Function IndexOf(AClass: TClass): Integer;
- Function First: TClass;
- Function Last: TClass;
- Procedure Insert(Index: Integer; AClass: TClass);
- property Items[Index: Integer]: TClass read GetItems write SetItems; default;
- end;
- TOrderedList = class(TObject)
- private
- FList: TList;
- protected
- Procedure PushItem(AItem: Pointer); virtual; abstract;
- Function PopItem: Pointer; virtual;
- Function PeekItem: Pointer; virtual;
- property List: TList read FList;
- public
- constructor Create;
- destructor Destroy; override;
- Function Count: Integer;
- Function AtLeast(ACount: Integer): Boolean;
- Function Push(AItem: Pointer): Pointer;
- Function Pop: Pointer;
- Function Peek: Pointer;
- end;
- { TStack class }
- TStack = class(TOrderedList)
- protected
- Procedure PushItem(AItem: Pointer); override;
- end;
- { TObjectStack class }
- TObjectStack = class(TStack)
- public
- Function Push(AObject: TObject): TObject;
- Function Pop: TObject;
- Function Peek: TObject;
- end;
- { TQueue class }
- TQueue = class(TOrderedList)
- protected
- Procedure PushItem(AItem: Pointer); override;
- end;
- { TObjectQueue class }
- TObjectQueue = class(TQueue)
- public
- Function Push(AObject: TObject): TObject;
- Function Pop: TObject;
- Function Peek: TObject;
- end;
- implementation
- constructor tobjectlist.create(freeobjects : boolean);
- begin
- inherited create;
- ffreeobjects:=freeobjects;
- end;
- Constructor tobjectlist.create;
- begin
- inherited create;
- ffreeobjects:=True;
- end;
- Procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
- begin
- if FFreeObjects then
- if (Action=lnDeleted) then
- TObject(Ptr).Free;
- inherited Notify(Ptr,Action);
- end;
- Function TObjectList.GetItem(Index: Integer): TObject;
- begin
- Result:=TObject(Inherited Get(Index));
- end;
- Procedure TObjectList.SetItem(Index: Integer; AObject: TObject);
- begin
- Put(Index,Pointer(AObject));
- end;
- Function TObjectList.Add(AObject: TObject): Integer;
- begin
- Result:=Inherited Add(Pointer(AObject));
- end;
- Function TObjectList.Extract(Item: TObject): TObject;
- begin
- Result:=Tobject(Inherited Extract(Pointer(Item)));
- end;
- Function TObjectList.Remove(AObject: TObject): Integer;
- begin
- Result:=Inherited Remove(Pointer(AObject));
- end;
- Function TObjectList.IndexOf(AObject: TObject): Integer;
- begin
- Result:=Inherited indexOF(Pointer(AObject));
- end;
- Function TObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
- Var
- I : Integer;
- begin
- I:=AStartAt;
- Result:=-1;
- If AExact then
- While (I<Count) and (Result=-1) do
- If Items[i].ClassType=AClass then
- Result:=I
- else
- Inc(I)
- else
- While (I<Count) and (Result=-1) do
- If Items[i].InheritsFrom(AClass) then
- Result:=I
- else
- Inc(I);
- end;
- Procedure TObjectList.Insert(Index: Integer; AObject: TObject);
- begin
- Inherited Insert(Index,Pointer(AObject));
- end;
- Function TObjectList.First: TObject;
- begin
- Result := TObject(Inherited First);
- end;
- Function TObjectList.Last: TObject;
- begin
- Result := TObject(Inherited Last);
- end;
- { TListComponent }
- Type
- TlistComponent = Class(TComponent)
- Private
- Flist : TComponentList;
- Public
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- end;
- procedure TlistComponent.Notification(AComponent: TComponent;
- Operation: TOperation);
- begin
- If (Operation=opremove) then
- Flist.HandleFreeNotify(Self,AComponent);
- inherited;
- end;
- { TComponentList }
- Function TComponentList.Add(AComponent: TComponent): Integer;
- begin
- Inherited Add(AComponent);
- end;
- destructor TComponentList.Destroy;
- begin
- FNotifier.Free;
- inherited;
- end;
- Function TComponentList.Extract(Item: TComponent): TComponent;
- begin
- Result:=TComponent(Inherited Extract(Item));
- end;
- Function TComponentList.First: TComponent;
- begin
- Result:=TComponent(Inherited First);
- end;
- Function TComponentList.GetItems(Index: Integer): TComponent;
- begin
- Result:=TComponent(Inherited Items[Index]);
- end;
- Procedure TComponentList.HandleFreeNotify(Sender: TObject;
- AComponent: TComponent);
- begin
- Extract(Acomponent);
- end;
- Function TComponentList.IndexOf(AComponent: TComponent): Integer;
- begin
- Result:=Inherited IndexOf(AComponent);
- end;
- Procedure TComponentList.Insert(Index: Integer; AComponent: TComponent);
- begin
- Inherited Insert(Index,Acomponent)
- end;
- Function TComponentList.Last: TComponent;
- begin
- Result:=TComponent(Inherited Last);
- end;
- Procedure TComponentList.Notify(Ptr: Pointer; Action: TListNotification);
- begin
- If FNotifier=NIl then
- begin
- FNotifier:=TlistComponent.Create(nil);
- TlistComponent(FNotifier).FList:=Self;
- end;
- If Assigned(Ptr) then
- With TComponent(Ptr) do
- case Action of
- lnAdded : FreeNotification(FNotifier);
- lnExtracted, lnDeleted: RemoveFreeNotification(FNotifier);
- end;
- inherited Notify(Ptr, Action);
- end;
- Function TComponentList.Remove(AComponent: TComponent): Integer;
- begin
- Result:=Inherited Remove(AComponent);
- end;
- Procedure TComponentList.SetItems(Index: Integer; AComponent: TComponent);
- begin
- Put(Index,AComponent);
- end;
- { TClassList }
- Function TClassList.Add(AClass: TClass): Integer;
- begin
- Result:=Inherited Add(Pointer(AClass));
- end;
- Function TClassList.Extract(Item: TClass): TClass;
- begin
- Result:=TClass(Inherited Extract(Pointer(Item)));
- end;
- Function TClassList.First: TClass;
- begin
- Result:=TClass(Inherited First);
- end;
- Function TClassList.GetItems(Index: Integer): TClass;
- begin
- Result:=TClass(Inherited Items[Index]);
- end;
- Function TClassList.IndexOf(AClass: TClass): Integer;
- begin
- Result:=Inherited IndexOf(Pointer(AClass));
- end;
- Procedure TClassList.Insert(Index: Integer; AClass: TClass);
- begin
- Inherited Insert(index,Pointer(AClass));
- end;
- Function TClassList.Last: TClass;
- begin
- Result:=TClass(Inherited Last);
- end;
- Function TClassList.Remove(AClass: TClass): Integer;
- begin
- Result:=Inherited Remove(Pointer(AClass));
- end;
- Procedure TClassList.SetItems(Index: Integer; AClass: TClass);
- begin
- Put(Index,Pointer(Aclass));
- end;
- { TOrderedList }
- Function TOrderedList.AtLeast(ACount: Integer): Boolean;
- begin
- Result:=(FList.Count>=Acount)
- end;
- Function TOrderedList.Count: Integer;
- begin
- Result:=FList.Count;
- end;
- constructor TOrderedList.Create;
- begin
- FList:=Tlist.Create;
- end;
- destructor TOrderedList.Destroy;
- begin
- FList.Free;
- end;
- Function TOrderedList.Peek: Pointer;
- begin
- If AtLeast(1) then
- Result:=PeekItem
- else
- Result:=Nil;
- end;
- Function TOrderedList.PeekItem: Pointer;
- begin
- With Flist do
- Result:=Items[Count-1]
- end;
- Function TOrderedList.Pop: Pointer;
- begin
- If Atleast(1) then
- Result:=PopItem
- else
- Result:=Nil;
- end;
- Function TOrderedList.PopItem: Pointer;
- begin
- With FList do
- If Count>0 then
- begin
- Result:=Items[Count-1];
- Delete(Count-1);
- end
- else
- Result:=Nil;
- end;
- Function TOrderedList.Push(AItem: Pointer): Pointer;
- begin
- PushItem(Aitem);
- Result:=AItem;
- end;
- { TStack }
- Procedure TStack.PushItem(AItem: Pointer);
- begin
- FList.Add(Aitem);
- end;
- { TObjectStack }
- Function TObjectStack.Peek: TObject;
- begin
- Result:=TObject(Inherited Peek);
- end;
- Function TObjectStack.Pop: TObject;
- begin
- Result:=TObject(Inherited Pop);
- end;
- Function TObjectStack.Push(AObject: TObject): TObject;
- begin
- Result:=TObject(Inherited Push(Pointer(AObject)));
- end;
- { TQueue }
- Procedure TQueue.PushItem(AItem: Pointer);
- begin
- With Flist Do
- Insert(0,AItem);
- end;
- { TObjectQueue }
- Function TObjectQueue.Peek: TObject;
- begin
- Result:=TObject(Inherited Peek);
- end;
- Function TObjectQueue.Pop: TObject;
- begin
- Result:=TObject(Inherited Pop);
- end;
- Function TObjectQueue.Push(AObject: TObject): TObject;
- begin
- Result:=TObject(Inherited Push(Pointer(Aobject)));
- end;
- end.
- {
- $Log$
- Revision 1.6 2002-09-07 15:15:24 peter
- * old logs removed and tabs fixed
- Revision 1.5 2002/08/09 09:48:28 michael
- + mode directive added plus some types fixed
- Revision 1.4 2002/08/09 09:44:33 michael
- + Implemented stack and queue (untested)
- Revision 1.3 2002/07/26 11:26:26 michael
- + Initial implementation. Untested
- Revision 1.2 2002/07/21 12:04:49 michael
- + No optional parameters in 1.0.6
- Revision 1.1 2002/07/16 13:34:39 florian
- + skeleton for contnr.pp added
- }
|