如何在boost :: spirit :: karma中访问嵌套对象的数据? [英] How to access data of nested objects in boost::spirit::karma?

查看:113
本文介绍了如何在boost :: spirit :: karma中访问嵌套对象的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包含Item对象列表的ItemList中,如何访问生成器中的Item对象?

In an ItemList containing lists of Item objects, how do I access the Item objects in the generator?

以下示例代码在VC9上编译(具有适当设置的boost include和link目录). 我不知道如何设置list_generator::item.

The following sample code compiles on VC9 (with boost include and link directories set apropriately). I don't know how to set up list_generator::item.

#include <boost/config/warning_disable.hpp>

#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>

#include <iostream>
#include <string>
#include <list>

namespace karma = boost::spirit::karma;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;


class Item
{
public:
    typedef std::vector<int> Values;

    Item(const std::string  & i, const Values & v) : m_id(i), m_values(v) {}
    std::string getId() const { return m_id; }
    const Values & getValues() const { return m_values; }

private:
    std::string m_id;
    Values m_values;
};

class ItemList
{
public:
    typedef std::map<std::string, Item> Items;

    ItemList() {}
    ItemList(const Items & s, const Items & o) : m_some(s), m_other(o) {}
    const Items getSome() const { return m_some; }
    const Items getOther() const { return m_other; }

private:
    Items m_some;;
    Items m_other;
};

template <typename Iterator>
struct list_generator : karma::grammar<Iterator, ItemList()>
{
    list_generator(const ItemList & i) : list_generator::base_type(start)
{
    using karma::int_;
    using karma::_1;
    using karma::lit;

    // Convert maps into lists containing only the values
    typedef std::vector<Item> Cells;
    const Cells some  = boost::copy_range<Cells>(i.getSome() | boost::adaptors::map_values);
    const Cells other = boost::copy_range<Cells>(i.getOther() | boost::adaptors::map_values);

    item =
        lit("<item>")
        << lit("<id>")      /*<< lit[_1 = ??.getId()]*/ << lit("</id>") // Item ID
        << lit("<values>")  /*<< (int_ % ';')[_1 = ??.getValues()]*/ << lit("</values>") // List of Item values
        << lit("</item>");

    start =
        lit("<some>")     << (*item)[_1 = some] << lit("</some>")
        << lit("<other>")  << (*item)[_1 = other] << lit("</other>");
}

karma::rule<Iterator, Item()> item;
karma::rule<Iterator, ItemList()> start;
};

int main()
{
    const Item::Values values = boost::assign::list_of(1)(2)(3);
    const Item a("a", values);
    const Item b("b", values);

    ItemList::Items some, other;
    some.insert(std::make_pair(a.getId(), a));
    other.insert(std::make_pair(b.getId(), b));
    const ItemList items(some, other);

    typedef std::back_insert_iterator<std::string> Iter;
    typedef list_generator<Iter> Generator;

    Generator grammar(items);

    std::string generated;
    Iter sink(generated);
    if (!karma::generate(sink, grammar))
    {
        std::cout << "Generating failed\n";
    }
    else
    {
        std::cout << "Generated: " << generated << "\n";
    }

    return 0;
}

输出为:

Generated: <some><item><id></id><values></values></item></some><other><item><id></id><values></values></item></other>

推荐答案

您应使用karma::_val.例如,您可以编写一些活页夹(简单的示例)

You should use karma::_val. For example you can write some binders (simple example)

<< lit("<id>") 
<< karma::string[_1 = phoenix::bind(&Item::getId, karma::_val)]
<< lit("</id>") // Item ID
<< lit("<values>")
<< (int_ % ';')[_1 = phoenix::bind(&Item::getValues, karma::_val)]
<< lit("</values>") // List of Item values

这篇关于如何在boost :: spirit :: karma中访问嵌套对象的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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