Hello,<br><br>I am trying to create an abstract pattern using R reference objects in C++; anyone knows if this is possible/not possible? Where to look? <br><br>Rcpp:::Function() is quite useful to "extend" or create "callbacks" for c++ classes/methods; I am looking for a mechanism similar to Rcpp::Function<br>
which works on R Reference objects.<br> <br>as example show it is possible to extend C++ class exported with Rcpp then call inherited methods<br>cheers,<br>steve<br>---------------------------------------------------------- example ----------------------------------------------------------------------<br>
#################<br>## setup: <br>#################<br># create package <br>Rcpp.package.skeleton("inheritence", module=T)<br><br># edit rcpp_module.cpp<br># by extending World in an intrusive way<br>--------- begin c++: rcpp_module ---------------<br>
<br> World( SEXP xptr_) : msg("hello"){<br> <br> Rcpp::XPtr<World> xptr(xptr_);<br> this->msg = xptr->msg;<br> }<br><br>// now we can pass a recreated "World" object<br>
// being aware this is nothing but raw pointer with "World" jacket pulled on<br> void callback( World world, std::string msg ){<br> Rprintf("message: %s\n", msg.data() );<br> world.set( msg ); // and therefore this can't really callback the extended generated Reference object :(<br>
}<br><br>// don't forget to export it<br>.method( "callback", &World::callback , "callback" )<br>------- end c++ -----------<br><br>#install it from CMD prompt/console<br>R --vanilla CMD INSTALL inheritence<br>
<br>brother <- setRefClass("brother",<br> contains=World,<br> methods = list(<br> deal = function() {<br> message("George Orwell")<br> },<br> <br> set = function( msg ) {<br>
<br> message( "1984" )<br> callSuper( msg )<br> }<br> ))<br><br>big <- brother$new()<br><br># works as adverised<br>brave <- new ( World )<br>brave$set( 'Aldous Huxley' ) <br>
<br>big$deal() # reachable<br>######################################################################<br># DOES WORK :) so one can extend the generated R ref class in R and call super<br>#####################################################################<br>
big$set(" with smile ") # reachable<br>big$greet() # worked<br><br>######################################################################<br># DOES NOT WORK :( as the xptr is still the same object c++ runtime instatiated<br>
#####################################################################<br>big$callback( big@.xData$.pointer, " hmmm " )<br>