[zhoulongyu]: 使用嵌套std::map 把配置文件相关的三个参数映射起来, 完成char[]数据类型的set和get

This commit is contained in:
jas 2023-12-12 10:36:07 +08:00
parent 1d214080ce
commit b94f6ac956
2 changed files with 17 additions and 15 deletions

View File

@ -16,6 +16,8 @@
#include "ILog.h"
#include <string.h>
#define CHECK_MAP(map) (map.size()==1? true:false)
IpcConfig::IpcConfig()
{
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
@ -26,12 +28,13 @@ IpcConfig::IpcConfig()
IpcConfigKey::TEST_NUM,
std::reference_wrapper<int>(mAllData.testNum)));
mCfgMapString.insert(
std::make_pair<
IpcConfigKey,
std::reference_wrapper<CHAR_STRING>>(
IpcConfigKey::TEST_STRING,
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMap;
innerMap.insert(
std::make_pair(
"test_string",
std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMap));
}
const StatusCode IpcConfig::Init(void)
{
@ -296,26 +299,26 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value)
}
const std::string IpcConfig::GetString(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::reference_wrapper<CHAR_STRING>>::iterator iter;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::iterator iter;
iter = mCfgMapString.find(key);
if (iter != mCfgMapString.end())
if (iter != mCfgMapString.end() && CHECK_MAP(iter->second))
{
std::reference_wrapper<CHAR_STRING> s = iter->second;
const std::string sv(s);
const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg
return sv;
}
LogError("Can't find the key.\n");
const std::string UNKNOWN_CONFIG = "undefine";
return UNKNOWN_CONFIG;
}
void IpcConfig::SetString(const IpcConfigKey &key, const std::string string)
{
std::map<IpcConfigKey, std::reference_wrapper<CHAR_STRING>>::iterator iter;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::iterator iter;
iter = mCfgMapString.find(key);
if (iter != mCfgMapString.end())
if (iter != mCfgMapString.end() && CHECK_MAP(iter->second))
{
strncpy(iter->second, string.c_str(), sizeof(CHAR_STRING));
strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[]
const char * name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
ConfigSetString(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else
@ -335,7 +338,6 @@ void IpcConfig::ReadAllConfigParameters(void)
mAllData.testNum = DEFAULT_TEST_NUM;
ConfigSetInt(mCfg, "test_num", mAllData.testNum);
}
const char *testString = NULL;
StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString));
if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST"))

View File

@ -80,6 +80,6 @@ private:
std::map<IpcConfigKey, std::reference_wrapper<double>> mCfgMapDouble;
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;
std::map<IpcConfigKey, std::reference_wrapper<bool>> mCfgMapBool;
std::map<IpcConfigKey, std::reference_wrapper<CHAR_STRING>> mCfgMapString;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>> mCfgMapString;
};
#endif