接口实现

内容表

关于

An interface declares an empty virtual class. The actual functionality must be defined in an implementation. For a given interface multiple implementations can exist. Such an implementation is called a component .

An implementation can be defined in any source code file of a module/plugin. This way it is possible to publish the interface header file and to keep the implementation private.

Implementation

A simple interface is defined as:

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

// --------------------------------------------------------------------- // Simple class that stores a maxon::Int number. // --------------------------------------------------------------------- class SimpleClassInterface : MAXON_INTERFACE_BASES (maxon::ObjectInterface) { MAXON_INTERFACE (SimpleClassInterface, MAXON_REFERENCE_NORMAL , "net.maxonexample.interfaces.simpleclass" );

public : // --------------------------------------------------------------------- // Sets the number to store. // --------------------------------------------------------------------- MAXON_METHOD void SetNumber( maxon::Int number);

// --------------------------------------------------------------------- // Returns the stored number. // --------------------------------------------------------------------- MAXON_METHOD maxon::Int GetNumber() const ; };

// This interface is declared in a file named "simpleclass.h". The automatically // generated files are therefore named "simpleclass1.hxx" and "simpleclass2.hxx"

// The .hxx header files define the reference class "SimpleClassRef".

#include "simpleclass1.hxx"

// declare the published objects "SomeSimpleClass" and "OtherSimpleClass" // that give access to implementations of SimpleClassInterface

// the declaration must be placed between the two hxx files since "SimpleClassRef" is defined in the first hxx file

MAXON_DECLARATION ( maxon::Class<SimpleClassRef> , SomeSimpleClass, "net.maxonexample.somesimpleclass" ); MAXON_DECLARATION (SimpleClassRef, OtherSimpleClass, "net.maxonexample.othersimpleclass" ); #include "simpleclass2.hxx"

A simple implementation of that interface can look like this:

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

// This class implements SimpleClassInterface. class SimpleClassImplementation : public maxon::Component <SimpleClassImplementation, SimpleClassInterface> { MAXON_COMPONENT (); public : // implementation of interface methods MAXON_METHOD void SetNumber( maxon::Int number) { _value = number; } MAXON_METHOD maxon::Int GetNumber() const { return _value; }

// private data only available inside the implementation private : maxon::Int _value = 1; };

注册

A new component must be registered to inform Cinema 4D about it. The registration defines the ID which can be used to access the component class which in return can be used to create instances. The ID is created by combining the reverse domain name and the implementation class name.

注意
If it should be possible that another interface inherits from a custom interface it is needed that one or many implementations of that interface are accessible (e.g. as published object using MAXON_COMPONENT_CLASS_REGISTER ). See Interface Inheritance .
// This example registers the component using the base identifier // and the implementation class name. MAXON_COMPONENT_CLASS_REGISTER (SimpleClassImplementation, "net.maxonexample.class.somesimpleclass" );

It is also possible to expose a given component as a published object that was declared with MAXON_DECLARATION :

// This example registers the component and publishes it as the published object "SomeSimpleClass". // The published object must be declared in a header file. MAXON_COMPONENT_CLASS_REGISTER (OtherClassImplementation, SomeSimpleClass);

This published object is used like this:

// This example creates a reference class from a component // registered at the given declaration.

// create reference // The "SomeSimpleClass" published object is declared in a header file. // The obtained component is used to create a new instance using Create(). const SimpleClassRef simpleClass = SomeSimpleClass().Create() iferr_return ;

// use reference simpleClass.SetNumber(456); const maxon::Int number = simpleClass.GetNumber(); DiagnosticOutput ( "Number: @" , number);

MAXON_COMPONENT_OBJECT_REGISTER is used to create an instance of the component class and to publish that object:

// This example registers the given implementation, creates an instance of the class // and makes that object available with the given published object "OtherSimpleClass". MAXON_COMPONENT_OBJECT_REGISTER (ThirdSimpleClassImplementation, OtherSimpleClass);

The object published this way can be obtained like this:

// This example shows how to access an object created // and registered with MAXON_COMPONENT_OBJECT_REGISTER. MAXON_SCOPE { // access object crated with MAXON_COMPONENT_OBJECT_REGISTER const SimpleClassRef& simpleClass = OtherSimpleClass(); const maxon::Int number = simpleClass.GetNumber(); DiagnosticOutput ( "Number: @" , number); } MAXON_SCOPE { // create copy of the object created with MAXON_COMPONENT_OBJECT_REGISTER const SimpleClassRef simpleClass = OtherSimpleClass();

// use reference simpleClass.SetNumber(789); const maxon::Int number = simpleClass.GetNumber(); DiagnosticOutput ( "Number: @" , number); }

Multiple Components

The attribute MAXON_COMPONENT() declares a given class as a component. The attribute can be used to define the component type:

Additionally, MAXON_COMPONENT() can define additional components that are added to a given component. If this is not sufficient one can implement maxon::ComponentRoot::ConfigureClass(). These additional components typically implement the interface's base interfaces. See Interface Inheritance .

Default Functions

Every component can implement these default functions. They are declared in the maxon::ComponentRoot class.

注意
InitImplementation() is called before the Cinema 4D core is loaded. This means that no classic API code can be called in this context.

Static Methods

Static methods of interfaces can only be implemented once. The implementation class of these methods must be additionally registered with MAXON_STATIC_REGISTER() .

延伸阅读

maxon::ComponentWithBase
定义: objectbase.h:2472
maxon::Class
定义: objectbase.h:681
MAXON_COMPONENT_OBJECT_REGISTER
#define MAXON_COMPONENT_OBJECT_REGISTER(C,...)
定义: objectbase.h:2297
MAXON_INTERFACE_BASES
#define MAXON_INTERFACE_BASES(...)
定义: objectbase.h:977
MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
定义: objectbase.h:2036
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_COMPONENT_CLASS_REGISTER
#define MAXON_COMPONENT_CLASS_REGISTER(C,...)
定义: objectbase.h:2233
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
MAXON_DECLARATION
#define MAXON_DECLARATION(T, Name, id)
定义: module.h:797
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
MAXON_INTERFACE
#define MAXON_INTERFACE(Name, REFKIND, ID)
定义: objectbase.h:1048
MAXON_REFERENCE_NORMAL
#define MAXON_REFERENCE_NORMAL(DUMMY)
定义: interfacebase.h:957
MAXON_SCOPE
#define MAXON_SCOPE
定义: apibase.h:2645

Copyright  © 2014-2025 乐数软件    

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