[Rcpp-commits] r1328 - in pkg/Rcpp: . inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed May 26 18:12:41 CEST 2010


Author: romain
Date: 2010-05-26 18:12:41 +0200 (Wed, 26 May 2010)
New Revision: 1328

Modified:
   pkg/Rcpp/NEWS
   pkg/Rcpp/inst/unitTests/runit.Module.R
Log:
unit tests for class_

Modified: pkg/Rcpp/NEWS
===================================================================
--- pkg/Rcpp/NEWS	2010-05-26 15:51:41 UTC (rev 1327)
+++ pkg/Rcpp/NEWS	2010-05-26 16:12:41 UTC (rev 1328)
@@ -1,6 +1,14 @@
 0.8.1   (under development)
 
 	o	Evaluating a call inside an environment did not work properly
+	
+	o	Rcpp modules. An Rcpp module is a collection of internal (c++)
+	functions and classes that are exposed to R. Inspired from
+	Boost.Python
+	
+	Modules are created internally using the RCPP_MODULE macro and retrieved
+	in the R side with the Module function. The unit test file 
+	runit.Module.R shows an example.
 
 0.8.0   2010-05-17
 

Modified: pkg/Rcpp/inst/unitTests/runit.Module.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.R	2010-05-26 15:51:41 UTC (rev 1327)
+++ pkg/Rcpp/inst/unitTests/runit.Module.R	2010-05-26 16:12:41 UTC (rev 1328)
@@ -46,7 +46,18 @@
 		Rprintf( "hello (x = %d, y = %5.2f)\\n", x, y ) ;
 	}
 	
+	class World {
+	public:
+	    World() : msg("hello"){}
+	    void set(std::string msg) { this->msg = msg; }
+	    std::string greet() { return msg; }
 	
+	private:
+	    std::string msg;
+	};
+
+	
+	
 	RCPP_MODULE(yada){
 		using namespace Rcpp ;
 		
@@ -57,17 +68,28 @@
 		function( "bla1"  , &bla1   ) ;
 		function( "bla2"  , &bla2   ) ;
 		
+		class_<World>( "World" )
+			.method( "greet", &World::greet )
+			.method( "set", &World::set )
+		;
+
 	}                     
 	
 	'
 	fx <- cppfunction( signature(), "" , include = inc )
 	
-	mod <- Module( "yada", getDLL(fx) )
+	mod <- Module( "yada", getDynLib(fx) )
 	checkEquals( mod$bar( 2L ), 4L )
 	checkEquals( mod$foo( 2L, 10.0 ), 20.0 )
 	checkEquals( mod$hello(), "hello" )
 	checkEquals( capture.output( mod$bla() ), "hello" )
 	checkEquals( capture.output( mod$bla1(2L) ), "hello (x = 2)" )
     checkEquals( capture.output( mod$bla2(2L, 5.0) ), "hello (x = 2, y =  5.00)" )
-   
+    
+    World <- mod$World
+    w <- new( World )
+    checkEquals( w$greet(), "hello" )
+    w$set( "hello world" ) 
+    checkEquals( w$greet(), "hello world" )
+    
 }



More information about the Rcpp-commits mailing list