[Rcpp-commits] r4486 - in pkg/Rcpp: . inst/include/Rcpp inst/include/Rcpp/macros src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Sep 15 14:59:41 CEST 2013


Author: romain
Date: 2013-09-15 14:59:41 +0200 (Sun, 15 Sep 2013)
New Revision: 4486

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/InputParameter.h
   pkg/Rcpp/inst/include/Rcpp/macros/module.h
   pkg/Rcpp/src/attributes.cpp
Log:
introduce traits::input_parameter so that we can have std::vector<T>& again

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-09-14 20:24:06 UTC (rev 4485)
+++ pkg/Rcpp/ChangeLog	2013-09-15 12:59:41 UTC (rev 4486)
@@ -1,9 +1,19 @@
+2013-09-15 Romain Francois <romain at r-enthusiasts.com>
+        
+	* include/Rcpp/InputParameter.h : added the traits::input_parameter trait
+	to add another layer of abstration. 
+	* include/Rcpp/macros/module.h : taking advantage of input_parameter to
+	specialize how to work with module objects
+	* src/attributes.cpp : using traits::input_parameter<T> instead of 
+	InputParameter<T>
+        
 2013-09-14  Dirk Eddelbuettel  <edd at debian.org>
 
+	* src/attributes.cpp : Precede closing '>' by space to avoid '>>'
 	* inst/include/Rcpp/platform/compiler.h: Further refine #if test for
 	'long long' by conditioning __LP64__ on also using clang/llvm
 
-	* src/attributes.cpp (Rcpp): Precede closing '>' by space to avoid '>>'
+	* src/attributes.cpp : Precede closing '>' by space to avoid '>>'
 
 2013-09-13  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/include/Rcpp/InputParameter.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/InputParameter.h	2013-09-14 20:24:06 UTC (rev 4485)
+++ pkg/Rcpp/inst/include/Rcpp/InputParameter.h	2013-09-15 12:59:41 UTC (rev 4486)
@@ -22,8 +22,10 @@
 #ifndef Rcpp__InputParameter__h
 #define Rcpp__InputParameter__h
 
-namespace Rcpp {
+namespace Rcpp { 
     
+    // default implementation used for pass by value and modules objects
+    // as<> is called on the conversion operator
     template <typename T>
     class InputParameter {
     public:
@@ -35,6 +37,48 @@
         SEXP x ;
     } ;
     
+    // impl for references. It holds an object at the constructor and then 
+    // returns a reference in the reference operator
+    template <typename T>
+    class ReferenceInputParameter {
+    public:
+        typedef T& reference ;
+        ReferenceInputParameter(SEXP x_) : obj( as<T>(x_) ){}
+        
+        inline operator reference() { return obj ; }
+        
+    private:
+        T obj ;
+    } ;
+    
+    // same for const references
+    template <typename T>
+    class ConstReferenceInputParameter {
+    public:
+        typedef const T& const_reference ;
+        ConstReferenceInputParameter(SEXP x_) : obj( as<T>(x_) ){}
+        
+        inline operator const_reference() { return obj ; }
+        
+    private:
+        T obj ;
+    } ;
+    
+    namespace traits{
+        template <typename T>
+        struct input_parameter {
+            typedef typename Rcpp::InputParameter<T> type ;
+        } ;
+        template <typename T>
+        struct input_parameter<T&> {
+            typedef typename Rcpp::ReferenceInputParameter<T> type ;
+        } ;
+        template <typename T>
+        struct input_parameter<const T&> {
+            typedef typename Rcpp::ConstReferenceInputParameter<T> type ;
+        } ;
+    }    
+    
 }
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/macros/module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/macros/module.h	2013-09-14 20:24:06 UTC (rev 4485)
+++ pkg/Rcpp/inst/include/Rcpp/macros/module.h	2013-09-15 12:59:41 UTC (rev 4486)
@@ -44,6 +44,21 @@
     template<> struct r_type_traits< const CLASS& >{                          \
         typedef r_type_module_object_const_reference_tag r_category ;         \
     } ;                                                                       \
+    template<> struct input_parameter< CLASS* >{                              \
+        typedef Rcpp::InputParameter<CLASS*> type ;                           \
+    } ;                                                                       \
+    template<> struct input_parameter< const CLASS* >{                        \
+        typedef Rcpp::InputParameter<const CLASS*> type ;                     \
+    } ;                                                                       \
+    template<> struct input_parameter< CLASS >{                               \
+        typedef Rcpp::InputParameter<CLASS> type ;                            \
+    } ;                                                                       \
+    template<> struct input_parameter< CLASS& >{                              \
+        typedef Rcpp::InputParameter<CLASS&> type ;                           \
+    } ;                                                                       \
+    template<> struct input_parameter< const CLASS& >{                        \
+        typedef Rcpp::InputParameter<const CLASS&> type ;                     \
+    } ;                                                                       \
     }}
     
 #define RCPP_EXPOSED_WRAP(CLASS) namespace Rcpp{ namespace traits{ template<> struct wrap_type_traits< CLASS >{typedef wrap_type_module_object_tag wrap_category ; } ; }}

Modified: pkg/Rcpp/src/attributes.cpp
===================================================================
--- pkg/Rcpp/src/attributes.cpp	2013-09-14 20:24:06 UTC (rev 4485)
+++ pkg/Rcpp/src/attributes.cpp	2013-09-15 12:59:41 UTC (rev 4486)
@@ -2167,8 +2167,8 @@
             for (size_t i = 0; i<arguments.size(); i++) {
                 const Argument& argument = arguments[i];
                 
-                ostr << "        Rcpp::InputParameter< " 
-                     << argument.type().full_name() << " > " << argument.name() 
+                ostr << "        Rcpp::traits::input_parameter< " 
+                     << argument.type().full_name() << " >::type " << argument.name() 
                      << "(" << argument.name() << "SEXP );" << std::endl;
             }
             



More information about the Rcpp-commits mailing list