#include <tvobjs.h>
Inheritance diagram for TNSCollection::
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 |
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. 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 *);
|
|
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. |
|
Constructor. This constructor sets variable shouldDelete to True and variables count, limit and delta to 0. |
|
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().
|
|
Removes the object at position `index' from the array. Then calls delete on the object. |
|
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().
|
|
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().
|
|
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. |
|
Called whenever a collection error is encountered. By default, this member function produces a run-time error of (212 - `code'). |
|
This function is called on error conditions. By default calls function exit() to terminate the program. |
|
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); }
Reimplemented in TFileCollection, and TDirCollection. Referenced by TDirCollection::firstThat(), and TFileCollection::firstThat().
|
|
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.
|
|
Removes and destroys the given item. It just does `atRemove(indexOf(item))'. Then calls delete on the object.
Referenced by TDirCollection::free(), and TFileCollection::free().
|
|
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().
|
|
Returns the number of items stored in the collection, up to maxCollectionSize. |
|
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().
|
|
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().
|
|
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.
Reimplemented in TFileCollection, and TDirCollection. Referenced by TDirCollection::lastThat(), and TFileCollection::lastThat().
|
|
Packs the array by removing null pointers from it. Deletes all null pointers in the collection and moves items up to fill any gaps. |
|
Removes the item given by `item' from the collection. Equivalent to `atRemove(indexOf(item))'. Does not call delete on the object.
Referenced by TDirCollection::remove(), and TFileCollection::remove().
|
|
Removes all items from the collection by just setting count to 0.
Reimplemented in TPWrittenObjects, and TPReadObjects. Referenced by TPReadObjects::removeAll().
|
|
Expands or shrinks the collection by changing the allocated size to `aLimit'.
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().
|
|
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. |
|
This variable stores the number of objects in the array.
|
|
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. |
|
A pointer to an array of generic item pointers. This variable stores the array starting address. |
|
The currently allocated size (in elements) of the items list. Current size of the array. Greater or equal than count. |
|
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. |