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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Feb 21 20:01:48 CET 2010


Author: romain
Date: 2010-02-21 20:01:48 +0100 (Sun, 21 Feb 2010)
New Revision: 759

Modified:
   pkg/Rcpp/src/Rcpp/as.h
   pkg/Rcpp/src/Rcpp/internal/export.h
   pkg/Rcpp/src/as.cpp
Log:
some more genericity in as and fixed a stack imbalance

Modified: pkg/Rcpp/src/Rcpp/as.h
===================================================================
--- pkg/Rcpp/src/Rcpp/as.h	2010-02-21 18:41:46 UTC (rev 758)
+++ pkg/Rcpp/src/Rcpp/as.h	2010-02-21 19:01:48 UTC (rev 759)
@@ -29,7 +29,7 @@
 	template <typename T> T as( SEXP x, ::Rcpp::traits::r_type_primitive_tag ){
 		if( ::Rf_length(x) != 1 ) throw std::range_error( "expecting a single value" ) ;
 		const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
-		SEXP y = r_cast<RTYPE>(x) ;
+		SEXP y = PROTECT( r_cast<RTYPE>(x) );
 		typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
 		T res = caster<STORAGE,T>( *r_vector_start<RTYPE,STORAGE>( y ) ) ;
 		UNPROTECT(1) ;
@@ -47,7 +47,7 @@
 	}
 	
 	template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_generic_tag ){
-		traits::Exporter<T> exporter(x);
+		::Rcpp::traits::Exporter<T> exporter(x);
 		return exporter.get() ;
 	}
 	
@@ -75,13 +75,6 @@
 
 template<> SEXP as(SEXP m_sexp) ;
 
-/**
- * Converts the R object to a std::vector<std::string>
- *
- * The R object must be a character vector
- */
-template<> std::vector<std::string> 	as< std::vector<std::string> >(SEXP m_sexp) ;
-
 } // Rcpp 
 
 #endif

Modified: pkg/Rcpp/src/Rcpp/internal/export.h
===================================================================
--- pkg/Rcpp/src/Rcpp/internal/export.h	2010-02-21 18:41:46 UTC (rev 758)
+++ pkg/Rcpp/src/Rcpp/internal/export.h	2010-02-21 19:01:48 UTC (rev 759)
@@ -26,6 +26,7 @@
 namespace Rcpp{
 namespace internal{
 
+	/* iterating */
 	
 	template <typename InputIterator, typename value_type>
 	void export_range__impl( SEXP x, InputIterator first, ::Rcpp::traits::false_type ){
@@ -47,7 +48,36 @@
 		UNPROTECT(1) ;
 	}
 	
+	template <typename InputIterator, typename value_type>
+	void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_primitive_tag ){
+		export_range__impl<InputIterator,value_type>(
+			x, 
+			first,
+			typename ::Rcpp::traits::r_sexptype_needscast<value_type>() );
+	}
 	
+	template <typename InputIterator, typename value_type>
+	void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_string_tag ){
+		if( ! ::Rf_isString( x) ) throw std::range_error( "expecting a string vector" ) ;
+		std::string buf ;
+		R_len_t n = ::Rf_length(x) ;
+		for( R_len_t i=0; i<n; i++, ++first ){
+			*first = CHAR( STRING_ELT(x, i )) ;
+		}
+	}
+	
+	template <typename InputIterator>
+	void export_range( SEXP x, InputIterator first ){
+		export_range__dispatch<InputIterator,typename std::iterator_traits<InputIterator>::value_type>( 
+			x, 
+			first, 
+			typename ::Rcpp::traits::r_type_traits<typename std::iterator_traits<InputIterator>::value_type>::r_category() 
+			);
+	}
+	
+	
+	/* indexing */
+	
 	template <typename T, typename value_type>
 	void export_indexing__impl( SEXP x, T& res, ::Rcpp::traits::false_type ){
 		const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
@@ -58,7 +88,7 @@
 		for( R_len_t i=0; i<size; i++){
 			res[i] =  start[i] ;
 		}
-		UNPROTECT(1) ;
+		UNPROTECT(1) ; /* y */
 	}
 	
 	template <typename T, typename value_type>
@@ -69,26 +99,35 @@
 		STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE,STORAGE>(y) ;
 		R_len_t size = ::Rf_length(y)  ;
 		for( R_len_t i=0; i<size; i++){
-			res[i] =  caster<STORAGE,value_type>(start[i]) ;
+			res[i] = caster<STORAGE,value_type>(start[i]) ;
 		}
-		UNPROTECT(1) ;
+		UNPROTECT(1) ; /* y */
 	}
 
-	
-	template <typename InputIterator>
-	void export_range( SEXP x, InputIterator first){
-		export_range__impl<InputIterator,typename std::iterator_traits<InputIterator>::value_type>(
+	template <typename T, typename value_type>
+	void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_primitive_tag ){
+		export_indexing__impl<T,value_type>(
 			x, 
-			first,
-			typename ::Rcpp::traits::r_sexptype_needscast< typename std::iterator_traits<InputIterator>::value_type >() );
+			res,
+			typename ::Rcpp::traits::r_sexptype_needscast<value_type>() );
 	}
 	
 	template <typename T, typename value_type>
+	void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_string_tag ){
+		if( ! ::Rf_isString( x) ) throw std::range_error( "expecting a string vector" ) ;
+		std::string buf ;
+		R_len_t n = ::Rf_length(x) ;
+		for( R_len_t i=0; i<n; i++ ){
+			res[i] = CHAR( STRING_ELT(x, i )) ;
+		}
+	}
+	
+	template <typename T, typename value_type>
 	void export_indexing( SEXP x, T& res ){
-		export_indexing__impl<T,value_type>( 
+		export_indexing__dispatch<T,value_type>( 
 			x, 
 			res, 
-			typename ::Rcpp::traits::r_sexptype_needscast<value_type>() ) ;
+			typename ::Rcpp::traits::r_type_traits<value_type>::r_category() );
 	}
 	
 }

Modified: pkg/Rcpp/src/as.cpp
===================================================================
--- pkg/Rcpp/src/as.cpp	2010-02-21 18:41:46 UTC (rev 758)
+++ pkg/Rcpp/src/as.cpp	2010-02-21 19:01:48 UTC (rev 759)
@@ -27,17 +27,5 @@
 	return m_sexp ;
 }
 
-template<> std::vector<std::string> as< std::vector<std::string> >(SEXP m_sexp){
-    R_len_t n = Rf_length(m_sexp);
-    std::vector<std::string> v(n);
-    if (!Rf_isString(m_sexp)) {
-    	    throw std::range_error("as< vector<string> >:  expects string");
-    }
-    for (int i = 0; i < n; i++) {
-	v[i] = std::string(CHAR(STRING_ELT(m_sexp,i)));
-    }
-    return v;
-}
-
 } // namespace Rcpp
 



More information about the Rcpp-commits mailing list