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