From 18934033ce82a5ab9e3e2a385e29ba682cf94989 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Fri, 12 Jul 2024 11:30:22 +0800 Subject: [PATCH 01/48] add some header file --- middleware/FilesManager/include/IFilesManager.h | 1 + middleware/MediaManager/include/IMediaManager.h | 1 + utils/ConfigBase/src/ConfigBaseImpl.h | 1 + utils/MediaBase/src/MediaBase.cpp | 1 + utils/TcpModule/src/ITcpClient.h | 1 + 5 files changed, 5 insertions(+) diff --git a/middleware/FilesManager/include/IFilesManager.h b/middleware/FilesManager/include/IFilesManager.h index 1e086bda..a0c1eb56 100644 --- a/middleware/FilesManager/include/IFilesManager.h +++ b/middleware/FilesManager/include/IFilesManager.h @@ -16,6 +16,7 @@ #define I_FILES_MANAGER_H #include "StatusCode.h" #include +#include typedef struct save_file_info { save_file_info(const std::string &fileName); diff --git a/middleware/MediaManager/include/IMediaManager.h b/middleware/MediaManager/include/IMediaManager.h index 8f189f49..0a9dc6bc 100644 --- a/middleware/MediaManager/include/IMediaManager.h +++ b/middleware/MediaManager/include/IMediaManager.h @@ -17,6 +17,7 @@ #include "StatusCode.h" #include #include +#include bool CreateMediaManagerModule(void); bool DestroyMediaManagerModule(void); enum class MediaChannel diff --git a/utils/ConfigBase/src/ConfigBaseImpl.h b/utils/ConfigBase/src/ConfigBaseImpl.h index 56979ac0..58c35df5 100644 --- a/utils/ConfigBase/src/ConfigBaseImpl.h +++ b/utils/ConfigBase/src/ConfigBaseImpl.h @@ -16,6 +16,7 @@ #define CONFIG_BASE_IMPL_H #include "IConfigBase.h" #include +#include class ConfigBaseImpl : public IConfigBase { public: diff --git a/utils/MediaBase/src/MediaBase.cpp b/utils/MediaBase/src/MediaBase.cpp index 8b1c256f..44d0499b 100644 --- a/utils/MediaBase/src/MediaBase.cpp +++ b/utils/MediaBase/src/MediaBase.cpp @@ -19,6 +19,7 @@ #include #include #include +#include void *ICreateMediaBase(const MediaHandleType type) { return NewIMediaBase(type); diff --git a/utils/TcpModule/src/ITcpClient.h b/utils/TcpModule/src/ITcpClient.h index 8948a45d..ae4cf3ff 100644 --- a/utils/TcpModule/src/ITcpClient.h +++ b/utils/TcpModule/src/ITcpClient.h @@ -16,6 +16,7 @@ #define I_TCP_CLIENT_H #include "StatusCode.h" #include +#include class ITcpClient { public: From 2477158095866c9959b90d7cb9a1f177372cb17a Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Fri, 12 Jul 2024 14:49:48 +0800 Subject: [PATCH 02/48] add --disable-share --- utils/Servers/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/Servers/CMakeLists.txt b/utils/Servers/CMakeLists.txt index eb4eb669..d2b00b7c 100755 --- a/utils/Servers/CMakeLists.txt +++ b/utils/Servers/CMakeLists.txt @@ -81,7 +81,7 @@ add_custom_target( curl DEPENDS openssl COMMAND echo "Build curl. openssl path = ${EXTERNAL_SOURCE_PATH}" - COMMAND ./configure --without-zlib --prefix=${EXTERNAL_SOURCE_PATH}/curl --with-ssl=${EXTERNAL_SOURCE_PATH}/openssl/build ${CURL_HOST} CC=${CMAKE_C_COMPILER} + COMMAND ./configure --disable-shared --without-zlib --prefix=${EXTERNAL_SOURCE_PATH}/curl --with-ssl=${EXTERNAL_SOURCE_PATH}/openssl/build ${CURL_HOST} CC=${CMAKE_C_COMPILER} COMMAND make COMMAND cp ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/lib/.libs/lib*.a ${LIBS_OUTPUT_PATH} COMMAND cp ${EXTERNAL_SOURCE_PATH}/openssl/build/lib/lib*.a ${LIBS_OUTPUT_PATH} From dc4996eeea5a5c14f35a728757fb620144831a6c Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Fri, 12 Jul 2024 22:28:35 +0800 Subject: [PATCH 03/48] add some annotate --- utils/Log/include/ILog.h | 1 - utils/Log/include/ILogCpp.h | 5 +++++ utils/Log/src/ILogMakePtr.h | 5 +++++ utils/Log/src/easyloggingpp/default-logger.conf | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/utils/Log/include/ILog.h b/utils/Log/include/ILog.h index 9554c8b9..4755f8e2 100644 --- a/utils/Log/include/ILog.h +++ b/utils/Log/include/ILog.h @@ -65,7 +65,6 @@ extern "C" #define LOGE(...) #define LOGF(...) #endif - typedef struct i_log ILog; typedef struct i_log { void (*init)(ILog *, const enum LogInstance); diff --git a/utils/Log/include/ILogCpp.h b/utils/Log/include/ILogCpp.h index 36739a90..cb82656e 100644 --- a/utils/Log/include/ILogCpp.h +++ b/utils/Log/include/ILogCpp.h @@ -16,6 +16,11 @@ #define ILOGCPP_H #include "ILog.h" #include +/** + * @brief Abstract interface class + * If you want to add additional functions, you can derive a class from it to + * meet the product requirements. + */ class ILogCpp { public: diff --git a/utils/Log/src/ILogMakePtr.h b/utils/Log/src/ILogMakePtr.h index 07cb58d2..ae3737c3 100644 --- a/utils/Log/src/ILogMakePtr.h +++ b/utils/Log/src/ILogMakePtr.h @@ -12,6 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +/** + * @brief Create a log instance + * + */ #ifndef ILOG_MAKE_PTR_H #define ILOG_MAKE_PTR_H #include "ILogCpp.h" diff --git a/utils/Log/src/easyloggingpp/default-logger.conf b/utils/Log/src/easyloggingpp/default-logger.conf index 9e34afd2..f847709a 100644 --- a/utils/Log/src/easyloggingpp/default-logger.conf +++ b/utils/Log/src/easyloggingpp/default-logger.conf @@ -22,4 +22,4 @@ ENABLED = false FILENAME = "/tmp/logs/myeasylog-configuration.cpp-error.log" * FATAL: - ENABLED = false + ENABLED = false \ No newline at end of file From 8d10ca43a1d3a889c303fc589670cece628def0a Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 13 Jul 2024 14:52:03 +0800 Subject: [PATCH 04/48] repair Log bug --- utils/Log/include/ILog.h | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/Log/include/ILog.h b/utils/Log/include/ILog.h index 4755f8e2..9554c8b9 100644 --- a/utils/Log/include/ILog.h +++ b/utils/Log/include/ILog.h @@ -65,6 +65,7 @@ extern "C" #define LOGE(...) #define LOGF(...) #endif + typedef struct i_log ILog; typedef struct i_log { void (*init)(ILog *, const enum LogInstance); From eb49a6af0a776519fbd5586028b1ea305561bf39 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Fri, 19 Jul 2024 17:48:19 +0800 Subject: [PATCH 05/48] add readme.md --- utils/TcpModule/include/TcpModule.h | 6 +++--- utils/TcpModule/readme.md | 31 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 utils/TcpModule/readme.md diff --git a/utils/TcpModule/include/TcpModule.h b/utils/TcpModule/include/TcpModule.h index f3a57c5f..4b8a2b5b 100644 --- a/utils/TcpModule/include/TcpModule.h +++ b/utils/TcpModule/include/TcpModule.h @@ -16,7 +16,7 @@ #define TCP_MODULE_H #include #include -#define TCP_MODULE_WRITE_ERROR -1; +#define TCP_MODULE_WRITE_ERROR -1 #ifdef __cplusplus extern "C" { #endif @@ -30,8 +30,8 @@ typedef struct client_accept_parm } ClientAcceptParam; typedef struct tcp_server_parm { - const char *mIp; - const int mPort; + const char *mIp; /**Server ip */ + const int mPort; /**Server port */ const TcpAcceptClientFunction mAcceptClientFunc; const ClientAcceptParam mClientAcceptParam; } TcpServerParam; diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md new file mode 100644 index 00000000..4e693acd --- /dev/null +++ b/utils/TcpModule/readme.md @@ -0,0 +1,31 @@ +# TCP模块 +这个项目实现了一个用于网络通信的TCP模块,提供了服务器和客户端功能。 + +# 涉及的知识 +* 网络通信 +* 多线程处理 +* 回调函数 +* C++/C +* I/O多路复用 +* 套接字编程 +* TCP协议 + +# 各文件的作用 +* **TcpModule.h**:定义了TCP模块的公共接口和数据结构,包括服务器和客户端参数结构体、回调函数类型等。 + +* **TcpModule.cpp**:实现了TcpModule.h中定义的接口,包括创建和释放TCP服务器和客户端的函数。 + +* **ITcpClient.h/ITcpClient.cpp**:定义和实现了TCP客户端接口,包括初始化、读取数据、写入数据和关闭连接等虚函数。 + +* **ITcpServer.h/ITcpServer.cpp**:定义和实现了TCP服务器接口,包括初始化、关闭服务器等虚函数。 + +* **TcpClientImpl.h/TcpClientImpl.cpp**:实现了ITcpClient接口,是TCP客户端的具体实现。 + +* **TcpServerImpl.h/TcpServerImpl.cpp**:实现了ITcpServer接口,是TCP服务器的具体实现。 + +* **TcpModuleMakePtr.h/TcpModuleMakePtr.cpp**:提供了创建TCP服务器和客户端实例的工厂方法。 + +* **TcpServerHandle.h/TcpServerHandle.cpp**:管理TCP服务器的实例,提供了添加和获取服务器实例的方法。 + +* **TcpClientAcceptImpl.h/TcpClientAcceptImpl.cpp**:实现了ITcpClientAccept接口,处理客户端连接的接受和数据交换。 + From 8bf3d6f15acb84a63e66ff7b84106238d70fa678 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Mon, 22 Jul 2024 08:48:20 +0800 Subject: [PATCH 06/48] add TcpModule.cpp annotate --- utils/TcpModule/src/TcpModule.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/utils/TcpModule/src/TcpModule.cpp b/utils/TcpModule/src/TcpModule.cpp index 59fd681c..8f38357f 100644 --- a/utils/TcpModule/src/TcpModule.cpp +++ b/utils/TcpModule/src/TcpModule.cpp @@ -21,6 +21,14 @@ #include #include #include +/** + * @brief Verify that object is a legitimate (existing) server. + * + * @param object Save the address of the character pointer variable. + * If the value of the character pointer pointed by *object is "tcp_server", return turn + * @return true Indicates that the server exists. + * @return false Indicates that the server does not exist + */ static bool TcpServerObjectCheck(void *object) { if (nullptr == object) { @@ -33,6 +41,14 @@ static bool TcpServerObjectCheck(void *object) } return true; } +/** + * @brief Verify that object is a legitimate (existing) client. + * + * @param object Save the address of the character pointer variable. + * If the value of the character pointer pointed by *object is "tcp_client", return turn + * @return true Indicates that the client exists. + * @return false Indicates that the client does not exist + */ static bool TcpClientObjectCheck(void *object) { if (nullptr == object) { From cd4754e70bb0256d61b95b185229b7b75c18586e Mon Sep 17 00:00:00 2001 From: binbinnomoney <1749303514@qq.com> Date: Tue, 23 Jul 2024 22:24:49 +0800 Subject: [PATCH 07/48] =?UTF-8?q?=E6=8F=90=E4=BA=A4md=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/Log/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/Log/README.md b/utils/Log/README.md index 92cbe22d..121aaeb0 100644 --- a/utils/Log/README.md +++ b/utils/Log/README.md @@ -54,4 +54,5 @@ function(log_disable target) PROPERTY COMPILE_DEFINITIONS ${defs}) endforeach() endfunction() +... ``` \ No newline at end of file From 8e3939f08affcd14b0c9280e88a0f12f6cc31521 Mon Sep 17 00:00:00 2001 From: binbinnomoney <1749303514@qq.com> Date: Tue, 23 Jul 2024 23:09:21 +0800 Subject: [PATCH 08/48] =?UTF-8?q?=E6=8F=90=E4=BA=A4md=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/Log/README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/utils/Log/README.md b/utils/Log/README.md index 121aaeb0..5939fb4b 100644 --- a/utils/Log/README.md +++ b/utils/Log/README.md @@ -55,4 +55,23 @@ function(log_disable target) endforeach() endfunction() ... -``` \ No newline at end of file +``` + +## 1.4. 各文件的作用 +* **easyloggingpp**:在Log目录下发挥了日志记录、存储、管理的重要作用,为C++项目的开发和维护提供了强有力的支持。 + +* **lLogMakePtr.h**:提供了一个日志实例创建和管理的机制,可能是作为应用程序中日志系统的一部分。通过使用单例模式和虚函数,它允许在应用程序的不同部分之间共享和交换日志实现,同时保持对日志系统底层实现的抽象。 + +* **lLogMakePtr.cpp**: 提供了一个日志模块的创建和销毁机制,以及基于不同实现的日志对象的创建方法。 + +* **Log.h**: 提供了一个C语言兼容的日志接口,允许C或C++代码以统一的方式使用日志功能。 + +* **Log.cpp**: 提供了一个C兼容的日志接口实现,该实现基于C++的 ILogCpp 类来处理实际的日志记录。它允许C或C++代码使用统一的日志接口,而无需关心底层的日志实现细节。 + +* **LogEasylogging.h**: 为基于 Easylogging++ 的日志系统提供了一个C++接口实现。通过继承 ILogCpp 接口并实现其中的函数,它使得日志功能的使用者能够以一种统一和标准化的方式记录和管理日志,而无需关心底层的日志实现细节。 + +* **LogEasylogging.cpp**: 通过实现 LogEasylogging 类,为应用程序提供了一个基于 Easylogging++ 库的日志系统,支持不同级别的日志记录,并允许通过配置文件来定制日志的行为。 + +* **Loglmpl.h**: 提供了一个用于扩展和实现具体日志功能的基类,同时保持了与 ILogCpp 接口的兼容性。 + +* **Loglmpl.cpp**: 为空。 \ No newline at end of file From bd3b65e55d67829949e0c8377775501cf34617d6 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Thu, 25 Jul 2024 14:06:05 +0800 Subject: [PATCH 09/48] Add comments to client-related files --- utils/TcpModule/src/TcpClientImpl.cpp | 34 ++++++++++++++++++++++++--- utils/TcpModule/src/TcpClientImpl.h | 10 ++++---- utils/TcpModule/src/TcpModule.cpp | 2 +- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/utils/TcpModule/src/TcpClientImpl.cpp b/utils/TcpModule/src/TcpClientImpl.cpp index 346ed616..fa3b5a3e 100644 --- a/utils/TcpModule/src/TcpClientImpl.cpp +++ b/utils/TcpModule/src/TcpClientImpl.cpp @@ -23,12 +23,24 @@ #include #include #include +/** + * @brief Called when data is received on a TCP connection. + * + * @param io + * @param buf + * @param len + */ static void on_message(hio_t *io, void *buf, int len) { LogInfo("onmessage: %.*s\n", len, (char *)buf); TcpClientImpl *tcpClient = (TcpClientImpl *)hevent_userdata(io); tcpClient->Readed(buf, len); } +/** + * @brief Called when TCP connection is established. + * + * @param io + */ static void on_connect(hio_t *io) { LogInfo("onconnect: connfd=%d\n", hio_fd(io)); @@ -44,10 +56,14 @@ static void on_close(hio_t *io) TcpClientImpl::TcpClientImpl(const TcpClientParam ¶m, const void *object) : mParam(param), mObjectThis(object) { } +/** + * @brief Initialize TCP clients, create event loops, I/O objects, and set callback functions for connection and closure. + * + */ void TcpClientImpl::Init(void) { constexpr int NO_FALGS = 0; - mLoop = hloop_new(NO_FALGS); + mLoop = hloop_new(NO_FALGS); ///Initialize event loop if (nullptr == mLoop) { LogError("TcpClientImpl::Init hloop_new failed.\n"); return; @@ -68,6 +84,10 @@ void TcpClientImpl::Init(void) }; mTcpClientThread = std::thread(recvThread, impl); } +/** + * @brief De-initialize the TCP client, close the I/O object and wait for the receiving thread to end. + * + */ void TcpClientImpl::UnInit(void) { if (nullptr != mIo) { @@ -108,6 +128,10 @@ void TcpClientImpl::Closed(void) hloop_stop(mLoop); } } +/** + * @brief Run event loop + * + */ void TcpClientImpl::Loop(void) { if (nullptr == mLoop) { @@ -120,7 +144,7 @@ void TcpClientImpl::Loop(void) } std::shared_ptr *NewTcpClient(const TcpClientParam ¶m) { - LogInfo("Create tcp server object.\n"); + LogInfo("Create tcp client object.\n"); TcpClient *impl = (TcpClient *)malloc(sizeof(TcpClient)); if (nullptr == impl) { LogError("NewTcpServer::malloc failed.\n"); @@ -129,8 +153,12 @@ std::shared_ptr *NewTcpClient(const TcpClientParam ¶m) TcpClient tmp; memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient)); impl->mHeader.mCheckName = GetTcpClientModuleName(); + /** + * @brief ObjectThis points to the first address of the impl and offsets the address by ITcpClientHeader bytes, + * that is, skips the mHeader part of the impl. + */ std::shared_ptr *objectThis = (std::shared_ptr *)(((char *)impl) + sizeof(ITcpClientHeader)); impl->mTcpClient = std::make_shared(param, objectThis); - return objectThis; + return objectThis; ///ObjectThis is used to verify whether the client is legal. } \ No newline at end of file diff --git a/utils/TcpModule/src/TcpClientImpl.h b/utils/TcpModule/src/TcpClientImpl.h index 4e0a3379..eefe9883 100644 --- a/utils/TcpModule/src/TcpClientImpl.h +++ b/utils/TcpModule/src/TcpClientImpl.h @@ -35,12 +35,12 @@ public: void Loop(void); private: - std::mutex mMutex; - hloop_t *mLoop; - hio_t *mIo; - const TcpClientParam mParam; + std::mutex mMutex; ///A mutex lock used to synchronize access to shared resources. + hloop_t *mLoop; + hio_t *mIo; ///Socket handle + const TcpClientParam mParam; ///Basic information of the client, including port, ip, reading and closing. std::thread mTcpClientThread; - const void *mObjectThis; + const void *mObjectThis; ///ObjectThis is used to verify whether the client is legal. }; std::shared_ptr *NewTcpClient(const TcpClientParam ¶m); #endif \ No newline at end of file diff --git a/utils/TcpModule/src/TcpModule.cpp b/utils/TcpModule/src/TcpModule.cpp index 8f38357f..8cb81dc2 100644 --- a/utils/TcpModule/src/TcpModule.cpp +++ b/utils/TcpModule/src/TcpModule.cpp @@ -45,7 +45,7 @@ static bool TcpServerObjectCheck(void *object) * @brief Verify that object is a legitimate (existing) client. * * @param object Save the address of the character pointer variable. - * If the value of the character pointer pointed by *object is "tcp_client", return turn + * The correct object is returned by the function NewTcpClient in Itcpclientlmpl.cpp file. * @return true Indicates that the client exists. * @return false Indicates that the client does not exist */ From 0fc5d9a82ac782d53e72f76dfa1e89932b2d1076 Mon Sep 17 00:00:00 2001 From: binbinnomoney <1749303514@qq.com> Date: Thu, 25 Jul 2024 15:02:26 +0800 Subject: [PATCH 10/48] doxygen --- utils/Log/src/Log.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/utils/Log/src/Log.cpp b/utils/Log/src/Log.cpp index 40ac0778..5974d916 100644 --- a/utils/Log/src/Log.cpp +++ b/utils/Log/src/Log.cpp @@ -19,6 +19,11 @@ #include #include #include +/** + * @brief jieshao + * + * @param object + */ static void LogFree(ILog *object) { printf("log instance free.\n"); From b151dd7647dbcd4a863a4f0b67bb0527f0f880ab Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Thu, 25 Jul 2024 15:56:17 +0800 Subject: [PATCH 11/48] Add comments --- utils/TcpModule/src/TcpClientImpl.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/utils/TcpModule/src/TcpClientImpl.cpp b/utils/TcpModule/src/TcpClientImpl.cpp index fa3b5a3e..b2f27146 100644 --- a/utils/TcpModule/src/TcpClientImpl.cpp +++ b/utils/TcpModule/src/TcpClientImpl.cpp @@ -26,8 +26,8 @@ /** * @brief Called when data is received on a TCP connection. * - * @param io - * @param buf + * @param io Socket handle + * @param buf The transmitted data content * @param len */ static void on_message(hio_t *io, void *buf, int len) @@ -38,8 +38,7 @@ static void on_message(hio_t *io, void *buf, int len) } /** * @brief Called when TCP connection is established. - * - * @param io + * @param io Socket handle */ static void on_connect(hio_t *io) { @@ -47,6 +46,10 @@ static void on_connect(hio_t *io) hio_setcb_read(io, on_message); hio_read(io); } +/** + * @brief Called when tcp connection is disconnected. + * @param io Socket handle + */ static void on_close(hio_t *io) { LogInfo("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io)); @@ -74,9 +77,9 @@ void TcpClientImpl::Init(void) return; } hevent_set_userdata(io, this); - hio_setcb_connect(io, on_connect); + hio_setcb_connect(io, on_connect);///Set the callback function of successful connection, and there is no connection operation. hio_setcb_close(io, on_close); - hio_connect(io); + hio_connect(io); ///Connection operation mIo = io; std::shared_ptr impl = std::dynamic_pointer_cast(shared_from_this()); auto recvThread = [](std::shared_ptr tcpClient) { @@ -114,7 +117,7 @@ ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght) LogError("mIo is nullptr.\n"); return TCP_MODULE_WRITE_ERROR; } - return hio_write(mIo, buf, bufLenght); + return hio_write(mIo, buf, bufLenght); ///Returns the byte length of a packet.If it fails, an error code is returned. } void TcpClientImpl::Closed(void) { From 20e127d7df8f963bdc11e737350f18e7eaeb97cb Mon Sep 17 00:00:00 2001 From: binbinnomoney <1749303514@qq.com> Date: Fri, 26 Jul 2024 01:21:18 +0800 Subject: [PATCH 12/48] =?UTF-8?q?Log=E5=8F=98=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/utils/Log/CMakeLists.txt | 4 ++- test/utils/Log/src/ILogTest.cpp | 40 ++++++++++++++++++++++++++++ test/utils/Log/src/easylogging++.xml | 34 +++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/utils/Log/src/easylogging++.xml diff --git a/test/utils/Log/CMakeLists.txt b/test/utils/Log/CMakeLists.txt index 277058e2..d9249b23 100644 --- a/test/utils/Log/CMakeLists.txt +++ b/test/utils/Log/CMakeLists.txt @@ -6,11 +6,13 @@ include_directories( . ./src ./include - ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/Log/src/easyloggingpp ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include + ) + link_directories( ${LIBS_OUTPUT_PATH} ${EXTERNAL_LIBS_OUTPUT_PATH} diff --git a/test/utils/Log/src/ILogTest.cpp b/test/utils/Log/src/ILogTest.cpp index 4e3bb5b6..055ca0c5 100644 --- a/test/utils/Log/src/ILogTest.cpp +++ b/test/utils/Log/src/ILogTest.cpp @@ -1,3 +1,4 @@ +/* #include "ILog.h" // #include #include @@ -14,4 +15,43 @@ namespace ILogTest ILogUnInit(); DestroyLogModule(); } +} // namespace ILogTest +*/ + +#define ELPP_THREAD_SAFE +#include "easylogging++.h" +#include +#include + +INITIALIZE_EASYLOGGINGPP + +namespace ILogTest +{ + TEST(ILogTest, Demo) + { + + + // 如果你使用的是配置文件,确保它已正确设置并加载 + // 否则,你可以在这里通过编程方式配置elpp + + // 示例:动态设置配置以启用颜色输出(注意:这只是一个示例,实际配置可能不同) + // el::Configurations defaultConf; + // defaultConf.setToDefault(); + // defaultConf.set(el::Level::Global, + // el::ConfigurationType::Format, + // "%datetime %level %msg %func %loc"); + // defaultConf.set(el::Level::Global, + // el::ConfigurationType::EnableColoredLogMessages, + // "true"); + // el::Loggers::reconfigureLogger("default", defaultConf); + + // 使用easylogging++的日志宏 + LOG(INFO) << "hello world..."; + LOG(ERROR) << "create ... failed."; + LOG(DEBUG) << "a = " << 124 << " b = " << "apple"; + + // 注意:在单元测试中,你可能不需要初始化或销毁日志模块, + // 因为easylogging++已经为你处理了这些。 + // 但是,如果你有自己的日志封装或初始化代码,你应该根据需要进行调整。 + } } // namespace ILogTest \ No newline at end of file diff --git a/test/utils/Log/src/easylogging++.xml b/test/utils/Log/src/easylogging++.xml new file mode 100644 index 00000000..5d78fccd --- /dev/null +++ b/test/utils/Log/src/easylogging++.xml @@ -0,0 +1,34 @@ + + + + + + true + + + + + %datetime %level %msg + + + + + + + true + + + + + + + + + + + + + + + + \ No newline at end of file From 277a6798103ef9598b420c99534cfe6c3146289a Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Fri, 26 Jul 2024 16:51:55 +0800 Subject: [PATCH 13/48] add some annotate --- utils/TcpModule/readme.md | 1 + utils/TcpModule/src/ITcpClient.h | 5 ++++- utils/TcpModule/src/ITcpServer.h | 6 ++++++ utils/TcpModule/src/TcpClientImpl.cpp | 4 +--- utils/TcpModule/src/TcpClientImpl.h | 2 +- utils/TcpModule/src/TcpModule.cpp | 5 +++-- utils/TcpModule/src/TcpServerImpl.cpp | 2 +- 7 files changed, 17 insertions(+), 8 deletions(-) diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md index 4e693acd..389b72da 100644 --- a/utils/TcpModule/readme.md +++ b/utils/TcpModule/readme.md @@ -9,6 +9,7 @@ * I/O多路复用 * 套接字编程 * TCP协议 +* 事件循环 # 各文件的作用 * **TcpModule.h**:定义了TCP模块的公共接口和数据结构,包括服务器和客户端参数结构体、回调函数类型等。 diff --git a/utils/TcpModule/src/ITcpClient.h b/utils/TcpModule/src/ITcpClient.h index ae4cf3ff..43573c25 100644 --- a/utils/TcpModule/src/ITcpClient.h +++ b/utils/TcpModule/src/ITcpClient.h @@ -28,10 +28,13 @@ public: virtual ssize_t Write(const void *buf, const size_t bufLenght); virtual void Closed(void); }; +/** + * @brief Verify that the header of the client object is legal. + */ typedef struct i_tcp_client_header { const char *mCheckName; -} ITcpClientHeader; +} ITcpClientHeader; typedef struct tcp_client { ITcpClientHeader mHeader; diff --git a/utils/TcpModule/src/ITcpServer.h b/utils/TcpModule/src/ITcpServer.h index 098ad0b0..23ba73f5 100644 --- a/utils/TcpModule/src/ITcpServer.h +++ b/utils/TcpModule/src/ITcpServer.h @@ -17,6 +17,9 @@ #include "StatusCode.h" #include "TcpModule.h" #include +/** + * @brief Handle data interaction and connection state changes on established TCP connections. + */ class ITcpClientAccept { public: @@ -35,6 +38,9 @@ public: virtual void Init(void); virtual void UnInit(void); }; +/** + * @brief Verify that the header of the server object is legal. + */ typedef struct i_tcp_server_header { const char *mCheckName; diff --git a/utils/TcpModule/src/TcpClientImpl.cpp b/utils/TcpModule/src/TcpClientImpl.cpp index b2f27146..0ef5d44b 100644 --- a/utils/TcpModule/src/TcpClientImpl.cpp +++ b/utils/TcpModule/src/TcpClientImpl.cpp @@ -28,7 +28,7 @@ * * @param io Socket handle * @param buf The transmitted data content - * @param len + * @param len Byte length of transmitted data */ static void on_message(hio_t *io, void *buf, int len) { @@ -61,7 +61,6 @@ TcpClientImpl::TcpClientImpl(const TcpClientParam ¶m, const void *object) : } /** * @brief Initialize TCP clients, create event loops, I/O objects, and set callback functions for connection and closure. - * */ void TcpClientImpl::Init(void) { @@ -89,7 +88,6 @@ void TcpClientImpl::Init(void) } /** * @brief De-initialize the TCP client, close the I/O object and wait for the receiving thread to end. - * */ void TcpClientImpl::UnInit(void) { diff --git a/utils/TcpModule/src/TcpClientImpl.h b/utils/TcpModule/src/TcpClientImpl.h index eefe9883..3bb3262f 100644 --- a/utils/TcpModule/src/TcpClientImpl.h +++ b/utils/TcpModule/src/TcpClientImpl.h @@ -36,7 +36,7 @@ public: private: std::mutex mMutex; ///A mutex lock used to synchronize access to shared resources. - hloop_t *mLoop; + hloop_t *mLoop; ///Event loop, listening for io objects hio_t *mIo; ///Socket handle const TcpClientParam mParam; ///Basic information of the client, including port, ip, reading and closing. std::thread mTcpClientThread; diff --git a/utils/TcpModule/src/TcpModule.cpp b/utils/TcpModule/src/TcpModule.cpp index 8cb81dc2..5ebc8a7a 100644 --- a/utils/TcpModule/src/TcpModule.cpp +++ b/utils/TcpModule/src/TcpModule.cpp @@ -25,7 +25,8 @@ * @brief Verify that object is a legitimate (existing) server. * * @param object Save the address of the character pointer variable. - * If the value of the character pointer pointed by *object is "tcp_server", return turn + * Should be substituted into the private member objectThis of the client instance. + * If the value of the character pointer pointed by *object is "tcp_server", return turn * @return true Indicates that the server exists. * @return false Indicates that the server does not exist */ @@ -45,7 +46,7 @@ static bool TcpServerObjectCheck(void *object) * @brief Verify that object is a legitimate (existing) client. * * @param object Save the address of the character pointer variable. - * The correct object is returned by the function NewTcpClient in Itcpclientlmpl.cpp file. + * * @return true Indicates that the client exists. * @return false Indicates that the client does not exist */ diff --git a/utils/TcpModule/src/TcpServerImpl.cpp b/utils/TcpModule/src/TcpServerImpl.cpp index d82bdf1e..6d646670 100644 --- a/utils/TcpModule/src/TcpServerImpl.cpp +++ b/utils/TcpModule/src/TcpServerImpl.cpp @@ -109,7 +109,7 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param) void TcpServerImpl::Init(void) { constexpr int NO_FALGS = 0; - mLoop = hloop_new(NO_FALGS); + mLoop = hloop_new(NO_FALGS); ///Initialize event loop if (nullptr == mLoop) { LogError("hloop_new failed\n"); return; From 3fbec1f9bcb049f62214700f73bdfe1bac2f166b Mon Sep 17 00:00:00 2001 From: ssslleep <2720057338@qq.com> Date: Fri, 26 Jul 2024 09:05:43 +0000 Subject: [PATCH 14/48] .md Signed-off-by: ssslleep <2720057338@qq.com> --- utils/UartDevice/readme.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 utils/UartDevice/readme.md diff --git a/utils/UartDevice/readme.md b/utils/UartDevice/readme.md new file mode 100644 index 00000000..2480b329 --- /dev/null +++ b/utils/UartDevice/readme.md @@ -0,0 +1,16 @@ +# UartDevice模块 +该模块是UART设备接口模块,提供了UART 设备操作接口,通过uart通信操作相关的硬件设备。 + +# 涉及的知识 +* uart通信协议 +* 多线程 +* 系统调用函数的使用 +* C++类和对象 +* 函数指针 + + +# 各文件的作用 +* **UartDevice.h**:定义了一个名为uart_info的结构体存储UART设备的配置信息,定义了用于UART通信的函数接口,这些函数通常与UART硬件的底层实现相对应。 +* **UartDevice.cpp**:提供了一个跨平台的UART设备接口模块,使得上层应用可以通过统一的接口来操作不同的UART硬件设备。 +* **UartDeviceImpl.cpp**:提供了UART 设备操作接口,允许用户打开、发送和接收数据到 UART 设备,实现了 UART 设备的配置、数据发送和接收等功能。 +* **UartDeviceImpl.h**:定义了一个用于UART设备操作的类 UartDeviceImpl 和相关的结构体和函数,提供了一种通过接口和智能指针封装具体实现的方式来管理UART设备。 \ No newline at end of file From fe85f7be329e623a4da7414e5c21c35e2eac7e9e Mon Sep 17 00:00:00 2001 From: binbinnomoney <1749303514@qq.com> Date: Fri, 26 Jul 2024 22:50:02 +0800 Subject: [PATCH 15/48] =?UTF-8?q?Log=E5=8F=98=E9=A2=9C=E8=89=B2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/utils/Log/src/ILogTest.cpp | 37 +++++++++++++++++++++++++++- test/utils/Log/src/easylogging++.xml | 4 +-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/test/utils/Log/src/ILogTest.cpp b/test/utils/Log/src/ILogTest.cpp index 055ca0c5..91474bfa 100644 --- a/test/utils/Log/src/ILogTest.cpp +++ b/test/utils/Log/src/ILogTest.cpp @@ -29,6 +29,41 @@ namespace ILogTest { TEST(ILogTest, Demo) { + // 假设你已经在全局范围内设置了日志格式 + el::Configurations defaultConf; + defaultConf.setToDefault(); + /* + defaultConf.set(el::Level::Global, + el::ConfigurationType::Format, + "\x1b[32m%datetime %level %msg\x1b[0m"); // 绿色日志消息 + + */ + + // 设置 INFO 级别的日志格式为蓝色 + defaultConf.set(el::Level::Info, + el::ConfigurationType::Format, + "\x1b[32m%datetime %level %msg\x1b[0m"); + + // 设置 WARNING 级别的日志格式为黄色 + defaultConf.set(el::Level::Debug, + el::ConfigurationType::Format, + "\x1b[33m%datetime %level %msg\x1b[0m"); + + // 设置 ERROR 级别的日志格式为红色 + defaultConf.set(el::Level::Error, + el::ConfigurationType::Format, + "\x1b[31m%datetime %level %msg\x1b[0m"); + + // 注意:上面的格式将所有日志消息都设置为绿色。 + // 如果你想要为不同的日志级别设置不同的颜色,你需要为每个级别分别设置格式。 + + // 另一种方法是使用条件格式,但这在 INI 配置文件中可能更难以实现, + // 因此你可能需要通过编程方式来实现。 + + el::Loggers::reconfigureLogger("default", defaultConf); + + + // 如果你使用的是配置文件,确保它已正确设置并加载 @@ -46,7 +81,7 @@ namespace ILogTest // el::Loggers::reconfigureLogger("default", defaultConf); // 使用easylogging++的日志宏 - LOG(INFO) << "hello world..."; + LOG(INFO) << "hello world......."; LOG(ERROR) << "create ... failed."; LOG(DEBUG) << "a = " << 124 << " b = " << "apple"; diff --git a/test/utils/Log/src/easylogging++.xml b/test/utils/Log/src/easylogging++.xml index 5d78fccd..6c52d073 100644 --- a/test/utils/Log/src/easylogging++.xml +++ b/test/utils/Log/src/easylogging++.xml @@ -2,9 +2,7 @@ - - true - + true From 5e978069b6a180f21932bf48db02b12fe9c92a72 Mon Sep 17 00:00:00 2001 From: binbinnomoney <1749303514@qq.com> Date: Fri, 26 Jul 2024 23:02:56 +0800 Subject: [PATCH 16/48] =?UTF-8?q?Log=E5=8F=98=E9=A2=9C=E8=89=B2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/utils/Log/src/ILogTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/utils/Log/src/ILogTest.cpp b/test/utils/Log/src/ILogTest.cpp index 91474bfa..143b14fa 100644 --- a/test/utils/Log/src/ILogTest.cpp +++ b/test/utils/Log/src/ILogTest.cpp @@ -39,7 +39,7 @@ namespace ILogTest */ - // 设置 INFO 级别的日志格式为蓝色 + // 设置 INFO 级别的日志格式为绿色 defaultConf.set(el::Level::Info, el::ConfigurationType::Format, "\x1b[32m%datetime %level %msg\x1b[0m"); @@ -81,7 +81,7 @@ namespace ILogTest // el::Loggers::reconfigureLogger("default", defaultConf); // 使用easylogging++的日志宏 - LOG(INFO) << "hello world......."; + LOG(INFO) << "hello world."; LOG(ERROR) << "create ... failed."; LOG(DEBUG) << "a = " << 124 << " b = " << "apple"; From d0b38294c61d6196a3ab300247a297de6ae73a94 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 08:41:50 +0800 Subject: [PATCH 17/48] remove environmental contruction --- Environmental.md | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Environmental.md diff --git a/Environmental.md b/Environmental.md new file mode 100644 index 00000000..a192e69f --- /dev/null +++ b/Environmental.md @@ -0,0 +1,84 @@ +# 前期需要下载的软件 +* VMware Workstration +* git bash +* vscode +* Beyond Compare +* MoBaXterm + +# 前期快速上手 +* git的基本操作(我们主要使用git进行代码管理) +* Linux的基本操作(主要是命令行操作,了解常用的命令,了解怎么使用命令行管理文件) + //csdn相关文章写得很详尽,搭配上ai工具边用边学可以很快地熟悉常用的指令 + + +# 前期需要准备的基本环境 +1. 在VMware Workstation中安装Ubuntu,安装好gcc和g++编译器,并配置好网络(能正常上网) +2. 学会vscode用ssh远程连接Ubuntu,下载CMake、CMake Tools、Baidu Comate(ai工具,对后续学习有比较大的帮助)等基本插件 +3. 创建一个gitee账号 + +# 编译过程 +1. 将gitee上的源码文件保存到虚拟机中 +2. 依照顺序执行以下命令 + + +* 安装libssl-dev软件包 + +``` +sudo apt-get update +sudo apt-get install openssl +sudo apt-get install libssl-dev +``` + +* 安装LDAP软件包 +``` +sudo apt-get update +sudo apt-get install libldap2-dev +``` + + +* 安装cmake(llvm使用cmake编译,cmake版本要求 3.20以上,此处安装cmake-3.27.4) + cmake源码压缩包所在的路径:embedded-framework/tools/cmake/cmake-3.27.4.tar.gz + 在embedded-framework/tools/cmake目录下执行以下指令来安装cmake-3.27.4: +``` +tar zxvf cmake-3.27.4.tar.gz +cd cmake-3.27.4 +sudo apt-get install openssl // 如果执行./bootstrap提示缺少ssl相关资源,执行此安装命令 +./bootstrap +make +sudo make install +cmake --version //查看版本,确认安装完成 +``` + +* 安装llvm +``` +// 下载源码 +git clone https://github.com/llvm/llvm-project.git +cd llvm-project +mkdir build +cd build +cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm +make -j8 //这个过程会有点久 +find ./ -name clang-tidy //查看是否有clang-tidy的文件 +clang-tidy --version //查看版本,确认编译完成 +``` + +3. 进行编译,在embedded-framework目录下执行以下命令: +``` +make clean //CmakeLists.txt是生成Makefile文件的构建脚本,如果CmakeLists.txt被修改需要重新执行make clean清除之前的构建文件 +make cmake //构建源码,生成Makefile文件 +cd cmake-shell //在中间文件目录进行编译,把所有中间文件创建在此 +make //编译全部输出构建文件 +``` + + + + + + + +# 问题汇总 +1. gcc编译器版本过低后续编译会出bug,亲测gcc 7.5.0版本不能用,可以使用gcc --version命令查看自己gcc的版本,版本过低需要更新版本,可以自行搜索怎么更新也可以参考https://blog.csdn.net/weixin_43354152/article/details/129247408 + +2. 下载llvm源码时git clone https://github.com/llvm/llvm-project.git 这个过程可能会经常报网络错误,多试几次,可以用网易uu加速器的学术资源加速可能会好下一点 + +3. 编译llvm过程可能会多次卡顿退出,可能的原因有:磁盘空间不足、编译器版本过低、缺少依赖等。可以尝试多线程编译(make -j8) \ No newline at end of file From e5b8188fe5742ec8c2133aba4361265f25d9ba79 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 08:58:10 +0800 Subject: [PATCH 18/48] add bug.md --- bug.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 bug.md diff --git a/bug.md b/bug.md new file mode 100644 index 00000000..792a1bd2 --- /dev/null +++ b/bug.md @@ -0,0 +1,3 @@ +# 仓库中隐藏的bug +1. “小”指针强制转换成“大”指针,可能造成内存的非法操作 +* embedded.framework/utils/confaBase /src/ConfigBaselmpl.cpp \ No newline at end of file From c81e031a80d6d4e331b94651810d3460f2fc1fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 05:25:35 +0000 Subject: [PATCH 19/48] configbasee README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- readme.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 00000000..e2e131f6 --- /dev/null +++ b/readme.md @@ -0,0 +1,34 @@ +# CONFIGBASE + +#涉及的知识 +---------------------------------------------------------------- +1.CMake + +2.C/C++ + +3.配置文件 + +4.状态码 + +5.libconfig库 + +### 各文件的作用 +*1.conffig_base.cmake:定义了一个名为config_owner的函数,该函数旨在为一个特定的CMake目标(比如一个可执行文件或库)的所有源文件添加一个新的编译定义(COMPILE_DEFINITIONS)。在这个上下文中,编译定义通常用于在编译时向源代码中注入宏定义,这些宏定义可以在源代码中被用作条件编译的开关。* + +*2.ConfigBase.h:是一个C/C++兼容的头文件(CONFIG_BASE_H),它定义了一个配置管理的基础接口。这个接口提供了打开、保存、关闭配置文件以及获取和设置配置文件中不同类型数据的功能。* + +*3.ConfigBase.cpp:实现了一个配置文件管理器的接口封装,它提供了一系列用于打开、关闭、保存配置文件以及获取和设置配置文件中的不同类型配置项的功能。* + +*4.ConfigBaseCode.c:涉及到一个用于处理配置代码(ConfigCode)的系统,这些配置代码似乎是从一个更大的状态码(StatusCode)系统中派生出来的。代码依赖于几个自定义的类型和函数,包括StatusCode结构体、ILog接口(通过LogInfo和LogError宏或函数实现)、以及ConfigBase和ConfigBaseCode。* + +*5.ConfigBaseCode.h:用于定义与配置代码相关的接口。它采用了C和C++的混合编程方式,并通过预处理器指令来控制代码的包含和编译。* + +*6.ConfigBaselmpl.cpp:定义了一个名为ConfigBaseImpl的类,它用于处理基于文本的配置文件(如INI或类似格式的文件),使用libconfig库来实现这一功能。libconfig是一个用于处理结构化配置文件的C库,它支持读取和写入配置文件。ConfigBaseImpl类封装了libconfig的基本操作,如打开、关闭配置文件,以及读取和写入配置项的值。* + +*7.ConfigBaselmpl.h:定义了一个名为 ConfigBaseImpl 的类,它实现了 IConfigBase 接口,用于处理配置文件。它使用了 libconfig 库来解析和生成配置文件。* + +*8.lConfigBase.cpp:定义了一个名为 IConfigBase 的接口类,用于配置文件的操作,包括打开、关闭、读取和设置不同类型的配置值(如整数、短整数、长整数、长长整数、字符、布尔值、浮点数、双精度浮点数和字符串)。然而,这些函数的具体实现在 IConfigBase 类中都被设置为返回特定的状态码 STATUS_CODE_VIRTUAL_FUNCTION,这通常表示该函数是虚函数,需要在派生类中具体实现。* + +*9.lConfigBase.h:定义了一个C++接口(IConfigBase)和一些相关的类型及函数声明,主要用于配置文件的操作。* + +*10.CMakeLists.txt:是一个CMake脚本,用于配置和编译一个静态库项目(名为ConfigBase),并包含了一些额外的自定义目标和依赖管理。* \ No newline at end of file From b343bb5ecf7d901c5ea923d6835b56bcb490e367 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 05:56:42 +0000 Subject: [PATCH 20/48] Revert "configbasee README.md." This reverts commit c81e031a80d6d4e331b94651810d3460f2fc1fe0. --- readme.md | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 readme.md diff --git a/readme.md b/readme.md deleted file mode 100644 index e2e131f6..00000000 --- a/readme.md +++ /dev/null @@ -1,34 +0,0 @@ -# CONFIGBASE - -#涉及的知识 ----------------------------------------------------------------- -1.CMake - -2.C/C++ - -3.配置文件 - -4.状态码 - -5.libconfig库 - -### 各文件的作用 -*1.conffig_base.cmake:定义了一个名为config_owner的函数,该函数旨在为一个特定的CMake目标(比如一个可执行文件或库)的所有源文件添加一个新的编译定义(COMPILE_DEFINITIONS)。在这个上下文中,编译定义通常用于在编译时向源代码中注入宏定义,这些宏定义可以在源代码中被用作条件编译的开关。* - -*2.ConfigBase.h:是一个C/C++兼容的头文件(CONFIG_BASE_H),它定义了一个配置管理的基础接口。这个接口提供了打开、保存、关闭配置文件以及获取和设置配置文件中不同类型数据的功能。* - -*3.ConfigBase.cpp:实现了一个配置文件管理器的接口封装,它提供了一系列用于打开、关闭、保存配置文件以及获取和设置配置文件中的不同类型配置项的功能。* - -*4.ConfigBaseCode.c:涉及到一个用于处理配置代码(ConfigCode)的系统,这些配置代码似乎是从一个更大的状态码(StatusCode)系统中派生出来的。代码依赖于几个自定义的类型和函数,包括StatusCode结构体、ILog接口(通过LogInfo和LogError宏或函数实现)、以及ConfigBase和ConfigBaseCode。* - -*5.ConfigBaseCode.h:用于定义与配置代码相关的接口。它采用了C和C++的混合编程方式,并通过预处理器指令来控制代码的包含和编译。* - -*6.ConfigBaselmpl.cpp:定义了一个名为ConfigBaseImpl的类,它用于处理基于文本的配置文件(如INI或类似格式的文件),使用libconfig库来实现这一功能。libconfig是一个用于处理结构化配置文件的C库,它支持读取和写入配置文件。ConfigBaseImpl类封装了libconfig的基本操作,如打开、关闭配置文件,以及读取和写入配置项的值。* - -*7.ConfigBaselmpl.h:定义了一个名为 ConfigBaseImpl 的类,它实现了 IConfigBase 接口,用于处理配置文件。它使用了 libconfig 库来解析和生成配置文件。* - -*8.lConfigBase.cpp:定义了一个名为 IConfigBase 的接口类,用于配置文件的操作,包括打开、关闭、读取和设置不同类型的配置值(如整数、短整数、长整数、长长整数、字符、布尔值、浮点数、双精度浮点数和字符串)。然而,这些函数的具体实现在 IConfigBase 类中都被设置为返回特定的状态码 STATUS_CODE_VIRTUAL_FUNCTION,这通常表示该函数是虚函数,需要在派生类中具体实现。* - -*9.lConfigBase.h:定义了一个C++接口(IConfigBase)和一些相关的类型及函数声明,主要用于配置文件的操作。* - -*10.CMakeLists.txt:是一个CMake脚本,用于配置和编译一个静态库项目(名为ConfigBase),并包含了一些额外的自定义目标和依赖管理。* \ No newline at end of file From f6d84acc304363f86bbd9ad10745d39424a57429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:11:53 +0000 Subject: [PATCH 21/48] configbasee README.md. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/readme.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 utils/ConfigBase/readme.md diff --git a/utils/ConfigBase/readme.md b/utils/ConfigBase/readme.md new file mode 100644 index 00000000..e2e131f6 --- /dev/null +++ b/utils/ConfigBase/readme.md @@ -0,0 +1,34 @@ +# CONFIGBASE + +#涉及的知识 +---------------------------------------------------------------- +1.CMake + +2.C/C++ + +3.配置文件 + +4.状态码 + +5.libconfig库 + +### 各文件的作用 +*1.conffig_base.cmake:定义了一个名为config_owner的函数,该函数旨在为一个特定的CMake目标(比如一个可执行文件或库)的所有源文件添加一个新的编译定义(COMPILE_DEFINITIONS)。在这个上下文中,编译定义通常用于在编译时向源代码中注入宏定义,这些宏定义可以在源代码中被用作条件编译的开关。* + +*2.ConfigBase.h:是一个C/C++兼容的头文件(CONFIG_BASE_H),它定义了一个配置管理的基础接口。这个接口提供了打开、保存、关闭配置文件以及获取和设置配置文件中不同类型数据的功能。* + +*3.ConfigBase.cpp:实现了一个配置文件管理器的接口封装,它提供了一系列用于打开、关闭、保存配置文件以及获取和设置配置文件中的不同类型配置项的功能。* + +*4.ConfigBaseCode.c:涉及到一个用于处理配置代码(ConfigCode)的系统,这些配置代码似乎是从一个更大的状态码(StatusCode)系统中派生出来的。代码依赖于几个自定义的类型和函数,包括StatusCode结构体、ILog接口(通过LogInfo和LogError宏或函数实现)、以及ConfigBase和ConfigBaseCode。* + +*5.ConfigBaseCode.h:用于定义与配置代码相关的接口。它采用了C和C++的混合编程方式,并通过预处理器指令来控制代码的包含和编译。* + +*6.ConfigBaselmpl.cpp:定义了一个名为ConfigBaseImpl的类,它用于处理基于文本的配置文件(如INI或类似格式的文件),使用libconfig库来实现这一功能。libconfig是一个用于处理结构化配置文件的C库,它支持读取和写入配置文件。ConfigBaseImpl类封装了libconfig的基本操作,如打开、关闭配置文件,以及读取和写入配置项的值。* + +*7.ConfigBaselmpl.h:定义了一个名为 ConfigBaseImpl 的类,它实现了 IConfigBase 接口,用于处理配置文件。它使用了 libconfig 库来解析和生成配置文件。* + +*8.lConfigBase.cpp:定义了一个名为 IConfigBase 的接口类,用于配置文件的操作,包括打开、关闭、读取和设置不同类型的配置值(如整数、短整数、长整数、长长整数、字符、布尔值、浮点数、双精度浮点数和字符串)。然而,这些函数的具体实现在 IConfigBase 类中都被设置为返回特定的状态码 STATUS_CODE_VIRTUAL_FUNCTION,这通常表示该函数是虚函数,需要在派生类中具体实现。* + +*9.lConfigBase.h:定义了一个C++接口(IConfigBase)和一些相关的类型及函数声明,主要用于配置文件的操作。* + +*10.CMakeLists.txt:是一个CMake脚本,用于配置和编译一个静态库项目(名为ConfigBase),并包含了一些额外的自定义目标和依赖管理。* \ No newline at end of file From cacb6dcbf82c7c1847c54bb4cdd00a393ff9caad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:40:25 +0000 Subject: [PATCH 22/48] configbasee MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase.h | 56 ++++++++++++++++++++++++++++++++++++++ utils/ConfigBaseCode.h | 35 ++++++++++++++++++++++++ utils/ConfigBaseImpl.h | 52 +++++++++++++++++++++++++++++++++++ utils/IConfigBase.h | 62 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 utils/ConfigBase.h create mode 100644 utils/ConfigBaseCode.h create mode 100644 utils/ConfigBaseImpl.h create mode 100644 utils/IConfigBase.h diff --git a/utils/ConfigBase.h b/utils/ConfigBase.h new file mode 100644 index 00000000..576ffd9d --- /dev/null +++ b/utils/ConfigBase.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CONFIG_BASE_H +#define CONFIG_BASE_H +#include "StatusCode.h" +#ifdef __cplusplus +extern "C" //声明是为了让C++编译器按照C语言的方式来处理被extern "C"包围的代码。这是必要的 +{ +#endif +//#ifndef CONFIG_BASE_H、#define CONFIG_BASE_H 和 #endif 这三个预处理指令一起工作,确保头文件CONFIG_BASE_H在任何编译单元中只被包含(include)一次。这是通过定义一个唯一的宏(CONFIG_BASE_H)来实现的,如果宏已定义,则跳过文件内容。 +enum CONFIG_CODE//定义了一个枚举类型CONFIG_CODE,用于可能的配置错误代码。它开始于STATUS_CODE_END(这里假设STATUS_CODE_END是在StatusCode.h中定义的某个状态码,表示一组状态码的结束),并定义了一个CONFIG_CODE_END作为枚举的结束标志。 +{ + CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, + CONFIG_CODE_END +}; +// StatusCode ConfigInit(void); +// StatusCode ConfigUnInit(void); +void *OpenConfigFile(const char *fileName); +StatusCode ConfigSaveFile(void *object); +void CloseConfigFile(void *object); +StatusCode ConfigGetInt(void *object, const char *name, int *value); +StatusCode ConfigSetInt(void *object, const char *name, const int value); +StatusCode ConfigGetShort(void *object, const char *name, short *value); +StatusCode ConfigSetShort(void *object, const char *name, const short value); +StatusCode ConfigGetLong(void *object, const char *name, long *value); +StatusCode ConfigSetLong(void *object, const char *name, const long value); +StatusCode ConfigGetLLong(void *object, const char *name, long long *value); +StatusCode ConfigSetLLong(void *object, const char *name, const long long value); +StatusCode ConfigGetChar(void *object, const char *name, char *value); +StatusCode ConfigSetChar(void *object, const char *name, const char value); +StatusCode ConfigGetBool(void *object, const char *name, bool *value); +StatusCode ConfigSetBool(void *object, const char *name, const bool value); +StatusCode ConfigGetFloat(void *object, const char *name, float *value); +StatusCode ConfigSetFloat(void *object, const char *name, const float value); +StatusCode ConfigGetDouble(void *object, const char *name, double *value); +StatusCode ConfigSetDouble(void *object, const char *name, const double value); +StatusCode ConfigGetString(void *object, const char *name, const char **value); +StatusCode ConfigSetString(void *object, const char *name, const char *value); +//提供了一系列函数,用于打开、保存、关闭配置文件,以及获取和设置配置文件中的不同数据类型(如整型、短整型、长整型、长长整型、字符、布尔值、浮点数、双精度浮点数和字符串)的值。这些函数大多数都接受一个void *object作为第一个参数,这通常是一个指向配置文件上下文或对象的指针,用于访问和操作配置文件。其他参数包括配置项的名称(const char *name)和用于存储获取值的变量的指针(如int *value)。 +#ifdef __cplusplus +} +#endif +#endif +//总的来说,这个头文件定义了一个配置管理的接口,允许开发者以类型安全的方式读取和写入配置文件的值,同时考虑到了C和C++之间的互操作性。 \ No newline at end of file diff --git a/utils/ConfigBaseCode.h b/utils/ConfigBaseCode.h new file mode 100644 index 00000000..45ff9217 --- /dev/null +++ b/utils/ConfigBaseCode.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//版权声明和许可:文件开头包含了一段版权声明和许可信息,指出这段代码是由Fancy Code版权所有,并遵循Apache License, Version 2.0的条款。这意味着使用这段代码必须遵守Apache 2.0许可证的规定 +#ifndef CONFIG_BASE_CODE_H +#define CONFIG_BASE_CODE_H +//#ifndef CONFIG_BASE_CODE_H、#define CONFIG_BASE_CODE_H、#endif 这三段代码是标准的头文件保护符(也称为包含卫士),用于防止头文件被重复包含。 +#include "ConfigBase.h" +#include "StatusCode.h" +//这两行代码包含了其他两个头文件,这些文件可能定义了ConfigBase类和StatusCode枚举或类,这些在当前的头文件中会被使用。 +#ifdef __cplusplus +extern "C" { +#endif +//是为了在C++环境中使用时,确保C语言编写的代码能够被正确地链接和使用。extern "C"告诉C++编译器,这部分代码应该按照C语言的规则来编译和链接,以避免C++的名称修饰(Name Mangling)问题。 +#ifndef CONFIG_OWNER + #error This is internal file, never include it. + // 这两行代码是一个编译时检查。如果CONFIG_OWNER宏未定义,编译器会报错,并显示“This is internal file, never include it.”这条信息。这通常用于防止这个头文件被外部直接包含,可能是因为它包含了一些只应该在库内部使用的定义或声明。 +#endif +const StatusCode CreateConfigCode(const long int code);// 这行代码声明了一个函数CreateConfigCode,它接受一个long int类型的参数code,并返回一个StatusCode类型的值。这个函数可能用于根据给定的代码(可能是某种配置标识符)来创建一个配置对象或执行某种配置操作,并返回一个状态码来表示操作的成功与否。 +#ifdef __cplusplus +} +#endif +#endif +//总之,这个头文件定义了一个与配置代码相关的函数声明,并通过一系列的条件编译和头文件保护符来确保代码的正确性和安全性。 \ No newline at end of file diff --git a/utils/ConfigBaseImpl.h b/utils/ConfigBaseImpl.h new file mode 100644 index 00000000..79b03c34 --- /dev/null +++ b/utils/ConfigBaseImpl.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CONFIG_BASE_IMPL_H +#define CONFIG_BASE_IMPL_H +//标准的头文件保护,用于防止头文件被重复包含。 +#include "IConfigBase.h"//这是一个接口定义文件,ConfigBaseImpl类实现了这个接口中声明的所有纯虚函数。 +#include //这是libconfig库的头文件,提供了处理配置文件的API。 +#include //C++标准库中的字符串处理类。 +class ConfigBaseImpl : public IConfigBase//ConfigBaseImpl类继承自IConfigBase接口,这意味着它需要实现接口中声明的所有纯虚函数。 +{ +public: + ConfigBaseImpl(const std::string &fileName); + virtual ~ConfigBaseImpl() = default; + bool OpenConfigFile(void) override;//打开配置文件 + void CloseConfigFile(void) override;//关闭配置文件 + StatusCode ConfigSaveFile(void) override;//用于将配置更改保存回文件,但返回类型是StatusCode + StatusCode ConfigGetInt(const char *name, int *value) override; + StatusCode ConfigSetInt(const char *name, const int value) override; + StatusCode ConfigGetShort(const char *name, short *value) override; + StatusCode ConfigSetShort(const char *name, const short value) override; + StatusCode ConfigGetLong(const char *name, long *value) override; + StatusCode ConfigSetLong(const char *name, const long value) override; + StatusCode ConfigGetLLong(const char *name, long long *value) override; + StatusCode ConfigSetLLong(const char *name, const long long value) override; + StatusCode ConfigGetChar(const char *name, char *value) override; + StatusCode ConfigSetChar(const char *name, const char value) override; + StatusCode ConfigGetBool(const char *name, bool *value) override; + StatusCode ConfigSetBool(const char *name, const bool value) override; + StatusCode ConfigGetFloat(const char *name, float *value) override; + StatusCode ConfigSetFloat(const char *name, const float value) override; + StatusCode ConfigGetDouble(const char *name, double *value) override; + StatusCode ConfigSetDouble(const char *name, const double value) override; + StatusCode ConfigGetString(const char *name, const char **value) override; + StatusCode ConfigSetString(const char *name, const char *value) override; +//这些函数提供了从配置文件中获取和设置各种数据类型(如整数、浮点数、字符串等)的接口。它们接受一个配置项的名称和一个指向值的指针(对于Get函数)或值本身(对于Set函数),并返回一个StatusCode表示操作结果。 +private: + const std::string mFileName;// 存储配置文件的路径。 + config_t mCfg;//libconfig库中的config_t类型,用于表示配置文件的内部结构。 +}; +#endif \ No newline at end of file diff --git a/utils/IConfigBase.h b/utils/IConfigBase.h new file mode 100644 index 00000000..ab2caeb0 --- /dev/null +++ b/utils/IConfigBase.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef I_CONFIG_BASE_H +#define I_CONFIG_BASE_H +//头文件保护(防止头文件被重复包含)。 +#include "StatusCode.h" +#include +//StatusCode.h(可能是一个定义状态码枚举或类的文件,用于表示操作成功或失败的状态)和(C++标准库中的一部分,提供智能指针等内存管理功能)。 +class IConfigBase// IConfigBase 接口 +{ +public: + IConfigBase() = default; + virtual ~IConfigBase() = default; + virtual bool OpenConfigFile(void); + virtual void CloseConfigFile(void); + virtual StatusCode ConfigSaveFile(void); + virtual StatusCode ConfigGetInt(const char *name, int *value); + virtual StatusCode ConfigSetInt(const char *name, const int value); + virtual StatusCode ConfigGetShort(const char *name, short *value); + virtual StatusCode ConfigSetShort(const char *name, const short value); + virtual StatusCode ConfigGetLong(const char *name, long *value); + virtual StatusCode ConfigSetLong(const char *name, const long value); + virtual StatusCode ConfigGetLLong(const char *name, long long *value); + virtual StatusCode ConfigSetLLong(const char *name, const long long value); + virtual StatusCode ConfigGetChar(const char *name, char *value); + virtual StatusCode ConfigSetChar(const char *name, const char value); + virtual StatusCode ConfigGetBool(const char *name, bool *value); + virtual StatusCode ConfigSetBool(const char *name, const bool value); + virtual StatusCode ConfigGetFloat(const char *name, float *value); + virtual StatusCode ConfigSetFloat(const char *name, const float value); + virtual StatusCode ConfigGetDouble(const char *name, double *value); + virtual StatusCode ConfigSetDouble(const char *name, const double value); + virtual StatusCode ConfigGetString(const char *name, const char **value); + virtual StatusCode ConfigSetString(const char *name, const char *value); +}; +//IConfigBase是一个抽象基类,定义了与配置管理相关的接口。它包含了构造函数和析构函数的默认实现(即不执行任何操作),以及一系列虚函数,用于打开和关闭配置文件、保存配置以及获取和设置各种数据类型的配置值。这些虚函数的具体实现将由派生类提供。 +typedef struct i_config_base_header//定义了一个结构体i_config_base_header,包含一个指向字符的指针mCheckName,可能用于某种形式的验证或识别。 +{ + const char *mCheckName; +} IConfigBaseHeader; +typedef struct config_base//定义了一个结构体config_base,它包含一个IConfigBaseHeader类型的成员mHeader和一个指向IConfigBase的智能指针mIConfigBase。这个结构体可能用于在应用程序中存储和管理配置实例。 +{ + IConfigBaseHeader mHeader; + std::shared_ptr mIConfigBase; +} ConfigBase; +const char *GetConfigBaseModuleName(void); +std::shared_ptr *NewConfigBase(const char *fileName); +//声明了两个函数:GetConfigBaseModuleName(可能用于获取配置模块的名称)和NewConfigBase(接受一个文件名作为参数,并返回一个指向IConfigBase智能指针的指针,用于创建和返回一个新的配置实例)。 +#endif +//这段代码定义了一个用于配置管理的C++接口和相关数据结构,以及与之相关的函数声明。它使用了C++的抽象基类、虚函数、智能指针等特性来实现灵活的配置管理功能。 \ No newline at end of file From 6d4bbe46c11dc1780ca7ac432ab8895a7822fa49 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 08:44:09 +0000 Subject: [PATCH 23/48] Revert "configbasee " This reverts commit cacb6dcbf82c7c1847c54bb4cdd00a393ff9caad. --- utils/ConfigBase.h | 56 -------------------------------------- utils/ConfigBaseCode.h | 35 ------------------------ utils/ConfigBaseImpl.h | 52 ----------------------------------- utils/IConfigBase.h | 62 ------------------------------------------ 4 files changed, 205 deletions(-) delete mode 100644 utils/ConfigBase.h delete mode 100644 utils/ConfigBaseCode.h delete mode 100644 utils/ConfigBaseImpl.h delete mode 100644 utils/IConfigBase.h diff --git a/utils/ConfigBase.h b/utils/ConfigBase.h deleted file mode 100644 index 576ffd9d..00000000 --- a/utils/ConfigBase.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CONFIG_BASE_H -#define CONFIG_BASE_H -#include "StatusCode.h" -#ifdef __cplusplus -extern "C" //声明是为了让C++编译器按照C语言的方式来处理被extern "C"包围的代码。这是必要的 -{ -#endif -//#ifndef CONFIG_BASE_H、#define CONFIG_BASE_H 和 #endif 这三个预处理指令一起工作,确保头文件CONFIG_BASE_H在任何编译单元中只被包含(include)一次。这是通过定义一个唯一的宏(CONFIG_BASE_H)来实现的,如果宏已定义,则跳过文件内容。 -enum CONFIG_CODE//定义了一个枚举类型CONFIG_CODE,用于可能的配置错误代码。它开始于STATUS_CODE_END(这里假设STATUS_CODE_END是在StatusCode.h中定义的某个状态码,表示一组状态码的结束),并定义了一个CONFIG_CODE_END作为枚举的结束标志。 -{ - CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, - CONFIG_CODE_END -}; -// StatusCode ConfigInit(void); -// StatusCode ConfigUnInit(void); -void *OpenConfigFile(const char *fileName); -StatusCode ConfigSaveFile(void *object); -void CloseConfigFile(void *object); -StatusCode ConfigGetInt(void *object, const char *name, int *value); -StatusCode ConfigSetInt(void *object, const char *name, const int value); -StatusCode ConfigGetShort(void *object, const char *name, short *value); -StatusCode ConfigSetShort(void *object, const char *name, const short value); -StatusCode ConfigGetLong(void *object, const char *name, long *value); -StatusCode ConfigSetLong(void *object, const char *name, const long value); -StatusCode ConfigGetLLong(void *object, const char *name, long long *value); -StatusCode ConfigSetLLong(void *object, const char *name, const long long value); -StatusCode ConfigGetChar(void *object, const char *name, char *value); -StatusCode ConfigSetChar(void *object, const char *name, const char value); -StatusCode ConfigGetBool(void *object, const char *name, bool *value); -StatusCode ConfigSetBool(void *object, const char *name, const bool value); -StatusCode ConfigGetFloat(void *object, const char *name, float *value); -StatusCode ConfigSetFloat(void *object, const char *name, const float value); -StatusCode ConfigGetDouble(void *object, const char *name, double *value); -StatusCode ConfigSetDouble(void *object, const char *name, const double value); -StatusCode ConfigGetString(void *object, const char *name, const char **value); -StatusCode ConfigSetString(void *object, const char *name, const char *value); -//提供了一系列函数,用于打开、保存、关闭配置文件,以及获取和设置配置文件中的不同数据类型(如整型、短整型、长整型、长长整型、字符、布尔值、浮点数、双精度浮点数和字符串)的值。这些函数大多数都接受一个void *object作为第一个参数,这通常是一个指向配置文件上下文或对象的指针,用于访问和操作配置文件。其他参数包括配置项的名称(const char *name)和用于存储获取值的变量的指针(如int *value)。 -#ifdef __cplusplus -} -#endif -#endif -//总的来说,这个头文件定义了一个配置管理的接口,允许开发者以类型安全的方式读取和写入配置文件的值,同时考虑到了C和C++之间的互操作性。 \ No newline at end of file diff --git a/utils/ConfigBaseCode.h b/utils/ConfigBaseCode.h deleted file mode 100644 index 45ff9217..00000000 --- a/utils/ConfigBaseCode.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -//版权声明和许可:文件开头包含了一段版权声明和许可信息,指出这段代码是由Fancy Code版权所有,并遵循Apache License, Version 2.0的条款。这意味着使用这段代码必须遵守Apache 2.0许可证的规定 -#ifndef CONFIG_BASE_CODE_H -#define CONFIG_BASE_CODE_H -//#ifndef CONFIG_BASE_CODE_H、#define CONFIG_BASE_CODE_H、#endif 这三段代码是标准的头文件保护符(也称为包含卫士),用于防止头文件被重复包含。 -#include "ConfigBase.h" -#include "StatusCode.h" -//这两行代码包含了其他两个头文件,这些文件可能定义了ConfigBase类和StatusCode枚举或类,这些在当前的头文件中会被使用。 -#ifdef __cplusplus -extern "C" { -#endif -//是为了在C++环境中使用时,确保C语言编写的代码能够被正确地链接和使用。extern "C"告诉C++编译器,这部分代码应该按照C语言的规则来编译和链接,以避免C++的名称修饰(Name Mangling)问题。 -#ifndef CONFIG_OWNER - #error This is internal file, never include it. - // 这两行代码是一个编译时检查。如果CONFIG_OWNER宏未定义,编译器会报错,并显示“This is internal file, never include it.”这条信息。这通常用于防止这个头文件被外部直接包含,可能是因为它包含了一些只应该在库内部使用的定义或声明。 -#endif -const StatusCode CreateConfigCode(const long int code);// 这行代码声明了一个函数CreateConfigCode,它接受一个long int类型的参数code,并返回一个StatusCode类型的值。这个函数可能用于根据给定的代码(可能是某种配置标识符)来创建一个配置对象或执行某种配置操作,并返回一个状态码来表示操作的成功与否。 -#ifdef __cplusplus -} -#endif -#endif -//总之,这个头文件定义了一个与配置代码相关的函数声明,并通过一系列的条件编译和头文件保护符来确保代码的正确性和安全性。 \ No newline at end of file diff --git a/utils/ConfigBaseImpl.h b/utils/ConfigBaseImpl.h deleted file mode 100644 index 79b03c34..00000000 --- a/utils/ConfigBaseImpl.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CONFIG_BASE_IMPL_H -#define CONFIG_BASE_IMPL_H -//标准的头文件保护,用于防止头文件被重复包含。 -#include "IConfigBase.h"//这是一个接口定义文件,ConfigBaseImpl类实现了这个接口中声明的所有纯虚函数。 -#include //这是libconfig库的头文件,提供了处理配置文件的API。 -#include //C++标准库中的字符串处理类。 -class ConfigBaseImpl : public IConfigBase//ConfigBaseImpl类继承自IConfigBase接口,这意味着它需要实现接口中声明的所有纯虚函数。 -{ -public: - ConfigBaseImpl(const std::string &fileName); - virtual ~ConfigBaseImpl() = default; - bool OpenConfigFile(void) override;//打开配置文件 - void CloseConfigFile(void) override;//关闭配置文件 - StatusCode ConfigSaveFile(void) override;//用于将配置更改保存回文件,但返回类型是StatusCode - StatusCode ConfigGetInt(const char *name, int *value) override; - StatusCode ConfigSetInt(const char *name, const int value) override; - StatusCode ConfigGetShort(const char *name, short *value) override; - StatusCode ConfigSetShort(const char *name, const short value) override; - StatusCode ConfigGetLong(const char *name, long *value) override; - StatusCode ConfigSetLong(const char *name, const long value) override; - StatusCode ConfigGetLLong(const char *name, long long *value) override; - StatusCode ConfigSetLLong(const char *name, const long long value) override; - StatusCode ConfigGetChar(const char *name, char *value) override; - StatusCode ConfigSetChar(const char *name, const char value) override; - StatusCode ConfigGetBool(const char *name, bool *value) override; - StatusCode ConfigSetBool(const char *name, const bool value) override; - StatusCode ConfigGetFloat(const char *name, float *value) override; - StatusCode ConfigSetFloat(const char *name, const float value) override; - StatusCode ConfigGetDouble(const char *name, double *value) override; - StatusCode ConfigSetDouble(const char *name, const double value) override; - StatusCode ConfigGetString(const char *name, const char **value) override; - StatusCode ConfigSetString(const char *name, const char *value) override; -//这些函数提供了从配置文件中获取和设置各种数据类型(如整数、浮点数、字符串等)的接口。它们接受一个配置项的名称和一个指向值的指针(对于Get函数)或值本身(对于Set函数),并返回一个StatusCode表示操作结果。 -private: - const std::string mFileName;// 存储配置文件的路径。 - config_t mCfg;//libconfig库中的config_t类型,用于表示配置文件的内部结构。 -}; -#endif \ No newline at end of file diff --git a/utils/IConfigBase.h b/utils/IConfigBase.h deleted file mode 100644 index ab2caeb0..00000000 --- a/utils/IConfigBase.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef I_CONFIG_BASE_H -#define I_CONFIG_BASE_H -//头文件保护(防止头文件被重复包含)。 -#include "StatusCode.h" -#include -//StatusCode.h(可能是一个定义状态码枚举或类的文件,用于表示操作成功或失败的状态)和(C++标准库中的一部分,提供智能指针等内存管理功能)。 -class IConfigBase// IConfigBase 接口 -{ -public: - IConfigBase() = default; - virtual ~IConfigBase() = default; - virtual bool OpenConfigFile(void); - virtual void CloseConfigFile(void); - virtual StatusCode ConfigSaveFile(void); - virtual StatusCode ConfigGetInt(const char *name, int *value); - virtual StatusCode ConfigSetInt(const char *name, const int value); - virtual StatusCode ConfigGetShort(const char *name, short *value); - virtual StatusCode ConfigSetShort(const char *name, const short value); - virtual StatusCode ConfigGetLong(const char *name, long *value); - virtual StatusCode ConfigSetLong(const char *name, const long value); - virtual StatusCode ConfigGetLLong(const char *name, long long *value); - virtual StatusCode ConfigSetLLong(const char *name, const long long value); - virtual StatusCode ConfigGetChar(const char *name, char *value); - virtual StatusCode ConfigSetChar(const char *name, const char value); - virtual StatusCode ConfigGetBool(const char *name, bool *value); - virtual StatusCode ConfigSetBool(const char *name, const bool value); - virtual StatusCode ConfigGetFloat(const char *name, float *value); - virtual StatusCode ConfigSetFloat(const char *name, const float value); - virtual StatusCode ConfigGetDouble(const char *name, double *value); - virtual StatusCode ConfigSetDouble(const char *name, const double value); - virtual StatusCode ConfigGetString(const char *name, const char **value); - virtual StatusCode ConfigSetString(const char *name, const char *value); -}; -//IConfigBase是一个抽象基类,定义了与配置管理相关的接口。它包含了构造函数和析构函数的默认实现(即不执行任何操作),以及一系列虚函数,用于打开和关闭配置文件、保存配置以及获取和设置各种数据类型的配置值。这些虚函数的具体实现将由派生类提供。 -typedef struct i_config_base_header//定义了一个结构体i_config_base_header,包含一个指向字符的指针mCheckName,可能用于某种形式的验证或识别。 -{ - const char *mCheckName; -} IConfigBaseHeader; -typedef struct config_base//定义了一个结构体config_base,它包含一个IConfigBaseHeader类型的成员mHeader和一个指向IConfigBase的智能指针mIConfigBase。这个结构体可能用于在应用程序中存储和管理配置实例。 -{ - IConfigBaseHeader mHeader; - std::shared_ptr mIConfigBase; -} ConfigBase; -const char *GetConfigBaseModuleName(void); -std::shared_ptr *NewConfigBase(const char *fileName); -//声明了两个函数:GetConfigBaseModuleName(可能用于获取配置模块的名称)和NewConfigBase(接受一个文件名作为参数,并返回一个指向IConfigBase智能指针的指针,用于创建和返回一个新的配置实例)。 -#endif -//这段代码定义了一个用于配置管理的C++接口和相关数据结构,以及与之相关的函数声明。它使用了C++的抽象基类、虚函数、智能指针等特性来实现灵活的配置管理功能。 \ No newline at end of file From d432adea526fbb387a23acd5dcd58a6514380a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:58:52 +0000 Subject: [PATCH 24/48] configbasee MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/ConfigBase.h | 56 ++++++++++++++++++++++++++++ utils/ConfigBase/ConfigBaseCode.h | 35 +++++++++++++++++ utils/ConfigBase/ConfigBaseImpl.h | 53 ++++++++++++++++++++++++++ utils/ConfigBase/IConfigBase.h | 62 +++++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 utils/ConfigBase/ConfigBase.h create mode 100644 utils/ConfigBase/ConfigBaseCode.h create mode 100644 utils/ConfigBase/ConfigBaseImpl.h create mode 100644 utils/ConfigBase/IConfigBase.h diff --git a/utils/ConfigBase/ConfigBase.h b/utils/ConfigBase/ConfigBase.h new file mode 100644 index 00000000..e001ed41 --- /dev/null +++ b/utils/ConfigBase/ConfigBase.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CONFIG_BASE_H +#define CONFIG_BASE_H +#include "StatusCode.h" +#ifdef __cplusplus +extern "C" //The declaration is to allow the C++compiler to process code surrounded by extern "C" in the same way as the C language. This is necessary +{ +#endif +//#The three preprocessing instructions ifndef VNet BASE.H, # define VNet BASE.H, and # endif work together to ensure that the header file VNet BASE.H is only included once in any compilation unit. This is achieved by defining a unique macro (VNet BASE_H), skipping the file content if the macro has already been defined. +enum CONFIG_CODE//Defined an enumeration type VNet CODE for possible configuration error codes. It starts with VNet CODE-IND (assuming VNet CODE-IND is a status code defined in Status Code. h, indicating the end of a set of status codes), and defines a VNet CODE-IND as the end flag for enumeration. +{ + CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, + CONFIG_CODE_END +}; +// StatusCode ConfigInit(void); +// StatusCode ConfigUnInit(void); +void *OpenConfigFile(const char *fileName); +StatusCode ConfigSaveFile(void *object); +void CloseConfigFile(void *object); +StatusCode ConfigGetInt(void *object, const char *name, int *value); +StatusCode ConfigSetInt(void *object, const char *name, const int value); +StatusCode ConfigGetShort(void *object, const char *name, short *value); +StatusCode ConfigSetShort(void *object, const char *name, const short value); +StatusCode ConfigGetLong(void *object, const char *name, long *value); +StatusCode ConfigSetLong(void *object, const char *name, const long value); +StatusCode ConfigGetLLong(void *object, const char *name, long long *value); +StatusCode ConfigSetLLong(void *object, const char *name, const long long value); +StatusCode ConfigGetChar(void *object, const char *name, char *value); +StatusCode ConfigSetChar(void *object, const char *name, const char value); +StatusCode ConfigGetBool(void *object, const char *name, bool *value); +StatusCode ConfigSetBool(void *object, const char *name, const bool value); +StatusCode ConfigGetFloat(void *object, const char *name, float *value); +StatusCode ConfigSetFloat(void *object, const char *name, const float value); +StatusCode ConfigGetDouble(void *object, const char *name, double *value); +StatusCode ConfigSetDouble(void *object, const char *name, const double value); +StatusCode ConfigGetString(void *object, const char *name, const char **value); +StatusCode ConfigSetString(void *object, const char *name, const char *value); +//Provides a series of functions for opening, saving, closing configuration files, as well as obtaining and setting values for different data types (such as integer, short integer, long integer, long integer, character, boolean, floating-point, double precision floating-point, and string) in the configuration file. Most of these functions accept a void * object as the first parameter, which is typically a pointer to the configuration file context or object used to access and manipulate the configuration file. Other parameters include the name of the configuration item (const char * name) and a pointer to the variable used to store the retrieved value (such as int * value). +#ifdef __cplusplus +} +#endif +#endif +//Overall, this header file defines a configuration management interface that allows developers to read and write configuration file values in a type safe manner, while considering interoperability between C and C++. \ No newline at end of file diff --git a/utils/ConfigBase/ConfigBaseCode.h b/utils/ConfigBase/ConfigBaseCode.h new file mode 100644 index 00000000..030aac9a --- /dev/null +++ b/utils/ConfigBase/ConfigBaseCode.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//Copyright Statement and License: The beginning of the file contains a copyright statement and license information, indicating that this code is copyrighted by Fancy Code and follows the terms of Apache License, Version 2.0. This means that using this code must comply with the Apache 2.0 license regulations +#ifndef CONFIG_BASE_CODE_H +#define CONFIG_BASE_CODE_H +//#The three pieces of code, ifndef CONFIG_SBASE-CEDE.H, # define CONFIG_SBASE-CEDE.H, and # endif, are standard header file protectors (also known as inclusion guards) used to prevent header files from being duplicated. +#include "ConfigBase.h" +#include "StatusCode.h" +//These two lines of code contain two other header files that may define the Config Base class and Status Code enumeration or class, which will be used in the current header file. +#ifdef __cplusplus +extern "C" { +#endif +//It is to ensure that the code written in C language can be linked and used correctly when used in the C++environment. Extern "C" tells the C++compiler that this part of the code should be compiled and linked according to the rules of the C language to avoid the problem of name mangling in C++. +#ifndef CONFIG_OWNER + #error This is internal file, never include it. + // These two lines of code are a compile time check. If the VNet OWNER macro is not defined, the compiler will report an error and display the message 'This is internal file, never include it.'. This is usually used to prevent this header file from being directly included externally, possibly because it contains some definitions or declarations that should only be used internally within the library. +#endif +const StatusCode CreateConfigCode(const long int code);// This line of code declares a function called CreateConfig Code, which takes a parameter code of type long int and returns a value of type Status Code. This function may be used to create a configuration object or perform a configuration operation based on a given code (possibly some configuration identifier), and return a status code to indicate the success or failure of the operation. +#ifdef __cplusplus +} +#endif +#endif +//In summary, this header file defines a function declaration related to the configuration code, and ensures the correctness and security of the code through a series of conditional compilations and header file protectors. \ No newline at end of file diff --git a/utils/ConfigBase/ConfigBaseImpl.h b/utils/ConfigBase/ConfigBaseImpl.h new file mode 100644 index 00000000..e5ac3ae0 --- /dev/null +++ b/utils/ConfigBase/ConfigBaseImpl.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CONFIG_BASE_IMPL_H +#define CONFIG_BASE_IMPL_H +//Standard header file protection is used to prevent duplicate header files from being included. +#include "IConfigBase.h"//This is an interface definition file, and the Config Base Impl class implements all the pure virtual functions declared in this interface. +#include //This is the header file of the libconfig library, which provides an API for handling configuration files. +#include //String processing class in the C++standard library. +class ConfigBaseImpl : public IConfigBase//The Config Base Impl class inherits from the FHIR figBase interface, which means it needs to implement all the pure virtual functions declared in the interface. +{ +public: + ConfigBaseImpl(const std::string &fileName); + virtual ~ConfigBaseImpl() = default; + bool OpenConfigFile(void) override;//Open configuration file + void CloseConfigFile(void) override;//Close configuration file + StatusCode ConfigSaveFile(void) override;//Used to save configuration changes back to a file, but the return type is Status Code + StatusCode ConfigGetInt(const char *name, int *value) override; + StatusCode ConfigSetInt(const char *name, const int value) override; + StatusCode ConfigGetShort(const char *name, short *value) override; + StatusCode ConfigSetShort(const char *name, const short value) override; + StatusCode ConfigGetLong(const char *name, long *value) override; + StatusCode ConfigSetLong(const char *name, const long value) override; + StatusCode ConfigGetLLong(const char *name, long long *value) override; + StatusCode ConfigSetLLong(const char *name, const long long value) override; + StatusCode ConfigGetChar(const char *name, char *value) override; + StatusCode ConfigSetChar(const char *name, const char value) override; + StatusCode ConfigGetBool(const char *name, bool *value) override; + StatusCode ConfigSetBool(const char *name, const bool value) override; + StatusCode ConfigGetFloat(const char *name, float *value) override; + StatusCode ConfigSetFloat(const char *name, const float value) override; + StatusCode ConfigGetDouble(const char *name, double *value) override; + StatusCode ConfigSetDouble(const char *name, const double value) override; + StatusCode ConfigGetString(const char *name, const char **value) override; + StatusCode ConfigSetString(const char *name, const char *value) override; +//These functions provide interfaces for obtaining and setting various data types (such as integers, floating-point numbers, strings, etc.) from configuration files. They accept the name of a configuration item and a pointer to the value (for Get functions) or the value itself (for Set functions), and return a Status Code representing the operation result. +private: + const std::string mFileName;// The path to store configuration files. + config_t mCfg;//The confic_t type in the libconfig library is used to represent the internal structure of configuration files. +}; +#endif +//In summary, this code defines a class for handling configuration files that provides rich interfaces to read and modify configuration items in the configuration file, achieved through the use of the libconfig library. \ No newline at end of file diff --git a/utils/ConfigBase/IConfigBase.h b/utils/ConfigBase/IConfigBase.h new file mode 100644 index 00000000..a69b0596 --- /dev/null +++ b/utils/ConfigBase/IConfigBase.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef I_CONFIG_BASE_H +#define I_CONFIG_BASE_H +//Header file protection (preventing header files from being duplicated). +#include "StatusCode.h" +#include +//Status Code. h (which may be a file that defines a status code enumeration or class to indicate the status of successful or failed operations) and(a part of the C++standard library that provides memory management features such as smart pointers). +class IConfigBase// FHIR gBase interface +{ +public: + IConfigBase() = default; + virtual ~IConfigBase() = default; + virtual bool OpenConfigFile(void); + virtual void CloseConfigFile(void); + virtual StatusCode ConfigSaveFile(void); + virtual StatusCode ConfigGetInt(const char *name, int *value); + virtual StatusCode ConfigSetInt(const char *name, const int value); + virtual StatusCode ConfigGetShort(const char *name, short *value); + virtual StatusCode ConfigSetShort(const char *name, const short value); + virtual StatusCode ConfigGetLong(const char *name, long *value); + virtual StatusCode ConfigSetLong(const char *name, const long value); + virtual StatusCode ConfigGetLLong(const char *name, long long *value); + virtual StatusCode ConfigSetLLong(const char *name, const long long value); + virtual StatusCode ConfigGetChar(const char *name, char *value); + virtual StatusCode ConfigSetChar(const char *name, const char value); + virtual StatusCode ConfigGetBool(const char *name, bool *value); + virtual StatusCode ConfigSetBool(const char *name, const bool value); + virtual StatusCode ConfigGetFloat(const char *name, float *value); + virtual StatusCode ConfigSetFloat(const char *name, const float value); + virtual StatusCode ConfigGetDouble(const char *name, double *value); + virtual StatusCode ConfigSetDouble(const char *name, const double value); + virtual StatusCode ConfigGetString(const char *name, const char **value); + virtual StatusCode ConfigSetString(const char *name, const char *value); +}; +//IKON Base is an abstract base class that defines interfaces related to configuration management. It includes default implementations of constructors and destructors (i.e. do not perform any operations), as well as a series of virtual functions for opening and closing configuration files, saving configurations, and obtaining and setting configuration values for various data types. The specific implementation of these virtual functions will be provided by derived classes. +typedef struct i_config_base_header//Defined a struct i_config.base_header containing a pointer mCheckName to a character, which may be used for some form of validation or recognition. +{ + const char *mCheckName; +} IConfigBaseHeader; +typedef struct config_base//Defined a struct config.base that contains a member mHeader of type iKON figBaseHeader and a smart pointer mIConfig Base pointing to iKON figBase. This structure may be used to store and manage configuration instances in applications. +{ + IConfigBaseHeader mHeader; + std::shared_ptr mIConfigBase; +} ConfigBase; +const char *GetConfigBaseModuleName(void); +std::shared_ptr *NewConfigBase(const char *fileName); +//Declared two functions: FHIR nfigBaseModulus Name (which may be used to obtain the name of a configuration module) and NewConfig Base (which takes a file name as a parameter and returns a pointer to the FHIR figBase smart pointer for creating and returning a new configuration instance). +#endif +//This code defines a C++interface and related data structures for configuration management, as well as function declarations related to it. It utilizes the abstract base classes, virtual functions, smart pointers, and other features of C++to achieve flexible configuration management functionality. \ No newline at end of file From ea213cada64afafa73a44db61572bc7c6d0e90f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 08:59:16 +0000 Subject: [PATCH 25/48] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/ConfigBase.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/ConfigBase.h | 56 ----------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 utils/ConfigBase/ConfigBase.h diff --git a/utils/ConfigBase/ConfigBase.h b/utils/ConfigBase/ConfigBase.h deleted file mode 100644 index e001ed41..00000000 --- a/utils/ConfigBase/ConfigBase.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CONFIG_BASE_H -#define CONFIG_BASE_H -#include "StatusCode.h" -#ifdef __cplusplus -extern "C" //The declaration is to allow the C++compiler to process code surrounded by extern "C" in the same way as the C language. This is necessary -{ -#endif -//#The three preprocessing instructions ifndef VNet BASE.H, # define VNet BASE.H, and # endif work together to ensure that the header file VNet BASE.H is only included once in any compilation unit. This is achieved by defining a unique macro (VNet BASE_H), skipping the file content if the macro has already been defined. -enum CONFIG_CODE//Defined an enumeration type VNet CODE for possible configuration error codes. It starts with VNet CODE-IND (assuming VNet CODE-IND is a status code defined in Status Code. h, indicating the end of a set of status codes), and defines a VNet CODE-IND as the end flag for enumeration. -{ - CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, - CONFIG_CODE_END -}; -// StatusCode ConfigInit(void); -// StatusCode ConfigUnInit(void); -void *OpenConfigFile(const char *fileName); -StatusCode ConfigSaveFile(void *object); -void CloseConfigFile(void *object); -StatusCode ConfigGetInt(void *object, const char *name, int *value); -StatusCode ConfigSetInt(void *object, const char *name, const int value); -StatusCode ConfigGetShort(void *object, const char *name, short *value); -StatusCode ConfigSetShort(void *object, const char *name, const short value); -StatusCode ConfigGetLong(void *object, const char *name, long *value); -StatusCode ConfigSetLong(void *object, const char *name, const long value); -StatusCode ConfigGetLLong(void *object, const char *name, long long *value); -StatusCode ConfigSetLLong(void *object, const char *name, const long long value); -StatusCode ConfigGetChar(void *object, const char *name, char *value); -StatusCode ConfigSetChar(void *object, const char *name, const char value); -StatusCode ConfigGetBool(void *object, const char *name, bool *value); -StatusCode ConfigSetBool(void *object, const char *name, const bool value); -StatusCode ConfigGetFloat(void *object, const char *name, float *value); -StatusCode ConfigSetFloat(void *object, const char *name, const float value); -StatusCode ConfigGetDouble(void *object, const char *name, double *value); -StatusCode ConfigSetDouble(void *object, const char *name, const double value); -StatusCode ConfigGetString(void *object, const char *name, const char **value); -StatusCode ConfigSetString(void *object, const char *name, const char *value); -//Provides a series of functions for opening, saving, closing configuration files, as well as obtaining and setting values for different data types (such as integer, short integer, long integer, long integer, character, boolean, floating-point, double precision floating-point, and string) in the configuration file. Most of these functions accept a void * object as the first parameter, which is typically a pointer to the configuration file context or object used to access and manipulate the configuration file. Other parameters include the name of the configuration item (const char * name) and a pointer to the variable used to store the retrieved value (such as int * value). -#ifdef __cplusplus -} -#endif -#endif -//Overall, this header file defines a configuration management interface that allows developers to read and write configuration file values in a type safe manner, while considering interoperability between C and C++. \ No newline at end of file From f0ff1c2a75759f8d5080730505d7edcae06fbc9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:07:01 +0000 Subject: [PATCH 26/48] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/ConfigBaseCode.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/ConfigBaseCode.h | 35 ------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 utils/ConfigBase/ConfigBaseCode.h diff --git a/utils/ConfigBase/ConfigBaseCode.h b/utils/ConfigBase/ConfigBaseCode.h deleted file mode 100644 index 030aac9a..00000000 --- a/utils/ConfigBase/ConfigBaseCode.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -//Copyright Statement and License: The beginning of the file contains a copyright statement and license information, indicating that this code is copyrighted by Fancy Code and follows the terms of Apache License, Version 2.0. This means that using this code must comply with the Apache 2.0 license regulations -#ifndef CONFIG_BASE_CODE_H -#define CONFIG_BASE_CODE_H -//#The three pieces of code, ifndef CONFIG_SBASE-CEDE.H, # define CONFIG_SBASE-CEDE.H, and # endif, are standard header file protectors (also known as inclusion guards) used to prevent header files from being duplicated. -#include "ConfigBase.h" -#include "StatusCode.h" -//These two lines of code contain two other header files that may define the Config Base class and Status Code enumeration or class, which will be used in the current header file. -#ifdef __cplusplus -extern "C" { -#endif -//It is to ensure that the code written in C language can be linked and used correctly when used in the C++environment. Extern "C" tells the C++compiler that this part of the code should be compiled and linked according to the rules of the C language to avoid the problem of name mangling in C++. -#ifndef CONFIG_OWNER - #error This is internal file, never include it. - // These two lines of code are a compile time check. If the VNet OWNER macro is not defined, the compiler will report an error and display the message 'This is internal file, never include it.'. This is usually used to prevent this header file from being directly included externally, possibly because it contains some definitions or declarations that should only be used internally within the library. -#endif -const StatusCode CreateConfigCode(const long int code);// This line of code declares a function called CreateConfig Code, which takes a parameter code of type long int and returns a value of type Status Code. This function may be used to create a configuration object or perform a configuration operation based on a given code (possibly some configuration identifier), and return a status code to indicate the success or failure of the operation. -#ifdef __cplusplus -} -#endif -#endif -//In summary, this header file defines a function declaration related to the configuration code, and ensures the correctness and security of the code through a series of conditional compilations and header file protectors. \ No newline at end of file From bef62c33927e204546e35aced34c0d643b05e6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:07:07 +0000 Subject: [PATCH 27/48] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/ConfigBaseImpl.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/ConfigBaseImpl.h | 53 ------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 utils/ConfigBase/ConfigBaseImpl.h diff --git a/utils/ConfigBase/ConfigBaseImpl.h b/utils/ConfigBase/ConfigBaseImpl.h deleted file mode 100644 index e5ac3ae0..00000000 --- a/utils/ConfigBase/ConfigBaseImpl.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CONFIG_BASE_IMPL_H -#define CONFIG_BASE_IMPL_H -//Standard header file protection is used to prevent duplicate header files from being included. -#include "IConfigBase.h"//This is an interface definition file, and the Config Base Impl class implements all the pure virtual functions declared in this interface. -#include //This is the header file of the libconfig library, which provides an API for handling configuration files. -#include //String processing class in the C++standard library. -class ConfigBaseImpl : public IConfigBase//The Config Base Impl class inherits from the FHIR figBase interface, which means it needs to implement all the pure virtual functions declared in the interface. -{ -public: - ConfigBaseImpl(const std::string &fileName); - virtual ~ConfigBaseImpl() = default; - bool OpenConfigFile(void) override;//Open configuration file - void CloseConfigFile(void) override;//Close configuration file - StatusCode ConfigSaveFile(void) override;//Used to save configuration changes back to a file, but the return type is Status Code - StatusCode ConfigGetInt(const char *name, int *value) override; - StatusCode ConfigSetInt(const char *name, const int value) override; - StatusCode ConfigGetShort(const char *name, short *value) override; - StatusCode ConfigSetShort(const char *name, const short value) override; - StatusCode ConfigGetLong(const char *name, long *value) override; - StatusCode ConfigSetLong(const char *name, const long value) override; - StatusCode ConfigGetLLong(const char *name, long long *value) override; - StatusCode ConfigSetLLong(const char *name, const long long value) override; - StatusCode ConfigGetChar(const char *name, char *value) override; - StatusCode ConfigSetChar(const char *name, const char value) override; - StatusCode ConfigGetBool(const char *name, bool *value) override; - StatusCode ConfigSetBool(const char *name, const bool value) override; - StatusCode ConfigGetFloat(const char *name, float *value) override; - StatusCode ConfigSetFloat(const char *name, const float value) override; - StatusCode ConfigGetDouble(const char *name, double *value) override; - StatusCode ConfigSetDouble(const char *name, const double value) override; - StatusCode ConfigGetString(const char *name, const char **value) override; - StatusCode ConfigSetString(const char *name, const char *value) override; -//These functions provide interfaces for obtaining and setting various data types (such as integers, floating-point numbers, strings, etc.) from configuration files. They accept the name of a configuration item and a pointer to the value (for Get functions) or the value itself (for Set functions), and return a Status Code representing the operation result. -private: - const std::string mFileName;// The path to store configuration files. - config_t mCfg;//The confic_t type in the libconfig library is used to represent the internal structure of configuration files. -}; -#endif -//In summary, this code defines a class for handling configuration files that provides rich interfaces to read and modify configuration items in the configuration file, achieved through the use of the libconfig library. \ No newline at end of file From 7a9c8357ada7998e7a89c7e67921018415a614a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:07:21 +0000 Subject: [PATCH 28/48] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/IConfigBase.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/IConfigBase.h | 62 ---------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 utils/ConfigBase/IConfigBase.h diff --git a/utils/ConfigBase/IConfigBase.h b/utils/ConfigBase/IConfigBase.h deleted file mode 100644 index a69b0596..00000000 --- a/utils/ConfigBase/IConfigBase.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef I_CONFIG_BASE_H -#define I_CONFIG_BASE_H -//Header file protection (preventing header files from being duplicated). -#include "StatusCode.h" -#include -//Status Code. h (which may be a file that defines a status code enumeration or class to indicate the status of successful or failed operations) and(a part of the C++standard library that provides memory management features such as smart pointers). -class IConfigBase// FHIR gBase interface -{ -public: - IConfigBase() = default; - virtual ~IConfigBase() = default; - virtual bool OpenConfigFile(void); - virtual void CloseConfigFile(void); - virtual StatusCode ConfigSaveFile(void); - virtual StatusCode ConfigGetInt(const char *name, int *value); - virtual StatusCode ConfigSetInt(const char *name, const int value); - virtual StatusCode ConfigGetShort(const char *name, short *value); - virtual StatusCode ConfigSetShort(const char *name, const short value); - virtual StatusCode ConfigGetLong(const char *name, long *value); - virtual StatusCode ConfigSetLong(const char *name, const long value); - virtual StatusCode ConfigGetLLong(const char *name, long long *value); - virtual StatusCode ConfigSetLLong(const char *name, const long long value); - virtual StatusCode ConfigGetChar(const char *name, char *value); - virtual StatusCode ConfigSetChar(const char *name, const char value); - virtual StatusCode ConfigGetBool(const char *name, bool *value); - virtual StatusCode ConfigSetBool(const char *name, const bool value); - virtual StatusCode ConfigGetFloat(const char *name, float *value); - virtual StatusCode ConfigSetFloat(const char *name, const float value); - virtual StatusCode ConfigGetDouble(const char *name, double *value); - virtual StatusCode ConfigSetDouble(const char *name, const double value); - virtual StatusCode ConfigGetString(const char *name, const char **value); - virtual StatusCode ConfigSetString(const char *name, const char *value); -}; -//IKON Base is an abstract base class that defines interfaces related to configuration management. It includes default implementations of constructors and destructors (i.e. do not perform any operations), as well as a series of virtual functions for opening and closing configuration files, saving configurations, and obtaining and setting configuration values for various data types. The specific implementation of these virtual functions will be provided by derived classes. -typedef struct i_config_base_header//Defined a struct i_config.base_header containing a pointer mCheckName to a character, which may be used for some form of validation or recognition. -{ - const char *mCheckName; -} IConfigBaseHeader; -typedef struct config_base//Defined a struct config.base that contains a member mHeader of type iKON figBaseHeader and a smart pointer mIConfig Base pointing to iKON figBase. This structure may be used to store and manage configuration instances in applications. -{ - IConfigBaseHeader mHeader; - std::shared_ptr mIConfigBase; -} ConfigBase; -const char *GetConfigBaseModuleName(void); -std::shared_ptr *NewConfigBase(const char *fileName); -//Declared two functions: FHIR nfigBaseModulus Name (which may be used to obtain the name of a configuration module) and NewConfig Base (which takes a file name as a parameter and returns a pointer to the FHIR figBase smart pointer for creating and returning a new configuration instance). -#endif -//This code defines a C++interface and related data structures for configuration management, as well as function declarations related to it. It utilizes the abstract base classes, virtual functions, smart pointers, and other features of C++to achieve flexible configuration management functionality. \ No newline at end of file From e56cc6bf1af81a3264f6cd94ab24ef59e2371ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:08:38 +0000 Subject: [PATCH 29/48] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/include/ConfigBase.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/include/ConfigBase.h | 52 --------------------------- 1 file changed, 52 deletions(-) delete mode 100644 utils/ConfigBase/include/ConfigBase.h diff --git a/utils/ConfigBase/include/ConfigBase.h b/utils/ConfigBase/include/ConfigBase.h deleted file mode 100644 index 0f2a330a..00000000 --- a/utils/ConfigBase/include/ConfigBase.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CONFIG_BASE_H -#define CONFIG_BASE_H -#include "StatusCode.h" -#ifdef __cplusplus -extern "C" { -#endif -enum CONFIG_CODE -{ - CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, - CONFIG_CODE_END -}; -// StatusCode ConfigInit(void); -// StatusCode ConfigUnInit(void); -void *OpenConfigFile(const char *fileName); -StatusCode ConfigSaveFile(void *object); -void CloseConfigFile(void *object); -StatusCode ConfigGetInt(void *object, const char *name, int *value); -StatusCode ConfigSetInt(void *object, const char *name, const int value); -StatusCode ConfigGetShort(void *object, const char *name, short *value); -StatusCode ConfigSetShort(void *object, const char *name, const short value); -StatusCode ConfigGetLong(void *object, const char *name, long *value); -StatusCode ConfigSetLong(void *object, const char *name, const long value); -StatusCode ConfigGetLLong(void *object, const char *name, long long *value); -StatusCode ConfigSetLLong(void *object, const char *name, const long long value); -StatusCode ConfigGetChar(void *object, const char *name, char *value); -StatusCode ConfigSetChar(void *object, const char *name, const char value); -StatusCode ConfigGetBool(void *object, const char *name, bool *value); -StatusCode ConfigSetBool(void *object, const char *name, const bool value); -StatusCode ConfigGetFloat(void *object, const char *name, float *value); -StatusCode ConfigSetFloat(void *object, const char *name, const float value); -StatusCode ConfigGetDouble(void *object, const char *name, double *value); -StatusCode ConfigSetDouble(void *object, const char *name, const double value); -StatusCode ConfigGetString(void *object, const char *name, const char **value); -StatusCode ConfigSetString(void *object, const char *name, const char *value); -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file From 706f23472870aaabc1c37c970f988673d78f0976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:11:35 +0000 Subject: [PATCH 30/48] 12 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/include/ConfigBase.h | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 utils/ConfigBase/include/ConfigBase.h diff --git a/utils/ConfigBase/include/ConfigBase.h b/utils/ConfigBase/include/ConfigBase.h new file mode 100644 index 00000000..e001ed41 --- /dev/null +++ b/utils/ConfigBase/include/ConfigBase.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CONFIG_BASE_H +#define CONFIG_BASE_H +#include "StatusCode.h" +#ifdef __cplusplus +extern "C" //The declaration is to allow the C++compiler to process code surrounded by extern "C" in the same way as the C language. This is necessary +{ +#endif +//#The three preprocessing instructions ifndef VNet BASE.H, # define VNet BASE.H, and # endif work together to ensure that the header file VNet BASE.H is only included once in any compilation unit. This is achieved by defining a unique macro (VNet BASE_H), skipping the file content if the macro has already been defined. +enum CONFIG_CODE//Defined an enumeration type VNet CODE for possible configuration error codes. It starts with VNet CODE-IND (assuming VNet CODE-IND is a status code defined in Status Code. h, indicating the end of a set of status codes), and defines a VNet CODE-IND as the end flag for enumeration. +{ + CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, + CONFIG_CODE_END +}; +// StatusCode ConfigInit(void); +// StatusCode ConfigUnInit(void); +void *OpenConfigFile(const char *fileName); +StatusCode ConfigSaveFile(void *object); +void CloseConfigFile(void *object); +StatusCode ConfigGetInt(void *object, const char *name, int *value); +StatusCode ConfigSetInt(void *object, const char *name, const int value); +StatusCode ConfigGetShort(void *object, const char *name, short *value); +StatusCode ConfigSetShort(void *object, const char *name, const short value); +StatusCode ConfigGetLong(void *object, const char *name, long *value); +StatusCode ConfigSetLong(void *object, const char *name, const long value); +StatusCode ConfigGetLLong(void *object, const char *name, long long *value); +StatusCode ConfigSetLLong(void *object, const char *name, const long long value); +StatusCode ConfigGetChar(void *object, const char *name, char *value); +StatusCode ConfigSetChar(void *object, const char *name, const char value); +StatusCode ConfigGetBool(void *object, const char *name, bool *value); +StatusCode ConfigSetBool(void *object, const char *name, const bool value); +StatusCode ConfigGetFloat(void *object, const char *name, float *value); +StatusCode ConfigSetFloat(void *object, const char *name, const float value); +StatusCode ConfigGetDouble(void *object, const char *name, double *value); +StatusCode ConfigSetDouble(void *object, const char *name, const double value); +StatusCode ConfigGetString(void *object, const char *name, const char **value); +StatusCode ConfigSetString(void *object, const char *name, const char *value); +//Provides a series of functions for opening, saving, closing configuration files, as well as obtaining and setting values for different data types (such as integer, short integer, long integer, long integer, character, boolean, floating-point, double precision floating-point, and string) in the configuration file. Most of these functions accept a void * object as the first parameter, which is typically a pointer to the configuration file context or object used to access and manipulate the configuration file. Other parameters include the name of the configuration item (const char * name) and a pointer to the variable used to store the retrieved value (such as int * value). +#ifdef __cplusplus +} +#endif +#endif +//Overall, this header file defines a configuration management interface that allows developers to read and write configuration file values in a type safe manner, while considering interoperability between C and C++. \ No newline at end of file From e0e30696cb32b79e909f67122c5b480ea43755e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:12:31 +0000 Subject: [PATCH 31/48] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ut?= =?UTF-8?q?ils/ConfigBase/src?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ConfigBase/src/ConfigBase.cpp | 183 ------------- utils/ConfigBase/src/ConfigBaseCode.c | 55 ---- utils/ConfigBase/src/ConfigBaseCode.h | 29 -- utils/ConfigBase/src/ConfigBaseImpl.cpp | 347 ------------------------ utils/ConfigBase/src/ConfigBaseImpl.h | 51 ---- utils/ConfigBase/src/IConfigBase.cpp | 123 --------- utils/ConfigBase/src/IConfigBase.h | 57 ---- 7 files changed, 845 deletions(-) delete mode 100644 utils/ConfigBase/src/ConfigBase.cpp delete mode 100644 utils/ConfigBase/src/ConfigBaseCode.c delete mode 100644 utils/ConfigBase/src/ConfigBaseCode.h delete mode 100644 utils/ConfigBase/src/ConfigBaseImpl.cpp delete mode 100644 utils/ConfigBase/src/ConfigBaseImpl.h delete mode 100644 utils/ConfigBase/src/IConfigBase.cpp delete mode 100644 utils/ConfigBase/src/IConfigBase.h diff --git a/utils/ConfigBase/src/ConfigBase.cpp b/utils/ConfigBase/src/ConfigBase.cpp deleted file mode 100644 index e9a16365..00000000 --- a/utils/ConfigBase/src/ConfigBase.cpp +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "ConfigBase.h" -#include "IConfigBase.h" -#include "ILog.h" -#include "StatusCode.h" -#include -#include -static bool ObjectCheck(void *object) -{ - if (nullptr == object) { - LogError("nullptr object!\n"); - return false; - } - if (*((const char **)(((char *)object) - sizeof(IConfigBaseHeader))) != GetConfigBaseModuleName()) { - LogError("Illegal object!\n"); - return false; - } - return true; -} -void *OpenConfigFile(const char *fileName) -{ - std::shared_ptr *configObject = NewConfigBase(fileName); - if (nullptr != configObject) { - if ((*configObject)->OpenConfigFile() == false) { - return nullptr; - } - } - return configObject; -} -StatusCode ConfigSaveFile(void *object) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSaveFile(); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -void CloseConfigFile(void *object) -{ - if (ObjectCheck(object) == true) { - (*(std::shared_ptr *)object)->CloseConfigFile(); - (*(std::shared_ptr *)object).reset(); - free(((char *)object) - sizeof(IConfigBaseHeader)); // TODO: bug? - } -} -StatusCode ConfigGetInt(void *object, const char *name, int *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetInt(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetInt(void *object, const char *name, const int value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetInt(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetShort(void *object, const char *name, short *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetShort(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetShort(void *object, const char *name, const short value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetShort(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetLong(void *object, const char *name, long *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetLong(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetLong(void *object, const char *name, const long value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetLong(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetLLong(void *object, const char *name, long long *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetLLong(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetLLong(void *object, const char *name, const long long value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetLLong(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetChar(void *object, const char *name, char *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetChar(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetChar(void *object, const char *name, const char value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetChar(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetBool(void *object, const char *name, bool *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetBool(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetBool(void *object, const char *name, const bool value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetBool(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetFloat(void *object, const char *name, float *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetFloat(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetFloat(void *object, const char *name, const float value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetFloat(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetDouble(void *object, const char *name, double *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetDouble(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetDouble(void *object, const char *name, const double value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetDouble(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigGetString(void *object, const char *name, const char **value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigGetString(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} -StatusCode ConfigSetString(void *object, const char *name, const char *value) -{ - if (ObjectCheck(object) == true) { - return (*(std::shared_ptr *)object)->ConfigSetString(name, value); - } - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.c b/utils/ConfigBase/src/ConfigBaseCode.c deleted file mode 100644 index 3eca51ae..00000000 --- a/utils/ConfigBase/src/ConfigBaseCode.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "ConfigBaseCode.h" -#include "ConfigBase.h" -#include "ILog.h" -#include "StatusCode.h" -#include -static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {"CONFIG_CODE_PARAM_NOT_EXIST", - "CONFIG_CODE_END"}; -static const char *PrintStringConfigCode(const StatusCode this) -{ - const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END; - if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END) { - LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]); - return ConfigCodeString[CODE_INDEX]; - } - LogError("Config code undefine.\n"); - return "Config code undefine.\n"; -} -static const bool CodeEqual(const StatusCode code, const char *value) -{ - if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) { - return true; - } - return false; -} -static StatusCode NewConfigCode(const long int code) -{ - StatusCode result = {PrintStringConfigCode, CodeEqual, code}; - return result; -} -const StatusCode CreateConfigCode(const long int code) -{ - // if (STATUS_CODE_OK <= code && code < STATUS_CODE_END) - // { - // return CreateStatusCode(code); - // } - if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) { - return NewConfigCode(code); - } - LogError("undefined code.\n"); - return CreateStatusCode(STATUS_CODE_END); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.h b/utils/ConfigBase/src/ConfigBaseCode.h deleted file mode 100644 index 4b070a1d..00000000 --- a/utils/ConfigBase/src/ConfigBaseCode.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CONFIG_BASE_CODE_H -#define CONFIG_BASE_CODE_H -#include "ConfigBase.h" -#include "StatusCode.h" -#ifdef __cplusplus -extern "C" { -#endif -#ifndef CONFIG_OWNER - #error This is internal file, never include it. -#endif -const StatusCode CreateConfigCode(const long int code); -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.cpp b/utils/ConfigBase/src/ConfigBaseImpl.cpp deleted file mode 100644 index 09fdffd3..00000000 --- a/utils/ConfigBase/src/ConfigBaseImpl.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "ConfigBaseImpl.h" -#include "ConfigBase.h" -#include "ConfigBaseCode.h" -#include "ILog.h" -#include "StatusCode.h" -#include -#include -#include -#include -#include -#include -#include - -constexpr int INVALID_RESULT = -1; -#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)) -#define CHECK_FLOAT_LIMIT(value) (fabs(value - ((float)value)) < 0.000001 ? false : true) - -ConfigBaseImpl::ConfigBaseImpl(const std::string &fileName) : mFileName(fileName) -{ -} -bool ConfigBaseImpl::OpenConfigFile(void) -{ - config_init(&mCfg); - config_set_options(&mCfg, - (CONFIG_OPTION_FSYNC | CONFIG_OPTION_SEMICOLON_SEPARATORS | - CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE)); - constexpr int FIEL_EXIST = 0; - if (FIEL_EXIST == access(mFileName.c_str(), F_OK)) { - if (!config_read_file(&mCfg, mFileName.c_str())) { - LogError("Read file failed[%s].\n", mFileName.c_str()); - fprintf( - stderr, "%s:%d - %s\n", config_error_file(&mCfg), config_error_line(&mCfg), config_error_text(&mCfg)); - return false; - } - } - else { - LogInfo("Config file doesn't exist.mFileName = %s\n", mFileName.c_str()); - /* Write out the new configuration. */ - if (!config_write_file(&mCfg, mFileName.c_str())) { - fprintf(stderr, "Error while writing file.\n"); - return false; - } - } - return true; -} -void ConfigBaseImpl::CloseConfigFile(void) -{ - config_destroy(&mCfg); -} -StatusCode ConfigBaseImpl::ConfigSaveFile(void) -{ - LogInfo("Save file[%s].\n", mFileName.c_str()); - if (!config_write_file(&mCfg, mFileName.c_str())) { - LogError("Save config failed.\n"); - return CreateStatusCode(STATUS_CODE_NOT_OK); - } - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetInt(const char *name, int *value) -{ - int result = INVALID_RESULT; - result = config_lookup_int(&mCfg, name, value); - if (CONFIG_FALSE == result) { - return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); - } - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigSetInt(const char *name, const int value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_INT); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - config_setting_set_int(setting, value); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetShort(const char *name, short *value) -{ - int intValue = 0; - int result = 0; - result = config_lookup_int(&mCfg, name, &intValue); - if (CONFIG_FALSE == result || CHECK_SHORT_LIMIT(intValue)) { - return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); - } - *value = (short)intValue; - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigSetShort(const char *name, const short value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_INT); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - int intValue = value; - config_setting_set_int(setting, intValue); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetLong(const char *name, long *value) -{ - long long llongValue = 0; - int result = 0; - result = config_lookup_int64(&mCfg, 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); -} -StatusCode ConfigBaseImpl::ConfigSetLong(const char *name, const long value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_INT64); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - long long llongValue = value; - config_setting_set_int64(setting, llongValue); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetLLong(const char *name, long long *value) -{ - int result = 0; - result = config_lookup_int64(&mCfg, name, value); - if (CONFIG_FALSE == result) { - return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); - } - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigSetLLong(const char *name, const long long value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_INT64); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - config_setting_set_int64(setting, value); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetChar(const char *name, char *value) -{ - int charValue = 0; - int result = 0; - result = config_lookup_int(&mCfg, 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); -} -StatusCode ConfigBaseImpl::ConfigSetChar(const char *name, const char value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_INT); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - int charValue = (int)value; - config_setting_set_int(setting, charValue); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetBool(const char *name, bool *value) -{ - int result = 0; - result = config_lookup_bool(&mCfg, name, (int *)value); - if (CONFIG_FALSE == result) { - return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); - } - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigSetBool(const char *name, const bool value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_BOOL); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - config_setting_set_bool(setting, (int)value); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetFloat(const char *name, float *value) -{ - double dValue = 0; - int result = 0; - result = config_lookup_float(&mCfg, 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); -} -StatusCode ConfigBaseImpl::ConfigSetFloat(const char *name, const float value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_FLOAT); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - double dValue = value; - config_setting_set_float(setting, dValue); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetDouble(const char *name, double *value) -{ - int result = 0; - result = config_lookup_float(&mCfg, name, value); - if (CONFIG_FALSE == result) { - return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); - } - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigSetDouble(const char *name, const double value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (nullptr == setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_FLOAT); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - config_setting_set_float(setting, value); - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigGetString(const char *name, const char **value) -{ - int result = 0; - result = config_lookup_string(&mCfg, name, value); - if (CONFIG_FALSE == result) { - return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); - } - return CreateStatusCode(STATUS_CODE_OK); -} -StatusCode ConfigBaseImpl::ConfigSetString(const char *name, const char *value) -{ - config_setting_t *root = nullptr; - config_setting_t *setting = nullptr; - root = config_root_setting(&mCfg); - if (nullptr == root) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - setting = config_setting_get_member(root, name); - if (!setting) { - setting = config_setting_add(root, name, CONFIG_TYPE_STRING); - } - if (nullptr == setting) { - LogError("Config function failed.\n"); - return CreateConfigCode(STATUS_CODE_NOT_OK); - } - config_setting_set_string(setting, value); - return CreateStatusCode(STATUS_CODE_OK); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.h b/utils/ConfigBase/src/ConfigBaseImpl.h deleted file mode 100644 index 58c35df5..00000000 --- a/utils/ConfigBase/src/ConfigBaseImpl.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CONFIG_BASE_IMPL_H -#define CONFIG_BASE_IMPL_H -#include "IConfigBase.h" -#include -#include -class ConfigBaseImpl : public IConfigBase -{ -public: - ConfigBaseImpl(const std::string &fileName); - virtual ~ConfigBaseImpl() = default; - bool OpenConfigFile(void) override; - void CloseConfigFile(void) override; - StatusCode ConfigSaveFile(void) override; - StatusCode ConfigGetInt(const char *name, int *value) override; - StatusCode ConfigSetInt(const char *name, const int value) override; - StatusCode ConfigGetShort(const char *name, short *value) override; - StatusCode ConfigSetShort(const char *name, const short value) override; - StatusCode ConfigGetLong(const char *name, long *value) override; - StatusCode ConfigSetLong(const char *name, const long value) override; - StatusCode ConfigGetLLong(const char *name, long long *value) override; - StatusCode ConfigSetLLong(const char *name, const long long value) override; - StatusCode ConfigGetChar(const char *name, char *value) override; - StatusCode ConfigSetChar(const char *name, const char value) override; - StatusCode ConfigGetBool(const char *name, bool *value) override; - StatusCode ConfigSetBool(const char *name, const bool value) override; - StatusCode ConfigGetFloat(const char *name, float *value) override; - StatusCode ConfigSetFloat(const char *name, const float value) override; - StatusCode ConfigGetDouble(const char *name, double *value) override; - StatusCode ConfigSetDouble(const char *name, const double value) override; - StatusCode ConfigGetString(const char *name, const char **value) override; - StatusCode ConfigSetString(const char *name, const char *value) override; - -private: - const std::string mFileName; - config_t mCfg; -}; -#endif \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.cpp b/utils/ConfigBase/src/IConfigBase.cpp deleted file mode 100644 index de94df75..00000000 --- a/utils/ConfigBase/src/IConfigBase.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "IConfigBase.h" -#include "ConfigBaseImpl.h" -#include "ILog.h" -#include "StatusCode.h" -#include -#include -#include -bool IConfigBase::OpenConfigFile(void) -{ - return false; -} -void IConfigBase::CloseConfigFile(void) -{ -} -StatusCode IConfigBase::ConfigSaveFile(void) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetInt(const char *name, int *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetInt(const char *name, const int value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetShort(const char *name, short *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetShort(const char *name, const short value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetLong(const char *name, long *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetLong(const char *name, const long value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetLLong(const char *name, long long *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetLLong(const char *name, const long long value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetChar(const char *name, char *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetChar(const char *name, const char value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetBool(const char *name, bool *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetBool(const char *name, const bool value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetFloat(const char *name, float *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetFloat(const char *name, const float value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetDouble(const char *name, double *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetDouble(const char *name, const double value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigGetString(const char *name, const char **value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode IConfigBase::ConfigSetString(const char *name, const char *value) -{ - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -static const char *CONFIG_BASE_NAME = "config_base"; -const char *GetConfigBaseModuleName(void) -{ - return CONFIG_BASE_NAME; -} -std::shared_ptr *NewConfigBase(const char *fileName) -{ - LogInfo("Create the config base object.\n"); - ConfigBase *impl = (ConfigBase *)malloc(sizeof(ConfigBase)); - if (nullptr == impl) { - LogError("NewConfigBase::malloc failed.\n"); - return nullptr; - } - ConfigBase tmp; - memcpy((void *)impl, (void *)&tmp, sizeof(ConfigBase)); - impl->mHeader.mCheckName = CONFIG_BASE_NAME; - impl->mIConfigBase = std::make_shared(fileName); - return (std::shared_ptr *)(((char *)impl) + sizeof(IConfigBaseHeader)); -} \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.h b/utils/ConfigBase/src/IConfigBase.h deleted file mode 100644 index 0fec945a..00000000 --- a/utils/ConfigBase/src/IConfigBase.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef I_CONFIG_BASE_H -#define I_CONFIG_BASE_H -#include "StatusCode.h" -#include -class IConfigBase -{ -public: - IConfigBase() = default; - virtual ~IConfigBase() = default; - virtual bool OpenConfigFile(void); - virtual void CloseConfigFile(void); - virtual StatusCode ConfigSaveFile(void); - virtual StatusCode ConfigGetInt(const char *name, int *value); - virtual StatusCode ConfigSetInt(const char *name, const int value); - virtual StatusCode ConfigGetShort(const char *name, short *value); - virtual StatusCode ConfigSetShort(const char *name, const short value); - virtual StatusCode ConfigGetLong(const char *name, long *value); - virtual StatusCode ConfigSetLong(const char *name, const long value); - virtual StatusCode ConfigGetLLong(const char *name, long long *value); - virtual StatusCode ConfigSetLLong(const char *name, const long long value); - virtual StatusCode ConfigGetChar(const char *name, char *value); - virtual StatusCode ConfigSetChar(const char *name, const char value); - virtual StatusCode ConfigGetBool(const char *name, bool *value); - virtual StatusCode ConfigSetBool(const char *name, const bool value); - virtual StatusCode ConfigGetFloat(const char *name, float *value); - virtual StatusCode ConfigSetFloat(const char *name, const float value); - virtual StatusCode ConfigGetDouble(const char *name, double *value); - virtual StatusCode ConfigSetDouble(const char *name, const double value); - virtual StatusCode ConfigGetString(const char *name, const char **value); - virtual StatusCode ConfigSetString(const char *name, const char *value); -}; -typedef struct i_config_base_header -{ - const char *mCheckName; -} IConfigBaseHeader; -typedef struct config_base -{ - IConfigBaseHeader mHeader; - std::shared_ptr mIConfigBase; -} ConfigBase; -const char *GetConfigBaseModuleName(void); -std::shared_ptr *NewConfigBase(const char *fileName); -#endif \ No newline at end of file From 62ba1ec99d2e1f0adad87a16b930440a46e302b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 27 Jul 2024 09:12:47 +0000 Subject: [PATCH 32/48] 11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/src/ConfigBase.cpp | 183 +++++++++++++ utils/ConfigBase/src/ConfigBaseCode.c | 55 ++++ utils/ConfigBase/src/ConfigBaseCode.h | 35 +++ utils/ConfigBase/src/ConfigBaseImpl.cpp | 347 ++++++++++++++++++++++++ utils/ConfigBase/src/ConfigBaseImpl.h | 53 ++++ utils/ConfigBase/src/IConfigBase.cpp | 123 +++++++++ utils/ConfigBase/src/IConfigBase.h | 62 +++++ 7 files changed, 858 insertions(+) create mode 100644 utils/ConfigBase/src/ConfigBase.cpp create mode 100644 utils/ConfigBase/src/ConfigBaseCode.c create mode 100644 utils/ConfigBase/src/ConfigBaseCode.h create mode 100644 utils/ConfigBase/src/ConfigBaseImpl.cpp create mode 100644 utils/ConfigBase/src/ConfigBaseImpl.h create mode 100644 utils/ConfigBase/src/IConfigBase.cpp create mode 100644 utils/ConfigBase/src/IConfigBase.h diff --git a/utils/ConfigBase/src/ConfigBase.cpp b/utils/ConfigBase/src/ConfigBase.cpp new file mode 100644 index 00000000..e7f19927 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBase.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ConfigBase.h" +#include "IConfigBase.h" +#include "ILog.h" +#include "StatusCode.h" +#include +#include +static bool ObjectCheck(void *object) +{ + if (nullptr == object) { + LogError("nullptr object!\n"); + return false; + } + if (*((const char **)(((char *)object) - sizeof(IConfigBaseHeader))) != GetConfigBaseModuleName()) { + LogError("Illegal object!\n"); + return false; + } + return true; +} +void *OpenConfigFile(const char *fileName) +{ + std::shared_ptr *configObject = NewConfigBase(fileName); + if (nullptr != configObject) { + if ((*configObject)->OpenConfigFile() == false) { + return nullptr; + } + } + return configObject; +} +StatusCode ConfigSaveFile(void *object) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSaveFile(); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +void CloseConfigFile(void *object) +{ + if (ObjectCheck(object) == true) { + (*(std::shared_ptr *)object)->CloseConfigFile(); + (*(std::shared_ptr *)object).reset(); + free(((char *)object) - sizeof(IConfigBaseHeader)); // TODO: bug? + } +} +StatusCode ConfigGetInt(void *object, const char *name, int *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetInt(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetInt(void *object, const char *name, const int value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetInt(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetShort(void *object, const char *name, short *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetShort(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetShort(void *object, const char *name, const short value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetShort(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetLong(void *object, const char *name, long *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetLong(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetLong(void *object, const char *name, const long value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetLong(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetLLong(void *object, const char *name, long long *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetLLong(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetLLong(void *object, const char *name, const long long value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetLLong(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetChar(void *object, const char *name, char *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetChar(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetChar(void *object, const char *name, const char value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetChar(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetBool(void *object, const char *name, bool *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetBool(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetBool(void *object, const char *name, const bool value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetBool(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetFloat(void *object, const char *name, float *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetFloat(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetFloat(void *object, const char *name, const float value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetFloat(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetDouble(void *object, const char *name, double *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetDouble(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetDouble(void *object, const char *name, const double value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetDouble(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigGetString(void *object, const char *name, const char **value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigGetString(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +StatusCode ConfigSetString(void *object, const char *name, const char *value) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->ConfigSetString(name, value); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.c b/utils/ConfigBase/src/ConfigBaseCode.c new file mode 100644 index 00000000..02b0f434 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseCode.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ConfigBaseCode.h" +#include "ConfigBase.h" +#include "ILog.h" +#include "StatusCode.h" +#include +static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {"CONFIG_CODE_PARAM_NOT_EXIST", + "CONFIG_CODE_END"}; +static const char *PrintStringConfigCode(const StatusCode this) +{ + const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END; + if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END) { + LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]); + return ConfigCodeString[CODE_INDEX]; + } + LogError("Config code undefine.\n"); + return "Config code undefine.\n"; +} +static const bool CodeEqual(const StatusCode code, const char *value) +{ + if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) { + return true; + } + return false; +} +static StatusCode NewConfigCode(const long int code) +{ + StatusCode result = {PrintStringConfigCode, CodeEqual, code}; + return result; +} +const StatusCode CreateConfigCode(const long int code) +{ + // if (STATUS_CODE_OK <= code && code < STATUS_CODE_END) + // { + // return CreateStatusCode(code); + // } + if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) { + return NewConfigCode(code); + } + LogError("undefined code.\n"); + return CreateStatusCode(STATUS_CODE_END); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseCode.h b/utils/ConfigBase/src/ConfigBaseCode.h new file mode 100644 index 00000000..030aac9a --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseCode.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//Copyright Statement and License: The beginning of the file contains a copyright statement and license information, indicating that this code is copyrighted by Fancy Code and follows the terms of Apache License, Version 2.0. This means that using this code must comply with the Apache 2.0 license regulations +#ifndef CONFIG_BASE_CODE_H +#define CONFIG_BASE_CODE_H +//#The three pieces of code, ifndef CONFIG_SBASE-CEDE.H, # define CONFIG_SBASE-CEDE.H, and # endif, are standard header file protectors (also known as inclusion guards) used to prevent header files from being duplicated. +#include "ConfigBase.h" +#include "StatusCode.h" +//These two lines of code contain two other header files that may define the Config Base class and Status Code enumeration or class, which will be used in the current header file. +#ifdef __cplusplus +extern "C" { +#endif +//It is to ensure that the code written in C language can be linked and used correctly when used in the C++environment. Extern "C" tells the C++compiler that this part of the code should be compiled and linked according to the rules of the C language to avoid the problem of name mangling in C++. +#ifndef CONFIG_OWNER + #error This is internal file, never include it. + // These two lines of code are a compile time check. If the VNet OWNER macro is not defined, the compiler will report an error and display the message 'This is internal file, never include it.'. This is usually used to prevent this header file from being directly included externally, possibly because it contains some definitions or declarations that should only be used internally within the library. +#endif +const StatusCode CreateConfigCode(const long int code);// This line of code declares a function called CreateConfig Code, which takes a parameter code of type long int and returns a value of type Status Code. This function may be used to create a configuration object or perform a configuration operation based on a given code (possibly some configuration identifier), and return a status code to indicate the success or failure of the operation. +#ifdef __cplusplus +} +#endif +#endif +//In summary, this header file defines a function declaration related to the configuration code, and ensures the correctness and security of the code through a series of conditional compilations and header file protectors. \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.cpp b/utils/ConfigBase/src/ConfigBaseImpl.cpp new file mode 100644 index 00000000..c0e91c53 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseImpl.cpp @@ -0,0 +1,347 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ConfigBaseImpl.h" +#include "ConfigBase.h" +#include "ConfigBaseCode.h" +#include "ILog.h" +#include "StatusCode.h" +#include +#include +#include +#include +#include +#include +#include + +constexpr int INVALID_RESULT = -1; +#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)) +#define CHECK_FLOAT_LIMIT(value) (fabs(value - ((float)value)) < 0.000001 ? false : true) + +ConfigBaseImpl::ConfigBaseImpl(const std::string &fileName) : mFileName(fileName) +{ +} +bool ConfigBaseImpl::OpenConfigFile(void) +{ + config_init(&mCfg); + config_set_options(&mCfg, + (CONFIG_OPTION_FSYNC | CONFIG_OPTION_SEMICOLON_SEPARATORS | + CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE)); + constexpr int FIEL_EXIST = 0; + if (FIEL_EXIST == access(mFileName.c_str(), F_OK)) { + if (!config_read_file(&mCfg, mFileName.c_str())) { + LogError("Read file failed[%s].\n", mFileName.c_str()); + fprintf( + stderr, "%s:%d - %s\n", config_error_file(&mCfg), config_error_line(&mCfg), config_error_text(&mCfg)); + return false; + } + } + else { + LogInfo("Config file doesn't exist.mFileName = %s\n", mFileName.c_str()); + /* Write out the new configuration. */ + if (!config_write_file(&mCfg, mFileName.c_str())) { + fprintf(stderr, "Error while writing file.\n"); + return false; + } + } + return true; +} +void ConfigBaseImpl::CloseConfigFile(void) +{ + config_destroy(&mCfg); +} +StatusCode ConfigBaseImpl::ConfigSaveFile(void) +{ + LogInfo("Save file[%s].\n", mFileName.c_str()); + if (!config_write_file(&mCfg, mFileName.c_str())) { + LogError("Save config failed.\n"); + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetInt(const char *name, int *value) +{ + int result = INVALID_RESULT; + result = config_lookup_int(&mCfg, name, value); + if (CONFIG_FALSE == result) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigSetInt(const char *name, const int value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_INT); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + config_setting_set_int(setting, value); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetShort(const char *name, short *value) +{ + int intValue = 0; + int result = 0; + result = config_lookup_int(&mCfg, name, &intValue); + if (CONFIG_FALSE == result || CHECK_SHORT_LIMIT(intValue)) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + *value = (short)intValue; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigSetShort(const char *name, const short value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_INT); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + int intValue = value; + config_setting_set_int(setting, intValue); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetLong(const char *name, long *value) +{ + long long llongValue = 0; + int result = 0; + result = config_lookup_int64(&mCfg, 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); +} +StatusCode ConfigBaseImpl::ConfigSetLong(const char *name, const long value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_INT64); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + long long llongValue = value; + config_setting_set_int64(setting, llongValue); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetLLong(const char *name, long long *value) +{ + int result = 0; + result = config_lookup_int64(&mCfg, name, value); + if (CONFIG_FALSE == result) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigSetLLong(const char *name, const long long value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_INT64); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + config_setting_set_int64(setting, value); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetChar(const char *name, char *value) +{ + int charValue = 0; + int result = 0; + result = config_lookup_int(&mCfg, 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); +} +StatusCode ConfigBaseImpl::ConfigSetChar(const char *name, const char value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_INT); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + int charValue = (int)value; + config_setting_set_int(setting, charValue); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetBool(const char *name, bool *value) +{ + int result = 0; + result = config_lookup_bool(&mCfg, name, (int *)value); + if (CONFIG_FALSE == result) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigSetBool(const char *name, const bool value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_BOOL); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + config_setting_set_bool(setting, (int)value); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetFloat(const char *name, float *value) +{ + double dValue = 0; + int result = 0; + result = config_lookup_float(&mCfg, 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); +} +StatusCode ConfigBaseImpl::ConfigSetFloat(const char *name, const float value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_FLOAT); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + double dValue = value; + config_setting_set_float(setting, dValue); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetDouble(const char *name, double *value) +{ + int result = 0; + result = config_lookup_float(&mCfg, name, value); + if (CONFIG_FALSE == result) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigSetDouble(const char *name, const double value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (nullptr == setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_FLOAT); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + config_setting_set_float(setting, value); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigGetString(const char *name, const char **value) +{ + int result = 0; + result = config_lookup_string(&mCfg, name, value); + if (CONFIG_FALSE == result) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode ConfigBaseImpl::ConfigSetString(const char *name, const char *value) +{ + config_setting_t *root = nullptr; + config_setting_t *setting = nullptr; + root = config_root_setting(&mCfg); + if (nullptr == root) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + setting = config_setting_get_member(root, name); + if (!setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_STRING); + } + if (nullptr == setting) { + LogError("Config function failed.\n"); + return CreateConfigCode(STATUS_CODE_NOT_OK); + } + config_setting_set_string(setting, value); + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/ConfigBaseImpl.h b/utils/ConfigBase/src/ConfigBaseImpl.h new file mode 100644 index 00000000..e5ac3ae0 --- /dev/null +++ b/utils/ConfigBase/src/ConfigBaseImpl.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CONFIG_BASE_IMPL_H +#define CONFIG_BASE_IMPL_H +//Standard header file protection is used to prevent duplicate header files from being included. +#include "IConfigBase.h"//This is an interface definition file, and the Config Base Impl class implements all the pure virtual functions declared in this interface. +#include //This is the header file of the libconfig library, which provides an API for handling configuration files. +#include //String processing class in the C++standard library. +class ConfigBaseImpl : public IConfigBase//The Config Base Impl class inherits from the FHIR figBase interface, which means it needs to implement all the pure virtual functions declared in the interface. +{ +public: + ConfigBaseImpl(const std::string &fileName); + virtual ~ConfigBaseImpl() = default; + bool OpenConfigFile(void) override;//Open configuration file + void CloseConfigFile(void) override;//Close configuration file + StatusCode ConfigSaveFile(void) override;//Used to save configuration changes back to a file, but the return type is Status Code + StatusCode ConfigGetInt(const char *name, int *value) override; + StatusCode ConfigSetInt(const char *name, const int value) override; + StatusCode ConfigGetShort(const char *name, short *value) override; + StatusCode ConfigSetShort(const char *name, const short value) override; + StatusCode ConfigGetLong(const char *name, long *value) override; + StatusCode ConfigSetLong(const char *name, const long value) override; + StatusCode ConfigGetLLong(const char *name, long long *value) override; + StatusCode ConfigSetLLong(const char *name, const long long value) override; + StatusCode ConfigGetChar(const char *name, char *value) override; + StatusCode ConfigSetChar(const char *name, const char value) override; + StatusCode ConfigGetBool(const char *name, bool *value) override; + StatusCode ConfigSetBool(const char *name, const bool value) override; + StatusCode ConfigGetFloat(const char *name, float *value) override; + StatusCode ConfigSetFloat(const char *name, const float value) override; + StatusCode ConfigGetDouble(const char *name, double *value) override; + StatusCode ConfigSetDouble(const char *name, const double value) override; + StatusCode ConfigGetString(const char *name, const char **value) override; + StatusCode ConfigSetString(const char *name, const char *value) override; +//These functions provide interfaces for obtaining and setting various data types (such as integers, floating-point numbers, strings, etc.) from configuration files. They accept the name of a configuration item and a pointer to the value (for Get functions) or the value itself (for Set functions), and return a Status Code representing the operation result. +private: + const std::string mFileName;// The path to store configuration files. + config_t mCfg;//The confic_t type in the libconfig library is used to represent the internal structure of configuration files. +}; +#endif +//In summary, this code defines a class for handling configuration files that provides rich interfaces to read and modify configuration items in the configuration file, achieved through the use of the libconfig library. \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.cpp b/utils/ConfigBase/src/IConfigBase.cpp new file mode 100644 index 00000000..09447bd5 --- /dev/null +++ b/utils/ConfigBase/src/IConfigBase.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "IConfigBase.h" +#include "ConfigBaseImpl.h" +#include "ILog.h" +#include "StatusCode.h" +#include +#include +#include +bool IConfigBase::OpenConfigFile(void) +{ + return false; +} +void IConfigBase::CloseConfigFile(void) +{ +} +StatusCode IConfigBase::ConfigSaveFile(void) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetInt(const char *name, int *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetInt(const char *name, const int value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetShort(const char *name, short *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetShort(const char *name, const short value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetLong(const char *name, long *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetLong(const char *name, const long value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetLLong(const char *name, long long *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetLLong(const char *name, const long long value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetChar(const char *name, char *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetChar(const char *name, const char value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetBool(const char *name, bool *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetBool(const char *name, const bool value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetFloat(const char *name, float *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetFloat(const char *name, const float value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetDouble(const char *name, double *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetDouble(const char *name, const double value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigGetString(const char *name, const char **value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode IConfigBase::ConfigSetString(const char *name, const char *value) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +static const char *CONFIG_BASE_NAME = "config_base"; +const char *GetConfigBaseModuleName(void) +{ + return CONFIG_BASE_NAME; +} +std::shared_ptr *NewConfigBase(const char *fileName) +{ + LogInfo("Create the config base object.\n"); + ConfigBase *impl = (ConfigBase *)malloc(sizeof(ConfigBase)); + if (nullptr == impl) { + LogError("NewConfigBase::malloc failed.\n"); + return nullptr; + } + ConfigBase tmp; + memcpy((void *)impl, (void *)&tmp, sizeof(ConfigBase)); + impl->mHeader.mCheckName = CONFIG_BASE_NAME; + impl->mIConfigBase = std::make_shared(fileName); + return (std::shared_ptr *)(((char *)impl) + sizeof(IConfigBaseHeader)); +} \ No newline at end of file diff --git a/utils/ConfigBase/src/IConfigBase.h b/utils/ConfigBase/src/IConfigBase.h new file mode 100644 index 00000000..a69b0596 --- /dev/null +++ b/utils/ConfigBase/src/IConfigBase.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef I_CONFIG_BASE_H +#define I_CONFIG_BASE_H +//Header file protection (preventing header files from being duplicated). +#include "StatusCode.h" +#include +//Status Code. h (which may be a file that defines a status code enumeration or class to indicate the status of successful or failed operations) and(a part of the C++standard library that provides memory management features such as smart pointers). +class IConfigBase// FHIR gBase interface +{ +public: + IConfigBase() = default; + virtual ~IConfigBase() = default; + virtual bool OpenConfigFile(void); + virtual void CloseConfigFile(void); + virtual StatusCode ConfigSaveFile(void); + virtual StatusCode ConfigGetInt(const char *name, int *value); + virtual StatusCode ConfigSetInt(const char *name, const int value); + virtual StatusCode ConfigGetShort(const char *name, short *value); + virtual StatusCode ConfigSetShort(const char *name, const short value); + virtual StatusCode ConfigGetLong(const char *name, long *value); + virtual StatusCode ConfigSetLong(const char *name, const long value); + virtual StatusCode ConfigGetLLong(const char *name, long long *value); + virtual StatusCode ConfigSetLLong(const char *name, const long long value); + virtual StatusCode ConfigGetChar(const char *name, char *value); + virtual StatusCode ConfigSetChar(const char *name, const char value); + virtual StatusCode ConfigGetBool(const char *name, bool *value); + virtual StatusCode ConfigSetBool(const char *name, const bool value); + virtual StatusCode ConfigGetFloat(const char *name, float *value); + virtual StatusCode ConfigSetFloat(const char *name, const float value); + virtual StatusCode ConfigGetDouble(const char *name, double *value); + virtual StatusCode ConfigSetDouble(const char *name, const double value); + virtual StatusCode ConfigGetString(const char *name, const char **value); + virtual StatusCode ConfigSetString(const char *name, const char *value); +}; +//IKON Base is an abstract base class that defines interfaces related to configuration management. It includes default implementations of constructors and destructors (i.e. do not perform any operations), as well as a series of virtual functions for opening and closing configuration files, saving configurations, and obtaining and setting configuration values for various data types. The specific implementation of these virtual functions will be provided by derived classes. +typedef struct i_config_base_header//Defined a struct i_config.base_header containing a pointer mCheckName to a character, which may be used for some form of validation or recognition. +{ + const char *mCheckName; +} IConfigBaseHeader; +typedef struct config_base//Defined a struct config.base that contains a member mHeader of type iKON figBaseHeader and a smart pointer mIConfig Base pointing to iKON figBase. This structure may be used to store and manage configuration instances in applications. +{ + IConfigBaseHeader mHeader; + std::shared_ptr mIConfigBase; +} ConfigBase; +const char *GetConfigBaseModuleName(void); +std::shared_ptr *NewConfigBase(const char *fileName); +//Declared two functions: FHIR nfigBaseModulus Name (which may be used to obtain the name of a configuration module) and NewConfig Base (which takes a file name as a parameter and returns a pointer to the FHIR figBase smart pointer for creating and returning a new configuration instance). +#endif +//This code defines a C++interface and related data structures for configuration management, as well as function declarations related to it. It utilizes the abstract base classes, virtual functions, smart pointers, and other features of C++to achieve flexible configuration management functionality. \ No newline at end of file From a795e42b1291c256038aaf4c635c6a7f9363ff16 Mon Sep 17 00:00:00 2001 From: ssslleep <2720057338@qq.com> Date: Sat, 27 Jul 2024 09:33:16 +0000 Subject: [PATCH 33/48] update utils/UartDevice/readme.md. Signed-off-by: ssslleep <2720057338@qq.com> --- utils/UartDevice/readme.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/utils/UartDevice/readme.md b/utils/UartDevice/readme.md index 2480b329..09c8174e 100644 --- a/utils/UartDevice/readme.md +++ b/utils/UartDevice/readme.md @@ -4,13 +4,11 @@ # 涉及的知识 * uart通信协议 * 多线程 -* 系统调用函数的使用 * C++类和对象 * 函数指针 # 各文件的作用 -* **UartDevice.h**:定义了一个名为uart_info的结构体存储UART设备的配置信息,定义了用于UART通信的函数接口,这些函数通常与UART硬件的底层实现相对应。 -* **UartDevice.cpp**:提供了一个跨平台的UART设备接口模块,使得上层应用可以通过统一的接口来操作不同的UART硬件设备。 -* **UartDeviceImpl.cpp**:提供了UART 设备操作接口,允许用户打开、发送和接收数据到 UART 设备,实现了 UART 设备的配置、数据发送和接收等功能。 -* **UartDeviceImpl.h**:定义了一个用于UART设备操作的类 UartDeviceImpl 和相关的结构体和函数,提供了一种通过接口和智能指针封装具体实现的方式来管理UART设备。 \ No newline at end of file +* **UartDevice.h**:为上层应用提供了封装好的函数接口,用于与uart硬件设备进行交互。 +* **UartDevice.cpp**:UartDevice.h中函数接口的具体定义和封装。 +* **UartDeviceImpl.cpp/UartDeviceImpl.h**:对uart硬件设备更底层更具体的一些操作,这里面定义和声明的函数会提供给UartDevice.cpp做进一步的封装。 From bf6c7f7b381e1f74600bd2a14279fbed9537df84 Mon Sep 17 00:00:00 2001 From: ssslleep <2720057338@qq.com> Date: Sat, 27 Jul 2024 09:35:49 +0000 Subject: [PATCH 34/48] update utils/UartDevice/readme.md. Signed-off-by: ssslleep <2720057338@qq.com> --- utils/UartDevice/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/UartDevice/readme.md b/utils/UartDevice/readme.md index 09c8174e..3d492a85 100644 --- a/utils/UartDevice/readme.md +++ b/utils/UartDevice/readme.md @@ -11,4 +11,4 @@ # 各文件的作用 * **UartDevice.h**:为上层应用提供了封装好的函数接口,用于与uart硬件设备进行交互。 * **UartDevice.cpp**:UartDevice.h中函数接口的具体定义和封装。 -* **UartDeviceImpl.cpp/UartDeviceImpl.h**:对uart硬件设备更底层更具体的一些操作,这里面定义和声明的函数会提供给UartDevice.cpp做进一步的封装。 +* **UartDeviceImpl.cpp/UartDeviceImpl.h**:这里面定义和声明的函数是对uart硬件设备更底层更具体的一些操作,它们会被提供给UartDevice.cpp做进一步的封装。 From 5b31f9fbde07d0137c5fa0d418fda0be6c79e6f8 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 27 Jul 2024 20:59:44 +0800 Subject: [PATCH 35/48] add some annotate --- utils/TcpModule/src/TcpServerImpl.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/TcpModule/src/TcpServerImpl.h b/utils/TcpModule/src/TcpServerImpl.h index 007f63fb..44dd08a4 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -52,9 +52,9 @@ public: void Closed(void); private: - std::mutex mMutex; - hloop_t *mLoop; - hio_t *mIo; + std::mutex mMutex; ///A mutex lock used to synchronize access to shared resources. + hloop_t *mLoop; ///Event loop, listening for io objects + hio_t *mIo; ///Socket handle const TcpServerParam mParam; std::thread mTcpServerThread; std::map *> mClients; From 0eda768579f540f14463039104e4f148aaef9b96 Mon Sep 17 00:00:00 2001 From: binbinnomoney <1749303514@qq.com> Date: Thu, 1 Aug 2024 12:56:20 +0800 Subject: [PATCH 36/48] Log --- test/utils/Log/CMakeLists.txt | 2 +- test/utils/Log/src/ILogTest.cpp | 77 +--------------------------- test/utils/Log/src/easylogging++.xml | 32 ------------ utils/Log/src/Log.cpp | 20 +++++++- 4 files changed, 22 insertions(+), 109 deletions(-) delete mode 100644 test/utils/Log/src/easylogging++.xml diff --git a/test/utils/Log/CMakeLists.txt b/test/utils/Log/CMakeLists.txt index d9249b23..0b4e92c5 100644 --- a/test/utils/Log/CMakeLists.txt +++ b/test/utils/Log/CMakeLists.txt @@ -6,7 +6,7 @@ include_directories( . ./src ./include - ${UTILS_SOURCE_PATH}/Log/src/easyloggingpp + ${UTILS_SOURCE_PATH}/Log/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include diff --git a/test/utils/Log/src/ILogTest.cpp b/test/utils/Log/src/ILogTest.cpp index 143b14fa..4e219da7 100644 --- a/test/utils/Log/src/ILogTest.cpp +++ b/test/utils/Log/src/ILogTest.cpp @@ -1,4 +1,4 @@ -/* + #include "ILog.h" // #include #include @@ -7,6 +7,7 @@ namespace ILogTest // ../output_files/test/bin/LogTest --gtest_filter=ILogTest.Demo TEST(ILogTest, Demo) { + CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); LogInfo("hello world."); @@ -15,78 +16,4 @@ namespace ILogTest ILogUnInit(); DestroyLogModule(); } -} // namespace ILogTest -*/ - -#define ELPP_THREAD_SAFE -#include "easylogging++.h" -#include -#include - -INITIALIZE_EASYLOGGINGPP - -namespace ILogTest -{ - TEST(ILogTest, Demo) - { - // 假设你已经在全局范围内设置了日志格式 - el::Configurations defaultConf; - defaultConf.setToDefault(); - /* - defaultConf.set(el::Level::Global, - el::ConfigurationType::Format, - "\x1b[32m%datetime %level %msg\x1b[0m"); // 绿色日志消息 - - */ - - // 设置 INFO 级别的日志格式为绿色 - defaultConf.set(el::Level::Info, - el::ConfigurationType::Format, - "\x1b[32m%datetime %level %msg\x1b[0m"); - - // 设置 WARNING 级别的日志格式为黄色 - defaultConf.set(el::Level::Debug, - el::ConfigurationType::Format, - "\x1b[33m%datetime %level %msg\x1b[0m"); - - // 设置 ERROR 级别的日志格式为红色 - defaultConf.set(el::Level::Error, - el::ConfigurationType::Format, - "\x1b[31m%datetime %level %msg\x1b[0m"); - - // 注意:上面的格式将所有日志消息都设置为绿色。 - // 如果你想要为不同的日志级别设置不同的颜色,你需要为每个级别分别设置格式。 - - // 另一种方法是使用条件格式,但这在 INI 配置文件中可能更难以实现, - // 因此你可能需要通过编程方式来实现。 - - el::Loggers::reconfigureLogger("default", defaultConf); - - - - - - // 如果你使用的是配置文件,确保它已正确设置并加载 - // 否则,你可以在这里通过编程方式配置elpp - - // 示例:动态设置配置以启用颜色输出(注意:这只是一个示例,实际配置可能不同) - // el::Configurations defaultConf; - // defaultConf.setToDefault(); - // defaultConf.set(el::Level::Global, - // el::ConfigurationType::Format, - // "%datetime %level %msg %func %loc"); - // defaultConf.set(el::Level::Global, - // el::ConfigurationType::EnableColoredLogMessages, - // "true"); - // el::Loggers::reconfigureLogger("default", defaultConf); - - // 使用easylogging++的日志宏 - LOG(INFO) << "hello world."; - LOG(ERROR) << "create ... failed."; - LOG(DEBUG) << "a = " << 124 << " b = " << "apple"; - - // 注意:在单元测试中,你可能不需要初始化或销毁日志模块, - // 因为easylogging++已经为你处理了这些。 - // 但是,如果你有自己的日志封装或初始化代码,你应该根据需要进行调整。 - } } // namespace ILogTest \ No newline at end of file diff --git a/test/utils/Log/src/easylogging++.xml b/test/utils/Log/src/easylogging++.xml deleted file mode 100644 index 6c52d073..00000000 --- a/test/utils/Log/src/easylogging++.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - true - - - - %datetime %level %msg - - - - - - - true - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/utils/Log/src/Log.cpp b/utils/Log/src/Log.cpp index 5974d916..d9f59b50 100644 --- a/utils/Log/src/Log.cpp +++ b/utils/Log/src/Log.cpp @@ -19,6 +19,7 @@ #include #include #include +#include /** * @brief jieshao * @@ -33,7 +34,24 @@ static void LogFree(ILog *object) } static int LogPrintf(ILog *object, const char *function, const int line, const enum LogType type, const char *format, ...) -{ +{ + el::Configurations defaultConf; + defaultConf.setToDefault(); + + defaultConf.set(el::Level::Info, + el::ConfigurationType::Format, + "\x1b[32m%datetime %level %msg\x1b[0m"); + + defaultConf.set(el::Level::Debug, + el::ConfigurationType::Format, + "\x1b[33m%datetime %level %msg\x1b[0m"); + + defaultConf.set(el::Level::Error, + el::ConfigurationType::Format, + "\x1b[31m%datetime %level %msg\x1b[0m"); + + el::Loggers::reconfigureLogger("default", defaultConf); + // TODO: // LogTypeToString(type); constexpr int SEND_TRACE_BUFF_SIZE = 2048; From 625e9aabb3fbf97008b6de871e99e5dbf5a8b24a Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Fri, 2 Aug 2024 22:37:34 +0800 Subject: [PATCH 37/48] add some annotate --- utils/TcpModule/include/TcpModule.h | 12 ++-- utils/TcpModule/readme.md | 1 + utils/TcpModule/src/ITcpClient.h | 2 +- utils/TcpModule/src/TcpClientImpl.cpp | 26 ++++---- utils/TcpModule/src/TcpClientImpl.h | 10 ++-- utils/TcpModule/src/TcpModule.cpp | 6 +- utils/TcpModule/src/TcpServerHandle.h | 1 + utils/TcpModule/src/TcpServerImpl.cpp | 86 ++++++++++++++++++++++++++- utils/TcpModule/src/TcpServerImpl.h | 26 +++++--- 9 files changed, 135 insertions(+), 35 deletions(-) diff --git a/utils/TcpModule/include/TcpModule.h b/utils/TcpModule/include/TcpModule.h index 4b8a2b5b..dd772b3c 100644 --- a/utils/TcpModule/include/TcpModule.h +++ b/utils/TcpModule/include/TcpModule.h @@ -25,15 +25,15 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *); typedef void (*SocketClosedFunction)(const void *); typedef struct client_accept_parm { - const TcpReadFunction mReadFunc; - const SocketClosedFunction mClosedFunc; + const TcpReadFunction mReadFunc; /// This function is defined in the test file. + const SocketClosedFunction mClosedFunc; /// This function is defined in the test file. } ClientAcceptParam; typedef struct tcp_server_parm { - const char *mIp; /**Server ip */ - const int mPort; /**Server port */ - const TcpAcceptClientFunction mAcceptClientFunc; - const ClientAcceptParam mClientAcceptParam; + const char *mIp; /// Server ip + const int mPort; /// Server port + const TcpAcceptClientFunction mAcceptClientFunc; /// This function is defined in the test file. + const ClientAcceptParam mClientAcceptParam; /// This function is defined in the test file. } TcpServerParam; typedef struct tcp_parm { diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md index 389b72da..ad41416b 100644 --- a/utils/TcpModule/readme.md +++ b/utils/TcpModule/readme.md @@ -1,5 +1,6 @@ # TCP模块 这个项目实现了一个用于网络通信的TCP模块,提供了服务器和客户端功能。 +在网络编程中,套接字(socket)是一个用于网络通信的端点。在客户端和服务器模型中,客户端会创建一个套接字来与服务器建立连接,而服务器则监听来自客户端的连接请求。一旦连接建立,客户端和服务器就可以通过各自的套接字进行数据的发送和接收。 # 涉及的知识 * 网络通信 diff --git a/utils/TcpModule/src/ITcpClient.h b/utils/TcpModule/src/ITcpClient.h index 43573c25..35bf82c9 100644 --- a/utils/TcpModule/src/ITcpClient.h +++ b/utils/TcpModule/src/ITcpClient.h @@ -34,7 +34,7 @@ public: typedef struct i_tcp_client_header { const char *mCheckName; -} ITcpClientHeader; +} ITcpClientHeader; typedef struct tcp_client { ITcpClientHeader mHeader; diff --git a/utils/TcpModule/src/TcpClientImpl.cpp b/utils/TcpModule/src/TcpClientImpl.cpp index 0ef5d44b..8b420736 100644 --- a/utils/TcpModule/src/TcpClientImpl.cpp +++ b/utils/TcpModule/src/TcpClientImpl.cpp @@ -25,7 +25,7 @@ #include /** * @brief Called when data is received on a TCP connection. - * + * * @param io Socket handle * @param buf The transmitted data content * @param len Byte length of transmitted data @@ -60,12 +60,14 @@ TcpClientImpl::TcpClientImpl(const TcpClientParam ¶m, const void *object) : { } /** - * @brief Initialize TCP clients, create event loops, I/O objects, and set callback functions for connection and closure. + * @brief Initialize TCP clients, create event loops, I/O objects, and set callback functions for connection and + * closure. */ void TcpClientImpl::Init(void) { constexpr int NO_FALGS = 0; - mLoop = hloop_new(NO_FALGS); ///Initialize event loop + /// Initialize event loop + mLoop = hloop_new(NO_FALGS); if (nullptr == mLoop) { LogError("TcpClientImpl::Init hloop_new failed.\n"); return; @@ -76,9 +78,11 @@ void TcpClientImpl::Init(void) return; } hevent_set_userdata(io, this); - hio_setcb_connect(io, on_connect);///Set the callback function of successful connection, and there is no connection operation. + /// Set the callback function of successful connection, and there is no connection operation. + hio_setcb_connect(io, on_connect); hio_setcb_close(io, on_close); - hio_connect(io); ///Connection operation + /// Connection operation + hio_connect(io); mIo = io; std::shared_ptr impl = std::dynamic_pointer_cast(shared_from_this()); auto recvThread = [](std::shared_ptr tcpClient) { @@ -115,7 +119,8 @@ ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght) LogError("mIo is nullptr.\n"); return TCP_MODULE_WRITE_ERROR; } - return hio_write(mIo, buf, bufLenght); ///Returns the byte length of a packet.If it fails, an error code is returned. + /// Returns the byte length of a packet.If it fails, an error code is returned. + return hio_write(mIo, buf, bufLenght); } void TcpClientImpl::Closed(void) { @@ -130,8 +135,8 @@ void TcpClientImpl::Closed(void) } } /** - * @brief Run event loop - * + * @brief Run an event loop and release resources after completion or error. + * */ void TcpClientImpl::Loop(void) { @@ -155,11 +160,12 @@ std::shared_ptr *NewTcpClient(const TcpClientParam ¶m) memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient)); impl->mHeader.mCheckName = GetTcpClientModuleName(); /** - * @brief ObjectThis points to the first address of the impl and offsets the address by ITcpClientHeader bytes, + * @brief ObjectThis points to the first address of the impl and offsets the address by ITcpClientHeader bytes, * that is, skips the mHeader part of the impl. */ std::shared_ptr *objectThis = (std::shared_ptr *)(((char *)impl) + sizeof(ITcpClientHeader)); impl->mTcpClient = std::make_shared(param, objectThis); - return objectThis; ///ObjectThis is used to verify whether the client is legal. + /// ObjectThis is used to verify whether the client is legal. + return objectThis; } \ No newline at end of file diff --git a/utils/TcpModule/src/TcpClientImpl.h b/utils/TcpModule/src/TcpClientImpl.h index 3bb3262f..49f60eb2 100644 --- a/utils/TcpModule/src/TcpClientImpl.h +++ b/utils/TcpModule/src/TcpClientImpl.h @@ -35,12 +35,12 @@ public: void Loop(void); private: - std::mutex mMutex; ///A mutex lock used to synchronize access to shared resources. - hloop_t *mLoop; ///Event loop, listening for io objects - hio_t *mIo; ///Socket handle - const TcpClientParam mParam; ///Basic information of the client, including port, ip, reading and closing. + std::mutex mMutex; /// A mutex lock used to synchronize access to shared resources. + hloop_t *mLoop; /// Event loop, listening for io objects + hio_t *mIo; /// Socket handle + const TcpClientParam mParam; /// Basic information of the client, including port, ip, reading and closing. std::thread mTcpClientThread; - const void *mObjectThis; ///ObjectThis is used to verify whether the client is legal. + const void *mObjectThis; /// ObjectThis is used to verify whether the client is legal. }; std::shared_ptr *NewTcpClient(const TcpClientParam ¶m); #endif \ No newline at end of file diff --git a/utils/TcpModule/src/TcpModule.cpp b/utils/TcpModule/src/TcpModule.cpp index 5ebc8a7a..877c0ddf 100644 --- a/utils/TcpModule/src/TcpModule.cpp +++ b/utils/TcpModule/src/TcpModule.cpp @@ -23,7 +23,7 @@ #include /** * @brief Verify that object is a legitimate (existing) server. - * + * * @param object Save the address of the character pointer variable. * Should be substituted into the private member objectThis of the client instance. * If the value of the character pointer pointed by *object is "tcp_server", return turn @@ -44,9 +44,9 @@ static bool TcpServerObjectCheck(void *object) } /** * @brief Verify that object is a legitimate (existing) client. - * + * * @param object Save the address of the character pointer variable. - * + * * @return true Indicates that the client exists. * @return false Indicates that the client does not exist */ diff --git a/utils/TcpModule/src/TcpServerHandle.h b/utils/TcpModule/src/TcpServerHandle.h index 8413bec6..646b2680 100644 --- a/utils/TcpModule/src/TcpServerHandle.h +++ b/utils/TcpModule/src/TcpServerHandle.h @@ -27,6 +27,7 @@ public: bool GetServer(const int &fd, std::shared_ptr &server); private: + /// Store multiple servers std::map> mFd; }; #endif \ No newline at end of file diff --git a/utils/TcpModule/src/TcpServerImpl.cpp b/utils/TcpModule/src/TcpServerImpl.cpp index 6d646670..61147c62 100644 --- a/utils/TcpModule/src/TcpServerImpl.cpp +++ b/utils/TcpModule/src/TcpServerImpl.cpp @@ -24,18 +24,35 @@ #include #include #include +/** + * @brief Disconnect the connected client. + * + * @param io Socket + */ static void on_close(hio_t *io) { LogInfo("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io)); TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io); server->RemoveClient(io); } +/** + * @brief Close the port on which the server listens for client connections. + * + * @param io The port on which the server listens for client connections. + */ static void server_on_close(hio_t *io) { LogInfo("server_on_close fd=%d error=%d\n", hio_fd(io), hio_error(io)); TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io); server->Closed(); } +/** + * @brief The server processes data + * + * @param io Socket + * @param buf data + * @param readbytes bytes + */ static void on_recv(hio_t *io, void *buf, int readbytes) { LogInfo("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes); @@ -48,6 +65,11 @@ static void on_recv(hio_t *io, void *buf, int readbytes) std::shared_ptr *client = server->GetClient(io); (*client)->Readed((const char *)buf, readbytes); } +/** + * @brief Handles a new accept client connection event + * + * @param io Socket for newly accepted client connection + */ static void on_accept(hio_t *io) { LogInfo("on_accept connfd=%d\n", hio_fd(io)); @@ -63,12 +85,17 @@ static void on_accept(hio_t *io) // std::shared_ptr *client = NewTcpClientAccept(io); TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io); server->AddClient(io); + /// Set up a read event hio_read_start(io); } TcpClientAcceptImpl::TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m) : mIo(io), mObjectThis(object), mParam(param) { } +/** + * @brief Disconnect the client + * + */ void TcpClientAcceptImpl::Close(void) { if (nullptr != mIo) { @@ -76,14 +103,28 @@ void TcpClientAcceptImpl::Close(void) hio_close(io); } } +/** + * @brief The server reads the data received by tcp connection. + * + * @param data + * @param length bytes + */ void TcpClientAcceptImpl::Readed(const void *data, size_t length) { if (nullptr != mParam.mReadFunc) { mParam.mReadFunc(data, length, mObjectThis); return; } - LogWarning("mParam.mClosedFunc is null\n"); + LogWarning("mParam.mReadedFunc is null\n"); } +/** + * @brief The server writes data to the tcp connection. + * + * @param data Written data + * @param length Byte length of written data + * @return ssize_t + * Returns the number of bytes written if the writing is successful, and returns the error code -1 if it fails. + */ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length) { if (mIo) { @@ -109,11 +150,15 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param) void TcpServerImpl::Init(void) { constexpr int NO_FALGS = 0; - mLoop = hloop_new(NO_FALGS); ///Initialize event loop + /// Initialize event loop + /// mLoop is used to manage all I/O objects and the events that occur on them. + mLoop = hloop_new(NO_FALGS); if (nullptr == mLoop) { LogError("hloop_new failed\n"); return; } + /// Create a listening event to listen whether a new client sends a connection request. + /// Listenio is part of mLoop. hio_t *listenio = hloop_create_tcp_server(mLoop, mParam.mIp, mParam.mPort, on_accept); if (nullptr == listenio) { LogError("hloop_create_tcp_server failed\n"); @@ -132,11 +177,17 @@ void TcpServerImpl::Init(void) }; mTcpServerThread = std::thread(recvThread, impl); } +/** + * @brief De-initialize the TCP server, close the I/O object and wait for the receiving thread to end. + * + */ void TcpServerImpl::UnInit(void) { LogInfo("UnInit TcpServerImpl\n"); + /// Close all client connections FreeClients(); if (nullptr != mIo) { + /// Turn off connection monitoring hio_close(mIo); mIo = nullptr; } @@ -144,6 +195,10 @@ void TcpServerImpl::UnInit(void) mTcpServerThread.join(); } } +/** + * @brief Run an event loop and release resources after completion or error. + * + */ void TcpServerImpl::Loop(void) { if (nullptr == mLoop) { @@ -154,6 +209,11 @@ void TcpServerImpl::Loop(void) hloop_free(&mLoop); mLoop = nullptr; } +/** + * @brief + * + * @param io Add the socket of the newly connected client. + */ void TcpServerImpl::AddClient(hio_t *io) { mMutex.lock(); @@ -165,6 +225,9 @@ void TcpServerImpl::AddClient(hio_t *io) SOCKADDR_STR(hio_peeraddr(io), peeraddrstr)); std::shared_ptr *addClient = NewTcpClientAccept(io, mParam.mClientAcceptParam); mClients[hio_fd(io)] = addClient; + /** + * @brief Check whether the server side accepts the connection of the client side. + */ if (mParam.mAcceptClientFunc) { if (mParam.mAcceptClientFunc(addClient, peeraddrstr) == true) { mMutex.unlock(); @@ -176,6 +239,12 @@ void TcpServerImpl::AddClient(hio_t *io) hio_close(io); LogWarning("AddClient failed.\n"); } +/** + * @brief + * + * @param io socket + * @return std::shared_ptr* + */ std::shared_ptr *TcpServerImpl::GetClient(hio_t *io) { std::lock_guard locker(mMutex); @@ -186,6 +255,11 @@ std::shared_ptr *TcpServerImpl::GetClient(hio_t *io) LogError("GetClient failed, client not exit.\n"); return nullptr; } +/** + * @brief Remove the data element in the map, that is, the connected client. + * + * @param io Socket + */ void TcpServerImpl::RemoveClient(hio_t *io) { std::lock_guard locker(mMutex); @@ -203,6 +277,10 @@ void TcpServerImpl::RemoveClient(hio_t *io) LogError("RemoveClient failed, client not exit.\n"); } } +/** + * @brief Clear all connected clients. + * + */ void TcpServerImpl::FreeClients(void) { std::lock_guard locker(mMutex); @@ -218,6 +296,10 @@ void TcpServerImpl::FreeClients(void) } mClients.clear(); } +/** + * @brief Stop listening to the event loop + * + */ void TcpServerImpl::Closed(void) { if (nullptr != mLoop) { diff --git a/utils/TcpModule/src/TcpServerImpl.h b/utils/TcpModule/src/TcpServerImpl.h index 44dd08a4..e4c19de5 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -22,6 +22,11 @@ #include #include #include +/** + * @brief The server manages the connection life cycle and data interaction of each connected client through this kind + * of instance. + * + */ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this { public: @@ -33,10 +38,15 @@ public: void Closed(void) override; private: - const hio_t *mIo; + const hio_t *mIo; /// Connected client socket const void *mObjectThis; const ClientAcceptParam mParam; }; +/** + * @brief The TcpServerImpl class is responsible for listening to connection requests from clients and creating an + * instance of TcpClientAcceptImpl for each accepted connection. + * + */ class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this { public: @@ -44,17 +54,17 @@ public: virtual ~TcpServerImpl() = default; void Init(void) override; void UnInit(void) override; - void Loop(void); + void Loop(void); /// Run an event loop and release resources after completion or error. void AddClient(hio_t *io); std::shared_ptr *GetClient(hio_t *io); - void RemoveClient(hio_t *io); - void FreeClients(void); - void Closed(void); + void RemoveClient(hio_t *io); /// Remove the data element in the map, that is, the connected client. + void FreeClients(void); /// Clear all connected clients. + void Closed(void); /// Stop listening to the event loop(mLoop) private: - std::mutex mMutex; ///A mutex lock used to synchronize access to shared resources. - hloop_t *mLoop; ///Event loop, listening for io objects - hio_t *mIo; ///Socket handle + std::mutex mMutex; /// A mutex lock used to synchronize access to shared resources. + hloop_t *mLoop; /// Event loop, listening for all io objects + hio_t *mIo; /// listen whether a new client sends a connection request. const TcpServerParam mParam; std::thread mTcpServerThread; std::map *> mClients; From 1a85c104d11e9f9c551fe2f9dcab51cd2bf4b480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80?= <3213487792@qq.com> Date: Sat, 3 Aug 2024 00:47:16 +0000 Subject: [PATCH 38/48] add some note MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀 <3213487792@qq.com> --- utils/ConfigBase/ConfigBase.h | 226 +++++++++++++++++++++++++++++ utils/ConfigBase/ConfigBaseCode.h | 53 +++++++ utils/ConfigBase/ConfigBaseImpl.h | 229 ++++++++++++++++++++++++++++++ utils/ConfigBase/IConfigBase.h | 105 ++++++++++++++ 4 files changed, 613 insertions(+) create mode 100644 utils/ConfigBase/ConfigBase.h create mode 100644 utils/ConfigBase/ConfigBaseCode.h create mode 100644 utils/ConfigBase/ConfigBaseImpl.h create mode 100644 utils/ConfigBase/IConfigBase.h diff --git a/utils/ConfigBase/ConfigBase.h b/utils/ConfigBase/ConfigBase.h new file mode 100644 index 00000000..60c60e8f --- /dev/null +++ b/utils/ConfigBase/ConfigBase.h @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/*! + * This file contains the base configuration interface for managing configuration files. + * It provides functions to open, read, write, and close configuration files, + * as well as to get and set various data types stored within these files. + */ +/** + * @file ConfigBase.h + * @brief Configuration management definitions and interfaces. + * + * This header file provides the necessary declarations for configuration management. + * It includes the definition of the configuration codes, as well as the function + * prototypes for interacting with the configuration system. + */ +#ifndef CONFIG_BASE_H +#define CONFIG_BASE_H +#include "StatusCode.h" +#ifdef __cplusplus +extern "C" { +#endif +/** + * @brief Enumeration for configuration codes. + * + * Defines specific codes for configuration-related status messages. + */ +enum CONFIG_CODE +{ + CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, + /**< Parameter does not exist in the configuration. */ + CONFIG_CODE_END + /**< End of configuration codes. */ +}; + +// StatusCode ConfigInit(void); +// StatusCode ConfigUnInit(void); +/** + * @brief Opens a configuration file. + * @param fileName The name of the configuration file to open. + * @return A pointer to the opened configuration file object. + */ +void *OpenConfigFile(const char *fileName); +/** + * @brief Saves the current configuration to a file. + * @param object The configuration object to save. + * @return A StatusCode indicating the result of the operation. + */ +StatusCode ConfigSaveFile(void *object); +/** + * @brief Closes a configuration file. + * @param object The configuration file object to close. + */ +void CloseConfigFile(void *object); +/** + * @brief Retrieves an integer value from the configuration. + * @param object The configuration object. + * @param name The name of the configuration parameter. + * @param value A pointer to store the retrieved integer value. + * @return A StatusCode indicating the result of the operation. + */ +StatusCode ConfigGetInt(void *object, const char *name, int *value); +/** + * @brief Sets an integer value in the configuration. + * @param object The configuration object. + * @param name The name of the configuration parameter. + * @param value The integer value to set. + * @return A StatusCode indicating the result of the operation. + */ +StatusCode ConfigSetInt(void *object, const char *name, const int value); +/** + * @brief Retrieves a short configuration value. + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to retrieve. + * @param value A pointer to store the retrieved short value. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigGetShort(void *object, const char *name, short *value); +/** + * @brief Sets a short configuration value. + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The short value to set for the configuration parameter. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetShort(void *object, const char *name, const short value); +/** + * @brief Retrieves a long integer value from the configuration. + * @param object Pointer to the configuration object. + * @param name The name of the configuration parameter to read. + * @param value Pointer to store the retrieved long integer value. + * @return StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigGetLong(void *object, const char *name, long *value); +/** + * @brief Sets a long integer value in the configuration. + * @param object Pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The long integer value to set. + * @return StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetLong(void *object, const char *name, const long value); +/** + * @brief Retrieves a long long integer value from the configuration. + * @param object Pointer to the configuration object. + * @param name The name of the configuration parameter to read. + * @param value Pointer to store the retrieved long long integer value. + * @return StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigGetLLong(void *object, const char *name, long long *value); +/** + * @brief Sets a long long integer value in the configuration. + * @param object Pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The long long integer value to set. + * @return StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetLLong(void *object, const char *name, const long long value); +/** + * @brief Retrieves a string value from the configuration. + * @param object Pointer to the configuration object. + * @param name The name of the configuration parameter to read. + * @param value Pointer to store the retrieved string value. + * @return StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigGetChar(void *object, const char *name, char *value); +/** + * @brief Sets a string value in the configuration. + * @param object Pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The string value to set. + * @return StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetChar(void *object, const char *name, const char value); +/** + * @brief Retrieves a boolean configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to read. + * @param value A pointer to store the retrieved boolean value. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigGetBool(void *object, const char *name, bool *value); +/** + * @brief Sets a boolean configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The boolean value to set. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetBool(void *object, const char *name, const bool value); +/** + * @brief Retrieves a floating-point configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to read. + * @param value A pointer to store the retrieved floating-point value. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigGetFloat(void *object, const char *name, float *value); +/** + * @brief Sets a floating-point configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The floating-point value to set. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetFloat(void *object, const char *name, const float value); +/** + * @brief Retrieves a double-precision floating-point configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to read. + * @param value A pointer to store the retrieved double-precision floating-point value. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigGetDouble(void *object, const char *name, double *value); + +/** + * @brief Sets a double-precision floating-point configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The double-precision floating-point value to set. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetDouble(void *object, const char *name, const double value); +/** + * @brief Retrieves a string configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to read. + * @param value A reference to a pointer where the string value will be stored. + * @return A StatusCode indicating the success or failure of the operation. + * If the operation is successful, the pointed-to pointer will hold + * the address of the string value. The caller should not attempt to + * free this memory if the string is managed internally by the + * configuration system. + */ +StatusCode ConfigGetString(void *object, const char *name, const char **value); +/** + * @brief Sets a string configuration value. + * + * @param object A pointer to the configuration object. + * @param name The name of the configuration parameter to set. + * @param value The string value to set for the configuration parameter. + * @return A StatusCode indicating the success or failure of the operation. + */ +StatusCode ConfigSetString(void *object, const char *name, const char *value); +#ifdef __cplusplus +} +#endif +#endif diff --git a/utils/ConfigBase/ConfigBaseCode.h b/utils/ConfigBase/ConfigBaseCode.h new file mode 100644 index 00000000..2eb84b2b --- /dev/null +++ b/utils/ConfigBase/ConfigBaseCode.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file ConfigBaseCode.h + * @brief Contains internal definitions and declarations for configuration codes. + * + * This header file is intended for internal use only and should not be included + * directly by other files. It defines the StatusCode creation function for configuration + * related codes. + */ +#ifndef CONFIG_BASE_CODE_H +#define CONFIG_BASE_CODE_H +#include "ConfigBase.h" +#include "StatusCode.h" +#ifdef __cplusplus +extern "C" { +#endif +/** + * @brief Error directive indicating that this file is for internal use only. + * + * This directive is used to prevent accidental inclusion of the internal header file + * by external code. If this file is included without the CONFIG_OWNER macro being defined, + * it will cause a compile-time error. + */ +#ifndef CONFIG_OWNER + #error This is internal file, never include it. +#endif +/** + * @brief Creates a StatusCode for configuration codes. + * + * This function is used internally to create a StatusCode that represents a configuration + * related code. It is not part of the public API and should not be used by external code. + * + * @param code The long integer value to be encapsulated in a StatusCode. + * @return A StatusCode object representing the configuration code. + */ +const StatusCode CreateConfigCode(const long int code); +#ifdef __cplusplus +} +#endif +#endif diff --git a/utils/ConfigBase/ConfigBaseImpl.h b/utils/ConfigBase/ConfigBaseImpl.h new file mode 100644 index 00000000..9e31b52c --- /dev/null +++ b/utils/ConfigBase/ConfigBaseImpl.h @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file ConfigBaseImpl.h + * @brief Provides an implementation of the IConfigBase interface using libconfig. + * + * This header file declares the ConfigBaseImpl class which implements the configuration + * interface defined by IConfigBase. It uses the libconfig library to handle the + * configuration file parsing and saving. + */ +#ifndef CONFIG_BASE_IMPL_H +#define CONFIG_BASE_IMPL_H +#include "IConfigBase.h" +#include +#include +/** + * @class ConfigBaseImpl + * @brief Concrete implementation of the IConfigBase interface. + * + * ConfigBaseImpl is a class that provides an implementation of the IConfigBase interface. + * It uses the libconfig library to manage configuration files and settings. + */ +class ConfigBaseImpl : public IConfigBase +{ +public: + ConfigBaseImpl(const std::string &fileName); + virtual ~ConfigBaseImpl() = default; + /** + * @brief Opens the configuration file for reading and writing. + * + * @return true if the file was successfully opened, false otherwise. + */ + bool OpenConfigFile(void) override; + /** + * @brief Closes the configuration file. + */ + void CloseConfigFile(void) override; + /** + * @brief Saves the current configuration to the file. + * + * This function writes the current in-memory configuration settings to the file + * specified during the construction of the ConfigBaseImpl object. It uses the libconfig + * library to perform the save operation. + * + * @return A StatusCode indicating the success or failure of the operation. + */ + StatusCode ConfigSaveFile(void) override; + /** + * @brief Retrieves an integer configuration value. + * + * This function gets the integer value associated with the specified configuration + * parameter name. + * + * @param name The name of the configuration parameter to retrieve. + * @param value A pointer to store the retrieved integer value. + * @return A StatusCode indicating the success or failure of the operation. + */ + StatusCode ConfigGetInt(const char *name, int *value) override; + /** + * @brief Sets an integer configuration value. + * + * This function sets the integer value for the specified configuration parameter name. + * + * @param name The name of the configuration parameter to set. + * @param value The integer value to set. + * @return A StatusCode indicating the success or failure of the operation. + */ + StatusCode ConfigSetInt(const char *name, const int value) override; + /** + * @brief Retrieves a long long integer configuration value. + * + * @param name The name of the configuration parameter to retrieve. + * @param value A pointer to store the retrieved long long integer value. + * @return A StatusCode for the operation. + */ + StatusCode ConfigGetShort(const char *name, short *value) override; + /** + * @brief Sets a long long integer configuration value. + * + * @param name The name of the configuration parameter to set. + * @param value The long long integer value to set. + * @return A StatusCode for the operation. + */ + StatusCode ConfigSetShort(const char *name, const short value) override; + /** + * @brief Retrieves a long integer configuration value. + * + * @param name The name of the configuration setting to retrieve. + * @param value A pointer to a long integer where the retrieved value will be stored. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigGetLong(const char *name, long *value) override; + /** + * @brief Sets a long integer configuration value. + * + * @param name The name of the configuration setting to set. + * @param value The long integer value to set for the configuration setting. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigSetLong(const char *name, const long value) override; + /** + * @brief Retrieves a long long integer configuration value. + * + * @param name The name of the configuration parameter to retrieve. + * @param value A pointer to store the retrieved long long integer value. + * @return A StatusCode for the operation. + */ + StatusCode ConfigGetLLong(const char *name, long long *value) override; + /** + * @brief Sets a long long integer configuration value. + * + * @param name The name of the configuration parameter to set. + * @param value The long long integer value to set. + * @return A StatusCode for the operation. + */ + StatusCode ConfigSetLLong(const char *name, const long long value) override; + /** + * @brief Retrieves a character configuration value. + * + * @param name The name of the configuration parameter to retrieve. + * @param value A pointer to store the retrieved character value. + * @return A StatusCode for the operation. + */ + StatusCode ConfigGetChar(const char *name, char *value) override; + /** + * @brief Sets a character configuration value. + * + * @param name The name of the configuration parameter to set. + * @param value The character value to set. + * @return A StatusCode for the operation. + */ + StatusCode ConfigSetChar(const char *name, const char value) override; + /** + * @brief Retrieves a boolean configuration value. + * + * @param name The name of the configuration setting to retrieve. + * @param value A pointer to a bool where the retrieved value will be stored. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigGetBool(const char *name, bool *value) override; + /** + * @brief Sets a boolean configuration value. + * + * @param name The name of the configuration setting to set. + * @param value The boolean value to set for the configuration setting. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigSetBool(const char *name, const bool value) override; + /** + * @brief Retrieves a floating-point configuration value. + * + * @param name The name of the configuration setting to retrieve. + * @param value A pointer to a float where the retrieved value will be stored. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigGetFloat(const char *name, float *value) override; + /** + * @brief Sets a floating-point configuration value. + * + * @param name The name of the configuration setting to set. + * @param value The float value to set for the configuration setting. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigSetFloat(const char *name, const float value) override; + + /** + * @brief Retrieves a double-precision floating-point configuration value. + * + * @param name The name of the configuration setting to retrieve. + * @param value A pointer to a double where the retrieved value will be stored. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigGetDouble(const char *name, double *value) override; + /** + * @brief Sets a double-precision floating-point configuration value. + * + * @param name The name of the configuration setting to set. + * @param value The double value to set for the configuration setting. + * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. + */ + StatusCode ConfigSetDouble(const char *name, const double value) override; + /** + * @brief Retrieves a string configuration value. + * + * This function gets the string value associated with the specified configuration + * parameter name. The returned string is managed by the libconfig library and should + * not be freed by the caller. + * + * @param name The name of the configuration parameter to retrieve. + * @param value A pointer to store the retrieved string value. + * @return A StatusCode indicating the success or failure of the operation. + */ + StatusCode ConfigGetString(const char *name, const char **value) override; + /** + * @brief Retrieves a string configuration value. + * + * This function gets the string value associated with the specified configuration + * parameter name. The returned string is managed by the libconfig library and should + * not be freed by the caller. + * + * @param name The name of the configuration parameter to retrieve. + * @param value A pointer to store the retrieved string value. + * @return A StatusCode indicating the success or failure of the operation. + */ + StatusCode ConfigSetString(const char *name, const char *value) override; + +private: + /** + * @brief The file name of the configuration file managed by this instance. + */ + const std::string mFileName; + /** + * @brief The libconfig structure used to hold the configuration settings. + */ + config_t mCfg; +}; +#endif diff --git a/utils/ConfigBase/IConfigBase.h b/utils/ConfigBase/IConfigBase.h new file mode 100644 index 00000000..8e782e23 --- /dev/null +++ b/utils/ConfigBase/IConfigBase.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2023 Fancy Code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file IConfigBase.h + * @brief Defines the interface for configuration base operations. + * + * This header file declares the IConfigBase interface and associated types for + * managing configuration settings. It provides a common set of functions for + * interacting with configuration data, which can be implemented by various + * configuration backends. + */ +#ifndef I_CONFIG_BASE_H +#define I_CONFIG_BASE_H +#include "StatusCode.h" +#include +/** + * @class IConfigBase + * @brief Interface for configuration management. + * + * IConfigBase is an abstract interface that defines the contract for configuration + * operations. Concrete implementations of this interface will provide the + * functionality for opening, modifying, and saving configuration settings. + */ +class IConfigBase +{ +public: + IConfigBase() = default; + virtual ~IConfigBase() = default; + virtual bool OpenConfigFile(void); + virtual void CloseConfigFile(void); + virtual StatusCode ConfigSaveFile(void); + virtual StatusCode ConfigGetInt(const char *name, int *value); + virtual StatusCode ConfigSetInt(const char *name, const int value); + virtual StatusCode ConfigGetShort(const char *name, short *value); + virtual StatusCode ConfigSetShort(const char *name, const short value); + virtual StatusCode ConfigGetLong(const char *name, long *value); + virtual StatusCode ConfigSetLong(const char *name, const long value); + virtual StatusCode ConfigGetLLong(const char *name, long long *value); + virtual StatusCode ConfigSetLLong(const char *name, const long long value); + virtual StatusCode ConfigGetChar(const char *name, char *value); + virtual StatusCode ConfigSetChar(const char *name, const char value); + virtual StatusCode ConfigGetBool(const char *name, bool *value); + virtual StatusCode ConfigSetBool(const char *name, const bool value); + virtual StatusCode ConfigGetFloat(const char *name, float *value); + virtual StatusCode ConfigSetFloat(const char *name, const float value); + virtual StatusCode ConfigGetDouble(const char *name, double *value); + virtual StatusCode ConfigSetDouble(const char *name, const double value); + virtual StatusCode ConfigGetString(const char *name, const char **value); + virtual StatusCode ConfigSetString(const char *name, const char *value); +}; +/** + * @struct IConfigBaseHeader + * @brief A structure that may be used for validation or recognition purposes. + * + * This structure is meant to contain a pointer to a character which could be + * utilized for some form of validation or recognition check. + */ +typedef struct i_config_base_header // Defined a struct i_config.base_header containing a pointer mCheckName to a + // character, which may be used for some form of validation or recognition. +{ + const char *mCheckName; +} IConfigBaseHeader; +/** + * @struct ConfigBase + * @brief A structure that combines the IConfigBaseHeader with a shared pointer to IConfigBase. + * + * This structure is intended to provide additional context or metadata along with + * the actual configuration base object. + */ +typedef struct config_base +{ + IConfigBaseHeader mHeader; + std::shared_ptr mIConfigBase; +} ConfigBase; +/** + * @brief Gets the module name of the configuration base. + * + * This function returns a constant character pointer to the module name as a string. + * + * @return A constant character pointer to the module name. + */ +const char *GetConfigBaseModuleName(void); +/** + * @brief Creates a new instance of the IConfigBase implementation. + * + * This function constructs a new ConfigBase structure with a shared pointer to an + * IConfigBase object, initialized with the provided file name. + * + * @param fileName The name of the configuration file to associate with the new object. + * @return A pointer to a new ConfigBase structure. + */ +std::shared_ptr *NewConfigBase(const char *fileName); +#endif From 9ec8699eedf7e13f3ad949fdd0690c19eb365476 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sat, 3 Aug 2024 01:08:08 +0000 Subject: [PATCH 39/48] Revert "add some note" This reverts commit 1a85c104d11e9f9c551fe2f9dcab51cd2bf4b480. --- utils/ConfigBase/ConfigBase.h | 226 ----------------------------- utils/ConfigBase/ConfigBaseCode.h | 53 ------- utils/ConfigBase/ConfigBaseImpl.h | 229 ------------------------------ utils/ConfigBase/IConfigBase.h | 105 -------------- 4 files changed, 613 deletions(-) delete mode 100644 utils/ConfigBase/ConfigBase.h delete mode 100644 utils/ConfigBase/ConfigBaseCode.h delete mode 100644 utils/ConfigBase/ConfigBaseImpl.h delete mode 100644 utils/ConfigBase/IConfigBase.h diff --git a/utils/ConfigBase/ConfigBase.h b/utils/ConfigBase/ConfigBase.h deleted file mode 100644 index 60c60e8f..00000000 --- a/utils/ConfigBase/ConfigBase.h +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/*! - * This file contains the base configuration interface for managing configuration files. - * It provides functions to open, read, write, and close configuration files, - * as well as to get and set various data types stored within these files. - */ -/** - * @file ConfigBase.h - * @brief Configuration management definitions and interfaces. - * - * This header file provides the necessary declarations for configuration management. - * It includes the definition of the configuration codes, as well as the function - * prototypes for interacting with the configuration system. - */ -#ifndef CONFIG_BASE_H -#define CONFIG_BASE_H -#include "StatusCode.h" -#ifdef __cplusplus -extern "C" { -#endif -/** - * @brief Enumeration for configuration codes. - * - * Defines specific codes for configuration-related status messages. - */ -enum CONFIG_CODE -{ - CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, - /**< Parameter does not exist in the configuration. */ - CONFIG_CODE_END - /**< End of configuration codes. */ -}; - -// StatusCode ConfigInit(void); -// StatusCode ConfigUnInit(void); -/** - * @brief Opens a configuration file. - * @param fileName The name of the configuration file to open. - * @return A pointer to the opened configuration file object. - */ -void *OpenConfigFile(const char *fileName); -/** - * @brief Saves the current configuration to a file. - * @param object The configuration object to save. - * @return A StatusCode indicating the result of the operation. - */ -StatusCode ConfigSaveFile(void *object); -/** - * @brief Closes a configuration file. - * @param object The configuration file object to close. - */ -void CloseConfigFile(void *object); -/** - * @brief Retrieves an integer value from the configuration. - * @param object The configuration object. - * @param name The name of the configuration parameter. - * @param value A pointer to store the retrieved integer value. - * @return A StatusCode indicating the result of the operation. - */ -StatusCode ConfigGetInt(void *object, const char *name, int *value); -/** - * @brief Sets an integer value in the configuration. - * @param object The configuration object. - * @param name The name of the configuration parameter. - * @param value The integer value to set. - * @return A StatusCode indicating the result of the operation. - */ -StatusCode ConfigSetInt(void *object, const char *name, const int value); -/** - * @brief Retrieves a short configuration value. - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to retrieve. - * @param value A pointer to store the retrieved short value. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigGetShort(void *object, const char *name, short *value); -/** - * @brief Sets a short configuration value. - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The short value to set for the configuration parameter. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetShort(void *object, const char *name, const short value); -/** - * @brief Retrieves a long integer value from the configuration. - * @param object Pointer to the configuration object. - * @param name The name of the configuration parameter to read. - * @param value Pointer to store the retrieved long integer value. - * @return StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigGetLong(void *object, const char *name, long *value); -/** - * @brief Sets a long integer value in the configuration. - * @param object Pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The long integer value to set. - * @return StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetLong(void *object, const char *name, const long value); -/** - * @brief Retrieves a long long integer value from the configuration. - * @param object Pointer to the configuration object. - * @param name The name of the configuration parameter to read. - * @param value Pointer to store the retrieved long long integer value. - * @return StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigGetLLong(void *object, const char *name, long long *value); -/** - * @brief Sets a long long integer value in the configuration. - * @param object Pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The long long integer value to set. - * @return StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetLLong(void *object, const char *name, const long long value); -/** - * @brief Retrieves a string value from the configuration. - * @param object Pointer to the configuration object. - * @param name The name of the configuration parameter to read. - * @param value Pointer to store the retrieved string value. - * @return StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigGetChar(void *object, const char *name, char *value); -/** - * @brief Sets a string value in the configuration. - * @param object Pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The string value to set. - * @return StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetChar(void *object, const char *name, const char value); -/** - * @brief Retrieves a boolean configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to read. - * @param value A pointer to store the retrieved boolean value. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigGetBool(void *object, const char *name, bool *value); -/** - * @brief Sets a boolean configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The boolean value to set. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetBool(void *object, const char *name, const bool value); -/** - * @brief Retrieves a floating-point configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to read. - * @param value A pointer to store the retrieved floating-point value. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigGetFloat(void *object, const char *name, float *value); -/** - * @brief Sets a floating-point configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The floating-point value to set. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetFloat(void *object, const char *name, const float value); -/** - * @brief Retrieves a double-precision floating-point configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to read. - * @param value A pointer to store the retrieved double-precision floating-point value. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigGetDouble(void *object, const char *name, double *value); - -/** - * @brief Sets a double-precision floating-point configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The double-precision floating-point value to set. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetDouble(void *object, const char *name, const double value); -/** - * @brief Retrieves a string configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to read. - * @param value A reference to a pointer where the string value will be stored. - * @return A StatusCode indicating the success or failure of the operation. - * If the operation is successful, the pointed-to pointer will hold - * the address of the string value. The caller should not attempt to - * free this memory if the string is managed internally by the - * configuration system. - */ -StatusCode ConfigGetString(void *object, const char *name, const char **value); -/** - * @brief Sets a string configuration value. - * - * @param object A pointer to the configuration object. - * @param name The name of the configuration parameter to set. - * @param value The string value to set for the configuration parameter. - * @return A StatusCode indicating the success or failure of the operation. - */ -StatusCode ConfigSetString(void *object, const char *name, const char *value); -#ifdef __cplusplus -} -#endif -#endif diff --git a/utils/ConfigBase/ConfigBaseCode.h b/utils/ConfigBase/ConfigBaseCode.h deleted file mode 100644 index 2eb84b2b..00000000 --- a/utils/ConfigBase/ConfigBaseCode.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @file ConfigBaseCode.h - * @brief Contains internal definitions and declarations for configuration codes. - * - * This header file is intended for internal use only and should not be included - * directly by other files. It defines the StatusCode creation function for configuration - * related codes. - */ -#ifndef CONFIG_BASE_CODE_H -#define CONFIG_BASE_CODE_H -#include "ConfigBase.h" -#include "StatusCode.h" -#ifdef __cplusplus -extern "C" { -#endif -/** - * @brief Error directive indicating that this file is for internal use only. - * - * This directive is used to prevent accidental inclusion of the internal header file - * by external code. If this file is included without the CONFIG_OWNER macro being defined, - * it will cause a compile-time error. - */ -#ifndef CONFIG_OWNER - #error This is internal file, never include it. -#endif -/** - * @brief Creates a StatusCode for configuration codes. - * - * This function is used internally to create a StatusCode that represents a configuration - * related code. It is not part of the public API and should not be used by external code. - * - * @param code The long integer value to be encapsulated in a StatusCode. - * @return A StatusCode object representing the configuration code. - */ -const StatusCode CreateConfigCode(const long int code); -#ifdef __cplusplus -} -#endif -#endif diff --git a/utils/ConfigBase/ConfigBaseImpl.h b/utils/ConfigBase/ConfigBaseImpl.h deleted file mode 100644 index 9e31b52c..00000000 --- a/utils/ConfigBase/ConfigBaseImpl.h +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @file ConfigBaseImpl.h - * @brief Provides an implementation of the IConfigBase interface using libconfig. - * - * This header file declares the ConfigBaseImpl class which implements the configuration - * interface defined by IConfigBase. It uses the libconfig library to handle the - * configuration file parsing and saving. - */ -#ifndef CONFIG_BASE_IMPL_H -#define CONFIG_BASE_IMPL_H -#include "IConfigBase.h" -#include -#include -/** - * @class ConfigBaseImpl - * @brief Concrete implementation of the IConfigBase interface. - * - * ConfigBaseImpl is a class that provides an implementation of the IConfigBase interface. - * It uses the libconfig library to manage configuration files and settings. - */ -class ConfigBaseImpl : public IConfigBase -{ -public: - ConfigBaseImpl(const std::string &fileName); - virtual ~ConfigBaseImpl() = default; - /** - * @brief Opens the configuration file for reading and writing. - * - * @return true if the file was successfully opened, false otherwise. - */ - bool OpenConfigFile(void) override; - /** - * @brief Closes the configuration file. - */ - void CloseConfigFile(void) override; - /** - * @brief Saves the current configuration to the file. - * - * This function writes the current in-memory configuration settings to the file - * specified during the construction of the ConfigBaseImpl object. It uses the libconfig - * library to perform the save operation. - * - * @return A StatusCode indicating the success or failure of the operation. - */ - StatusCode ConfigSaveFile(void) override; - /** - * @brief Retrieves an integer configuration value. - * - * This function gets the integer value associated with the specified configuration - * parameter name. - * - * @param name The name of the configuration parameter to retrieve. - * @param value A pointer to store the retrieved integer value. - * @return A StatusCode indicating the success or failure of the operation. - */ - StatusCode ConfigGetInt(const char *name, int *value) override; - /** - * @brief Sets an integer configuration value. - * - * This function sets the integer value for the specified configuration parameter name. - * - * @param name The name of the configuration parameter to set. - * @param value The integer value to set. - * @return A StatusCode indicating the success or failure of the operation. - */ - StatusCode ConfigSetInt(const char *name, const int value) override; - /** - * @brief Retrieves a long long integer configuration value. - * - * @param name The name of the configuration parameter to retrieve. - * @param value A pointer to store the retrieved long long integer value. - * @return A StatusCode for the operation. - */ - StatusCode ConfigGetShort(const char *name, short *value) override; - /** - * @brief Sets a long long integer configuration value. - * - * @param name The name of the configuration parameter to set. - * @param value The long long integer value to set. - * @return A StatusCode for the operation. - */ - StatusCode ConfigSetShort(const char *name, const short value) override; - /** - * @brief Retrieves a long integer configuration value. - * - * @param name The name of the configuration setting to retrieve. - * @param value A pointer to a long integer where the retrieved value will be stored. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigGetLong(const char *name, long *value) override; - /** - * @brief Sets a long integer configuration value. - * - * @param name The name of the configuration setting to set. - * @param value The long integer value to set for the configuration setting. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigSetLong(const char *name, const long value) override; - /** - * @brief Retrieves a long long integer configuration value. - * - * @param name The name of the configuration parameter to retrieve. - * @param value A pointer to store the retrieved long long integer value. - * @return A StatusCode for the operation. - */ - StatusCode ConfigGetLLong(const char *name, long long *value) override; - /** - * @brief Sets a long long integer configuration value. - * - * @param name The name of the configuration parameter to set. - * @param value The long long integer value to set. - * @return A StatusCode for the operation. - */ - StatusCode ConfigSetLLong(const char *name, const long long value) override; - /** - * @brief Retrieves a character configuration value. - * - * @param name The name of the configuration parameter to retrieve. - * @param value A pointer to store the retrieved character value. - * @return A StatusCode for the operation. - */ - StatusCode ConfigGetChar(const char *name, char *value) override; - /** - * @brief Sets a character configuration value. - * - * @param name The name of the configuration parameter to set. - * @param value The character value to set. - * @return A StatusCode for the operation. - */ - StatusCode ConfigSetChar(const char *name, const char value) override; - /** - * @brief Retrieves a boolean configuration value. - * - * @param name The name of the configuration setting to retrieve. - * @param value A pointer to a bool where the retrieved value will be stored. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigGetBool(const char *name, bool *value) override; - /** - * @brief Sets a boolean configuration value. - * - * @param name The name of the configuration setting to set. - * @param value The boolean value to set for the configuration setting. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigSetBool(const char *name, const bool value) override; - /** - * @brief Retrieves a floating-point configuration value. - * - * @param name The name of the configuration setting to retrieve. - * @param value A pointer to a float where the retrieved value will be stored. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigGetFloat(const char *name, float *value) override; - /** - * @brief Sets a floating-point configuration value. - * - * @param name The name of the configuration setting to set. - * @param value The float value to set for the configuration setting. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigSetFloat(const char *name, const float value) override; - - /** - * @brief Retrieves a double-precision floating-point configuration value. - * - * @param name The name of the configuration setting to retrieve. - * @param value A pointer to a double where the retrieved value will be stored. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigGetDouble(const char *name, double *value) override; - /** - * @brief Sets a double-precision floating-point configuration value. - * - * @param name The name of the configuration setting to set. - * @param value The double value to set for the configuration setting. - * @return StatusCode::SUCCESS if the operation was successful, an error code otherwise. - */ - StatusCode ConfigSetDouble(const char *name, const double value) override; - /** - * @brief Retrieves a string configuration value. - * - * This function gets the string value associated with the specified configuration - * parameter name. The returned string is managed by the libconfig library and should - * not be freed by the caller. - * - * @param name The name of the configuration parameter to retrieve. - * @param value A pointer to store the retrieved string value. - * @return A StatusCode indicating the success or failure of the operation. - */ - StatusCode ConfigGetString(const char *name, const char **value) override; - /** - * @brief Retrieves a string configuration value. - * - * This function gets the string value associated with the specified configuration - * parameter name. The returned string is managed by the libconfig library and should - * not be freed by the caller. - * - * @param name The name of the configuration parameter to retrieve. - * @param value A pointer to store the retrieved string value. - * @return A StatusCode indicating the success or failure of the operation. - */ - StatusCode ConfigSetString(const char *name, const char *value) override; - -private: - /** - * @brief The file name of the configuration file managed by this instance. - */ - const std::string mFileName; - /** - * @brief The libconfig structure used to hold the configuration settings. - */ - config_t mCfg; -}; -#endif diff --git a/utils/ConfigBase/IConfigBase.h b/utils/ConfigBase/IConfigBase.h deleted file mode 100644 index 8e782e23..00000000 --- a/utils/ConfigBase/IConfigBase.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2023 Fancy Code. - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @file IConfigBase.h - * @brief Defines the interface for configuration base operations. - * - * This header file declares the IConfigBase interface and associated types for - * managing configuration settings. It provides a common set of functions for - * interacting with configuration data, which can be implemented by various - * configuration backends. - */ -#ifndef I_CONFIG_BASE_H -#define I_CONFIG_BASE_H -#include "StatusCode.h" -#include -/** - * @class IConfigBase - * @brief Interface for configuration management. - * - * IConfigBase is an abstract interface that defines the contract for configuration - * operations. Concrete implementations of this interface will provide the - * functionality for opening, modifying, and saving configuration settings. - */ -class IConfigBase -{ -public: - IConfigBase() = default; - virtual ~IConfigBase() = default; - virtual bool OpenConfigFile(void); - virtual void CloseConfigFile(void); - virtual StatusCode ConfigSaveFile(void); - virtual StatusCode ConfigGetInt(const char *name, int *value); - virtual StatusCode ConfigSetInt(const char *name, const int value); - virtual StatusCode ConfigGetShort(const char *name, short *value); - virtual StatusCode ConfigSetShort(const char *name, const short value); - virtual StatusCode ConfigGetLong(const char *name, long *value); - virtual StatusCode ConfigSetLong(const char *name, const long value); - virtual StatusCode ConfigGetLLong(const char *name, long long *value); - virtual StatusCode ConfigSetLLong(const char *name, const long long value); - virtual StatusCode ConfigGetChar(const char *name, char *value); - virtual StatusCode ConfigSetChar(const char *name, const char value); - virtual StatusCode ConfigGetBool(const char *name, bool *value); - virtual StatusCode ConfigSetBool(const char *name, const bool value); - virtual StatusCode ConfigGetFloat(const char *name, float *value); - virtual StatusCode ConfigSetFloat(const char *name, const float value); - virtual StatusCode ConfigGetDouble(const char *name, double *value); - virtual StatusCode ConfigSetDouble(const char *name, const double value); - virtual StatusCode ConfigGetString(const char *name, const char **value); - virtual StatusCode ConfigSetString(const char *name, const char *value); -}; -/** - * @struct IConfigBaseHeader - * @brief A structure that may be used for validation or recognition purposes. - * - * This structure is meant to contain a pointer to a character which could be - * utilized for some form of validation or recognition check. - */ -typedef struct i_config_base_header // Defined a struct i_config.base_header containing a pointer mCheckName to a - // character, which may be used for some form of validation or recognition. -{ - const char *mCheckName; -} IConfigBaseHeader; -/** - * @struct ConfigBase - * @brief A structure that combines the IConfigBaseHeader with a shared pointer to IConfigBase. - * - * This structure is intended to provide additional context or metadata along with - * the actual configuration base object. - */ -typedef struct config_base -{ - IConfigBaseHeader mHeader; - std::shared_ptr mIConfigBase; -} ConfigBase; -/** - * @brief Gets the module name of the configuration base. - * - * This function returns a constant character pointer to the module name as a string. - * - * @return A constant character pointer to the module name. - */ -const char *GetConfigBaseModuleName(void); -/** - * @brief Creates a new instance of the IConfigBase implementation. - * - * This function constructs a new ConfigBase structure with a shared pointer to an - * IConfigBase object, initialized with the provided file name. - * - * @param fileName The name of the configuration file to associate with the new object. - * @return A pointer to a new ConfigBase structure. - */ -std::shared_ptr *NewConfigBase(const char *fileName); -#endif From 7dbe7409ec91f288d6dc27a950e2b4c2d1cc58c1 Mon Sep 17 00:00:00 2001 From: Hebin Chen <3076186116@qq.com> Date: Sat, 3 Aug 2024 03:23:33 -0700 Subject: [PATCH 40/48] explanation: --- utils/TcpModule/src/TcpServerHandle.cpp | 19 +++++++++++++++++++ utils/TcpModule/src/TcpServerHandle.h | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/utils/TcpModule/src/TcpServerHandle.cpp b/utils/TcpModule/src/TcpServerHandle.cpp index 3928fbae..e5cc292d 100644 --- a/utils/TcpModule/src/TcpServerHandle.cpp +++ b/utils/TcpModule/src/TcpServerHandle.cpp @@ -16,6 +16,12 @@ #include "ILog.h" #include "ITcpServer.h" #include + +/** + * @brief Get the singleton instance of TcpServerHandle + * @param impl Optional implementation pointer to replace the current instance + * @return The singleton instance of TcpServerHandle + */ std::shared_ptr &TcpServerHandle::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); @@ -30,11 +36,24 @@ std::shared_ptr &TcpServerHandle::GetInstance(std::shared_ptr &server) { LogInfo("AddServer fd = %d\n", fd); mFd[fd] = server; } + +/** + * @brief Get a server by file descriptor + * @param fd File descriptor of the server + * @param server Reference to a shared pointer to store the ITcpServer instance + * @return True if the server is found, false otherwise + */ bool TcpServerHandle::GetServer(const int &fd, std::shared_ptr &server) { auto it = mFd.find(fd); diff --git a/utils/TcpModule/src/TcpServerHandle.h b/utils/TcpModule/src/TcpServerHandle.h index 646b2680..e7a6acf3 100644 --- a/utils/TcpModule/src/TcpServerHandle.h +++ b/utils/TcpModule/src/TcpServerHandle.h @@ -20,10 +20,32 @@ class TcpServerHandle { public: + /** + * @brief Default constructor + */ TcpServerHandle() = default; + /** + * @brief Default destructor + */ ~TcpServerHandle() = default; + /** + * @brief Get the singleton instance of TcpServerHandle + * @param impl Optional implementation pointer to replace the current instance + * @return The singleton instance of TcpServerHandle + */ static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + /** + * @brief Add a server + * @param fd File descriptor of the server + * @param server Shared pointer to the ITcpServer instance + */ void AddServer(const int &fd, const std::shared_ptr &server); + /** + * @brief Get a server by file descriptor + * @param fd File descriptor of the server + * @param server Reference to a shared pointer to store the ITcpServer instance + * @return True if the server is found, false otherwise + */ bool GetServer(const int &fd, std::shared_ptr &server); private: From 538da1878bb9d262f8ebda74002e8711d1553b6c Mon Sep 17 00:00:00 2001 From: Hebin Chen <3076186116@qq.com> Date: Sat, 10 Aug 2024 01:03:32 -0700 Subject: [PATCH 42/48] =?UTF-8?q?=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/TcpModule/include/TcpModule.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/TcpModule/include/TcpModule.h b/utils/TcpModule/include/TcpModule.h index dd772b3c..a47a928c 100644 --- a/utils/TcpModule/include/TcpModule.h +++ b/utils/TcpModule/include/TcpModule.h @@ -25,7 +25,7 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *); typedef void (*SocketClosedFunction)(const void *); typedef struct client_accept_parm { - const TcpReadFunction mReadFunc; /// This function is defined in the test file. + const TcpReadFunction mReadFunc; /// This function is defined in the test file const SocketClosedFunction mClosedFunc; /// This function is defined in the test file. } ClientAcceptParam; typedef struct tcp_server_parm From 15fb932e6a3c2f53255b7e94f30a38676fc7f245 Mon Sep 17 00:00:00 2001 From: Hebin Chen <3076186116@qq.com> Date: Sat, 10 Aug 2024 01:03:57 -0700 Subject: [PATCH 43/48] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/TcpModule/include/TcpModule.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/TcpModule/include/TcpModule.h b/utils/TcpModule/include/TcpModule.h index a47a928c..e5f03540 100644 --- a/utils/TcpModule/include/TcpModule.h +++ b/utils/TcpModule/include/TcpModule.h @@ -25,7 +25,7 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *); typedef void (*SocketClosedFunction)(const void *); typedef struct client_accept_parm { - const TcpReadFunction mReadFunc; /// This function is defined in the test file + const TcpReadFunction mReadFunc; const SocketClosedFunction mClosedFunc; /// This function is defined in the test file. } ClientAcceptParam; typedef struct tcp_server_parm From f508c8bbd524ef7091a6d7a0fbf9eaca116ff163 Mon Sep 17 00:00:00 2001 From: Hebin Chen <3076186116@qq.com> Date: Sat, 10 Aug 2024 01:30:49 -0700 Subject: [PATCH 45/48] =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/TcpModule/include/TcpModule.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/TcpModule/include/TcpModule.h b/utils/TcpModule/include/TcpModule.h index e5f03540..a47a928c 100644 --- a/utils/TcpModule/include/TcpModule.h +++ b/utils/TcpModule/include/TcpModule.h @@ -25,7 +25,7 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *); typedef void (*SocketClosedFunction)(const void *); typedef struct client_accept_parm { - const TcpReadFunction mReadFunc; + const TcpReadFunction mReadFunc; /// This function is defined in the test file const SocketClosedFunction mClosedFunc; /// This function is defined in the test file. } ClientAcceptParam; typedef struct tcp_server_parm From 5a33dbfc06f81f570ed55682ab567ad2deb2dc55 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Sun, 11 Aug 2024 15:57:17 +0800 Subject: [PATCH 46/48] Improve readme files and comments --- utils/TcpModule/readme.md | 26 +++++++++++++ utils/TcpModule/src/TcpClientImpl.cpp | 54 ++++++++++++++++++++------- utils/TcpModule/src/TcpClientImpl.h | 23 ++++++------ utils/TcpModule/src/TcpServerImpl.cpp | 13 +++---- utils/TcpModule/src/TcpServerImpl.h | 4 +- 5 files changed, 87 insertions(+), 33 deletions(-) diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md index ad41416b..ff2f0267 100644 --- a/utils/TcpModule/readme.md +++ b/utils/TcpModule/readme.md @@ -31,3 +31,29 @@ * **TcpClientAcceptImpl.h/TcpClientAcceptImpl.cpp**:实现了ITcpClientAccept接口,处理客户端连接的接受和数据交换。 +# 一些易混淆的点 +1. 客户端创建的套接字包含指定`目标的IP地址和端口号`(TcpClientImpl类中的init函数),服务器创建的套接字只包含`本机IP地址和监听端口`。 + +2. 在网络编程中,服务器通常会在一个监听端口上等待来自客户端的连接请求。当服务器接受到一个连接请求时,它会创建一个新的套接字(或文件描述符)来表示这个新建立的连接,并且通常会为这个新的连接设置一系列的事件回调函数。 + +3. 客户端实例(TcpClientImpl类对象)进行init操作时,会创建一个套接字并对目标服务器发出连接请求并启动事件循环(Loop函数),然后客户端实例会一直进行事件循环(监听是否有读写操作)直至连接关闭(Close函数)。 + +# 该模块的实现过程 +## 服务器端: +1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。 +2. 设置回调函数:为服务器端套接字设置回调函数,包括: +on_accept:当有新的客户端尝试连接时调用。 +on_close:当服务器端套接字关闭时调用。 +3. 启动事件循环:使用 hloop_run 启动服务器的事件循环,等待客户端的连接请求。 +4. 接受连接:在 on_accept 回调函数中,接受客户端的连接请求,并创建用于该连接的新套接字。 +5. 创建客户端接受对象:为新的客户端连接创建一个 TcpClientAcceptImpl 对象,并设置相应的回调函数。 +管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。 +## 客户端: +1. 创建客户端套接字:使用 hio_create_socket 函数创建一个新的 TCP 客户端套接字。 +2. 设置回调函数:为客户端套接字设置回调函数,包括: +on_connect:当连接成功建立时调用。 +on_close:当连接关闭时调用。 +on_message:当接收到服务器发送的数据时调用。 +3. 连接到服务器:使用 hio_connect 函数向服务器发起连接请求。 +4. 启动事件循环:使用 hloop_run 启动客户端的事件循环,等待连接结果和数据传输。 +5. 处理连接结果:在 on_connect 回调函数中处理连接结果,如果连接成功,可以开始发送和接收数据。 \ No newline at end of file diff --git a/utils/TcpModule/src/TcpClientImpl.cpp b/utils/TcpModule/src/TcpClientImpl.cpp index 8b420736..58c723e2 100644 --- a/utils/TcpModule/src/TcpClientImpl.cpp +++ b/utils/TcpModule/src/TcpClientImpl.cpp @@ -26,7 +26,7 @@ /** * @brief Called when data is received on a TCP connection. * - * @param io Socket handle + * @param io Client's socket * @param buf The transmitted data content * @param len Byte length of transmitted data */ @@ -38,17 +38,18 @@ static void on_message(hio_t *io, void *buf, int len) } /** * @brief Called when TCP connection is established. - * @param io Socket handle + * @param io Client's socket */ static void on_connect(hio_t *io) { LogInfo("onconnect: connfd=%d\n", hio_fd(io)); hio_setcb_read(io, on_message); + /// Start a read operation. Read data if it arrives. hio_read(io); } /** * @brief Called when tcp connection is disconnected. - * @param io Socket handle + * @param io Client't socket */ static void on_close(hio_t *io) { @@ -60,8 +61,8 @@ TcpClientImpl::TcpClientImpl(const TcpClientParam ¶m, const void *object) : { } /** - * @brief Initialize TCP clients, create event loops, I/O objects, and set callback functions for connection and - * closure. + * @brief Create a socket and connect it, and start an event loop to listen to io object operations (read and write, + * etc.) */ void TcpClientImpl::Init(void) { @@ -72,6 +73,19 @@ void TcpClientImpl::Init(void) LogError("TcpClientImpl::Init hloop_new failed.\n"); return; } + /** + * @brief The client's socket, which is used to actively send connection requests to the server and handle related + * I/O operations, has no listening function. + * @param mLoop Event loop. + * When I/O events (connection closed, connection successful, data readable, etc.) occur, the event loop will call + * the corresponding callback function to handle them. + * + * @param mParam.mIp Server IP address + * @param mParam.mPort Server port + * @param HIO_TYPE_TCP TCP connection + * @param HIO_CLIENT_SIDE Indicates that the socket will be used as a client. + * + */ hio_t *io = hio_create_socket(mLoop, mParam.mIp, mParam.mPort, HIO_TYPE_TCP, HIO_CLIENT_SIDE); if (nullptr == io) { LogError("TcpClientImpl::Init hio_create_socket failed.\n"); @@ -80,9 +94,9 @@ void TcpClientImpl::Init(void) hevent_set_userdata(io, this); /// Set the callback function of successful connection, and there is no connection operation. hio_setcb_connect(io, on_connect); + /// Set the callback function to close the connection, and there is no connection operation. hio_setcb_close(io, on_close); - /// Connection operation - hio_connect(io); + hio_connect(io); ///< Connection operation mIo = io; std::shared_ptr impl = std::dynamic_pointer_cast(shared_from_this()); auto recvThread = [](std::shared_ptr tcpClient) { @@ -91,7 +105,7 @@ void TcpClientImpl::Init(void) mTcpClientThread = std::thread(recvThread, impl); } /** - * @brief De-initialize the TCP client, close the I/O object and wait for the receiving thread to end. + * @brief De-initialize the TCP client, close the client't socket and wait for the event cycle to end. */ void TcpClientImpl::UnInit(void) { @@ -104,6 +118,12 @@ void TcpClientImpl::UnInit(void) mTcpClientThread.join(); } } +/** + * @brief Read the data sent by the server in tcp connection. + * + * @param data Data content sent by the server + * @param length Byte length of data content sent by the server. + */ void TcpClientImpl::Readed(const void *data, size_t length) { if (nullptr != mParam.mReadFunc) { @@ -112,6 +132,13 @@ void TcpClientImpl::Readed(const void *data, size_t length) } LogError("mParam.mReadFunc is nullptr.\n"); } +/** + * @brief The client writes data to the server, and the writing operation can only be performed in the connected state. + * + * @param buf Content of written data + * @param bufLenght Byte length of written data + * @return ssize_t + */ ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght) { std::lock_guard locker(mMutex); @@ -122,6 +149,10 @@ ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght) /// Returns the byte length of a packet.If it fails, an error code is returned. return hio_write(mIo, buf, bufLenght); } +/** + * @brief Close the tcp connection and stop the event loop (reading and writing I/O objects). + * + */ void TcpClientImpl::Closed(void) { std::lock_guard locker(mMutex); @@ -157,15 +188,12 @@ std::shared_ptr *NewTcpClient(const TcpClientParam ¶m) return nullptr; } TcpClient tmp; + /// Initialize impl with tmp memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient)); impl->mHeader.mCheckName = GetTcpClientModuleName(); - /** - * @brief ObjectThis points to the first address of the impl and offsets the address by ITcpClientHeader bytes, - * that is, skips the mHeader part of the impl. - */ + /// ObjectThis is actually a pointer to mTcpClient in the TcpClient structure. std::shared_ptr *objectThis = (std::shared_ptr *)(((char *)impl) + sizeof(ITcpClientHeader)); impl->mTcpClient = std::make_shared(param, objectThis); - /// ObjectThis is used to verify whether the client is legal. return objectThis; } \ No newline at end of file diff --git a/utils/TcpModule/src/TcpClientImpl.h b/utils/TcpModule/src/TcpClientImpl.h index 49f60eb2..b2023168 100644 --- a/utils/TcpModule/src/TcpClientImpl.h +++ b/utils/TcpModule/src/TcpClientImpl.h @@ -27,20 +27,21 @@ class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this *NewTcpClient(const TcpClientParam ¶m); #endif \ No newline at end of file diff --git a/utils/TcpModule/src/TcpServerImpl.cpp b/utils/TcpModule/src/TcpServerImpl.cpp index 61147c62..16a8e5bd 100644 --- a/utils/TcpModule/src/TcpServerImpl.cpp +++ b/utils/TcpModule/src/TcpServerImpl.cpp @@ -25,9 +25,9 @@ #include #include /** - * @brief Disconnect the connected client. + * @brief Handle the close connection event. * - * @param io Socket + * @param io Server's socket */ static void on_close(hio_t *io) { @@ -150,15 +150,13 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param) void TcpServerImpl::Init(void) { constexpr int NO_FALGS = 0; - /// Initialize event loop /// mLoop is used to manage all I/O objects and the events that occur on them. mLoop = hloop_new(NO_FALGS); if (nullptr == mLoop) { LogError("hloop_new failed\n"); return; } - /// Create a listening event to listen whether a new client sends a connection request. - /// Listenio is part of mLoop. + /// Create a socket to listen whether a new client sends a connection request. hio_t *listenio = hloop_create_tcp_server(mLoop, mParam.mIp, mParam.mPort, on_accept); if (nullptr == listenio) { LogError("hloop_create_tcp_server failed\n"); @@ -256,9 +254,9 @@ std::shared_ptr *TcpServerImpl::GetClient(hio_t *io) return nullptr; } /** - * @brief Remove the data element in the map, that is, the connected client. + * @brief Removes a specific connection from the list of client connections maintained by the server. * - * @param io Socket + * @param io A server-side socket associated with an established client connection. */ void TcpServerImpl::RemoveClient(hio_t *io) { @@ -316,6 +314,7 @@ std::shared_ptr *NewTcpServer(const TcpServerParam ¶m) return nullptr; } TcpServer tmp; + /// Initialize impl with tmp memcpy((void *)impl, (void *)&tmp, sizeof(TcpServer)); impl->mHeader.mCheckName = GetTcpServerModuleName(); impl->mTcpServer = std::make_shared(param); diff --git a/utils/TcpModule/src/TcpServerImpl.h b/utils/TcpModule/src/TcpServerImpl.h index e4c19de5..74a7a93b 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -54,7 +54,7 @@ public: virtual ~TcpServerImpl() = default; void Init(void) override; void UnInit(void) override; - void Loop(void); /// Run an event loop and release resources after completion or error. + void Loop(void); /// Run an event loop and release resources after completion or error. void AddClient(hio_t *io); std::shared_ptr *GetClient(hio_t *io); void RemoveClient(hio_t *io); /// Remove the data element in the map, that is, the connected client. @@ -64,7 +64,7 @@ public: private: std::mutex mMutex; /// A mutex lock used to synchronize access to shared resources. hloop_t *mLoop; /// Event loop, listening for all io objects - hio_t *mIo; /// listen whether a new client sends a connection request. + hio_t *mIo; /// A server socket to listen whether a new client sends a connection request. const TcpServerParam mParam; std::thread mTcpServerThread; std::map *> mClients; From e7bf6d761a50d8e665ff014e942934aacd7eaa3c Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Mon, 12 Aug 2024 17:13:12 +0800 Subject: [PATCH 47/48] modify some annotation --- utils/TcpModule/readme.md | 3 ++- utils/TcpModule/src/TcpServerImpl.cpp | 27 ++++++++++++------- utils/TcpModule/src/TcpServerImpl.h | 39 +++++++++++++-------------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md index ff2f0267..741ca01c 100644 --- a/utils/TcpModule/readme.md +++ b/utils/TcpModule/readme.md @@ -34,10 +34,11 @@ # 一些易混淆的点 1. 客户端创建的套接字包含指定`目标的IP地址和端口号`(TcpClientImpl类中的init函数),服务器创建的套接字只包含`本机IP地址和监听端口`。 -2. 在网络编程中,服务器通常会在一个监听端口上等待来自客户端的连接请求。当服务器接受到一个连接请求时,它会创建一个新的套接字(或文件描述符)来表示这个新建立的连接,并且通常会为这个新的连接设置一系列的事件回调函数。 +2. 在网络编程中,`服务器`通常会在一个监听端口上等待来自客户端的连接请求。当服务器接受到一个连接请求时,它会创建一个新的套接字(或文件描述符)来表示这个新建立的连接(TcpClientAccept类实例),并且通常会为这个新的连接设置一系列的事件回调函数。 3. 客户端实例(TcpClientImpl类对象)进行init操作时,会创建一个套接字并对目标服务器发出连接请求并启动事件循环(Loop函数),然后客户端实例会一直进行事件循环(监听是否有读写操作)直至连接关闭(Close函数)。 + # 该模块的实现过程 ## 服务器端: 1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。 diff --git a/utils/TcpModule/src/TcpServerImpl.cpp b/utils/TcpModule/src/TcpServerImpl.cpp index 16a8e5bd..d8d7c48a 100644 --- a/utils/TcpModule/src/TcpServerImpl.cpp +++ b/utils/TcpModule/src/TcpServerImpl.cpp @@ -106,8 +106,8 @@ void TcpClientAcceptImpl::Close(void) /** * @brief The server reads the data received by tcp connection. * - * @param data - * @param length bytes + * @param data Read data content + * @param length Read data byte length */ void TcpClientAcceptImpl::Readed(const void *data, size_t length) { @@ -134,6 +134,10 @@ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length) LogError("mIo is null\n"); return TCP_MODULE_WRITE_ERROR; } +/** + * @brief Used to perform some cleaning work or notify upper layer logic when a TCP client accepts a connection that is closed + * + */ void TcpClientAcceptImpl::Closed(void) { if (nullptr != mParam.mClosedFunc) { @@ -147,6 +151,10 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param) mLoop = nullptr; mIo = nullptr; } +/** + * @brief Create an event loop to listen for connection requests from the server socket, set up relevant callback functions, and start a separate thread to run the event loop + * + */ void TcpServerImpl::Init(void) { constexpr int NO_FALGS = 0; @@ -208,9 +216,9 @@ void TcpServerImpl::Loop(void) mLoop = nullptr; } /** - * @brief + * @brief Add a newly connected client to the server connection management list. * - * @param io Add the socket of the newly connected client. + * @param io Socket associated with server and client connections */ void TcpServerImpl::AddClient(hio_t *io) { @@ -223,9 +231,7 @@ void TcpServerImpl::AddClient(hio_t *io) SOCKADDR_STR(hio_peeraddr(io), peeraddrstr)); std::shared_ptr *addClient = NewTcpClientAccept(io, mParam.mClientAcceptParam); mClients[hio_fd(io)] = addClient; - /** - * @brief Check whether the server side accepts the connection of the client side. - */ + /// Check whether the server side accepts the connection of the client side. if (mParam.mAcceptClientFunc) { if (mParam.mAcceptClientFunc(addClient, peeraddrstr) == true) { mMutex.unlock(); @@ -238,9 +244,9 @@ void TcpServerImpl::AddClient(hio_t *io) LogWarning("AddClient failed.\n"); } /** - * @brief + * @brief Retrieve the corresponding client acceptance object based on the socket handle. * - * @param io socket + * @param io Handle pointing to socket * @return std::shared_ptr* */ std::shared_ptr *TcpServerImpl::GetClient(hio_t *io) @@ -322,7 +328,7 @@ std::shared_ptr *NewTcpServer(const TcpServerParam ¶m) } std::shared_ptr *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m) { - LogInfo("Create tcp server object.\n"); + LogInfo("Create tcp client accept object.\n"); TcpClientAccept *impl = (TcpClientAccept *)malloc(sizeof(TcpClientAccept)); if (nullptr == impl) { LogError("NewTcpServer::malloc failed.\n"); @@ -331,6 +337,7 @@ std::shared_ptr *NewTcpClientAccept(const hio_t *io, const Cli TcpClientAccept tmp; memcpy((void *)impl, (void *)&tmp, sizeof(TcpClientAccept)); impl->mHeader.mCheckName = GetTcpClientAcceptName(); + ///ObjectThis actually refers to mTcpClientAccept std::shared_ptr *objectThis = (std::shared_ptr *)(((char *)impl) + sizeof(ITcpServerHeader)); impl->mTcpClientAccept = std::make_shared(io, objectThis, param); diff --git a/utils/TcpModule/src/TcpServerImpl.h b/utils/TcpModule/src/TcpServerImpl.h index 74a7a93b..ea2cc5d0 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -23,8 +23,7 @@ #include #include /** - * @brief The server manages the connection life cycle and data interaction of each connected client through this kind - * of instance. + * @brief The TcpClient Accept class instance is used to associate with the socket after connecting to the server and client, responsible for a series of operations (read/write, etc.) after the connection * */ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this @@ -32,14 +31,14 @@ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_f public: TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m); virtual ~TcpClientAcceptImpl() = default; - void Close(void) override; - void Readed(const void *data, size_t length) override; - ssize_t Write(const void *data, size_t length) override; - void Closed(void) override; + void Close(void) override; /// *GetClient(hio_t *io); - void RemoveClient(hio_t *io); /// Remove the data element in the map, that is, the connected client. - void FreeClients(void); /// Clear all connected clients. - void Closed(void); /// Stop listening to the event loop(mLoop) + void UnInit(void) override; + void Loop(void); ///< Run an event loop and release resources after completion or error. + void AddClient(hio_t *io); ///< Add a newly connected client to the server connection management list. + std::shared_ptr *GetClient(hio_t *io);///< Retrieve the corresponding client acceptance object based on the socket handle. + void RemoveClient(hio_t *io); ///< Remove the data element in the map, that is, the connected client. + void FreeClients(void); ///< Clear all connected clients. + void Closed(void); ///< Stop listening to the event loop(mLoop) private: - std::mutex mMutex; /// A mutex lock used to synchronize access to shared resources. - hloop_t *mLoop; /// Event loop, listening for all io objects - hio_t *mIo; /// A server socket to listen whether a new client sends a connection request. + std::mutex mMutex; ///< A mutex lock used to synchronize access to shared resources. + hloop_t *mLoop; ///< Event loop, listening for all io objects + hio_t *mIo; ///< A server socket to listen whether a new client sends a connection request. const TcpServerParam mParam; - std::thread mTcpServerThread; - std::map *> mClients; + std::thread mTcpServerThread; ///< A separate thread used to run event loops + std::map *> mClients;///< Container for storing clients connected to servers }; std::shared_ptr *NewTcpServer(const TcpServerParam ¶m); std::shared_ptr *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m); From 63147a9928767f098a413e7c19fcfe2ee2996110 Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Tue, 13 Aug 2024 15:48:29 +0800 Subject: [PATCH 48/48] Improve the readme file --- utils/TcpModule/include/TcpModule.h | 12 +++++------ utils/TcpModule/readme.md | 16 ++++++++------ utils/TcpModule/src/TcpClientImpl.h | 4 ++-- utils/TcpModule/src/TcpModuleMakePtr.h | 4 ++++ utils/TcpModule/src/TcpServerImpl.cpp | 12 ++++++----- utils/TcpModule/src/TcpServerImpl.h | 29 ++++++++++++++------------ 6 files changed, 45 insertions(+), 32 deletions(-) diff --git a/utils/TcpModule/include/TcpModule.h b/utils/TcpModule/include/TcpModule.h index a47a928c..c8c35edd 100644 --- a/utils/TcpModule/include/TcpModule.h +++ b/utils/TcpModule/include/TcpModule.h @@ -25,15 +25,15 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *); typedef void (*SocketClosedFunction)(const void *); typedef struct client_accept_parm { - const TcpReadFunction mReadFunc; /// This function is defined in the test file - const SocketClosedFunction mClosedFunc; /// This function is defined in the test file. + const TcpReadFunction mReadFunc; ///< This function is defined in the test file + const SocketClosedFunction mClosedFunc; ///< This function is defined in the test file. } ClientAcceptParam; typedef struct tcp_server_parm { - const char *mIp; /// Server ip - const int mPort; /// Server port - const TcpAcceptClientFunction mAcceptClientFunc; /// This function is defined in the test file. - const ClientAcceptParam mClientAcceptParam; /// This function is defined in the test file. + const char *mIp; ///< Server ip + const int mPort; ///< Server port + const TcpAcceptClientFunction mAcceptClientFunc; ///< This function is defined in the test file. + const ClientAcceptParam mClientAcceptParam; ///< This function is defined in the test file. } TcpServerParam; typedef struct tcp_parm { diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md index 741ca01c..9f95174a 100644 --- a/utils/TcpModule/readme.md +++ b/utils/TcpModule/readme.md @@ -38,23 +38,27 @@ 3. 客户端实例(TcpClientImpl类对象)进行init操作时,会创建一个套接字并对目标服务器发出连接请求并启动事件循环(Loop函数),然后客户端实例会一直进行事件循环(监听是否有读写操作)直至连接关闭(Close函数)。 +4. 服务器`监听连接的套接字`和`连接后新建立的套接字`要分别设置回调函数,这是两个东西,不要混淆。 + # 该模块的实现过程 ## 服务器端: 1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。 2. 设置回调函数:为服务器端套接字设置回调函数,包括: -on_accept:当有新的客户端尝试连接时调用。 -on_close:当服务器端套接字关闭时调用。 + on_accept:当有新的客户端尝试连接时调用。 + on_close:当服务器端套接字关闭时调用。 3. 启动事件循环:使用 hloop_run 启动服务器的事件循环,等待客户端的连接请求。 4. 接受连接:在 on_accept 回调函数中,接受客户端的连接请求,并创建用于该连接的新套接字。 5. 创建客户端接受对象:为新的客户端连接创建一个 TcpClientAcceptImpl 对象,并设置相应的回调函数。 -管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。 + on_close:当客户端连接关闭时调用。 + on_recv:当客户端发送数据时调用。 +6. 管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。 ## 客户端: 1. 创建客户端套接字:使用 hio_create_socket 函数创建一个新的 TCP 客户端套接字。 2. 设置回调函数:为客户端套接字设置回调函数,包括: -on_connect:当连接成功建立时调用。 -on_close:当连接关闭时调用。 -on_message:当接收到服务器发送的数据时调用。 + on_connect:当连接成功建立时调用。 + on_close:当连接关闭时调用。 + on_message:当接收到服务器发送的数据时调用。 3. 连接到服务器:使用 hio_connect 函数向服务器发起连接请求。 4. 启动事件循环:使用 hloop_run 启动客户端的事件循环,等待连接结果和数据传输。 5. 处理连接结果:在 on_connect 回调函数中处理连接结果,如果连接成功,可以开始发送和接收数据。 \ No newline at end of file diff --git a/utils/TcpModule/src/TcpClientImpl.h b/utils/TcpModule/src/TcpClientImpl.h index b2023168..20036652 100644 --- a/utils/TcpModule/src/TcpClientImpl.h +++ b/utils/TcpModule/src/TcpClientImpl.h @@ -27,8 +27,8 @@ class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this +/** + * @brief A factory class that indirectly creates server-side and client-side instances through this class + * + */ class TcpModuleMakePtr { public: diff --git a/utils/TcpModule/src/TcpServerImpl.cpp b/utils/TcpModule/src/TcpServerImpl.cpp index d8d7c48a..9d053c38 100644 --- a/utils/TcpModule/src/TcpServerImpl.cpp +++ b/utils/TcpModule/src/TcpServerImpl.cpp @@ -135,8 +135,9 @@ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length) return TCP_MODULE_WRITE_ERROR; } /** - * @brief Used to perform some cleaning work or notify upper layer logic when a TCP client accepts a connection that is closed - * + * @brief Used to perform some cleaning work or notify upper layer logic when a TCP client accepts a connection that is + * closed + * */ void TcpClientAcceptImpl::Closed(void) { @@ -152,8 +153,9 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param) mIo = nullptr; } /** - * @brief Create an event loop to listen for connection requests from the server socket, set up relevant callback functions, and start a separate thread to run the event loop - * + * @brief Create an event loop to listen for connection requests from the server socket, set up relevant callback + * functions, and start a separate thread to run the event loop + * */ void TcpServerImpl::Init(void) { @@ -337,7 +339,7 @@ std::shared_ptr *NewTcpClientAccept(const hio_t *io, const Cli TcpClientAccept tmp; memcpy((void *)impl, (void *)&tmp, sizeof(TcpClientAccept)); impl->mHeader.mCheckName = GetTcpClientAcceptName(); - ///ObjectThis actually refers to mTcpClientAccept + /// ObjectThis actually refers to mTcpClientAccept std::shared_ptr *objectThis = (std::shared_ptr *)(((char *)impl) + sizeof(ITcpServerHeader)); impl->mTcpClientAccept = std::make_shared(io, objectThis, param); diff --git a/utils/TcpModule/src/TcpServerImpl.h b/utils/TcpModule/src/TcpServerImpl.h index ea2cc5d0..35b5eecb 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -23,7 +23,8 @@ #include #include /** - * @brief The TcpClient Accept class instance is used to associate with the socket after connecting to the server and client, responsible for a series of operations (read/write, etc.) after the connection + * @brief The TcpClient Accept class instance is used to associate with the socket after connecting to the server and + * client, responsible for a series of operations (read/write, etc.) after the connection * */ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this @@ -31,14 +32,15 @@ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_f public: TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m); virtual ~TcpClientAcceptImpl() = default; - void Close(void) override; /// *GetClient(hio_t *io);///< Retrieve the corresponding client acceptance object based on the socket handle. + void UnInit(void) override; + void Loop(void); ///< Run an event loop and release resources after completion or error. + void AddClient(hio_t *io); ///< Add a newly connected client to the server connection management list. + std::shared_ptr * + GetClient(hio_t *io); ///< Retrieve the corresponding client acceptance object based on the socket handle. void RemoveClient(hio_t *io); ///< Remove the data element in the map, that is, the connected client. void FreeClients(void); ///< Clear all connected clients. void Closed(void); ///< Stop listening to the event loop(mLoop) @@ -65,8 +68,8 @@ private: hloop_t *mLoop; ///< Event loop, listening for all io objects hio_t *mIo; ///< A server socket to listen whether a new client sends a connection request. const TcpServerParam mParam; - std::thread mTcpServerThread; ///< A separate thread used to run event loops - std::map *> mClients;///< Container for storing clients connected to servers + std::thread mTcpServerThread; ///< A separate thread used to run event loops + std::map *> mClients; ///< Container for storing clients connected to servers }; std::shared_ptr *NewTcpServer(const TcpServerParam ¶m); std::shared_ptr *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m);