From 7a0bfe7a4d24de0a84a6878ffc5d188610e7441a Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Wed, 13 Dec 2023 22:13:25 +0800 Subject: [PATCH] =?UTF-8?q?[zhoulongyu]:=20=E5=AE=8C=E6=88=90=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=BA=93=E7=9A=84=20bool=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/include/IIpcConfig.h | 1 + middleware/IpcConfig/src/IpcConfig.cpp | 27 ++++++++++++++----- middleware/IpcConfig/src/IpcConfig.h | 3 ++- .../IpcConfig/src/IpcConfig_Test.cpp | 5 ++++ utils/Config/include/Config.h | 6 +++++ utils/Config/src/Config.c | 14 ++++++++++ utils/Config/src/ConfigImpl.c | 22 +++++++++++++++ 7 files changed, 71 insertions(+), 7 deletions(-) diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index f438133..9783c50 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -26,6 +26,7 @@ enum class IpcConfigKey TEST_LONG, TEST_LLONG, TEST_CHAR, + TEST_BOOL, TEST_STRING, END }; diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 2fea2dc..b1d9650 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -42,6 +42,10 @@ IpcConfig::IpcConfig() innerMapChar.insert(std::make_pair("test_char", std::reference_wrapper(mAllData.testChar))); mCfgMapChar.insert(std::make_pair(IpcConfigKey::TEST_CHAR, innerMapChar)); + std::map> innerMapBool; + innerMapBool.insert(std::make_pair("test_bool", std::reference_wrapper(mAllData.testBool))); + mCfgMapBool.insert(std::make_pair(IpcConfigKey::TEST_BOOL, innerMapBool)); + std::map> innerMapString; innerMapString.insert(std::make_pair("test_string", std::reference_wrapper(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>::iterator iter; + std::map>>::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>::iterator iter; + std::map>>::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")) { diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index ddcbd36..55b9ad8 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -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> mCfgMapFloat; std::map> mCfgMapDouble; std::map> mCfgMapLongDouble; - std::map> mCfgMapBool; + std::map>> mCfgMapBool; std::map>> mCfgMapString; }; #endif \ No newline at end of file diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp index 2c20739..283fe07 100644 --- a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -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"; diff --git a/utils/Config/include/Config.h b/utils/Config/include/Config.h index d0711da..e9b085e 100644 --- a/utils/Config/include/Config.h +++ b/utils/Config/include/Config.h @@ -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 diff --git a/utils/Config/src/Config.c b/utils/Config/src/Config.c index eda2ff3..9c5ee8c 100644 --- a/utils/Config/src/Config.c +++ b/utils/Config/src/Config.c @@ -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) { diff --git a/utils/Config/src/ConfigImpl.c b/utils/Config/src/ConfigImpl.c index b454ae9..db3c040 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -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;