[Rcpp-commits] r4112 - in pkg/Rcpp: . src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Dec 7 17:05:45 CET 2012


Author: jjallaire
Date: 2012-12-07 17:05:45 +0100 (Fri, 07 Dec 2012)
New Revision: 4112

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/TODO
   pkg/Rcpp/src/AttributesGen.cpp
Log:
use __ prefix for variables in generated code; add RNGScope for attribute exported functions.

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-12-07 15:48:17 UTC (rev 4111)
+++ pkg/Rcpp/ChangeLog	2012-12-07 16:05:45 UTC (rev 4112)
@@ -1,3 +1,8 @@
+2012-12-07  JJ Allaire <jj at rstudio.org>
+
+        * src/AttributesGen.cpp: use __ prefix for variables in generated
+        code; add RNGScope for attribute exported functions.
+
 2012-12-07 Romain Francois <romain at r-enthusiasts.com>
 
         * src/random.cpp: instantiations of random generators here

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2012-12-07 15:48:17 UTC (rev 4111)
+++ pkg/Rcpp/TODO	2012-12-07 16:05:45 UTC (rev 4112)
@@ -93,7 +93,8 @@
      
     o   Add unit tests 
 
-    o   Evaluate whether to do anything with RNGScope 
+    o   Add random = false parameter to Rcpp::export once we support parsing
+        key/value pairs in attribute parameters
 
     o   Rcpp.package.skeleton should look for Rcpp::depends in cpp_files
         and call inline plugins, etc. as necessary to create a package

Modified: pkg/Rcpp/src/AttributesGen.cpp
===================================================================
--- pkg/Rcpp/src/AttributesGen.cpp	2012-12-07 15:48:17 UTC (rev 4111)
+++ pkg/Rcpp/src/AttributesGen.cpp	2012-12-07 16:05:45 UTC (rev 4112)
@@ -357,9 +357,9 @@
                        << getCCallable(package() + "_" + function.name()) << ";" 
                        << std::endl;
                 ostr() << "        }" << std::endl;
+                ostr() << "        RNGScope __rngScope;" << std::endl;
+                ostr() << "        RObject __result = " << ptrName << "(";
                 
-                ostr() << "        RObject result = " << ptrName << "(";
-                
                 const std::vector<Argument>& args = function.arguments();
                 for (std::size_t i = 0; i<args.size(); i++) {
                     ostr() << "Rcpp::wrap(" << args[i].name() << ")";
@@ -369,13 +369,13 @@
                        
                 ostr() << ");" << std::endl;
                 
-                ostr() << "        if (result.inherits(\"try-error\"))" 
+                ostr() << "        if (__result.inherits(\"try-error\"))" 
                        << std::endl
                        << "            throw Rcpp::exception(as<std::string>("
-                       << "result).c_str());"
+                       << "__result).c_str());"
                        << std::endl;
                 ostr() << "        return Rcpp::as<" << function.type() << " >"
-                       << "(result);" << std::endl;
+                       << "(__result);" << std::endl;
                 
                 ostr() << "    }" << std::endl << std::endl;
             } 
@@ -812,6 +812,8 @@
             std::string args = ostrArgs.str();
             ostr << args << ") {" << std::endl;
             ostr << "BEGIN_RCPP" << std::endl;
+            if (!cppInterface)
+                ostr << "    Rcpp::RNGScope __rngScope;" << std::endl;
             for (size_t i = 0; i<arguments.size(); i++) {
                 const Argument& argument = arguments[i];
                 
@@ -823,7 +825,7 @@
             
             ostr << "    ";
             if (!function.type().isVoid())
-                ostr << function.type() << " result = ";
+                ostr << function.type() << " __result = ";
             ostr << function.name() << "(";
             for (size_t i = 0; i<arguments.size(); i++) {
                 const Argument& argument = arguments[i];
@@ -834,7 +836,7 @@
             ostr << ");" << std::endl;
             
             std::string res = function.type().isVoid() ? "R_NilValue" : 
-                                                         "Rcpp::wrap(result)";
+                                                         "Rcpp::wrap(__result)";
             ostr << "    return " << res << ";" << std::endl;
             ostr << (cppInterface ? "END_RCPP_RETURN_ERROR" : "END_RCPP")
                  << std::endl;
@@ -844,7 +846,8 @@
             if (cppInterface) {
                 ostr << "RcppExport SEXP " << funcName << "(" << args << ") {"
                      << std::endl;
-                ostr << "    SEXP result = PROTECT(" << funcName 
+                ostr << "    Rcpp::RNGScope __rngScope;" << std::endl;
+                ostr << "    SEXP __result = PROTECT(" << funcName 
                      << kTrySuffix << "(";
                 for (size_t i = 0; i<arguments.size(); i++) {
                     const Argument& argument = arguments[i];
@@ -854,12 +857,12 @@
                 }
                 ostr << "));" << std::endl;
                 ostr << "    "
-                     << "Rboolean isError = Rf_inherits(result, \"try-error\");"
+                     << "Rboolean __isError = Rf_inherits(__result, \"try-error\");"
                      << std::endl;
                 ostr << "    UNPROTECT(1);" << std::endl
-                     << "    if (isError)" << std::endl
-                     << "        Rf_error(CHAR(Rf_asChar(result)));" << std::endl
-                     << "    return result;" << std::endl
+                     << "    if (__isError)" << std::endl
+                     << "        Rf_error(CHAR(Rf_asChar(__result)));" << std::endl
+                     << "    return __result;" << std::endl
                      << "}" << std::endl;
             }
         }



More information about the Rcpp-commits mailing list