Skip to content

Instantly share code, notes, and snippets.

Created November 13, 2009 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/234089 to your computer and use it in GitHub Desktop.
Save anonymous/234089 to your computer and use it in GitHub Desktop.
require 'mkmf-rice'
create_makefile('virtual')
require 'virtual'
class Toto < VirtualBase
def initialize
super
puts "init"
end
def process_worker
puts "worker processing"
end
end
b = Toto.new
b.do_work
b.process_worker
puts "=========="
v = VirtualBase.new
v.do_work
v.process_worker
#include <iostream>
class VirtualBase {
public:
VirtualBase() { std::cout << "init from VirtualBase" << std::endl; }
virtual int doWork() {
std::cout << "doing work from c++" << std::endl;
return 0;
}
virtual int processWorker() = 0;
};
#include "rice/Director.hpp"
using namespace Rice;
class VirtualBaseProxy : public VirtualBase, public Rice::Director {
public:
VirtualBaseProxy(Object self) : Rice::Director(self) { }
virtual int doWork() {
std::cout << "salut!!" << std::endl;
if(callIsFromRuby("do_work")) {
std::cout << "hello!!" << std::endl;
return VirtualBase::doWork();
} else {
return from_ruby<int>( getSelf().call("do_work") );
}
}
virtual int processWorker() {
if(callIsFromRuby("process_worker")) {
raisePureVirtual();
} else {
return from_ruby<int>( getSelf().call("process_worker") );
}
}
};
#include "rice/Constructor.hpp"
extern "C"
void Init_virtual() {
// See Caveat below
define_class<VirtualBase>("__VirtualBase__");
// .define_method("do_work", &VirtualBase::doWork);
define_class<VirtualBaseProxy, VirtualBase>("VirtualBase")
.define_constructor(Constructor<VirtualBaseProxy, Object>())
.define_method("do_work", &VirtualBaseProxy::doWork)
.define_method("process_worker", &VirtualBaseProxy::processWorker);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment