[Rcpp-commits] r1358 - in pkg/Rcpp/inst/doc: . snippets

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat May 29 08:35:12 CEST 2010


Author: romain
Date: 2010-05-29 08:35:11 +0200 (Sat, 29 May 2010)
New Revision: 1358

Added:
   pkg/Rcpp/inst/doc/snippets/modulestdvec.cpp
   pkg/Rcpp/inst/doc/snippets/stdvectorback.cpp
Modified:
   pkg/Rcpp/inst/doc/Rcpp-modules.Rnw
Log:
document previous commit

Modified: pkg/Rcpp/inst/doc/Rcpp-modules.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-modules.Rnw	2010-05-28 19:35:10 UTC (rev 1357)
+++ pkg/Rcpp/inst/doc/Rcpp-modules.Rnw	2010-05-29 06:35:11 UTC (rev 1358)
@@ -219,6 +219,22 @@
 
 \InputIfFileExists{snippets/WorldModuleR}{}{}
 
+\subsubsection{const and non const member functions}
+
+\texttt{method} is able to expose both \texttt{const} and \texttt{non const}
+member functions of a class. There are however situations where
+a class defines two versions of the same method, differing only in their 
+signature by the \texttt{const}-ness. It is for example the case of the 
+member functions \texttt{back} of the \texttt{std::vector} template from 
+the STL. 
+
+\InputIfFileExists{snippets/stdvectorback}{}{}
+
+The following example illustrates how to use Rcpp modules to expose
+the class \texttt{std::vector<double>} from the STL. 
+
+\InputIfFileExists{snippets/modulestdvec}{}{}
+
 \section{Future extensions}
 
 \texttt{Boost.Python} has many more features that we would like to port 

Added: pkg/Rcpp/inst/doc/snippets/modulestdvec.cpp
===================================================================
--- pkg/Rcpp/inst/doc/snippets/modulestdvec.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/doc/snippets/modulestdvec.cpp	2010-05-29 06:35:11 UTC (rev 1358)
@@ -0,0 +1,47 @@
+// convenience typedef
+typedef std::vector<double> vec ;
+
+// helpers
+void vec_assign( vec* obj, Rcpp::NumericVector data ){
+	obj->assign( data.begin(), data.end() ) ;
+}
+
+void vec_insert( vec* obj, int position, Rcpp::NumericVector data){
+	vec::iterator it = obj->begin() + position ;
+	obj->insert( it, data.begin(), data.end() ) ;
+}
+
+Rcpp::NumericVector vec_asR( vec* obj ){
+	return Rcpp::wrap( *obj ) ;
+}
+
+RCPP_MODULE(yada){
+	using namespace Rcpp ;
+	
+	// we expose the class std::vector<double> as "vec" on the R side
+	class_<vec>( "vec")
+	
+		// exposing member functions
+	 	.method( "size", &vec::size)
+ 		.method( "max_size", &vec::max_size) 
+ 		.method( "resize", &vec::resize) 
+ 		.method( "capacity", &vec::capacity) 
+ 		.method( "empty", &vec::empty) 
+ 		.method( "reserve", &vec::reserve) 
+ 		.method( "push_back", &vec::push_back )
+ 		.method( "pop_back", &vec::pop_back )
+ 		.method( "clear", &vec::clear )
+ 		
+ 		// specifically exposing const member functions
+ 		.const_method( "back", &vec::back )
+		.const_method( "front", &vec::front )
+		.const_method( "at", &vec::at )
+		
+		// exposing free functions taking a std::vector<double>*
+		// as their first argument
+		.method( "assign", &vec_assign )
+		.method( "insert", &vec_insert )
+		.method( "as.vector", &vec_asR ) 
+	;
+}                     
+

Added: pkg/Rcpp/inst/doc/snippets/stdvectorback.cpp
===================================================================
--- pkg/Rcpp/inst/doc/snippets/stdvectorback.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/doc/snippets/stdvectorback.cpp	2010-05-29 06:35:11 UTC (rev 1358)
@@ -0,0 +1,2 @@
+reference back ( );
+const_reference back ( ) const;



More information about the Rcpp-commits mailing list