Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

TNSCollection Class Reference

Handles a non-streamable collection of objects. More...

#include <tvobjs.h>

Inheritance diagram for TNSCollection::

TObject TCollection TNSSortedCollection TPReadObjects TDirCollection TSortedCollection TPWrittenObjects TSortedCollection TStreamableTypes TFileCollection TStringCollection TFileCollection TStringCollection TResourceCollection TResourceCollection List of all members.

Public Methods

 TNSCollection (ccIndex aLimit, ccIndex aDelta)
 ~TNSCollection ()
virtual void shutDown ()
void* at (ccIndex index)
virtual ccIndex indexOf (void *item)
void atFree (ccIndex index)
void atRemove (ccIndex index)
void remove (void *item)
void removeAll ()
void free (void *item)
void freeAll ()
void atInsert (ccIndex index, void *item)
void atPut (ccIndex index, void *item)
virtual ccIndex insert (void *item)
virtual void error (ccIndex code, ccIndex info)
void* firstThat (ccTestFunc Test, void *arg)
void* lastThat (ccTestFunc Test, void *arg)
void forEach (ccAppFunc action, void *arg)
void pack ()
virtual void setLimit (ccIndex aLimit)
ccIndex getCount ()

Static Public Methods

void error (ccIndex code, ccIndex info)

Protected Methods

 TNSCollection ()

Protected Attributes

void** items
ccIndex count
ccIndex limit
ccIndex delta
Boolean shouldDelete

Detailed Description

TNSCollection implements a nonstreamable collection of items. It provides a base class for the streamable collection class, TCollection. TNSCollection provides TCollection with the functions for adding, accessing, and removing items from a collection.

This class stores an array of pointers to generic objects. This array may grow or shrink at run-time.

Note: type ccIndex is defined in file `ttypes.h' as int.

Definition at line 89 of file tvobjs.h.


Constructor & Destructor Documentation

TNSCollection::TNSCollection ( ccIndex aLimit,
ccIndex aDelta )
 

Constructor.

Creates a collection with limit set to `aLimit' and delta set to `aDelta'. count and items data members are both set to 0. shouldDelete is set True.

The initial number of items will be limited to `aLimit', but the collection is allowed to grow in increments of `aDelta' until memory runs out or the number of items reaches maxCollectionSize.

maxCollectionSize is defined in `tvconfig.h' as:

 const int maxCollectionSize = INT_MAX / sizeof(void *);
 
See also:
maxCollectionSize

TNSCollection::~TNSCollection ( )
 

Destructor.

If shouldDelete is True, the destructor removes and destroys all items in the collection by calling freeAll and setting limit to 0.

If shouldDelete is False, the destructor sets limit to 0 but does not destroy the collection.

TNSCollection::TNSCollection ( ) [protected]
 

Constructor.

This constructor sets variable shouldDelete to True and variables count, limit and delta to 0.


Member Function Documentation

void * TNSCollection::at ( ccIndex index )
 

Returns a pointer to the item indexed by `index' in the collection. If `index' is less than 0 or greater than or equal to count, error() is called with an argument of coIndexError, and 0 is then returned.

Reimplemented in TFileCollection, and TDirCollection.

Referenced by TDirCollection::at(), and TFileCollection::at().

void TNSCollection::atFree ( ccIndex index )
 

Removes the object at position `index' from the array.

Then calls delete on the object.

void TNSCollection::atInsert ( ccIndex index,
void * item )
 

Inserts a new object at position `index'.

Moves the following items down by one position, then inserts `item' at the `index' position. If `index' is less than 0 or greater than count data member, error() is called with an argument of coIndexError and the new item is not inserted.

If count is equal to limit data member before the call to atInsert(), the allocated size of the collection is expanded by delta items using a call to setLimit().

If the setLimit() call fails to expand the collection, the error() member function is called with an argument of coOverflow and the new item is not inserted.

Referenced by TDirCollection::atInsert(), and TFileCollection::atInsert().

void TNSCollection::atPut ( ccIndex index,
void * item )
 

Replaces the object at position `index'.

Replaces the item at position `index' with the given `item'. If `index' is less than 0 or greater than or equal to count, error() is called with an argument of coIndexError.

Old object is lost.

Referenced by TDirCollection::atPut(), and TFileCollection::atPut().

void TNSCollection::atRemove ( ccIndex index )
 

Removes the object at position `index' from the array.

Removes the item at the position `index' by moving the following items up by one position. count is decremented by 1, but the memory allocated to the collection is not reduced. If `index' is greater than or equal to count, error() is called.

The item itself is not destroyed.

void TNSCollection::error ( ccIndex code,
ccIndex info ) [static]
 

