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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 11 08:19:01 CEST 2010


Author: romain
Date: 2010-06-11 08:19:01 +0200 (Fri, 11 Jun 2010)
New Revision: 1512

Added:
   pkg/Rcpp/inst/include/Rcpp/module/Module_Field.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Module.h
   pkg/Rcpp/inst/unitTests/runit.Module.R
Log:
support for exposing public data members

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-06-11 06:10:34 UTC (rev 1511)
+++ pkg/Rcpp/inst/ChangeLog	2010-06-11 06:19:01 UTC (rev 1512)
@@ -1,3 +1,11 @@
+2010-06-11  Romain Francois <romain at r-enthusiasts.com>
+
+	* R/help.R: workaround to allow the syntax "Rcpp ? something" to 
+	bring Rcpp documentation
+
+	* inst/include/Rcpp/Module.h: support for exposing public data 
+	members
+
 2010-06-09  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.8.2

Modified: pkg/Rcpp/inst/include/Rcpp/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Module.h	2010-06-11 06:10:34 UTC (rev 1511)
+++ pkg/Rcpp/inst/include/Rcpp/Module.h	2010-06-11 06:19:01 UTC (rev 1512)
@@ -277,9 +277,11 @@
 	VOID_END_RCPP
 	}
 
+#include <Rcpp/module/Module_Field.h>
+
 #include <Rcpp/module/Module_Add_Property.h>
-	
-	
+
+
 private:
 	METHOD_MAP methods ;
 	PROPERTY_MAP properties ;
@@ -293,7 +295,6 @@
 template <typename Class> 
 class_<Class>* class_<Class>::singleton ;
 
-
 // function factories
 #include <Rcpp/module/Module_generated_function.h>
 

Added: pkg/Rcpp/inst/include/Rcpp/module/Module_Field.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/module/Module_Field.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/module/Module_Field.h	2010-06-11 06:19:01 UTC (rev 1512)
@@ -0,0 +1,73 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Module_Add_Property.h: Rcpp R/C++ interface class library -- Rcpp modules
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp_Module_Field_h
+#define Rcpp_Module_Field_h
+	
+	// getter through a member function
+	template <typename PROP>
+	class CppProperty_Getter_Setter : public CppProperty<Class> {
+		public: 	
+			typedef PROP Class::*pointer ;
+			
+			CppProperty_Getter_Setter( pointer ptr_ ) : ptr(ptr_) {}
+			
+			SEXP get(Class* object) throw(std::range_error){ return Rcpp::wrap( object->*ptr ) ; }
+			void set(Class* object, SEXP value) throw(std::range_error){ object->*ptr = Rcpp::as<PROP>( value ) ; }		
+	
+		private:
+			pointer ptr ;
+	} ;
+	
+	
+	// getter through a member function
+	template <typename PROP>
+	class CppProperty_Getter : public CppProperty<Class> {
+		public: 	
+			typedef PROP Class::*pointer ;
+			
+			CppProperty_Getter( pointer ptr_ ) : ptr(ptr_) {}
+			
+			SEXP get(Class* object) throw(std::range_error){ return Rcpp::wrap( object->*ptr ) ; }
+			void set(Class* object, SEXP value) throw(std::range_error){ throw std::range_error("read only data member") ; }		
+	
+		private:
+			pointer ptr ;
+	} ;
+
+	
+	template <typename T>
+	self& field( const char* name_, T Class::*ptr ){
+		AddProperty( name_, 
+			new CppProperty_Getter_Setter<T>( ptr )
+		) ;
+		return *this ;
+	}
+	
+	template <typename T>
+	self& field_readonly( const char* name_, T Class::*ptr ){
+		AddProperty( name_, 
+			new CppProperty_Getter<T>( ptr )
+		) ;
+		return *this ;
+	}
+
+#endif

Modified: pkg/Rcpp/inst/unitTests/runit.Module.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.R	2010-06-11 06:10:34 UTC (rev 1511)
+++ pkg/Rcpp/inst/unitTests/runit.Module.R	2010-06-11 06:19:01 UTC (rev 1512)
@@ -209,4 +209,44 @@
     checkException( { w$y <- 3 } )
 }
 
+          
+test.Module.member <- function(){
+
+	inc  <- '
+	class Num{
+	public:
+	    Num() : x(0.0), y(0){} ;
+	    	    
+	    double x ;
+	    int y ;
+	};
+	
+	RCPP_MODULE(yada){
+		using namespace Rcpp ;
+	
+		class_<Num>( "Num" )
+		
+			// read and write data member
+			.field( "x", &Num::x )
+			
+			// read only data member
+			.field_readonly( "y", &Num::y )
+		;
+	}
+	'
+	fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )
+	
+	mod <- Module( "yada", getDynLib(fx) )
+	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