rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cjim%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">
接口实现头文件(mobile_explorer_host_xml.h)
~~待续~~
手机探索者开发实录—代码产生器(上)
转载时请注明出处和作者联系方式
作者联系方式:李先静 <xianjimli at hotmail dot com>
我喜欢C语言,因为它很简单。用C语言写程序时,通常不需要记忆什么,也不需要考虑语言本身的问题(当然对计算机原理应该比较熟悉才行)。不过有些地方用C语言来实现确实有点繁琐。比如在分离接口和实现时,我们先要声明一组函数指针,再定义一个结构,这个结构包含这组函数指针,再定义inline函数包装这组函数指针,在具体实现这个接口时,还要做些类似工作。
在手机探索者中,所有接口的框架都是这样实现的。这样的代码写多了,人都会变傻,至少会变得机械,编程也会变得很无聊。正因为这样,我又喜欢上了awk,它经常帮我做些单调重复的工作,它甚至比C更简单,即使长时间不用它,只要看一下帮助,我也马上可以用它来编程。
这里我们用awk来写一个代码产生器,这里的代码产生器虽然有点复杂,不过它至少可以让我的工作更有趣一点,而且可以不同模块中重复使用,所以花点时间去写这个代码产生器完全值得。
接口描述(Mobile_Explorer.def)
- 0:MeRet:Mobile_Explorer:Get_Type_Info:MobileExplorer* thiz, MeType* type:
- 1:MeRet:Mobile_Explorer:Get_Device_Info:MobileExplorer* thiz, MeDeviceInfo* info:
- 2:MeRet:Mobile_Explorer:Auth:MobileExplorer* thiz, const char* user, const char* passwd:
- 3:MeRet:Mobile_Explorer:Del_Objects:MobileExplorer* thiz, MePath* path:
- 4:MeRet:Mobile_Explorer:Add_Objects:MobileExplorer* thiz, MeObjects* objs:
- 5:MeRet:Mobile_Explorer:Update_Objects:MobileExplorer* thiz, MeObjects* objs:
- 6:MeRet:Mobile_Explorer:Get_Objects:MobileExplorer* thiz, MePath* path, MeObjects* hdrs:
- 7:MeRet:Mobile_Explorer:Get_ObjectsHdr:MobileExplorer* thiz, MePath* path, MeObjectsHdr* hdrs:
- 8:MeRet:Mobile_Explorer:Get_ObjectsAttr:MobileExplorer* thiz, MePath* path, MeObjectsAttr* attr:
- 9:MeRet:Mobile_Explorer:IME_Commit:MobileExplorer* thiz, const char* text:
- 10:MeRet:Mobile_Explorer:Clipboard_Set_Data:MobileExplorer* thiz, MeData* data:
- 11:MeRet:Mobile_Explorer:Clipboard_Get_Data:MobileExplorer* thiz, MeData* data:
- 12:MeRet:Mobile_Explorer:Reg_Listener:MobileExplorer* thiz, MobileExplorerListener* listener:
- 13:MeRet:Mobile_Explorer:Destroy:MobileExplorer* thiz:
产生的代码
接口定义(mobile_explorer.h)
- #include "mobile_explorer_types.h"
- #include "mobile_explorer_listener.h"
- #ifndef MOBILE_EXPLORER_H
- #define MOBILE_EXPLORER_H
- ME_DECLS_BEGIN
- struct _MobileExplorer;
- typedef struct _MobileExplorer MobileExplorer;
- typedef MeRet (*MobileExplorerGetTypeInfoFunc)(MobileExplorer* thiz, MeType* type);
- typedef MeRet (*MobileExplorerGetDeviceInfoFunc)(MobileExplorer* thiz, MeDeviceInfo* info);
- typedef MeRet (*MobileExplorerAuthFunc)(MobileExplorer* thiz, const char* user, const char* passwd);
- typedef MeRet (*MobileExplorerDelObjectsFunc)(MobileExplorer* thiz, MePath* path);
- typedef MeRet (*MobileExplorerAddObjectsFunc)(MobileExplorer* thiz, MeObjects* objs);
- typedef MeRet (*MobileExplorerUpdateObjectsFunc)(MobileExplorer* thiz, MeObjects* objs);
- typedef MeRet (*MobileExplorerGetObjectsFunc)(MobileExplorer* thiz, MePath* path, MeObjects* hdrs);
- typedef MeRet (*MobileExplorerGetObjectsHdrFunc)(MobileExplorer* thiz, MePath* path, MeObjectsHdr* hdrs);
- typedef MeRet (*MobileExplorerGetObjectsAttrFunc)(MobileExplorer* thiz, MePath* path, MeObjectsAttr* attr);
- typedef MeRet (*MobileExplorerIMECommitFunc)(MobileExplorer* thiz, const char* text);
- typedef MeRet (*MobileExplorerClipboardSetDataFunc)(MobileExplorer* thiz, MeData* data);
- typedef MeRet (*MobileExplorerClipboardGetDataFunc)(MobileExplorer* thiz, MeData* data);
- typedef MeRet (*MobileExplorerRegListenerFunc)(MobileExplorer* thiz, MobileExplorerListener* listener);
- typedef MeRet (*MobileExplorerDestroyFunc)(MobileExplorer* thiz);
- struct _MobileExplorer
- {
- MobileExplorerGetTypeInfoFunc get_type_info;
- MobileExplorerGetDeviceInfoFunc get_device_info;
- MobileExplorerAuthFunc auth;
- MobileExplorerDelObjectsFunc del_objects;
- MobileExplorerAddObjectsFunc add_objects;
- MobileExplorerUpdateObjectsFunc update_objects;
- MobileExplorerGetObjectsFunc get_objects;
- MobileExplorerGetObjectsHdrFunc get_objectshdr;
- MobileExplorerGetObjectsAttrFunc get_objectsattr;
- MobileExplorerIMECommitFunc ime_commit;
- MobileExplorerClipboardSetDataFunc clipboard_set_data;
- MobileExplorerClipboardGetDataFunc clipboard_get_data;
- MobileExplorerRegListenerFunc reg_listener;
- MobileExplorerDestroyFunc destroy;
- char priv[0];
- };
- static inline MeRet mobile_explorer_get_type_info(MobileExplorer* thiz, MeType* type)
- {
- me_return_val_if_fail(thiz != NULL && thiz->get_type_info != NULL, ME_RET_PARAMS);
- return thiz->get_type_info(thiz, type);
- }
- static inline MeRet mobile_explorer_get_device_info(MobileExplorer* thiz, MeDeviceInfo* info)
- {
- me_return_val_if_fail(thiz != NULL && thiz->get_device_info != NULL, ME_RET_PARAMS);
- return thiz->get_device_info(thiz, info);
- }
- static inline MeRet mobile_explorer_auth(MobileExplorer* thiz, const char* user, const char* passwd)
- {
- me_return_val_if_fail(thiz != NULL && thiz->auth != NULL, ME_RET_PARAMS);
- return thiz->auth(thiz, user, passwd);
- }
- static inline MeRet mobile_explorer_del_objects(MobileExplorer* thiz, MePath* path)
- {
- me_return_val_if_fail(thiz != NULL && thiz->del_objects != NULL, ME_RET_PARAMS);
- return thiz->del_objects(thiz, path);
- }
- static inline MeRet mobile_explorer_add_objects(MobileExplorer* thiz, MeObjects* objs)
- {
- me_return_val_if_fail(thiz != NULL && thiz->add_objects != NULL, ME_RET_PARAMS);
- return thiz->add_objects(thiz, objs);
- }
- static inline MeRet mobile_explorer_update_objects(MobileExplorer* thiz, MeObjects* objs)
- {
- me_return_val_if_fail(thiz != NULL && thiz->update_objects != NULL, ME_RET_PARAMS);
- return thiz->update_objects(thiz, objs);
- }
- static inline MeRet mobile_explorer_get_objects(MobileExplorer* thiz, MePath* path, MeObjects* hdrs)
- {
- me_return_val_if_fail(thiz != NULL && thiz->get_objects != NULL, ME_RET_PARAMS);
- return thiz->get_objects(thiz, path, hdrs);
- }
- static inline MeRet mobile_explorer_get_objectshdr(MobileExplorer* thiz, MePath* path, MeObjectsHdr* hdrs)
- {
- me_return_val_if_fail(thiz != NULL && thiz->get_objectshdr != NULL, ME_RET_PARAMS);
- return thiz->get_objectshdr(thiz, path, hdrs);
- }
- static inline MeRet mobile_explorer_get_objectsattr(MobileExplorer* thiz, MePath* path, MeObjectsAttr* attr)
- {
- me_return_val_if_fail(thiz != NULL && thiz->get_objectsattr != NULL, ME_RET_PARAMS);
- return thiz->get_objectsattr(thiz, path, attr);
- }
- static inline MeRet mobile_explorer_ime_commit(MobileExplorer* thiz, const char* text)
- {
- me_return_val_if_fail(thiz != NULL && thiz->ime_commit != NULL, ME_RET_PARAMS);
- return thiz->ime_commit(thiz, text);
- }
- static inline MeRet mobile_explorer_clipboard_set_data(MobileExplorer* thiz, MeData* data)
- {
- me_return_val_if_fail(thiz != NULL && thiz->clipboard_set_data != NULL, ME_RET_PARAMS);
- return thiz->clipboard_set_data(thiz, data);
- }
- static inline MeRet mobile_explorer_clipboard_get_data(MobileExplorer* thiz, MeData* data)
- {
- me_return_val_if_fail(thiz != NULL && thiz->clipboard_get_data != NULL, ME_RET_PARAMS);
- return thiz->clipboard_get_data(thiz, data);
- }
- static inline MeRet mobile_explorer_reg_listener(MobileExplorer* thiz, MobileExplorerListener* listener)
- {
- me_return_val_if_fail(thiz != NULL && thiz->reg_listener != NULL, ME_RET_PARAMS);
- return thiz->reg_listener(thiz, listener);
- }
- static inline MeRet mobile_explorer_destroy(MobileExplorer* thiz)
- {
- me_return_val_if_fail(thiz != NULL && thiz->destroy != NULL, ME_RET_PARAMS);
- return thiz->destroy(thiz);
- }
- ME_DECLS_END
- #endif/*MOBILE_EXPLORER_H*/
- #include <mobile_explorer.h>
- #ifndef MOBILE_EXPLORER_HOST_XML_H
- #define MOBILE_EXPLORER_HOST_XML_H
- ME_DECLS_BEGIN
- MobileExplorer* mobile_explorer_host_xml_create();
- ME_DECLS_END
- #endif/*MOBILE_EXPLORER_HOST_XML_H*/
接口实现C文件(mobile_explorer_host_xml.c)
- #include <mobile_explorer_host_xml.h>
- typedef struct _PrivInfo
- {
- unsigned int dummy;
- }PrivInfo;
- static MeRet mobile_explorer_host_xml_get_type_info(MobileExplorer* thiz, MeType* type)
- {
- me_return_val_if_fail(thiz != NULL, ME_RET_PARAMS);
- PrivInfo* priv = (PrivInfo*)thiz->priv;
- return (MeRet)0;
- }
- static MeRet mobile_explorer_host_xml_get_device_info(MobileExplorer* thiz, MeDeviceInfo* info)
- {
- me_return_val_if_fail(thiz != NULL, ME_RET_PARAMS);
- PrivInfo* priv = (PrivInfo*)thiz->priv;
- return (MeRet)0;
- }
- …
- static MeRet mobile_explorer_host_xml_destroy(MobileExplorer* thiz)
- {
- me_return_val_if_fail(thiz != NULL, ME_RET_PARAMS);
- PrivInfo* priv = (PrivInfo*)thiz->priv;
- return (MeRet)0;
- }
- MobileExplorer* mobile_explorer_host_xml_create()
- {
- MobileExplorer* thiz = (MobileExplorer*)malloc(sizeof(MobileExplorer) + sizeof(PrivInfo));
- if(thiz != NULL)
- {
- thiz->get_type_info = mobile_explorer_host_xml_get_type_info;
- thiz->get_device_info = mobile_explorer_host_xml_get_device_info;
- thiz->auth = mobile_explorer_host_xml_auth;
- thiz->del_objects = mobile_explorer_host_xml_del_objects;
- thiz->add_objects = mobile_explorer_host_xml_add_objects;
- thiz->update_objects = mobile_explorer_host_xml_update_objects;
- thiz->get_objects = mobile_explorer_host_xml_get_objects;
- thiz->get_objectshdr = mobile_explorer_host_xml_get_objectshdr;
- thiz->get_objectsattr = mobile_explorer_host_xml_get_objectsattr;
- thiz->ime_commit = mobile_explorer_host_xml_ime_commit;
- thiz->clipboard_set_data = mobile_explorer_host_xml_clipboard_set_data;
- thiz->clipboard_get_data = mobile_explorer_host_xml_clipboard_get_data;
- thiz->reg_listener = mobile_explorer_host_xml_reg_listener;
- thiz->destroy = mobile_explorer_host_xml_destroy;
- }
- return thiz;
- }