Coder with classes: Default values and input checking

3 次查看(过去 30 天)
Hi
I have a use case where I have a Matlab class that I want to be part of the interface when generating c++ code. The class holds some double values.
When these values are being set by the user, the value have to be checked to be inside some boundaries. When constructing an object the values have to be set to useful default values.
This is my example class:
classdef MyClass
properties ( Access = private )
Value = 0;
end
methods
function obj = MyClass( val )
obj = obj.set_value( val );
end
function obj = set_value( obj, val )
if val < 0
val = 0;
end
obj.Value = val;
end
function val = get_value( obj )
val = obj.Value;
end
end
end
The property is private with a valid default value and I have a setter with bound checking and a getter.
The constructor explicitly calls the setter.
The generated C++ code looks like this:
header:
class MyClass
{
public:
double get_value() const;
void init(double b_Value);
MyClass();
~MyClass();
private:
double Value;
};
source file:
double MyClass::get_value() const
{
return this->Value;
}
MyClass::MyClass() {}
MyClass::~MyClass() {}
void MyClass::init(double b_Value)
{
this->Value = b_Value;
}
The default value is lost, the setter does not even get defined, the bound checking is not happening.
Coder always generates an init method that sets the property directly.
How can I have the coder generate c++ code that does not allow the user of the class to misuse it?

回答(1 个)

Darshan Ramakant Bhat
Yes this is slightly tricky. The definition of the generated class is decided based on the actual use of that class in the other function (users of the class). In your example I am suspecting nobody is calling "set_value"so it will not get generated (since the codere decided that it is not required based on the usage. ). Unfortunately as of today you have no control over such class definitions.
I have used some tricks of MATLAB Coder and modified your code a bit. Using the below command I could generate class containing the required definition.
>> codegen -config:lib classWrapper -args {0} -lang:c++ -report
Please go through the attached code.
Hope this will be helpful for you.
  2 个评论
Andreas Klotzek
Andreas Klotzek 2021-3-4
Thanks, this is interesting and also helpful. I have to show what I am doing in more detail though.
My codegen call is like this
codegen -config:lib classWrapper -args {coder.typeof(MyClass(0))} -lang:c++ -report
So the object of the class is the input to the entry function.
I understand now that I have to use the function set_value in my own Matlab code in order to enforce the code to be generated. I did this in my function like this:
function a = classWrapper( c )
c = c.set_value( c.getStoredValue() );
a = c.getStoredValue();
end
But: There is still an init method being generated, bypassing the bound check
void MyClass::init(double t0_Value)
{
this->Value = t0_Value;
}
Inside the entry function the generated code enforces the value to be reset, which will indirectly lead to the bound checking, even if the user had used the init method before, so this is a workaround
double classWrapper(MyClass *c)
{
c->set_value(c->getStoredValue());
return c->getStoredValue();
}
What I do not understand:
If I call the set_value method explicitly in the constructor of my class, why is it not being generated and used in the constructor of the c++ class?
If the user does not call the init method at all, the value of the property is not being initialized and is undefined. In my opinion the value should be initialized in the constructor of the c++ class with the default value provided in the matlab class.
Darshan Ramakant Bhat
In MATLAB Classes you can write custom setter / getter in the below manner :
These methods are supported for codegeneration as well. They should be called before setting or getting the values. I guess if you write your own setter and getters then it is not really guranteed to be called each time you set the values.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by