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 .
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" );
// --------------------------------------------------------------------- // Returns the mean value of all stored values. // --------------------------------------------------------------------- MAXON_METHOD maxon::Result<maxon::Float> GetMean();
// --------------------------------------------------------------------- // Resets the object and deletes all stored values. // --------------------------------------------------------------------- MAXON_METHOD void 重置 (); };
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 maxon::Result<void> AddValue( maxon::Float v) { return _values.Append(v); } void 重置 () { _values.Reset(); } protected : maxon::BaseArray<maxon::Float> _values; // array of float values };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> { MAXON_COMPONENT (); public : // implementation of the interface method MAXON_METHOD maxon::Result<maxon::Float> GetMean() { // 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); } };
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);