[Rcpp-commits] r2434 - in pkg/Rcpp: . R inst/include/Rcpp inst/include/Rcpp/module src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 13 14:28:53 CET 2010


Author: romain
Date: 2010-11-13 14:28:52 +0100 (Sat, 13 Nov 2010)
New Revision: 2434

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/NAMESPACE
   pkg/Rcpp/R/00_classes.R
   pkg/Rcpp/inst/include/Rcpp/Module.h
   pkg/Rcpp/inst/include/Rcpp/module/Module_generated_Constructor.h
   pkg/Rcpp/src/Module.cpp
Log:
exposing some more information about constructors at the R level

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2010-11-13 13:26:29 UTC (rev 2433)
+++ pkg/Rcpp/ChangeLog	2010-11-13 13:28:52 UTC (rev 2434)
@@ -1,3 +1,9 @@
+2010-11-13  Romain Francois <romain at r-enthusiasts.com>
+
+    * inst/include/Rcpp/Module.h: expose constructors as the "cpp_constructor"
+    field of "C++ClassRepresentation" and the "constructors" slot of 
+    "C++Class"
+
 2010-11-07  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/sugar/operators/minus.h: fixed Minus_Vector_Primitive<RTYPE,false,T>

Modified: pkg/Rcpp/NAMESPACE
===================================================================
--- pkg/Rcpp/NAMESPACE	2010-11-13 13:26:29 UTC (rev 2433)
+++ pkg/Rcpp/NAMESPACE	2010-11-13 13:28:52 UTC (rev 2434)
@@ -24,7 +24,7 @@
 import( methods )
 importFrom( utils, capture.output, assignInNamespace, .DollarNames, prompt, packageDescription )
 
-exportClasses( Module, "C++ObjectS3", "C++Field", "C++Method", 
+exportClasses( Module, "C++ObjectS3", "C++Field", "C++Method", "C++Constructor", 
     "C++Class", "C++Object", "C++Function", 
     "C++ClassRepresentation" # , "C++Property"
     )

Modified: pkg/Rcpp/R/00_classes.R
===================================================================
--- pkg/Rcpp/R/00_classes.R	2010-11-13 13:26:29 UTC (rev 2433)
+++ pkg/Rcpp/R/00_classes.R	2010-11-13 13:28:52 UTC (rev 2434)
@@ -57,29 +57,35 @@
     )
 )
 
+setRefClass( "C++Constructor", 
+    fields = list( 
+        pointer       = "externalptr", 
+        class_pointer = "externalptr", 
+        nargs         = "integer"
+    )
+)
 
 setClass( "C++Class", 
 	representation( 
-	    pointer = "externalptr", 
-	    module  = "externalptr", 
-	    fields  = "list",
-	    methods = "list",
-            generator = "refObjectGenerator"
+	    pointer      = "externalptr", 
+	    module       = "externalptr", 
+	    fields       = "list",
+	    methods      = "list",
+	    constructors = "list", 
+	    generator    = "refObjectGenerator"
 	), 
 	contains = "character"
 	)
 setClass( "C++ClassRepresentation", 
     representation( 
-        pointer       = "externalptr", 
-        generator     = "refObjectGenerator", 
-        cpp_fields    = "list", 
-        cpp_methods   = "list"
+        pointer         = "externalptr", 
+        generator       = "refObjectGenerator", 
+        cpp_fields      = "list", 
+        cpp_methods     = "list", 
+        cpp_constructor = "list"
     ), 
     contains = "classRepresentation" )
 
-# # might not actually use this
-# setClass( "C++Property" )	
-
 setClass( "C++Object")
 
 setClass( "C++Function", 

Modified: pkg/Rcpp/inst/include/Rcpp/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Module.h	2010-11-13 13:26:29 UTC (rev 2433)
+++ pkg/Rcpp/inst/include/Rcpp/Module.h	2010-11-13 13:28:52 UTC (rev 2434)
@@ -55,6 +55,8 @@
 	
 	virtual Rcpp::List fields(SEXP){ return Rcpp::List(0); }
 	virtual Rcpp::List getMethods(SEXP){ return Rcpp::List(0); }
+	virtual Rcpp::List getConstructors(SEXP){ return Rcpp::List(0); }
+	
 	virtual void run_finalizer(SEXP){ }
 	
 	virtual bool has_method( const std::string& ){ 
@@ -165,7 +167,37 @@
     }
 } ;
 
