Top Up Prev Next Bottom Contents Index Search

1.9 Class NamedObjList

Class NamedObjList is simply a list of objects of class NamedObj. It is privately inherited from class SequentialList (see section 1.5), and, as a rule, other classes privately inherit from it. It supports only a subset of the operations provided by SequentialList; in particular, objects are added only to the end of the list. It provides extra operations, like searching for an object by name and deleting objects. This object enforces the rule that only const pointers to members can be obtained if the list is itself const; hence, two versions of some functions are provided.

1.9.1 NamedObjList information functions

The size and initialize functions of SequentialList are re-exported. Note that initialize removes only the links to the objects and does not delete the objects. Here is what's new:

NamedObj* objWithName(const char* name);
const NamedObj* objWithName(const char* name) const;
Find the first NamedObj on the list whose name is equal to name, and return a pointer to it. Return 0 if it is not found. There are two forms, one of which returns a const object.

NamedObj* head(); 
const NamedObj* head() const;
Return a pointer to the first object on the list (0 if none). There are two forms, one of which returns a const object.

1.9.2 Other NamedObjList functions

void put(NamedObj& obj) 
Add a pointer to obj to the list, at the end. The object must live at least as long as the list.

void initElements(); 
Apply the initialize method to each NamedObj on the list.

int remove(NamedObj* obj); 
Remove obj from the list, if present (this does not delete obj). Return 1 if it was present, 0 if not.

void deleteAll(); 
Delete all elements from the list, and reset it to be an empty list. WARNING: this assumes that the members of the list are on the heap (allocated by new, so that deleting them is valid)!

1.9.3 NamedObjList iterators

There are two different iterators associated with NamedObjList; class NamedObjListIter and class CNamedObjListIter. The latter returns const objects (which cannot then be modified). The former returns a non-const pointer, and can only be used if the NamedObjList itself is not const. Both obey the standard iterator interface and are privately derived from class ListIter.

Top Up Prev Next Bottom Contents Index Search
Copyright © 1990-1997, University of California. All rights reserved.