如何设计这个C++程序的API?
写完了C++应用程序的代码,现在要做一个API生成DLL,请问大家,该如何来设计这样的接口呢?
看了中科院分词程序的API接口代码,有初始化资源的接口,有分词函数的接口,有释放资源的接口。
自己来设计时就遇到问题了:
我的C++项目大致是这样的:读入帖子的编号、标题和内容,返回一个XML结果的字符串;
int _Process(const char* pInfoID, const char* pTitle, const char* pContent, char* pResult);
参数依次是 信息编号、标题、内容,最后的pResult是返回结果;
需要需要用户自己申请一段内存来存放返回结果;
看中科院分词的程序有初始化资源以及最后释放资源的接口,我的代码中也需要读入词典等内容,但这一块我就不知道如何设计了。
- C/C++ code
#pragma once#ifndef _TUIXINDLL_H_#define _TUIXINDLL_H_#include "commonpredefine.h"#include "cutwords.h"#include "encoding.h"#include "linkrule.h"#include "resmgr.h"#include "utf16manip.h"#include <time.h>CHarSetsEncoding CSEncoding; //编码转换的类;UTF16CutWords UTF16CWords; //切词的类,会读入词典的内容;int _Process(const char* pInfoID, const char* pTitle, const char* pContent, char* pResult);#endif /* _TUIXINDLL_H_ */
朋友们帮帮忙啊,万分感谢!
[解决办法]
不是全局,它只是你API函数的处理对象而已,举个例子:
1、比如你自建了链表的API在DLL中,它们类似于这样的接口,
void list_add(list_t* self, int elem);
void list_remove(list_t* self, int i);
2、你在主程序中调入DLL后,类似这样的用法,
list_t mylist;
list_add(&mylist, 1);
list_add(&mylist, 5);
list_remove(&mylist, 0);
DLL只是封装了处理的方法和细节,而实体以及应用都在主程序中进行。
[解决办法]
int _Process(const char* pInfoID, const char* pTitle, const char* pContent, char* pResult);
参数依次是 信息编号、标题、内容,最后的pResult是返回结果;
-------------------------
向这种参数比较多的可以考虑封装一下,写个结构体,如果以后要扩展的话也比较容易