File: array_example.cpp

package info (click to toggle)
toml11 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,464 kB
  • sloc: cpp: 38,446; makefile: 8; sh: 5
file content (152 lines) | stat: -rw-r--r-- 6,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <toml.hpp>

#include <iostream>

#include <cassert>

int main()
{
    const auto root = toml::parse("array_example.toml");

    // using member functions
    {
        assert(root.at("integers").is_array());
        assert(root.at("integers").at(0).as_integer() == 1);
        assert(root.at("integers").at(1).as_integer() == 2);
        assert(root.at("integers").at(2).as_integer() == 3);

        assert(root.at("colors").at(0).as_string() == "red");
        assert(root.at("colors").at(1).as_string() == "yellow");
        assert(root.at("colors").at(2).as_string() == "green");

        assert(root.at("nested_arrays_of_ints").at(0).at(0).as_integer() == 1);
        assert(root.at("nested_arrays_of_ints").at(0).at(1).as_integer() == 2);
        assert(root.at("nested_arrays_of_ints").at(1).at(0).as_integer() == 3);
        assert(root.at("nested_arrays_of_ints").at(1).at(1).as_integer() == 4);
        assert(root.at("nested_arrays_of_ints").at(1).at(2).as_integer() == 5);

        assert(root.at("nested_mixed_array").at(0).at(0).as_integer() == 1);
        assert(root.at("nested_mixed_array").at(0).at(1).as_integer() == 2);
        assert(root.at("nested_mixed_array").at(1).at(0).as_string() == "a");
        assert(root.at("nested_mixed_array").at(1).at(1).as_string() == "b");
        assert(root.at("nested_mixed_array").at(1).at(2).as_string() == "c");

        assert(root.at("string_array").at(0).as_string() == "all");
        assert(root.at("string_array").at(1).as_string() == "strings");
        assert(root.at("string_array").at(2).as_string() == "are the same");
        assert(root.at("string_array").at(3).as_string() == "type");

        assert(root.at("numbers").at(0).as_floating() == std::stod("0.1"));
        assert(root.at("numbers").at(1).as_floating() == std::stod("0.2"));
        assert(root.at("numbers").at(2).as_floating() == std::stod("0.5"));
        assert(root.at("numbers").at(3).as_integer()  == 1);
        assert(root.at("numbers").at(4).as_integer()  == 2);
        assert(root.at("numbers").at(5).as_integer()  == 5);

        assert(root.at("contributors").at(0).as_string() == "Foo Bar <foo@example.com>");
        assert(root.at("contributors").at(1).at("name" ).as_string() == "Baz Qux");
        assert(root.at("contributors").at(1).at("email").as_string() == "bazqux@example.com");
        assert(root.at("contributors").at(1).at("url"  ).as_string() == "https://example.com/bazqux");
    }

    // using toml::find
    {
        assert(toml::find<int>(root, "integers", 0) == 1);
        assert(toml::find<int>(root, "integers", 1) == 2);
        assert(toml::find<int>(root, "integers", 2) == 3);

        const auto integers = toml::find<std::vector<int>>(root, "integers");
        assert(integers.at(0) == 1);
        assert(integers.at(1) == 2);
        assert(integers.at(2) == 3);

        assert(toml::find<std::string>(root, "colors", 0) == "red");
        assert(toml::find<std::string>(root, "colors", 1) == "yellow");
        assert(toml::find<std::string>(root, "colors", 2) == "green");

        const auto colors = toml::find<std::array<std::string, 3>>(root, "colors");
        assert(colors.at(0) == "red");
        assert(colors.at(1) == "yellow");
        assert(colors.at(2) == "green");

        assert(toml::find<int>(root, "nested_arrays_of_ints", 0, 0) == 1);
        assert(toml::find<int>(root, "nested_arrays_of_ints", 0, 1) == 2);
        assert(toml::find<int>(root, "nested_arrays_of_ints", 1, 0) == 3);
        assert(toml::find<int>(root, "nested_arrays_of_ints", 1, 1) == 4);
        assert(toml::find<int>(root, "nested_arrays_of_ints", 1, 2) == 5);

        const auto nested_arrays_of_ints = toml::find<std::vector<std::vector<int>>>(root, "nested_arrays_of_ints");
        assert(nested_arrays_of_ints.at(0).at(0) == 1);
        assert(nested_arrays_of_ints.at(0).at(1) == 2);
        assert(nested_arrays_of_ints.at(1).at(0) == 3);
        assert(nested_arrays_of_ints.at(1).at(1) == 4);
        assert(nested_arrays_of_ints.at(1).at(2) == 5);

        assert(toml::find<int        >(root, "nested_mixed_array", 0, 0) == 1);
        assert(toml::find<int        >(root, "nested_mixed_array", 0, 1) == 2);
        assert(toml::find<std::string>(root, "nested_mixed_array", 1, 0) == "a");
        assert(toml::find<std::string>(root, "nested_mixed_array", 1, 1) == "b");
        assert(toml::find<std::string>(root, "nested_mixed_array", 1, 2) == "c");

        const auto nested_mixed_array = toml::find<
                std::pair<std::vector<int>, std::vector<std::string>>
            >(root, "nested_mixed_array");

        assert(nested_mixed_array.first .at(0) == 1);
        assert(nested_mixed_array.first .at(1) == 2);
        assert(nested_mixed_array.second.at(0) == "a");
        assert(nested_mixed_array.second.at(1) == "b");
        assert(nested_mixed_array.second.at(2) == "c");

        assert(toml::find<std::string>(root, "string_array", 0) == "all");
        assert(toml::find<std::string>(root, "string_array", 1) == "strings");
        assert(toml::find<std::string>(root, "string_array", 2) == "are the same");
        assert(toml::find<std::string>(root, "string_array", 3) == "type");

        const auto numbers = toml::find<
            std::tuple<double, double, double, int, int, int>
            >(root, "numbers");

        assert(std::get<0>(numbers) == std::stod("0.1"));
        assert(std::get<1>(numbers) == std::stod("0.2"));
        assert(std::get<2>(numbers) == std::stod("0.5"));
        assert(std::get<3>(numbers) == 1);
        assert(std::get<4>(numbers) == 2);
        assert(std::get<5>(numbers) == 5);

        struct contributor_t
        {
            contributor_t(const toml::value& v)
            {
                if(v.is_string())
                {
                    name = v.as_string();
                }
                else
                {
                    assert(v.is_table());
                    name  = toml::find<std::string>(v, "name");
                    email = toml::find_or(v, "email", std::string(""));
                    url   = toml::find_or(v, "url"  , std::string(""));
                }
            }

            std::string name;
            std::string email;
            std::string url;
        };

        const auto contributors = toml::find<std::vector<contributor_t>>(root, "contributors");

        assert(contributors.at(0).name  == "Foo Bar <foo@example.com>");
        assert(contributors.at(0).email == "");
        assert(contributors.at(0).url   == "");
        assert(contributors.at(1).name  == "Baz Qux");
        assert(contributors.at(1).email == "bazqux@example.com");
        assert(contributors.at(1).url   == "https://example.com/bazqux");
    }

    std::cout << "ok." << std::endl;

    return 0;
}