api.cpp
Go to the documentation of this file.
1 // a sketch of what the new API might look like
2 
3 #include "yaml-cpp-pm/yaml.h"
4 #include <iostream>
5 
6 int main()
7 {
8  {
9  // test.yaml
10  // - foo
11  // - primes: [2, 3, 5, 7, 11]
12  // odds: [1, 3, 5, 7, 9, 11]
13  // - [x, y]
14 
15  // move-like semantics
16  YAML::Value root = YAML::Parse("test.yaml");
17 
18  std::cout << root[0].as<std::string>(); // "foo"
19  std::cout << str(root[0]); // "foo", shorthand?
20  std::cout << root[1]["primes"][3].as<int>(); // "7"
21  std::cout << root[1]["odds"][6].as<int>(); // throws?
22 
23  root[2].push_back(5);
24  root[3] = "Hello, World";
25  root[0].reset();
26  root[0]["key"] = "value";
27 
28  std::cout << root;
29  // # not sure about formatting
30  // - {key: value}
31  // - primes: [2, 3, 5, 7, 11]
32  // odds: [1, 3, 5, 7, 9, 11]
33  // - [x, y, 5]
34  // - Hello, World
35  }
36 
37  {
38  // for all copy-like commands, think of python's "name/value" semantics
39  YAML::Value root = "Hello"; // Hello
40  root = YAML::Sequence(); // []
41  root[0] = 0; // [0]
42  root[2] = "two"; // [0, ~, two] # forces root[1] to be initialized to null
43 
44  YAML::Value other = root; // both point to the same thing
45  other[0] = 5; // now root[0] is 0 also
46  other.push_back(root); // &1 [5, ~, two, *1]
47  other[3][0] = 0; // &1 [0, ~, two, *1] # since it's a true alias
48  other.push_back(Copy(root)); // &1 [0, ~, two, *1, &2 [0, ~, two, *2]]
49  other[4][0] = 5; // &1 [0, ~, two, *1, &2 [5, ~, two, *2]] # they're 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 caused the problem
58  node = "Hello"; // Hello # assignment overwrites everything, so it's now just a plain scalar
59  }
60 
61  {
62  YAML::Value map; // ~
63  map[3] = 1; // {3: 1} # auto-constructs a map, *not* a sequence
64 
65  YAML::Value seq; // ~
66  seq = YAML::Sequence(); // []
67  seq[3] = 1; // [~, ~, ~, 1]
68  }
69 
70  {
71  YAML::Value node; // ~
72  node[0] = node; // &1 [*1] # fun stuff
73  }
74 
75  {
76  YAML::Value node;
77  YAML::Value subnode = node["key"]; // 'subnode' is not instantiated ('node' is still null)
78  subnode = "value"; // {key: value} # now it is
79  YAML::Value subnode2 = node["key2"];
80  node["key3"] = subnode2; // subnode2 is still not instantiated, but node["key3"] is "pseudo" aliased to it
81  subnode2 = "monkey"; // {key: value, key2: &1 monkey, key3: *1} # bam! it instantiates both
82  }
83 
84  {
85  YAML::Value seq = YAML::Sequence();
86  seq[0] = "zero"; // [zero]
87  seq[1] = seq[0]; // [&1 zero, *1]
88  seq[0] = seq[1]; // [&1 zero, *1] # no-op (they both alias the same thing, so setting them equal is nothing)
89  Is(seq[0], seq[1]); // true
90  seq[1] = "one"; // [&1 one, *1]
91  UnAlias(seq[1]); // [one, one]
92  Is(seq[0], seq[1]); // false
93  }
94 
95  {
96  YAML::Value root;
97  root.push_back("zero");
98  root.push_back("one");
99  root.push_back("two");
100  YAML::Value two = root[2];
101  root = "scalar"; // 'two' is still "two", even though 'root' is "scalar" (the sequence effectively no longer exists)
102 
103  // Note: in all likelihood, the memory for nodes "zero" and "one" is still allocated. How can it go away? Weak pointers?
104  }
105 
106  {
107  YAML::Value root; // ~
108  root[0] = root; // &1 [*1]
109  root[0] = 5; // [5]
110  }
111 
112  {
113  YAML::Value root;
114  YAML::Value key;
115  key["key"] = "value";
116  root[key] = key; // &1 {key: value}: *1
117  }
118 
119  {
120  YAML::Value root;
121  root[0] = "hi";
122  root[1][0] = "bye";
123  root[1][1] = root; // &1 [hi, [bye, *1]] # root
124  YAML::Value sub = root[1]; // &1 [bye, [hi, *1]] # sub
125  root = "gone"; // [bye, gone] # sub
126  }
127 
128  return 0;
129 }
testing::internal::string
::std::string string
Definition: gtest.h:1979
yaml.h
main
int main()
Definition: api.cpp:6
YAML_PM::Value
@ Value
Definition: emittermanip.h:58


mp2p_icp
Author(s):
autogenerated on Thu Dec 26 2024 03:48:10