PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1

13.2. How to Make the Collection of Class Instances

The item of which size exceeds 4 bytes can not be stored into the Collection class. To make the collection of class instances, store the pointer to the class instance instead.

[Note] Exception

The SFXAnsiString and SFXWideString instances can be stored in the SFXHashmap class.

Example 13.23. Define variable-length array for storing SFXAnsiString instances

// Define variable-length array for storing SFXAnsiString instances
// for storing pointer to string
SFXArray<SFXAnsiStringPtr> array;

Example 13.24. Append items

// create string dynamically
SFXAnsiStringPtr str = new SFXAnsiString("abc");

// store pointer
array.InsertLast(str);

// it is possible to write directly as following
array.InsertLast(new SFXAnsiString("def"));

Example 13.25. Set and get items

// get second item
SFXAnsiStringPtr str = array[1];

// display
TRACE("%s", str->GetCString());

// display directly
TRACE("%s", array[1]->GetCString());

// set item

// if directly set array[0] = new SFXAnsiString("ghi");  memory leakage will occur
delete array[0]; // delete string that was dynamically generated

array[0] = new SFXAnsiString("ghi");

Example 13.26. Get the number of items

SInt32 n = array.GetSize(); // n = 2

Example 13.27. Delete items partially

// delete from array[3] to array[4]
SInt32 i;

for (i = 3; i < 5; ++i) {
    delete array[i];
}

array.Remove(3, 5);

Example 13.28. Delete all items

SFXArray<SFXAnsiStringPtr>::Iterator iterator = array.GetFirstIterator();

while (iterator.HasNext()) {
    SFXAnsiStringPtr str = iterator.GetNext();
    delete str;
}

array.Clear(); // array = ()

Example 13.29. Retrieve and check items

// SInt32 n1 = array.FirstIndexOf("abc"); invalid statement
// SInt32 n2 = array.FirstIndexOf(new SFXAnsiString("abc")); invalid statement

// It should be written as folowing

SFXArray<SFXAnsiStringPtr>::Iterator iterator = array.GetFirstIterator();

while (iterator.HasNext()) {
    SFXAnsiStringPtr str = iterator.GetNext();
    if (str->Equals("abc")) {
        return true;
    }
}
return false;

Sample code: Processing the SFXList