Component Bases


A component implements the functions of a given interface. Different implementations of the same interface define different components. Since these different implementations implement the functionality of the same interface they may have a lot of code in common. To reduce redundant code it is possible to define a base component that can be reused in different implementations.

Such a base component is used in a component with maxon::ComponentWithBase.

Base Implementation

This example interface allows to store multiple float values and to calculate the mean of the stored values:

// This example shows a simple interface with some MAXON_METHOD functions.
// ---------------------------------------------------------------------
// Class to store float values and to calculate their mean value.
// ---------------------------------------------------------------------
class DataSampleMeanInterface : MAXON_INTERFACE_BASES(maxon::ObjectInterface)
MAXON_INTERFACE(DataSampleMeanInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.interfaces.datasamplemean");
// ---------------------------------------------------------------------
// Adds the given float value to the object.
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Returns the mean value of all stored values.
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Resets the object and deletes all stored values.
// ---------------------------------------------------------------------
Float64 Float
Definition: apibase.h:199
void Reset(T &object)
Definition: apibase.h:2693
Definition: interfacebase.h:1090
Definition: objectbase.h:1125
Definition: interfacebase.h:942
Definition: objectbase.h:1054

Different implementations of this interface may calculate the mean value differently. But all implementations have to store an array of maxon::Float values. This functionality can be defined in a base component that is based on maxon::ComponentRoot.

// This example defines a base component that
// implements various functions of an interface.
class DataSampleMeanBase : public maxon::ComponentRoot
// implementations of methods of the DataSampleMeanInterface interface
return _values.Append(v);
void Reset()
maxon::BaseArray<maxon::Float> _values; // array of float values
Definition: basearray.h:413


A given implementation can inherit a base component by using maxon::ComponentWithBase. It can access the base functions and members.

// This example shows an implementation of the given interface using a base component.
class DataSampleMeanAverageImp : public maxon::ComponentWithBase<DataSampleMeanAverageImp, DataSampleMeanBase, DataSampleMeanInterface>
// implementation of the interface method
// check count
const maxon::Int count = _values.GetCount();
if (count == 0)
return maxon::IllegalStateError(MAXON_SOURCE_LOCATION, "No values set."_s);
// average
return maxon::GetAverage(_values);
Definition: objectbase.h:2632
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:190
MAXON_ATTRIBUTE_FORCE_INLINE std::remove_reference< ITERABLETYPE >::type::ValueType GetAverage(ITERABLETYPE &&array)
Returns the average of all elements.
Definition: lib_math.h:300
Definition: memoryallocationbase.h:67
Definition: objectbase.h:2193


A reference object gives access to functions implemented in both the base component and the specific component.

// This example uses methods implemented in both the
// base component and the specific implementation.
// create object
const DataSampleMeanRef averageRef = componentClass.Create() iferr_return;
// use object
averageRef.AddValue(1.0) iferr_return;
averageRef.AddValue(2.0) iferr_return;
averageRef.AddValue(3.0) iferr_return;
const maxon::Float average = averageRef.GetMean() iferr_return;
DiagnosticOutput("Average: @", average);
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
#define iferr_return
Definition: resultbase.h:1465

Further Reading