[Rcpp-commits] r1362 - in pkg/Rcpp: inst/include/Rcpp inst/unitTests src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat May 29 12:01:56 CEST 2010
Author: romain
Date: 2010-05-29 12:01:56 +0200 (Sat, 29 May 2010)
New Revision: 1362
Modified:
pkg/Rcpp/inst/include/Rcpp/Module.h
pkg/Rcpp/inst/unitTests/runit.Module.R
pkg/Rcpp/src/Module.cpp
Log:
more testing
Modified: pkg/Rcpp/inst/include/Rcpp/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Module.h 2010-05-29 09:21:00 UTC (rev 1361)
+++ pkg/Rcpp/inst/include/Rcpp/Module.h 2010-05-29 10:01:56 UTC (rev 1362)
@@ -56,6 +56,7 @@
virtual SEXP invoke( const std::string& method_name, SEXP obj, SEXP *args, int nargs ){
return R_NilValue ;
}
+ virtual Rcpp::CharacterVector method_names(){ return Rcpp::CharacterVector(0) ; }
virtual ~class_Base(){}
std::string name ;
@@ -173,10 +174,20 @@
#include <Rcpp/module/Module_generated_method.h>
#include <Rcpp/module/Module_generated_Pointer_method.h>
- inline bool has_method( const std::string& m){
+ bool has_method( const std::string& m){
return methods.find(m) != methods.end() ;
}
+ Rcpp::CharacterVector method_names(){
+ int n = methods.size() ;
+ Rcpp::CharacterVector out(n) ;
+ typename METHOD_MAP::iterator it = methods.begin( ) ;
+ for( int i=0; i<n; i++, ++it){
+ out[i] = it->first ;
+ }
+ return out ;
+ }
+
private:
METHOD_MAP methods ;
static self* singleton ;
Modified: pkg/Rcpp/inst/unitTests/runit.Module.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.R 2010-05-29 09:21:00 UTC (rev 1361)
+++ pkg/Rcpp/inst/unitTests/runit.Module.R 2010-05-29 10:01:56 UTC (rev 1362)
@@ -17,7 +17,8 @@
# You should have received a copy of the GNU General Public License
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
-if( Rcpp:::capabilities()[["Rcpp modules"]] )
+if( Rcpp:::capabilities()[["Rcpp modules"]] ) {
+
test.Module <- function(){
inc <- '
@@ -99,3 +100,68 @@
}
+
+
+test.Module.stdvec <- function(){
+
+code <- ''
+
+inc <- '
+typedef std::vector<double> vec ;
+
+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 ;
+
+ class_<vec>( "vec")
+ .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 )
+
+ .const_method( "back", &vec::back )
+ .const_method( "front", &vec::front )
+ .const_method( "at", &vec::at )
+
+ .method( "assign", &vec_assign )
+ .method( "insert", &vec_insert )
+ .method( "as.vector", &vec_asR )
+
+
+ ;
+}
+
+'
+ fx <- cxxfunction( signature(), "", include = inc, plugin = "Rcpp" )
+
+ yada <- Rcpp:::Module( "yada", getDynLib( fx ) )
+ v <- new( yada$vec )
+ v$assign( 1:10 )
+
+ checkEquals( v$back(), 10 )
+ v$push_back( 10 )
+ checkEquals( as.integer(v$size()), 11L )
+ checkEquals( v$at( 0 ), 1 )
+ checkEquals( v$as.vector(), c(1:10, 10 ) )
+
+}
+
+
+}
Modified: pkg/Rcpp/src/Module.cpp
===================================================================
--- pkg/Rcpp/src/Module.cpp 2010-05-29 09:21:00 UTC (rev 1361)
+++ pkg/Rcpp/src/Module.cpp 2010-05-29 10:01:56 UTC (rev 1362)
@@ -46,7 +46,11 @@
RCPP_FUNCTION_1( bool, CppObject__needs_init, SEXP xp ){
return EXTPTR_PTR(xp) == 0 ;
}
+RCPP_FUNCTION_1( Rcpp::CharacterVector, CppClass__methods, XP_Class cl){
+ return cl->method_names() ;
+}
+
extern "C" SEXP Module__funtions_arity( SEXP mod_xp ){
Rcpp::XPtr<Rcpp::Module> module(mod_xp) ;
return module-> functions_arity() ;
More information about the Rcpp-commits
mailing list