Does Embedded Coder support the access by reference of C++ generated instance parameters?

2 次查看(过去 30 天)
Since Matlab R2021a, instance-specific parameters are supported as described in the following MathWorks answer.
Explained in the answer above, I've set the Data Visibility to private and the Member Access Method to Inlined method for the Model parameter arguments. Hence, the Embedded Coder generates get/set methods to access the instance parameters.
// get method for instance parameters
const InstP_model_t &get_InstP() const
{
return model_InstP;
}
// set method for instance parameters
void set_InstP(const InstP_model_t &model_InstP_arg)
{
model_InstP = model_InstP_arg;
}
Due to the get-method's returns const object reference it is currently not possible to adjust the instance-specific parameters. (e.g. myModelInstance.getInstP().ParameterStruct.Parameter = 42) Is their a way, to generate these methods without the const keyword?

回答(1 个)

Harimurali
Harimurali 2024-3-28
Hi David,
MATLAB does not provide a direct setting or option in the Simulink model configuration or Embedded Coder settings to customize the const-ness of the get method for instance-specific parameters generated by Embedded Coder. The const keyword in C++ indicates that the object returned by the get method cannot be modified, which is a common practice to ensure that the internal state of an object can only be modified through controlled interfaces, thereby preserving the encapsulation and integrity of the data.
Instead you can use the set method generated by the embedded coder to modify the instance parameters. Follow the below given steps to do the same:
  • Create an object of "InstP_model_t" called "model_InstP_new".
  • Copy the const object returned by the "get_InstP" method to the "model_InstP_new" object.
  • Make the necessary changes to the "model_InstP_new" object, for example:
model_InstP_new.ParameterStruct.Parameter = 42
  • Call the "set_InstP" method with the "model_InstP_new" object as argument.
In this way you can modify the instance-specific parameters by using the get and set methods generated by Embedded oder.
Hope this helps.
  1 个评论
dh1024
dh1024 2024-3-28
编辑:dh1024 2024-3-28
Hi Harimurali,
Thank you for your answer!
Due to performance reasons I'd like to avoid the creation of a new variable e.g. "model_InstP_new" every time the instance-specific parameters are changed.
Sincerly

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Deployment, Integration, and Supported Hardware 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by