[Rcpp-commits] r447 - in pkg: inst inst/unitTests src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 25 10:43:03 CET 2010


Author: romain
Date: 2010-01-25 10:43:02 +0100 (Mon, 25 Jan 2010)
New Revision: 447

Modified:
   pkg/inst/ChangeLog
   pkg/inst/unitTests/runit.environments.R
   pkg/src/Rcpp/Environment.h
Log:
Environment::assign becomes a template

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-25 09:11:08 UTC (rev 446)
+++ pkg/inst/ChangeLog	2010-01-25 09:43:02 UTC (rev 447)
@@ -1,3 +1,15 @@
+2010-01-25  Romain Francois <francoisromain at free.fr>
+
+	* src/Rcpp/Environment.h: Environment::assign gains a templated
+	form so that we can assign anything that can be wrapped to 
+	a name in an environment
+
+	* inst/unitTests/runit.environments.R: new unit test 
+	'test.environment.assign.templated' to test the templated assign
+
+	* src/Makevars(.win?): listen to the RCPP_CXX0X environment variable
+	and if set to "yes" attempt to add c++0x support if possible
+
 2010-01-24  Romain Francois <francoisromain at free.fr>
 
 	* src/SimpleVector.h : new template to replace simple vector 

Modified: pkg/inst/unitTests/runit.environments.R
===================================================================
--- pkg/inst/unitTests/runit.environments.R	2010-01-25 09:11:08 UTC (rev 446)
+++ pkg/inst/unitTests/runit.environments.R	2010-01-25 09:43:02 UTC (rev 447)
@@ -101,16 +101,38 @@
 
 }
 
+test.environment.assign.templated <- function(){
+	
+	funx <- cfunction(signature(x="environment", name = "character", object = "ANY" ), '
+	Rcpp::Environment env(x) ;
+	std::string st = Rcpp::wrap(name).asStdString() ;
+	return Rcpp::wrap( env.assign(st, object) ) ;
+	', Rcpp=TRUE, verbose=FALSE)
+	
+	e <- new.env( )
+	
+	
+}
+
 test.environment.isLocked <- function(){
 	funx <- cfunction(signature(x="environment" ), '
 	Rcpp::Environment env(x) ;
-	return Rcpp::wrap( env.isLocked() ) ;
+	env.assign( "x1", 1 ) ;
+	env.assign( "x2", 10.0 ) ;
+	env.assign( "x3", std::string( "foobar" ) ) ;
+	env.assign( "x4", "foobar" ) ;
+	std::vector< std::string > aa(2) ; aa[0] = "foo" ; aa[1] = "bar" ;
+	env.assign( "x5", aa ) ;
+	return R_NilValue ;
 	', Rcpp=TRUE, verbose=FALSE)
 	
 	e <- new.env()
-	checkTrue( !funx(e), msg = "Environment::isLocked( new.env) -> false" )
-	checkTrue( funx(asNamespace("Rcpp")), msg = "Environment::isLocked( namespace:Rcpp ) -> true" )
-	
+	funx(e)
+	checkEquals( e[["x1"]], 1L  , msg = "Environment::assign( int ) " )
+	checkEquals( e[["x2"]], 10.0, msg = "Environment::assign( double ) " )
+	checkEquals( e[["x3"]], "foobar", msg = "Environment::assign( char* ) " )
+	checkEquals( e[["x4"]], "foobar", msg = "Environment::assign( std::string ) " )
+	checkEquals( e[["x5"]], c("foo", "bar" ), msg = "Environment::assign( vector<string> ) " )
 }
 
 test.environment.bindingIsActive <- function(){

Modified: pkg/src/Rcpp/Environment.h
===================================================================
--- pkg/src/Rcpp/Environment.h	2010-01-25 09:11:08 UTC (rev 446)
+++ pkg/src/Rcpp/Environment.h	2010-01-25 09:43:02 UTC (rev 447)
@@ -212,7 +212,7 @@
     	     */
     	    template <typename T>
     	    Binding& operator=(const T& rhs){
-    	    	    env.assign( name, wrap(rhs) ) ;
+    	    	    env.assign( name, wrap(rhs).asSexp() ) ;
     	    	    return *this ;
     	    }
     	    
@@ -346,6 +346,20 @@
     bool assign( const std::string& name, SEXP x ) const throw(binding_is_locked) ;
     
     /**
+     * wrap and assign. If there is a wrap method taking an object 
+     * of WRAPPABLE type, then it is wrapped and the corresponding SEXP
+     * is assigned in the environment
+     *
+     * @param name name of the object to assign
+     * @param x wrappable object. anything that has a wrap( WRAPPABLE ) is fine
+     */
+    template <typename WRAPPABLE>
+    bool assign( const std::string& name, const WRAPPABLE& x) const throw(binding_is_locked){
+    	    SEXP y = wrap( x ).asSexp() ;
+    	    return assign( name, y ) ;
+    }
+    
+    /**
      * @return true if this environment is locked
      * see ?environmentIsLocked for details of what this means
      */



More information about the Rcpp-commits mailing list