我有一个非常酷的名为libcage的 C++ 库。我需要库中的类方法来从 Ruby 脚本调用。我正在使用Rice在 C++ 中构建 Ruby 扩展。
join
类中的方法cage
使用参数作为回调函数的指针。简化说明性示例使用 C++ 中的回调:
/* File example.cpp */
#include <libcage/cage.hpp>
void joinCallback(bool joinResult) {
// do something
}
int main(int argc, char *argv[]) {
libcage::cage *cage = new libcage::cage;
cage->join("localhost", 80, &joinCallback);
}
/* End of file example.cpp */
在某些事件之后,该join
方法将调用callback
函数。这没问题,但我想在 Ruby 中定义回调函数!
# File example.rb
require './rb_libcage' # C++ extension library
def joinCallback(joinResult)
# do something
end
cage = Cage.new
cage.join "localhost", 80, method(:joinCallback)
# End of file example.rb
这现在不起作用。我不知道如何callback_to_ruby
通过下面示例中的调用参数将 Ruby 方法的指针传递给 C++ 函数。
编辑:
我现在实际上有这个:
/* File rb_libcage.cpp */
#include "rice/Class.hpp"
#include "rice/Data_Type.hpp"
#include "rice/Constructor.hpp"
#include "cage.hpp"
Rice::Object __callback_to_ruby;
ID __callback_id_to_ruby;
namespace {
void c_callback(bool result)
{
if (result)
std::cout << "join: succeeded" << std::endl;
else
std::cout << "join: failed" << std::endl;
/* This does not work! */
__callback_to_ruby.call(__callback_id_to_ruby,"AsyncCallback");
/**
* terminate called after throwing an instance of 'Rice::Exception'
* what(): undefined method `call' for "AsyncCallback":String
* Aborted
*/
}
class Cage_wrapper
{
public:
Cage_wrapper() : cage(new libcage::cage()) {}
void join(std::string host, int port, Rice::Object callback_to_ruby) {
__callback_to_ruby = callback_to_ruby;
__callback_id_to_ruby = rb_intern("call");
__callback_to_ruby.call(__callback_id_to_ruby,"test"); /* This works! */
/* cage->join() will make another thread that will call ASYNCHRONOUSLY c_callback function */
cage->join(host, port, &c_callback);
}
private:
libcage::cage *cage;
};
extern "C"
void Init_rb_libcage(void)
{
RUBY_TRY
{
Rice::define_class<Cage_wrapper>("Cage")
.define_constructor(Rice::Constructor<Cage_wrapper>())
.define_method("join", &Cage_wrapper::join);
}
RUBY_CATCH
}
} // namespace
/* End of file rb_libcage.cpp */
和 Ruby 脚本:
# File example.rb
require './rb_libcage' # C++ extension library
ruby_callback = lambda {|x| print "From ruby script join result: ", x, "\n" }
cage = Cage.new
cage.join "localhost", 80, ruby_callback
puts 'After join'
# End of file example.rb
rb_libcage.cpp
可编译和运行example.rb
如下所示:
From ruby script join result: test
After join
join: succeeded
terminate called after throwing an instance of 'Rice::Exception'
what(): undefined method `call' for "AsyncCallback":String
Aborted
我可以调用同步回调但不能调用异步回调。它会失去上下文吗?如何从 C++ 代码成功调用异步 ruby 回调?
最好的问候,帕维尔
PS:我找到了关于在 Ruby C 扩展中构建异步回调的有用文章