[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 "ILog.h"
#include <string.h> #include <string.h>
#define CHECK_MAP(map) (map.size()==1? true:false)
IpcConfig::IpcConfig() IpcConfig::IpcConfig()
{ {
mCfgChanged = CONFIG_HAS_NOT_CHANGED; mCfgChanged = CONFIG_HAS_NOT_CHANGED;
@ -26,12 +28,13 @@ IpcConfig::IpcConfig()
IpcConfigKey::TEST_NUM, IpcConfigKey::TEST_NUM,
std::reference_wrapper<int>(mAllData.testNum))); std::reference_wrapper<int>(mAllData.testNum)));
mCfgMapString.insert( std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMap;
std::make_pair< innerMap.insert(
IpcConfigKey, std::make_pair(
std::reference_wrapper<CHAR_STRING>>( "test_string",
IpcConfigKey::TEST_STRING,
std::reference_wrapper<CHAR_STRING>(mAllData.testString))); std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMap));
} }
const StatusCode IpcConfig::Init(void) 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) 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); 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(iter->second.begin()->second); // char[] --> const std::strinbg
const std::string sv(s);
return sv; return sv;
} }
LogError("Can't find the key.\n"); LogError("Can't find the key.\n");
const std::string UNKNOWN_CONFIG = "undefine"; const std::string UNKNOWN_CONFIG = "undefine";
return UNKNOWN_CONFIG; return UNKNOWN_CONFIG;
} }
void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) 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); 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; mCfgChanged = CONFIG_HAS_CHANGED;
} }
else else
@ -335,7 +338,6 @@ void IpcConfig::ReadAllConfigParameters(void)
mAllData.testNum = DEFAULT_TEST_NUM; mAllData.testNum = DEFAULT_TEST_NUM;
ConfigSetInt(mCfg, "test_num", mAllData.testNum); ConfigSetInt(mCfg, "test_num", mAllData.testNum);
} }
const char *testString = NULL; const char *testString = NULL;
StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString)); StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString));
if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) 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<double>> mCfgMapDouble;
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble; std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;
std::map<IpcConfigKey, std::reference_wrapper<bool>> mCfgMapBool; 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 #endif