To implement an application, application behaviour has to be described and implemented by attaching C++ code notes to UML operations.
Modelio C++ Designer conveniently lets you input ?++ code for UML operations directly in the operation edition dialog box. You simply enter your C++ code in the “Note Content” field of the “Operation Code” tab of the operation edition box. Modelio C++ Designer injects code entered in this field into the operation definition and generates the markers required to retrieve the code back into the model, if it is modified in the external editor.
As an example, we want to implement the assignment operator for the “Task” class in our application. For this, we open the C++ edition dialog box for the respective UML operation and input the operator code in the “Code” field.
Inputting the operator code
The entered code is injected into the definition of the assignment operator in the body of the “Task” class.
The “Return expression” field is used to input a return expression, which is always generated for the function independently of the function code. In our example, the standard return expression for assignment operators is input here. This note is generated automatically by the model-level creation operation pattern.
The “Base Constructor Code” field is used to input base construction clauses. Modelio C++ Designer automatically activates this field if the operation is detected as being a constructor (as a result of its name matching the owner class name or of the attached <<create>> stereotype).
Here is the generated code for this class:
1//class header file
2#include "MyPlanner/TaskManagement/Task.h"
3
4namespace MyPlanner
5{
6 namespace TaskManagement
7 {
8 Task::Task()
9 {
10 //modifiable zone @12366@30671900:196@T
11
12 //modifiable zone @12366@30671900:196@E
13 }
14
15 Task::Task(const Task& value)
16 {
17 //modifiable zone @12378@30671900:208@T
18 //TODO: write copy constructor code
19
20 //modifiable zone @12378@30671900:208@E
21 }
22
23 Task& Task::operator =(const Task& value)
24 {
25 //modifiable zone @12396@30671900:226@T
26 std::copy(value.getSubTask().begin(), value.getSubTask().end(), std::inserter(this.subTask, this.subTask.begin()));
27 std::copy(value.getResource().begin(), value.getResource().end(), std::inserter(this.resource, this.resource.begin()));
28 this.owner = value.getOwner();
29 this.name = value.getName();
30 this.wbsCode = value.getWbsCode();
31 //modifiable zone @12396@30671900:226@E
32
33 //modifiable zone @12397@30671900:227@T
34 return *this;
35 //modifiable zone @12397@30671900:227@E
36 }
37 //...
38 }
39}
Modelio C++ Designer lets you input ?++ code to be injected into the class header and body files directly in the class C++ edition dialog box. This can be useful to initialize global variables and implement utility functions used by the class and class members, which are not relevant to the model but which need to be provided.
To enter class-level C++ code, you simply enter it in the “Code” tab of the class C++ edition dialog box.
The “Code” tab of the C++ edition dialog box for the “TaskWindows” class
The “Placement” hierarchy defines where the input code will be injected:
Therefore, to input C++ code in the intended position in the class header or body file, you simply select the respective node in the “Areas” hierarchy and then type the code in the “C++” field.
For example, we want to inject the standard MFC macro “DECLARE_MESSAGE_MAP” into the header of the “TaskWindow” class, because this class represents the specific window (and is therefore inherited from the “CWnd” class) and should intercept window messages. The macro should be injected as a protected “member” of the class.
We open the “Code” tab of the class C++ edition dialog box, select the “Header –> Class [Class Members] –> Protected” node of the “Placement” hierarchy and type the intended code.
Modelio C++ Designer produces the following code for the “TaskWindow” class. The code entered for the “Header –> Class [Class Members] –> Protected” node of the “Placement” tree is injected into the protected part of the class.
1//includes for used library types
2
3#include <cstringt.h>
4#include <afxwin.h>
5#include <afxtempl.h>
6#include <afxcoll.h>
7
8//automatic includes (friends, associated classes, etc)
9#include "MyPlanner/GUI/Windows/CWnd.h"
10#include "MyPlanner/GUI/ITaskView.h"
11#include "MyPlanner/TaskManagement/Task.h"
12
13namespace MyPlanner
14{
15 namespace GUI
16 {
17 namespace Windows
18 {
19 class TaskWindow : public CWnd, public GUI::ITaskView
20 {
21 //...
22 private:
23 CString displayTitle;
24
25 public:
26 CDC dc;
27
28 //associations
29
30 public:
31 TaskManagement::Task* task;
32 CMap<CString,CString&,CBrush,CBrush&> brushResource;
33
34 //operations
35
36 public:
37 TaskWindow();
38 TaskWindow(const TaskWindow& value);
39 TaskWindow& operator =(const TaskWindow& value);
40 ~TaskWindow();
41 void formatDisplayTitle(std::string& FormatStr);
42 CDC getDc();
43 afx_msg int OnCreate(CREATESTRUCT* lpCreateStruct);
44 const CArray<CDC,const CDC&>& getAttr1() const;
45
46 //non-modeled members
47
48 protected:
49
50 //modifiable zone @16224@30671900:2404@T
51 DECLARE_MESSAGE_MAP()
52 //modifiable zone @16224@30671900:2404@E
53 };
54 }
55 }
56}
The "Edit header" and
"Edit body" buttons are available for
packages, classes, interfaces and operations.
For operations, the “Edit header” button opens the header of the owner class, and the “Edit body” button opens the body file of the owner file.
To edit C++ code in an editor, you simply select a model element and click on
the "Edit header" or
"Edit body" buttons in the “C++” property
view.
The editor containing the header or the body file contents is then opened. You can write implementation code in the editor, between markers. Save the editor to transfer the input code back into the model.
Note: You may prefer to input implementation code in an external editor that you can specify via the “External editor” Modelio C++ Designer settings. The model is then updated when you close this editor.
You can modify the application code outside the Modelio environment, for example, by editing it in your favorite IDE. Modelio C++ Designer lets you update the code notes belonging to model elements from externally modified code.
To update the code notes attached to a model element, just click on the
"Update" button in the “C++” property view.
Modelio C++ Designer reads the file (where the element’s code “lives”) and transfers the code between markers back into the respective notes.
Note: Only the code entered between markers generated by Modelio C++ Designer can be transferred back into the model. Any code input outside these markers is ignored!