点击(此处)折叠或打开
- Yaml的动态读写. 参考
-
-
YAML::Node config = YAML::LoadFile("config.yaml");
-
-
if (config["lastLogin"]) {
-
std::cout << "Last logged in: " << config["lastLogin"].as<DateTime>() << "\n";
-
}
-
-
const std::string username = config["username"].as<std::string>();
-
const std::string password = config["password"].as<std::string>();
-
login(username, password);
-
config["lastLogin"] = getCurrentDateTime();
-
-
std::ofstream fout("config.yaml");
- fout << config
点击(此处)折叠或打开
-
yaml文件的所有节点包括root 都是 YAML::Node
-
-
YAML::Node node = YAML::Load("[1, 2, 3]");
-
assert(node.Type() == YAML::NodeType::Sequence); //序列类型, 有很多种类型
-
assert(node.IsSequence()); // a
-
-
--------------------------------------------
-
--------------------------------------------
-
sequences 以及 maps 类型节点可以按照 std::vector/maps 进行操作
-
-
YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
-
for (std::size_t i=0;i<primes.size();i++) {
-
std::cout << primes[i].as<int>() << "\n";
-
}
-
// or:
-
for (YAML::const_iterator it=primes.begin();it!=primes.end();++it) {
-
std::cout << it->as<int>() << "\n";
-
}
-
-
primes.push_back(13);
-
assert(primes.size() == 6);
-
----------------------------
-
//maps 类型
-
YAML::Node lineup = YAML::Load("{1B: Prince Fielder, 2B: Rickie Weeks, LF: Ryan Braun}");
-
for(YAML::const_iterator it=lineup.begin();it!=lineup.end();++it) {
-
std::cout << "Playing at " << it->first.as<std::string>() << " is " << it->second.as<std::string>() << "\n";
-
}
-
-
lineup["RF"] = "Corey Hart";
-
lineup["C"] = "Jonathan Lucroy";
-
assert(lineup.size() == 5);
-
----------------------------
-
查询键是不会自动创建键的.
-
YAML::Node node = YAML::Load("{name: Brewers, city: Milwaukee}");
-
if (node["name"]) {
-
std::cout << node["name"].as<std::string>() << "\n";
-
}
-
if (node["mascot"]) {
-
std::cout << node["mascot"].as<std::string>() << "\n";
-
}
- assert(node.size() == 2); // the previous call didn
点击(此处)折叠或打开
-
建立 yaml::node
-
YAML::Node node; // starts out as null
-
node["key"] = "value"; // it now is a map node
-
node["seq"].push_back("first element"); // node["seq"] automatically becomes a sequence
-
node["seq"].push_back("second element");
-
-
node["mirror"] = node["seq"][0]; // this creates an alias
-
node["seq"][0] = "1st element"; // this also changes node["mirror"]
-
node["mirror"] = "element #1"; // and this changes node["seq"][0] - they're really the "same" node
-
-
node["self"] = node; // you can even create self-aliases
-
node[node["mirror"]] = node["seq"]; // and strange loops :)
-
-
此时内容
-
&1
-
key: value
-
&2 seq: [&3 "element #1", second element]
-
mirror: *3
-
self: *1
- *3 : *2
点击(此处)折叠或打开
-
----------------------------------------
-
列表Sequences 自动转化 maps 的情况. 如果在索引序列之内,则保持 sequences 状态, 如果超出索引序列,例如非整数的下标, 以及超出个数的下标, 则 Sequences 会被转换成 maps
-
-
YAML::Node node = YAML::Load("[1, 2, 3]");
-
node[1] = 5; // still a sequence, [1, 5, 3]
-
node.push_back(-3) // still a sequence, [1, 5, 3, -3]
-
node["key"] = "value"; // now it's a {0: 1, 1: 5, 2: 3, 3: -3, key: value}
-
-
----------------------------------------
-
YAML::Node node = YAML::Load("[1, 2, 3]");
-
node[3] = 4; // still a sequence, [1, 2, 3, 4]
- node[10] = 10; // // now it's a {0: 1, 1: 2, 2: 3, 3: 4, 10: 10}
点击(此处)折叠或打开
-
YAML::Node node = YAML::Load("{pi: 3.14159, [0, 1]: integers}");
-
-
// this needs the conversion from Node to double
-
double pi = node["pi"].as<double>();
-
-
// this needs the conversion from double to Node
-
node["e"] = 2.71828;
-
-
// this needs the conversion from Node to std::vector<int> (*not* the other way
-
std::vector<int> v;
-
v.push_back(0);
-
v.push_back(1);
- std::string str = node[v].as<std::string>()
点击(此处)折叠或打开
-
自定义类型需要重载 YAML::convert<> template class
-
-
struct Vec3 { double x, y, z; /* etc - make sure you have overloaded operator== */ };
-
-
namespace YAML {
-
template<>
-
struct convert<Vec3> {
-
static Node encode(const Vec3& rhs) {
-
Node node;
-
node.push_back(rhs.x);
-
node.push_back(rhs.y);
-
node.push_back(rhs.z);
-
return node;
-
}
-
-
static bool decode(const Node& node, Vec3& rhs) {
-
if(!node.IsSequence() || node.size() != 3) {
-
return false;
-
}
-
-
rhs.x = node[0].as<double>();
-
rhs.y = node[1].as<double>();
-
rhs.z = node[2].as<double>();
-
return true;
-
}
-
};
-
}
-
-
然后就可以应用了
-
YAML::Node node = YAML::Load("start: [1, 3, 0]");
-
Vec3 v = node["start"].as<Vec3>();
- node["end"] = Vec3(2, -1, 0)
点击(此处)折叠或打开
-
monsters.yaml
-
-
- name: Ogre
-
position: [0, 5, 0]
-
powers:
-
- name: Club
-
damage: 10
-
- name: Fist
-
damage: 8
-
- name: Dragon
-
position: [1, 0, 10]
-
powers:
-
- name: Fire Breath
-
damage: 25
-
- name: Claws
-
damage: 15
-
- name: Wizard
-
position: [5, -3, 0]
-
powers:
-
- name: Acid Rain
-
damage: 50
-
- name: Staff
- damage: 3
点击(此处)折叠或打开
-
#include "yaml-cpp/yaml.h"
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include <vector>
-
-
// our data types
-
struct Vec3 {
-
float x, y, z;
-
};
-
-
struct Power {
-
std::string name;
-
int damage;
-
};
-
-
struct Monster {
-
std::string name;
-
Vec3 position;
-
std::vector <Power> powers;
-
};
-
-
// now the extraction operators for these types
-
void operator >> (const YAML::Node& node, Vec3& v) {
-
node[0] >> v.x;
-
node[1] >> v.y;
-
node[2] >> v.z;
-
}
-
-
void operator >> (const YAML::Node& node, Power& power) {
-
node["name"] >> power.name;
-
node["damage"] >> power.damage;
-
}
-
-
void operator >> (const YAML::Node& node, Monster& monster) {
-
node["name"] >> monster.name;
-
node["position"] >> monster.position;
-
const YAML::Node& powers = node["powers"];
-
for(unsigned i=0;i<powers.size();i++) {
-
Power power;
-
powers[i] >> power;
-
monster.powers.push_back(power);
-
}
-
}
-
-
int main()
-
{
-
std::ifstream fin("monsters.yaml");
-
YAML::Parser parser(fin);
-
YAML::Node doc;
-
parser.GetNextDocument(doc);
-
for(unsigned i=0;i<doc.size();i++) {
-
Monster monster;
-
doc[i] >> monster;
-
std::cout << monster.name << "\n";
-
}
-
-
return 0;
- }