[zhoulongyu]: 完成配置库的 float 数据类型
This commit is contained in:
parent
18c4877ec9
commit
ae20412ee7
|
@ -27,6 +27,7 @@ enum class IpcConfigKey
|
||||||
TEST_LLONG,
|
TEST_LLONG,
|
||||||
TEST_CHAR,
|
TEST_CHAR,
|
||||||
TEST_BOOL,
|
TEST_BOOL,
|
||||||
|
TEST_FLOAT,
|
||||||
TEST_DOUBLE,
|
TEST_DOUBLE,
|
||||||
TEST_STRING,
|
TEST_STRING,
|
||||||
END
|
END
|
||||||
|
|
|
@ -46,6 +46,10 @@ IpcConfig::IpcConfig()
|
||||||
innerMapBool.insert(std::make_pair("test_bool", std::reference_wrapper<bool>(mAllData.testBool)));
|
innerMapBool.insert(std::make_pair("test_bool", std::reference_wrapper<bool>(mAllData.testBool)));
|
||||||
mCfgMapBool.insert(std::make_pair(IpcConfigKey::TEST_BOOL, innerMapBool));
|
mCfgMapBool.insert(std::make_pair(IpcConfigKey::TEST_BOOL, innerMapBool));
|
||||||
|
|
||||||
|
std::map<std::string, std::reference_wrapper<float>> innerMapFloat;
|
||||||
|
innerMapFloat.insert(std::make_pair("test_float", std::reference_wrapper<float>(mAllData.testFloat)));
|
||||||
|
mCfgMapFloat.insert(std::make_pair(IpcConfigKey::TEST_FLOAT, innerMapFloat));
|
||||||
|
|
||||||
std::map<std::string, std::reference_wrapper<double>> innerMapDouble;
|
std::map<std::string, std::reference_wrapper<double>> innerMapDouble;
|
||||||
innerMapDouble.insert(std::make_pair("test_double", std::reference_wrapper<double>(mAllData.testDouble)));
|
innerMapDouble.insert(std::make_pair("test_double", std::reference_wrapper<double>(mAllData.testDouble)));
|
||||||
mCfgMapDouble.insert(std::make_pair(IpcConfigKey::TEST_DOUBLE, innerMapDouble));
|
mCfgMapDouble.insert(std::make_pair(IpcConfigKey::TEST_DOUBLE, innerMapDouble));
|
||||||
|
@ -203,21 +207,23 @@ void IpcConfig::SetChar(const IpcConfigKey &key, const char &character)
|
||||||
}
|
}
|
||||||
const float IpcConfig::GetFloat(const IpcConfigKey &key)
|
const float IpcConfig::GetFloat(const IpcConfigKey &key)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<float>>::iterator iter;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
|
||||||
iter = mCfgMapFloat.find(key);
|
iter = mCfgMapFloat.find(key);
|
||||||
if (iter != mCfgMapFloat.end()) {
|
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
|
||||||
return iter->second;
|
return iter->second.begin()->second;
|
||||||
}
|
}
|
||||||
LogError("Can't find the key.\n");
|
LogError("Can't find the key.\n");
|
||||||
constexpr float UNKNOWN_CONFIG = -1.0;
|
constexpr float UNKNOWN_CONFIG = -1.00;
|
||||||
return UNKNOWN_CONFIG;
|
return UNKNOWN_CONFIG;
|
||||||
}
|
}
|
||||||
void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value)
|
void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value)
|
||||||
{
|
{
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<float>>::iterator iter;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
|
||||||
iter = mCfgMapFloat.find(key);
|
iter = mCfgMapFloat.find(key);
|
||||||
if (iter != mCfgMapFloat.end()) {
|
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
|
||||||
iter->second.get() = value;
|
iter->second.begin()->second.get() = value;
|
||||||
|
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
|
||||||
|
ConfigSetFloat(mCfg, name, iter->second.begin()->second);
|
||||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -380,11 +386,20 @@ void IpcConfig::ReadAllConfigParameters(void)
|
||||||
ConfigSetBool(mCfg, "test_bool", mAllData.testBool);
|
ConfigSetBool(mCfg, "test_bool", mAllData.testBool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StatusCode floatCode = ConfigGetFloat(mCfg, "test_float", &(mAllData.testFloat));
|
||||||
|
if (StatusCodeEqual(floatCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||||
|
LogWarning("test_float doesn't exist, will make it as default.\n");
|
||||||
|
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||||
|
constexpr float DEFAULT_TEST_FLOAT_NUM = 1.123456;
|
||||||
|
mAllData.testFloat = DEFAULT_TEST_FLOAT_NUM;
|
||||||
|
ConfigSetFloat(mCfg, "test_float", mAllData.testFloat);
|
||||||
|
}
|
||||||
|
|
||||||
StatusCode doubleCode = ConfigGetDouble(mCfg, "test_double", &(mAllData.testDouble));
|
StatusCode doubleCode = ConfigGetDouble(mCfg, "test_double", &(mAllData.testDouble));
|
||||||
if (StatusCodeEqual(doubleCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
if (StatusCodeEqual(doubleCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
|
||||||
LogWarning("test_double doesn't exist, will make it as default.\n");
|
LogWarning("test_double doesn't exist, will make it as default.\n");
|
||||||
mCfgChanged = CONFIG_HAS_CHANGED;
|
mCfgChanged = CONFIG_HAS_CHANGED;
|
||||||
constexpr double DEFAULT_TEST_DOUBLE_NUM = 3.123456789;
|
constexpr double DEFAULT_TEST_DOUBLE_NUM = 3.123456;
|
||||||
mAllData.testDouble = DEFAULT_TEST_DOUBLE_NUM;
|
mAllData.testDouble = DEFAULT_TEST_DOUBLE_NUM;
|
||||||
ConfigSetDouble(mCfg, "test_double", mAllData.testDouble);
|
ConfigSetDouble(mCfg, "test_double", mAllData.testDouble);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ typedef struct Config_s
|
||||||
long long testLLong;
|
long long testLLong;
|
||||||
char testChar;
|
char testChar;
|
||||||
bool testBool;
|
bool testBool;
|
||||||
|
float testFloat;
|
||||||
double testDouble;
|
double testDouble;
|
||||||
CHAR_STRING testString;
|
CHAR_STRING testString;
|
||||||
} Config_s;
|
} Config_s;
|
||||||
|
@ -81,7 +82,7 @@ private:
|
||||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>> mCfgMapLong;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>> mCfgMapLong;
|
||||||
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>> mCfgMapLLong;
|
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::map<std::string, std::reference_wrapper<char>>> mCfgMapChar;
|
||||||
std::map<IpcConfigKey, std::reference_wrapper<float>> mCfgMapFloat;
|
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>> mCfgMapFloat;
|
||||||
std::map<IpcConfigKey, std::map<std::string, 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::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<bool>>> mCfgMapBool;
|
||||||
|
|
|
@ -41,9 +41,14 @@ TEST(IpcConfigTest, Demo)
|
||||||
const bool numBool = false;
|
const bool numBool = false;
|
||||||
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::TEST_BOOL, numBool);
|
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::TEST_BOOL, numBool);
|
||||||
|
|
||||||
|
float testFloat = IIpcConfig::GetInstance()->GetFloat(IpcConfigKey::TEST_FLOAT);
|
||||||
|
LogInfo("Get test_float = %lf\n", testFloat);
|
||||||
|
const float numFloat = 1.123456;
|
||||||
|
IIpcConfig::GetInstance()->SetFloat(IpcConfigKey::TEST_FLOAT, numFloat);
|
||||||
|
|
||||||
double testDouble = IIpcConfig::GetInstance()->GetDouble(IpcConfigKey::TEST_DOUBLE);
|
double testDouble = IIpcConfig::GetInstance()->GetDouble(IpcConfigKey::TEST_DOUBLE);
|
||||||
LogInfo("Get test_double = %le\n", testDouble);
|
LogInfo("Get test_double = %lf\n", testDouble);
|
||||||
const double numDouble = 2.123456789;
|
const double numDouble = 123456789.123456;
|
||||||
IIpcConfig::GetInstance()->SetDouble(IpcConfigKey::TEST_DOUBLE, numDouble);
|
IIpcConfig::GetInstance()->SetDouble(IpcConfigKey::TEST_DOUBLE, numDouble);
|
||||||
|
|
||||||
const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING);
|
const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING);
|
||||||
|
|
|
@ -38,6 +38,8 @@ typedef struct v_config
|
||||||
const StatusCode (*set_char)(VConfig *, const char *, const char);
|
const StatusCode (*set_char)(VConfig *, const char *, const char);
|
||||||
const StatusCode (*get_bool)(VConfig *, const char *, bool *);
|
const StatusCode (*get_bool)(VConfig *, const char *, bool *);
|
||||||
const StatusCode (*set_bool)(VConfig *, const char *, const bool);
|
const StatusCode (*set_bool)(VConfig *, const char *, const bool);
|
||||||
|
const StatusCode (*get_float)(VConfig *, const char *, float *);
|
||||||
|
const StatusCode (*set_float)(VConfig *, const char *, const float);
|
||||||
const StatusCode (*get_double)(VConfig *, const char *, double *);
|
const StatusCode (*get_double)(VConfig *, const char *, double *);
|
||||||
const StatusCode (*set_double)(VConfig *, const char *, const double);
|
const StatusCode (*set_double)(VConfig *, const char *, const double);
|
||||||
const StatusCode (*get_string)(VConfig *, const char *, const char **);
|
const StatusCode (*get_string)(VConfig *, const char *, const char **);
|
||||||
|
@ -61,6 +63,8 @@ const StatusCode ConfigGetChar(VConfig *cfg, const char *name, char *value);
|
||||||
const StatusCode ConfigSetChar(VConfig *cfg, const char *name, const 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 ConfigGetBool(VConfig *cfg, const char *name, bool *value);
|
||||||
const StatusCode ConfigSetBool(VConfig *cfg, const char *name, const bool value);
|
const StatusCode ConfigSetBool(VConfig *cfg, const char *name, const bool value);
|
||||||
|
const StatusCode ConfigGetFloat(VConfig *cfg, const char *name, float *value);
|
||||||
|
const StatusCode ConfigSetFloat(VConfig *cfg, const char *name, const float value);
|
||||||
const StatusCode ConfigGetDouble(VConfig *cfg, const char *name, double *value);
|
const StatusCode ConfigGetDouble(VConfig *cfg, const char *name, double *value);
|
||||||
const StatusCode ConfigSetDouble(VConfig *cfg, const char *name, const 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 ConfigGetString(VConfig *cfg, const char *name, const char **value);
|
||||||
|
|
|
@ -118,6 +118,20 @@ const StatusCode ConfigSetBool(VConfig *cfg, const char *name, const bool value)
|
||||||
}
|
}
|
||||||
return cfg->set_bool(cfg, name, value);
|
return cfg->set_bool(cfg, name, value);
|
||||||
}
|
}
|
||||||
|
const StatusCode ConfigGetFloat(VConfig *cfg, const char *name, float *value)
|
||||||
|
{
|
||||||
|
if (NULL == cfg) {
|
||||||
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
|
}
|
||||||
|
return cfg->get_float(cfg, name, value);
|
||||||
|
}
|
||||||
|
const StatusCode ConfigSetFloat(VConfig *cfg, const char *name, const float value)
|
||||||
|
{
|
||||||
|
if (NULL == cfg) {
|
||||||
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
|
}
|
||||||
|
return cfg->set_float(cfg, name, value);
|
||||||
|
}
|
||||||
const StatusCode ConfigGetDouble(VConfig *cfg, const char *name, double *value)
|
const StatusCode ConfigGetDouble(VConfig *cfg, const char *name, double *value)
|
||||||
{
|
{
|
||||||
if (NULL == cfg) {
|
if (NULL == cfg) {
|
||||||
|
@ -132,7 +146,6 @@ const StatusCode ConfigSetDouble(VConfig *cfg, const char *name, const double va
|
||||||
}
|
}
|
||||||
return cfg->set_double(cfg, name, value);
|
return cfg->set_double(cfg, name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value)
|
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value)
|
||||||
{
|
{
|
||||||
if (NULL == cfg) {
|
if (NULL == cfg) {
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include "ConfigCode.h"
|
#include "ConfigCode.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -23,6 +24,7 @@
|
||||||
#define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true))
|
#define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true))
|
||||||
#define CHECK_LONG_LIMIT(value) (value > LONG_MAX ? false : (value < LONG_MIN ? false : true))
|
#define CHECK_LONG_LIMIT(value) (value > LONG_MAX ? false : (value < LONG_MIN ? false : true))
|
||||||
#define CHECK_CHAR_LIMIT(value) (value > CHAR_MAX ? false : (value < CHAR_MIN ? false : true))
|
#define CHECK_CHAR_LIMIT(value) (value > CHAR_MAX ? false : (value < CHAR_MIN ? false : true))
|
||||||
|
#define CHECK_FLOAT_LIMIT(value) (fabs(value - ((float)value)) < 0.000001 ? false : true)
|
||||||
|
|
||||||
static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
|
static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
|
||||||
{
|
{
|
||||||
|
@ -72,7 +74,7 @@ static const StatusCode ConfigGetShortImpl(VConfig *cfg, const char *name, short
|
||||||
int intValue = 0;
|
int intValue = 0;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
result = config_lookup_int(&(((Config *)cfg)->cfg), name, &intValue);
|
result = config_lookup_int(&(((Config *)cfg)->cfg), name, &intValue);
|
||||||
if (CONFIG_FALSE == result && CHECK_SHORT_LIMIT(intValue)) {
|
if (CONFIG_FALSE == result || CHECK_SHORT_LIMIT(intValue)) {
|
||||||
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||||
}
|
}
|
||||||
*value = (short)intValue;
|
*value = (short)intValue;
|
||||||
|
@ -95,7 +97,7 @@ static const StatusCode ConfigGetLongImpl(VConfig *cfg, const char *name, long *
|
||||||
long long llongValue = 0;
|
long long llongValue = 0;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
result = config_lookup_int64(&(((Config *)cfg)->cfg), name, &llongValue);
|
result = config_lookup_int64(&(((Config *)cfg)->cfg), name, &llongValue);
|
||||||
if (CONFIG_FALSE == result && CHECK_LONG_LIMIT(llongValue)) {
|
if (CONFIG_FALSE == result || CHECK_LONG_LIMIT(llongValue)) {
|
||||||
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||||
}
|
}
|
||||||
*value = (long)llongValue;
|
*value = (long)llongValue;
|
||||||
|
@ -176,7 +178,29 @@ static const StatusCode ConfigSetBoolImpl(VConfig *cfg, const char *name, const
|
||||||
config_setting_set_bool(setting, (int)value);
|
config_setting_set_bool(setting, (int)value);
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
|
static const StatusCode ConfigGetFloatImpl(VConfig *cfg, const char *name, float *value)
|
||||||
|
{
|
||||||
|
double dValue = 0;
|
||||||
|
int result = 0;
|
||||||
|
result = config_lookup_float(&(((Config *)cfg)->cfg), name, &dValue);
|
||||||
|
if (CONFIG_FALSE == result || CHECK_FLOAT_LIMIT(dValue)) {
|
||||||
|
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
|
||||||
|
}
|
||||||
|
*value = (float)dValue;
|
||||||
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
|
}
|
||||||
|
static const StatusCode ConfigSetFloatImpl(VConfig *cfg, const char *name, const float 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);
|
||||||
|
}
|
||||||
|
double dValue = value;
|
||||||
|
config_setting_set_float(setting, dValue);
|
||||||
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
|
}
|
||||||
static const StatusCode ConfigGetDoubleImpl(VConfig *cfg, const char *name, double *value)
|
static const StatusCode ConfigGetDoubleImpl(VConfig *cfg, const char *name, double *value)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
@ -197,7 +221,6 @@ static const StatusCode ConfigSetDoubleImpl(VConfig *cfg, const char *name, cons
|
||||||
config_setting_set_float(setting, value);
|
config_setting_set_float(setting, value);
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value)
|
static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
@ -239,6 +262,8 @@ static void ConfigImplInit(Config *cfg)
|
||||||
cfg->base.set_char = ConfigSetCharImpl;
|
cfg->base.set_char = ConfigSetCharImpl;
|
||||||
cfg->base.get_bool = ConfigGetBoolImpl;
|
cfg->base.get_bool = ConfigGetBoolImpl;
|
||||||
cfg->base.set_bool = ConfigSetBoolImpl;
|
cfg->base.set_bool = ConfigSetBoolImpl;
|
||||||
|
cfg->base.get_float = ConfigGetFloatImpl;
|
||||||
|
cfg->base.set_float = ConfigSetFloatImpl;
|
||||||
cfg->base.get_double = ConfigGetDoubleImpl;
|
cfg->base.get_double = ConfigGetDoubleImpl;
|
||||||
cfg->base.set_double = ConfigSetDoubleImpl;
|
cfg->base.set_double = ConfigSetDoubleImpl;
|
||||||
cfg->base.get_string = ConfigGetStringImpl;
|
cfg->base.get_string = ConfigGetStringImpl;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user