api.cpp (4215B)
1 // a sketch of what the new API might look like 2 3 #include "yaml-cpp/yaml.h" 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 { 64 YAML::Value map; // ~ 65 map[3] = 1; // {3: 1} # auto-constructs a map, *not* a sequence 66 67 YAML::Value seq; // ~ 68 seq = YAML::Sequence(); // [] 69 seq[3] = 1; // [~, ~, ~, 1] 70 } 71 72 { 73 YAML::Value node; // ~ 74 node[0] = node; // &1 [*1] # fun stuff 75 } 76 77 { 78 YAML::Value node; 79 YAML::Value subnode = 80 node["key"]; // 'subnode' is not instantiated ('node' is still null) 81 subnode = "value"; // {key: value} # now it is 82 YAML::Value subnode2 = node["key2"]; 83 node["key3"] = subnode2; // subnode2 is still not instantiated, but 84 // node["key3"] is "pseudo" aliased to it 85 subnode2 = "monkey"; // {key: value, key2: &1 monkey, key3: *1} # bam! it 86 // instantiates both 87 } 88 89 { 90 YAML::Value seq = YAML::Sequence(); 91 seq[0] = "zero"; // [zero] 92 seq[1] = seq[0]; // [&1 zero, *1] 93 seq[0] = seq[1]; // [&1 zero, *1] # no-op (they both alias the same thing, 94 // so setting them equal is nothing) 95 Is(seq[0], seq[1]); // true 96 seq[1] = "one"; // [&1 one, *1] 97 UnAlias(seq[1]); // [one, one] 98 Is(seq[0], seq[1]); // false 99 } 100 101 { 102 YAML::Value root; 103 root.push_back("zero"); 104 root.push_back("one"); 105 root.push_back("two"); 106 YAML::Value two = root[2]; 107 root = "scalar"; // 'two' is still "two", even though 'root' is "scalar" 108 // (the sequence effectively no longer exists) 109 110 // Note: in all likelihood, the memory for nodes "zero" and "one" is still 111 // allocated. How can it go away? Weak pointers? 112 } 113 114 { 115 YAML::Value root; // ~ 116 root[0] = root; // &1 [*1] 117 root[0] = 5; // [5] 118 } 119 120 { 121 YAML::Value root; 122 YAML::Value key; 123 key["key"] = "value"; 124 root[key] = key; // &1 {key: value}: *1 125 } 126 127 { 128 YAML::Value root; 129 root[0] = "hi"; 130 root[1][0] = "bye"; 131 root[1][1] = root; // &1 [hi, [bye, *1]] # root 132 YAML::Value sub = root[1]; // &1 [bye, [hi, *1]] # sub 133 root = "gone"; // [bye, gone] # sub 134 } 135 136 return 0; 137 }