configbasee

Signed-off-by: 张耀 <3213487792@qq.com>
This commit is contained in:
张耀 2024-07-27 08:40:25 +00:00 committed by Gitee
parent f6d84acc30
commit cacb6dcbf8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 205 additions and 0 deletions

56
utils/ConfigBase.h Normal file
View File

@ -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++之间的互操作性。

35
utils/ConfigBaseCode.h Normal file
View File

@ -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
//总之,这个头文件定义了一个与配置代码相关的函数声明,并通过一系列的条件编译和头文件保护符来确保代码的正确性和安全性。

52
utils/ConfigBaseImpl.h Normal file
View File

@ -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.h>//这是libconfig库的头文件提供了处理配置文件的API。
#include <string>//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

62
utils/IConfigBase.h Normal file
View File

@ -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 <memory>
//StatusCode.h可能是一个定义状态码枚举或类的文件用于表示操作成功或失败的状态和<memory>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<IConfigBase> mIConfigBase;
} ConfigBase;
const char *GetConfigBaseModuleName(void);
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName);
//声明了两个函数GetConfigBaseModuleName可能用于获取配置模块的名称和NewConfigBase接受一个文件名作为参数并返回一个指向IConfigBase智能指针的指针用于创建和返回一个新的配置实例
#endif
//这段代码定义了一个用于配置管理的C++接口和相关数据结构以及与之相关的函数声明。它使用了C++的抽象基类、虚函数、智能指针等特性来实现灵活的配置管理功能。