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

maxon::PointerArray::Reset
void Reset()
Deletes all elements (calls destructors and frees memory).
Definition: pointerarray.h:91
maxon::LessThan
Bool LessThan(UInt a1, UInt a2, UInt b1, UInt b2)
Definition: integer.h:151
maxon::BaseCollection< SortedArray< MYSELF, ARRAY, BASESORTFLAGS::NONE, false >, EmptyClass >::IsEqual
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value &&!STD_IS_REPLACEMENT(same, typename std::decay< COMPARE >::type, EQUALITY), Bool >::type IsEqual(const COLLECTION2 &other, COMPARE &&cmp=COMPARE()) const
Definition: collection.h:227
maxon::NonConstArray< T >
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:172
iferr_return
#define iferr_return
Definition: resultbase.h:1434
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:168
maxon::SortedArray
Definition: sortedarray.h:49
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:179
maxon::PointerArray::GetCount
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Definition: pointerarray.h:122
maxon::PointerArray
Definition: pointerarray.h:41
NewObj
#define NewObj(T,...)
Definition: newobj.h:108
maxon::PointerArray::AppendPtr
ResultPtr< T > AppendPtr(T *x)
Definition: pointerarray.h:250
maxon::BaseRef
Definition: apibase.h:1494