gpt4 book ai didi

yaml-cpp - 如何设置yaml-cpp节点样式?

转载 作者:行者123 更新时间:2023-12-02 19:24:21 24 4
gpt4 key购买 nike

我有一个 vector3 类。

class vector3
{
float x, y, z;
}

node["x"] = vector3.x;
node["y"] = vector3.y;
node["z"] = vector3.z;

结果是

x: 0
y: 0
z: 0

我想要的结果是:

{x: 0, y: 0, z: 0}

如果使用旧的API,我可以使用YAML::Flow设置样式:

YAML::Emitter emitter;
out << YAML::Flow << YAML::BeginMap << YAML::Key << "x" << YAML::Value << x << YAML::EndMap

使用新的API,如何设置样式?

我在 yaml-cpp 项目问题页面上问了这个问题:

https://code.google.com/p/yaml-cpp/issues/detail?id=186

我得到了答案:

You can still use the emitter and set the flow style:

YAML::Emitter emitter;
emitter << YAML::Flow << node;

但是vector3是对象的一部分。我专注于YAML::convert<>模板类

template<>
struct convert<vector3>
{
static Node encode(const vector3 & rhs)
{
Node node = YAML::Load("{}");
node["x"] = rhs.x;
node["y"] = rhs.y;
node["z"] = rhs.z;

return node;
}
}

所以我需要返回一个节点,但发射器无法转换为节点。

我需要像这样的对象:

GameObject:
m_Layer: 0
m_Pos: {x: 0.500000, y: 0.500000, z: 0.500000}

我该怎么做?

最佳答案

不久前,节点接口(interface)在 yaml-cpp 中进行了扩展,包含一个 SetStyle() 函数,在 encode 中的任何位置添加以下行应该会得到所需的结果

node.SetStyle(YAML::EmitterStyle::Flow);

关于yaml-cpp - 如何设置yaml-cpp节点样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com