Skip to content

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

1 message · Romain Francois

#
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