[Rcpp-commits] r250 - in pkg: inst src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 1 08:01:07 CET 2010


Author: romain
Date: 2010-01-01 08:01:02 +0100 (Fri, 01 Jan 2010)
New Revision: 250

Modified:
   pkg/inst/ChangeLog
   pkg/src/Evaluator.cpp
   pkg/src/RObject.cpp
   pkg/src/Rcpp/RObject.h
   pkg/src/Rcpp/XPtr.h
Log:
s/protect/preserve

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2009-12-31 16:57:08 UTC (rev 249)
+++ pkg/inst/ChangeLog	2010-01-01 07:01:02 UTC (rev 250)
@@ -1,3 +1,12 @@
+2010-01-01  Romain Francois <francoisromain at free.fr>
+
+	* pkg/src/Rcpp/RObject.h: s/protect/preserve/, added methods
+	isPreserved and forgetPreserve
+	
+	* pkg/src/Rcpp/XPtr.h 
+	* pkg/src/RObject.cpp
+	* pkg/src/Evaluator.cpp
+
 2009-12-31  Romain Francois <francoisromain at free.fr>
 
 	* src/Rcpp/Evaluator.h : new class Rcpp::Evaluator that eases

Modified: pkg/src/Evaluator.cpp
===================================================================
--- pkg/src/Evaluator.cpp	2009-12-31 16:57:08 UTC (rev 249)
+++ pkg/src/Evaluator.cpp	2010-01-01 07:01:02 UTC (rev 250)
@@ -36,11 +36,11 @@
 		Environment rcpp = Environment::namespace_env("Rcpp") ;
 		SEXP call = Rf_lang3( Rf_install("protectedEval"), expression, env ) ;
 		result = RObject( Rf_eval( call, rcpp ) ); 
-		result.protect() ;
+		result.preserve() ;
 		error_occured = LOGICAL( Rf_eval( Rf_lang1( Rf_install("errorOccured")) , rcpp) )[0] ;
 		if( error_occured ){
 			error = RObject( Rf_eval( Rf_lang1(Rf_install("getCurrentError")) , rcpp) );
-			error.protect() ;
+			error.preserve() ;
 		}
 	}
 	

Modified: pkg/src/RObject.cpp
===================================================================
--- pkg/src/RObject.cpp	2009-12-31 16:57:08 UTC (rev 249)
+++ pkg/src/RObject.cpp	2010-01-01 07:01:02 UTC (rev 250)
@@ -27,38 +27,38 @@
 RObject::RObject(const bool & v) {
     logTxt("RObject from bool\n");
     m_sexp = Rf_ScalarLogical(v);
-    protect() ;
+    preserve() ;
 }
 
 RObject::RObject(const double & v) {
     logTxt("RObject from double\n");
     m_sexp = Rf_ScalarReal(v);
-    protect() ;
+    preserve() ;
 }
 
 RObject::RObject(const int & v) {
     logTxt("RObject from int\n");
     m_sexp = Rf_ScalarInteger(v);
-    protect() ;
+    preserve() ;
 }
 
 RObject::RObject(const Rbyte & v) {
     logTxt("RObject from raw\n");
     m_sexp = Rf_ScalarRaw(v);
-    protect() ;
+    preserve() ;
 }
 
 RObject::RObject(const std::string & v) {
     logTxt("RObject from std::string\n");
     m_sexp = Rf_mkString(v.c_str());
-    protect() ;
+    preserve() ;
 }
 
 RObject::RObject(const std::vector<bool> & v) {
     logTxt("RObject from bool vector\n");
     int n = v.size();
     m_sexp = Rf_allocVector(LGLSXP, n);
-    protect() ;
+    preserve() ;
     copy( v.begin(), v.end(), LOGICAL(m_sexp) ) ;
 }
 
@@ -66,7 +66,7 @@
     logTxt("RObject from int vector\n");
     int n = v.size();
     m_sexp = Rf_allocVector(INTSXP, n);
-    protect() ;
+    preserve() ;
     copy( v.begin(), v.end(), INTEGER(m_sexp) ) ;
 }
 
@@ -74,7 +74,7 @@
     logTxt("RObject from double vector\n");
     int n = v.size();
     m_sexp = Rf_allocVector(REALSXP, n);
-    protect() ;
+    preserve() ;
     copy( v.begin(), v.end(), REAL(m_sexp) ) ;
 }
 
@@ -82,7 +82,7 @@
     logTxt("RObject from vector<Rbyte> \n");
     int n = v.size();
     m_sexp = Rf_allocVector(RAWSXP, n);
-    protect() ;
+    preserve() ;
     copy( v.begin(), v.end(), RAW(m_sexp) ) ;
 }
 
@@ -90,7 +90,7 @@
     logTxt("RObject from std::string vector\n");
     int n = v.size();
     m_sexp = Rf_allocVector(STRSXP, n);
-    protect() ;
+    preserve() ;
     int i=0; 
     std::vector<std::string>::const_iterator it = v.begin() ;
     while( i<n ){
@@ -106,7 +106,7 @@
     logTxt("RObject from set<int>\n");
     int n = v.size();
     m_sexp = Rf_allocVector(INTSXP, n);
-    protect() ;
+    preserve() ;
     copy( v.begin(), v.end(), INTEGER(m_sexp) ) ;
 }
 
