unit cruForDo;
// uncomment the compilerswitch below to throw exceptions when LoopProc is nil
// {$DEFINE NilLoopProcExcept}
// uncomment the compilerswitch below to exit when LoopProc is nil
{$DEFINE NilLoopProcExit}
interface
type
TLoopProc =
procedure(
const counter: Int64;
var break: Boolean);
TObjLoopProc =
procedure(
const counter: Int64;
var break: Boolean)
of object;
TExtLoopProc =
procedure(
const counter: Extended;
var break: Boolean);
TObjExtLoopProc =
procedure(
const counter: Extended;
var break: Boolean)
of object;
function ForDo(
const start, stop, step: Int64; LoopProc: TLoopProc): Boolean;
overload;
function ForDo(
const start, stop, step: Int64; LoopProc: TObjLoopProc): Boolean;
overload;
function ForDo(
const start, stop, step: Extended; LoopProc: TExtLoopProc): Boolean;
overload;
function ForDo(
const start, stop, step: Extended; LoopProc: TObjExtLoopProc): Boolean;
overload;
implementation
{$IFDEF NilLoopProcExcept}
uses SysUtils;
const LoopProcExcp = '
No LoopProc assigned!';
{$ENDIF}
function ForDo(
const start, stop, step: Int64; LoopProc: TLoopProc): Boolean;
var
counter: Int64;
break: Boolean;
begin
break := False;
Result := True;
if not Assigned(LoopProc)
then begin
{$IFDEF NilLoopProcExcept}
raise Exception.Create(LoopProcExcp);
{$ENDIF}
Result := False;
{$IFDEF NilLoopProcExit}
Exit;
{$ENDIF}
end;
counter := start;
if start <= stop
then while (counter <= stop)
and not break
do begin
LoopProc(counter, break);
Inc(counter, step);
end else while (stop <= counter)
and not break
do begin
LoopProc(counter, break);
Dec(counter, step);
end;
end;
function ForDo(
const start, stop, step: Int64; LoopProc: TObjLoopProc): Boolean;
var
counter: Int64;
break: Boolean;
begin
break := False;
Result := True;
if not Assigned(LoopProc)
then begin
{$IFDEF NilLoopProcExcept}
raise Exception.Create(LoopProcExcp);
{$ENDIF}
Result := False;
{$IFDEF NilLoopProcExit}
Exit;
{$ENDIF}
end;
counter := start;
if start <= stop
then while (counter <= stop)
and not break
do begin
LoopProc(counter, break);
Inc(counter, step);
end else while (stop <= counter)
and not break
do begin
LoopProc(counter, break);
Dec(counter, step);
end;
end;
function ForDo(
const start, stop, step: Extended; LoopProc: TExtLoopProc): Boolean;
var
counter, stop2: Extended;
break: Boolean;
begin
break := False;
Result := True;
if not Assigned(LoopProc)
then begin
{$IFDEF NilLoopProcExcept}
raise Exception.Create(LoopProcExcp);
{$ENDIF}
Result := False;
{$IFDEF NilLoopProcExit}
Exit;
{$ENDIF}
end;
if start <= stop
then begin
counter := 0;
stop2 := (stop + step / 100) - start;
while (counter <= stop2)
and not break
do begin
LoopProc(counter + start, break);
counter := counter + step;
end;
end else begin
counter := 0;
stop2 := start - (stop - step / 100);
while (counter <= stop2)
and not break
do begin
LoopProc(start - counter, break);
counter := counter + step;
end;
end;
end;
function ForDo(
const start, stop, step: Extended; LoopProc: TObjExtLoopProc): Boolean;
var
counter, stop2: Extended;
break: Boolean;
begin
break := False;
Result := True;
if not Assigned(LoopProc)
then begin
{$IFDEF NilLoopProcExcept}
raise Exception.Create(LoopProcExcp);
{$ENDIF}
Result := False;
{$IFDEF NilLoopProcExit}
Exit;
{$ENDIF}
end;
if start <= stop
then begin
counter := 0;
stop2 := (stop + step / 100) - start;
while (counter <= stop2)
and not break
do begin
LoopProc(counter + start, break);
counter := counter + step;
end;
end else begin
counter := 0;
stop2 := start - (stop - step / 100);
while (counter <= stop2)
and not break
do begin
LoopProc(start - counter, break);
counter := counter + step;
end;
end;
end;
end.