Providing a record source

To provide a record source:

  1. Define the record source using TRecordSource.
  2. Define the handle class to be used by client applications using TRecordSourceHandle.
This example illustrates how to define a one-column, read-once source of Fibonacci numbers.

Begin by defining the record source:

      class TFibonacciRecordSource : public TRecordSource
      {
      protected:
          TFibonacciRecordSource() : fCurrent(0), fNext(1) {}
          ELength GetRecordCount(long&) const
              {return TRecordSourceHandle::kInfinite;}
          ELength GetRecordsRemaining(long&) const
              {return TRecordSourceHandle::kInfinite;}
          long GetColumnCount() const {return 1;}
          long GetColumnTitle(TText title, ColumnPosition)
              {title.Replace("Fibonacci");}
          EColumnType GetColumnType(ColumnPosition)
              {return TRecordSourceHandle::kLong;}
          Boolean GetField(long& value, ColumnPosition) const
              {value = fCurrent;}
          Boolean FetchRecord()
              {   long value = fCurrent + fNext;
                  fCurrent = fNext;
                  fNext = value;
                  return TRUE;
              }
      private:
          long fCurrent;
          long fNext;
          friend class TFibonacciRecordSourceHandle;
      };
Next, define the handle class that will actually be used by clients needing a Fibonacci record source:

      class TFibonacciRecordSourceHandle : public TRecordSourceHandle
      {
      public:
          TFibonacciRecordSourceHandle()
              : TRecordSourceHandle(* new TFibonacciRecordSource);
      };
Since Fibonacci numbers are infinitely available, this program will run forever:

    { TFibonacciRecordSourceHandle fibonacci; long value; while (fibonacci.FetchRecord()) { GetField(value, 1); cout << value << ", "; } }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker