| 4 | #include <iostream> |
| 5 | |
| 6 | int main() { |
| 7 | { |
| 8 | // test.yaml |
| 9 | // - foo |
| 10 | // - primes: [2, 3, 5, 7, 11] |
| 11 | // odds: [1, 3, 5, 7, 9, 11] |
| 12 | // - [x, y] |
| 13 | |
| 14 | // move-like semantics |
| 15 | YAML::Value root = YAML::Parse("test.yaml"); |
| 16 | |
| 17 | std::cout << root[0].as<std::string>(); // "foo" |
| 18 | std::cout << str(root[0]); // "foo", shorthand? |
| 19 | std::cout << root[1]["primes"][3].as<int>(); // "7" |
| 20 | std::cout << root[1]["odds"][6].as<int>(); // throws? |
| 21 | |
| 22 | root[2].push_back(5); |
| 23 | root[3] = "Hello, World"; |
| 24 | root[0].reset(); |
| 25 | root[0]["key"] = "value"; |
| 26 | |
| 27 | std::cout << root; |
| 28 | // # not sure about formatting |
| 29 | // - {key: value} |
| 30 | // - primes: [2, 3, 5, 7, 11] |
| 31 | // odds: [1, 3, 5, 7, 9, 11] |
| 32 | // - [x, y, 5] |
| 33 | // - Hello, World |
| 34 | } |
| 35 | |
| 36 | { |
| 37 | // for all copy-like commands, think of python's "name/value" semantics |
| 38 | YAML::Value root = "Hello"; // Hello |
| 39 | root = YAML::Sequence(); // [] |
| 40 | root[0] = 0; // [0] |
| 41 | root[2] = "two"; // [0, ~, two] # forces root[1] to be initialized to null |
| 42 | |
| 43 | YAML::Value other = root; // both point to the same thing |
| 44 | other[0] = 5; // now root[0] is 0 also |
| 45 | other.push_back(root); // &1 [5, ~, two, *1] |
| 46 | other[3][0] = 0; // &1 [0, ~, two, *1] # since it's a true alias |
| 47 | other.push_back(Copy(root)); // &1 [0, ~, two, *1, &2 [0, ~, two, *2]] |
| 48 | other[4][0] = 5; // &1 [0, ~, two, *1, &2 [5, ~, two, *2]] # they're |
| 49 | // really different |
| 50 | } |
| 51 | |
| 52 | { |
| 53 | YAML::Value node; // ~ |
| 54 | node[0] = 1; // [1] # auto-construct a sequence |
| 55 | node["key"] = 5; // {0: 1, key: 5} # auto-turn it into a map |
| 56 | node.push_back(10); // error, can't turn a map into a sequence |
| 57 | node.erase("key"); // {0: 1} # still a map, even if we remove the key that |
| 58 | // caused the problem |
| 59 | node = "Hello"; // Hello # assignment overwrites everything, so it's now |
| 60 | // just a plain scalar |
| 61 | } |
| 62 | |
| 63 | { |