gpt4 book ai didi

c++ - 根据类(class)输入 boost spirit 业力的产生

转载 作者:搜寻专家 更新时间:2023-10-31 01:50:30 24 4
gpt4 key购买 nike

你好我想要的是根据我传递给生成器的内容生成一些文本

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);

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

最佳答案

最简单的修复是

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";
}

关于c++ - 根据类(class)输入 boost spirit 业力的产生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com