Interface Methods

About

An interface declares various methods. These methods must either be defined in an implementation of that interface or in the interface itself. Depending on the type of function, a method will also be part of the generated reference class.

MAXON_METHOD

The attribute MAXON_METHOD declares a function that must be defined in an implementation of the given interface.

A declaration of a MAXON_METHOD looks like this:

The implementation of this MAXON_METHOD is defined as:

// This example implements an interface and the MAXON_METHOD declared in the interface.
// implementation of AutomobileInterface
class TruckImplementation : public maxon::Component<TruckImplementation, AutomobileInterface>
{
public:
MAXON_METHOD maxon::Int GetWheelCount() const
{
return 6;
}
};
Definition: objectbase.h:2694
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:196
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2234
#define MAXON_METHOD
Definition: interfacebase.h:1020

Finally this method can be used with the generated reference class:

// This example creates an object of the given implementation and calls the implemented function.
// access component class
const maxon::Class<AutomobileRef> componentClass = maxon::Classes::Get<AutomobileRef>(id);
// create new object
const AutomobileRef truck = componentClass.Create() iferr_return;
// use object
const maxon::Int wheels = truck.GetWheelCount();
DiagnosticOutput("Wheels: @", wheels);
Definition: objectbase.h:698
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
#define iferr_return
Definition: resultbase.h:1771

If a MAXON_METHOD marked function is private or protected, it can still be defined in an implementation. But it will not be added to the reference class.

MAXON_FUNCTION

The attribute MAXON_FUNCTION defines a method that cannot be defined in a component. Instead it must be defined in the interface declaration. The method is added to the generated reference class.

Note
A MAXON_FUNCTION can be a function template.

The method can be used with the reference class.

// This example creates an object of the given implementation and calls a function implemented in the interface itself.
// access component class
const maxon::Id id { "net.maxonexample.class.automobiles.functions" };
const maxon::Class<AutomobileRef> componentClass = maxon::Classes::Get<AutomobileRef>(id);
// create new object
const AutomobileRef truck = componentClass.Create() iferr_return;
// use object
const maxon::Float wheels = truck.GetWheelCountFloat();
DiagnosticOutput("Wheels: @", wheels);
Definition: apibaseid.h:243
Float64 Float
Definition: apibase.h:205

Plain Functions

A function that is added to an interface without any attribute cannot be defined in a component and will not be part of the generated reference class. It can only act as an (internal) utility function that is used within other functions.

// This example implements a plain function declared in the interface.
maxon::Int AutomobileInterface::GetHalfWheelCount() const
{
return GetWheelCount() / 2;
}

Reference Class Attributes

With these attributes it is possible to add any additional code (e.g. simple functions) to the generated reference class.

Note
The code marked with these attributes is simply copied into the .hxx files. It can also be used for typedefs, using, etc.
In most cases it is recommended to rather use MAXON_FUNCTION.

The function that was defined this way is simply used with the reference class:

// This example creates a reference to the given implementation and
// calls a function that was added to the reference class.
// access component class
const maxon::Id id { "net.maxonexample.class.automobiles.const" };
const maxon::Class<AutomobileRef> componentClass = maxon::Classes::Get<AutomobileRef>(id);
// create new object
const AutomobileRef truck = componentClass.Create() iferr_return;
// use object
const maxon::Int wheels = truck.GetHalfWheelCount();
DiagnosticOutput("Wheels: @", wheels);

Often interfaces contain both const and non-const functions to access internal data. This can easily be implemented with MAXON_NONCONST_COUNTERPART. See Generic Macros.

Further Reading