Called whenever a collection error is encountered. By default, this member function produces a run-time error of (212 - `code').

void TNSCollection::error ( ccIndex code,
ccIndex info ) [virtual]
 

This function is called on error conditions.

By default calls function exit() to terminate the program.

void * TNSCollection::firstThat ( ccTestFunc func,
void * arg )
 

firstThat() applies a Boolean function `Test', along with an argument list given by `arg' to each item in the collection until the tested function returns True. The result is the item pointer for which the call returns True, or 0 if the call returned False for all items.

`Test' is a pointer to a function whose type ccTestFunc is defined as:

 typedef Boolean (*ccTestFunc)(void *, void *)
 

This method returns when one object of the array passes the test or when each object is tested without success. In the first case it returns the address of the object. In the latter case it returns 0. `arg' stores the argument of the function (if any).

This method scans the array forward. This is an example:

 define Uses_TNSCollection

 include "tv.h"

 class XObject {
     int value;
 public:
     XObject(int aValue): value(aValue) {}
     int getValue() { return value; }
 };

 Boolean matchTest(void *obj, void *value)
 {
     if (((XObject *) obj)->getValue() == *((int *) value)) return True;
     return False;
 }

 void main()
 {
     TNSCollection array(10, 5);
     array.insert(new XObject(14));
     array.insert(new XObject(32));
     array.insert(new XObject(23));
     array.insert(new XObject(41));
     int find = 23;
     XObject *p = (XObject *) array.firstThat(&matchTest, &find);
     if (p != 0) array.free(p);
 }
 
See also:
TNSCollection::forEach , TNSCollection::lastThat

Reimplemented in TFileCollection, and TDirCollection.

Referenced by TDirCollection::firstThat(), and TFileCollection::firstThat().

void TNSCollection::forEach ( ccAppFunc action,
void * arg )
 

The forEach() iterator applies an action, given by the function `action', to each item in the collection. The `arg' pointer can be used to pass additional arguments to the action.

`action' is a pointer to a function whose type ccAppFunc is defined as:

 typedef void (*ccAppFunc)(void *, void *);
 

This method scans the array forward.

See also:
TNSCollection::firstThat , TNSCollection::lastThat

void TNSCollection::free ( void * item )
 

Removes and destroys the given item.

It just does `atRemove(indexOf(item))'. Then calls delete on the object.

See also:
TNSCollection::atRemove , TNSCollection::indexOf

Referenced by TDirCollection::free(), and TFileCollection::free().

void TNSCollection::freeAll ( )
 

Removes and destroys all items in the collection and sets count to 0.

The array is cleared out but not deleted.

Referenced by TPWrittenObjects::removeAll().

ccIndex TNSCollection::getCount ( ) [inline]
 

Returns the number of items stored in the collection, up to maxCollectionSize.

Definition at line 351 of file tvobjs.h.

ccIndex TNSCollection::indexOf ( void * item ) [virtual]
 

Returns the index of the given item; that is, the converse operation to at(). If the item is not in the collection, indexOf() calls error().

The address of the item is passed in the `item' parameter.

Reimplemented in TNSSortedCollection.

Referenced by TDirCollection::indexOf().

ccIndex TNSCollection::insert ( void * item ) [virtual]
 

Inserts `item' into the collection, and adjusts other indexes if necessary. By default, insertions are made at the end of the collection by calling atInsert().

Reimplemented in TNSSortedCollection.

Referenced by TDirCollection::insert().

void * TNSCollection::lastThat ( ccTestFunc func,
void * arg )
 

lastThat() applies the Boolean function `Test', together with the `arg' argument list to each item in the collection, starting at the last item, and scanning in reverse order until the tested function returns True. The result is the item pointer for which the call returns True, or 0 if the call returned False for all items.

This method scans the array backward.

See also:
TNSCollection::firstThat , TNSCollection::forEach

Reimplemented in TFileCollection, and TDirCollection.

Referenced by TDirCollection::lastThat(), and TFileCollection::lastThat().

void TNSCollection::pack ( )
 

Packs the array by removing null pointers from it.

Deletes all null pointers in the collection and moves items up to fill any gaps.

void TNSCollection::remove ( void * item )
 

Removes the item given by `item' from the collection.

Equivalent to `atRemove(indexOf(item))'. Does not call delete on the object.

See also:
TNSCollection::atRemove , TNSCollection::indexOf

Referenced by TDirCollection::remove(), and TFileCollection::remove().

void TNSCollection::removeAll ( )
 

Removes all items from the collection by just setting count to 0.

See also:
TNSCollection::count

Reimplemented in TPWrittenObjects, and TPReadObjects.

Referenced by TPReadObjects::removeAll().

void TNSCollection::setLimit ( ccIndex aLimit ) [virtual]
 

Expands or shrinks the collection by changing the allocated size to `aLimit'.

  1. If `aLimit' is less than count, it is set to count.
  2. if `aLimit' is greater than maxCollectionSize, it is set to maxCollectionSize. Integer constant maxCollectionSize is defined in file `tvconfig.h' as:
 const int maxCollectionSize = INT_MAX / sizeof(void *);
 

Then, if `aLimit' is different from the current limit, a new items array of `aLimit' elements is allocated, the old items array is copied into the new array, and the old array is deleted.

Referenced by TCollection::TCollection(), and TNSSortedCollection::TNSSortedCollection().

void TNSCollection::shutDown ( ) [virtual]
 

Releases all the resources allocated by this class.

If class flag shouldDelete is True the function freeAll() is called. This will delete each object of the array.

Reimplemented from TObject.


Member Data Documentation

ccIndex TNSCollection::count [protected]
 

This variable stores the number of objects in the array.

See also:
TNSCollection::items , TNSCollection::limit

Definition at line 371 of file tvobjs.h.

ccIndex TNSCollection::delta [protected]
 

This value is used every time the array must be enlarged. In this case a number of delta pointers will be added to the array.

delta is the number of items by which to increase the items list whenever it becomes full. If delta is zero, the collection cannot grow beyond the size set by limit.

Definition at line 385 of file tvobjs.h.

void ** TNSCollection::items [protected]
 

A pointer to an array of generic item pointers. This variable stores the array starting address.

Definition at line 365 of file tvobjs.h.

ccIndex TNSCollection::limit [protected]
 

The currently allocated size (in elements) of the items list. Current size of the array. Greater or equal than count.

Definition at line 376 of file tvobjs.h.

Boolean TNSCollection::shouldDelete [protected]
 

If set True (the default), the TNSCollection destructor will call freeAll() before setting limit to 0. All objects will be deleted when method shutDown() is called.

If set False, the destructor simply sets limit to 0.

Definition at line 393 of file tvobjs.h.


The documentation for this class was generated from the following file:
Generated at Sat Sep 22 20:19:30 2001 for TVision by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001