这篇文章(boost spirit semantic action parameters)解释了如何使带有签名的普通函数使匹配无效
void f(int attribute, const boost::fusion::unused_type& it, bool& mFlag)
我想从语法的成员函数中使匹配无效:
#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/home/phoenix.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
class moduleAccessManager
{
public:
bool getModule(const std::string name)
{
if(name == "cat" || name == "dog")
return true;
else
return false;
}
};
void globalIsModule(std::string moduleName, const boost::spirit::unused_type&, bool& mFlag)
{
moduleAccessManager acm; /* Dirty workaround for this example */
if(acm.getModule(moduleName))
std::cout << "[isModule] Info: Found module with name >" << moduleName << "<" << std::endl;
else
{
std::cout << "[isModule] Error: No module with name >" << moduleName << "<" << std::endl;
mFlag = false; // No valid module name
}
}
template <typename Iterator, typename Skipper>
class moduleCommandParser : public qi::grammar<Iterator, Skipper>
{
private:
moduleAccessManager* m_acm;
qi::rule<Iterator, Skipper> start, module;
public:
std::string m_moduleName;
moduleCommandParser(moduleAccessManager* acm)
: moduleCommandParser::base_type(start)
, m_acm(acm)
, m_moduleName("<empty>")
{
module = qi::as_string[qi::lexeme[+(~qi::char_(' '))]]
[&globalIsModule] // This works fine
// [phoenix::bind(&moduleCommandParser::isModule, this)] // Compile error
;
start = module >> qi::as_string[+(~qi::char_('\n'))];
};
void isModule(std::string moduleName, const boost::spirit::unused_type&, bool& mFlag)
{
// Check if a module with moduleName exists
if(m_acm->getModule(moduleName))
std::cout << "[isModule] Info: Found module with name >" << moduleName << "<" << std::endl;
else
{
std::cout << "[isModule] Error: No module with name >" << moduleName << "<" << std::endl;
mFlag = false; // No valid module name
}
};
};
int main()
{
moduleAccessManager acm;
moduleCommandParser<std::string::const_iterator, qi::space_type> commandGrammar(&acm);
std::string str;
std::string::const_iterator first;
std::string::const_iterator last;
str = "cat run";
first = str.begin();
last = str.end();
qi::phrase_parse(first, last, commandGrammar, qi::space);
str = "bird fly";
first = str.begin();
last = str.end();
qi::phrase_parse(first, last, commandGrammar, qi::space);
}
Coliru代码:http://coliru.stacked-crooked.com/a/4319b38a6d36c362
重要的部分是这两行:
[&globalIsModule] // This works fine
// [phoenix::bind(&moduleCommandParser::isModule, this)] // Compile error
使用全局函数工作正常,但这对我来说不是一个选项,因为我需要访问特定于解析器的m_acm对象。
如何将成员函数绑定到语义操作,同时能够使该成员函数的匹配无效(使用上面提到的3参数函数签名)?
答案 0 :(得分:2)
有两种方法:
qi::_val
bool&
)语义动作功能这里有一个例子:
语义动作函数的解剖(使用第三个参数):
在你的情况下,你有一个成员函数,大致有&#34;原始语义动作函数&#34;签名。当然,您必须绑定this
参数(因为它是非静态成员函数)。
请注意,在这种特殊情况下,phoenix::bind
不适合使用,因为凤凰演员将被视为&#34;烹饪&#34; (不是原始的)语义动作,它们将在Spirit语境中执行。
你可以
使用boost::bind
(甚至std::bind
)绑定到保留成员函数的 arity (!)的函数中:
[boost::bind(&moduleCommandParser::isModule, this, ::_1, ::_2, ::_3)]
这有效: Live On Coliru
改为使用&#34;煮熟的&#34;语义动作,直接分配给_pass
上下文占位符:
[qi::_pass = phoenix::bind(&moduleAccessManager::getModule, m_acm, qi::_1)]
这也适用: Live On Coliru
后一个例子,供将来参考:
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
class moduleAccessManager {
public:
bool getModule(const std::string name) {
return name == "cat" || name == "dog";
}
};
void globalIsModule(std::string moduleName, const boost::spirit::unused_type&, bool& mFlag)
{
moduleAccessManager acm; /* Dirty workaround for this example */
if(acm.getModule(moduleName))
std::cout << "[isModule] Info: Found module with name >" << moduleName << "<" << std::endl;
else
{
std::cout << "[isModule] Error: No module with name >" << moduleName << "<" << std::endl;
mFlag = false; // No valid module name
}
}
template <typename Iterator, typename Skipper>
class moduleCommandParser : public qi::grammar<Iterator, Skipper>
{
private:
moduleAccessManager* m_acm;
qi::rule<Iterator, Skipper> start, module;
public:
std::string m_moduleName;
moduleCommandParser(moduleAccessManager* acm)
: moduleCommandParser::base_type(start)
, m_acm(acm)
, m_moduleName("<empty>")
{
using namespace phoenix::arg_names;
module = qi::as_string[qi::lexeme[+(~qi::char_(' '))]]
[qi::_pass = phoenix::bind(&moduleAccessManager::getModule, m_acm, qi::_1)]
;
start = module >> qi::as_string[+(~qi::char_('\n'))];
};
};
int main()
{
moduleAccessManager acm;
moduleCommandParser<std::string::const_iterator, qi::space_type> commandGrammar(&acm);
std::string str;
std::string::const_iterator first;
std::string::const_iterator last;
str = "cat run";
first = str.begin();
last = str.end();
std::cout << str << std::boolalpha
<< qi::phrase_parse(first, last, commandGrammar, qi::space)
<< "\n";
str = "bird fly";
first = str.begin();
last = str.end();
std::cout << str << std::boolalpha
<< qi::phrase_parse(first, last, commandGrammar, qi::space)
<< "\n";
}