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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Oct 4 07:27:41 CEST 2010


Author: romain
Date: 2010-10-04 07:27:40 +0200 (Mon, 04 Oct 2010)
New Revision: 2267

Modified:
   pkg/Rcpp/NAMESPACE
   pkg/Rcpp/R/00_classes.R
   pkg/Rcpp/inst/include/Rcpp/Module.h
   pkg/Rcpp/inst/include/Rcpp/routines.h
   pkg/Rcpp/src/Module.cpp
   pkg/Rcpp/src/Rcpp_init.c
Log:
preliminary internal code for 

Modified: pkg/Rcpp/NAMESPACE
===================================================================
--- pkg/Rcpp/NAMESPACE	2010-10-03 19:33:09 UTC (rev 2266)
+++ pkg/Rcpp/NAMESPACE	2010-10-04 05:27:40 UTC (rev 2267)
@@ -10,7 +10,7 @@
     
     Module__classes_info,Module__complete,Module__get_class,
     Module__has_class,Module__has_function,Module__functions_arity,
-    Module__name, 
+    Module__name, CppObject__finalize, 
     
     # .External functions
     CppMethod__invoke, InternalFunction_invoke, Module__invoke, class__newInstance

Modified: pkg/Rcpp/R/00_classes.R
===================================================================
--- pkg/Rcpp/R/00_classes.R	2010-10-03 19:33:09 UTC (rev 2266)
+++ pkg/Rcpp/R/00_classes.R	2010-10-04 05:27:40 UTC (rev 2267)
@@ -62,7 +62,7 @@
 	representation( 
 	    pointer = "externalptr", 
 	    module  = "externalptr", 
-	    fields  = "list", 
+	    fields  = "list",
 	    methods = "list",
             generator = "refObjectGenerator"
 	), 
@@ -70,10 +70,10 @@
 	)
 setClass( "C++ClassRepresentation", 
     representation( 
-        pointer     = "externalptr", 
-        generator   = "refObjectGenerator", 
-        cpp_fields  = "list", 
-        cpp_methods = "list"
+        pointer       = "externalptr", 
+        generator     = "refObjectGenerator", 
+        cpp_fields    = "list", 
+        cpp_methods   = "list"
     ), 
     contains = "classRepresentation" )
 

Modified: pkg/Rcpp/inst/include/Rcpp/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Module.h	2010-10-03 19:33:09 UTC (rev 2266)
+++ pkg/Rcpp/inst/include/Rcpp/Module.h	2010-10-04 05:27:40 UTC (rev 2267)
@@ -55,6 +55,7 @@
 	
 	virtual Rcpp::List fields(SEXP){ return Rcpp::List(0); }
 	virtual Rcpp::List getMethods(SEXP){ return Rcpp::List(0); }
+	virtual void run_finalizer(SEXP){ }
 	
 	virtual bool has_method( const std::string& ){ 
 		return false ; 
@@ -181,6 +182,27 @@
 } ;
 
 template <typename Class>
+class CppFinalizer{ 
+public:
+    CppFinalizer(){} ;
+    virtual void run(Class* ){} ;
+} ;
+
+template <typename Class>
+class FunctionFinalizer : public CppFinalizer<Class> {
+public:
+    typedef void (*Pointer)(Class*) ;
+    FunctionFinalizer( Pointer p ) : finalizer(p){} ;
+    
+    virtual void run(Class* object){ 
+        finalizer( object ) ;
+    }
+    
+private:
+    Pointer finalizer ;    
+} ;
+
+template <typename Class>
 class S4_field : public Rcpp::Reference {
 public:             
     S4_field( CppProperty<Class>* p, SEXP class_xp ) : Reference( "C++Field" ){
@@ -188,12 +210,9 @@
         field( "cpp_class" )     = p->get_class();
         field( "pointer" )       = Rcpp::XPtr< CppProperty<Class> >( p, false ) ;
         field( "class_pointer" ) = class_xp ;
-        
-    
     }
 } ;
 
-
 #include <Rcpp/module/Module_Property.h>
 
 template <typename Class>
@@ -204,15 +223,17 @@
 	typedef std::map<std::string,method_class*> METHOD_MAP ;
 	typedef std::pair<const std::string,method_class*> PAIR ;
 	typedef Rcpp::XPtr<Class> XP ;
