[Rcpp-devel] C++ class data members exposed as properties

Romain Francois romain at r-enthusiasts.com
Fri Jun 4 11:49:00 CEST 2010


Hello,

I've added support for properties in classes that are exposed by Rcpp 
modules.

Here is a full example that works with inline 0.3.5 and the svn version 
of Rcpp:

	inc  <- '
	class Num{
	public:
	    Num() : x(0.0), y(0){} ;
	
	    double getX() const { return x ; }
	    void setX(double value){ x = value ; }
	
	    int getY() { return y ; }
	
	private:
	    double x ;
	    int y ;
	};
	
	RCPP_MODULE(yada){
		using namespace Rcpp ;
	
		class_<Num>( "Num" )
		
			// read and write property
			.property( "x", &Num::getX, &Num::setX )
			
			// read-only property
			.property( "y", &Num::getY )
		;
	}
	'
	fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )



 > mod <- Module( "yada", getDynLib(fx) )
 > Num <- mod$Num
 >     w <- new( Num )
 > w$x
[1] 0
 > w$x <- 3
 > w$x
[1] 3
 > w$y
[1] 0
 > w$y <- 6L
Erreur dans `$<-`(`*tmp*`, "y", value = 6L) : property is read only

The vignette (http://addictedtor.free.fr/misc/rcpp/Rcpp-modules.pdf) has 
been updated to document this.

For public data members, I might do what Boost.Python does later:
http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_data_members


Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/c6YnCi : graph gallery collage
|- http://bit.ly/bZ7ltC : inline 0.3.5
`- http://bit.ly/8YUsiC : highlight 0.2-0



More information about the Rcpp-devel mailing list