1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- /**
- *
- * @file config.hpp
- * @author zxw
- * Copyright 2024, yjrobotics. All rights reserved.
- *
- * robotics
- *
- */
- #pragma once
- //boost
- #include <boost/format.hpp>
- #include <boost/property_tree/ptree.hpp>
- #include <boost/property_tree/ini_parser.hpp>
- namespace robotics {
- namespace v3 {
- class config {
- public:
- /**
- * @brief 读取配置文件
- * @tparam 返回值类型
- * @param 配置段名称
- * @param 配置键名称
- * @param 默认值
- * @param 文件名称
- * @return 返回值
- */
- template<typename _Ret>
- 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<std::string>(section, key, dc, filename);
- }
- /**
- * @brief 写配置文件
- * @tparam 值类型
- * @param 返回值类型
- * @param 配置段名称
- * @param 配置值
- * @param 文件名称
- */
- template<typename _Value>
- 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 (...) {
- }
- }
- };
- }
- }
|