[Rcpp-devel] nicer show method for C++Class

Romain Francois romain at r-enthusiasts.com
Mon Nov 22 19:48:37 CET 2010


Le 22/11/10 16:08, Romain Francois a écrit :
> Hello,
>
> I've modified the show method for C++Class so that it prints more
> information:
 > [...]
> Things to do:
> - maybe pull out the const-ness of methods

I've done the two items below :
> - grab reflection (typenames of parameters) from constructors
> - add docstring to constructors

So now we can document constructors:

require( Rcpp )
require( inline )
inc <- '

class Simple {
public:
     Simple( ) : d_x(10.0){} ;
     Simple( double x ) : d_x(x){} ;

     const double& x(){ return d_x ;}

     void move( double x ){ d_x += x ; }

private:
     double d_x ;
		
} ;

double bla( Simple* data ){ return 3; }

RCPP_MODULE(mod){

	class_<Simple>( "Simple" )

	    .constructor( "default constructor" )
	    .constructor<double>( "bla bla bla" )
	
		.property( "x" , &Simple::x )
		.method( "move", &Simple::move, "some documentation for the move 
method" )
		.method( "bla", &bla, "bla bla" )
		;
		

}
'

fx <- cxxfunction( , '', includes = inc, plugin = "Rcpp" )
mod <- Module( "mod", getDynLib( fx ) )
Simple <- mod$Simple

 > show(Simple)
C++ class 'Simple' <0x1005228e0>
Constructors:
     Simple()
         docstring : default constructor
     Simple(double)
         docstring : bla bla bla

Fields:
     double x [readonly]

Methods:
      double bla()
            docstring : bla bla
      void move(double)
            docstring : some documentation for the move method


Formatting advice are still welcome, even more so if they come with an 
implementation. The relevant code is the show method for the C++Class 
class in 01_show.R ;-)

As for the doc strings in methods, this has the effect of sliding the 
constructor validator to the right.

I'll work on this one later

 > - add a docstring to fields

Romain ... still on a roll

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/9VOd3l : ZAT! 2010
|- http://bit.ly/c6DzuX : Impressionnism with R
`- http://bit.ly/czHPM7 : Rcpp Google tech talk on youtube




More information about the Rcpp-devel mailing list