提升业力语义动作呼叫地图

时间:2015-10-09 17:53:43

标签: boost boost-spirit-karma

我需要将值映射到std :: string(使用以下映射和BOOST_FUSION_ADAPT_STRUCT)

    std::map< TYPEX, std::string> author2name;
    struct Emp
   {
     std::string name;
     TYPEX author;
   };

使用以下代码我想生成输出:

karma::rule< it, std::string()> quote = '"' >> karma::string >> '"';
karma::rule< it, Emp> emp = karma::delimit('\t')[ quite << quite[ author2name[ karma::_1] ]];

Emp x;
karma::generate( std::ostream_iterator<char>(std::cout), emp, x);

但它没有编译。

有没有办法可以像这样写一个标题:

karma::rule< it, std::vector<std::string>()> header = karma::delimit('\t')[ % quote];
karma::rule< it, Emp> emp = header >> karma::eol >> karma::delimit('\t')[ quite << quite[ author2name[ karma::_1] ]];

karma::generate( std::ostream_iterator<char>(std::cout), {"A", "B", "C"},emp, x);

1 个答案:

答案 0 :(得分:3)

有许多小型剪纸在那里杀了你:)

工作示例:

<强> Live On Coliru

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <map>

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

enum TYPEX { AUTHOR1, AUTHOR2, AUTHOR3, AUTHOR4 };

std::map<TYPEX, std::string> author2name;
struct Emp {
    std::string name;
    TYPEX author;
};

BOOST_FUSION_ADAPT_STRUCT(Emp, name, author) // boost 1_59
//BOOST_FUSION_ADAPT_STRUCT(Emp, (std::string, name)(std::string, author)) // older boost

int main() {
    using it = boost::spirit::ostream_iterator;

    karma::rule<it, std::string()> quote;
    karma::rule<it, TYPEX()> author;
    karma::rule<it, Emp()> emp;

    {
        using namespace karma;
        quote  %= '"' << string << '"';
        author  = quote [ _1 = phx::ref(author2name)[ _val ] ];
        emp    %= delimit('\t')[ quote << author ];
    }

    Emp x { "one", AUTHOR2 };
    author2name[AUTHOR2] = "TWO!";
    std::cout << karma::format(emp, x);
}

打印:

"one"   "TWO!"  

造成麻烦的事情:

  • 建议使用boost::spirit::ostream_iteratorkarma::format来获得更加用户友好的API
  • emp上添加缺少的括号:

    karma::rule<it, Emp()> emp;
    
      

    注意:最近的提升(1_59 IIRC)不再要求这些。这就是我在Coliru上发现的原因

  • 下面:

    quote[ author2name[ karma::_1] ]
    

    使用... std::map将[]索引到qi::_1。那不能编译。你想要的是调用operator[]的Phoenix 懒惰表达式模板。您必须包含Phoenix标头并强制author2name成为Phoenix参考演员:

    quote [ _1 = phx::ref(author2name)[_1] ]
    
      

    另请注意,分配回_1非常重要!

  • 此外,要在存在语义操作的情况下拥有自动规则,您需要使用%=分配规则(否则Karma将禁止所有自动属性传播)