@@ -114,7 +114,7 @@
     logTxt("RObject from set<double>\n");
     int n = v.size();
     m_sexp = Rf_allocVector(REALSXP, n);
-    protect() ;
+    preserve() ;
     copy( v.begin(), v.end(), REAL(m_sexp) ) ;
 }
 
@@ -122,7 +122,7 @@
     logTxt("RObject from set<Rbyte> \n");
     int n = v.size();
     m_sexp = Rf_allocVector(RAWSXP, n);
-    protect() ;
+    preserve() ;
     copy( v.begin(), v.end(), RAW(m_sexp) ) ;
 }
 
@@ -130,7 +130,7 @@
     logTxt("RObject from set<string>\n");
     int n = v.size();
     m_sexp = Rf_allocVector(STRSXP, n);
-    protect() ;
+    preserve() ;
     int i=0;
     std::set<std::string>::iterator it = v.begin(); 
     while( i<n ){
@@ -333,22 +333,23 @@
     return v;
 }
 
-
-
-
-void RObject::protect(){
-	if( !isProtected ){
-		isProtected = true ;
+void RObject::preserve(){
+	if( !preserved ){
+		preserved = true ;
 		R_PreserveObject( m_sexp ); 
 	}
 }
 
 void RObject::release(){
-	if( isProtected ){
+	if( preserved ){
 		R_ReleaseObject(m_sexp); 
 	}
 }
 
+void RObject::forgetPreserve(){
+	preserved = false ;
+}
+
 std::vector<std::string> RObject::attributeNames() const {
 	/* inspired from do_attributes at attrib.c */
 	

Modified: pkg/src/Rcpp/RObject.h
===================================================================
--- pkg/src/Rcpp/RObject.h	2009-12-31 16:57:08 UTC (rev 249)
+++ pkg/src/Rcpp/RObject.h	2010-01-01 07:01:02 UTC (rev 250)
@@ -38,7 +38,7 @@
      * the SEXP from garbage collection, and release to 
      * remove the protection
      */
-    RObject(SEXP m_sexp = R_NilValue) : m_sexp(m_sexp), isProtected(false){};
+    RObject(SEXP m_sexp = R_NilValue) : m_sexp(m_sexp), preserved(false){};
     
     /**
      * if this object is protected rom R's GC, then it is released
@@ -85,7 +85,7 @@
      *
      * Note that this does not use the PROTECT/UNPROTECT dance
      */
-    void protect();
+    void preserve();
 	
     /**
      * explicitely release this object to R garbage collection. This
@@ -96,11 +96,32 @@
     void release();
     
     /**
+     * Indicates if the underlying SEXP is preserved by this object
+     */
+    inline bool isPreserved() const{ return preserved ; }
+    
+    /**
+     * when this object goes out of scope, if the wrapped SEXP is currently
+     * protected from R's garbage collection, it becomes subject to garbage
+     * collection. 
+     *
+     * This method allows this object to forget that it is preserving
+     * the SEXP.
+     *
+     * This can be used when we want some other RObject to assume ownership
+     * of the SEXP. This needs to be used with EXTRA care. If the SEXP 
+     * was preserved by one object and the protection was not passed to another,
+     * there is a great chance that there will be memory leaks.
+     *
+     * This might be improved later, possibly with using shared smart pointer
+     * or by doing what auto_ptr does with the assignment operator
+     */
+    void forgetPreserve() ;
+    
+    /**
      * implicit conversion to SEXP
      */
-    inline operator SEXP() const {
-	return m_sexp ;
-    }
+    inline operator SEXP() const { return m_sexp ; }
 	
     
     /* attributes */
@@ -123,24 +144,19 @@
     /**
      * is this object NULL
      */
-    inline bool isNULL() const{
-    	return m_sexp == R_NilValue ;
-    }
+    inline bool isNULL() const{ return m_sexp == R_NilValue ; }
     
     /**
      * The SEXP typeof, calls TYPEOF on the underlying SEXP
      */
-    inline int sexp_type() const {
-    	return TYPEOF(m_sexp) ;
-    }
+    inline int sexp_type() const { return TYPEOF(m_sexp) ; }
     
     /** 
      * explicit conversion to SEXP
      */
-    inline SEXP asSexp() const {
-	return m_sexp ;
-    }
-	
+    inline SEXP asSexp() const { return m_sexp ; }
+    
+    
 protected:
 	
     /**
@@ -155,7 +171,7 @@
      * if this is true then the object will be release and become
      * subject to R garbage collection when this object is deleted
      */
-    bool isProtected ;    
+    bool preserved ;    
     
 };
 

Modified: pkg/src/Rcpp/XPtr.h
===================================================================
--- pkg/src/Rcpp/XPtr.h	2009-12-31 16:57:08 UTC (rev 249)
+++ pkg/src/Rcpp/XPtr.h	2010-01-01 07:01:02 UTC (rev 250)
@@ -96,7 +96,7 @@
     if( set_delete_finalizer ){
 	setDeleteFinalizer() ;
     }
-    protect() ;
+    preserve() ;
 }
 
 template<typename T>



More information about the Rcpp-commits mailing list