欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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上搜索,看有沒有開源的庫,功夫不負(fù)有心人,找到了yaml-cpp,用他解析了一個(gè)yaml的例子非常好使,分享一下如何使用他。
git clone git@github.com:jbeder/yaml-cpp.git下來編譯成靜態(tài)庫

mkdir build
cd build
cmake ..
make

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

yaml_demo
 |__ include
     |__yaml-cpp 頭文件夾
 |__ lib 
     |__yaml-cpp 庫文件夾
 |__ main.cpp

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

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) 

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

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

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

  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í)候他會判斷有沒有實(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文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語言鏈表實(shí)現(xiàn)工資管理系統(tǒng)

    C語言鏈表實(shí)現(xiàn)工資管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言鏈表實(shí)現(xiàn)工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 手把手帶你學(xué)習(xí)C++的數(shù)據(jù)類型

    手把手帶你學(xué)習(xí)C++的數(shù)據(jù)類型

    這篇文章主要為大家介紹了C++的數(shù)據(jù)類型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助,希望能夠給你帶來幫助
    2021-11-11
  • C++資源管理操作方法詳解

    C++資源管理操作方法詳解

    系統(tǒng)中的資源,諸如動態(tài)申請的內(nèi)存,文件描述符,數(shù)據(jù)庫連接,網(wǎng)絡(luò)socket等,在不用的時(shí)候,應(yīng)該及時(shí)歸還給系統(tǒng),否則就會造成內(nèi)存泄露
    2022-09-09
  • C++ 轉(zhuǎn)換函數(shù)用法案例詳解

    C++ 轉(zhuǎn)換函數(shù)用法案例詳解

    這篇文章主要介紹了C++ 轉(zhuǎn)換函數(shù)用法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • c++中的volatile和variant關(guān)鍵字詳解

    c++中的volatile和variant關(guān)鍵字詳解

    大家好,本篇文章主要講的是c++中的volatile和variant關(guān)鍵字詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • C++自動析構(gòu)時(shí)的順序問題

    C++自動析構(gòu)時(shí)的順序問題

    這篇文章主要介紹了C++自動析構(gòu)時(shí)的順序,通過實(shí)例代碼給大家講解了C++ 構(gòu)造與析構(gòu)的執(zhí)行順序,代碼簡單易懂,非常不錯(cuò)對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • C語言完美實(shí)現(xiàn)動態(tài)數(shù)組代碼分享

    C語言完美實(shí)現(xiàn)動態(tài)數(shù)組代碼分享

    本文給大家分享的是一則使用C語言實(shí)現(xiàn)動態(tài)數(shù)組的代碼,完美解決內(nèi)存溢出以及內(nèi)存回收問題,有需要的小伙伴可以參考下。
    2016-02-02
  • C++ min/max_element 函數(shù)用法詳解

    C++ min/max_element 函數(shù)用法詳解

    這篇文章主要介紹了C++ min/max_element 函數(shù)用法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器

    c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器

    這篇文章主要介紹了c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器,幫助大家更好的理解和使用libuv,感興趣的朋友可以了解下
    2021-02-02
  • VS2022 無法打開源文件“stdio.h”問題解決

    VS2022 無法打開源文件“stdio.h”問題解決

    本文主要介紹了VS2022 無法打開源文件“stdio.h”問題解決,文中通過圖文的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06

最新評論