18

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.

SingerOfTheFall
  • 28,216
  • 8
  • 63
  • 102
delephin
  • 1,033
  • 1
  • 8
  • 10

2 Answers2

27

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.)

Jesse Beder
  • 31,716
  • 20
  • 105
  • 143
1

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>());
}
Nejc Galof
  • 2,410
  • 3
  • 30
  • 63