Arrays Manual
The MAXON API provides various array templates. These array types should be used instead of simple C-arrays to store data.
A maxon::PointerArray is optimized to store C++ pointers and owns the pointed objects.
// create PointerArray maxon::PointerArray<ExampleClass> objects;
// 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 maxon::UniqueRef<ExampleClass> storage(obj); // array takes ownership 对象。 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 对象。 重置 ();
A sorted array automatically sorts the stored elements on first read access.
"LessThan"
and
"IsEqual"
.
// 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()); }
These generic array types are used to define functions that can be used with various array types:
// print the value of the array's first element if (size > 0) { maxon::Int element = array[0]; DiagnosticOutput ( "First Element: @" , element); } }