- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
下面是 boost spirit 文档中的 employee.cpp 源文件。它是“struct employee”,后跟一个告诉融合有关“struct employee”的宏,然后是 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);
}
}
最佳答案
struct
之间没有区别和 class
¹.
除此之外,人们通常的意思是“我想要没有直接数据成员(“字段”)访问的类”。
现在我可以直接指向您 BOOST_FUSION_ADAPT_ADT
.这就是您要寻找的。
但是。
这意味着您无论如何都为所有数据成员公开了 setter。这是一个巨大的反模式²,因为它只会导致准类³。
考虑使用工厂函数(使用 Phoenix 来调整它们以从语义操作中调用//但请参阅 Boost Spirit: "Semantic actions are evil"? )或者,确实有一个干净的 AST 表示,您然后使用它来构建域对象图来自。
如果您负担不起(因为拷贝),那么您就真的买不起 Spirit V2 IMO。 Spirit 旨在快速开发/原型(prototype)化(更改)语法,同时不会生成糟糕的代码。但是,如果您买不起拷贝,那么是时候手动滚动您的解析器了(或转移到 Spirit X3)
¹ 从字面上看,唯一的区别是 struct
默认公开所有成员,但您仍然可以使用 private:
和 protected:
² 可能起源于 Java 的 PoJo 或“Bean”的历史
³ "Pseudo-Classes and Quasi-ClassesConfuse Object-Oriented Programming"
关于c++ - 用精神解析成类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44581141/
我正在使用此代码将“k1 = v1; k2 = v2; k3 = v3; kn = vn”字符串解析为映射。 qi::phrase_parse( begin,end,
我不理解以下示例中 x3 的行为(取自更大的语法)。 当然,语法有点奇怪,但大致实现了 (lal)?()? .当第二组不存在时,默认为 .我不明白为什么要输入 "lal"我得到 defaultcha
所以我的问题很简单,在我的词法分析器类(扩展 lex::lexer )中,我有以下内容; this->self.add ... ("&&", AND_AND) ("||", O
我正在尝试修改以下示例:http://www.boost.org/doc/libs/1_57_0/libs/spirit/example/qi/employee.cpp 我想在 employee 结构
我正在为当前分支为多个版本的应用程序寻找版本编号方案/模式/系统 shell game样式发布日期。这使得版本控制成为一场噩梦。我想只使用典型的 Major.Minor.Revision 但是这会很快
是的,我开始做这个测试了! 但是我不知道该用什么=/ Rspec + 应该吗? Rspec + 牛排? 迷你测试? cucumber ? capybara ? 可以吗? ( cucumber 与 Sh
我是一名优秀的程序员,十分优秀!