如何设置yaml-cpp节点样式? [英] how to set yaml-cpp node style?

查看:413
本文介绍了如何设置yaml-cpp节点样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个vector3类.

I have a vector3 class.

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设置样式:

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

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

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

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

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

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<>模板类

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.

我需要这样的物体:

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

我该怎么做?

推荐答案

前一阵子,节点接口在yaml-cpp中进行了扩展,以包含一个SetStyle()函数,在encode中的任何位置添加以下行应具有所需的结果

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

这篇关于如何设置yaml-cpp节点样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