mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
configbasee
Signed-off-by: 张耀 <3213487792@qq.com>
This commit is contained in:
parent
6d4bbe46c1
commit
d432adea52
56
utils/ConfigBase/ConfigBase.h
Normal file
56
utils/ConfigBase/ConfigBase.h
Normal 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" //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++.
|
35
utils/ConfigBase/ConfigBaseCode.h
Normal file
35
utils/ConfigBase/ConfigBaseCode.h
Normal 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.
|
||||
*/
|
||||
//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.
|
53
utils/ConfigBase/ConfigBaseImpl.h
Normal file
53
utils/ConfigBase/ConfigBaseImpl.h
Normal file
|
@ -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 <libconfig.h>//This is the header file of the libconfig library, which provides an API for handling configuration files.
|
||||
#include <string>//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.
|
62
utils/ConfigBase/IConfigBase.h
Normal file
62
utils/ConfigBase/IConfigBase.h
Normal 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
|
||||
//Header file protection (preventing header files from being duplicated).
|
||||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
//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<memory>(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<IConfigBase> mIConfigBase;
|
||||
} ConfigBase;
|
||||
const char *GetConfigBaseModuleName(void);
|
||||
std::shared_ptr<IConfigBase> *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.
|
Loading…
Reference in New Issue
Block a user