[zhoulongyu]: 完成配置库的 char 数据类型

This commit is contained in:
jas 2023-12-13 21:13:21 +08:00
parent 85ab73ddc0
commit 4853e5c038
7 changed files with 77 additions and 11 deletions

View File

@ -25,6 +25,7 @@ enum class IpcConfigKey
TEST_SHORT,
TEST_LONG,
TEST_LLONG,
TEST_CHAR,
TEST_STRING,
END
};

View File

@ -38,6 +38,10 @@ IpcConfig::IpcConfig()
innerMapLLong.insert(std::make_pair("test_llong", std::reference_wrapper<long long>(mAllData.testLLong)));
mCfgMapLLong.insert(std::make_pair(IpcConfigKey::TEST_LLONG, innerMapLLong));
std::map<std::string, std::reference_wrapper<char>> innerMapChar;
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<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));
@ -107,7 +111,7 @@ void IpcConfig::SetShort(const IpcConfigKey &key, const short &value)
if (iter != mCfgMapShort.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 *
ConfigSetInt(mCfg, name, iter->second.begin()->second);
ConfigSetShort(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
@ -157,7 +161,7 @@ void IpcConfig::SetLLong(const IpcConfigKey &key, const long long &value)
if (iter != mCfgMapLLong.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 *
ConfigSetInt(mCfg, name, iter->second.begin()->second);
ConfigSetLLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
@ -166,10 +170,10 @@ void IpcConfig::SetLLong(const IpcConfigKey &key, const long long &value)
}
const char IpcConfig::GetChar(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::reference_wrapper<char>>::iterator iter;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end()) {
return iter->second;
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr char UNKNOWN_CONFIG = '\0';
@ -177,10 +181,12 @@ const char IpcConfig::GetChar(const IpcConfigKey &key)
}
void IpcConfig::SetChar(const IpcConfigKey &key, const char &character)
{
std::map<IpcConfigKey, std::reference_wrapper<char>>::iterator iter;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end()) {
iter->second.get() = character;
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = character;
const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char *
ConfigSetChar(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
@ -344,6 +350,15 @@ void IpcConfig::ReadAllConfigParameters(void)
ConfigSetLLong(mCfg, "test_llong", mAllData.testLLong);
}
StatusCode charCode = ConfigGetChar(mCfg, "test_char", &(mAllData.testChar));
if (StatusCodeEqual(charCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_char doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr char DEFAULT_TEST_CHAR_NUM = 'B';
mAllData.testChar = DEFAULT_TEST_CHAR_NUM;
ConfigSetChar(mCfg, "test_char", mAllData.testChar);
}
const char *testString = NULL;
StatusCode stringCode = ConfigGetString(mCfg, "test_string", &(testString));
if (StatusCodeEqual(stringCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {

View File

@ -29,6 +29,7 @@ typedef struct Config_s
short testShort;
long testLong;
long long testLLong;
char testChar;
CHAR_STRING testString;
} Config_s;
class MapInt
@ -77,7 +78,7 @@ private:
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>> mCfgMapShort;
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::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::reference_wrapper<double>> mCfgMapDouble;
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;

View File

@ -31,6 +31,11 @@ TEST(IpcConfigTest, Demo)
const long long numLLong = 666;
IIpcConfig::GetInstance()->SetLLong(IpcConfigKey::TEST_LLONG, numLLong);
char testChar = IIpcConfig::GetInstance()->GetChar(IpcConfigKey::TEST_CHAR);
LogInfo("Get test_char = %c\n", testChar);
const char numChar = 'A';
IIpcConfig::GetInstance()->SetChar(IpcConfigKey::TEST_CHAR, numChar);
const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING);
LogInfo("Get testString = %s\n", testString.c_str());
const std::string string = "define";

View File

@ -34,6 +34,8 @@ typedef struct v_config
const StatusCode (*set_long)(VConfig *, const char *, const long);
const StatusCode (*get_llong)(VConfig *, const char *, long long *);
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_string)(VConfig *, const char *, const char **);
const StatusCode (*set_string)(VConfig *, const char *, const char *);
const StatusCode (*save)(VConfig *);
@ -51,6 +53,8 @@ const StatusCode ConfigGetLong(VConfig *cfg, const char *name, long *value);
const StatusCode ConfigSetLong(VConfig *cfg, const char *name, const long value);
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 ConfigGetString(VConfig *cfg, const char *name, const char **value);
const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value);
#ifdef __cplusplus

View File

@ -90,6 +90,20 @@ const StatusCode ConfigSetLLong(VConfig *cfg, const char *name, const long long
}
return cfg->set_llong(cfg, name, value);
}
const StatusCode ConfigGetChar(VConfig *cfg, const char *name, char *value)
{
if (NULL == cfg) {
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return cfg->get_char(cfg, name, value);
}
const StatusCode ConfigSetChar(VConfig *cfg, const char *name, const char value)
{
if (NULL == cfg) {
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return cfg->set_char(cfg, name, value);
}
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value)
{
if (NULL == cfg) {

View File

@ -22,6 +22,7 @@
#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_CHAR_LIMIT(value) (value > CHAR_MAX ? false : (value < CHAR_MIN ? false : true))
static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
{
@ -106,7 +107,7 @@ static const StatusCode ConfigSetLongImpl(VConfig *cfg, const char *name, const
root = config_root_setting(&(((Config *)cfg)->cfg));
setting = config_setting_get_member(root, name);
if (!setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
setting = config_setting_add(root, name, CONFIG_TYPE_INT64);
}
long long llongValue = value;
config_setting_set_int64(setting, llongValue);
@ -127,11 +128,34 @@ static const StatusCode ConfigSetLLongImpl(VConfig *cfg, const char *name, const
root = config_root_setting(&(((Config *)cfg)->cfg));
setting = config_setting_get_member(root, name);
if (!setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT);
setting = config_setting_add(root, name, CONFIG_TYPE_INT64);
}
config_setting_set_int64(setting, value);
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigGetCharImpl(VConfig *cfg, const char *name, char *value)
{
int charValue = 0;
int result = 0;
result = config_lookup_int(&(((Config *)cfg)->cfg), name, &charValue);
if (CONFIG_FALSE == result && CHECK_CHAR_LIMIT(charValue)) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
*value = (char)charValue;
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigSetCharImpl(VConfig *cfg, const char *name, const char 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_INT);
}
int charValue = (int)value;
config_setting_set_int(setting, charValue);
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value)
{
int result = 0;
@ -169,6 +193,8 @@ static void ConfigImplInit(Config *cfg)
cfg->base.set_long = ConfigSetLongImpl;
cfg->base.get_llong = ConfigGetLLongImpl;
cfg->base.set_llong = ConfigSetLLongImpl;
cfg->base.get_char = ConfigGetCharImpl;
cfg->base.set_char = ConfigSetCharImpl;
cfg->base.get_string = ConfigGetStringImpl;
cfg->base.set_string = ConfigSetStringImpl;
cfg->base.save = ConfigSaveFileImpl;