how to set yaml-cpp node style?

这一生的挚爱 提交于 2019-12-06 08:54:40

问题


I have a vector3 class.

class vector3
{
    float x, y, z;
}

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

The result is

x: 0
y: 0
z: 0

I want the result to be:

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

If use the old API, I can use YAML::Flow to set the style:

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

Using the new API, how can I set the style?

I asked this question on a yaml-cpp project issue page:

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

I got the answer:

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

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

but the vector3 is part of the object. I specialize the YAML::convert<> template class

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;
    }
}

so I need to return a node, but the emitter can't convert to a node.

I need the object to like that:

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

How can I do this?


回答1:


A while ago the node interface was extended in yaml-cpp to include a SetStyle() function adding the following line anywhere in encode should have the desired result

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


来源:https://stackoverflow.com/questions/14659434/how-to-set-yaml-cpp-node-style

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!