diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index 9783c50..1f37241 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -27,6 +27,7 @@ enum class IpcConfigKey TEST_LLONG, TEST_CHAR, TEST_BOOL, + TEST_DOUBLE, TEST_STRING, END }; diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index b1d9650..2716fb6 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -46,6 +46,10 @@ IpcConfig::IpcConfig() innerMapBool.insert(std::make_pair("test_bool", std::reference_wrapper(mAllData.testBool))); mCfgMapBool.insert(std::make_pair(IpcConfigKey::TEST_BOOL, innerMapBool)); + std::map> innerMapDouble; + innerMapDouble.insert(std::make_pair("test_double", std::reference_wrapper(mAllData.testDouble))); + mCfgMapDouble.insert(std::make_pair(IpcConfigKey::TEST_DOUBLE, innerMapDouble)); + std::map> innerMapString; innerMapString.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString)); @@ -222,21 +226,23 @@ void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value) } const double IpcConfig::GetDouble(const IpcConfigKey &key) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapDouble.find(key); - if (iter != mCfgMapDouble.end()) { - return iter->second; + if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) { + return iter->second.begin()->second; } LogError("Can't find the key.\n"); - constexpr double UNKNOWN_CONFIG = -1.0; + constexpr double UNKNOWN_CONFIG = -1.00; return UNKNOWN_CONFIG; } void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapDouble.find(key); - if (iter != mCfgMapDouble.end()) { - iter->second.get() = value; + if (iter != mCfgMapDouble.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 * + ConfigSetDouble(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } else { @@ -374,6 +380,15 @@ void IpcConfig::ReadAllConfigParameters(void) ConfigSetBool(mCfg, "test_bool", mAllData.testBool); } + StatusCode doubleCode = ConfigGetDouble(mCfg, "test_double", &(mAllData.testDouble)); + if (StatusCodeEqual(doubleCode, "CONFIG_CODE_PARAM_NOT_EXIST")) { + LogWarning("test_double doesn't exist, will make it as default.\n"); + mCfgChanged = CONFIG_HAS_CHANGED; + constexpr double DEFAULT_TEST_DOUBLE_NUM = 3.123456789; + mAllData.testDouble = DEFAULT_TEST_DOUBLE_NUM; + ConfigSetDouble(mCfg, "test_double", mAllData.testDouble); + } + 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 55b9ad8..70ed224 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -31,6 +31,7 @@ typedef struct Config_s long long testLLong; char testChar; bool testBool; + double testDouble; CHAR_STRING testString; } Config_s; class MapInt @@ -81,7 +82,7 @@ private: std::map>> mCfgMapLLong; std::map>> mCfgMapChar; std::map> mCfgMapFloat; - std::map> mCfgMapDouble; + std::map>> mCfgMapDouble; std::map> mCfgMapLongDouble; std::map>> mCfgMapBool; std::map>> mCfgMapString; diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp index 283fe07..5ef50af 100644 --- a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -41,6 +41,11 @@ TEST(IpcConfigTest, Demo) const bool numBool = false; IIpcConfig::GetInstance()->SetBool(IpcConfigKey::TEST_BOOL, numBool); + double testDouble = IIpcConfig::GetInstance()->GetDouble(IpcConfigKey::TEST_DOUBLE); + LogInfo("Get test_double = %le\n", testDouble); + const double numDouble = 2.123456789; + IIpcConfig::GetInstance()->SetDouble(IpcConfigKey::TEST_DOUBLE, numDouble); + 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 e9b085e..b497759 100644 --- a/utils/Config/include/Config.h +++ b/utils/Config/include/Config.h @@ -38,6 +38,8 @@ typedef struct v_config 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_double)(VConfig *, const char *, double *); + const StatusCode (*set_double)(VConfig *, const char *, const double); const StatusCode (*get_string)(VConfig *, const char *, const char **); const StatusCode (*set_string)(VConfig *, const char *, const char *); const StatusCode (*save)(VConfig *); @@ -57,10 +59,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 ConfigGetDouble(VConfig *cfg, const char *name, double *value); +const StatusCode ConfigSetDouble(VConfig *cfg, const char *name, const double 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 9c5ee8c..aea0077 100644 --- a/utils/Config/src/Config.c +++ b/utils/Config/src/Config.c @@ -118,6 +118,21 @@ const StatusCode ConfigSetBool(VConfig *cfg, const char *name, const bool value) } return cfg->set_bool(cfg, name, value); } +const StatusCode ConfigGetDouble(VConfig *cfg, const char *name, double *value) +{ + if (NULL == cfg) { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->get_double(cfg, name, value); +} +const StatusCode ConfigSetDouble(VConfig *cfg, const char *name, const double value) +{ + if (NULL == cfg) { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->set_double(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 db3c040..88ec389 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -176,6 +176,28 @@ static const StatusCode ConfigSetBoolImpl(VConfig *cfg, const char *name, const config_setting_set_bool(setting, (int)value); return CreateStatusCode(STATUS_CODE_OK); } + +static const StatusCode ConfigGetDoubleImpl(VConfig *cfg, const char *name, double *value) +{ + int result = 0; + result = config_lookup_float(&(((Config *)cfg)->cfg), name, value); + if (CONFIG_FALSE == result) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +static const StatusCode ConfigSetDoubleImpl(VConfig *cfg, const char *name, const double 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_FLOAT); + } + config_setting_set_float(setting, value); + return CreateStatusCode(STATUS_CODE_OK); +} + static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value) { int result = 0; @@ -217,6 +239,8 @@ static void ConfigImplInit(Config *cfg) cfg->base.set_char = ConfigSetCharImpl; cfg->base.get_bool = ConfigGetBoolImpl; cfg->base.set_bool = ConfigSetBoolImpl; + cfg->base.get_double = ConfigGetDoubleImpl; + cfg->base.set_double = ConfigSetDoubleImpl; cfg->base.get_string = ConfigGetStringImpl; cfg->base.set_string = ConfigSetStringImpl; cfg->base.save = ConfigSaveFileImpl;