[Rcpp-commits] r1415 - in pkg/Rcpp/inst: include/Rcpp/module unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 4 11:42:18 CEST 2010


Author: romain
Date: 2010-06-04 11:42:17 +0200 (Fri, 04 Jun 2010)
New Revision: 1415

Modified:
   pkg/Rcpp/inst/include/Rcpp/module/Module_Add_Property.h
   pkg/Rcpp/inst/include/Rcpp/module/Module_Property.h
   pkg/Rcpp/inst/unitTests/runit.Module.R
Log:
taking const-ness into account in property getters

Modified: pkg/Rcpp/inst/include/Rcpp/module/Module_Add_Property.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/module/Module_Add_Property.h	2010-06-04 09:35:05 UTC (rev 1414)
+++ pkg/Rcpp/inst/include/Rcpp/module/Module_Add_Property.h	2010-06-04 09:42:17 UTC (rev 1415)
@@ -27,6 +27,12 @@
 		AddProperty( name, new CppProperty_GetMethod<Class,PROP>(GetMethod) ) ;
 		return *this ;
 	}
+
+	template <typename PROP>
+	self& property( const char* name, PROP (Class::*GetMethod)(void) const ){
+		AddProperty( name, new CppProperty_GetConstMethod<Class,PROP>(GetMethod) ) ;
+		return *this ;
+	}
 	
 	template <typename PROP>
 	self& property( const char* name, PROP (*GetMethod)(Class*) ){
@@ -43,7 +49,16 @@
 		) ;
 		return *this ;
 	}
+	template <typename PROP>
+	self& property( const char* name, PROP (Class::*GetMethod)(void) const, void (Class::*SetMethod)(PROP) ){
+		AddProperty( 
+			name, 
+			new CppProperty_GetConstMethod_SetMethod<Class,PROP>(GetMethod, SetMethod)
+		) ;
+		return *this ;
+	}
 	
+	
 	template <typename PROP>
 	self& property( const char* name, PROP (Class::*GetMethod)(void), void (*SetMethod)(Class*,PROP) ){
 		AddProperty( 
@@ -52,7 +67,16 @@
 		) ;
 		return *this ;
 	}
+	template <typename PROP>
+	self& property( const char* name, PROP (Class::*GetMethod)(void) const , void (*SetMethod)(Class*,PROP) ){
+		AddProperty( 
+			name, 
+			new CppProperty_GetConstMethod_SetPointer<Class,PROP>(GetMethod, SetMethod)
+		) ;
+		return *this ;
+	}
 	
