From c5691b1b957baaa4eaa385bfacd85361c59f7d4a Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Tue, 12 Dec 2023 11:46:09 +0800 Subject: [PATCH] =?UTF-8?q?[zhoulongyu]:=20=E4=BD=BF=E7=94=A8=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7=E7=BC=96=E8=AF=91=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/include/IIpcConfig.h | 8 +- middleware/IpcConfig/src/IIpcConfig.cpp | 13 +- middleware/IpcConfig/src/IpcConfig.cpp | 141 ++++++------------ middleware/IpcConfig/src/IpcConfig.h | 11 +- middleware/IpcConfig/src/IpcConfigMakePtr.cpp | 10 +- middleware/IpcConfig/src/IpcConfigMakePtr.h | 4 +- .../IpcConfig/src/IpcConfig_Test.cpp | 42 +++--- 7 files changed, 88 insertions(+), 141 deletions(-) diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index b39e736..04c0e6e 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,9 +15,9 @@ #ifndef IIPCCONFIG_H #define IIPCCONFIG_H #include "StatusCode.h" +#include #include #include -#include enum class IpcConfigKey { @@ -31,7 +31,7 @@ class IIpcConfig public: IIpcConfig() = default; virtual ~IIpcConfig() = default; - virtual const StatusCode ConfigFileSave(void) {return CreateStatusCode(STATUS_CODE_OK); } + virtual const StatusCode ConfigFileSave(void) { return CreateStatusCode(STATUS_CODE_OK); } static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } diff --git a/middleware/IpcConfig/src/IIpcConfig.cpp b/middleware/IpcConfig/src/IIpcConfig.cpp index 41ee2de..422240a 100644 --- a/middleware/IpcConfig/src/IIpcConfig.cpp +++ b/middleware/IpcConfig/src/IIpcConfig.cpp @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,15 +17,12 @@ std::shared_ptr &IIpcConfig::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); - if (impl) - { - if (instance.use_count() == 1) - { + if (impl) { + if (instance.use_count() == 1) { LogInfo("Instance changed succeed.\n"); instance = *impl; } - else - { + else { LogError("Can't changing the instance becase of using by some one.\n"); } } diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 1e28df9..a07ca58 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,32 +16,23 @@ #include "ILog.h" #include -#define CHECK_MAP(map) (map.size()==1? true:false) +#define CHECK_MAP(map) (map.size() == 1 ? true : false) IpcConfig::IpcConfig() { mCfgChanged = CONFIG_HAS_NOT_CHANGED; - mCfgMapInt.insert( - std::make_pair< - IpcConfigKey, - std::reference_wrapper>( - IpcConfigKey::TEST_NUM, - std::reference_wrapper(mAllData.testNum))); + mCfgMapInt.insert(std::make_pair>( + IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); std::map> innerMap; - innerMap.insert( - std::make_pair( - "test_string", - std::reference_wrapper(mAllData.testString))); + innerMap.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMap)); - } const StatusCode IpcConfig::Init(void) { memset(&mAllData, 0, sizeof(Config_s)); mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH); - if (nullptr == mCfg) - { + if (nullptr == mCfg) { LogError("Open config file failed.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } @@ -50,25 +41,20 @@ const StatusCode IpcConfig::Init(void) } const StatusCode IpcConfig::UnInit(void) { - if (CONFIG_HAS_CHANGED == mCfgChanged) - { + if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save config files.\n"); ConfigSaveFile(mCfg); } CloseConfigFile(mCfg); return CreateStatusCode(STATUS_CODE_OK); } -const StatusCode IpcConfig::ConfigFileSave(void) -{ - return ConfigSaveFile(mCfg); -} +const StatusCode IpcConfig::ConfigFileSave(void) { return ConfigSaveFile(mCfg); } const int IpcConfig::GetInt(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapInt.find(key); - if (iter != mCfgMapInt.end()) - { + if (iter != mCfgMapInt.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -79,13 +65,11 @@ void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) { std::map>::iterator iter; iter = mCfgMapInt.find(key); - if (iter != mCfgMapInt.end()) - { + if (iter != mCfgMapInt.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -93,8 +77,7 @@ const short IpcConfig::GetShort(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapShort.find(key); - if (iter != mCfgMapShort.end()) - { + if (iter != mCfgMapShort.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -105,13 +88,11 @@ void IpcConfig::SetShort(const IpcConfigKey &key, const short &value) { std::map>::iterator iter; iter = mCfgMapShort.find(key); - if (iter != mCfgMapShort.end()) - { + if (iter != mCfgMapShort.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -119,8 +100,7 @@ const long IpcConfig::GetLong(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLong.find(key); - if (iter != mCfgMapLong.end()) - { + if (iter != mCfgMapLong.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -131,13 +111,11 @@ void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) { std::map>::iterator iter; iter = mCfgMapLong.find(key); - if (iter != mCfgMapLong.end()) - { + if (iter != mCfgMapLong.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -145,8 +123,7 @@ const long long IpcConfig::GetLongLong(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLongLong.find(key); - if (iter != mCfgMapLongLong.end()) - { + if (iter != mCfgMapLongLong.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -157,13 +134,11 @@ void IpcConfig::SetLongLong(const IpcConfigKey &key, const long long &value) { std::map>::iterator iter; iter = mCfgMapLongLong.find(key); - if (iter != mCfgMapLongLong.end()) - { + if (iter != mCfgMapLongLong.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -171,8 +146,7 @@ const char IpcConfig::GetChar(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapChar.find(key); - if (iter != mCfgMapChar.end()) - { + if (iter != mCfgMapChar.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -183,13 +157,11 @@ void IpcConfig::SetChar(const IpcConfigKey &key, const char &character) { std::map>::iterator iter; iter = mCfgMapChar.find(key); - if (iter != mCfgMapChar.end()) - { + if (iter != mCfgMapChar.end()) { iter->second.get() = character; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -197,8 +169,7 @@ const float IpcConfig::GetFloat(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapFloat.find(key); - if (iter != mCfgMapFloat.end()) - { + if (iter != mCfgMapFloat.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -209,13 +180,11 @@ void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value) { std::map>::iterator iter; iter = mCfgMapFloat.find(key); - if (iter != mCfgMapFloat.end()) - { + if (iter != mCfgMapFloat.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -223,8 +192,7 @@ const double IpcConfig::GetDouble(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapDouble.find(key); - if (iter != mCfgMapDouble.end()) - { + if (iter != mCfgMapDouble.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -235,13 +203,11 @@ void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value) { std::map>::iterator iter; iter = mCfgMapDouble.find(key); - if (iter != mCfgMapDouble.end()) - { + if (iter != mCfgMapDouble.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -249,8 +215,7 @@ const long double IpcConfig::GetLongDouble(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLongDouble.find(key); - if (iter != mCfgMapLongDouble.end()) - { + if (iter != mCfgMapLongDouble.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -261,13 +226,11 @@ void IpcConfig::SetLongDouble(const IpcConfigKey &key, const long double &value) { std::map>::iterator iter; iter = mCfgMapLongDouble.find(key); - if (iter != mCfgMapLongDouble.end()) - { + if (iter != mCfgMapLongDouble.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -275,8 +238,7 @@ const bool IpcConfig::GetBool(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapBool.find(key); - if (iter != mCfgMapBool.end()) - { + if (iter != mCfgMapBool.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -287,13 +249,11 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value) { std::map>::iterator iter; iter = mCfgMapBool.find(key); - if (iter != mCfgMapBool.end()) - { + if (iter != mCfgMapBool.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -301,9 +261,8 @@ const std::string IpcConfig::GetString(const IpcConfigKey &key) { std::map>>::iterator iter; iter = mCfgMapString.find(key); - if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) - { - const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg + if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { + const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg return sv; } LogError("Can't find the key.\n"); @@ -314,15 +273,13 @@ void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) { std::map>>::iterator iter; iter = mCfgMapString.find(key); - if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) - { - strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[] - const char * name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * + if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { + strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[] + const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * ConfigSetString(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -330,8 +287,7 @@ void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) void IpcConfig::ReadAllConfigParameters(void) { StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum)); - if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) - { + if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) { LogWarning("test_num doesn't exist, will make it as default.\n"); mCfgChanged = CONFIG_HAS_CHANGED; constexpr int DEFAULT_TEST_NUM = 10; @@ -340,21 +296,18 @@ void IpcConfig::ReadAllConfigParameters(void) } const char *testString = NULL; StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString)); - if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) - { + if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) { LogWarning("test_string doesn't exist, will make it as default.\n"); mCfgChanged = CONFIG_HAS_CHANGED; - char DEFAULT_TEST_STRING[] = "undefine"; + char DEFAULT_TEST_STRING[] = "undefine"; strncpy(mAllData.testString, DEFAULT_TEST_STRING, sizeof(mAllData.testString)); ConfigSetString(mCfg, "test_string", mAllData.testString); } - else - { + else { strncpy(mAllData.testString, testString, sizeof(mAllData.testString)); } - if (CONFIG_HAS_CHANGED == mCfgChanged) - { + if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save the config file.\n"); mCfgChanged = CONFIG_HAS_NOT_CHANGED; ConfigSaveFile(mCfg); diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 6bad323..2e472fd 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -14,11 +14,11 @@ */ #ifndef IPCCONFIG_H #define IPCCONFIG_H -#include "StatusCode.h" -#include "IIpcConfig.h" #include "Config.h" -#include +#include "IIpcConfig.h" +#include "StatusCode.h" #include +#include constexpr bool CONFIG_HAS_CHANGED = true; constexpr bool CONFIG_HAS_NOT_CHANGED = false; @@ -63,7 +63,6 @@ public: const std::string GetString(const IpcConfigKey &key) override; void SetString(const IpcConfigKey &key, const std::string string) override; - private: void ReadAllConfigParameters(void); diff --git a/middleware/IpcConfig/src/IpcConfigMakePtr.cpp b/middleware/IpcConfig/src/IpcConfigMakePtr.cpp index de87cfb..a924eac 100644 --- a/middleware/IpcConfig/src/IpcConfigMakePtr.cpp +++ b/middleware/IpcConfig/src/IpcConfigMakePtr.cpp @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,8 +19,7 @@ bool CreateIpcConfig(void) { auto instance = std::make_shared(); StatusCode code = IpcConfigMakePtr::GetInstance()->CreateIpcConfig(instance); - if (IsCodeOK(code)) - { + if (IsCodeOK(code)) { LogInfo("CreateIpcConfig is ok.\n"); IIpcConfig::GetInstance(&instance); return true; @@ -30,8 +29,7 @@ bool CreateIpcConfig(void) std::shared_ptr &IpcConfigMakePtr::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); - if (impl) - { + if (impl) { instance = *impl; } return instance; diff --git a/middleware/IpcConfig/src/IpcConfigMakePtr.h b/middleware/IpcConfig/src/IpcConfigMakePtr.h index 36c737a..7ce0e20 100644 --- a/middleware/IpcConfig/src/IpcConfigMakePtr.h +++ b/middleware/IpcConfig/src/IpcConfigMakePtr.h @@ -3,9 +3,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp index 1088742..e204624 100644 --- a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -1,29 +1,29 @@ -#include "ILog.h" #include "IIpcConfig.h" +#include "ILog.h" // #include #include namespace IpcConfigTest { - // ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.Demo - TEST(IpcConfigTest, Demo) - { - CreateLogModule(); - CreateIpcConfig(); - ILogInit(LOG_INSTANCE_TYPE_END); - IIpcConfig::GetInstance()->Init(); - int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM); - LogInfo("Get testNum = %d\n", testNum); - const int num999 = 999; - IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, num999); +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.Demo +TEST(IpcConfigTest, Demo) +{ + CreateLogModule(); + CreateIpcConfig(); + ILogInit(LOG_INSTANCE_TYPE_END); + IIpcConfig::GetInstance()->Init(); + int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM); + LogInfo("Get testNum = %d\n", testNum); + const int num999 = 999; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, num999); - 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(); + 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()->UnInit(); - ILogUnInit(); - DestroyLogModule(); - } + IIpcConfig::GetInstance()->UnInit(); + ILogUnInit(); + DestroyLogModule(); +} } // namespace IpcConfigTest \ No newline at end of file