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

7 ビュー (過去 30 日間)
dh1024
dh1024 2024 年 3 月 1 日
編集済み: dh1024 2024 年 3 月 28 日 14:54
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 日 5:16
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 日 14:52
編集済み: dh1024 2024 年 3 月 28 日 14:54
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 Center および File ExchangeDeployment, 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