+	typedef CppFinalizer<Class> finalizer_class ;
 	
 	typedef CppProperty<Class> prop_class ;
 	typedef std::map<std::string,prop_class*> PROPERTY_MAP ;
 	typedef std::pair<const std::string,prop_class*> PROP_PAIR ;
 	
-	class_( const char* name_ ) : class_Base(name_), methods(), properties(), specials(0) {
+	class_( const char* name_ ) : class_Base(name_), methods(), properties(), specials(0), finalizer_pointer(0) {
 		if( !singleton ){
 			singleton = new self ;
 			singleton->name = name_ ;
+			singleton->finalizer_pointer = new finalizer_class ;
 			getCurrentScope()->AddClass( name_, singleton ) ;
 		}
 	}
@@ -395,11 +416,26 @@
 
 #include <Rcpp/module/Module_Add_Property.h>
 
+    self& finalizer( void (*f)(Class*) ){
+        SetFinalizer( new FunctionFinalizer<Class>( f ) ) ;
+        return *this ;
+    }    
 
+    virtual void run_finalizer( SEXP object ){
+        finalizer_pointer->run( XP(object) ) ;
+    }
+    
 private:
+    
+    void SetFinalizer( finalizer_class* f ){
+        if( singleton->finalizer_pointer ) delete singleton->finalizer ;
+        singleton->finalizer_pointer = f ; 
+    }
+    
 	METHOD_MAP methods ;
 	PROPERTY_MAP properties ;
 	static self* singleton ;
+	finalizer_class* finalizer_pointer ;
 	int specials ;
 	
 	class_( ) : class_Base(), methods(), properties(), specials(0) {}; 

Modified: pkg/Rcpp/inst/include/Rcpp/routines.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/routines.h	2010-10-03 19:33:09 UTC (rev 2266)
+++ pkg/Rcpp/inst/include/Rcpp/routines.h	2010-10-04 05:27:40 UTC (rev 2267)
@@ -51,6 +51,7 @@
 CALLFUN_2(Module__has_class);
 CALLFUN_2(Module__has_function);
 CALLFUN_1(Module__name);
+CALLFUN_2(CppObject__finalize);
 
 /* .External functions */
 EXTFUN(CppMethod__invoke) ;

Modified: pkg/Rcpp/src/Module.cpp
===================================================================
--- pkg/Rcpp/src/Module.cpp	2010-10-03 19:33:09 UTC (rev 2266)
+++ pkg/Rcpp/src/Module.cpp	2010-10-04 05:27:40 UTC (rev 2267)
@@ -100,6 +100,10 @@
 	cl->setProperty( field_xp, obj, value ) ;
 	return R_NilValue ;
 }
+RCPP_FUNCTION_2(SEXP, CppObject__finalize, XP_Class cl, SEXP obj){
+	cl->run_finalizer( obj ) ;
+	return R_NilValue ;
+}
 
 
 
@@ -286,7 +290,7 @@
 		slot( ".Data" ) = mangled_name ;
 		
 		slot( "fields" ) = cl->fields( clxp.asSexp() ) ;
-		slot( "methods" ) = cl->getMethods( clxp.asSexp() ) ; 
+		slot( "methods" ) = cl->getMethods( clxp.asSexp() ) ;
 	}
 
 	CppObject::CppObject( Module* p, class_Base* clazz, SEXP xp ) : S4("C++Object") {

Modified: pkg/Rcpp/src/Rcpp_init.c
===================================================================
--- pkg/Rcpp/src/Rcpp_init.c	2010-10-03 19:33:09 UTC (rev 2266)
+++ pkg/Rcpp/src/Rcpp_init.c	2010-10-04 05:27:40 UTC (rev 2267)
@@ -43,6 +43,8 @@
     CALLDEF(CppClass__complete,1),
     CALLDEF(CppClass__methods,1),
     
+    CALLDEF(CppObject__finalize,2),
+    
     CALLDEF(Module__classes_info,1),
     CALLDEF(Module__complete,1),
     CALLDEF(Module__get_class,2),



More information about the Rcpp-commits mailing list