[Rcpp-commits] r2398 - pkg/Rcpp/inst/include/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 5 14:15:39 CET 2010


Author: romain
Date: 2010-11-05 14:15:39 +0100 (Fri, 05 Nov 2010)
New Revision: 2398

Modified:
   pkg/Rcpp/inst/include/Rcpp/Module.h
Log:
allowing multiple constructors

Modified: pkg/Rcpp/inst/include/Rcpp/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Module.h	2010-11-05 12:35:44 UTC (rev 2397)
+++ pkg/Rcpp/inst/include/Rcpp/Module.h	2010-11-05 13:15:39 UTC (rev 2398)
@@ -169,8 +169,6 @@
 #include <Rcpp/module/Module_generated_CppMethod.h>
 #include <Rcpp/module/Module_generated_Pointer_CppMethod.h>
 
-typedef bool (*ValidConstructor)(SEXP*,int) ;
-
 template <typename Class>
 class CppProperty {
 	public:
@@ -220,6 +218,8 @@
 #include <Rcpp/module/Module_generated_Constructor.h>
 #include <Rcpp/module/Module_generated_class_signature.h>
 
+typedef bool (*ValidConstructor)(SEXP*,int) ;
+
 template <typename Class>
 class SignedConstructor {
 public:
@@ -264,12 +264,12 @@
 	~class_(){}
 	
 	
-	self& AddConstructor( constructor_class* ctor, ValidConstructor valid = &yes ){
+	self& AddConstructor( constructor_class* ctor, ValidConstructor valid ){
 		singleton->constructors.push_back( new signed_constructor_class( ctor, valid ) );  
 		return *this ;
 	}
 	
-	self& default_constructor( ValidConstructor valid = &yes ){
+	self& default_constructor( ValidConstructor valid = &yes_arity<0> ){
 	    return constructor( init_0(), valid ) ;  
 	}
 		
@@ -278,8 +278,18 @@
 public:
 	
 	SEXP newInstance( SEXP* args, int nargs ){
-		SEXP out = XP( constructors[0]->ctor->get_new(args,nargs), true ) ;
-		return out ;
+		BEGIN_RCPP
+		signed_constructor_class* p ;
+	    int n = constructors.size() ;
+	    for( int i=0; i<n; i++ ){
+	        p = constructors[i];
+	        bool ok = (p->valid)(args, nargs) ;
+	        if( ok ){
+	            return XP( p->ctor->get_new( args, nargs ), true ) ;
+	        }
+	    }
+	    throw std::range_error( "no valid constructor available for the argument list" ) ;
+	    END_RCPP
 	}
 	
 	SEXP invoke( SEXP method_xp, SEXP object, SEXP *args, int nargs ){ 
@@ -287,7 +297,7 @@
 		method_class* met = reinterpret_cast< method_class* >( EXTPTR_PTR( method_xp ) ) ;
 	    // maybe this is not needed as the R side handles it
 		if( met->nargs() > nargs ){
-		    Rprintf( "met->nargs() = %d\nnargs=%d\n", met->nargs(), nargs ) ;
+		    // Rprintf( "met->nargs() = %d\nnargs=%d\n", met->nargs(), nargs ) ;
 			throw std::range_error( "incorrect number of arguments" ) ; 	
 		}
 		return met->operator()( XP(object), args ); 



More information about the Rcpp-commits mailing list