Component Bases

About

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");
public:
// ---------------------------------------------------------------------
// Adds the given float value to the object.
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Returns the mean value of all stored values.
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Resets the object and deletes all stored values.
// ---------------------------------------------------------------------
};
PyObject PyObject * v
Definition: abstract.h:297
[interfaces_component_base_interface]
Definition: component_bases.h:14
MAXON_METHOD maxon::Result< maxon::Float > GetMean()
MAXON_METHOD void Reset()
MAXON_METHOD maxon::Result< void > AddValue(maxon::Float v)
MAXON_INTERFACE(DataSampleMeanInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.interfaces.datasamplemean")
Float64 Float
Definition: apibase.h:197
#define MAXON_REFERENCE_NORMAL(FREEIMPL)
Definition: interfacebase.h:1173
#define MAXON_METHOD
Definition: interfacebase.h:1001
#define MAXON_INTERFACE_BASES(...)
Definition: objectbase.h:1062

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
{
public:
// implementations of methods of the DataSampleMeanInterface interface
{
return _values.Append(v);
}
void Reset()
{
_values.Reset();
}
protected:
maxon::BaseArray<maxon::Float> _values; // array of float values
};
Definition: basearray.h:412
void Reset(T &object)
Definition: apibase.h:2736

Inheritance

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>
{
public:
// 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);
}
};
Py_ssize_t count
Definition: abstract.h:640
Definition: objectbase.h:2651
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:188
MAXON_ATTRIBUTE_FORCE_INLINE std::remove_reference< ITERABLETYPE >::type::ValueType GetAverage(ITERABLETYPE &&array)
Returns the average of all elements.
Definition: lib_math.h:300
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2212

Usage

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:1519

Further Reading