[zhoulongyu]: 完成配置库的 double 数据类型
This commit is contained in:
parent
7a0bfe7a4d
commit
18c4877ec9
|
@ -27,6 +27,7 @@ enum class IpcConfigKey
|
|||
TEST_LLONG,
|
||||
TEST_CHAR,
|
||||
TEST_BOOL,
|
||||
TEST_DOUBLE,
|
||||
TEST_STRING,
|
||||
END
|
||||
};
|
||||
|
|
|
@ -46,6 +46,10 @@ IpcConfig::IpcConfig()
|
|||
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<double>> innerMapDouble;
|
||||
innerMapDouble.insert(std::make_pair("test_double", std::reference_wrapper<double>(mAllData.testDouble)));
|
||||
mCfgMapDouble.insert(std::make_pair(IpcConfigKey::TEST_DOUBLE, innerMapDouble));
|
||||
|
||||
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));
|
||||
|
@ -222,21 +226,23 @@ void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value)
|
|||
}
|
||||
const double IpcConfig::GetDouble(const IpcConfigKey &key)
|
||||
{
|
||||
std::map<IpcConfigKey, std::reference_wrapper<double>>::iterator iter;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::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<IpcConfigKey, std::reference_wrapper<double>>::iterator iter;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::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")) {
|
||||
|
|
|
@ -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<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>> mCfgMapLLong;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>> mCfgMapChar;
|
||||
std::map<IpcConfigKey, std::reference_wrapper<float>> mCfgMapFloat;
|
||||
std::map<IpcConfigKey, std::reference_wrapper<double>> mCfgMapDouble;
|
||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>> mCfgMapDouble;
|
||||
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;
|
||||
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;
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user