手机探索者开发实录—代码产生器(上)

手机探索者开发实录代码产生器(上)

 

转载时请注明出处和作者联系方式
作者联系方式:李先静 <xianjimli at hotmail dot com>


我喜欢C语言,因为它很简单。用C语言写程序时,通常不需要记忆什么,也不需要考虑语言本身的问题(当然对计算机原理应该比较熟悉才行)。不过有些地方用C语言来实现确实有点繁琐。比如在分离接口和实现时,我们先要声明一组函数指针,再定义一个结构,这个结构包含这组函数指针,再定义inline函数包装这组函数指针,在具体实现这个接口时,还要做些类似工作。

 

手机探索者中,所有接口的框架都是这样实现的。这样的代码写多了,人都会变傻,至少会变得机械,编程也会变得很无聊。正因为这样,我又喜欢上了awk,它经常帮我做些单调重复的工作,它甚至比C更简单,即使长时间不用它,只要看一下帮助,我也马上可以用它来编程。

 

这里我们用awk来写一个代码产生器,这里的代码产生器虽然有点复杂,不过它至少可以让我的工作更有趣一点,而且可以不同模块中重复使用,所以花点时间去写这个代码产生器完全值得。

 

接口描述(Mobile_Explorer.def)


  1. 0:MeRet:Mobile_Explorer:Get_Type_Info:MobileExplorer* thiz, MeType* type:
  2. 1:MeRet:Mobile_Explorer:Get_Device_Info:MobileExplorer* thiz, MeDeviceInfo* info:
  3. 2:MeRet:Mobile_Explorer:Auth:MobileExplorer* thiz, const char* user, const char* passwd:
  4. 3:MeRet:Mobile_Explorer:Del_Objects:MobileExplorer* thiz, MePath* path:
  5. 4:MeRet:Mobile_Explorer:Add_Objects:MobileExplorer* thiz, MeObjects* objs:
  6. 5:MeRet:Mobile_Explorer:Update_Objects:MobileExplorer* thiz, MeObjects* objs:
  7. 6:MeRet:Mobile_Explorer:Get_Objects:MobileExplorer* thiz, MePath* path, MeObjects* hdrs:
  8. 7:MeRet:Mobile_Explorer:Get_ObjectsHdr:MobileExplorer* thiz, MePath* path, MeObjectsHdr* hdrs:
  9. 8:MeRet:Mobile_Explorer:Get_ObjectsAttr:MobileExplorer* thiz, MePath* path, MeObjectsAttr* attr:
  10. 9:MeRet:Mobile_Explorer:IME_Commit:MobileExplorer* thiz, const char* text:
  11. 10:MeRet:Mobile_Explorer:Clipboard_Set_Data:MobileExplorer* thiz, MeData* data:
  12. 11:MeRet:Mobile_Explorer:Clipboard_Get_Data:MobileExplorer* thiz, MeData* data:
  13. 12:MeRet:Mobile_Explorer:Reg_Listener:MobileExplorer* thiz, MobileExplorerListener* listener:
  14. 13:MeRet:Mobile_Explorer:Destroy:MobileExplorer* thiz:


产生的代码


