Professional Documents
Culture Documents
Ooc Embed System
Ooc Embed System
EventHelix.com
eventstudio model object and
message flows
visualether Wireshark pcap
to call flow
5G NR call flows
architecture
LTE call flows
architecture
company contact us
support
This article focuses on platforms where C++ compilers are not available. The
developers working on these platforms should still be able to use object
oriented analysis and design. When it comes to final code development they
can use C.
The following sections cover an example of C++ code and its implementation
in C.
Terminal Manager
TerminalManager.hpp
1 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
TerminalManager.cpp
2 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
23
24 Terminal *pTerm = FindTerminal(terminalId);
25 Terminal *pOtherTerm = NULL;
26
27 if (pTerm != NULL)
28 {
29 switch (pMsg->GetType())
30 {
31 case CREATE_TERMINAL:
32 pTerm->Activate((const TerminalCreateMsg *)pMsg);
33 break;
34 case DELETE_TERMINAL:
35 pTerm->Deactivate((const TerminalDeleteMsg *) pMsg);
36 break;
37 case RUN_DIAGNOSTICS:
38 status = pTerm->HandleRunDiagnostics((const RunDiagnosticsMsg *) pMsg);
39 break;
40 case PERFORM_SWITCHOVER:
41 pOtherTerm = FindTerminal(pMsg->GetOtherTerminalId());
42 status = pTerm->HandleOutOfService();
43 status1 = pOtherTerm->HandleInService();
44 break;
45 }
46 }
47 delete pMsg;
48 }
49
50 Terminal *TerminalManager::FindTerminal(int terminalId)
51 {
52 if (terminalId < MAX_TERMINALS)
53 {
54 return (&terminals[terminalId]);
55 }
56 else
57 {
58 return NULL;
59 }
60 }
Terminal
3 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
Terminal.hpp
Terminal.cpp
4 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
10 Terminal::Terminal()
11 {
12 terminalId = UNKNOWN;
13 terminalType = UNKNOWN;
14 terminalStatus = UNKNOWN;
15 }
16
17 Terminal::~Terminal()
18 {
19 //...
20 }
21
22 int Terminal::HandleRunDiagnostics(const RunDiagnosticsMsg *pMsg)
23 {
24 int status = 1;
25 //...
26 return status;
27 }
28
29 int Terminal::HandleOutOfService()
30 {
31 int status = 1;
32 terminalStatus = OUT_OF_SERVICE;
33 //...
34 return status;
35 }
36
37 int Terminal::HandleInService()
38 {
39 int status = 1;
40 terminalStatus = INSERVICE;
41 //...
42 return status;
43 }
44
45 void Terminal::Activate(const TerminalCreateMsg *pMsg)
46 {
47 terminalId = pMsg->GetTerminalId();
48 terminalType = pMsg->GetTerminalType();
49 terminalStatus = pMsg->GetTerminalStatus();
50 //...
51
52 TerminalCreateAck *pAck = new TerminalCreateAck(terminalId, terminalStatus);
5 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
53 SendMessage(pAck);
54 }
55
56 void Terminal::Deactivate(const TerminalDeleteMsg *pMsg)
57 {
58 //...
59 terminalId = UNKNOWN;
60 terminalType = UNKNOWN;
61 terminalStatus = UNKNOWN;
62 //...
63 }
Msg.hpp
6 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
C Source Code
Terminal Manager and Terminal C header and source files are shown below.
The important points to note are:
TerminalManager
TerminalManager.h
1 /*
2 TerminalManager header file. We will be using this class
3 as an example for Object Oriented Programming in C.
4 */
5
6 #include "Terminal.h"
7
8 #define MAX_TERMINALS 500
9
10 /* Structure contains all data used by the Terminal Manager. */
11 typedef struct
7 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
12 {
13 Terminal terminals[MAX_TERMINALS];
14 } TerminalManager;
15
16 /*
17 ANSI C Function prototypes of all functions that operate
18 on the TerminalManager structure.
19 */
20 void TerminalManager_Construct(TerminalManager *pMgr);
21 void TerminalManager_Destroy(TerminalManager *pMgr);
22 void TerminalManager_HandleMessage(TerminalManager *pMgr, Msg* pMsg);
TerminalManager.c
1 /*
2 TerminalManager source file. We will be using this class
3 as an example for Object Oriented Programming in C.
4 */
5
6 #include <stdio.h>
7 #include "TerminalManager.h"
8 #include "Msg.h"
9 #include <stdlib.h>
10
11 Terminal *TerminalManager_FindTerminal(TerminalManager *pMgr, int terminalId)
12 {
13 if (terminalId < MAX_TERMINALS)
14 {
15 return (&(pMgr->terminals[terminalId]));
16 }
17 else
18 {
19 return NULL;
20 }
21 }
22
23 void TerminalManager_Construct(TerminalManager *pMgr)
24 {
25 int i;
26 /*
27 C will not call construction functions, so loop through all call the
28 construction functions for all terminals.
8 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
29 */
30 for (i=0; i < MAX_TERMINALS; i++)
31 {
32 Terminal_Construct(&(pMgr->terminals[i]));
33 }
34 }
35
36 void TerminalManager_Destroy(TerminalManager *pMgr)
37 {
38 int i;
39 /*
40 C will not call destruction functions, so loop through all call the
41 destruction functions for all terminals.
42 */
43 for (i=0; i < MAX_TERMINALS; i++)
44 {
45 Terminal_Destroy(&(pMgr->terminals[i]));
46 }
47 }
48
49 void TerminalManager_HandleMessage(TerminalManager *pMgr, Msg* pMsg)
50 {
51 int status, status1;
52
53 int terminalId = pMsg->terminalId;
54
55 Terminal *pTerm = TerminalManager_FindTerminal(pMgr, terminalId);
56 Terminal *pOtherTerm = NULL;
57
58 /*
59 Switch on the message type and invoke the Terminal's message handlers for
60 the terminal specified in the message. Here the terminal manager takes
61 care of indexing into the terminal structure and it passes the pointer
62 to the terminal handler functions. Due to this design, implementation
63 of the terminal handler functions just focus on handling the specified
64 terminal.
65 */
66 if (pTerm != NULL)
67 {
68 switch (pMsg->msgType)
69 {
70 case CREATE_TERMINAL:
71 Terminal_Activate(pTerm, (const TerminalCreateMsg *)pMsg);
9 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
72 break;
73 case DELETE_TERMINAL:
74 Terminal_Deactivate(pTerm, (const TerminalDeleteMsg *) pMsg);
75 break;
76 case RUN_DIAGNOSTICS:
77 status = Terminal_HandleRunDiagnostics(pTerm, (const RunDiagnosticsMsg *) pMsg);
78 break;
79 case PERFORM_SWITCHOVER:
80 pOtherTerm = TerminalManager_FindTerminal(pMgr, pMsg->otherTerminalId);
81 status = Terminal_HandleOutOfService(pTerm);
82 status1 = Terminal_HandleInService(pOtherTerm);
83 break;
84 }
85 }
86 free(pMsg);
87 }
Terminal
Terminal.h
10 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
22 */
23
24 void Terminal_Activate(Terminal *pTerm, const TerminalCreateMsg *pMsg);
25 void Terminal_Deactivate(Terminal *pTerm, const TerminalDeleteMsg *pMsg);
26 int Terminal_HandleRunDiagnostics(Terminal *pTerm, const RunDiagnosticsMsg *pMsg);
27 int Terminal_HandleOutOfService(Terminal *pTerm);
28 int Terminal_HandleInService(Terminal *pTerm);
29 void Terminal_Construct(Terminal *pTerm);
30 void Terminal_Destroy(Terminal *pTerm);
Terminal.c
11 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
31 int status = 1;
32 /*...*/
33 return status;
34 }
35
36 int Terminal_HandleOutOfService(Terminal *pTerm)
37 {
38 int status = 1;
39 pTerm->terminalStatus = OUT_OF_SERVICE;
40 /*...*/
41 return status;
42 }
43
44 int Terminal_HandleInService(Terminal *pTerm)
45 {
46 int status = 1;
47 pTerm->terminalStatus = INSERVICE;
48 /*...*/
49 return status;
50 }
51
52 void Terminal_Activate(Terminal *pTerm, const TerminalCreateMsg *pMsg)
53 {
54 TerminalCreateAck *pAck;
55 pTerm->terminalId = pMsg->header.terminalId;
56 pTerm->terminalType = pMsg->header.terminalType;
57 pTerm->terminalStatus = pMsg->header.terminalStatus;
58 /*...*/
59
60 pAck = (TerminalCreateAck *)malloc(sizeof(TerminalCreateAck));
61 pAck->header.terminalId = pTerm->terminalId;
62 pAck->header.terminalStatus = pTerm->terminalStatus;
63
64 Terminal_SendMessage(pTerm, (Msg *)pAck);
65 }
66
67 void Terminal_Deactivate(Terminal *pTerm, const TerminalDeleteMsg *pMsg)
68 {
69 /*...*/
70 pTerm->terminalId = UNKNOWN;
71 pTerm->terminalType = UNKNOWN;
72 pTerm->terminalStatus = UNKNOWN;
73 /*...*/
12 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
74 }
Msg.h
13 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
39 Msg header;
40 } TerminalDeleteMsg;
41
42
43 typedef struct
44 {
45 Msg header;
46 } RunDiagnosticsMsg;
47 #endif
Explore More
Compare C++ and the equivalent C code for class declarations and
method invocation
Compare C++ and the equivalent C code for inheritance and virtual
functions.
EventStudio
VisualEther
Wireshark gallery
visualize Wireshark
auto diagnose
select fields
download free trial
Telecom+networking
Software Design
14 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...
Follow
medium
twitter
linkedin
facebook
Company
contact us
blog
15 of 15 16/02/2020 16:27