Class AnObjectVector

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type AnObjectVector = class(APointerVector)

Description

This class represents an array that contains references to objects: descendants of AnObject. Although it is assumed that all of the objects in the array will be of the same class, this is not required.

As with its parent, instances of AnObjectVector can dynamically grow as needed and can write themselves to arbitrary streams. The base implementation of AnObjectVector cannot successfully read its contents from a stream since the value of AnObjectVector.ObjectType refers to AnObject. Descendant classes that wish to make use of the base implementation of AnObjectVector.readFrom should override AnObjectVector.init and set the value of AnObjectVector.ObjectType to refer to the class of objects that will be read from the stream; alternatively, descendant classes may override AnObjectVector.readFrom to provide their own means of determining which classes to construct and insert into the vector.

Hierarchy

Overview

Fields

Protected MyObjectType: AnObjectClass;
Protected myOwnObjects: boolean;

Methods

Public function init: boolean; override;
Public destructor destroy; override;
Public procedure pushObject(const ThisObject: AnObject); virtual;
Public function appendObject(const ThisObject: AnObject): TNodeAbsoluteIndex; virtual;
Public function PopObject: AnObject; virtual;
Public procedure insertObjectAt(const thisIndex: TNodeAbsoluteIndex; const ThisObject: AnObject); virtual;
Public function RemoveObjectAt(const thisIndex: TNodeAbsoluteIndex): AnObject; virtual;
Public procedure deleteObjectAt(const thisIndex: TNodeAbsoluteIndex); virtual;
Public function ObjectAt(const thisIndex: TNodeAbsoluteIndex): AnObject; virtual;
Public function SetObjectAt(const thisIndex: TNodeAbsoluteIndex; const ThisObject: AnObject): TObject; virtual;
Public function streamingLength: TStreamIOSize; override;
Public function writeTo(const Dest: AStream): TStreamIOSize; override;
Public function readFrom(const Source: AStream): TStreamIOSize; override;
Public function printTo(const Dest: ATextOutputStream; prefix: AnsiString = ''; suffix: AnsiString = ''): TStreamIOSize; override;
Public function ObjectType: AnObjectClass; virtual;
Public function ownsObjects: boolean; virtual;
Public function setOwnsObjects(const flag: boolean): boolean; virtual;

Description

Fields

Protected MyObjectType: AnObjectClass;

Refers to the type of object managed by the vector

Protected myOwnObjects: boolean;

Indicates whether the vector owns the objects it contains

Methods

Public function init: boolean; override;

Initializer

Public destructor destroy; override;

Destroy the array and its contents.

If AnObjectVector.ownsObjects is True, then this method will call TObject.free on all valid pointers contained by the array. Otherwise, it is the caller's responsibility to maintain references to these objects so that they can be freed when they are no longer required.

Public procedure pushObject(const ThisObject: AnObject); virtual;

Push an object onto the array. The new object will be placed as the last element in the array.

This method calls APointerVector.pushReference.

Public function appendObject(const ThisObject: AnObject): TNodeAbsoluteIndex; virtual;

Append an object to the array.

The new object will be placed as the last element in the array and its index will be returned. However, the index returned is not guaranteed to remain valid; if objects are inserted into or removed from the array at indices that come before the index of the new object, then its index will change.

This method calls APointerVector.appendReference.

Public function PopObject: AnObject; virtual;

Pop the last object from the array and return it to the caller.

If the array contains no objects, this method returns Nil.

This method calls APointerVector.popReference.

Public procedure insertObjectAt(const thisIndex: TNodeAbsoluteIndex; const ThisObject: AnObject); virtual;

Insert the specified object at the specified index within the array.

If an object already exists at thisIndex, it will be "shifted right"; that is, its index (and the indices of all objects that come after it) will increase by one. If thisIndex specifies a value that is greater than AVector.length, then the new object will be added to the end of the array in much the same way as if AnObjectVector.pushObject were called.

This method calls APointerVector.insertReferenceAt.

Public function RemoveObjectAt(const thisIndex: TNodeAbsoluteIndex): AnObject; virtual;

Remove the object at the specified index and return it to the caller.

The object is removed from the array and any objects that come after it will be "shifted left"; that is, their indices will each decrease by one. If thisIndex specifies a value that is greater than or equal to AVector.length, then the last object in the array is removed and returned, exactly as if AnObjectVector.PopObject were called.

This method calls APointerVector.removeReferenceAt.

Public procedure deleteObjectAt(const thisIndex: TNodeAbsoluteIndex); virtual;