接口定义(mobile_explorer.h)


  1. #include "mobile_explorer_types.h"
  2. #include "mobile_explorer_listener.h"
  3.  
  4. #ifndef MOBILE_EXPLORER_H
  5. #define MOBILE_EXPLORER_H
  6.  
  7. ME_DECLS_BEGIN
  8.  
  9. struct _MobileExplorer;
  10. typedef struct _MobileExplorer MobileExplorer;
  11.  
  12. typedef MeRet (*MobileExplorerGetTypeInfoFunc)(MobileExplorer* thiz, MeType* type);
  13. typedef MeRet (*MobileExplorerGetDeviceInfoFunc)(MobileExplorer* thiz, MeDeviceInfo* info);
  14. typedef MeRet (*MobileExplorerAuthFunc)(MobileExplorer* thiz, const char* user, const char* passwd);
  15. typedef MeRet (*MobileExplorerDelObjectsFunc)(MobileExplorer* thiz, MePath* path);
  16. typedef MeRet (*MobileExplorerAddObjectsFunc)(MobileExplorer* thiz, MeObjects* objs);
  17. typedef MeRet (*MobileExplorerUpdateObjectsFunc)(MobileExplorer* thiz, MeObjects* objs);
  18. typedef MeRet (*MobileExplorerGetObjectsFunc)(MobileExplorer* thiz, MePath* path, MeObjects* hdrs);
  19. typedef MeRet (*MobileExplorerGetObjectsHdrFunc)(MobileExplorer* thiz, MePath* path, MeObjectsHdr* hdrs);
  20. typedef MeRet (*MobileExplorerGetObjectsAttrFunc)(MobileExplorer* thiz, MePath* path, MeObjectsAttr* attr);
  21. typedef MeRet (*MobileExplorerIMECommitFunc)(MobileExplorer* thiz, const char* text);
  22. typedef MeRet (*MobileExplorerClipboardSetDataFunc)(MobileExplorer* thiz, MeData* data);
  23. typedef MeRet (*MobileExplorerClipboardGetDataFunc)(MobileExplorer* thiz, MeData* data);
  24. typedef MeRet (*MobileExplorerRegListenerFunc)(MobileExplorer* thiz, MobileExplorerListener* listener);
  25. typedef MeRet (*MobileExplorerDestroyFunc)(MobileExplorer* thiz);
  26.  
  27. struct _MobileExplorer
  28. {
  29.          MobileExplorerGetTypeInfoFunc get_type_info;
  30.          MobileExplorerGetDeviceInfoFunc get_device_info;
  31.          MobileExplorerAuthFunc auth;
  32.          MobileExplorerDelObjectsFunc del_objects;
  33.          MobileExplorerAddObjectsFunc add_objects;
  34.          MobileExplorerUpdateObjectsFunc update_objects;
  35.          MobileExplorerGetObjectsFunc get_objects;
  36.          MobileExplorerGetObjectsHdrFunc get_objectshdr;
  37.          MobileExplorerGetObjectsAttrFunc get_objectsattr;
  38.          MobileExplorerIMECommitFunc ime_commit;
  39.          MobileExplorerClipboardSetDataFunc clipboard_set_data;
  40.          MobileExplorerClipboardGetDataFunc clipboard_get_data;
  41.          MobileExplorerRegListenerFunc reg_listener;
  42.          MobileExplorerDestroyFunc destroy;
  43.  
  44.          char priv[0];
  45. };
  46.  
  47. static inline MeRet mobile_explorer_get_type_info(MobileExplorer* thiz, MeType* type)
  48. {
  49.          me_return_val_if_fail(thiz != NULL && thiz->get_type_info != NULL, ME_RET_PARAMS);
  50.  
  51.          return thiz->get_type_info(thiz, type);
  52. }
  53.  
  54. static inline MeRet mobile_explorer_get_device_info(MobileExplorer* thiz, MeDeviceInfo* info)
  55. {
  56.          me_return_val_if_fail(thiz != NULL && thiz->get_device_info != NULL, ME_RET_PARAMS);
  57.  
  58.          return thiz->get_device_info(thiz, info);
  59. }
  60.  
  61. static inline MeRet mobile_explorer_auth(MobileExplorer* thiz, const char* user, const char* passwd)
  62. {
  63.          me_return_val_if_fail(thiz != NULL && thiz->auth != NULL, ME_RET_PARAMS);
  64.  
  65.          return thiz->auth(thiz, user, passwd);
  66. }
  67.  
  68. static inline MeRet mobile_explorer_del_objects(MobileExplorer* thiz, MePath* path)
  69. {
  70.          me_return_val_if_fail(thiz != NULL && thiz->del_objects != NULL, ME_RET_PARAMS);
  71.  
  72.          return thiz->del_objects(thiz, path);
  73. }
  74.  
  75. static inline MeRet mobile_explorer_add_objects(MobileExplorer* thiz, MeObjects* objs)
  76. {
  77.          me_return_val_if_fail(thiz != NULL && thiz->add_objects != NULL, ME_RET_PARAMS);
  78.  
  79.          return thiz->add_objects(thiz, objs);
  80. }
  81.  
  82. static inline MeRet mobile_explorer_update_objects(MobileExplorer* thiz, MeObjects* objs)
  83. {
  84.          me_return_val_if_fail(thiz != NULL && thiz->update_objects != NULL, ME_RET_PARAMS);
  85.  
  86.          return thiz->update_objects(thiz, objs);
  87. }
  88.  
  89. static inline MeRet mobile_explorer_get_objects(MobileExplorer* thiz, MePath* path, MeObjects* hdrs)
  90. {
  91.          me_return_val_if_fail(thiz != NULL && thiz->get_objects != NULL, ME_RET_PARAMS);
  92.  
  93.          return thiz->get_objects(thiz, path, hdrs);
  94. }
  95.  
  96. static inline MeRet mobile_explorer_get_objectshdr(MobileExplorer* thiz, MePath* path, MeObjectsHdr* hdrs)
  97. {
  98.          me_return_val_if_fail(thiz != NULL && thiz->get_objectshdr != NULL, ME_RET_PARAMS);
  99.  
  100.          return thiz->get_objectshdr(thiz, path, hdrs);
  101. }
  102.  
  103. static inline MeRet mobile_explorer_get_objectsattr(MobileExplorer* thiz, MePath* path, MeObjectsAttr* attr)
  104. {
  105.          me_return_val_if_fail(thiz != NULL && thiz->get_objectsattr != NULL, ME_RET_PARAMS);
  106.  
  107.          return thiz->get_objectsattr(thiz, path, attr);
  108. }
  109.  
  110. static inline MeRet mobile_explorer_ime_commit(MobileExplorer* thiz, const char* text)
  111. {
  112.          me_return_val_if_fail(thiz != NULL && thiz->ime_commit != NULL, ME_RET_PARAMS);
  113.  
  114.          return thiz->ime_commit(thiz, text);
  115. }
  116.  
  117. static inline MeRet mobile_explorer_clipboard_set_data(MobileExplorer* thiz, MeData* data)
  118. {
  119.          me_return_val_if_fail(thiz != NULL && thiz->clipboard_set_data != NULL, ME_RET_PARAMS);
  120.  
  121.          return thiz->clipboard_set_data(thiz, data);
  122. }
  123.  
  124. static inline MeRet mobile_explorer_clipboard_get_data(MobileExplorer* thiz, MeData* data)
  125. {
  126.          me_return_val_if_fail(thiz != NULL && thiz->clipboard_get_data != NULL, ME_RET_PARAMS);
  127.  
  128.          return thiz->clipboard_get_data(thiz, data);
  129. }
  130.  
  131. static inline MeRet mobile_explorer_reg_listener(MobileExplorer* thiz, MobileExplorerListener* listener)
  132. {
  133.          me_return_val_if_fail(thiz != NULL && thiz->reg_listener != NULL, ME_RET_PARAMS);
  134.  
  135.          return thiz->reg_listener(thiz, listener);
  136. }
  137.  
  138. static inline MeRet mobile_explorer_destroy(MobileExplorer* thiz)
  139. {
  140.          me_return_val_if_fail(thiz != NULL && thiz->destroy != NULL, ME_RET_PARAMS);
  141.  
  142.          return thiz->destroy(thiz);
  143. }
  144.  
  145. ME_DECLS_END
  146.  
  147. #endif/*MOBILE_EXPLORER_H*/
  148.  

  149.  
