Generate C++ Classes for MATLAB Classes - MATLAB & Simulink (original) (raw)

Main Content

When you generate C++ code, the default behavior of the code generator produces C++ classes for the classes in your MATLAB® code. These include all MATLAB classes such as value classes, handle classes, and system objects.

You can change the default behavior of the code generator to produce structures for MATLAB classes. To change the default behavior, use one of these approaches:

Mapping MATLAB Classes to C++ Classes

The code generator follows certain rules when mapping MATLAB classes to C++ classes:

Example: Generate Code for a Handle Class That Has Private and Public Members

This example illustrates how the code generator applies these rules to map MATLAB classes to C++ classes.

Consider the handle classMyClass:

classdef MyClass < handle properties publicProp = 1; end properties(Access = private) privateProp end methods function obj = MyClass(value) obj.privateProp = value; end function publicMethod(obj,value) obj.privateMethod(value); end function res = calculateSomeValue(obj) res = obj.publicProp*obj.privateProp; end end methods (Access = private) function privateMethod(obj,value) obj.publicProp = obj.publicProp + value; obj.privateProp = obj.privateProp + obj.doubleThisValue(value); end end methods(Static) function res = doubleThisValue(val) res = 2 * val; end end end

Define a MATLAB function useMyClass that usesMyClass:

function out = useMyClass(x,y) obj = MyClass(x); obj.publicMethod(y); out = obj.calculateSomeValue; end

Generate a static C++ library for useMyClass. Specify the input argument to be a scalar double. Set the code generation configuration propertyInlineBetweenUserFunctions to'Readability'.

cfg = coder.config('lib'); cfg.TargetLang = 'C++'; cfg.InlineBetweenUserFunctions = 'Readability'; codegen -config cfg useMyClass -args {0,0} -report

Code generation successful: View report

Open the code generation report and inspect the generated code. The fileuseMyClass.cpp contains the generated C++ function.

double useMyClass(double x, double y)
{
  MyClass obj;
  obj.init(x);
  obj.publicMethod(y);
  return obj.calculateSomeValue();
}

The file MyClass.h contains the definition of the generated C++ class MyClass:

class MyClass {
public:
  MyClass *init(double b_value);
  void publicMethod(double b_value);
  static double doubleThisValue(double val);
  double calculateSomeValue() const;
  double publicProp;

private:
  double privateProp;
};

This table shows how the code generator applies its mapping rules to generate C++ code for useMyClass.

Rule Code Snippet
The class constructor in MATLAB is mapped onto an init method. When an instance of the class is created, the generated code explicitly calls theinit method. The file MyClass.cpp contains the definition ofinit.MyClass *MyClass::init(double b_value) { MyClass *obj; obj = this; obj->publicProp = 1.0; obj->privateProp = b_value; return obj; }
In most cases, if a class member is set as private in MATLAB, it is also set as private in the generated C++ code.In certain situations, inlining a public method in the generated C++ code changes a private property in the your MATLAB code to a public property in the generated code and breaks data encapsulation. For example, suppose that a public methodmyMethod that uses a private propertyprop of the object is called by an entry-point function. If myMethod is inlined in the generated code, the propertyprop must be visible from outside the object and changed to a public property.To limit this occurrence, the code generator uses a special inlining rule for public methods in this situation:If the code configuration propertyInlineBetweenUserFunctions or the equivalent code generation setting Inline between user functions in the MATLAB Coderâ„¢ app is set to 'Readability', the code generator does not inline the public method calls that appear outside the class definition.In the following situations, the same inlining rules apply to both ordinary functions and public methods:The function or method is called using coder.inlineCall or coder.nonInlineCall. This call overridescoder.inline directives in the body of the function, as well as global inlining settings.The body of the function or the method contains an explicitcoder.inline('always') orcoder.inline('never') directive. This directive overrides global inlining settings.You set the code configuration propertyInlineBetweenUserFunctions or the equivalent code generation setting Inline between user functions in the MATLAB Coder app to 'Never','Speed', or 'Always'. A call to a method appears inside another method of the same class.See Control Inlining to Fine-Tune Performance and Readability of Generated Code. The definition of the generated C++ class MyClass is:class MyClass { public: MyClass *init(double b_value); void publicMethod(double b_value); static double doubleThisValue(double val); double calculateSomeValue() const; double publicProp; private: double privateProp; };The visibility of all data and member functions is preserved between MATLAB and the generated code.The private methodprivateMethod does not appear in this definition.privateMethod is inlined in the definition ofpublicMethod, which is found in the fileMyClass.cpp.void MyClass::publicMethod(double b_value) { publicProp += b_value; privateProp += MyClass::doubleThisValue(b_value); }
Static methods in MATLAB are mapped onto static C++ methods. The generated code for the static methoddoubleThisValue has this signature:double MyClass::doubleThisValue(double val) { return 2.0 * val; }
Methods that do not mutate the object are marked with theconst qualifier in the generated code. The public method calculateSomeValue does not mutate the object. The generated method has this signature:double MyClass::calculateSomeValue() const { return publicProp * privateProp; }

Additional Usage Notes and Limitations

These additional usage notes and limitations apply when generating C++ classes from MATLAB classes:

See Also

coder.CodeConfig | coder.EmbeddedCodeConfig | coder.MexCodeConfig | coder.inlineCall | coder.nonInlineCall | coder.inline

Topics