boost karma - 从一个属性生成多个字符串

时间:2014-10-26 23:19:56

标签: c++ boost boost-spirit boost-spirit-karma

我正在使用一个消耗对子矢量的业力量化器 - 类似于http://boost-spirit.com/home/articles/karma-examples/output-generation-from-a-list-of-key-value-pairs-using-spirit-karma/

我根据上面的文章构建了一个示例来显示我的问题

#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/karma.hpp>

namespace karma = boost::spirit::karma;
typedef std::pair<std::string, std::string > pair_type;

template <typename OutputIterator>    
struct keys_and_values : karma::grammar<OutputIterator, std::vector<pair_type>()>
{
    keys_and_values() : keys_and_values::base_type(query)
    {
        query =  *pair;
        // here is the interesting part
        pair  =  karma::string << ' ' << karma::string << ' ' << karma::string << karma::eol;
    }
karma::rule<OutputIterator, std::vector<pair_type>()> query;
karma::rule<OutputIterator, pair_type()> pair;
};

int main(int argc, char *argv[])
{
    typedef std::back_insert_iterator<std::string> sink_type;

    std::vector<pair_type> v;
    v.push_back(pair_type("key1", "value1"));
    v.push_back(pair_type("key2", "value2"));
    v.push_back(pair_type("key3", "value3"));

    std::string generated;
    sink_type sink(generated);
    keys_and_values<sink_type> g;

    bool result = karma::generate(sink, g, v);

    std::cout << generated << std::endl;

    return 0;
}

我想要实现的是像“value1 key1 value1”这样的输出。通常它会输出“key1 value1”(但只有当你删除我的例子中的第三个karma :: string时) 我已经尝试了大量具有语义动作的东西,例如。

pair = karma::string[karma::_1 = karma::_val] ...

然而,这不起作用。我可能需要其他东西来从我的std :: pair中获取值。

这两个问题看起来很有趣,但没有解决我的问题 reuse parsed variable with boost karma How to access data of nested objects in boost::spirit::karma?

1 个答案:

答案 0 :(得分:3)

虽然技术上 karma::duplicate[] 在这里看起来很自然,但我可能会在这里使用当地人:

更新:正如评论者所说,我误读并交换了键/值。在这里,BOOT_FUSION_ADAPT_STRUCT_NAMED似乎是有序的!

BOOST_FUSION_ADAPT_STRUCT_NAMED(
    pair_type const, pair_as_vkv,
    (std::string, second)
    (std::string, first)
    (std::string, second)
)

现在你可以写

template <typename OutputIterator>
struct keys_and_values : karma::grammar<OutputIterator, std::vector<pair_type>()>
{
    keys_and_values() : keys_and_values::base_type(query)
    {
        query = *pair;
        pair = karma::string << ' ' << karma::string << ' ' << karma::string << karma::eol;
    }
    karma::rule<OutputIterator, std::vector<pair_type>()> query;
    karma::rule<OutputIterator, boost::fusion::adapted::pair_as_vkv()> pair;
};

不用多说:看看 Live On Coliru

输出

value1 key1 value1
value2 key2 value2
value3 key3 value3

完整列表

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>

typedef std::pair<std::string, std::string> pair_type;

BOOST_FUSION_ADAPT_STRUCT_NAMED(
    pair_type const, pair_as_vkv,
    (std::string, second)
    (std::string, first)
    (std::string, second)
)

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

template <typename OutputIterator>
struct keys_and_values : karma::grammar<OutputIterator, std::vector<pair_type>()>
{
    keys_and_values() : keys_and_values::base_type(query)
    {
        query = *pair;
        pair = karma::string << ' ' << karma::string << ' ' << karma::string << karma::eol;
    }
    karma::rule<OutputIterator, std::vector<pair_type>()> query;
    karma::rule<OutputIterator, boost::fusion::adapted::pair_as_vkv()> pair;
};

int main(int argc, char *argv[])
{
    typedef std::back_insert_iterator<std::string> sink_type;

    std::vector<pair_type> v;
    v.push_back(pair_type("key1", "value1"));
    v.push_back(pair_type("key2", "value2"));
    v.push_back(pair_type("key3", "value3"));

    std::string generated;
    sink_type sink(generated);
    keys_and_values<sink_type> g;

    karma::generate(sink, g, v);

    std::cout << generated << std::endl;

    return 0;
}