[Rcpp-devel] reference classes in C++ :is it possible?

Steven Varga steven.varga at gmail.com
Thu Feb 28 10:44:27 CET 2013


Hello,

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?

Rcpp:::Function() is quite useful to "extend" or  create "callbacks" for
c++ classes/methods;  I am looking for a mechanism similar to Rcpp::Function
which works on R Reference objects.

as example show it is possible to extend C++ class exported with Rcpp then
call inherited methods
cheers,
steve
---------------------------------------------------------- example
----------------------------------------------------------------------
#################
## setup:
#################
# create package
Rcpp.package.skeleton("inheritence", module=T)

# edit rcpp_module.cpp
# by extending World in an intrusive way
--------- begin c++: rcpp_module ---------------

    World( SEXP xptr_) : msg("hello"){

        Rcpp::XPtr<World> xptr(xptr_);
        this->msg = xptr->msg;
    }

// now we can pass a recreated "World" object
// being aware this is nothing but raw pointer with "World" jacket pulled on
    void callback( World world, std::string msg ){
        Rprintf("message: %s\n", msg.data() );
        world.set( msg );  // and therefore this can't really callback the
extended generated Reference object :(
    }

// don't forget to export it
.method( "callback", &World::callback , "callback" )
------- end c++ -----------

#install it from CMD prompt/console
R --vanilla CMD INSTALL inheritence

brother <- setRefClass("brother",
    contains=World,
    methods = list(
          deal = function() {
                message("George Orwell")
          },

          set = function( msg ) {

                message( "1984" )
                callSuper( msg )
          }
    ))

big <- brother$new()

# works as adverised
brave <- new ( World )
brave$set(  'Aldous Huxley' )

big$deal() # reachable
######################################################################
# DOES WORK :) so one can extend the generated R ref class in R and call
super
#####################################################################
big$set(" with smile ") # reachable
big$greet()                 # worked

######################################################################
# DOES NOT WORK :(  as the xptr is still the same object  c++ runtime
instatiated
#####################################################################
big$callback( big at .xData$.pointer, " hmmm "  )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130228/294947f9/attachment.html>


More information about the Rcpp-devel mailing list