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] 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