Reference Types

内容表

关于

Interfaces in the MAXON API can be "virtual" or "non-virtual" interfaces. Such interfaces can be registered using different reference types. These reference types define how the created reference classes are build and how they behave.

注意
"Virtual" interfaces should be preferred.

Interface Reference Types

These reference types are applicable to virtual and non-virtual interfaces:

注意
Virtual interface instances always use reference counting.

CONST

An interface using MAXON_REFERENCE_CONST only adds const functions to the created reference class. To create instances of such a read-only object a helper interface might be used that is able to create different objects (see also Factories ).

The declaration of the const interface and the helper interface could look like this:

// This example shows an interface which uses MAXON_REFERENCE_CONST // so only const functions are added to the reference class.

// --------------------------------------------------------------------- // A custom unique ID class. // --------------------------------------------------------------------- class CustomUniqueIdInterface : MAXON_INTERFACE_BASES (maxon::ObjectInterface) { MAXON_INTERFACE (CustomUniqueIdInterface, MAXON_REFERENCE_CONST , "net.maxonexample.interfaces.uniqueid" );

public : // --------------------------------------------------------------------- // Returns the internal unique ID. // --------------------------------------------------------------------- MAXON_METHOD maxon::UInt GetId() const ; };

// forward declaration of the reference class // const classes do not use the suffix "Ref" class CustomUniqueId;

// --------------------------------------------------------------------- // Non-virtual interface as a CustomUniqueId generator. // --------------------------------------------------------------------- class CustomUniqueIdGenerator { MAXON_INTERFACE_NONVIRTUAL (CustomUniqueIdGenerator, MAXON_REFERENCE_NONE , "net.maxonexample.interfaces.uniqueidgenerator" );

public : // --------------------------------------------------------------------- // Returns a new CustomUniqueId or an error. // --------------------------------------------------------------------- static MAXON_METHOD maxon::Result<CustomUniqueId> GetUniqueID(); };

The implementation of the const class can look like this. Notice the Init() function that takes an argument:

// This example shows an implementation of a const interface. // The only way to modify the internally stored data is on creation with Init().

// implementation of CustomUniqueIdInterface class UniqueIdImp : public maxon::Component <UniqueIdImp, CustomUniqueIdInterface> { MAXON_COMPONENT (); public : maxon::ResultOk<void> Init( maxon::UInt id ) { _id = id; return maxon::OK ; } MAXON_METHOD maxon::UInt GetId() const { return _id; } private : maxon::UInt _id; };

The implementation of the helper interface creates a new object by creating a new instance of the component. Calling CreateInit() will invoke the above Init() 函数。

// This example shows the implementation of a static non-virtual interface function. // This function can access an implementation class (UniqueIdImp) to create a new instance. maxon::Result<CustomUniqueId> UniqueIdGeneratorImpl::GetUniqueID() { // increment global ID g_uniqueIDs++;

// create new UniqueId return UniqueIdImp::CreateInit(g_uniqueIDs); }

Now one can use the helper interface to create a new instance of the read-only const interface.

// This example creates a new CustomUniqueId object using a static function of a helper interface.

// create ID const CustomUniqueId id = CustomUniqueIdGenerator::GetUniqueID() iferr_return ;

// use ID const maxon :: UInt value = id .GetId(); DiagnosticOutput ("UID: @", value);

COPY_ON_WRITE

If an object of a copy-on-write class is copied, it will not copy its content. It will only store a reference to the original object. Only if the copy or original object are changed, data is copied.

A simple copy-on-write interface can look like this:

// This example shows a simple interface of the reference type MAXON_REFERENCE_COPY_ON_WRITE.

// --------------------------------------------------------------------- // Class to store an array of maxon::Float values. // --------------------------------------------------------------------- class FloatArrayInterface : MAXON_INTERFACE_BASES (maxon::ObjectInterface) { MAXON_INTERFACE (FloatArrayInterface, MAXON_REFERENCE_COPY_ON_WRITE , "net.maxonexample.interfaces.floatarray" ); public : // --------------------------------------------------------------------- // Adds the given value to the array. // --------------------------------------------------------------------- MAXON_METHOD maxon::Result<void> AddValue( maxon::Float value); };

The implementation is straightforward:

// This example shows an implementation of a copy-on-write interface.

// implementation of FloatArrayInterface class FloatArrayImpl : public maxon::Component <FloatArrayImpl, FloatArrayInterface> { MAXON_COMPONENT (); public : // implementation of an interface function MAXON_METHOD maxon::Result<void> AddValue( maxon::Float value) { return _data.Append(value); }

// implementation of maxon::ComponentRoot::CopyFrom() maxon::Result<void> CopyFrom( const FloatArrayImpl& arr) { return _data.CopyFrom(arr._data); }

