Core classes — lists and streams

PXX provides a lightweight, FPC-compatible implementation of core Object Pascal container and stream classes in the classes unit.

These classes are reference types (managed on the heap) and must be instantiated with Create and released with Free.


TList (Pointer List)

TList maintains a dynamic, ordered list of raw pointers (Pointer). It is useful for managing low-level collections of objects or records.

Key Members


TStrings & TStringList (String List)

TStringList is a concrete class that implements the abstract TStrings contract. It manages a list of strings, providing sorting, search, multiline conversion, and association of custom metadata objects with each string.

Key Members


TStream & TMemoryStream (Byte Streams)

TStream is the abstract base class for sequential byte streams. TMemoryStream is a concrete implementation that backs the stream with a dynamic, automatically resizing memory buffer.

Key Members


Compiling Example

The following program demonstrates lists, string lists, and streams. It compiles and runs on the pinned compiler:

program core_classes_demo;

uses classes, sysutils;

procedure DemoList;
var
  list: TList;
begin
  writeln('--- TList Demo ---');
  list := TList.Create;
  try
    list.Add(Pointer(100));
    list.Add(Pointer(200));
    list.Add(Pointer(300));
    
    writeln('List count: ', list.Count);
    writeln('Item at index 1: ', Int64(list[1]));
    
    // Insert an item
    list.Insert(1, Pointer(150));
    writeln('After insert, item at index 1: ', Int64(list[1]));
    writeln('New count: ', list.Count);
  finally
    list.Free;
  end;
end;

procedure DemoStringList;
var
  sl: TStringList;
  i: Integer;
begin
  writeln('--- TStringList Demo ---');
  sl := TStringList.Create;
  try
    sl.Add('orange');
    sl.Add('apple');
    sl.Add('banana');
    
    sl.Sort;
    writeln('Sorted fruit:');
    for i := 0 to sl.Count - 1 do
      writeln('  ', sl[i]);
      
    writeln('Multiline representation:');
    write(sl.Text);
  finally
    sl.Free;
  end;
end;

procedure DemoMemoryStream;
var
  ms: TMemoryStream;
  wVal, rVal: Integer;
begin
  writeln('--- TMemoryStream Demo ---');
  ms := TMemoryStream.Create;
  try
    wVal := 12345;
    
    // Write 4 bytes (size of Integer) to the stream
    ms.Write(wVal, 4);
    writeln('Stream size: ', ms.Size);
    writeln('Stream position: ', ms.Position);
    
    // Reset position to the beginning to read
    ms.Position := 0;
    ms.Read(rVal, 4);
    writeln('Read integer value: ', rVal);
  finally
    ms.Free;
  end;
end;

begin
  DemoList;
  DemoStringList;
  DemoMemoryStream;
end.

Output:

--- TList Demo ---
List count: 3
Item at index 1: 200
After insert, item at index 1: 150
New count: 4
--- TStringList Demo ---
Sorted fruit:
  apple
  banana
  orange
Multiline representation:
apple
banana
orange
--- TMemoryStream Demo ---
Stream size: 4
Stream position: 4
Read integer value: 12345