-
Notifications
You must be signed in to change notification settings - Fork 7
TListErrorsStack
TListErrorsStack is generic stack over list of T which contains errors codes.
uses
utils.errorsstack;
type
generic TListErrorsStack<T> = class
If macro {$USE_OPTIONAL}
is defined, then all methods return a TOptional wrapper, otherwise T.
uses
utils.optional;
type
TOptionalError = {$IFDEF FPC}specialize{$ENDIF} TOptional<T>;
For non-existent values, returns a empty TOptionalError if defined or an EErrorNotExists is thrown.
type
{$IFNDEF USE_OPTIONAL}
EErrorNotExists = class(Exception);
{$ENDIF}
A new list error stack can be created by call its constructor.
constructor Create;
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
begin
errors := TErrors.Create;
FreeAndNil(errors);
end;
Push error to stack.
procedure Push (AError : T);
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
const
SOMETHING_WRONG = 1;
var
errors : TErrors;
begin
errors := TErrors.Create;
errors.Push(SOMETHING_WRONG);
FreeAndNil(errors);
end;
Return top error and remove it from stack.
function Pop : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalError{$ENDIF};
If errors stack not contains values then returns empty TOptionalError or raise EErrorNotExists.
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
begin
errors := TErrors.Create;
writeln(errors.Pop);
FreeAndNil(errors);
end;
Return stack count elements.
function Count : LongInt;
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
begin
errors := TErrors.Create;
writeln(errors.Count);
FreeAndNil(errors);
end;
It is possible to iterate for TListErrorsStack values using in
operator. Each value would present as TListErrorsStack.TIterator object.
uses
utils.errorsstack;
type
generic TListErrorsStack<T> = class
type
TIterator = class({$IFDEF FPC}specialize{$ENDIF} TForwardIterator<T, TIterator>)
end;
TForwardIterator is a abstract class which provide interface for iterable object that can moves to forward side.
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
value : Integer;
begin
errors := TErrors.Create;
for value in errors do
;
FreeAndNil(errors);
end;
Retrive the iterator for first entry in an array errors stack.
function FirstEntry : TIterator;
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
iterator : TErrors.TIterator;
begin
errors := TErrors.Create;
iterator := errors.FirstEntry;
FreeAndNil(errors);
end;
Return true if iterator has correct value.
function HasValue : Boolean;
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
iterator : TErrors.TIterator;
begin
errors := TErrors.Create;
iterator := errors.FirstEntry;
while iterator.HasValue do
begin
iterator := iterator.Next;
end;
FreeAndNil(errors);
end;
Retrieve the iterator for next entry in an list errors stack.
function Next : TIterator;
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
iterator : TErrors.TIterator;
begin
errors := TErrors.Create;
iterator := errors.FirstEntry;
while iterator.HasValue do
begin
iterator := iterator.Next;
end;
FreeAndNil(errors);
end;
To get value use Value property.
property Value : {$IFNDEF USE_OPTIONAL}V{$ELSE}TOptionalError{$ENDIF};
If iterator not have correct value returns empty TOptionalError or raise EErrorNotExists.
uses
utils.errorsstack;
type
TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;
var
errors : TErrors;
iterator : TErrors.TIterator;
begin
errors := TErrors.Create;
iterator := errors.FirstEntry;
while iterator.HasValue do
begin
writeln(iterator.Value);
iterator := iterator.Next;
end;
FreeAndNil(errors);
end;