Class TgoRingBuffer

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type TgoRingBuffer<T:record> = class(TObject)

Description

A ring buffer (aka "circular buffer"). This is a fixed-size buffer as if it were connected end-to-end. Lends itself for buffering data streams.

This is a generic implementation that you can use to buffer any value type (such as integers, bytes, floats and records). It does not work with reference types (such as strings, objects and interfaces).

All "Count"s used in this class refer the a number of elements (of type T), and not to a number of bytes (unless T is an 8-bit value).

Hierarchy

  • TObject
  • TgoRingBuffer

Overview

Methods

Public constructor Create(const ACapacity: Integer);
Public function Write(const AData: TArray<T>): Integer; overload;
Public function Write(const AData: array of T): Integer; overload;
Public function Write(const AData: TArray<T>; const AIndex, ACount: Integer): Integer; overload;
Public function Write(const AData: array of T; const AIndex, ACount: Integer): Integer; overload;
Public function TryWrite(const AData: TArray<T>): Boolean; overload;
Public function TryWrite(const AData: array of T): Boolean; overload;
Public function TryWrite(const AData: TArray<T>; const AIndex, ACount: Integer): Boolean; overload;
Public function TryWrite(const AData: array of T; const AIndex, ACount: Integer): Boolean; overload;
Public function Read(var AData: TArray<T>): Integer; overload;
Public function Read(var AData: array of T): Integer; overload;
Public function Read(var AData: TArray<T>; const AIndex, ACount: Integer): Integer; overload;
Public function Read(var AData: array of T; const AIndex, ACount: Integer): Integer; overload;
Public function TryRead(var AData: TArray<T>): Boolean; overload;
Public function TryRead(var AData: array of T): Boolean; overload;
Public function TryRead(var AData: TArray<T>; const AIndex, ACount: Integer): Boolean; overload;
Public function TryRead(var AData: array of T; const AIndex, ACount: Integer): Boolean; overload;

Properties

Public property Capacity: Integer read FCapacity;
Public property Count: Integer read FCount;
Public property Available: Integer read GetAvailable;

Description

Methods

Public constructor Create(const ACapacity: Integer);

Creates a ring buffer of a given size.

Parameters
ACapacity
the number of elements (of type T) that the buffer will hold.
Public function Write(const AData: TArray<T>): Integer; overload;

Writes a data array to the buffer.

Parameters
AData
the data to write to the buffer.
Returns

The number of elements written to the buffer. This may be less than the length of AData in case the buffer has become full.

Public function Write(const AData: array of T): Integer; overload;
 
Public function Write(const AData: TArray<T>; const AIndex, ACount: Integer): Integer; overload;

Writes a segment of a data array to the buffer.

AIndex and ACount must point to a valid segment in the array.

Parameters
AData
array containing the data to write to the buffer.
AIndex
start index into AData.
ACount
number of elements in AData to write to the buffer, starting at AIndex.
Returns

The number of elements written to the buffer. This may be less than ACount in case the buffer has become full.

Public function Write(const AData: array of T; const AIndex, ACount: Integer): Integer; overload;
 
Public function TryWrite(const AData: TArray<T>): Boolean; overload;

Tries to write a data array to the buffer. Either the entire operation will succeed or fail. Unlike Write, no data will be written if this operation would lead to an overflow.

Parameters
AData
the data to write to the buffer.
Returns

True if the data was successfully written, or False in case the buffer would overflow if the data would be written. In that case, no data is written to the buffer at all.

Public function TryWrite(const AData: array of T): Boolean; overload;
 
Public function TryWrite(const AData: TArray<T>; const AIndex, ACount: Integer): Boolean; overload;

Tries to write a segment of a data array to the buffer. Either the entire operation will succeed or fail. Unlike Write, no data will be written if this operation would lead to an overflow.

AIndex and ACount must point to a valid segment in the array.

Parameters
AData
array containing the data to write to the buffer.
AIndex
start index into AData.
ACount
number of elements in AData to write to the buffer, starting at AIndex.
Returns

True if the data was successfully written, or False in case the buffer would overflow if the data would be written. In that case, no data is written to the buffer at all.

Public function TryWrite(const AData: array of T; const AIndex, ACount: Integer): Boolean; overload;
 
Public function Read(var AData: TArray<T>): Integer; overload;

Reads to a data array from the buffer.

Parameters
AData
the data array that will be filled with data read from the buffer. It will try to read Length(AData) elements to fill the entire array.
Returns

The number of elements read from buffer. This may be less than the length of AData in case the buffer did not have enough data available.

Public function Read(var AData: array of T): Integer; overload;
 
Public function Read(var AData: TArray<T>; const AIndex, ACount: Integer): Integer; overload;

Reads to a segment of a data array from the buffer.

AIndex and ACount must point to a valid segment in the array.

Parameters
AData
the data array that will be filled with data read from the buffer.
AIndex
start index into AData.
ACount
the number of elements to read into AData, starting at AIndex.
Returns

The number of elements read from buffer. This may be less than ACount in case the buffer did not have enough data available.

Public function Read(var AData: array of T; const AIndex, ACount: Integer): Integer; overload;
 
Public function TryRead(var AData: TArray<T>): Boolean; overload;

Tries to read to a data array from the buffer. Either the entire operation will succeed or fail. Unlike Read, no data will be read if the buffer does not have enough data available.

Parameters
AData
the data array that will be filled with data read from the buffer. It will try to read Length(AData) elements to fill the entire array.
Returns

True if the data was successfully read, or False if the buffer does not have enough data available to read the requested amount. In that case, no data is read from the buffer at all.

Public function TryRead(var AData: array of T): Boolean; overload;
 
Public function TryRead(var AData: TArray<T>; const AIndex, ACount: Integer): Boolean; overload;

Tries to read to a segment of a data array from the buffer. Either the entire operation will succeed or fail. Unlike Read, no data will be read if the buffer does not have enough data available.

AIndex and ACount must point to a valid segment in the array.

Parameters
AData
the data array that will be filled with data read from the buffer.
AIndex
start index into AData.
ACount
the number of elements to read into AData, starting at AIndex.
Returns

True if the data was successfully read, or False if the buffer does not have enough data available to read the requested amount. In that case, no data is read from the buffer at all.

Public function TryRead(var AData: array of T; const AIndex, ACount: Integer): Boolean; overload;
 

Properties

Public property Capacity: Integer read FCapacity;

The capacity of the ring buffer (in number of elements), as passed to the constructor.

Public property Count: Integer read FCount;

The number of elements currently in the buffer (available for reading).

Public property Available: Integer read GetAvailable;

The number of elements available for writing (= Capacity - Count).


Generated by P2PasDoc 0.13.0 on 2017-04-25 12:54:26