TOML for Modern C++

栏目: IT技术 · 发布时间: 5年前

toml++ works whether you have exceptions enabled or not. For the most part the usage is the same, the main difference being how parsing errors are reported to the caller. When exceptions are enabled a successful call to a parsing function simply returns atoml::table, whereas a failed call sees atoml::parse_error thrown directly from the site of the error:

#include <iostream>
#include <fstream> //required for parse_file()
#include <toml++/toml.h>
using namespace std::string_view_literals;

int main()
{
    toml::table tbl;
    try
    {
        tbl = toml::parse_file("configuration.toml");
    }
    catch (const toml::parse_error& err)
    {
        std::cerr
            << "Error parsing file '"sv << *err.source().path
            << "':\n"sv << err.description()
            << "\n  ("sv << err.source().begin << ")"sv
            << std::endl;
        return 1;
    }
    
    do_stuff_with_your_config(tbl);
    return 0;
}

When exceptions are disabled parsing functions return atoml::parse_result instead and it is up to the caller to check if parsing has been successful by examining the return value:

#include <iostream>
#include <fstream> //required for parse_file()
#include <toml++/toml.h>
using namespace std::string_view_literals;

int main()
{
    toml::parse_result tbl = toml::parse_file("configuration.toml");
    if (!tbl)
    {
        std::cerr
            << "Error parsing file '"sv << *tbl.error().source().path
            << "':\n"sv << tbl.error().description()
            << "\n  ("sv << tbl.error().source().begin << ")"sv
            << std::endl;
        return 1;
    }
    
    do_stuff_with_your_config(tbl); //toml::parse_result is convertible to toml::table
    return 0;
}

Instances oftoml::parse_error can be printed directly to streams:

try
{
    auto tbl = toml::parse("enabled = trUe"sv); //fails; TOML booleans are case-sensitive
}
catch (const toml::parse_error & err)
{
    std::cerr << "Parsing failed:\n"sv << err << std::endl;
    return 1;
}
Parsing failed:
Encountered unexpected character while parsing boolean; expected 'true', saw 'trU'
    (error occurred at line 1, column 13)

If the default error formatting is not be suitable for your use-case you can access the error'stoml::source_region and description directly from the error object (as in the examples above).

Parsing TOML directly from strings and streams

Strings and std::istreams can be read directly usingtoml::parse():

#include <iostream>
#include <sstream>
#include <toml++/toml.h>
using namespace std::string_view_literals;

int main()
{
    static constexpr auto source = R"(
        [library]
        name = "toml++"
        version = "0.1.0"
        authors = ["Mark Gillard <mark@notarealwebsite.com>"]
    
        [dependencies]
        cpp = 17
    )"sv;
    
    // parse directly from a string view:
    {
        auto tbl = toml::parse(source);
        std::cout << tbl << std::endl;
    }
    
    // parse from a string stream:
    {
        std::stringstream ss{ std::string{ source } };
        auto tbl = toml::parse(ss);
        std::cout << tbl << std::endl;
    }
    
    return 0;
}
[dependencies]
cpp = 17

[library]
authors = ["Mark Gillard <mark@notarealwebsite.com>"]
name = "toml++"
version = "0.1.0"

... exactly as above, but twice

Traversing and manipulating data

A TOML document is a tree of values, arrays and tables, represented as thetoml::value,toml::array andtoml::table, respectively. All three inherit fromtoml::node, and can be easily accessed via thetoml::node_view:

#include <iostream>
#include <toml++/toml.h>
using namespace std::string_view_literals;

