yamp-cpp搞砸了导入std :: map <std :: string,int =“”>数据

时间:2018-07-06 00:15:18

标签: c++ yaml yaml-cpp

所以我有这个yaml-cpp代码:

namespace YAML {
template <>
struct convert<SpriteMetaData> {
  static Node encode(const SpriteMetaData& spriteMetaData) {
    Node yamldata;

    yamldata["width"] = spriteMetaData.height;
    yamldata["height"] = spriteMetaData.width;
    yamldata["spritesPerRow"] = spriteMetaData.spritesPerRow;
    yamldata["sprites"] = spriteMetaData.sprites;
    yamldata["filePath"] = spriteMetaData.filePath;

    return yamldata;
  }

  static bool decode(const Node& yamlData, SpriteMetaData& spriteMetaData) {
    // @todo validation
    spriteMetaData.width = yamlData["width"].as<int>();
    spriteMetaData.height = yamlData["height"].as<int>();
    spriteMetaData.spritesPerRow = yamlData["spritesPerRow"].as<int>();
    spriteMetaData.sprites = yamlData["sprites"].as<std::map<std::string, int>>();
    spriteMetaData.filePath = yamlData["filePath"].as<std::string>();

    return true;
  }
};

但是当我尝试导入此Yaml

filePath: Assets/Graphics/items24.png
width: 24
height: 24
spritesPerRow: 42
sprites:
  baseball: 8
  baseballBat: 9
  berry: 17
  berrySeeds: 19
  bullet: 1
  carrot: 15
  carrotSeeds: 20
  cowboyHat: 2
  emptyShell: 0
  gunPowder: 23
  hammer: 14
  handGun: 5
  kitchenKnife: 12
  lighter: 13
  machineGun: 7
  plank: 22
  potato: 16
  potatoSeeds: 18
  scrapMetal: 11
  shotGun: 6
  soil: 21
  stone: 10
  stoneWall: 4
  trowel: 24
  whiteGloves: 3

这是我得到的结果:

sprites[baseball] = 0
sprites[baseballBat] = 1
sprites[berry] = 2
sprites[berrySeeds] = 3
sprites[bullet] = 4
sprites[carrot] = 5
sprites[carrotSeeds] = 6
sprites[cowboyHat] = 7
sprites[emptyShell] = 8
sprites[gunPowder] = 9
sprites[hammer] = 10
sprites[handGun] = 11
sprites[kitchenKnife] = 12
sprites[lighter] = 13
sprites[machineGun] = 14
sprites[plank] = 15
sprites[potato] = 16
sprites[potatoSeeds] = 17
sprites[scrapMetal] = 18
sprites[shotGun] = 19
sprites[soil] = 20
sprites[stone] = 21
sprites[stoneWall] = 22
sprites[trowel] = 23
sprites[whiteGloves] = 24

我确实在github上注意到了一些问题,但是它们似乎都已经解决了一段时间,并且我正在使用最新的代码,所以我不知道自己在做什么错/我可以做些什么来解决这个问题。

编辑:

生成输出的代码是:

for (const auto& item: nameIndexes) {
  std::cout << "sprites[" << item.first << "] = " << item.second << '\n';
}

0 个答案:

没有答案