Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yaml-cpp Easiest way to iterate through a map with undefined values

Tags:

c++

yaml-cpp

I'd like to obtain every node in a map without knowing the keys.

My YAML looks like this:

characterType :
 type1 :
  attribute1 : something
  attribute2 : something
 type2 :
  attribute1 : something
  attribute2 : something

I don't know how many "type"s will be declared or what the name of those keys will be. That's why I'm trying to iterate through the map.

struct CharacterType{
  std::string attribute1;
  std::string attribute2;
};

namespace YAML{
  template<>
  struct convert<CharacterType>{
    static bool decode(const Node& node, CharacterType& cType){ 
       cType.attribute1 = node["attribute1"].as<std::string>();
       cType.attribute2 = node["attribute2"].as<std::string>();
       return true;
    }
  };
}

---------------------
std::vector<CharacterType> cTypeList;

for(YAML::const_iterator it=node["characterType"].begin(); it != node["characterType"].end(); ++it){
   cTypeList.push_back(it->as<CharacterType>());
}

The previous code doesn't give any trouble when compiling but then at execution time I get this error: terminate called after throwing an instance of YAML::TypedBadConversion<CharacterType>

I've also tried using a subindex instead of the iterator, getting the same error.

I'm sure I'm doing something wrong, I just can't see it.

like image 533
delephin Avatar asked Sep 11 '12 17:09

delephin


2 Answers

The answer from @jesse-beder is right, I give just another option with using range-based for loop like below:

for(const auto& characterType : node["characterType"]) {
   std::string key = characterType.first.as<std::string>();
   cTypeList.push_back(characterType.second.as<CharacterType>());
}
like image 31
Nejc Galof Avatar answered Nov 08 '22 18:11

Nejc Galof


When you iterate through a map, the iterator points to a key/value pair of nodes, not a single node. For example:

YAML::Node characterType = node["characterType"];
for(YAML::const_iterator it=characterType.begin();it != characterType.end();++it) {
   std::string key = it->first.as<std::string>();       // <- key
   cTypeList.push_back(it->second.as<CharacterType>()); // <- value
}

(The reason that your code compiled, even though your node is a map node, is that YAML::Node is effectively dynamically typed, so its iterator has to act (statically) as both a sequence iterator and a map iterator.)

like image 101
Jesse Beder Avatar answered Nov 08 '22 19:11

Jesse Beder