[Rcpp-commits] r3958 - in pkg/Rcpp: . inst/include/Rcpp inst/include/Rcpp/internal inst/include/Rcpp/module inst/include/Rcpp/traits

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Nov 13 10:15:10 CET 2012


Author: romain
Date: 2012-11-13 10:15:10 +0100 (Tue, 13 Nov 2012)
New Revision: 3958

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/as.h
   pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
   pkg/Rcpp/inst/include/Rcpp/module/macros.h
   pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h
   pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h
Log:
helping wrapping of enums

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-13 07:50:20 UTC (rev 3957)
+++ pkg/Rcpp/ChangeLog	2012-11-13 09:15:10 UTC (rev 3958)
@@ -1,3 +1,12 @@
+2012-11-13  Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/as.h: as<enum>
+        * include/Rcpp/internal/wrap.h : wrap( enum )
+        * include/Rcpp/traits/wrap_type_traits.h: trait to help wrap( enum )
+        * include/Rcpp/traits/r_type_traits.h: trait to help as<enum>
+        * include/Rcpp/module/macros.h: macros RCPP_EXPOSED_ENUM to help as/wrap
+        of enums
+        
 2012-11-12  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/announce/ANNOUNCE-0.10.0.txt: Announcement draft

Modified: pkg/Rcpp/inst/include/Rcpp/as.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/as.h	2012-11-13 07:50:20 UTC (rev 3957)
+++ pkg/Rcpp/inst/include/Rcpp/as.h	2012-11-13 09:15:10 UTC (rev 3958)
@@ -26,7 +26,7 @@
 
     namespace internal{
         
-        template <typename T> T as( SEXP x, ::Rcpp::traits::r_type_primitive_tag ) {
+        template <typename T> T primitive_as( SEXP x ){
             if( ::Rf_length(x) != 1 ) throw ::Rcpp::not_compatible( "expecting a single value" ) ;
             const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
             SEXP y = PROTECT( r_cast<RTYPE>(x) );
@@ -36,6 +36,10 @@
             return res ; 
         }
         
+        template <typename T> T as( SEXP x, ::Rcpp::traits::r_type_primitive_tag ) {
+            return primitive_as<T>(x) ;
+        }
+        
         template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_string_tag ) {
             if( ! ::Rf_isString(x) ){
                 throw ::Rcpp::not_compatible( "expecting a string" ) ;
@@ -69,6 +73,11 @@
             return *obj ;
         }
         
+        /** handling enums by converting to int first */
+        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_enum_tag ){
+            return T( primitive_as<int>(x) ) ;
+        }
+        
     }
         
         

Modified: pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/wrap.h	2012-11-13 07:50:20 UTC (rev 3957)
+++ pkg/Rcpp/inst/include/Rcpp/internal/wrap.h	2012-11-13 09:15:10 UTC (rev 3958)
@@ -660,6 +660,11 @@
 }
 
 template <typename T>
+inline SEXP wrap_dispatch( const T& object, ::Rcpp::traits::wrap_type_enum_tag ){
+	return wrap( (int)object ) ;	
+}
+
+template <typename T>
 inline SEXP wrap_dispatch_eigen( const T& object, ::Rcpp::traits::false_type){
 	return wrap_dispatch_unknown( object, typename ::Rcpp::traits::is_convertible<T,SEXP>::type() ) ;
 }

Modified: pkg/Rcpp/inst/include/Rcpp/module/macros.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/module/macros.h	2012-11-13 07:50:20 UTC (rev 3957)
+++ pkg/Rcpp/inst/include/Rcpp/module/macros.h	2012-11-13 09:15:10 UTC (rev 3958)
@@ -37,4 +37,19 @@
   class CLASS;                    \
   RCPP_EXPOSED_CLASS_NODECL(CLASS)
 
+/** 
+ * handling enums: TODO use is_enum from C++11 or boost to have those automatic
+ */
+#define RCPP_EXPOSED_ENUM_AS(CLASS)   namespace Rcpp{ namespace traits{ template<> struct r_type_traits< CLASS >{ typedef r_type_enum_tag r_category ; } ; }}
+#define RCPP_EXPOSED_ENUM_WRAP(CLASS) namespace Rcpp{ namespace traits{ template<> struct wrap_type_traits< CLASS >{typedef wrap_type_enum_tag wrap_category ; } ; }}
+ 
+#define RCPP_EXPOSED_ENUM_NODECL(CLASS) \
+  RCPP_EXPOSED_ENUM_AS(CLASS)          \
+  RCPP_EXPOSED_ENUM_WRAP(CLASS)
+
+#define RCPP_EXPOSED_ENUM(CLASS) \
+  class CLASS;                    \
+  RCPP_EXPOSED_ENUM_NODECL(CLASS)
+  
+  
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h	2012-11-13 07:50:20 UTC (rev 3957)
+++ pkg/Rcpp/inst/include/Rcpp/traits/r_type_traits.h	2012-11-13 09:15:10 UTC (rev 3958)
@@ -71,6 +71,11 @@
 struct r_type_module_object_tag{} ;
 
 /**
+ * identifies an enum. conversions from/to int is used
+ */ 
+struct r_type_enum_tag{} ;
+
+/**
  * R type trait. Helps wrap.
  */
 template <typename T> struct r_type_traits { typedef r_type_generic_tag r_category ; } ;

Modified: pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h	2012-11-13 07:50:20 UTC (rev 3957)
+++ pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h	2012-11-13 09:15:10 UTC (rev 3958)
@@ -47,6 +47,11 @@
 struct wrap_type_module_object_tag{} ;
 
 /**
+ * enums
+ */
+struct wrap_type_enum_tag{} ;
+
+/**
  * Type trait that helps the dispatch of wrap to the proper method
  *
  * This builds a struct that contains a typedef called wrap_category



More information about the Rcpp-commits mailing list