PrevNextUpHome SophiaFramework UNIVERSE 5.3

13.6. How to Handle Class Instance As Element

Data bigger than 4 bytes such as the class instance can not be stored into the collection as an element. To handle it as an element, use the pointer(4 bytes) to it instead.

Concretely, store not the class instance itself but the pointer(4 bytes) to it into the collection as an element.

[Note] Exception

In the hashmap(SFXLinkedHashMap / SFXFlatHashMap), a string(SFXAnsiString / SFXWideString) can be stored into the hashmap as the key of the pair element.

However, it cannot be stored as the value of the pair element. In this case, the pointer to the string should be stored there.

[Caution] Memory leakage

The heap area pointed to by the pointer which is stored into the collection as an element will not be automatically released when the element is destroyed.

Therefore, the heap pointer should be released explicitly using the delete statement before the element is destroyed. If this will not be performed, memory leakage may occur.

The code below is to define the array with data of the pointer to the SFXAnsiString string as elements.

// define the array with data of the pointer to the SFXAnsiString string as elements
SFXArray<SFXAnsiStringPtr> array;

// current status: array = () 

In the code below, 5 elements of the SFXAnsiString strings are stored into the array using the SFXArray::InsertLast function.

[Tip] Tip

The pointer to a SFXAnsiString string is stored into the array as an element.

// current status: array = () 

// generate "abc" dynamically and the pointer to "abc" is stored as an element
array.InsertLast(new SFXAnsiString("abc"));
// current status: array = (pointer to "abc" ) 

// generate "def" dynamically and the pointer to "def" is stored as an element
array.InsertLast(new SFXAnsiString("def"));
// current status: array = (pointer to "abc", pointer to "def") 

// generate "ghi" dynamically and the pointer to "ghi" is stored as an element
array.InsertLast(new SFXAnsiString("ghi"));
// current status: array = (pointer to "abc", pointer to "def", pointer to "ghi") 

// generate "jkl" dynamically and the pointer to "jkl" is stored as an element
array.InsertLast(new SFXAnsiString("jkl"));
// current status: array = (pointer to "abc", pointer to "def", pointer to "ghi", pointer to "jkl") 

// generate "mno" dynamically and the pointer to "mno" is stored as an element
array.InsertLast(new SFXAnsiString("mno"));
// current status: array = (pointer to "abc", pointer to "def", pointer to "ghi", pointer to "jkl", pointer to "mno") 

The code below is to access a string element using the index.

// current status: array = (pointer to "abc", pointer to "def", pointer to "ghi", pointer to "jkl", pointer to "mno") 

// display the string which array[1] points to onto BREW Output Window
TRACE("%s", array[1]->GetCString());

// set array[0] to the pointer to "xyz"

// if "array[0] = new SFXAnsiString("ghi"); " is executed before "delete array[0];", 
// memory leakage will occur

// delete the string which array[0] points to
delete array[0]; 

array[0] = new SFXAnsiString("xyz");
// current status: array = (pointer to "xyz", pointer to "def", pointer to "ghi", pointer to "jkl", pointer to "mno") 

The code below is to get the number of the string elements with the SFXArray::GetSize function.

// current status: array = (pointer to "xyz", pointer to "def", pointer to "ghi", pointer to "jkl", pointer to "mno") 

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

The code below is to delete the string elements specified in the index arguments with the SFXArray::Remove function.

// current status: array = (pointer to "xyz", pointer to "def", pointer to "ghi", pointer to "jkl", pointer to "mno") 

// delete elements from array[3] to array[4]
SInt32 i;
for (i = 3; i < 5; ++i) {

    delete array[i];
}

array.Remove(3, 5);
// current status: array = (pointer to "xyz", pointer to "def", pointer to "ghi") 
[Note] Note

To prevent memory leakage, delete the strings which the pointer elements point to using the delete statement.

The code below is to delete all the string elements with the SFXArray::Clear function.

// current status: array = (pointer to "xyz", pointer to "def", pointer to "ghi") 

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

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

array.Clear(); 
// current status: array = () 
[Note] Note

To prevent memory leakage, delete all the strings which the pointer elements point to using the delete statement.

A string cannot be specified in the argument of the SFXArray::FirstIndexOf / SFXArray::LastIndexOf or SFXArray::Contains function.

To implement the same functionality with this, describe it using the enumerator as follows:

// No string can be specified in the argument of FirstIndexOf() / LastIndexOf() / Contains()
// For instance, the following statements are invalid.
//    "SInt32 n = array.FirstIndexOf("abc");" 
//    "SInt32 n = array.LastIndexOf("abc");" 
//    "Bool b = array.Contains("abc");"


// "SInt32 n = array.FirstIndexOf("abc");" should be written as follows:

SFXArray<SFXAnsiStringPtr>::Enumerator etor = array.GetFirstEnumerator();

SInt32 n(-1);
SInt32 i(0);

while (etor.HasNext()) {

    SFXAnsiStringPtr string = etor.GetNext();

    if (string->Equals("abc")) {

        n = i;
        break;
    }
    i++;
}
// The index value will be stored into n, if found. Otherwise, -1 will be stored.


// SInt32 n = array.LastIndexOf("abc"); should be written as follows:

SFXArray<SFXAnsiStringPtr>::Enumerator etor = array.GetLastEnumerator();

SInt32 n(-1);
SInt32 i = array.GetSize() - 1;

while (etor.HasPrevious()) {

    SFXAnsiStringPtr string = etor.GetPrevious();

    if (string->Equals("abc")) {

        n = i;
        break;
    }
    i--;
}
// The index value will be stored into n, if found. Otherwise, -1 will be stored.


// "Bool b = array.Contains("abc");" should be written as follows:

SFXArray<SFXAnsiStringPtr>::Enumerator etor = array.GetFirstEnumerator();

Bool b(false);

while (etor.HasNext()) {

    SFXAnsiStringPtr string = etor.GetNext();

    if (string->Equals("abc")) {

        b = true;
        break;
    }
}
// "true" will be stored into b, if found. Otherwise, "false" will be stored.

Sample code: List Processing of the Class Instances