Delete the object at the specified index.

The object is removed from the array and freed with a call to TObject.free. Any objects that come after it will be "shifted left"; that is, their indices will each decrease by one. If thisIndex specifies a value that is greater than or equal to AVector.length, then the last object in the array is removed and freed.

This method calls APointerVector.removeReferenceAt.

Public function ObjectAt(const thisIndex: TNodeAbsoluteIndex): AnObject; virtual;

Retrieve the object at the specified index.

The reference returned by this routine should not be freed without also setting the reference in the array to Nil; otherwise, the array will contain a reference to an invalid pointer that may cause your program to crash with a segmentation fault when the array is destroyed or when another routine attempts to make use of the same reference.

If thisIndex specifies a value that is greater than or equal to AVector.length, then the last object in the array is returned. If the array has no objects, this method will return Nil.

This method calls APointerVector.referenceAt.

Public function SetObjectAt(const thisIndex: TNodeAbsoluteIndex; const ThisObject: AnObject): TObject; virtual;

Set the object at the specified index.

This method updates the object reference at thisIndex and returns the previous object reference that was stored there, if any. If this method returns a value that is not Nil, it may be the caller's responsibility to ensure that the object referred to will be freed when it is no longer required.

If thisIndex specifies a value that is greater than or equal to AVector.length, then the last object in the array is altered. If the array has no objects, then this method does nothing but return Nil.

Public function streamingLength: TStreamIOSize; override;

Calculate the number of bytes required to stream the vector.

This method overrides the behavior inherited from AVector.streamingLength: it queries the contents of the array to determine which objects support streaming and only those objects that implement CanStream will be included in the count. It then adds the size of AVector.length and returns the final value to the caller.

Public function writeTo(const Dest: AStream): TStreamIOSize; override;

Write the array and its contents to the specified stream.

This method overrides the behavior inherited from AVector.writeTo: it first queries the contents of the array to determine which objects support streaming; only those objects that implement CanStream will be written to the stream. Once the number of objects that support streaming is known, that number is written to the stream as the count of elements in the array; afterward, this method calls CanStream.writeTo on each object that supports it.

Returns

The total number of bytes written to Dest.

Public function readFrom(const Source: AStream): TStreamIOSize; override;

Read the array and its contents from the specified stream.

This method overrides the behavior inherited from AVector.readFrom: it reads the number of elements from the stream, then constructs instances of AnObjectVector.ObjectType to read each element from the stream. These instances are then inserted into the vector.

If AnObjectVector.ObjectType does not support the CanStream interface, then this method will only read the number of elements in the stream before returning.

Descendant classes may override this method where more complex behavior is needed – for instance, when there are many types of objects to be read from the stream.

Returns

The total number of bytes read from Source.

Public function printTo(const Dest: ATextOutputStream; prefix: AnsiString = ''; suffix: AnsiString = ''): TStreamIOSize; override;

Print the array and its contents to the specified stream.

This method overrides the behavior inherited from APrintingObject.printTo: it first writes a string representation of the vector obtained by calling AVector.toString. It then queries each object it contains; for those objects that implement CanPrint, it calls CanPrint.printTo; for all other objects it simply prints the name of the class as obtained by a call to TObject.className.

Returns

The total number of bytes prited to Dest.

Public function ObjectType: AnObjectClass; virtual;

Obtain a reference to the type of object that is managed by the vector. Strictly speaking, of course, the vector can manage any descendant of AnObject and they need not all be the same; however, in those cases where the objects contained by the vector are all of the same type, this value allows the vector to construct instances of that type on the fly.

Public function ownsObjects: boolean; virtual;

Determine whether or not the vector assumes ownership of the objects that it contains.

If this value is True, then the vector will attempt to free all valid (non-Nil) object references when it is destroyed, by calling TObject.free on each one. Otherwise, these objects will NOT be freed when the vector is destroyed, and it is the responsibility of the caller to ensure that they are properly discarded when they are no longer required.

Public function setOwnsObjects(const flag: boolean): boolean; virtual;

Set whether or not the vector assumes ownership of the objects that it contains.

If flag is set to True, then the vector will attempt to free all valid (non-Nil) object references when it is destroyed, by calling TObject.free on each one. If flag is set to False, then these objects will NOT be freed when the vector is destroyed, and it is the responsiblity of the caller to ensure that this is done.

Returns

The previous value of AnObjectVector.ownsObjects.


Generated by PasDoc 0.13.0 on 2015-01-10 17:13:18