关于c++使用toml plusplus(俗称toml++)的使用(3)
链接#
-
使用要求: c++ 17 及以上版本
toml 完整版读取与写入#
toml读取#
toml写入#
目标: 数组的写入#
文件内容#
[NET_INTERFACE]
bool = false
bool_arr = [ false, false ]
complex_arr = [ false, '456', 123 ]
integer = 1234567890
integer_arr = [ 1, 0 ]
str_arr = [ '1', '0' ]
string = 'this is a string'
[[fruit]]
kilograms = 2
name = 'banana'
[[fruit]]
kilograms = 1
name = 'apple'
[[fruit]]
kilograms = 3
name = 'blueberry'
关键代码#
arrayInsert
函数定义
/// @brief 数组的插入
/// @tparam T
/// @param arr
/// @param value
template<typename T>
static void arrayInsert(toml::array& arr, const T& value)
{
arr.insert(arr.begin(), toml::value<T>(value));
}
写文件代码#
NetInterfaceConfigPropertyName propertyName;
toml::table rootNode{};
tableNodeInsert<std::string>(rootNode, "string", "this is a string");
tableNodeInsert<bool>(rootNode, "bool", false);
tableNodeInsert<int64_t>(rootNode, "integer", 1234567890);
toml::table netInterfaceNode{};
/// 创建[NET_INTERFACE]
netInterfaceNode.insert_or_assign(propertyName.m_tableName, rootNode);
/// 下一是一个数组的写入
{
/// 定义了水果信息
struct FruitInfo
{
/// 水果的名称
std::string m_name{""};
/// 水果的重量
int64_t m_kilograms{0};
};
/// <key-水果的名称,value-水果信息>
using HashFruitInfo = std::unordered_map<std::string, FruitInfo>;
HashFruitInfo fruitInfoHash{{"apple", {"apple", 1}}, {"banana", {"banana", 2}}, {"blueberry", {"blueberry", 3}}};
toml::array tmpArr;
// for(auto& [key, value] : tmpArr)
for (HashFruitInfo::iterator it = fruitInfoHash.begin(); it != fruitInfoHash.end(); ++ it)
{
toml::table tblTmp{};
tableNodeInsert<std::string>(tblTmp, "name", it->first);
tableNodeInsert<int64_t>(tblTmp, "kilograms", it->second.m_kilograms);
tmpArr.insert(tmpArr.begin(), tblTmp);
}
/// 将数组中的内容
netInterfaceNode.insert_or_assign("fruit", tmpArr);
}
/// 数组的写入
{
/// 整数数组
toml::array tmpArrInt;
/// 字符串数组
toml::array tmpArrStr;
/// bool数组
toml::array tmpArrBool;
for (int index = 0; index < 2; ++ index)
{
arrayInsert<int64_t>(tmpArrInt, index);
arrayInsert<std::string>(tmpArrStr, std::to_string(index));
arrayInsert<bool>(tmpArrBool, false);
}
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("integer_arr", tmpArrInt);
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("str_arr", tmpArrStr);
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("bool_arr", tmpArrBool);
/// 混合数组
toml::array tmpArrComplex;
arrayInsert<int64_t>(tmpArrComplex, 123);
arrayInsert<std::string>(tmpArrComplex, "456");
arrayInsert<bool>(tmpArrComplex, false);
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("complex_arr", tmpArrComplex);
}
/// 使用流打开文件
std::ofstream tomlFile(std::string{"example.toml"}, std::ios::out | std::ios::trunc);
if (tomlFile.is_open())
{
// 使用 toml++ 的内置方法将 TOML 值写入文件
tomlFile << netInterfaceNode;
tomlFile.close();
}
else
{
/// TODO
}
数组写入的关键代码#
/// 数组的写入
{
/// 整数数组
toml::array tmpArrInt;
/// 字符串数组
toml::array tmpArrStr;
/// bool数组
toml::array tmpArrBool;
for (int index = 0; index < 2; ++ index)
{
arrayInsert<int64_t>(tmpArrInt, index);
arrayInsert<std::string>(tmpArrStr, std::to_string(index));
arrayInsert<bool>(tmpArrBool, false);
}
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("integer_arr", tmpArrInt);
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("str_arr", tmpArrStr);
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("bool_arr", tmpArrBool);
/// 混合数组
toml::array tmpArrComplex;
arrayInsert<int64_t>(tmpArrComplex, 123);
arrayInsert<std::string>(tmpArrComplex, "456");
arrayInsert<bool>(tmpArrComplex, false);
netInterfaceNode[propertyName.m_tableName].as_table()->insert_or_assign("complex_arr", tmpArrComplex);
}
作者: mohist
出处:https://www.cnblogs.com/pandamohist/p/18366396
版权:本站使用「CC BY 4.0」创作共享协议,未经作者同意,请勿转载;若经同意转载,请在文章明显位置注明作者和出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2021-08-18 cmake之if
2021-08-18 cmake之譬判断cmake的版本