Adding class operation
Adding class operation:
An operation added to a Class is called Class operation.
1. Static Class operation
- In ‘C’, functions that don’t have access to the ‘me’ pointer( pointer of the instance of the main application structure)
2. Non-static Class operation
- In C, a non-static class operation corresponds to a function that (by a coding convention) takes the “me” pointer to the struct with the class attribute. This is a function that can access instance of a main application structure(via ‘me’ pointer)
3. Class constructor
- A function of the class/structure used to initialize the instance of a structure. The return type must be void.
Just explore more about the class operation here: QM: Working with Class Operations
Let’s head over to the model. Now let’s try to create some operations.
- First of all, QHsmTst:QHsm is the main structure or main class. And let me add a class operation by choosing the option Add operation, as shown in Figure 1.
- Give one name. Let me create a static operation this_is_static_operation. To make this a static operation, you have to check static. That’s how you add a static operation.
You can add parameters here if you want.
- For example, right-click on this_is_static_operation → Add parameter
- Let’s name it ‘param1’. Give the type is ‘int,’ as shown in Figure 2.
Generate the code. You can see that a ‘static’ operation is created static void QHsmTst_this_is_static_operation; the parameters are int param1, as shown in Figure 3. But the ‘me’ pointer is not available here. And you can also see that the QHsmTst structure name is prefixed here, which signifies this is a class operation.
Now let’s add another operation, which is a non-static class operation. The class operation Name is this_is_non_static_operation; you can keep this as private; the return type is void. (Figure 4)
In Figure 5, you can see that static void QHsmTst_this_is_non_static_operation is a non-static operation, and it receives the ‘me’ pointer. Pointer to our main application structure QHsmTst. And by using this ‘me’ pointer, you can access all the member elements of this QHsmTst structure.
this_is_static_operation and this_is_non_static_operation these 2 are class operation.
You can also add a free operation. For that, you have to click on the HSM package and select Add Operation.
Let’s name it this_is_free_operation. The return type is void. You can control the visibility here; either make it a file scope or global. If you want to share that, if other files share this operation, you can keep it global—otherwise, file scope.
This free operation is outside QHsmTst:QHsm class. You have to use these directives to generate the code. Let me create that at the end. I will use $define, drag and drop this_is_free_operation, as shown in Figure 6.
Generate code. You see that this_is_free_operation is created, as shown in Figure 7. You can even assign any parameters here, so use the code field(shown in Figure 6) to put the code and add parameters. That’s about operations, adding operations to a class or structure.
Now here QHsmTst_ctor: void is a constructor of our class, which is created as a free operation. The return type must be void and make it global because QHsm_ctor we are going to share with the main.cpp. After that, let’s create the code for that. $define, drag and drop QHsmTst_ctor: void, as shown in Figure 8.
The ctor is created, as shown in Figure 9.
Let’s call this QHsmTst_ctor() from main.cpp, as shown in Figure 10.
Now, you have to give the prototype that you can implement in QHSM_Test.h. So, go to QHSM_Test.h, and here provide the declaration of the constructor, as shown in Figure 11.
In the QHSM_Test.h, you can see that the declaration of ctor is created, as shown in Figure 12. So, the main.c calls that.
Here you have to call the ctor of the superclass. So, let’s go to our class ctor.
QHsm_ctor use that here. So, the first parameter is a pointer to the superclass. So, that we have already created super_QHsmTst, the second parameter is the initial state handler’s address.
The address of the initial state handler is &QHsmTst_initial. You have to use one macro; the macro is a Q_STATE_CAST. This macro you have to use. This is just used for casting, and you need not do it manually; it is shown in Figure 13.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1