当您遇到一大堆用数字串对应的规则时,记这些字符串有时会让人很头痛。没关系,用 枚举+map。
1.定义一些枚举。
enum UpdateKind
{
enCommenFile = 1,
enCom, //
enDelCommenFile,//
enDelCom, //
enRegTable, //
enShortCut, //
enModifySec, //
enDelSec, //
enActiveX, //
};
2.定义一个map的成员变量和枚举的成员变量,来映射数字串和枚举的关系
map<CString,UpdateKind> m_mapKind;
UpdateKind m_enSrvType;
3.同时有个接口给人设置类型。
void CAutoCommand::Set_SrvType(const CString &strType)
{
m_enSrvType = m_mapKind[strType];
};
4.构造函数中来个初始化吧
CAutoCommand::CAutoCommand()
{
m_mapKind.insert(make_pair(_T("0100"),enCommenFile));
m_mapKind.insert(make_pair(_T("0200"),enCom));
m_mapKind.insert(make_pair(_T("0300"),enDelCommenFile));
m_mapKind.insert(make_pair(_T("0400"),enDelCom));
m_mapKind.insert(make_pair(_T("0500"),enRegTable));
m_mapKind.insert(make_pair(_T("0600"),enShortCut));
m_mapKind.insert(make_pair(_T("0702"),enModifySec));
m_mapKind.insert(make_pair(_T("0703"),enDelSec));
m_mapKind.insert(make_pair(_T("0800"),enActiveX));
};
5.别人设置了类型。
pUpdateCmd->Set_SrvType(sectioninfoSrv.strType);
6.该是你自己表现的时候了
UpdateResult CAutoCommand::UpdateFile()
{
UpdateResult updateRtn = enFailure;
switch(m_enSrvType)
{
case enCommenFile: //
updateRtn=commen();
break;
case enCom: //
updateRtn=com();
break;
case enDelCommenFile://
updateRtn=delCommen();
break;
case enDelCom: //
updateRtn=delCom();
break;
case enRegTable: //
updateRtn=regedit();
break;
case enShortCut: //
updateRtn=shortcut();
break;
case enModifySec: //
updateRtn=modifyIni();
break;
case enDelSec: //
updateRtn=deleteIni();
break;
case enActiveX: //
updateRtn=myIE();
break;
default:
break;
}
return updateRtn;
}