Skip to content

C++ classes

Classes and structs declared in C++ header files are usable from statecharts as well.

class Point
{
    public:
        int32_t get_x();
        void set_x(int32_t x);
        int32_t get_y();
        void set_y(int32_t y);
    private:
        int32_t x;
        int32_t y;
};

By importing a header file containing this C++ class definition, one or more variables of the Point type can be defined in the statechart:

import: "Point.h"
interface:
    var PointA: Point
    var PointB: Point
    out event e

As expected, it is possible to access public functions and fields of these variables. For example, x and y can be set and read from within a state’s or a transition’s reactions:

entry / PointA.set_x(42); PointA.set_y(0)
[PointA.get_x() == 42] / raise e

There are some constraints which you must consider:

  1. If you declare variable values or event payloads to be instances of a class or struct then the class must define a public default constructor (one without parameters).
  2. If a variable value in a public interface is defined to be a class instance then the class must define a public copy constructor and a public assign operator. These exist if they are not explicitly deleted or declared private. This constraint does not apply to internal variables.
  3. If an event payload is defined to be a class instance then the class must define a public copy constructor and a public assign operator. This constraint applies independent of the interface type.

Reference types can be used without these constraints.