[Rcpp-commits] r448 - in pkg: inst src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 25 11:11:49 CET 2010


Author: romain
Date: 2010-01-25 11:11:48 +0100 (Mon, 25 Jan 2010)
New Revision: 448

Modified:
   pkg/inst/ChangeLog
   pkg/src/Rcpp/Environment.h
   pkg/src/Rcpp/as.h
Log:
smarter as<> and smarter lhs behavior for Environment::operator[]

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-25 09:43:02 UTC (rev 447)
+++ pkg/inst/ChangeLog	2010-01-25 10:11:48 UTC (rev 448)
@@ -1,8 +1,18 @@
 2010-01-25  Romain Francois <francoisromain at free.fr>
 
+	* src/Rcpp/as.h: smarter template as<T>, now the template 
+	attempts to create an object by passing the SEXP as the first 
+	parameter, so any class that has a constructor taking a SEXP
+	can be as<>'ed. see item below for how this can be useful
+
+	* src/Rcpp/Environment.h: lhs use of Environment::Binding is now
+	a bit smarter and uses as<> to implicitely convert the SEXP
+	to the requested type, so that if as<Foo> makes sense, this 
+	works: Foo foo = env["x"] ; 
+
 	* 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
+	a name in an environment. 
 
 	* inst/unitTests/runit.environments.R: new unit test 
 	'test.environment.assign.templated' to test the templated assign

Modified: pkg/src/Rcpp/Environment.h
===================================================================
--- pkg/src/Rcpp/Environment.h	2010-01-25 09:43:02 UTC (rev 447)
+++ pkg/src/Rcpp/Environment.h	2010-01-25 10:11:48 UTC (rev 448)
@@ -28,6 +28,7 @@
 #include <Rcpp/wrap.h>
 #include <Rcpp/Symbol.h>
 #include <Rcpp/Language.h>
+#include <Rcpp/as.h>
 
 namespace Rcpp{ 
 
@@ -210,9 +211,9 @@
     	     * with GCC4.4 :
     	     * e["bla" ] = { 1,2,3};
     	     */
-    	    template <typename T>
-    	    Binding& operator=(const T& rhs){
-    	    	    env.assign( name, wrap(rhs).asSexp() ) ;
+    	    template <typename WRAPPABLE>
+    	    Binding& operator=(const WRAPPABLE& rhs){
+    	    	    env.assign( name, rhs ) ;
     	    	    return *this ;
     	    }
     	    
@@ -233,7 +234,7 @@
     	    template <typename T> 
     	    operator T() const{
     	    	    SEXP x = env.get(name) ;
-    	    	    T t(x) ;
+    	    	    T t = as<T>(x) ;
     	    	    return t; 
     	    }
     	    

Modified: pkg/src/Rcpp/as.h
===================================================================
--- pkg/src/Rcpp/as.h	2010-01-25 09:43:02 UTC (rev 447)
+++ pkg/src/Rcpp/as.h	2010-01-25 10:11:48 UTC (rev 448)
@@ -29,10 +29,23 @@
 namespace Rcpp{ 
 
 /** 
- * Generic converted from SEXP to the typename
+ * Generic converted from SEXP to the typename. T can be any type that 
+ * has a constructor taking a SEXP, which is the case for all our 
+ * RObject and derived classes. 
+ *
+ * If it is not possible to add the SEXP constructor, e.g you don't control
+ * the type, you can overload the as template to perform the 
+ * requested conversion
+ *
+ * This is used for example in Environment, so that for example the code
+ * below will work as long as there is a way to as<> the Foo type
+ *
+ * Environment x = ... ; // some environment
+ * Foo y = x["bla"] ;    // if as<Foo> makes sense then this works !!
  */
 template <typename T> T as( SEXP m_sexp) {
-	throw std::runtime_error("not implemented") ; 
+	T t(m_sexp);
+	return t ;
 }
 template<> bool 			as<bool>(SEXP m_sexp) ;
 template<> double                   	as<double>(SEXP m_sexp) ;
@@ -45,6 +58,11 @@
 template<> std::vector<Rbyte>       	as< std::vector<Rbyte> >(SEXP m_sexp) ;
 template<> std::vector<bool>        	as< std::vector<bool> >(SEXP m_sexp) ;
 
+
+/* FIXME: turn the functions below into a template */
+
+
+
 /* these do not take care of coercion*/
 inline bool Rboolean_to_bool( int x){ return x == TRUE ; }
 inline bool int_to_bool(int x){ return x != 0 ; }



More information about the Rcpp-commits mailing list