Object Oriented Programming in C

Embedded software development is slowly moving towards object orientedanalysis, designand programming. The introduction of object oriented technologies in somesystems has not happened due to lack of C++ support on some platforms.

This article focuses on platforms where C++ compilers are not available. Thedevelopers working on these platforms should still be able to use objectoriented analysis and design. When it comes to final code development they canuse C.

The following sections cover an example of C++ code and its implementation inC.

  • C++ Source Code: C++ source files implementing TerminalManager and Terminal classes.
  • C Source Code: TerminalManager and Terminal implemented in C for a platform that does not support C++.

C++ Source Code

Terminal Manager and Terminal C++ header and source files are shownbelow: 

Terminal Manager

TerminalManager.hpp

 
 
  1. // TerminalManager header file. We will be using this class
  2. // as an example for Object Oriented Programming in C.
  3. #include "Terminal.hpp"
  4.  
  5. class TerminalManager
  6. {
  7. private:
  8.     enum {MAX_TERMINALS=500};
  9.  
  10.     Terminal terminals[MAX_TERMINALS];
  11.  
  12.     Terminal *FindTerminal(int terminalId);
  13.  
  14. public:
  15.     TerminalManager();
  16.     ~TerminalManager();
  17.     void HandleMessage(Msg* pMsg);
  18. };

TerminalManager.cpp

 
 
  1. // TerminalManager source file. We will be using this class
  2. // as an example for Object Oriented Programming in C.
  3.  
  4. #include <stdio.h>
  5. #include "TerminalManager.hpp"
  6. #include "Msg.hpp"
  7.  
  8. TerminalManager::TerminalManager()
  9.  
  10. {
  11.    //...
  12. }
  13.  
  14. TerminalManager::~TerminalManager()
  15. {
  16. }
  17.  
  18. void TerminalManager::HandleMessage(Msg* pMsg)
  19. {
  20.     int status, status1;
  21.  
  22.     int terminalId = pMsg->GetTerminalId();
  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

Terminal.hpp

 
 
  1. // Terminal class header file.
  2.  
  3. // Forward declaration for messages
  4. class TerminalCreateMsg;
  5. class TerminalDeleteMsg;
  6. class RunDiagnosticsMsg;
  7. class Msg;
  8.  
  9. // Terminal class
  10. class Terminal
  11. {
  12.     enum { UNKNOWN = 0 };
  13.     enum {OUT_OF_SERVICE=1, INSERVICE=2};
  14.     //...
  15.     int terminalId;
  16.     int terminalType;
  17.     int terminalStatus;
  18.     void SendMessage(Msg *pMsg);
  19.  
  20. public:
  21.     void Activate(const TerminalCreateMsg *pMsg);
  22.     void Deactivate(const TerminalDeleteMsg *pMsg);
  23.     int HandleRunDiagnostics(const RunDiagnosticsMsg *pMsg);
  24.     int HandleOutOfService();
  25.     int HandleInService();
  26.     Terminal();
  27.     ~Terminal();
  28. };

Terminal.cpp

 
 
  1. // Terminal class source file.
  2.  
  3. #include "Terminal.hpp"
  4. #include "Msg.hpp"
  5. void Terminal::SendMessage(Msg *pMsg)
  6. {
  7.    //...
  8. }
  9.  
  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);
  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

 
 
  1. // Messages used by the Terminal and TerminalManager classes.
  2.  
  3. enum MsgType
  4. {
  5.     CREATE_TERMINAL,
  6.     DELETE_TERMINAL,
  7.     RUN_DIAGNOSTICS,
  8.     PERFORM_SWITCHOVER
  9. };
  10.  
  11.  
  12. class Msg
  13. {
  14.     //...
  15.     int msgType;
  16.     int terminalType;
  17.     int terminalId;
  18.     int otherTerminalId;
  19.     int terminalStatus;
  20.  
  21. public:
  22.     MsgType GetType() const;
  23.     int GetTerminalId() const;
  24.     int GetOtherTerminalId() const;
  25.     int GetTerminalType() const;
  26. };
  27.  
  28. // Message used to create a terminal
  29. class TerminalCreateMsg : public Msg
  30. {
  31. public:
  32.     int GetTerminalStatus() const;
  33. };
  34.  
  35. // Acknowledgement to Terminal Create message.
  36. class TerminalCreateAck : public Msg
  37. {
  38. public:
  39.     TerminalCreateAck(int terminalId, int terminalStatus);
  40. };
  41.  
  42. // Terminal Delete message
  43. class TerminalDeleteMsg : public Msg
  44. {
  45. };

