文章类型: |
VC&C++ |
关键词: |
使用,函数指针,map,加载,不确定配置文件,实现 |
内容摘要: |
使用函数指针和map加载不确定配置文件的实现 |
使用函数指针和map加载不确定配置文件的实现
2018/9/17 13:55:53 来源:apple 阅读:
#include<iostream>
#include<string>
#include<map>
typedef void (*pFunc)(); //用于指向具体加载配置文件的函数
using namespace std;
enum TYPE //定义两个配置文件,以它作为map的key
{
CHARGE,
CHARGESWITCH
};
struct configInfo
{
unsigned int lastVesion; //保存上一个版本号
string configName; //配置文件名称
string configMod; //配置文件对应的版本文件名称
pFunc pfunc; //加载该配置的函数指针
};
map<TYPE,configInfo*> *configMap = new map<TYPE,configInfo*>();
void loadCharge()
{
cout<<"load Charge success!"<<endl;
}
void loadChargeSwitch()
{
cout<<"load ChargSwitch success!"<<endl;
}
void Init() //初始化,完成map最初的数据导入
{
configInfo *charge = new configInfo;
charge->lastVesion = 0;
charge->configName = "charge.cfg";
charge->configMod = "charge_mod.cfg";
charge->pfunc = &loadCharge; //指向charge配置
(charge->pfunc)();
configMap->insert(pair<TYPE,configInfo*>(CHARGE,charge));
configInfo *chargeswitch = new configInfo;
chargeswitch->lastVesion = 0;
chargeswitch->configName = "chargeswitch.cfg";
chargeswitch->configMod = "chargeswitch_mod.cfg";
chargeswitch->pfunc = &loadChargeSwitch; //指向chargeswitch配置
(chargeswitch->pfunc)();
configMap->insert(pair<TYPE,configInfo*>(CHARGESWITCH,chargeswitch));
}
int GetCurVersion()
{
return 1;
}
bool ExecuteOneTime(TYPE e)
{
map<TYPE,configInfo*>::iterator iter = configMap->find(e);
if(iter == configMap->end()) //map中没有该配置返回false
return false;
int curVersion = GetCurVersion();
if(curVersion <= iter->second->lastVesion) //如果当前版本不是最新的,就选择不加载该配置
return false;
(iter->second->pfunc)(); //加载配置
return true;
}
int main()
{
Init();
ExecuteOneTime(CHARGE);
ExecuteOneTime(CHARGESWITCH);
return 0;
}
运行结果如下:
<img src="https://img-blog.csdn.net/20160922004402276?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="261" height="74" alt="" />
该程序可以拓展,如某个应用同时要加载几个配置文件,但不确定加载哪个,可以给每个配置设置一个mod文件,mod文件可以存入版本号,有几个配置就调用几个ExecuteOneTime,
ExecuteOneTime中根据mod中的版本号和之前版本进行对比,再决定是否需要加载该配置。
↑ 上一篇文章:
.net报“System.MissingMethodException: Method not found(找不到方法)”错误的解决方法 关键词:.net,System,MissingMethodExc.. 发布日期:2018/9/14 15:48:45
↓ 下一篇文章:
VC++ CString类完美总结(整理) 关键词:VC++,,MFC,CString,类,完美,总结,整理 发布日期:2018/9/17 13:58:49
相关文章:
VC++之 CreateEvent和SetEvent及WaitForSingleObject的用法 关键词:VC++,线程同步,CreateEvent,SetEvent,WaitForSingleObje.. 发布日期:2017-04-14 11:53
VC中CList用法及其成员的使用 关键词:VC,CList,用法,成员,使用 发布日期:2017-10-26 12:04
VC++中ListBox控件的使用 关键词:VC++中ListBox控件的使用 发布日期:2017-05-15 10:48
相关目录:
.NET,
VC&C++