[Rcpp-commits] r201 - in pkg: inst inst/examples/RcppInline src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Dec 19 19:39:35 CET 2009


Author: edd
Date: 2009-12-19 19:39:34 +0100 (Sat, 19 Dec 2009)
New Revision: 201

Modified:
   pkg/inst/ChangeLog
   pkg/inst/examples/RcppInline/RcppSexpTests.r
   pkg/src/RcppSexp.cpp
   pkg/src/RcppSexp.h
Log:
the 'if in doubt ask Romain for advice' release with simplied reference counting


Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2009-12-19 16:38:24 UTC (rev 200)
+++ pkg/inst/ChangeLog	2009-12-19 18:39:34 UTC (rev 201)
@@ -1,5 +1,7 @@
 2009-12-19  Dirk Eddelbuettel  <edd at debian.org>
 
+	* src/RcppSexp.{h,cpp}: Switch to R_ProtectObject and R_ReleaseObject
+	  with a big thanks to Romain for the most appropriate suggestion
 	* src/RcppSexp.{h,cpp}: Added converters for vectors of
 	  int, double, and std::string vectors
 	* src/RcppResultsSetp.{h,cpp}: Added simple single SEXP return

Modified: pkg/inst/examples/RcppInline/RcppSexpTests.r
===================================================================
--- pkg/inst/examples/RcppInline/RcppSexpTests.r	2009-12-19 16:38:24 UTC (rev 200)
+++ pkg/inst/examples/RcppInline/RcppSexpTests.r	2009-12-19 18:39:34 UTC (rev 201)
@@ -48,19 +48,18 @@
 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
 print(funx(x=2:5))
 
-## does not work
-#cat("\n===Int Vector\n")
-#foo <- '
-#        std::vector<int> iv = RcppSexp(x).asStdVectorInt();
-#	std::cout << "Returning twice the value of vector : ";
-#        for (size_t i=0; i<iv.size(); i++) {
-#            iv[i] = 2*iv[i];
-#        }
-#        RcppSexp t = RcppSexp( iv );
-#	return(t.asSexp());
-#        '
-#funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
-#print(funx(x=2:5))
+cat("\n===Int Vector\n")
+foo <- '
+        std::vector<int> iv = RcppSexp(x).asStdVectorInt();
+	std::cout << "Returning twice the value of vector : ";
+        for (size_t i=0; i<iv.size(); i++) {
+            iv[i] = 2*iv[i];
+        }
+        RcppSexp t = RcppSexp( iv );
+	return(t.asSexp());
+        '
+funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
+print(funx(x=2:5))
 
 
 cat("\n===Double Vector via RcppResultSet.getSEXP\n")

Modified: pkg/src/RcppSexp.cpp
===================================================================
--- pkg/src/RcppSexp.cpp	2009-12-19 16:38:24 UTC (rev 200)
+++ pkg/src/RcppSexp.cpp	2009-12-19 18:39:34 UTC (rev 201)
@@ -23,39 +23,38 @@
 
 RcppSexp::RcppSexp(const double & v) {
     logTxt("RcppSexp from double\n");
-    m_sexp = PROTECT(Rf_allocVector(REALSXP, 1));
-    m_nprot++;
+    m_sexp = Rf_allocVector(REALSXP, 1);
+    R_PreserveObject(m_sexp);
     REAL(m_sexp)[0] = v;
 }
 
 RcppSexp::RcppSexp(const int & v) {
     logTxt("RcppSexp from int\n");
-    m_sexp = PROTECT(Rf_allocVector(INTSXP, 1));
-    m_nprot++;
+    m_sexp = Rf_allocVector(INTSXP, 1);
+    R_PreserveObject(m_sexp);
     INTEGER(m_sexp)[0] = v;
 }
 
 RcppSexp::RcppSexp(const std::string & v) {
     logTxt("RcppSexp from std::string\n");
-    m_sexp = PROTECT(Rf_allocVector(STRSXP, 1));
-    m_nprot++;
+    m_sexp = Rf_allocVector(STRSXP, 1);
+    R_PreserveObject(m_sexp);
     SET_STRING_ELT(m_sexp, 0, Rf_mkChar(v.c_str()));
 }
 
 RcppSexp::RcppSexp(const std::vector<int> & v) {
     logTxt("RcppSexp from int vector\n");
     int n = v.size();
-    m_sexp = PROTECT(Rf_allocVector(INTSXP, n));
-    m_nprot++;
+    m_sexp = Rf_allocVector(INTSXP, n);
+    R_PreserveObject(m_sexp);
     for (int i = 0; i < n; i++) {
-	Rprintf("%d\n", v[i]);
 	INTEGER(m_sexp)[i] = v[i];
     }	
 }
 
 RcppSexp::~RcppSexp() {
     logTxt("dtor");
-    UNPROTECT(m_nprot);
+    R_ReleaseObject(m_sexp);
 }
 
 double RcppSexp::asDouble() const {
@@ -103,8 +102,7 @@
 }
 
 SEXP RcppSexp::asSexp() const {
-    SEXP val = m_sexp;
-    return val;
+    return m_sexp;
 }
 
 std::vector<int> RcppSexp::asStdVectorInt() const {

Modified: pkg/src/RcppSexp.h
===================================================================
--- pkg/src/RcppSexp.h	2009-12-19 16:38:24 UTC (rev 200)
+++ pkg/src/RcppSexp.h	2009-12-19 18:39:34 UTC (rev 201)
@@ -26,8 +26,8 @@
 
 class RcppSexp {
 public:
-    RcppSexp(SEXP sexp, int numprot=0) : m_sexp(sexp), m_nprot(numprot) { }
-    RcppSexp() : m_sexp(R_NilValue), m_nprot(0) { }
+    RcppSexp(SEXP sexp, int numprot=0) : m_sexp(sexp) { }
+    RcppSexp() : m_sexp(R_NilValue) { }
     RcppSexp(const double & v);
     RcppSexp(const int & v);
     RcppSexp(const std::string & v);
@@ -44,7 +44,6 @@
     
 private:
     SEXP m_sexp;
-    int m_nprot;
 };
 
 #endif



More information about the Rcpp-commits mailing list