Arrays Manual

About

The MAXON API provides various array templates. These array types should be used instead of simple C-arrays to store data.

PointerArray

A maxon::PointerArray is optimized to store C++ pointers and owns the pointed objects.

Note
In general it is recommended to manage the lifetime of an object using references, see References.
// This example shows how to use a PointerArray to manage objects.
// create PointerArray
// create new objects and insert them into the PointerArray.
for (maxon::Int i = 0; i < count; ++i)
{
// create new object
ExampleClass* const obj = NewObj(ExampleClass, i) iferr_return;
// UniqueRef holds temporary ownership
// array takes ownership
objects.AppendPtr(obj) iferr_return;
// release ownership after the PointerArray successfully took the ownership
storage.Disconnect();
}
// check objects
const maxon::Int numberOfObjects = objects.GetCount();
DiagnosticOutput("The array stores @ pointers.", numberOfObjects);
// reset the array; this will delete all elements owned by the array
objects.Reset();

SortedArray

A sorted array automatically sorts the stored elements on first read access.

// This example uses a SortedArray to store instances of an example class.
// definition of the custom SortedArray based array class
class ExampleClassSortedArray : public maxon::SortedArray<ExampleClassSortedArray, maxon::BaseArray<ExampleClass>>
{
public:
static maxon::Bool LessThan(const ExampleClass& a, const ExampleClass& b)
{
return a.GetValue() < b.GetValue();
}
static maxon::Bool IsEqual(const ExampleClass& a, const ExampleClass& b)
{
return a.GetValue() == b.GetValue();
}
};
// create sorted array and insert new objects
ExampleClassSortedArray sortedArray;
sortedArray.Append(ExampleClass(100)) iferr_return;
sortedArray.Append(ExampleClass(10)) iferr_return;
// get first object; the array will get sorted
ExampleClass* const object = sortedArray.GetFirst();
if (object)
{
DiagnosticOutput("First Element Value: @", object->GetValue());
}

Utility

These generic array types are used to define functions that can be used with various array types:

// This example function uses WritableArrayInterface to present
// a flexible interface that can be used with various array types.
static void PrintIntArrayInfo(maxon::WritableArrayInterface<maxon::Int>& array)
{
// get the size of the array
const maxon::Int size = array.GetCount();
DiagnosticOutput("Array Size: @", size);
// print the value of the array's first element
if (size > 0)
{
maxon::Int element = array[0];
DiagnosticOutput("First Element: @", element);
}
}

Further Reading