fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <functional>
  4. #include <memory>
  5.  
  6. using namespace std;
  7. using namespace std::placeholders;
  8.  
  9. typedef enum { TCP, SERIAL, CONNECT_COUNT} CONNECT_TYPE;
  10.  
  11. // Define interface
  12. class IConnector
  13. {
  14. public:
  15. virtual void open() = 0;
  16. virtual void recv(void* buffer, size_t size) = 0;
  17. virtual void send(void* buffer, size_t size) = 0;
  18. virtual void close() = 0;
  19. };
  20.  
  21. // Define CSerial
  22. class CSerial /*: public IConnector*/ // From 3rd?
  23. {
  24. public:
  25. void open();
  26. void recv(void* buffer, size_t size);
  27. void send(void* buffer, size_t size);
  28. void close();
  29. };
  30.  
  31. // Define CTcp
  32. class CTcp /*: public IConnector*/ // From 3rd?
  33. {
  34. public:
  35. void open();
  36. void recv(void* buffer, size_t size);
  37. void send(void* buffer, size_t size);
  38. void close();
  39. };
  40.  
  41. typedef void (OPEN_FUNC)();
  42. typedef void (RECV_FUNC)(void*, size_t);
  43. typedef void (SEND_FUNC)(void*, size_t);
  44. typedef void (CLOSE_FUNC)();
  45.  
  46. // Define CConnector
  47. class CConnector: public IConnector
  48. {
  49. public:
  50. CConnector();
  51. void open();
  52. void recv(void* buffer, size_t size);
  53. void send(void* buffer, size_t size);
  54. void close();
  55. void setType(CONNECT_TYPE type);
  56.  
  57. private:
  58. template <class T>
  59. void bindConnector(T* connector);
  60. CONNECT_TYPE mType;
  61. CTcp mTcp;
  62. CSerial mSerial;
  63. std::function<OPEN_FUNC> openFunc;
  64. std::function<RECV_FUNC> recvFunc;
  65. std::function<SEND_FUNC> sendFunc;
  66. std::function<CLOSE_FUNC> closeFunc;
  67. };
  68.  
  69. // Implement CSerial
  70. void CSerial::open()
  71. {
  72. cout << "CSerial::" << __func__ << endl;
  73. }
  74.  
  75. void CSerial::recv(void* buffer, size_t size)
  76. {
  77. cout << "CSerial::" << __func__ << endl;
  78. }
  79.  
  80. void CSerial::send(void* buffer, size_t size)
  81. {
  82. cout << "CSerial::" << __func__ << endl;
  83. }
  84.  
  85. void CSerial::close()
  86. {
  87. cout << "CSerial::" << __func__ << endl;
  88. }
  89.  
  90. // Implement CTcp
  91. void CTcp::open()
  92. {
  93. cout << "CTcp::" << __func__ << endl;
  94. }
  95.  
  96. void CTcp::recv(void* buffer, size_t size)
  97. {
  98. cout << "CTcp::" << __func__ << endl;
  99. }
  100.  
  101. void CTcp::send(void* buffer, size_t size)
  102. {
  103. cout << "CTcp::" << __func__ << endl;
  104. }
  105.  
  106. void CTcp::close()
  107. {
  108. cout << "CTcp::" << __func__ << endl;
  109. }
  110.  
  111. // Implement CConnector
  112. CConnector::CConnector(): mTcp(), mSerial(), openFunc(), recvFunc(), sendFunc(), closeFunc()
  113. {
  114. }
  115.  
  116. void CConnector::open()
  117. {
  118. if(openFunc != NULL)
  119. openFunc();
  120. }
  121.  
  122. void CConnector::recv(void* buffer, size_t size)
  123. {
  124. if(recvFunc != NULL)
  125. recvFunc(buffer, size);
  126. }
  127.  
  128. void CConnector::send(void* buffer, size_t size)
  129. {
  130. if(sendFunc != NULL)
  131. sendFunc(buffer, size);
  132. }
  133.  
  134. void CConnector::close()
  135. {
  136. if(closeFunc != NULL)
  137. closeFunc();
  138. }
  139.  
  140. template <class T>
  141. void CConnector::bindConnector(T* connector)
  142. {
  143. openFunc = std::bind(&T::open, connector);
  144. recvFunc = std::bind(&T::recv, connector, _1, _2);
  145. sendFunc = std::bind(&T::send, connector, _1, _2);
  146. closeFunc = std::bind(&T::close, connector);
  147. }
  148.  
  149. void CConnector::setType(CONNECT_TYPE type)
  150. {
  151. mType = type;
  152. switch(mType)
  153. {
  154. case CONNECT_TYPE::SERIAL:
  155. bindConnector(&mTcp);
  156. break;
  157. case CONNECT_TYPE::TCP:
  158. bindConnector(&mTcp);
  159. break;
  160. default:
  161. throw "error";
  162. }
  163. }
  164.  
  165. void testTCP()
  166. {
  167. CConnector* conector = new CConnector();
  168. conector->setType(CONNECT_TYPE::TCP);
  169. conector->open();
  170. conector->recv(NULL, 0);
  171. conector->send(NULL, 0);
  172. conector->close();
  173. delete conector;
  174. cout << endl;
  175. }
  176.  
  177. void testSerial()
  178. {
  179. CConnector* conector = new CConnector();
  180. conector->setType(CONNECT_TYPE::SERIAL);
  181. conector->open();
  182. conector->recv(NULL, 0);
  183. conector->send(NULL, 0);
  184. conector->close();
  185. delete conector;
  186. cout << endl;
  187. }
  188.  
  189. int main(int argc, char* argv[])
  190. {
  191. testTCP();
  192. testSerial();
  193.  
  194. return 0;
  195. }
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
CTcp::open
CTcp::recv
CTcp::send
CTcp::close

CTcp::open
CTcp::recv
CTcp::send
CTcp::close