Top Up Prev Next Bottom Contents Index Search

16.1 Class CodeStream


Class CodeStream is publicly derived from class StringList, and is used to make a sequential stream of code. In class CGTarget, a base target class for code generation, there are two CodeStreams: myCode and procedures.

CodeStream myCode; 
CodeStream procedures;
These are protected members of class CGTarget. They are the default entries in codeStringLists, the list of code streams that CGTarget maintains.

CodeStreamList codeStringLists; 
This is a protected member of class CGTarget. We can add a CodeStream to codeStringLists by using the following method of class CGTarget:

void addStream(const char* name, CodeStream* code); 
This is a public method of class CGTarget. The first argument is the name of the CodeStream, and the second argument is a pointer to the CodeStream. This method should be called in the constructor of a target class. If a target attempts to add a CodeStream with an existing name, an error will be signaled.

CodeStream* getStream(const char* name=NULL);
This is a public method of class CGTarget. This method returns a pointer to the CodeStream with the given name. If no stream with the given name is found, this method returns NULL. If name=NULL, a pointer to defaultStream is returned. Class CGStar has a corresponding method to get the CodeStream with the given name from the CGStar's target.

The following method allows CGStars to construct a new CodeStream and add it to the CGTarget's list of CodeStreams. Some of the possible uses for this method are:

CodeStream* newStream(const char* name);
This is a public method of class CGTarget. There is a corresponding protected method of CGStar. This method adds a new CodeStream with the given name to the codeStringLists member of the CGTarget, and returns a pointer to the new CodeStream.

Now we will explain the public methods and members of class CodeStream.

int put(const char* code, const char* name=NULL); 
This method puts the given segment of code at the end of the CodeStream. Optionally, the name of the code segment can be given. If name=NULL, we append the code unconditionally. Otherwise, we check to see if code with the same name has already ben added, by examining the sharedNames member of the CodeStream. If no code segment with the same name is found, the code segment is appended. This method returns TRUE if code was successfully added to the stream, FALSE otherwise.

UniqueStringList sharedNames;
This is a public member of class CodeStream. It is used to store the names of code segments added by name to the CodeStream. Class UniqueStringList is privately derived from class StringList.

void initialize();
This is a public method of class CodeStream. It is used to initialize both the code list and sharedNames.

int isUnique(const char* name);
This is a public method of class UniqueStringList. This method returns FALSE if the argument string already exists in the UniqueStringList. If not, then the method adds the string to the list and returns TRUE.

Class CodeStreamList contains a list of CodeStreams. It is publicly derived from class NamedList since each CodeStream is assigned a name. There are four public methods in the CodeStreamList class:

int append(CodeStream* stream, const char* name); 
int add(const char* name, CodeStream* stream);
CodeStream* get(const char* name) const;
int remove(const char* name);
The first two methods append a CodeStream to the list. They differ from each other in the order of arguments. The third method returns a CodeStream with the given name while the last method removes the CodeStream with the given name from the list.

16.1.1 Class NamedList

Class NamedList is privately derived from class SequentialList, and is used to make a list of objects with names. It has a default constructor. The destructor deletes all objects in the list. There are no protected members in this class.

int append(Pointer object, const char* name);
void prepend(Pointer object, const char* name);
These methods put an object, object, with name name at the end and the beginning of the list, respectively. In the first method, we may not append multiple objects with the same name. If an object with the same name exists in the list, FALSE is returned. On the other hand, the second method allows multiple objects with the same name to be prepended. Only the most recently prepended object will be visible.

Pointer get(const char* name=NULL); 
This method returns the object with the given name. If no name is given, it returns the object at the head of the list. If no object is found, it returns NULL.

int remove(const char* name=NULL); 
This method removes the object with the given name. If no name is given, it removes the first object at the head of the list. If no object is found it returns FALSE, otherwise it returns TRUE.

There is an iterator class associated with the NamedList class, called NamedListIter. It returns a pointer to the next object in the list as it iterates through the list.



Top Up Prev Next Bottom Contents Index Search

Copyright © 1990-1997, University of California. All rights reserved.