C Source Code

Terminal Manager and Terminal C header and source files are shown below. Theimportant points to note are:

  • Data members of a class map to C structures
  • Methods of a class map to C functions with the "data member" structure pointer as the first parameter.

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
  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.
  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);
  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

 
 
  1. /* Terminal struct header file. */
  2.  
  3. #include "Msg.h"
  4.  
  5. #define UNKNOWN 0
  6. #define OUT_OF_SERVICE 1
  7. #define INSERVICE 2
  8.  
  9. /* Terminal struct */
  10. typedef struct
  11. {
  12.     /*...*/
  13.     int terminalId;
  14.     int terminalType;
  15.     int terminalStatus;
  16. } Terminal;
  17.  
  18. /*
  19. Prototypes for Terminal structure related functions. Helper
  20. functions needed by these functions are marked static are not
  21. included here.
  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

 
 
  1. /* Terminal struct source file. */
  2.  
  3. #include "Terminal.h"
  4. #include "Msg.h"
  5. #include <stdlib.h>
  6.  
  7. /*
  8. Terminal_SendMessage is not visible to outside the Terminal.c file.
  9. This is equivalent to a private method in C++.
  10. */
  11.  
  12. static void Terminal_SendMessage(Terminal *pTerm, Msg *pMsg)
  13. {
  14.     /*...*/
  15. }
  16.  
  17. void Terminal_Construct(Terminal *pTerm)
  18. {  
  19.     pTerm->terminalId = UNKNOWN;
  20.     pTerm->terminalType = UNKNOWN;
  21.     pTerm->terminalStatus = UNKNOWN;
  22. }
  23.  
  24. void Terminal_Destroy(Terminal *pTerm)
  25. {
  26.     /*...*/
  27. }
  28.  
  29. int Terminal_HandleRunDiagnostics(Terminal *pTerm, const RunDiagnosticsMsg *pMsg)
  30. {
  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.     /*...*/
  74. }

Msg.h

 
 
  1. /* Messages used by the Terminal and TerminalManager classes. */
  2.  
  3. #ifndef MSG_H
  4. #define MSG_H
  5.  
  6. enum MsgType
  7. {
  8.     CREATE_TERMINAL,
  9.     DELETE_TERMINAL,
  10.     RUN_DIAGNOSTICS,
  11.     PERFORM_SWITCHOVER
  12. };
  13.  
  14. typedef struct
  15. {
  16.     /*...*/
  17.     int msgType;
  18.     int terminalType;
  19.     int terminalId;
  20.     int otherTerminalId;
  21.     int terminalStatus;
  22. } Msg;
  23.  
  24. /* Message used to create a terminal. */
  25. typedef struct
  26. {
  27.     Msg header;
  28. }TerminalCreateMsg;
  29.  
  30. /* Acknowledgement to Terminal Create message. */
  31. typedef struct
  32. {
  33.     Msg header;
  34. } TerminalCreateAck;
  35.  
  36. /* Terminal Delete message */
  37. typedef struct
  38. {
  39.     Msg header;
  40. } TerminalDeleteMsg;
  41.  
  42.  
  43. typedef struct
  44. {
  45.     Msg header;
  46. } RunDiagnosticsMsg;
  47. #endif

Explore More


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值