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

This commit is contained in:
jas 2023-12-13 01:21:52 +08:00
parent 9f07183680
commit e6b0040e7b
6 changed files with 70 additions and 10 deletions

View File

@ -23,6 +23,7 @@ enum class IpcConfigKey
{
TEST_NUM = 0,
TEST_SHORT,
TEST_LONG,
TEST_STRING,
END
};

View File

@ -30,6 +30,10 @@ IpcConfig::IpcConfig()
innerMapShort.insert(std::make_pair("test_short", std::reference_wrapper<short>(mAllData.testShort)));
mCfgMapShort.insert(std::make_pair(IpcConfigKey::TEST_SHORT, innerMapShort));
std::map<std::string, std::reference_wrapper<long>> innerMapLong;
innerMapLong.insert(std::make_pair("test_long", std::reference_wrapper<long>(mAllData.testLong)));
mCfgMapLong.insert(std::make_pair(IpcConfigKey::TEST_LONG, innerMapLong));
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));
@ -108,10 +112,10 @@ void IpcConfig::SetShort(const IpcConfigKey &key, const short &value)
}
const long IpcConfig::GetLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::reference_wrapper<long>>::iterator iter;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end()) {
return iter->second;
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr long UNKNOWN_CONFIG = -1;
@ -119,10 +123,12 @@ const long IpcConfig::GetLong(const IpcConfigKey &key)
}
void IpcConfig::SetLong(const IpcConfigKey &key, const long &value)
{
std::map<IpcConfigKey, std::reference_wrapper<long>>::iterator iter;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end()) {
iter->second.get() = value;
if (iter != mCfgMapLong.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 *
ConfigSetLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
@ -314,6 +320,15 @@ void IpcConfig::ReadAllConfigParameters(void)
ConfigSetShort(mCfg, "test_short", mAllData.testShort);
}
StatusCode longCode = ConfigGetLong(mCfg, "test_long", &(mAllData.testLong));
if (StatusCodeEqual(longCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_long doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr short DEFAULT_TEST_LONG_NUM = 12;
mAllData.testLong = DEFAULT_TEST_LONG_NUM;
ConfigSetLong(mCfg, "test_long", mAllData.testLong);
}
const char *testString = NULL;
StatusCode stringCode = ConfigGetString(mCfg, "test_string", &(testString));
if (StatusCodeEqual(stringCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {

View File

@ -27,6 +27,7 @@ typedef struct Config_s
{
int testNum;
short testShort;
long testLong;
CHAR_STRING testString;
} Config_s;
class MapInt
@ -73,7 +74,7 @@ private:
Config_s mAllData;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>> mCfgMapInt;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>> mCfgMapShort;
std::map<IpcConfigKey, std::reference_wrapper<long>> mCfgMapLong;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>> mCfgMapLong;
std::map<IpcConfigKey, std::reference_wrapper<long long>> mCfgMapLongLong;
std::map<IpcConfigKey, std::reference_wrapper<char>> mCfgMapChar;
std::map<IpcConfigKey, std::reference_wrapper<float>> mCfgMapFloat;

View File

@ -21,12 +21,17 @@ TEST(IpcConfigTest, Demo)
const int numShort = 888;
IIpcConfig::GetInstance()->SetShort(IpcConfigKey::TEST_SHORT, numShort);
long testLong = IIpcConfig::GetInstance()->GetLong(IpcConfigKey::TEST_LONG);
LogInfo("Get test_long = %d\n", testLong);
const int numLong = 777;
IIpcConfig::GetInstance()->SetLong(IpcConfigKey::TEST_LONG, numLong);
const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING);
LogInfo("Get testString = %s\n", testString.c_str());
const std::string string = "define";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::TEST_STRING, string);
IIpcConfig::GetInstance()->ConfigFileSave();
IIpcConfig::GetInstance()->ConfigFileSave();
IIpcConfig::GetInstance()->UnInit();
ILogUnInit();
DestroyLogModule();

View File

@ -48,7 +48,6 @@ const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value)
}
return cfg->set_int(cfg, name, value);
}
const StatusCode ConfigGetShort(VConfig *cfg, const char *name, short *value)
{
if (NULL == cfg) {
@ -63,6 +62,20 @@ const StatusCode ConfigSetShort(VConfig *cfg, const char *name, const short valu
}
return cfg->set_short(cfg, name, value);
}
const StatusCode ConfigGetLong(VConfig *cfg, const char *name, long *value)
{
if (NULL == cfg) {
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return cfg->get_long(cfg, name, value);
}
const StatusCode ConfigSetLong(VConfig *cfg, const char *name, const long value)
{
if (NULL == cfg) {
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return cfg->set_long(cfg, name, value);
}
const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value)
{

View File

@ -21,6 +21,7 @@
#include <unistd.h>
#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))
static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
{
@ -76,7 +77,6 @@ static const StatusCode ConfigGetShortImpl(VConfig *cfg, const char *name, short
*value = (short)intValue;
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigSetShortImpl(VConfig *cfg, const char *name, const short value)
{
config_setting_t *root, *setting;
@ -89,6 +89,29 @@ static const StatusCode ConfigSetShortImpl(VConfig *cfg, const char *name, const
config_setting_set_int(setting, intValue);
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigGetLongImpl(VConfig *cfg, const char *name, long *value)
{
long long llongValue = 0;
int result = 0;
result = config_lookup_int64(&(((Config *)cfg)->cfg), name, &llongValue);
if (CONFIG_FALSE == result && CHECK_LONG_LIMIT(llongValue)) {
return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
}
*value = (long)llongValue;
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigSetLongImpl(VConfig *cfg, const char *name, const long 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);
}
long long llongValue = value;
config_setting_set_int64(setting, llongValue);
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value)
{
int result = 0;
@ -122,6 +145,8 @@ static void ConfigImplInit(Config *cfg)
cfg->base.set_int = ConfigSetIntImpl;
cfg->base.get_short = ConfigGetShortImpl;
cfg->base.set_short = ConfigSetShortImpl;
cfg->base.get_long = ConfigGetLongImpl;
cfg->base.set_long = ConfigSetLongImpl;
cfg->base.get_string = ConfigGetStringImpl;
cfg->base.set_string = ConfigSetStringImpl;
cfg->base.save = ConfigSaveFileImpl;