[Rcpp-commits] r547 - in pkg/src: . Rcpp/internal Rcpp/traits

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Feb 1 18:36:11 CET 2010


Author: romain
Date: 2010-02-01 18:36:11 +0100 (Mon, 01 Feb 2010)
New Revision: 547

Added:
   pkg/src/Rcpp/traits/r_type_traits.h
Modified:
   pkg/src/Rcpp/internal/wrap.h
   pkg/src/RcppCommon.h
Log:
move r_type_traits into Rcpp::traits

Modified: pkg/src/Rcpp/internal/wrap.h
===================================================================
--- pkg/src/Rcpp/internal/wrap.h	2010-02-01 17:23:49 UTC (rev 546)
+++ pkg/src/Rcpp/internal/wrap.h	2010-02-01 17:36:11 UTC (rev 547)
@@ -82,44 +82,10 @@
 template<> void r_init_vector<STRSXP>(SEXP x) ;
 // }}}
 
-// {{{ r_type_traits
-struct r_type_primitive_tag{} ;
-struct r_type_string_tag{} ;
-struct r_type_generic_tag{} ;
-struct r_type_bool_tag{} ;
-
-struct r_type_pairstring_primitive_tag{} ;
-struct r_type_pairstring_string_tag{} ;
-struct r_type_pairstring_generic_tag{} ;
-struct r_type_pairstring_bool_tag{} ;
-
-template <typename T> struct r_type_traits { typedef r_type_generic_tag category ; } ;
-
-// special cases pair<string,T> to deal with map<string,T> etc ...
-template <typename T> struct r_type_traits< std::pair<const std::string,T> > { typedef r_type_pairstring_generic_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,int> >{ typedef r_type_pairstring_primitive_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,size_t> >{ typedef r_type_pairstring_primitive_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,double> >{ typedef r_type_pairstring_primitive_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,Rbyte> >{ typedef r_type_pairstring_primitive_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,Rcomplex> >{ typedef r_type_pairstring_primitive_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,bool> >{ typedef r_type_pairstring_bool_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,std::string> >{ typedef r_type_pairstring_string_tag category ; } ;
-template<> struct r_type_traits< std::pair<const std::string,char> >{ typedef r_type_pairstring_string_tag category ; } ;
-
-template<> struct r_type_traits<int>{ typedef r_type_primitive_tag category ; } ;
-template<> struct r_type_traits<size_t>{ typedef r_type_primitive_tag category ; } ;
-template<> struct r_type_traits<double>{ typedef r_type_primitive_tag category ; } ;
-template<> struct r_type_traits<Rbyte>{ typedef r_type_primitive_tag category ; } ;
-template<> struct r_type_traits<Rcomplex>{ typedef r_type_primitive_tag category ; } ;
-template<> struct r_type_traits<bool>{ typedef r_type_bool_tag category ; } ;
-template<> struct r_type_traits<std::string>{ typedef r_type_string_tag category ; } ;
-template<> struct r_type_traits<char>{ typedef r_type_string_tag category ; } ;
-// }}}
-
 // {{{ range wrap 
 // {{{ unnamed range wrap
 template <typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_primitive_tag){ 
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_primitive_tag){ 
 	size_t size = std::distance( first, last ) ;
 	const int RTYPE = r_sexptype<T>::rtype ;
 	SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
@@ -129,7 +95,7 @@
 } ;
 
 template <typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_bool_tag){ 
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_bool_tag){ 
 	size_t size = std::distance( first, last ) ;
 	SEXP x = PROTECT( Rf_allocVector( LGLSXP, size ) );
 	std::transform( first, last, LOGICAL(x), bool_to_Rboolean ) ; 
@@ -141,7 +107,7 @@
 // this implementation is used when T is not a primitive type.
 // T needs to be wrappable though
 template <typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_generic_tag ){ 
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_generic_tag ){ 
 	size_t size = std::distance( first, last ) ;
 	SEXP x = PROTECT( Rf_allocVector( VECSXP, size ) );
 	size_t i =0 ;
@@ -155,7 +121,7 @@
 } ;
 
 template<typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_string_tag ){
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_string_tag ){
 	size_t size = std::distance( first, last ) ;
 	SEXP x = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
 	size_t i = 0 ;
@@ -172,7 +138,7 @@
 
 template<typename InputIterator, typename T>
 SEXP range_wrap_dispatch( InputIterator first, InputIterator last ){
-	return range_wrap_dispatch___impl<InputIterator,T>( first, last, typename r_type_traits<T>::category() ) ;
+	return range_wrap_dispatch___impl<InputIterator,T>( first, last, typename ::Rcpp::traits::r_type_traits<T>::r_category() ) ;
 }
 // }}}
 
@@ -180,7 +146,7 @@
 // we get into these when iterating over a pair<const string,T> 
 // which is what e.g. map<string,T> produces
 template <typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_pairstring_primitive_tag){ 
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pairstring_primitive_tag){ 
 	size_t size = std::distance( first, last ) ;
 	const int RTYPE = r_sexptype<typename T::second_type>::rtype ;
 	SEXP x = PROTECT( Rf_allocVector( RTYPE, size ) );
@@ -199,7 +165,7 @@
 } ;
 
 template <typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_pairstring_bool_tag){ 
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pairstring_bool_tag){ 
 	size_t size = std::distance( first, last ) ;
 	SEXP x = PROTECT( Rf_allocVector( LGLSXP, size ) );
 	SEXP names = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
@@ -220,7 +186,7 @@
 // this implementation is used when T is not a primitive type.
 // T needs to be wrappable though
 template <typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_pairstring_generic_tag ){ 
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pairstring_generic_tag ){ 
 	size_t size = std::distance( first, last ) ;
 	SEXP x = PROTECT( Rf_allocVector( VECSXP, size ) );
 	SEXP names = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
@@ -241,7 +207,7 @@
 } ;
 
 template<typename InputIterator, typename T>
-SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, r_type_pairstring_string_tag ){
+SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pairstring_string_tag ){
 	size_t size = std::distance( first, last ) ;
 	SEXP x = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
 	SEXP names = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
@@ -274,7 +240,7 @@
 
 // for 'easy' primitive types: int, double, Rbyte, Rcomplex
 template <typename T>
-SEXP primitive_wrap__impl( const T& object, r_type_primitive_tag ){
+SEXP primitive_wrap__impl( const T& object, ::Rcpp::traits::r_type_primitive_tag ){
 	const int RTYPE = r_sexptype<T>::rtype ;
 	SEXP x = PROTECT( Rf_allocVector( RTYPE, 1 ) );
 	r_vector_start<RTYPE, typename storage_type<RTYPE>::type >(x)[0] = object ;
@@ -284,7 +250,7 @@
 
 // for bool
 template <typename T>
-SEXP primitive_wrap__impl( const T& object, r_type_bool_tag){
+SEXP primitive_wrap__impl( const T& object, ::Rcpp::traits::r_type_bool_tag){
 	SEXP x = PROTECT( ::Rf_allocVector( LGLSXP, 1) );
 	LOGICAL(x)[0] = static_cast<int>(object);
 	UNPROTECT(1) ; /* x */
@@ -293,7 +259,7 @@
 
 // for std::string
 template <typename T>
-SEXP primitive_wrap__impl( const T& object, r_type_string_tag){
+SEXP primitive_wrap__impl( const T& object, ::Rcpp::traits::r_type_string_tag){
 	SEXP x = PROTECT( ::Rf_allocVector( STRSXP, 1) ) ;
 	std::string y = object ; /* give a chance to implicit conversion */
 	SET_STRING_ELT( x, 0, Rf_mkChar(y.c_str()) ) ;
@@ -305,7 +271,7 @@
 // wrapping this in an R vector of the appropriate type
 template <typename T>
 SEXP primitive_wrap(const T& object){
-	return primitive_wrap__impl( object, typename r_type_traits<T>::category() ) ;
+	return primitive_wrap__impl( object, typename ::Rcpp::traits::r_type_traits<T>::r_category() ) ;
 }
 // }}}
 

Added: pkg/src/Rcpp/traits/r_type_traits.h
===================================================================
--- pkg/src/Rcpp/traits/r_type_traits.h	                        (rev 0)
+++ pkg/src/Rcpp/traits/r_type_traits.h	2010-02-01 17:36:11 UTC (rev 547)
@@ -0,0 +1,102 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// r_type_traits.h: Rcpp R/C++ interface class library -- traits to help wrap
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__traits__r_type_traits__h
+#define Rcpp__traits__r_type_traits__h
+
+namespace Rcpp{
+namespace traits{
+
+/**
+ * Identifies a primitive type that needs to special handling
+ * int, double, size_t, Rbyte, Rcomplex
+ */
+struct r_type_primitive_tag{} ;
+
+/**
+ * Identifies that the associated type can be implicitely converted
+ * to std::string
+ */
+struct r_type_string_tag{} ;
+
+/**
+ * Default
+ */
+struct r_type_generic_tag{} ;
+
+/**
+ * Indentifies the bool type
+ */
+struct r_type_bool_tag{} ;
+
+/**
+ * Identifies that the type if pair<const std::string,T> where T 
+ * is a primitive type that needs no special handling
+ */
+struct r_type_pairstring_primitive_tag{} ;
+
+/**
+ * Identifies that the associated type is pair<const std::string,std::string>
+ */
+struct r_type_pairstring_string_tag{} ;
+
+/**
+ * Indentifies pair<const std::string,T>
+ */
+struct r_type_pairstring_generic_tag{} ;
+
+/**
+ * Identifies pair<const std::string,bool>
+ */
+struct r_type_pairstring_bool_tag{} ;
+
+/**
+ * R type trait. Helps wrap.
+ */
+template <typename T> struct r_type_traits { typedef r_type_generic_tag r_category ; } ;
+
+/** 
+ * special cases pair<string,T> to deal with map<string,T> etc ...
+ */
+template <typename T> struct r_type_traits< std::pair<const std::string,T> > { typedef r_type_pairstring_generic_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,int> >{ typedef r_type_pairstring_primitive_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,size_t> >{ typedef r_type_pairstring_primitive_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,double> >{ typedef r_type_pairstring_primitive_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,Rbyte> >{ typedef r_type_pairstring_primitive_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,Rcomplex> >{ typedef r_type_pairstring_primitive_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,bool> >{ typedef r_type_pairstring_bool_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,std::string> >{ typedef r_type_pairstring_string_tag r_category ; } ;
+template<> struct r_type_traits< std::pair<const std::string,char> >{ typedef r_type_pairstring_string_tag r_category ; } ;
+
+template<> struct r_type_traits<int>{ typedef r_type_primitive_tag r_category ; } ;
+template<> struct r_type_traits<size_t>{ typedef r_type_primitive_tag r_category ; } ;
+template<> struct r_type_traits<double>{ typedef r_type_primitive_tag r_category ; } ;
+template<> struct r_type_traits<Rbyte>{ typedef r_type_primitive_tag r_category ; } ;
+template<> struct r_type_traits<Rcomplex>{ typedef r_type_primitive_tag r_category ; } ;
+template<> struct r_type_traits<bool>{ typedef r_type_bool_tag r_category ; } ;
+template<> struct r_type_traits<std::string>{ typedef r_type_string_tag r_category ; } ;
+template<> struct r_type_traits<char>{ typedef r_type_string_tag r_category ; } ;
+
+} // traits
+} // Rcpp
+
+#endif

Modified: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h	2010-02-01 17:23:49 UTC (rev 546)
+++ pkg/src/RcppCommon.h	2010-02-01 17:36:11 UTC (rev 547)
@@ -225,6 +225,7 @@
 } // namespace Rcpp
 
 // DO NOT CHANGE THE ORDER OF THESE INCLUDES
+#include <Rcpp/traits/r_type_traits.h>
 #include <Rcpp/traits/wrap_type_traits.h>
 
 #include <Rcpp/internal/convertible.h>



More information about the Rcpp-commits mailing list