Skip to content

Commit

Permalink
기존 Position을 PosByIdx로 변경,
Browse files Browse the repository at this point in the history
property PosBy: T 추가
  • Loading branch information
ewsajjang committed Mar 5, 2014
1 parent f59b6cb commit 93be048
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions source/utils/mRepeator.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ interface
type
TSimpleRepeator<T> = class
private
FPosition: Integer;
FPosByIdx: Integer;
FItemIsClass: Boolean;
FItemClass: TClass;
FList: TList<T>;
procedure OnNotify(Sender: TObject; const Item: T; Action: TCollectionNotification);
private
FOwnsObjects: Boolean;
function GetCount: Integer;
procedure SetPosition(const Value: Integer);
procedure SetPosByIdx(const Value: Integer);
function GetCurrent: T;
function GetPosBy: T;
procedure SetPosBy(const Value: T);
public
constructor Create(AOwnsObjects: Boolean = True);
destructor Destroy; override;
Expand All @@ -29,7 +31,8 @@ TSimpleRepeator<T> = class

property Count: Integer read GetCount;
property Current: T read GetCurrent;
property Position: Integer read FPosition write SetPosition;
property PosByIdx: Integer read FPosByIdx write SetPosByIdx;
property PosBy: T read GetPosBy write SetPosBy;

property OwnsObjects: Boolean read FOwnsObjects write FOwnsObjects;
end;
Expand All @@ -38,7 +41,7 @@ implementation

uses
mConsts,
System.RTTI, System.TypInfo;
System.RTTI, System.TypInfo, System.Math;

{ TSimpleRepeator<T> }

Expand All @@ -59,7 +62,7 @@ destructor TSimpleRepeator<T>.Destroy;

function TSimpleRepeator<T>.Eof: Boolean;
begin
Result := FPosition = Count - 1;
Result := FPosByIdx = Count - 1;
end;

function TSimpleRepeator<T>.GetCount: Integer;
Expand All @@ -69,14 +72,20 @@ function TSimpleRepeator<T>.GetCount: Integer;

function TSimpleRepeator<T>.GetCurrent: T;
begin
Result := FList[FPosition];
Result := FList[FPosByIdx];
end;

function TSimpleRepeator<T>.GetPosBy: T;
begin
Result := Current;
end;

procedure TSimpleRepeator<T>.Init(AItems: array of T);
var
LInfo: PTypeInfo;
LItem: T;
begin
FList.Clear;
if FOwnsObjects then
begin
LInfo := TypeInfo(T);
Expand All @@ -88,15 +97,15 @@ procedure TSimpleRepeator<T>.Init(AItems: array of T);
for LItem in AItems do
FList.Add(LItem);

FPosition := 0;
FPosByIdx := 0;
end;

function TSimpleRepeator<T>.Next: T;
begin
if FPosition < Count then
Inc(FPosition);
if FPosByIdx < Count then
Inc(FPosByIdx);

Result := FList[FPosition];
Result := FList[FPosByIdx];
end;

procedure TSimpleRepeator<T>.OnNotify(Sender: TObject; const Item: T;
Expand All @@ -107,10 +116,19 @@ procedure TSimpleRepeator<T>.OnNotify(Sender: TObject; const Item: T;
(Item as FItemClass).Free;
end;

procedure TSimpleRepeator<T>.SetPosition(const Value: Integer);
procedure TSimpleRepeator<T>.SetPosBy(const Value: T);
var
LIdx: Integer;
begin
LIdx := FList.IndexOf(Value);
if InRange(LIdx, 0, FList.Count - 1) then
FPosByIdx := LIdx;
end;

procedure TSimpleRepeator<T>.SetPosByIdx(const Value: Integer);
begin
if Value < Count then
FPosition := Value;
FPosByIdx := Value;
end;

end.

0 comments on commit 93be048

Please sign in to comment.