LCOV - code coverage report
Current view: top level - include/base - config.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 2 2
Test Date: 2025-12-19 03:13:09 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  * @file config.hpp
       3              :  * @brief INI 格式配置文件解析器
       4              :  *
       5              :  * 提供线程安全的单例配置管理器,支持从 INI 文件加载配置,
       6              :  * 并按 section/key 方式访问配置项。
       7              :  */
       8              : 
       9              : #pragma once
      10              : 
      11              : #include <string>
      12              : #include <unordered_map>
      13              : #include <mutex>
      14              : 
      15              : namespace fix40 {
      16              : 
      17              : /**
      18              :  * @class Config
      19              :  * @brief 线程安全的 INI 配置文件解析器(单例模式)
      20              :  *
      21              :  * 支持标准 INI 格式:
      22              :  * - [section] 定义配置节
      23              :  * - key = value 定义配置项
      24              :  * - ; 或 # 开头的行为注释
      25              :  *
      26              :  * @note 该类为单例模式,通过 instance() 获取唯一实例
      27              :  *
      28              :  * @par 使用示例
      29              :  * @code
      30              :  * Config::instance().load("config.ini");
      31              :  * int port = Config::instance().get_int("server", "port", 9000);
      32              :  * @endcode
      33              :  */
      34              : class Config {
      35              : public:
      36              :     /**
      37              :      * @brief 获取 Config 单例实例
      38              :      * @return Config& 单例引用
      39              :      */
      40              :     static Config& instance();
      41              : 
      42              :     /**
      43              :      * @brief 从文件加载配置
      44              :      * @param filename 配置文件路径
      45              :      * @return true 加载成功
      46              :      * @return false 加载失败(文件不存在或无法打开)
      47              :      *
      48              :      * @note 调用此方法会清空之前加载的所有配置
      49              :      */
      50              :     bool load(const std::string& filename);
      51              : 
      52              :     /**
      53              :      * @brief 获取字符串类型的配置值
      54              :      * @param section 配置节名称
      55              :      * @param key 配置项名称
      56              :      * @param default_value 默认值,当配置项不存在时返回
      57              :      * @return std::string 配置值或默认值
      58              :      */
      59              :     std::string get(const std::string& section, const std::string& key, const std::string& default_value = "");
      60              : 
      61              :     /**
      62              :      * @brief 获取整数类型的配置值
      63              :      * @param section 配置节名称
      64              :      * @param key 配置项名称
      65              :      * @param default_value 默认值,当配置项不存在或转换失败时返回
      66              :      * @return int 配置值或默认值
      67              :      */
      68              :     int get_int(const std::string& section, const std::string& key, int default_value = 0);
      69              : 
      70              :     /**
      71              :      * @brief 获取浮点数类型的配置值
      72              :      * @param section 配置节名称
      73              :      * @param key 配置项名称
      74              :      * @param default_value 默认值,当配置项不存在或转换失败时返回
      75              :      * @return double 配置值或默认值
      76              :      */
      77              :     double get_double(const std::string& section, const std::string& key, double default_value = 0.0);
      78              : 
      79              : private:
      80            1 :     Config() = default;
      81            1 :     ~Config() = default;
      82              :     Config(const Config&) = delete;
      83              :     Config& operator=(const Config&) = delete;
      84              : 
      85              :     /**
      86              :      * @brief 去除字符串首尾空白字符
      87              :      * @param str 输入字符串
      88              :      * @return std::string 去除空白后的字符串
      89              :      */
      90              :     std::string trim(const std::string& str);
      91              : 
      92              :     /// 配置数据存储:section -> (key -> value)
      93              :     std::unordered_map<std::string, std::unordered_map<std::string, std::string>> data_;
      94              :     /// 保护 data_ 的互斥锁
      95              :     std::mutex mutex_;
      96              : };
      97              : 
      98              : } // namespace fix40 
        

Generated by: LCOV version 2.0-1