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" );

public : // --------------------------------------------------------------------- // Adds the given float value to the object. // --------------------------------------------------------------------- MAXON_METHOD maxon::Result<void> AddValue( maxon::Float v);

// --------------------------------------------------------------------- // 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);

延伸阅读

maxon::ComponentWithBase
定义: objectbase.h:2472
maxon::GetAverage
MAXON_ATTRIBUTE_FORCE_INLINE std::remove_reference< ITERABLETYPE >::type::ValueType GetAverage(ITERABLETYPE &&array)
Returns the average of all elements.
定义: lib_math.h:300
MAXON_INTERFACE_BASES
#define MAXON_INTERFACE_BASES(...)
定义: objectbase.h:977
MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
定义: objectbase.h:2036
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
maxon::Float
Float64 Float
定义: apibase.h:193
maxon::BaseArray
定义: basearray.h:366
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::Result< void >
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
MAXON_INTERFACE
#define MAXON_INTERFACE(Name, REFKIND, ID)
定义: objectbase.h:1048
MAXON_REFERENCE_NORMAL
#define MAXON_REFERENCE_NORMAL(DUMMY)
定义: interfacebase.h:957
maxon::Reset
void Reset(T &object)
定义: apibase.h:2592

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1