Skip to content
Prev 2301 / 10988 Next

[Rcpp-devel] Exposing constructors with same number of arguments with different types using Rcpp Modules

There is a mechanism for dispatching to the appropriate constructor, but 
this needs extra work from you on the C++ side. I'm currently not 
entirely happy about it, and this might change in the future for 
something nicer.

Some information about it was posted on this thread:
http://article.gmane.org/gmane.comp.lang.r.rcpp/929/match=constructors

You essentially have to supply an extra function that decides if the 
constructor is appropriate for the arguments that are passed in:

So your example would become something like this:

library('inline')
library('Rcpp')

test_code <-'
using namespace Rcpp;

class Uniform {
     public:
         Uniform(double min_, double max_) : min(min_), max(max_) {}
         Uniform(double max_, std::string dummy_) : min(0.0), max(max_){
             Rprintf("%s\\n", dummy_.c_str());
         }
         Uniform(double max_ ) : min(0.0), max(max_) {}

         NumericVector draw(int n) const {
             RNGScope scope;
             return runif( n, min, max );
         }

         double min, max;
};

double range( Uniform* w) {
     return w->max - w->min;
}

bool fun1( SEXP* args, int nargs){
     if( nargs != 2 ) return false ;
     if( TYPEOF(args[1]) == STRSXP ) return false ;
     return true ;
}
bool fun2( SEXP* args, int nargs){
     if( nargs != 2 ) return false ;
     if( TYPEOF(args[1]) != STRSXP ) return false ;
     return true ;
}

RCPP_MODULE(unif_module) {
     class_<Uniform>( "Uniform" )

     .constructor<double,double>("", &fun1 )
     .constructor<double,std::string>("", &fun2)
     .constructor<double>()

     .field( "min", &Uniform::min )
     .field( "max", &Uniform::max )

     .method( "draw", &Uniform::draw )
     .method( "range", &range )
     ;
}
'

fx <- cxxfunction( signature(), "" , include = test_code, plugin = "Rcpp" )
unif_module <- Module( "unif_module", getDynLib(fx) )
Uniform <- unif_module$Uniform

u1 <- new( Uniform, 0, 10 )
u1$min
u1$max
u1$range()
u1$draw( 10L )

u2 <- new( Uniform, 10, "test" )
u2$min
u2$max
u2$range()
u2$draw( 10L )

u3 <- new( Uniform, 10 )
u3$min
u3$max
u3$range()
u3$draw( 10L )


Romain


Le 13/05/11 16:35, Jelmer Ypma a ?crit :