2

嗨,我想要的是根据我传递给生成器的内容生成一些文本,例如

struct C1
{
    int Getter()
    {
        return 3;
    }
};

struct C2
{
    int Getter()
    {
        return 5;
    }
};




template<typename Iterator>
struct Temp:
    public karma::grammar<Iterator,boost::variant<C1*,C2*>()>
{
    Temp():
        Temp::base_type(start1)
    {
        using karma::int_;
        using karma::float_;
        using karma::lit;

        start1 = c1 | c2;
        c1 = karma::lazy(boost::phoenix::bind(&C1::Getter,karma::_1));
        c2 = karma::lazy(boost::phoenix::bind(&C2::Getter,karma::_1));

    }

    karma::rule<Iterator,boost::variant<C1*,C2*>()> start1;
    karma::rule<Iterator,C1*() > c1;
    karma::rule<Iterator,C2*() > c2;

...然后调用类似的东西

std::string str;
std::back_insert_iterator<std::string> out(str);
Temp<std::back_insert_iterator<std::string> > bla;
C1 c1;
karma::generate(out, bla,&c1);

我什至不确定我是否使用了正确的生成器,但它说惰性能够从属性中的内容转换为返回内部函子的内容

4

1 回答 1

4

最简单的解决方法是

c1 = int_ [ karma::_1 = boost::phoenix::bind(&C1::Getter,karma::_val) ];
c2 = int_ [ karma::_1 = boost::phoenix::bind(&C2::Getter,karma::_val) ];

BOOST_FUSION_ADAPT_ADT

我想你也想了解BOOST_FUSION_ADAPT_ADT()

struct C1 { int Getter() const { return 3; } void Setter(int){} }; 
struct C2 { int Getter() const { return 5; } void Setter(int){} }; 

BOOST_FUSION_ADAPT_ADT(C1, (int,int,obj.Getter(),obj.Setter(val)));
BOOST_FUSION_ADAPT_ADT(C2, (int,int,obj.Getter(),obj.Setter(val)));

备选方案 1:attr_cast

使用attr_cast和传递价值。为简洁起见,这是一个没有语法的示例:

using namespace karma;
std::cout << karma::format("C1:" << attr_cast<C1>(int_) | "C2:" << attr_cast<C2>(int_), c1) << "\n";
std::cout << karma::format("C1:" << attr_cast<C1>(int_) | "C2:" << attr_cast<C2>(int_), c2) << "\n";

哪个打印

C1:3
C2:5

备选方案 2:使用语法/规则

typedef boost::variant<C1,C2> Var;

template<typename Iterator>
struct Generator: public karma::grammar<Iterator,Var()>
{
    Generator(): Generator::base_type(start)
    {
        using namespace karma;

        start = "grammar: " << (c1 | c2);
        c1 = "C1:" << attr_cast<int>(int_);
        c2 = "C2:" << attr_cast<int>(int_);
    }

  private:
    karma::rule<Iterator,Var()> start;
    karma::rule<Iterator,C1()> c1;
    karma::rule<Iterator,C2()> c2;
};

这里有一个完整的示例,显示了这两种替代方案: http: //liveworkspace.org/code/JWB9B$0

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

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

struct C1 { int Getter() const { return 3; } void Setter(int){} }; 
struct C2 { int Getter() const { return 5; } void Setter(int){} }; 

BOOST_FUSION_ADAPT_ADT(C1, (int,int,obj.Getter(),obj.Setter(val)));
BOOST_FUSION_ADAPT_ADT(C2, (int,int,obj.Getter(),obj.Setter(val)));

typedef boost::variant<C1,C2> Var;

template<typename Iterator>
struct Generator: public karma::grammar<Iterator,Var()>
{
    Generator(): Generator::base_type(start)
    {
        using namespace karma;

        start = "grammar: " << (c1 | c2);
        c1 = "C1:" << attr_cast<int>(int_);
        c2 = "C2:" << attr_cast<int>(int_);
    }

  private:
    karma::rule<Iterator,Var()> start;
    karma::rule<Iterator,C1()> c1;
    karma::rule<Iterator,C2()> c2;
};

typedef boost::spirit::ostream_iterator It;

int main()
{
    C1 c1;
    C2 c2;

    using namespace karma;
    std::cout << karma::format("C1:" << attr_cast<C1>(int_) | "C2:" << attr_cast<C2>(int_), c1) << "\n";
    std::cout << karma::format("C1:" << attr_cast<C1>(int_) | "C2:" << attr_cast<C2>(int_), c2) << "\n";

    // or using a grammar:
    Generator<It> bla;
    std::cout << karma::format(bla, Var(c1)) << "\n";
    std::cout << karma::format(bla, Var(c2)) << "\n";
}
于 2013-02-22T16:03:58.847 回答