Arrays Manual

内容表

关于

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.

注意
In general it is recommended to manage the lifetime of an object using references, see 参考 .
// This example shows how to use a PointerArray to manage 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 对象。 重置 ();

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); } }

延伸阅读

maxon::PointerArray::Reset
void Reset()
Deletes all elements (calls destructors and frees memory).
定义: pointerarray.h:91
maxon::LessThan
Bool LessThan(UInt a1, UInt a2, UInt b1, UInt b2)
定义: 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
定义: collection.h:227
maxon::NonConstArray< T >
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
定义: apibase.h:177
iferr_return
#define iferr_return
定义: resultbase.h:1434
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::SortedArray
定义: sortedarray.h:49
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
maxon::PointerArray::GetCount
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
定义: pointerarray.h:122
maxon::PointerArray
定义: pointerarray.h:41
NewObj
#define NewObj(T,...)
定义: newobj.h:108
maxon::PointerArray::AppendPtr
ResultPtr< T > AppendPtr(T *x)
定义: pointerarray.h:250
maxon::BaseRef
定义: apibase.h:1522

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1