連結
-
toml++ - github
-
toml++ - 幫助文件
-
使用要求: c++ 17 及以上版本
-
toml語法-英文
-
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);
}