openmanus.cpp/include/config.h

72 lines
1.8 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#ifndef OPENMANUS_CONFIG_H
#define OPENMANUS_CONFIG_H
#include <string>
#include <map>
#include <fstream>
#include <sstream>
namespace openmanus {
/**
* @class Config
* @brief 配置文件读取类用于读取TOML格式的配置文件
*/
class Config {
public:
/**
* @brief 构造函数
* @param config_file 配置文件路径
*/
Config(const std::string& config_file = "config.toml");
/**
* @brief 加载配置文件
* @param config_file 配置文件路径
* @return 是否加载成功
*/
bool load(const std::string& config_file);
/**
* @brief 获取字符串配置项
* @param section 配置节
* @param key 配置键
* @param default_value 默认值
* @return 配置值
*/
std::string getString(const std::string& section, const std::string& key, const std::string& default_value = "") const;
/**
* @brief 获取整数配置项
* @param section 配置节
* @param key 配置键
* @param default_value 默认值
* @return 配置值
*/
int getInt(const std::string& section, const std::string& key, int default_value = 0) const;
/**
* @brief 获取浮点数配置项
* @param section 配置节
* @param key 配置键
* @param default_value 默认值
* @return 配置值
*/
double getDouble(const std::string& section, const std::string& key, double default_value = 0.0) const;
/**
* @brief 获取布尔配置项
* @param section 配置节
* @param key 配置键
* @param default_value 默认值
* @return 配置值
*/
bool getBool(const std::string& section, const std::string& key, bool default_value = false) const;
private:
std::map<std::string, std::map<std::string, std::string>> config_data_;
};
} // namespace openmanus
#endif // OPENMANUS_CONFIG_H