Skip to content

TListErrorsStack

Ivan Semenkov edited this page Feb 3, 2021 · 3 revisions

Table of contents

About

TListErrorsStack is generic stack over list of T which contains errors codes.

uses
  utils.errorsstack;
  
type
  generic TListErrorsStack<T> = class

TOptionalValue

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}

Create

A new list error stack can be created by call its constructor.

constructor Create;
Example
uses
  utils.errorsstack;
  
type
  TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;

var
  errors : TErrors;
  
begin
  errors := TErrors.Create;

  FreeAndNil(errors);
end;

Push

Push error to stack.

procedure Push (AError : T);
Example
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;

Pop

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.

Example
uses
  utils.errorsstack;
  
type
  TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;

var
  errors : TErrors;
  
begin
  errors := TErrors.Create;
  writeln(errors.Pop);

  FreeAndNil(errors);
end;

Count

Return stack count elements.

function Count : LongInt;
Example
uses
  utils.errorsstack;
  
type
  TErrors = {$IFDEF FPC}specialize{$ENDIF} TListErrorsStack<Integer>;

var
  errors : TErrors;
  
begin
  errors := TErrors.Create;
  writeln(errors.Count);

  FreeAndNil(errors);
end;

Iterate

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.

Example
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;

FirstEntry

Retrive the iterator for first entry in an array errors stack.

function FirstEntry : TIterator;
Example
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;

TIterator

HasValue

Return true if iterator has correct value.

function HasValue : Boolean;
Example
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;

Next

Retrieve the iterator for next entry in an list errors stack.

function Next : TIterator;
Example
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;

Value

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.

Example
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;
Clone this wiki locally