// implementation of maxon::ObjectInterface::ToString() MAXON_METHOD maxon::String ToString ( const maxon::FormatStatement * formatStatement) const { maxon::String result { "Values: " }; for ( const maxon::Float v : _data) result += maxon::String::FloatToString(v) + ", " _s; return result; } private : maxon::BaseArray<maxon::Float> _data; };

The reference class of a copy-on-write interface does not use the usual "Ref" suffix. It can be used as usual:

// This example creates and uses copy-on-write objects.

// create original FloatArray original = componentClass.Create() iferr_return ; // use original original.AddValue(1.0) iferr_return ; original.AddValue(2.0) iferr_return ;

// create copy FloatArray copy = componentClass.Create() iferr_return ;

// data is assigned but not yet copied copy = original;

// original and copy have access to the same data DiagnosticOutput ( "Original: @" , original); DiagnosticOutput ( "Copy: @" , copy);

// add further data; internal data will now be copied copy.AddValue(3.0) iferr_return ;

// original and copy now access different data DiagnosticOutput ( "Original: @" , original); DiagnosticOutput ( "Copy: @" , copy);

Non-Virtual Interface Reference Types

These reference types are only applicable to non-virtual interfaces:

NONE

Non-virtual interfaces can use the reference type MAXON_REFERENCE_NONE . This will create a static reference class.

The non-virtual interface is declared like this:

// This example shows a simple non-virtual interface of the reference type MAXON_REFERENCE_NONE.

// --------------------------------------------------------------------- // Class to roll a dice. // --------------------------------------------------------------------- class DiceInterface { MAXON_INTERFACE_NONVIRTUAL (DiceInterface, MAXON_REFERENCE_NONE , "net.maxonexample.interfaces.dice" ); public : // --------------------------------------------------------------------- // Rolls the dice and returns a random number. // --------------------------------------------------------------------- static MAXON_METHOD maxon::UInt RollDice(); };

The implementation can look like this:

// This example shows the implementation of a simple interface.

// implementation of DiceInterface class DiceInterfaceImpl : protected DiceInterface { MAXON_IMPLEMENTATION (DiceInterfaceImpl) public : static maxon::UInt RollDice(); };

// implementation of DiceInterface::RollDice() maxon::UInt DiceInterfaceImpl::RollDice() { // chosen by fair dice roll. // guaranteed to be random. return 4; } MAXON_IMPLEMENTATION_REGISTER (DiceInterfaceImpl);

The static reference class is used this way:

// This example uses the static member function of the interface. const maxon::UInt randomNumber = DiceInterface::RollDice(); DiagnosticOutput ( "Number: @" , randomNumber);

延伸阅读

maxon::ComponentWithBase
定义: objectbase.h:2472
MAXON_REFERENCE_NONE
#define MAXON_REFERENCE_NONE(DUMMY)
定义: interfacebase.h:885
maxon
The maxon namespace contains all declarations of the MAXON API.
定义: c4d_basedocument.h:15
MAXON_INTERFACE_BASES
#define MAXON_INTERFACE_BASES(...)
定义: objectbase.h:977
MAXON_IMPLEMENTATION_REGISTER
#define MAXON_IMPLEMENTATION_REGISTER(C,...)
定义: interfacebase.h:1398
MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
定义: objectbase.h:2036
maxon::String
定义: string.h:1197
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_REFERENCE_CONST
#define MAXON_REFERENCE_CONST(DUMMY)
定义: interfacebase.h:1026
maxon::Float
Float64 Float
定义: apibase.h:193
maxon::BaseArray
定义: basearray.h:366
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
MAXON_REFERENCE_COPY_ON_WRITE
#define MAXON_REFERENCE_COPY_ON_WRITE(DUMMY)
定义: interfacebase.h:1043
maxon::Result
定义: apibase.h:314
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
maxon::ToString
String ToString(const String &str, const FormatStatement *formatStatement, Bool=false)
定义: string.h:1680
MAXON_IMPLEMENTATION
#define MAXON_IMPLEMENTATION(C)
定义: interfacebase.h:1294
MAXON_INTERFACE_NONVIRTUAL
#define MAXON_INTERFACE_NONVIRTUAL(Name, REFKIND, ID)
定义: interfacebase.h:1169
MAXON_INTERFACE
#define MAXON_INTERFACE(Name, REFKIND, ID)
定义: objectbase.h:1048
maxon::FormatStatement
Class to store formatting statements.
定义: string.h:1960
maxon::ResultOk< void >
定义: resultbase.h:192
maxon::UInt
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
定义: apibase.h:185
UInt
maxon::UInt UInt
定义: ge_sys_math.h:63

Copyright  © 2014-2025 乐数软件    

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