最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++ 解析yaml文件的步驟

 更新時(shí)間:2024年07月01日 14:53:06   作者:li-peng  
這篇文章主要介紹了c++ 解析yaml文件的步驟,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下

作者:li-peng
出處:http://www.cnblogs.com/li-peng/

一直用c++操作ini做配置文件,想換成yaml,在github上搜索,看有沒(méi)有開(kāi)源的庫(kù),功夫不負(fù)有心人,找到了yaml-cpp,用他解析了一個(gè)yaml的例子非常好使,分享一下如何使用他。
git clone git@github.com:jbeder/yaml-cpp.git下來(lái)編譯成靜態(tài)庫(kù)

mkdir build
cd build
cmake ..
make

運(yùn)行完后,會(huì)得到libyaml-cpp.a。
新建一個(gè)項(xiàng)目,結(jié)構(gòu)大致如下

yaml_demo
 |__ include
     |__yaml-cpp 頭文件夾
 |__ lib 
     |__yaml-cpp 庫(kù)文件夾
 |__ main.cpp

配置CMakeLists.txt把頭文件和靜態(tài)庫(kù)加到項(xiàng)目里,這樣在編譯和鏈接時(shí)才能通過(guò)

project(yaml_demo)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) # 二進(jìn)制文件的輸出目錄
link_directories(${PROJECT_SOURCE_DIR}/lib/yaml-cpp)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME} yaml-cpp.a) 

對(duì)yaml-cpp的配置就完成了??匆幌挛业腸onfig文件

api: aaaaa
v: 1
label:
 app: hello
 image: abc
containers:
 - name: abc
  age: 18
 - name: 222
  age: 12

其中api和v是比較簡(jiǎn)單的鍵值,我們可以直接讀取他們的值

  std::cout << "api: " << config["api"].as<std::string>() << std::endl;
  std::cout << "v: " << config["v"].as<int>() << std::endl;

label是一個(gè)map,containers是一個(gè)列表,這就要特殊處理一下,yaml-cpp有自己的轉(zhuǎn)換模板

template <typename T>
struct convert;

在進(jìn)行轉(zhuǎn)換的時(shí)候他會(huì)判斷有沒(méi)有實(shí)現(xiàn) decode方法

struct as_if<T, void> {
 explicit as_if(const Node& node_) : node(node_) {}
 const Node& node;

 T operator()() const {
  if (!node.m_pNode)
   throw TypedBadConversion<T>(node.Mark());

  T t;
  if (convert<T>::decode(node, t))
   return t;
  throw TypedBadConversion<T>(node.Mark());
 }
};

Node是yaml-cpp的核心,我們的配置的所有操作都從這個(gè)類中進(jìn)行。
我們只要具體化自定義的struct就可以使用了

struct label {
  std::string app;
  std::string image;
};

namespace YAML {
  template<>
  struct convert<label> {
    static Node encode(const label &rhs) {
      Node node;
      node.push_back(rhs.app);
      node.push_back(rhs.image);
      return node;
    }

    static bool decode(const Node &node, label &rhs) {
      std::cout << node.Type() << std::endl;
      rhs.app = node["app"].as<std::string>();
      rhs.image = node["image"].as<std::string>();
      return true;
    }
  };
}

encode方法是把我們自定義的struct轉(zhuǎn)換成yaml-cpp的Node,
轉(zhuǎn)換時(shí)可以這樣

if (config["label"]) {
  label l = config["label"].as<label>();
  std::cout << "app: " << l.app << " image: " << l.image << std::endl;
}

container也是一樣的具體化

struct container {
  std::string name;
  int age;
};

namespace YAML {
  template<>
  struct convert<container> {
    static Node encode(const container &rhs) {
      Node node;
      node.push_back(rhs.name);
      node.push_back(rhs.age);
      return node;
    }

    static bool decode(const Node &node, container &rhs) {
      rhs.name = node["name"].as<std::string>();
      rhs.age = node["age"].as<int>();
      return true;
    }
  };
}

完整代碼如下:

#include <string>
#include <iostream>
#include <yaml-cpp/yaml.h>
#include <yaml-cpp/node/parse.h>

struct container {
  std::string name;
  int age;
};

namespace YAML {
  template<>
  struct convert<container> {
    static Node encode(const container &rhs) {
      Node node;
      node.push_back(rhs.name);
      node.push_back(rhs.age);
      return node;
    }

    static bool decode(const Node &node, container &rhs) {
      rhs.name = node["name"].as<std::string>();
      rhs.age = node["age"].as<int>();
      return true;
    }
  };
}

struct label {
  std::string app;
  std::string image;
};

namespace YAML {
  template<>
  struct convert<label> {
    static Node encode(const label &rhs) {
      Node node;
      node.push_back(rhs.app);
      node.push_back(rhs.image);
      return node;
    }

    static bool decode(const Node &node, label &rhs) {
      std::cout << node.Type() << std::endl;
      rhs.app = node["app"].as<std::string>();
      rhs.image = node["image"].as<std::string>();
      return true;
    }
  };
}

int main(int argc, char **argv) {
  std::string config_path = "./config.yaml";
  std::cout << config_path << std::endl;
  YAML::Node config = YAML::LoadFile(config_path);

  std::cout << "api: " << config["api"].as<std::string>() << std::endl;
  std::cout << "v: " << config["v"].as<int>() << std::endl;

  if (config["label"]) {
    label l = config["label"].as<label>();
    std::cout << "app: " << l.app << " image: " << l.image << std::endl;
  }

  if (config["containers"]) {
    std::vector<container> vi = config["containers"].as<std::vector<container>>();

    for (std::vector<container>::iterator it = vi.begin(); it != vi.end(); ++it) {
      std::cout << "vector: name: " << it->name << " age: " << it->age << std::endl;
    }
  }

  return 0;
}

以上就是c++ 解析yaml文件的步驟的詳細(xì)內(nèi)容,更多關(guān)于c++ 解析yaml文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

拜泉县| 德阳市| 大同县| 华池县| 博罗县| 苍溪县| 达州市| 阿拉尔市| 东乌珠穆沁旗| 长丰县| 湟中县| 克什克腾旗| 福建省| 徐汇区| 开封县| 儋州市| 民勤县| 容城县| 抚顺县| 永康市| 禹城市| 灵宝市| 吴桥县| 汽车| 安国市| 遂昌县| 科尔| 贵南县| 定安县| 湖州市| 大化| 鲁甸县| 青岛市| 张家川| 项城市| 锦州市| 洛宁县| 灌云县| 小金县| 长丰县| 中山市|