title: Design tips for data structures to interface with yaml-cpp?
body: <p>I'm getting started using YAML, and the yaml-cpp library to intepret my file.  I extended the "monsters" example with some information from my own project.  The code and yaml file are below, but here's my question first:</p>
  
  <p>Is it necessary to place all the data that I'll be getting out of the project into one massive structure?  In the monsters example, reading the values in from the document doc[i] was easy because it was a list of monsters.  In my example, I'll have some lists, but also scalars, etc.  The only way that I found to do this, is to make a list that technically only has one entry (i.e., there's a single '-' at the top of the file, and everything is indented into a block).  I think the answer is to take some of the content of the 'problemformulation' version of the overloaded >> operator, but I couldn't get it to work properly without having that content inside that function.  Any help or advice is appreciated.</p>
  
  <p>ea_test.cpp:</p>
  
  <pre><code>    #include "yaml-cpp/yaml.h"
      #include &lt;iostream&gt;
      #include &lt;fstream&gt;
      #include &lt;string&gt;
      #include &lt;vector&gt;
  
      struct Vec2{
    double x, y;
      };
  
      struct DecVar{
    std::string name;
    std::string tag;
        Vec2 range;
    std::string description;
      };
  
      struct ProblemFormulation{
    std::vector &lt;DecVar&gt; decvars;
    int numrealizations;
      };
  
      void operator &gt;&gt; (const YAML::Node&amp; node, Vec2&amp; v) {
    node[0] &gt;&gt; v.x;
    node[1] &gt;&gt; v.y;
      }
  
      void operator &gt;&gt; (const YAML::Node&amp; node, DecVar&amp; decvar){
    node["name"] &gt;&gt; decvar.name;
    node["tag"] &gt;&gt; decvar.tag;
        node["range"] &gt;&gt; decvar.range;
    node["description"] &gt;&gt; decvar.description;
      }
  
      void operator &gt;&gt; (const YAML::Node&amp; node, ProblemFormulation&amp; problemformulation){
        node["realizations"] &gt;&gt; problemformulation.numrealizations;
        std::cout &lt;&lt; " read realizations!" &lt;&lt; std::endl; 
        const YAML::Node&amp; decvarNode = node["decisions"];
        for (unsigned int i = 0; i &lt; decvarNode.size(); i++)
        {
      DecVar decvar;
      decvarNode[i] &gt;&gt; decvar;
          problemformulation.decvars.push_back(decvar);
        }
      }
  
      int main()
      {
          std::ifstream fin("./ea.yaml");
          YAML::Parser parser(fin);
          YAML::Node doc;
          parser.GetNextDocument(doc);
          std::cout &lt;&lt; "entering loop" &lt;&lt; std::endl;
  
          ProblemFormulation problemformulation;
  
          for (unsigned int i = 0; i &lt; doc.size(); i++)
          {
                doc[i] &gt;&gt; problemformulation;
          }
          return 0;
      }
  </code></pre>
  
  <p>And, ea.yaml:</p>
  
  <pre><code>    -
        realizations: 10
        decisions:
          - name: reservoir
            tag: res_tag
            range: [0, 1.0]
            description: &gt;
              This is a description.
          - name: flow
            tag: flow_tag
            range: [0, 2.0]
            description: &gt;
              This is how much flow is in the system.
  </code></pre>
  
  <p>Thanks in advance for your help and tips!</p>
  
  <p>Edit: I will probably only be running one yaml document, and there's only one problemformulation object that will ever be created.  My code adapts what you'd do for a list, but only does it once.  I would like to know the proper way to, "just do it once", since I think that would be cleaner and make a better looking YAML file (without all the things indented one block for no reason).</p>