+	
 	template <typename PROP>
 	self& property( const char* name, PROP (*GetMethod)(Class*), void (Class::*SetMethod)(PROP) ){
 		AddProperty( 

Modified: pkg/Rcpp/inst/include/Rcpp/module/Module_Property.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/module/Module_Property.h	2010-06-04 09:35:05 UTC (rev 1414)
+++ pkg/Rcpp/inst/include/Rcpp/module/Module_Property.h	2010-06-04 09:42:17 UTC (rev 1415)
@@ -39,6 +39,24 @@
 				
 } ;
 
+// getter through a const member function
+template <typename Class, typename PROP>
+class CppProperty_GetConstMethod : public CppProperty<Class> {
+	public: 	
+		typedef PROP (Class::*GetMethod)(void) const ;
+		typedef CppProperty<Class> prop_class ;
+
+		CppProperty_GetConstMethod( GetMethod getter_ ) : getter(getter_){}
+		
+		SEXP get(Class* object) throw(std::range_error){ return Rcpp::wrap( (object->*getter)() ) ; }
+		void set(Class*, SEXP) throw(std::range_error){ throw std::range_error("property is read only") ; }		
+
+	private:
+		GetMethod getter ;
+				
+} ;
+
+
 // getter through a free function taking a pointer to Class
 template <typename Class, typename PROP>
 class CppProperty_GetPointerMethod : public CppProperty<Class> {
@@ -81,7 +99,33 @@
 		SetMethod setter ;
 				
 } ;
+template <typename Class, typename PROP>
+class CppProperty_GetConstMethod_SetMethod : public CppProperty<Class> {
+	public: 	
+		typedef PROP (Class::*GetMethod)(void) const ;
+		typedef void (Class::*SetMethod)(PROP) ;
+		typedef CppProperty<Class> prop_class ;
 
+		CppProperty_GetConstMethod_SetMethod( GetMethod getter_, SetMethod setter_) : getter(getter_), setter(setter_){}
+		
+		SEXP get(Class* object) throw(std::range_error){ 
+			return Rcpp::wrap( (object->*getter)() ) ; 
+		}
+		void set(Class* object, SEXP value) throw(std::range_error){ 
+			(object->*setter)( 
+				Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >( value )
+			) ;
+		}		
+
+	private:
+		GetMethod getter ;
+		SetMethod setter ;
+				
+} ;
+
+
+
+
 // getter though a member function, setter through a pointer function
 template <typename Class, typename PROP>
 class CppProperty_GetMethod_SetPointer : public CppProperty<Class> {
@@ -106,7 +150,30 @@
 		SetMethod setter ;
 				
 } ;
+template <typename Class, typename PROP>
+class CppProperty_GetConstMethod_SetPointer : public CppProperty<Class> {
+	public: 	
+		typedef PROP (Class::*GetMethod)(void) const ;
+		typedef void (*SetMethod)(Class*,PROP) ;
+		typedef CppProperty<Class> prop_class ;
 
+		CppProperty_GetConstMethod_SetPointer( GetMethod getter_, SetMethod setter_) : getter(getter_), setter(setter_){}
+		
+		SEXP get(Class* object) throw(std::range_error){ 
+			return Rcpp::wrap( (object->*getter)() ) ;
+		}
+		void set(Class* object, SEXP value) throw(std::range_error){ 
+			setter( object, 
+				Rcpp::as< typename Rcpp::traits::remove_const_and_reference< PROP >::type >( value )
+			) ;
+		}		
+
+	private:
+		GetMethod getter ;
+		SetMethod setter ;
+				
+} ;
+
 // getter through pointer function, setter through member function
 template <typename Class, typename PROP>
 class CppProperty_GetPointer_SetMethod : public CppProperty<Class> {

Modified: pkg/Rcpp/inst/unitTests/runit.Module.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.R	2010-06-04 09:35:05 UTC (rev 1414)
+++ pkg/Rcpp/inst/unitTests/runit.Module.R	2010-06-04 09:42:17 UTC (rev 1415)
@@ -166,35 +166,45 @@
 test.Module.property <- function(){
 
 	inc  <- '
-	
-	class World {
+	class Num{
 	public:
-	    World() : msg("hello"){}
-	    void set(std::string msg) { this->msg = msg; }
-	    std::string greet() { return msg; }
-	
+	    Num() : x(0.0), y(0){} ;
+	    
+	    double getX() const { return x ; }
+	    void setX(double value){ x = value ; }
+	    
+	    int getY() { return y ; }
+	    
 	private:
-	    std::string msg;
+	    double x ;
+	    int y ;
 	};
-
+	
 	RCPP_MODULE(yada){
 		using namespace Rcpp ;
+	
+		class_<Num>( "Num" )
 		
-		class_<World>( "World" )
-			.property( "msg", &World::greet, &World::set ) 
+			// 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) )
-	World <- mod$World
-    w <- new( World )
-    checkEquals( w$msg, "hello" )
-    w$msg <- "hello world"
-    checkEquals( w$msg, "hello world" )
+	Num <- mod$Num
+    w <- new( Num )
+    checkEquals( w$x, 0.0 )  
+    checkEquals( w$y, 0L )  
+    
+    w$x <- 2.0
+    checkEquals( w$x, 2.0 )  
+    
+    checkException( { w$y <- 3 } )
 }
 
 }



More information about the Rcpp-commits mailing list