Skip to content
Prev 9540 / 10988 Next

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

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.
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