[Rcpp-devel] Rcpp : passing user-defined c++ class

Dirk Eddelbuettel edd at debian.org
Tue Feb 21 23:23:05 CET 2017


On 21 February 2017 at 14:15, Yousra El Bachir wrote:
| I am calling from R (say funcR) a c++ function (say funcCpp) which needs either
| some programmer-defined c++ class (say programmerClassCpp1, 2, etc...) or a
| user-defined c++ class (say userClassCpp). Here is an illustration of what I am
| doing:
| 
| funcR(..., class="ClassCpp"){
|   
|   # do something and then call the c++ function
|    
|    funcCpp(class);
| }
| 
| funcCpp(std::string classCpp){
| 
|   switch(classCpp){
|       case "class1" : point to the programmer defined c++
| class programmerClassCpp1; break;
|       case "class2" : point to the programmer defined c++
| class programmerClassCpp2; break;
|       case "new": point to the user defined c++ class userClassCpp; break; 
|       default : Rcpp::stop("not supported class");
|      };
| }
| 
| This post  
| 
|   http://gallery.rcpp.org/articles/passing-cpp-function-pointers/
| 
| reflects what I need for a function and I am wondering whether it is possible
| to do this for a class. The obvious solution is to replicate manually what has
| been done in the post to each method of the class (for instance, work on a
| vector of functors to the class methods), but I don't see it as the optimal
| solution.

>From and to R you can exchange only what R supports in types (ie integer,
double, char,...) plus aggregates thereof (vector,list,...) -- or external
pointer types.

But if you can work from R with "tokens", ie like your 'std::string classCpp'
then you can indeed branch and instanttiate your different classes
conditionally. Maybe their results can be reduced to standard types, and you
can proceed from there.

Maybe you can describe or mock your design more clearly. It's a little too
vagie to help more right now.

Dirk


-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list