[zhoulongyu]: 完成配置库的 bool 数据类型
This commit is contained in:
parent
4853e5c038
commit
7a0bfe7a4d
|
@ -26,6 +26,7 @@ enum class IpcConfigKey
|
|||
TEST_LONG,
|
||||
TEST_LLONG,
|
||||
TEST_CHAR,
|
||||
TEST_BOOL,
|
||||
TEST_STRING,
|
||||
END
|
||||
};
|
||||
|
|
|
@ -42,6 +42,10 @@ IpcConfig::IpcConfig()
|
|||
innerMapChar.insert(std::make_pair("test_char", std::reference_wrapper<char>(mAllData.testChar)));
|
||||
mCfgMapChar.insert(std::make_pair(IpcConfigKey::TEST_CHAR, innerMapChar));
|
||||
|
||||
std::map<std::string, std::reference_wrapper<bool>> innerMapBool;
|
||||
innerMapBool.insert(std::make_pair("test_bool", std::reference_wrapper<bool>(mAllData.testBool)));
|
||||
mCfgMapBool.insert(std::make_pair(IpcConfigKey::TEST_BOOL, innerMapBool));
|
||||
|
||||
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapString;
|
||||
innerMapString.insert(std::make_pair("test_string", std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
|
||||
mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString));
|
||||
|
@ -264,10 +268,10 @@ void IpcConfig::SetLongDouble(const IpcConfigKey &key, const long double &value)
|
|||
}
|
||||
const bool IpcConfig::GetBool(const IpcConfigKey &key)
|
||||
{
|
||||
std::map<IpcConfigKey, std::reference_wrapper<bool>>::iterator iter;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<bool>>>::iterator iter;
|
||||
iter = mCfgMapBool.find(key);
|
||||
if (iter != mCfgMapBool.end()) {
|
||||
return iter->second;
|
||||
if (iter != mCfgMapBool.end() && CHECK_MAP(iter->second)) {
|
||||
return iter->second.begin()->second;
|
||||
}
|
||||
LogError("Can't find the key.\n");
|
||||
constexpr bool UNKNOWN_CONFIG = false;
|
||||
|
@ -275,10 +279,12 @@ const bool IpcConfig::GetBool(const IpcConfigKey &key)
|
|||
}
|
||||
void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value)
|
||||
{
|
||||
std::map<IpcConfigKey, std::reference_wrapper<bool>>::iterator iter;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<bool>>>::iterator iter;
|
||||
iter = mCfgMapBool.find(key);
|
||||
if (iter != mCfgMapBool.end()) {
|
||||
iter->second.get() = value;
|
||||
if (iter != mCfgMapBool.end() && CHECK_MAP(iter->second)) {
|
||||
iter->second.begin()->second.get() = value;
|
||||
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||
ConfigSetBool(mCfg, name, iter->second.begin()->second);
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
}
|
||||
else {
|
||||
|
@ -359,6 +365,15 @@ void IpcConfig::ReadAllConfigParameters(void)
|
|||
ConfigSetChar(mCfg, "test_char", mAllData.testChar);
|
||||
}
|
||||
|
||||
StatusCode boolCode = ConfigGetBool(mCfg, "test_bool", &(mAllData.testBool));
|
||||
if (StatusCodeEqual(boolCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||
LogWarning("test_bool doesn't exist, will make it as default.\n");
|
||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||
constexpr bool DEFAULT_TEST_BOOL_NUM = true;
|
||||
mAllData.testBool = DEFAULT_TEST_BOOL_NUM;
|
||||
ConfigSetBool(mCfg, "test_bool", mAllData.testBool);
|
||||
}
|
||||
|
||||
const char *testString = NULL;
|
||||
StatusCode stringCode = ConfigGetString(mCfg, "test_string", &(testString));
|
||||
if (StatusCodeEqual(stringCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||
|
|
|
@ -30,6 +30,7 @@ typedef struct Config_s
|
|||
long testLong;
|
||||
long long testLLong;
|
||||
char testChar;
|
||||
bool testBool;
|
||||
CHAR_STRING testString;
|
||||
} Config_s;
|
||||
class MapInt
|
||||
|
@ -82,7 +83,7 @@ private:
|
|||
std::map<IpcConfigKey, std::reference_wrapper<float>> mCfgMapFloat;
|
||||
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::map<std::string, std::reference_wrapper<bool>>> mCfgMapBool;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>> mCfgMapString;
|
||||
};
|
||||
#endif
|
|
@ -36,6 +36,11 @@ TEST(IpcConfigTest, Demo)
|
|||
const char numChar = 'A';
|
||||
IIpcConfig::GetInstance()->SetChar(IpcConfigKey::TEST_CHAR, numChar);
|
||||
|
||||
bool testBool = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::TEST_BOOL);
|
||||
LogInfo("Get test_bool = %d\n", testBool);
|
||||
const bool numBool = false;
|
||||
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::TEST_BOOL, numBool);
|
||||
|
||||
const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING);
|
||||
LogInfo("Get testString = %s\n", testString.c_str());
|
||||
const std::string string = "define";
|
||||
|
|
|
@ -36,6 +36,8 @@ typedef struct v_config
|
|||
const StatusCode (*set_llong)(VConfig *, const char *, const long long);
|
||||
const StatusCode (*get_char)(VConfig *, const char *, char *);
|
||||
const StatusCode (*set_char)(VConfig *, const char *, const char);
|
||||
const StatusCode (*get_bool)(VConfig *, const char *, bool *);
|
||||
const StatusCode (*set_bool)(VConfig *, const char *, const bool);
|
||||
const StatusCode (*get_string)(VConfig *, const char *, const char **);
|
||||
const StatusCode (*set_string)(VConfig *, const char *, const char *);
|
||||
const StatusCode (*save)(VConfig *);
|
||||
|
@ -55,6 +57,10 @@ const StatusCode ConfigGetLLong(VConfig *cfg, const char *name, long long *value
|
|||
const StatusCode ConfigSetLLong(VConfig *cfg, const char *name, const long long value);
|
||||
const StatusCode ConfigGetChar(VConfig *cfg, const char *name, char *value);
|
||||
const StatusCode ConfigSetChar(VConfig *cfg, const char *name, const char value);
|
||||
|
||||
const StatusCode ConfigGetBool(VConfig *cfg, const char *name, bool *value);
|
||||
const StatusCode ConfigSetBool(VConfig *cfg, const char *name, const bool value);
|
||||
|
||||
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value);
|
||||
const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value);
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -104,6 +104,20 @@ const StatusCode ConfigSetChar(VConfig *cfg, const char *name, const char value)
|
|||
}
|
||||
return cfg->set_char(cfg, name, value);
|
||||
}
|
||||
const StatusCode ConfigGetBool(VConfig *cfg, const char *name, bool *value)
|
||||
{
|
||||
if (NULL == cfg) {
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
return cfg->get_bool(cfg, name, value);
|
||||
}
|
||||
const StatusCode ConfigSetBool(VConfig *cfg, const char *name, const bool value)
|
||||
{
|
||||
if (NULL == cfg) {
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
return cfg->set_bool(cfg, name, value);
|
||||
}
|
||||
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value)
|
||||
{
|
||||
if (NULL == cfg) {
|
||||
|
|
|
@ -156,6 +156,26 @@ static const StatusCode ConfigSetCharImpl(VConfig *cfg, const char *name, const
|
|||
config_setting_set_int(setting, charValue);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
static const StatusCode ConfigGetBoolImpl(VConfig *cfg, const char *name, bool *value)
|
||||
{
|
||||
int result = 0;
|
||||
result = config_lookup_bool(&(((Config *)cfg)->cfg), name, (int *)value);
|
||||
if (CONFIG_FALSE == result) {
|
||||
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
static const StatusCode ConfigSetBoolImpl(VConfig *cfg, const char *name, const bool value)
|
||||
{
|
||||
config_setting_t *root, *setting;
|
||||
root = config_root_setting(&(((Config *)cfg)->cfg));
|
||||
setting = config_setting_get_member(root, name);
|
||||
if (!setting) {
|
||||
setting = config_setting_add(root, name, CONFIG_TYPE_BOOL);
|
||||
}
|
||||
config_setting_set_bool(setting, (int)value);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value)
|
||||
{
|
||||
int result = 0;
|
||||
|
@ -195,6 +215,8 @@ static void ConfigImplInit(Config *cfg)
|
|||
cfg->base.set_llong = ConfigSetLLongImpl;
|
||||
cfg->base.get_char = ConfigGetCharImpl;
|
||||
cfg->base.set_char = ConfigSetCharImpl;
|
||||
cfg->base.get_bool = ConfigGetBoolImpl;
|
||||
cfg->base.set_bool = ConfigSetBoolImpl;
|
||||
cfg->base.get_string = ConfigGetStringImpl;
|
||||
cfg->base.set_string = ConfigSetStringImpl;
|
||||
cfg->base.save = ConfigSaveFileImpl;
|
||||
|
|
Loading…
Reference in New Issue
Block a user