1

ファイル test.yaml .....

   ---
   map:
        ? [L, itsy] : ISO_LOW_ITSY(out, in, ctrl) 
        ? [L, bitsy] : ISO_LOW_BITSY(out, in, ctrl) 
        ? [L, spider] : ISO_LOW_SPIDER(out, in, ctrl) 
        ? [H, ANY] : ISO_HIGH(out, in, ctrl)

yaml-cpp でこれらのいずれかにアクセスするには、どのコマンドを使用できますか。マップ全体にアクセスできますが、個々の要素にはアクセスできません。

これが私が試みていることです:

    YAML::Node doc = YAML::LoadFile("test.yaml");
    std::cout << "A:" << doc["mapping"] << std::endl;
    std::cout << "LS1:" << doc["mapping"]["[L, spider]"] << std::endl;
    std::cout << "LS2:" << doc["mapping"]["L", "spider"] << std::endl;

ここに私の結果があります:

    A:? ? - L
          - itsy
 : ISO_LOW_ITSY(out, in, ctrl)
: ~
? ? - L
    - bitsy
  : ISO_LOW_BITSY(out, in, ctrl)
: ~
? ? - L
    - spider
  : ISO_LOW_SPIDER(out, in, ctrl)
: ~
? ? - H
    - ANY
  : ISO_HIGH(out, in, ctrl)
: ~
LS1:
LS2:

これが yaml-cpp でまだ可能でない場合は、それも知りたいです。

4

1 に答える 1

1

キーに一致するタイプを定義する必要があります。たとえば、キーが 2 つのスカラーのシーケンスである場合:

struct Key {
  std::string a, b;

  Key(std::string A="", std::string B=""): a(A), b(B) {}

  bool operator==(const Key& rhs) const {
    return a == rhs.a && b == rhs.b;
  }
};

namespace YAML {
  template<>
  struct convert<Key> {
    static Node encode(const Key& rhs) {
      Node node;
      node.push_back(rhs.a);
      node.push_back(rhs.b);
      return node;
    }

    static bool decode(const Node& node, Key& rhs) {
      if(!node.IsSequence() || node.size() != 2)
        return false;

      rhs.a = node[0].as<std::string>();
      rhs.b = node[1].as<std::string>();
      return true;
    }
  };
}

次に、YAML ファイルが

? [foo, bar]
: some value

あなたは書くことができます:

YAML::Node doc = YAML::LoadFile("test.yaml");
std::cout << doc[Key("foo", "bar")];     // prints "some value"

ノート:

あなたの YAML はあなたが意図したことをしていないと思います。ブロック コンテキストでは、明示的なキーと値のペアは別の行にある必要があります。言い換えれば、あなたはすべきです

? [L, itsy]
: ISO_LOW_ITSY(out, in, ctrl)

はない

? [L, itsy] : ISO_LOW_ITSY(out, in, ctrl)

後者は、(暗黙の) null 値を持つ単一のキーにします。すなわち、それはと同じです

? [L, itsy] : ISO_LOW_ITSY(out, in, ctrl)
: ~

(これは、yaml-cpp が例を出力する方法で確認できます。) spec の関連する領域を参照してください。

于 2013-01-20T20:21:10.667 に答える