Class AnIterator

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type AnIterator = class(AnObject)

Description

This class represents an abstract iterator.

This class is not meant to be instantiated directly; rather, it defines those methods which its descendants are required to implement. In addition to the methods defined by this class, descendants will also provide some method for retrieving and manipulating the "current" item.

Note that when an instance of AnIterator or its descendants is created, they are expected to position themselves at the first item to be returned; this behavior allows calls to AnIterator.next to be positioned at the bottom of a loop, as shown in the following example:


  // Calculate a checksum for the stream
  function AStream.checksum: int64;

  var
    // Stores the size of the file
    streamSize: int64;
    // Stores the position in the stream before the checksum was calculated
    previousPosition: int64;
    // Used to iterate over the stream
    Element: AStreamIterator;

  begin
    result := 0;
    streamSize := Self.length;
    if streamSize > 0 then
    begin
      // Store the current position
      previousPosition := Self.position;

      Element := AStreamIterator(Self.iterator);

      while Element.continues do
      begin
        result := result + Element.value;
        Element.next;
      end;

      Element.free;
      // Seek the previous position
      Self.setPosition(previousPosition);
    end;
  end;
  

Hierarchy

Overview

Methods

Public procedure next; virtual; abstract;
Public function continues: boolean; virtual; abstract;

Description

Methods

Public procedure next; virtual; abstract;

Increments the iterator.

This method is expected to fail silently if there are no further items to retrieve from the source.

Public function continues: boolean; virtual; abstract;

Determines whether are not there are more items ready for iteration.

This method should return False when the end of the source has been reached, so that the calling loop can end correctly.


Generated by PasDoc 0.13.0 on 2015-06-23 19:40:11