[Rcpp-devel] Rcpp: Distinguishing between input types to function call

Romain Francois romain at r-enthusiasts.com
Tue Dec 4 13:21:51 CET 2012


Le 04/12/12 13:05, Christian Gunning a écrit :
> Just curious (and trying to learn the patterns):
> Is this a potential candidate for a functor/templated class,
> particularly if topoSort() was called repeatedly for a particular
> instantiation?
> ...I feel like I almost get this.
>
> thanks,
> -Christian
>
>> and two other functions to instantiate the template:
>>
>> RcppExport SEXP C_topoSort_st ( SEXP XX_ ){
>>       return topoSort< Eigen::Map<Eigen::MatrixXi> >( XX_ ) ;
>> }
>>
>> RcppExport SEXP C_topoSort_sp ( SEXP XX_ ){
>>       return topoSort< Eigen::MappedSparseMatrix<double> >( XX_ ) ;
>> }
>>
>> or Perhaps you would have one instead of the two, something like this:
>>
>> RcppExport SEXP topoSort_facade ( SEXP XX_ ){
>>
>>          if( Rf_inherits( XX_, "dgCMatrix" ) ){
>>                  return topoSort< Eigen::MappedSparseMatrix<double> >( XX_ ) ;
>>          } else {
>>                  return topoSort< Eigen::Map<Eigen::MatrixXi> >( XX_ ) ;
>>          }
>> }

I'm not sure I understand what you mean. I use functors whenever the 
function needs to hold its own data, something close to e.g. lexical 
scoping in R.

The issue here is that what we get from the R side is always a "SEXP", 
so we need to use this if/else code to disambiguate it at runtime and 
then let the template do its job.

Romain


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com
`- http://bit.ly/SweN1Z : SuperStorm Sandy

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible



More information about the Rcpp-devel mailing list