【发布时间】:2017-06-16 04:59:43
【问题描述】:
以下是 boost spirit 文档中的 employee.cpp 源文件。它是“struct employee”,后跟一个宏,告诉 fusion 关于“struct employee”,然后是员工解析器。
我正在尝试根据我的目的对其进行调整,但我没有使用“struct employee”,而是想使用一些类来代替“struct employee”。
我正在考虑尝试为类替换“struct employee”,但我没有看到宏在融合中可以做到这一点?我不想把它放在结构中的原因是因为我必须将它从结构复制到我的类,这似乎没有必要,更不用说性能损失了。
再想一想,我可能不明白 Fusion 和元组的用途,因此,也许我必须这样使用它,然后将数据移动到我自己的类结构中。
有什么指导吗?
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// Our employee struct
///////////////////////////////////////////////////////////////////////////
struct employee
{
int age;
std::string surname;
std::string forename;
double salary;
};
using boost::fusion::operator<<;
}}
// We need to tell fusion about our employee struct
// to make it a first-class fusion citizen. This has to
// be in global scope.
BOOST_FUSION_ADAPT_STRUCT(
client::ast::employee,
(int, age)
(std::string, surname)
(std::string, forename)
(double, salary)
)
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// Our employee parser
///////////////////////////////////////////////////////////////////////////////
namespace parser
{
namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;
using x3::int_;
using x3::lit;
using x3::double_;
using x3::lexeme;
using ascii::char_;
x3::rule<class employee, ast::employee> const employee = "employee";
auto const quoted_string = lexeme['"' >> +(char_ - '"') >> '"'];
auto const employee_def =
lit("employee")
>> '{'
>> int_ >> ','
>> quoted_string >> ','
>> quoted_string >> ','
>> double_
>> '}'
;
BOOST_SPIRIT_DEFINE(employee);
}
}
【问题讨论】:
标签: c++ boost-spirit boost-fusion