Classes

Two classes are provided for manipulating strings, InfString and StringList, these classes are summarized in table Class StringList and table Class InfString. The InfString class inherits all of the methods from StringList, adding only the cast to char*.

Class StringList

Method

Parameter

Description

StringList (...)

 

constructors can take one of the following possible arguments

 

none

return an empty StringList

 

const StringList& s

copy s and return a new, identical StringList

 

char c

return a StringList with one string of one character

 

const char* string

copy the string and makes a one element StringList containing it

 

int i

create an ASCII representation of the number and return a one element StringList

 

double x

create an ASCII representation of the number and return a one element StringList

 

unsigned int u

create an ASCII representation of the number and return a one element StringList

StringList& operator =

 

assignment takes the same types of arguments as the constructors, except none

StringList& operator <<

 

add one or more elements to a StringList; this takes the same types of arguments as the constructors, except none

operator const char*

 

join all elements together and return as a single string

void initialize ()

 

delete all elements, making the StringList empty

int length ()

 

return the length in characters (sum of the lengths of the elements)

int numPieces ()

 

return the number of elements

const char* head ()

 

return the first element

char* newCopy ()

 

return the concatenated elements in a single newly allocated string; the caller must free the memory allocated

Class InfString

Method

Parameter

Description

all StringList methods

 

see above

operator char*

 

join all elements together and return as a single string

Although these two classes are almost identical in design, their recommended uses are quite different. The first is designed for building up strings without having to be concerned about the current or maximum size of the string. New characters can be appended to the string at any time, and memory will be allocated to accommodate them. When you are ready to use the string, perhaps by passing it to a function that expects the standard character array representation of the string, simply cast the object to char*.

In fact, InfString is publicly derived from StringList, adding only the cast to char*. StringList is implemented as a list of strings, where the size of the list is not bounded ahead of time. StringList is recommended for applications where the list structure is to be preserved. The cast to char* in InfString destroys the list structure, consolidating all its strings into one contiguous string.

 A word of warning is necessary: if a function or expression returns a StringList or InfString, and that value is not assigned to a StringList or InfString variable or reference, and the const char* or char* cast is used, it is possible (like under g++) that the StringList or InfString temporary will be destroyed too soon, leaving the const char* or char* pointer pointing to garbage. The solution is to assign the returned value to a local StringList or InfString before performing the cast. Suppose, for example, that the function foo returns an InfString. Further, suppose the function bar takes a char* argument. Then the following code will fail:

bar(foo());

(Note that the cast to char* is implicit). The following code will succeed:

InfString x = foo();
bar(x);

The StringList class is one of several list classes in the MLDesigner kernel. A basic operation on list classes is to sequentially access their members one at a time. This is accomplished using an iterator class, in comparison to the list class. For the StringList class, the iterator is called StringListIter. Its methods are summarized in table Class StringListIter.

Here's an example program fragment using this:

StringListIter item(myList);
const char* string;
while ((string = item++) != 0) cout << string << "\n";

In this code sequence, myList is assumed to be a StringList previously set up.

 

Class StringListIter

Method

Parameter

Description

StringListIter (...)

 

constructor

 

StringList& list

the list on which the iterator will iterate

const char* next ()

 

return the next string on the list, or 0 if there are no more

const char* operator++ ()

 

a synonym for next, the prefix increment operator

void reset ()

 

reset the iterator to start at the head again