int main()
{
    static constexpr auto source = R"(
        numbers = [ 1, 2, 3, "four", 5.0 ]
        vegetables = [ "tomato", "onion", "mushroom", "lettuce" ]
        minerals = [ "quartz", "iron", "copper", "diamond" ]

        [animals]
        cats = [ "tiger", "lion", "puma" ]
        birds = [ "macaw", "pigeon", "canary" ]
        fish = [ "salmon", "trout", "carp" ]

    )"sv;
    auto tbl = toml::parse(source);
    
    // get a view of the element 'numbers'
    auto numbers = tbl["numbers"];
    std::cout << "table has 'numbers': "sv << !!numbers << std::endl;
    std::cout << "numbers is a: "sv << numbers.type() << std::endl;
    std::cout << "numbers: "sv << numbers << std::endl;

    // get the underlying array object to do some more advanced stuff
    if (auto arr = numbers.as_array())
    {
        for (auto& elem : *arr)
        {
            // visitation helps deal with the polymorphic nature of TOML data
            elem.visit([=](auto&& el) noexcept
            {
                if constexpr (toml::is_number<decltype(el)>)
                    (*el)++;
                else if constexpr (toml::is_string<decltype(el)>)
                    el = "five"sv;
            });
        }
        
        // arrays are very similar to std::vector
        arr->push_back(7);
        arr->emplace_back<toml::array>(8, 9);
        std::cout << "numbers: "sv << numbers << std::endl;
    }

    // node-views can be chained to quickly query deeper
    std::cout << "cats: "sv << tbl["animals"]["cats"] << std::endl;
    std::cout << "fish[1]: "sv << tbl["animals"]["fish"][1] << std::endl;
    
    // ...even if the element doesn't exist
    std::cout << "dinosaurs: "sv << tbl["animals"]["dinosaurs"] << std::endl; //no dinosaurs :(

    return 0;
}
table has 'numbers': true
numbers is an: array
numbers: [1, 2, 3, "four", 5.0]
numbers: [2, 3, 4, "five", 6.0, 7, [8, 9]]
cats: ["tiger", "lion", "puma"]
fish[1]: "trout"
dinosaurs:

Serializing as TOML and JSON

#include <iostream>
#include <toml++/toml.h>

int main()
{
    auto tbl = toml::table{{
        { "lib", "toml++" },
        { "cpp", toml::array{ 17, 20, "and beyond" } },
        { "toml", toml::array{ "0.5.0", "and beyond" } },
        { "repo", "https://github.com/marzer/tomlplusplus/" },
        { "author", toml::table{{
                { "name", "Mark Gillard" },
                { "github", "https://github.com/marzer" },
                { "twitter", "https://twitter.com/marzer8789" }
            }}
        },
    }};

    std::cout << "###### TOML ######"sv << std::endl;
    std::cout << tbl << std::endl << std::endl;
    
    std::cout << "###### JSON ######"sv << std::endl;
    std::cout << toml::json_formatter{ tbl } << std::endl;
    return 0;
}
###### TOML ######
cpp = [17, 20, "and beyond"]
lib = "toml++"
repo = "https://github.com/marzer/tomlplusplus/"
toml = ["0.5.0", "and beyond"]

[author]
github = "https://github.com/marzer"
name = "Mark Gillard"
twitter = "https://twitter.com/marzer8789"

###### JSON ######
{
    "author" : {
        "github" : "https://github.com/marzer",
        "name" : "Mark Gillard",
        "twitter" : "https://twitter.com/marzer8789"
    },
    "cpp" : [
        17,
        20,
        "and beyond"
    ],
    "lib" : "toml++",
    "repo" : "https://github.com/marzer/tomlplusplus/",
    "toml" : [
        "0.5.0",
        "and beyond"
    ]
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

UML基础与Rose建模案例

UML基础与Rose建模案例

吴建 / 人民邮电出版社 / 2004-10 / 29.00元

《UML 基础与Rose建模案例》介绍了用UML(统一建模语言)进行软件建模的基础知识以及Rational Rose工具的使用方法,其中,前8章是基础部分,对软件工程思想、UML的相关概念、Rational Rose工具以及RUP软件过程等进行了详细的介绍;后3章是案例部分,通过3个综合实例,对UML建模(以Rose为实现工具)的全过程进行了剖析;最后的附录中给出了UML中常用的术语、标准元素和元......一起来看看 《UML基础与Rose建模案例》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具