api.cpp
Go to the documentation of this file.
00001 // a sketch of what the new API might look like
00002 
00003 #include "yaml-cpp-pm/yaml.h"
00004 #include <iostream>
00005 
00006 int main()
00007 {
00008         {
00009                 // test.yaml
00010                 // - foo
00011                 // - primes: [2, 3, 5, 7, 11]
00012                 //   odds: [1, 3, 5, 7, 9, 11]
00013                 // - [x, y]
00014                 
00015                 // move-like semantics
00016                 YAML::Value root = YAML::Parse("test.yaml");
00017                 
00018                 std::cout << root[0].as<std::string>(); // "foo"
00019                 std::cout << str(root[0]); // "foo", shorthand?
00020                 std::cout << root[1]["primes"][3].as<int>(); // "7"
00021                 std::cout << root[1]["odds"][6].as<int>(); // throws?
00022                 
00023                 root[2].push_back(5);
00024                 root[3] = "Hello, World";
00025                 root[0].reset();
00026                 root[0]["key"] = "value";
00027                 
00028                 std::cout << root;
00029                 // # not sure about formatting
00030                 // - {key: value}
00031                 // - primes: [2, 3, 5, 7, 11]
00032                 //   odds: [1, 3, 5, 7, 9, 11]
00033                 // - [x, y, 5]
00034                 // - Hello, World
00035         }
00036 
00037         {
00038                 // for all copy-like commands, think of python's "name/value" semantics
00039                 YAML::Value root = "Hello";  // Hello
00040                 root = YAML::Sequence();     // []
00041                 root[0] = 0;                 // [0]
00042                 root[2] = "two";             // [0, ~, two]  # forces root[1] to be initialized to null
00043 
00044                 YAML::Value other = root;    // both point to the same thing
00045                 other[0] = 5;                // now root[0] is 0 also
00046                 other.push_back(root);       // &1 [5, ~, two, *1]
00047                 other[3][0] = 0;             // &1 [0, ~, two, *1]   # since it's a true alias
00048                 other.push_back(Copy(root)); // &1 [0, ~, two, *1, &2 [0, ~, two, *2]]
00049                 other[4][0] = 5;             // &1 [0, ~, two, *1, &2 [5, ~, two, *2]]  # they're really different
00050         }
00051         
00052         {
00053                 YAML::Value node;            // ~
00054                 node[0] = 1;                 // [1]  # auto-construct a sequence
00055                 node["key"] = 5;             // {0: 1, key: 5}  # auto-turn it into a map
00056                 node.push_back(10);          // error, can't turn a map into a sequence
00057                 node.erase("key");           // {0: 1}  # still a map, even if we remove the key that caused the problem
00058                 node = "Hello";              // Hello  # assignment overwrites everything, so it's now just a plain scalar
00059         }
00060         
00061         {
00062                 YAML::Value map;             // ~
00063                 map[3] = 1;                  // {3: 1}  # auto-constructs a map, *not* a sequence
00064                 
00065                 YAML::Value seq;             // ~ 
00066                 seq = YAML::Sequence();      // []
00067                 seq[3] = 1;                  // [~, ~, ~, 1]
00068         }
00069         
00070         {
00071                 YAML::Value node;            // ~
00072                 node[0] = node;              // &1 [*1]  # fun stuff
00073         }
00074         
00075         {
00076                 YAML::Value node;
00077                 YAML::Value subnode = node["key"]; // 'subnode' is not instantiated ('node' is still null)
00078                 subnode = "value";           // {key: value}  # now it is
00079                 YAML::Value subnode2 = node["key2"];
00080                 node["key3"] = subnode2;     // subnode2 is still not instantiated, but node["key3"] is "pseudo" aliased to it
00081                 subnode2 = "monkey";         // {key: value, key2: &1 monkey, key3: *1}  # bam! it instantiates both
00082         }
00083         
00084         {
00085                 YAML::Value seq = YAML::Sequence();
00086                 seq[0] = "zero";             // [zero]
00087                 seq[1] = seq[0];             // [&1 zero, *1]
00088                 seq[0] = seq[1];             // [&1 zero, *1]  # no-op (they both alias the same thing, so setting them equal is nothing)
00089                 Is(seq[0], seq[1]);          // true
00090                 seq[1] = "one";              // [&1 one, *1]
00091                 UnAlias(seq[1]);             // [one, one]
00092                 Is(seq[0], seq[1]);          // false
00093         }
00094         
00095         {
00096                 YAML::Value root;
00097                 root.push_back("zero");
00098                 root.push_back("one");
00099                 root.push_back("two");
00100                 YAML::Value two = root[2];
00101                 root = "scalar";             // 'two' is still "two", even though 'root' is "scalar" (the sequence effectively no longer exists)
00102                 
00103                 // Note: in all likelihood, the memory for nodes "zero" and "one" is still allocated. How can it go away? Weak pointers?
00104         }
00105         
00106         {
00107                 YAML::Value root;            // ~
00108                 root[0] = root;              // &1 [*1]
00109                 root[0] = 5;                 // [5]
00110         }
00111         
00112         {
00113                 YAML::Value root;
00114                 YAML::Value key;
00115                 key["key"] = "value";
00116                 root[key] = key;             // &1 {key: value}: *1
00117         }
00118         
00119         {
00120                 YAML::Value root;
00121                 root[0] = "hi";
00122                 root[1][0] = "bye";
00123                 root[1][1] = root;           // &1 [hi, [bye, *1]]  # root
00124                 YAML::Value sub = root[1];   // &1 [bye, [hi, *1]]  # sub
00125                 root = "gone";               // [bye, gone]  # sub
00126         }
00127 
00128         return 0;
00129 }


upstream_src
Author(s):
autogenerated on Mon Oct 6 2014 10:27:39