- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我又遇到了灵气障碍问题。
我在一个名为 error_handler 的仿函数结构中实现了错误处理。这通过引用传递给语法构造函数(参见 Qi 的 MiniC 示例)。
然后我有on_error<fail>
s 在语法的构造函数中定义:
typedef boost::phoenix::function<error_handler<> > error_handler_function;
on_error<fail>(gr_instruction,
error_handler_function(err_handler)(L"Error: Expecting ", _4, _3));
// more on_error<fail>s...
但是,我的error_handler
有私有(private)成员。好像每次都是on_error
被调用,err_handler
对象被复制,因此一旦仿函数离开,更改的局部变量将被销毁。
我尝试通过引用传递处理程序:
typedef boost::phoenix::function<error_handler<>& > error_handler_function; // <--- Note the ampersand!
on_error<fail>(gr_instruction,
error_handler_function(err_handler)(L"Error: Expecting ", _4, _3));
// more on_error<fail>s...
但是,问题依然存在:on_error()
在 err_handler
的拷贝上工作, 不是单个实例!!
我也试过 boost::phoenix::ref(err_handler)
的变体只有编译错误。
当然,必须有一个简单的解决方案来通过引用传递处理程序吗?
如有任何意见,我将不胜感激。感谢您的帮助。
最佳答案
是的,phx::bind 和 phx::function<> 将默认采用它们的包装器可调用对象。然而。
比方说[1],您有一个这样的错误处理程序……极简示例:
template <typename=void> struct my_error_handler {
my_error_handler() = default;
my_error_handler(my_error_handler const&) = delete;
template<typename...> struct result { typedef void type; };
template<typename... T> void operator()(T&&...) const {
std::cerr << "my_error_handler invoked " << proof++ << "\n";
}
mutable int proof = 0;
};
(如您所见,我将其明确设为不可复制,以确保编译器不会在我背后悄悄生成代码。)
现在,我不确定这是否是您不小心错过的组合,但是:
on_error<fail>(function, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(start, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(gr_instruction, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
工作得很好,同样如此
auto ll = phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3);
on_error<fail>(function, ll);
on_error<fail>(start, ll);
on_error<fail>(gr_instruction, ll);
请注意,因为 bind
需要引用,所以我们需要确保 err_handler
的生命周期匹配(或超过)解析器的生命周期,所以我制作了 err_handler
解析器类的成员。
当我将输入传递给失败时,我的程序将能够打印 my_error_handler
调用的 proof
:
std::cout << "The 'proof' in the err_handler instance is: " << p.err_handler.proof << "\n";
一如既往,一个完全集成的示例程序:
#define BOOST_SPIRIT_USE_PHOENIX_V3
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
namespace asmast
{
typedef std::string label;
}
template <typename=void> struct my_error_handler {
my_error_handler() = default;
my_error_handler(my_error_handler const&) = delete;
template<typename...> struct result { typedef void type; };
template<typename... T> void operator()(T&&...) const {
std::cerr << "my_error_handler invoked " << proof++ << "\n";
}
mutable int proof = 0;
};
template <typename It, typename Skipper = qi::blank_type>
struct parser : qi::grammar<It, Skipper>
{
parser() :
parser::base_type(start)
{
using namespace qi;
start = lexeme["Func" >> !(alnum | '_')] > function;
function = gr_identifier
>> "{"
>> -(
gr_instruction
| gr_label
//| gr_vardecl
//| gr_paramdecl
) % eol
> "}";
gr_instruction_names.add("Mov", unused);
gr_instruction_names.add("Push", unused);
gr_instruction_names.add("Exit", unused);
gr_instruction = lexeme [ gr_instruction_names >> !(alnum|"_") ] > gr_operands;
gr_operands = -(gr_operand % ',');
gr_identifier = lexeme [ alpha >> *(alnum | '_') ];
gr_operand = gr_identifier | gr_string;
gr_string = lexeme [ '"' >> *("\"\"" | ~char_("\"")) >> '"' ];
gr_newline = +( char_('\r')
|char_('\n')
);
gr_label = gr_identifier >> ':' > gr_newline;
#if 1
on_error<fail>(function, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(start, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(gr_instruction, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
#else
auto ll = phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3);
on_error<fail>(function, ll);
on_error<fail>(start, ll);
on_error<fail>(gr_instruction, ll);
#endif
// more on_error<fail>s...
BOOST_SPIRIT_DEBUG_NODES((start)(function)(gr_instruction)(gr_operands)(gr_identifier)(gr_operand)(gr_string));
}
my_error_handler<> err_handler;
private:
qi::symbols<char, qi::unused_type> gr_instruction_names;
qi::rule<It, Skipper> start, function, gr_instruction, gr_operands, gr_operand, gr_string;
qi::rule<It, qi::unused_type()> gr_newline;
qi::rule<It, asmast::label(), Skipper> gr_label, gr_identifier;
};
int main()
{
typedef boost::spirit::istream_iterator It;
std::cin.unsetf(std::ios::skipws);
It f(std::cin), l;
parser<It, qi::blank_type> p;
try
{
bool ok = qi::phrase_parse(f,l,p,qi::blank);
if (ok) std::cout << "parse success\n";
else std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
std::cout << "The 'proof' in the err_handler instance is: " << p.err_handler.proof << "\n";
return ok;
} catch(const qi::expectation_failure<It>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'\n";
}
return false;
}
输入时
Func Ident{
Mov name, "hello"
Push 5
Exit
}
它打印(作为第一行/最后一行)
my_error_handler invoked 0
my_error_handler invoked 1
...
The 'proof' in the err_handler instance is: 2
[1]这不是我第一次不得不想象相关代码
关于c++ - boost spirit qi on_error 通过引用传递 error_handler struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18475965/
我正在尝试使用 Boost::Spirit::qi 的 on_error 机制来找出解析失败的原因。 我在 on_error 函数处设置了一个断点,该函数正在被调用,但没有输出(nada、nothin
我创建了一个简单的 rx 运算符,它可以将字符串流转换为 json 流,并且工作正常。但是,我希望能够引发自定义异常,但我不确定如何调用订阅的 on_error 方法 运算符称为 convertStr
Bash 中的 on_error 陷阱似乎只在定义它的函数范围内起作用。例如运行这个脚本 #!/bin/bash on_error() { echo 'on_error' } f() {
有人可以解释一下 nightwatchjs 中屏幕截图生成的 on_error 和 on_failure 之间的区别吗?我不清楚下面的解释有什么区别。 来自http://nightwatchjs.or
bool EqnExprEvaluator::SetEqn(const std::string& eqnStr) { typedef std::string::const_iterator
我又遇到了灵气障碍问题。 我在一个名为 error_handler 的仿函数结构中实现了错误处理。这通过引用传递给语法构造函数(参见 Qi 的 MiniC 示例)。 然后我有on_error s 在语
我是一名优秀的程序员,十分优秀!