[Rcpp-devel] Inline, templates, and easy debugging

Romain Francois romain at r-enthusiasts.com
Thu Nov 22 09:48:30 CET 2012


Le 22/11/12 03:59, John Merrill a écrit :
> tl;dr summary -- is there a way to incorporate multiple functions into
> an inline call?  Specifically, is there a way to include a templated
> function in an inline compilation?
>
> Consider the following outline:
>
> template <typename T>
> void InsertTypedVector<const T& src, T* dest) {
>    < stuff >
> }
>
> RcppExport SEXP ExposedFunction(SEXP r_foobar) {
>    DataFrame df(r_foobar);
>
>    for (int i = 0; i < df.size(); ++i) {
>      RObject current_robj=df(i);
>
>      switch (TYPEOF(current_robj)) {
>        case CHARSXP:
>         {
>           StringVector current_str = current_robj;
>
>           InsertTypedVector(current_str...)
>          }
>
>          break;
>
>       case REALSXP:
>         {
>            NumericVector current_num = current_robj;
>
>            InsertTypedVector(current_num...)
>
>         }
> etc.
>
>
> The point of this code is that I'm trying to write a function which
> does the homologous thing to each column of a dataframe in a
> type-sensitive way -- treating strings as strings, reals as reals,
> complex values as complex values, etc.  That's cool, and it's what C++
> is written to support.
>
> Here's the problem: in order to compile this, I need it to be in a
> package.  Now, obviously, since I'm building a package, that is no
> problem in itself.  What I want to be able to do is DEVELOP this kind
> of code in Inline, and then transform it into package code after I'm
> done debugging it.
>
> Compiling this using inline is not trivial: embedded classes are not
> allowed to contain templated functions, so I can't hack the generic
> function in as a static or dynamic member function inside the body of
> the function I provide as an argument to inline.
>
> Is there a way to do this using inline?

I'm not sure I understand what the problem is. I guess what you ant to 
do is perfectly doable in inline, we tend to use sourceCpp now, e.g.

sourceCpp( code = '
#include <Rcpp.h>
using namespace Rcpp ;

template <typename T>
void dealWith( const T& object ){
     Rcout << "dealing with an object of type: " << DEMANGLE(T) << 
std::endl ;
}

// [[Rcpp::export]]
void ExposedFunction( DataFrame df ){
     int n = df.size() ;
     for( int i=0; i<n; i++){
         RObject current_robj=df(i);
         switch( current_robj.sexp_type() ){
         case INTSXP:
             {
             dealWith( IntegerVector( current_robj ) ) ;
             break;
             }
         case STRSXP:
             {
             dealWith( CharacterVector( current_robj ) ) ;
             break;
             }
         default:
             break;
         }

     }
}
')

or :

sourceCpp( "somefile.cpp" )


 > df <- data.frame( a = integer(10), b = character(10), 
stringsAsFactors = FALSE )
 > ExposedFunction( df )
dealing with an object of type: Rcpp::Vector<13>
dealing with an object of type: Rcpp::Vector<16>

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