Permalink
Browse files

기존 Position을 PosByIdx로 변경,

property PosBy: T 추가
  • Loading branch information...
1 parent f59b6cb commit 93be0485caa3004ad3bb1301ce6fbf61980925bc @gomsun2 committed Mar 5, 2014
Showing with 30 additions and 12 deletions.
  1. +30 −12 source/utils/mRepeator.pas
View
42 source/utils/mRepeator.pas
@@ -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;
@@ -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;
@@ -38,7 +41,7 @@ implementation
uses
mConsts,
- System.RTTI, System.TypInfo;
+ System.RTTI, System.TypInfo, System.Math;
{ TSimpleRepeator<T> }
@@ -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;
@@ -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);
@@ -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;
@@ -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.