+#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:
+    
+    SignedConstructor( 
+        Constructor_Base<Class>* ctor_, 
+        ValidConstructor valid_
+    ) : ctor(ctor_), valid(valid_){}
+    
+    Constructor_Base<Class>* ctor ;
+    ValidConstructor valid ;
+    
+    inline int nargs(){ return ctor->nargs() ; }
+} ;
+
+template <typename Class>
+class S4_CppConstructor : public Rcpp::Reference {
+public:             
+    S4_CppConstructor( SignedConstructor<Class>* m, SEXP class_xp ) : Reference( "C++Constructor" ){
+        field( "pointer" )       = Rcpp::XPtr< SignedConstructor<Class> >( m, false ) ;
+        field( "class_pointer" ) = class_xp ;
+        field( "nargs" )         = m->nargs() ;
+    }
+} ;
+
+
 #include <Rcpp/module/Module_generated_CppMethod.h>
 #include <Rcpp/module/Module_generated_Pointer_CppMethod.h>
 
@@ -215,25 +247,7 @@
 
 #include <Rcpp/module/Module_Property.h>
 
-#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:
-    
-    SignedConstructor( 
-        Constructor_Base<Class>* ctor_, 
-        ValidConstructor valid_
-    ) : ctor(ctor_), valid(valid_){}
-    
-    Constructor_Base<Class>* ctor ;
-    ValidConstructor valid ;
-} ;
-
-template <typename Class>
 class class_ : public class_Base {
 public:
 	typedef class_<Class> self ;
@@ -461,6 +475,17 @@
 		return out ;
 	}
 
+	
+	Rcpp::List getConstructors( SEXP class_xp ){
+	    int n = constructors.size() ;
+		Rcpp::List out(n) ;
+		typename vec_signed_constructor::iterator it = constructors.begin( ) ;
+		for( int i=0; i<n; i++, ++it){
+			out[i] = S4_CppConstructor<Class>( *it , class_xp ) ; 
+		} 
+		return out ;
+	}
+
 #include <Rcpp/module/Module_Field.h>
 
 #include <Rcpp/module/Module_Add_Property.h>

Modified: pkg/Rcpp/inst/include/Rcpp/module/Module_generated_Constructor.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/module/Module_generated_Constructor.h	2010-11-13 13:26:29 UTC (rev 2433)
+++ pkg/Rcpp/inst/include/Rcpp/module/Module_generated_Constructor.h	2010-11-13 13:28:52 UTC (rev 2434)
@@ -26,6 +26,7 @@
 class Constructor_Base {
 public:
     virtual Class* get_new( SEXP* args, int nargs ) = 0 ;
+    virtual int nargs() = 0 ;
 } ;
 
 template <typename Class>
@@ -34,12 +35,14 @@
     virtual Class* get_new( SEXP* args, int nargs ){
 	return new Class ;
     }
+    virtual int nargs(){ return 0 ; }
 } ;
 template <typename Class, typename U0>
 class Constructor_1 : public Constructor_Base<Class>{
     virtual Class* get_new( SEXP* args, int nargs ){
         return new Class( as<U0>(args[0]) ) ;
     }
+    virtual int nargs(){ return 1 ; }
 } ;
 template <typename Class, typename U0, typename U1>
 class Constructor_2 : public Constructor_Base<Class>{
@@ -49,6 +52,7 @@
             as<U1>(args[1]) 
             ) ;
     }
+    virtual int nargs(){ return 2 ; }
 } ;
 
 struct init_0 {};

Modified: pkg/Rcpp/src/Module.cpp
===================================================================
--- pkg/Rcpp/src/Module.cpp	2010-11-13 13:26:29 UTC (rev 2433)
+++ pkg/Rcpp/src/Module.cpp	2010-11-13 13:28:52 UTC (rev 2434)
@@ -287,8 +287,9 @@
 		mangled_name += cl->name ;
 		slot( ".Data" ) = mangled_name ;
 		
-		slot( "fields" ) = cl->fields( clxp.asSexp() ) ;
-		slot( "methods" ) = cl->getMethods( clxp.asSexp() ) ;
+		slot( "fields" )      = cl->fields( clxp.asSexp() ) ;
+		slot( "methods" )     = cl->getMethods( clxp.asSexp() ) ;
+		slot( "constructors") = cl->getConstructors( clxp.asSexp() ) ;
 	}
 
 	CppObject::CppObject( Module* p, class_Base* clazz, SEXP xp ) : S4("C++Object") {



More information about the Rcpp-commits mailing list