From a60d4769352a22f78ba3ebfaabcc987da10865c3 Mon Sep 17 00:00:00 2001 From: fancy <258828110.@qq.com> Date: Sat, 16 Sep 2023 08:36:11 -0700 Subject: [PATCH] Ipc config backup. --- middleware/IpcConfig/include/IIpcConfig.h | 1 + middleware/IpcConfig/src/IpcConfig.cpp | 41 +++++++++++++++++++++-- middleware/IpcConfig/src/IpcConfig.h | 16 ++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index 55cef41c..393fb215 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -16,6 +16,7 @@ public: virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const int GetInt(const IpcConfigKey &key) { return -1; } + virtual void SetInt(const IpcConfigKey &key, const int &value) {} }; bool CreateIpcConfig(void); #endif \ No newline at end of file diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 22117eb0..17052639 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -1,6 +1,17 @@ #include "IpcConfig.h" #include "ILog.h" #include +// #include +IpcConfig::IpcConfig() +{ + mCfgChanged = CONFIG_HAS_NOT_CHANGED; + mCfgMapInt.insert( + std::make_pair< + IpcConfigKey, + std::reference_wrapper>( + IpcConfigKey::TEST_NUM, + std::reference_wrapper(mAllData.testNum))); +} const StatusCode IpcConfig::Init(void) { memset(&mAllData, 0, sizeof(Config_s)); @@ -15,12 +26,38 @@ const StatusCode IpcConfig::Init(void) } const StatusCode IpcConfig::UnInit(void) { + if (CONFIG_HAS_CHANGED == mCfgChanged) + { + LogInfo("Save config files.\n"); + ConfigSaveFile(mCfg); + } CloseConfigFile(mCfg); return CreateStatusCode(STATUS_CODE_OK); } const int IpcConfig::GetInt(const IpcConfigKey &key) { - return 0; + std::map>::iterator iter; + iter = mCfgMapInt.find(key); + if (iter != mCfgMapInt.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr int UNKNOWN_CONFIG = -1; + return UNKNOWN_CONFIG; +} +void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) +{ + std::map>::iterator iter; + iter = mCfgMapInt.find(key); + if (iter != mCfgMapInt.end()) + { + iter->second.get() = value; + } + else + { + LogError("Can't find the key.\n"); + } } void IpcConfig::ReadAllConfigParameters(void) { @@ -30,7 +67,7 @@ void IpcConfig::ReadAllConfigParameters(void) { LogWarning("test_num doesn't exist, will make it as default.\n"); configChanging = true; - constexpr int DEFAULT_TEST_NUM = 0; + constexpr int DEFAULT_TEST_NUM = 10; mAllData.testNum = DEFAULT_TEST_NUM; ConfigSetInt(mCfg, "test_num", mAllData.testNum); } diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 28c379db..a6a328c9 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -4,24 +4,38 @@ #include "IIpcConfig.h" #include "Config.h" #include +#include +constexpr bool CONFIG_HAS_CHANGED = true; +constexpr bool CONFIG_HAS_NOT_CHANGED = false; typedef struct Config_s { int testNum; } Config_s; +class MapInt +{ +public: + MapInt() = default; + ~MapInt() = default; +}; class IpcConfig : public IIpcConfig { public: - IpcConfig() = default; + IpcConfig(); virtual ~IpcConfig() = default; const StatusCode Init(void) override; const StatusCode UnInit(void) override; const int GetInt(const IpcConfigKey &key) override; + void SetInt(const IpcConfigKey &key, const int &value) override; private: void ReadAllConfigParameters(void); private: + bool mCfgChanged; VConfig *mCfg; Config_s mAllData; + std::map> mCfgMapInt; + std::map> mCfgMapUInt; + std::map> mCfgMapString; }; #endif \ No newline at end of file