lazutils: add TMethodList enumerators.

git-svn-id: trunk@52398 -
This commit is contained in:
ondrej 2016-05-27 15:40:19 +00:00
parent f12d1d31f1
commit 5b81517c26

View File

@ -1,6 +1,7 @@
unit LazMethodList;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
@ -11,6 +12,19 @@ type
{ TMethodList - array of TMethod }
TMethodList = class
private type
TItemsEnumerator = record
private
Owner: TMethodList;
Index: Integer;
Reverse: Boolean;
function GetCurrent: TMethod;
public
procedure Init(AOwner: TMethodList; AReverse: Boolean);
function MoveNext: Boolean;
function GetEnumerator: TItemsEnumerator;
property Current: TMethod read GetCurrent;
end;
private
FAllowDuplicates: boolean;
FItems: ^TMethod;
@ -33,6 +47,8 @@ type
procedure Move(OldIndex, NewIndex: integer);
procedure RemoveAllMethodsOfObject(const AnObject: TObject);
procedure CallNotifyEvents(Sender: TObject); // calls from Count-1 downto 0, all methods must be TNotifyEvent
function GetReversedEnumerator: TItemsEnumerator;
function GetEnumerator: TItemsEnumerator;
public
property Items[Index: integer]: TMethod read GetItems write SetItems; default;
property AllowDuplicates: boolean read FAllowDuplicates write SetAllowDuplicates; // default false, changed in Lazarus 1.3
@ -40,6 +56,42 @@ type
implementation
{ TMethodList.TItemsEnumerator }
function TMethodList.TItemsEnumerator.GetCurrent: TMethod;
begin
Result := Owner[Index];
end;
function TMethodList.TItemsEnumerator.GetEnumerator: TItemsEnumerator;
begin
Result := Self;
end;
procedure TMethodList.TItemsEnumerator.Init(AOwner: TMethodList;
AReverse: Boolean);
begin
Owner := AOwner;
Reverse := AReverse;
if Reverse then
Index := AOwner.Count
else
Index := -1;
end;
function TMethodList.TItemsEnumerator.MoveNext: Boolean;
begin
if Reverse then
begin
Dec(Index);
Result := Index >= 0;
end else
begin
Inc(Index);
Result := Index < Owner.Count;
end;
end;
{ TMethodList }
function TMethodList.GetItems(Index: integer): TMethod;
@ -47,6 +99,11 @@ begin
Result:=FItems[Index];
end;
function TMethodList.GetReversedEnumerator: TItemsEnumerator;
begin
Result.Init(Self, True);
end;
procedure TMethodList.SetAllowDuplicates(AValue: boolean);
var
i, j: Integer;
@ -105,6 +162,11 @@ begin
inherited Destroy;
end;
function TMethodList.GetEnumerator: TItemsEnumerator;
begin
Result.Init(Self, False);
end;
function TMethodList.Count: integer;
begin
if Self<>nil then