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

7 views (last 30 days)
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?

Answers (1)

Harimurali
Harimurali on 28 Mar 2024 at 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 Comment
dh1024
dh1024 on 28 Mar 2024 at 14:52
Edited: dh1024 on 28 Mar 2024 at 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

Sign in to comment.

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!