升压property_tree:每个键多个值 [英] Boost property_tree: multiple values per key

查看:155
本文介绍了升压property_tree:每个键多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升压属性树似乎是一个很好的书房使用解析配置文件。但是,我无法弄清楚如何处理地方有每个键多个值的情况。例如,假设我被指定这样的框:

Boost property tree seems like an excellent library to use for parsing config files. However, I can't figure out how to handle situations where there are multiple values per key. For example, let's say I was specifying a box like this:

box
{
    x -1 1
    y -1 1
    z -1 1
}

其中, X 以Z 是盒子上的界限 X 以Z 轴分别采用property_tree的信息格式指定。我看到提到使用引号使用空间值的手册中,但我没有看到,我可以导入这些值作为数字。我不得不解析字符串到数字,这似乎击败首先使用property_tree的目的。我当然可以给每个号码一键:

where x, y, and z are the bounds of the box on the x, y, and z axes respectively, specified using property_tree's INFO format. I see mention in the manual of using quotes for values that use spaces, but then I don't see that I could import those values as numbers. I'd have to parse the string into numbers, which seems to defeat the purpose of using property_tree in the first place. I could certainly give each number a key:

box 
{
    xlo -1
    xhi 1
    ylo -1
    yhi 1
    zlo -1
    zhi 1
}    

但似乎麻烦,而且会夸大我的配置文件。我也注意到,我可以program_options处理这种情况,但我失去了嵌套的配置文件功能(是的,我知道我可以使用点符号鸟巢,但它是不一样的)。

but that seems cumbersome, and will inflate my config file. I also noted that I could handle this situation in program_options, but I lose the nested config file capabilities (yeah, I know I can use dot notation to "nest", but it's not the same).

有没有办法导入例如x作为这样的号码清单?

Is there a way to import e.g. x as a list of numbers like this?

推荐答案

,因为它被定义为标准property_tree处理每个键只有一个字符串值:

The standard property_tree handles only one string value per key since it is defined as:

typedef basic_ptree<std::string, std::string> ptree;

所以,唯一的选择就是使用字符串和解析。我认为最好的方法是定义存储的低值和高值新的类,然后创建get和set方法翻译类。例如:

So, the only option is to use strings and parse them. I think the best method is to define a new class that stores the low and high values and then create a translator class for the get and set methods. For example:

struct low_high_value
{
  low_high_value() : m_low(0), m_high(0) { }
  low_high_value(double low, double high) : m_low(low), m_high(high) { }
  double m_low;
  double m_high;
};

译者是:

struct low_high_value_translator
{
  typedef std::string    internal_type;
  typedef low_high_value external_type;
  // Get a low_high_value from a string
  boost::optional<external_type> get_value(const internal_type& str)
  {
    if (!str.empty())
    {
      low_high_value val;
      std::stringstream s(str);
      s >> val.m_high >> val.m_low;
      return boost::optional<external_type>(val);
    }
    else
      return boost::optional<external_type>(boost::none);
  }

  // Create a string from a low_high_value
  boost::optional<internal_type> put_value(const external_type& b)
  {
    std::stringstream ss;
    ss << b.m_low << " " << b.m_high;
    return boost::optional<internal_type>(ss.str());
  }
};

在previous的get_value方法很简单。如果文件可以由用户写入应当提高。

The previous get_value method is very simple. It should be improved if the file could be written by the user.

本类应该使用注册:

namespace boost { namespace property_tree 
{
  template<typename Ch, typename Traits, typename Alloc> 
  struct translator_between<std::basic_string< Ch, Traits, Alloc >, low_high_value>
  {
    typedef low_high_value_translator type;
  };
} }

在你包括previous code,你可以使用property_tree为:

After you include the previous code, you can use property_tree as:

  pt.get<low_high_value>("box.x")
  pt.put("box.u", low_high_value(-110, 200));

这篇关于升压property_tree:每个键多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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