Table of Contents

定制接口

XT框架中文档数据与第三方文件的相互转换通过继承接口类XInterface来实现。XInterface提供了下列接口定义:

接口的声明

新的接口类从XInterface继承即可。

class TestInterface : public XInterface
{
public:
  TestInterface (XDocument * pDoc);
  ~TestInterface ();
  ErrorCode doImport();
}

接口的实现

为了减少模块之间的耦合性, 上述接口均为虚接口,接口一般来说依赖核心库的application模块和domain模块即可。但需要注意的是,开发者在独立开发实现接口函数以外,还需要实现如下构造函数与析构函数(这些函数将在使用该接口之前被调用):

bool CreateInterface(XInterface ** ppIF,XDocument * pDoc)
{
  XInterface * pIF = new TestInterface((XDocument*)pDoc);
  if (pIF)	{
    *ppIF = pIF;
    return true;
  }
  else	{
    return false;
  }
}
void DestroyInterface(XInterface * pIF)
{
  delete (TestInterface*)pIF;
}

开发好的接口一般以动态链接库形式存在,动态库的名称按XIf_*形式约定,否则接口工厂将不能正确载入库。

接口的使用

开发好的接口通过接口工厂Interfacefactory完成。首先由接口工厂注册接口库名称,接口工程载入对应的库文件并创建接口,设置相应的参数并完成导入或导出操作,最后销毁接口。示意代码如下:

InterfaceFactory iff(0);
InterfaceSetting param;
param.strFileName.assign(fname); //设置接口参数
... 
XInterface * pIF = iff.createInterface("test"); //库文件名为XIf_test.dll, 载入该库文件然后创建接口
pIF->setParam(&param);
OpnRecorder::disable(); //禁止记录数据修改
pIF->importModel(mdl); //导入或导出操作
OpnRecorder::enable(); //恢复记录数据修改
iff.destroyInterface(pIF);//销毁接口