接口实现头文件(mobile_explorer_host_xml.h)


  1. #include <mobile_explorer.h>
  2. #ifndef MOBILE_EXPLORER_HOST_XML_H
  3. #define MOBILE_EXPLORER_HOST_XML_H
  4. ME_DECLS_BEGIN
  5. MobileExplorer* mobile_explorer_host_xml_create();
  6. ME_DECLS_END
  7. #endif/*MOBILE_EXPLORER_HOST_XML_H*/


接口实现C文件(mobile_explorer_host_xml.c)


  1. #include <mobile_explorer_host_xml.h>
  2.  
  3. typedef struct _PrivInfo
  4. {
  5.     unsigned int dummy;
  6. }PrivInfo;
  7.  
  8. static MeRet mobile_explorer_host_xml_get_type_info(MobileExplorer* thiz, MeType* type)
  9. {
  10.     me_return_val_if_fail(thiz != NULL, ME_RET_PARAMS);
  11.  
  12.     PrivInfo* priv = (PrivInfo*)thiz->priv;
  13.  
  14.     return (MeRet)0;
  15. }
  16.  
  17. static MeRet mobile_explorer_host_xml_get_device_info(MobileExplorer* thiz, MeDeviceInfo* info)
  18. {
  19.     me_return_val_if_fail(thiz != NULL, ME_RET_PARAMS);
  20.  
  21.     PrivInfo* priv = (PrivInfo*)thiz->priv;
  22.  
  23.     return (MeRet)0;
  24. }
  25. static MeRet mobile_explorer_host_xml_destroy(MobileExplorer* thiz)
  26. {
  27.     me_return_val_if_fail(thiz != NULL, ME_RET_PARAMS);
  28.  
  29.     PrivInfo* priv = (PrivInfo*)thiz->priv;
  30.  
  31.     return (MeRet)0;
  32. }
  33.  
  34. MobileExplorer* mobile_explorer_host_xml_create()
  35. {
  36.     MobileExplorer* thiz = (MobileExplorer*)malloc(sizeof(MobileExplorer) + sizeof(PrivInfo));
  37.  
  38.     if(thiz != NULL)
  39.     {
  40.         thiz->get_type_info = mobile_explorer_host_xml_get_type_info;
  41.         thiz->get_device_info = mobile_explorer_host_xml_get_device_info;
  42.         thiz->auth = mobile_explorer_host_xml_auth;
  43.         thiz->del_objects = mobile_explorer_host_xml_del_objects;
  44.         thiz->add_objects = mobile_explorer_host_xml_add_objects;
  45.         thiz->update_objects = mobile_explorer_host_xml_update_objects;
  46.         thiz->get_objects = mobile_explorer_host_xml_get_objects;
  47.         thiz->get_objectshdr = mobile_explorer_host_xml_get_objectshdr;
  48.         thiz->get_objectsattr = mobile_explorer_host_xml_get_objectsattr;
  49.         thiz->ime_commit = mobile_explorer_host_xml_ime_commit;
  50.         thiz->clipboard_set_data = mobile_explorer_host_xml_clipboard_set_data;
  51.         thiz->clipboard_get_data = mobile_explorer_host_xml_clipboard_get_data;
  52.         thiz->reg_listener = mobile_explorer_host_xml_reg_listener;
  53.         thiz->destroy = mobile_explorer_host_xml_destroy;
  54.     }
  55.  
  56.     return thiz;
  57. }
~~待续~~

原文地址:https://www.cnblogs.com/zhangyunlin/p/6167622.html