Add more examles, including using range-based for loops for iterating over `YAML::Node`s (link to my examples)
I really think you need some more examples, especially how to iterate over Sequences (lists) and Maps using range-based for loops.
I suggest you link to my examples here, in your readme: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/cpp/yaml-cpp_lib_demo.cpp
I'll add range-based for loop examples to my demo code above tonight, including like this, for example, to iterate over a Map:
YAML::Node data_phi = root_node["data"][6];
assert(data_phi.IsMap());
for (const std::pair<YAML::Node, YAML::Node>& keyValue : data_phi)
{
std::string key = keyValue.first.as<std::string>();
std::string value = keyValue.second.as<std::string>();
printf("key = %-15s value = %-15s\n", key.c_str(), value.c_str());
}
Done: my code example has been updated with a bunch of different iteration techniques.
I would also like to suggest adding examples on how to do value type checking. ElectricRCAircraftGuy's example shows how to do this by catching exceptions from bad conversions and that's just what I needed.
Here's that part that @bedaro mentions, from my example:
// Do some math on a value from a map node
double value = data_phi["value"].as<double>();
printf("phi value = %f; phi value*2.1 = %f\n", value, value*2.1);
// Try to cast a string as a double. Does an exception get thrown?
// Ans: YES! An exception
// gets thrown! So, if you want to be "safe" here and explicitly handle the error, you need
// to use a try/catch block. Here is the result of the following line:
//
// terminate called after throwing an instance of 'YAML::TypedBadConversion<double>'
// what(): yaml-cpp: error at line 65, column 11: bad conversion
// Aborted (core dumped)
//
// value = data_phi["name"].as<double>(); // uncomment this to watch it crash the program here,
// and core dump
// Use a try/catch block instead to catch the thrown exception!
try
{
value = data_phi["name"].as<double>();
}
catch (const std::exception& e)
{
std::cout << "Unable to cast yaml string to double.\n"
<< " Exception caught: e.what() = " << e.what() << "\n";
}