[Rcpp-commits] r4398 - in pkg/Rcpp: . inst/include/Rcpp inst/include/Rcpp/macros inst/include/Rcpp/traits inst/unitTests inst/unitTests/cpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 23 17:04:47 CEST 2013


Author: romain
Date: 2013-07-23 17:04:47 +0200 (Tue, 23 Jul 2013)
New Revision: 4398

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/as.h
   pkg/Rcpp/inst/include/Rcpp/macros/module.h
   pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h
   pkg/Rcpp/inst/include/Rcpp/traits/un_pointer.h
   pkg/Rcpp/inst/unitTests/cpp/Module.cpp
   pkg/Rcpp/inst/unitTests/runit.Module.R
Log:
adding as<T*> and as<const T*> for T exposed by modules

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-07-23 14:02:04 UTC (rev 4397)
+++ pkg/Rcpp/ChangeLog	2013-07-23 15:04:47 UTC (rev 4398)
@@ -1,3 +1,14 @@
+2013-07-23  Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/as.h: support as<T*> and as<const T*> where T is a class
+        exposed by modules
+        * include/Rcpp/macros/module.h: idem
+        * include/Rcpp/traits/un_pointer.h: handle the object<T> case
+        * include/Rcpp/traits/r_type_traits.h: adding traits to help the 
+        with as<T*> and as<const T*>
+        * unitTests/runit.Module.R: testing as<T*> and as<const T*>
+        * unitTests/cpp/Module.cpp: idem
+
 2013-07-17  Romain Francois <romain at r-enthusiasts.com>
 
         * include/Rcpp/vector/instantiation.h: added the DoubleVector alias

Modified: pkg/Rcpp/inst/include/Rcpp/as.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/as.h	2013-07-23 14:02:04 UTC (rev 4397)
+++ pkg/Rcpp/inst/include/Rcpp/as.h	2013-07-23 15:04:47 UTC (rev 4398)
@@ -89,8 +89,13 @@
         }
         
         /** handling object<T> */ 
+        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_module_object_const_pointer_tag ) {
+            typedef typename Rcpp::traits::remove_const<T>::type T_NON_CONST ;
+            return const_cast<T>( as_module_object<T_NON_CONST>( x ) ) ;
+        }
+        
         template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_module_object_pointer_tag ) {
-            return as_module_object<typename T::object_type>( x ) ;
+            return as_module_object<typename traits::un_pointer<T>::type>( x ) ;
         }
         
         /** handling T such that T is exposed by a module */

Modified: pkg/Rcpp/inst/include/Rcpp/macros/module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/macros/module.h	2013-07-23 14:02:04 UTC (rev 4397)
+++ pkg/Rcpp/inst/include/Rcpp/macros/module.h	2013-07-23 15:04:47 UTC (rev 4398)
@@ -29,6 +29,12 @@
  */
 #define RCPP_EXPOSED_AS(CLASS)                                         \
     namespace Rcpp{ namespace traits{                                  \
+    template<> struct r_type_traits< CLASS* >{                         \
+        typedef r_type_module_object_pointer_tag r_category ;          \
+    } ;                                                                \
+    template<> struct r_type_traits< const CLASS* >{                   \
+        typedef r_type_module_object_const_pointer_tag r_category ;    \
+    } ;                                                                \
     template<> struct r_type_traits< CLASS >{                          \
         typedef r_type_module_object_tag r_category ;                  \
     } ;                                                                \

Modified: pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h	2013-07-23 14:02:04 UTC (rev 4397)
+++ pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h	2013-07-23 15:04:47 UTC (rev 4398)
@@ -61,11 +61,16 @@
 struct r_type_pairstring_generic_tag{} ;
 
 /**
- * identifies a module object pointer (i.e. something like object<T>
+ * identifies a module object pointer
  */ 
 struct r_type_module_object_pointer_tag{} ;
 
 /**
+ * identifies a module object const pointer 
+ */ 
+struct r_type_module_object_const_pointer_tag{} ;
+
+/**
  * identifies a module object. Implementers of modules can define the 
  * r_type_traits to show that their object is handled
  */ 

Modified: pkg/Rcpp/inst/include/Rcpp/traits/un_pointer.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/un_pointer.h	2013-07-23 14:02:04 UTC (rev 4397)
+++ pkg/Rcpp/inst/include/Rcpp/traits/un_pointer.h	2013-07-23 15:04:47 UTC (rev 4398)
@@ -3,7 +3,7 @@
 //
 // un_pointer.h: Rcpp R/C++ interface class library -- 
 //
-// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2012-2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -28,6 +28,7 @@
 	
 template <typename T> struct un_pointer { typedef T type ;} ;
 template <typename T> struct un_pointer<T*> { typedef T type ;} ;
+template <typename T> struct un_pointer< object<T> > { typedef T* type ;} ;
 
 } // namespace traits
 } // namespace Rcpp

Modified: pkg/Rcpp/inst/unitTests/cpp/Module.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/cpp/Module.cpp	2013-07-23 14:02:04 UTC (rev 4397)
+++ pkg/Rcpp/inst/unitTests/cpp/Module.cpp	2013-07-23 15:04:47 UTC (rev 4398)
@@ -73,7 +73,8 @@
     double x ;
     int y ;
 };
-    
+   
+RCPP_EXPOSED_CLASS(Number)
 class Number{
 public:
     Number() : x(0.0), y(0){} ;
@@ -97,7 +98,26 @@
     double min, max ;
 } ;
 
+// [[Rcpp::export]]
+double Number_get_x_const_ref( const Number& x){
+    return x.x ;    
+}
 
+// [[Rcpp::export]]
+double Number_get_x_ref( Number& x){
+    return x.x ;    
+}
+
+// [[Rcpp::export]]
+double Number_get_x_const_pointer( const Number* x){
+    return x->x ;    
+}
+
+// [[Rcpp::export]]
+double Number_get_x_pointer( Number* x){
+    return x->x ;    
+}
+
 RCPP_MODULE(yada){
 	function( "hello" , &hello ) ;
 	function( "bar"   , &bar   ) ;

Modified: pkg/Rcpp/inst/unitTests/runit.Module.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.R	2013-07-23 14:02:04 UTC (rev 4397)
+++ pkg/Rcpp/inst/unitTests/runit.Module.R	2013-07-23 15:04:47 UTC (rev 4398)
@@ -19,7 +19,6 @@
 # along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
 .runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
-# .runThisTest <- FALSE 
 
 if( .runThisTest && Rcpp:::capabilities()[["Rcpp modules"]] ) {
 
@@ -42,6 +41,14 @@
     checkEquals( w$greet(), "" )
 }
 
+test.Module.exposed.class <- function(){
+    num <- new( Number )
+    checkEquals( Number_get_x_const_ref(num), 0.0 )
+    checkEquals( Number_get_x_const_pointer(num), 0.0 )
+    checkEquals( Number_get_x_ref(num), 0.0 )
+    checkEquals( Number_get_x_pointer(num), 0.0 )
+}
+
 test.Module.property <- function(){
     w <- new( Num )
     checkEquals( w$x, 0.0 )



More information about the Rcpp-commits mailing list