About
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:
{
public:
};
#include "simpleclass1.hxx"
MAXON_DECLARATION(SimpleClassRef, OtherSimpleClass,
"net.maxonexample.othersimpleclass");
#include "simpleclass2.hxx"
[interfaces_basic_virtual_interface]
Definition: simpleclass.h:15
MAXON_METHOD void SetNumber(maxon::Int number)
MAXON_INTERFACE(SimpleClassInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.interfaces.simpleclass")
MAXON_METHOD maxon::Int GetNumber() const
Definition: objectbase.h:696
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:202
#define MAXON_REFERENCE_NORMAL(FREEIMPL)
Definition: interfacebase.h:1184
#define MAXON_DECLARATION(T, Name, id,...)
Definition: module.h:937
#define MAXON_METHOD
Definition: interfacebase.h:1012
#define MAXON_INTERFACE_BASES(...)
Definition: objectbase.h:1049
A simple implementation of that interface can look like this:
class SimpleClassImplementation :
public maxon::Component<SimpleClassImplementation, SimpleClassInterface>
{
public:
{
_value = number;
}
{
return _value;
}
private:
};
Definition: objectbase.h:2655
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2212
- The attribute maxon::Component defines the interface this component implements. A component can also use code defined in a base component. See Component Bases for more details.
- The attribute MAXON_COMPONENT() declares the class as a component and adds additional code if needed.
- The component implements the virtual functions of the interface that are declared with MAXON_METHOD.
- Class members can only exist in the implementation class.
Register
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.
- Note
- 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.
#define MAXON_COMPONENT_CLASS_REGISTER(C,...)
Definition: objectbase.h:2410
It is also possible to expose a given component as a published object that was declared with MAXON_DECLARATION:
This published object is used like this:
const SimpleClassRef simpleClass = SomeSimpleClass().Create()
iferr_return;
simpleClass.SetNumber(456);
const maxon::Int number = simpleClass.GetNumber();
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
#define iferr_return
Definition: resultbase.h:1524
MAXON_COMPONENT_OBJECT_REGISTER is used to create an instance of the component class and to publish that object:
#define MAXON_COMPONENT_OBJECT_REGISTER(C,...)
Definition: objectbase.h:2473
The object published this way can be obtained like this:
{
const SimpleClassRef& simpleClass = OtherSimpleClass();
const maxon::Int number = simpleClass.GetNumber();
}
{
const SimpleClassRef simpleClass = OtherSimpleClass();
simpleClass.SetNumber(789);
const maxon::Int number = simpleClass.GetNumber();
}
#define MAXON_SCOPE
Definition: apibase.h:2886
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.
- maxon::ComponentRoot::InitImplementation(): Is called when the component is loaded (Cinema 4D startup).
- maxon::ComponentRoot::FreeImplementation(): Is called when the component is unloaded (Cinema 4D shutdown).
- maxon::ComponentRoot::InitComponent(): Is called when an instance of the component is created.
- maxon::ComponentRoot::FreeComponent(): Is called when an instance of the component is destroyed.
- maxon::ComponentRoot::CopyFrom(): Is needed if a component has data members and to support maxon::ObjectInterface::CopyFrom() and maxon::ObjectInterface::Clone().
- maxon::ComponentRoot::DescribeIO(): Is needed to describe how data members must be written to disc. See DescribeIO Manual.
- maxon::ComponentRoot::ConfigureClass(): Is called to add additional components.
- Note
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().
Further Reading