/** * * @file config.hpp * @author zxw * Copyright 2024, yjrobotics. All rights reserved. * * robotics * */ #pragma once //boost #include #include #include namespace robotics { namespace v3 { class config { public: /** * @brief 读取配置文件 * @tparam 返回值类型 * @param 配置段名称 * @param 配置键名称 * @param 默认值 * @param 文件名称 * @return 返回值 */ template static _Ret read(std::string const& section, std::string const& key, _Ret dc = _Ret(), std::string const& filename = "./config/config.ini") { boost::property_tree::ptree pt; try { boost::property_tree::ini_parser::read_ini(filename, pt); dc = pt.get<_Ret>(section + "." + key, dc); } catch (...) {} return dc; } /** * @brief 读取配置文件 * @param 配置段名称 * @param 配置键名称 * @param 默认值 * @param 文件名称 * @return 返回值 */ static std::string read(std::string const& section, std::string const& key, const char* dc = "", std::string const& filename = "./config/config.ini") { return read(section, key, dc, filename); } /** * @brief 写配置文件 * @tparam 值类型 * @param 返回值类型 * @param 配置段名称 * @param 配置值 * @param 文件名称 */ template static void write(std::string const& section, std::string const& key, _Value const& value, std::string const& filename = "./config/config.ini") { try { boost::property_tree::ptree pt; boost::property_tree::ini_parser::read_ini(filename, pt); pt.put(section + "." + key, boost::str(boost::format("%1%") % value)); boost::property_tree::ini_parser::write_ini(filename, pt); } catch (...) { } } }; } }