[Rcpp-commits] r3903 - in pkg/Rcpp: inst/include/Rcpp src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 7 00:05:44 CET 2012


Author: jjallaire
Date: 2012-11-07 00:05:43 +0100 (Wed, 07 Nov 2012)
New Revision: 3903

Modified:
   pkg/Rcpp/inst/include/Rcpp/Module.h
   pkg/Rcpp/src/Attributes.cpp
   pkg/Rcpp/src/Module.cpp
Log:
GetCppCallable: ensure that linked to libraries are loaded

Modified: pkg/Rcpp/inst/include/Rcpp/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Module.h	2012-11-06 21:37:58 UTC (rev 3902)
+++ pkg/Rcpp/inst/include/Rcpp/Module.h	2012-11-06 23:05:43 UTC (rev 3903)
@@ -451,7 +451,7 @@
         DL_FUNC p_fun ;
     } ;
     
-    FunctionProxy GetCppCallable( const std::string& mod, const std::string& fun) ;
+    FunctionProxy GetCppCallable( const std::string& pkg, const std::string& mod, const std::string& fun) ;
 }
 
 #define RCPP_MODULE_BOOT(name) _rcpp_module_boot_##name

Modified: pkg/Rcpp/src/Attributes.cpp
===================================================================
--- pkg/Rcpp/src/Attributes.cpp	2012-11-06 21:37:58 UTC (rev 3902)
+++ pkg/Rcpp/src/Attributes.cpp	2012-11-06 23:05:43 UTC (rev 3903)
@@ -384,8 +384,11 @@
     class ExportsGenerator {
     protected:
         ExportsGenerator(const std::string& targetFile, 
+                         const std::string& package,
                          const std::string& commentPrefix) 
-            : targetFile_(targetFile), commentPrefix_(commentPrefix) {
+            : targetFile_(targetFile), 
+              package_(package),
+              commentPrefix_(commentPrefix) {
             
             // read the existing target file if it exists
             if (FileInfo(targetFile_).exists()) {
@@ -415,6 +418,11 @@
             return targetFile_;
         }
         
+        // Name of package
+        const std::string& package() const {
+            return package_;
+        }
+        
         // Abstract interface for code generation
         virtual void writeBegin() = 0;
         virtual void writeFunctions(const SourceFileAttributes &attributes,
@@ -499,6 +507,7 @@
     
     private:
         std::string targetFile_;
+        std::string package_;
         std::string commentPrefix_;
         std::string existingCode_;
         std::ostringstream codeStream_;
@@ -509,15 +518,18 @@
     class CppExportsGenerator : public ExportsGenerator {
     public:
         explicit CppExportsGenerator(const std::string& packageDir, 
+                                     const std::string& package,
                                      const std::string& fileSep)
             : ExportsGenerator( 
                 packageDir + fileSep + "src" +  fileSep + "RcppExports.cpp", 
+                package,
                 "//")
         {
         }
         
         virtual void writeBegin() {
-            ostr() << "RCPP_MODULE(RcppExports) {" << std::endl;
+            ostr() << "RCPP_MODULE(" << package() << "_RcppExports) {" 
+                    << std::endl;
         }
         
         virtual void writeFunctions(const SourceFileAttributes &attributes,
@@ -564,24 +576,24 @@
     
     };
     
-    // Class which manages generating RcppExports.cpp
+    // Class which manages generating the header file for the package
     class CppIncludeGenerator : public ExportsGenerator {
     public:
         explicit CppIncludeGenerator(const std::string& packageDir, 
-                                     const std::string& fileSep,
-                                     const std::string& scope)
+                                     const std::string& package,
+                                     const std::string& fileSep)
             : ExportsGenerator( 
                 packageDir +  fileSep + "inst" +  fileSep + "include" +
-                fileSep + scope + ".hpp", 
+                fileSep + package + ".hpp", 
+                package,
                 "//")
         {
-            scope_ = scope;
             includeDir_ = packageDir +  fileSep + "inst" +  fileSep + "include";
             hasCppInterface_ = false; 
         }
         
         virtual void writeBegin() {
-            ostr() << "namespace " << scope_ << " {" << std::endl;
+            ostr() << "namespace " << package() << " {" << std::endl;
         }
     
         virtual void writeFunctions(const SourceFileAttributes &attributes,
@@ -610,8 +622,9 @@
                     ostr() << "    inline " << function << " {" 
                             << std::endl;
                     
+                    std::string ptrName = "p_" + function.name();
                     ostr() << "        static " << function.type() 
-                           << "(*p_" << function.name() << ")(";
+                           << "(*" << ptrName << ")(";
                     
                     const std::vector<Argument>& args = 
                                                 function.arguments();
@@ -622,12 +635,13 @@
                             ostr() << ",";
                     }
                     
-                    ostr() << ") = Rcpp::GetCppCallable(\"RcppExports\", "
+                    ostr() << ") = Rcpp::GetCppCallable"
+                           << "(\"" << package() << "\", "
+                           << "\"" << package() << "_RcppExports\", "
                            << "\"" << function.name() << "\");" 
                            << std::endl;
                     
-                    ostr() << "        return p_" << function.name()
-                           << "(";
+                    ostr() << "        return " << ptrName  << "(";
                            
                     for (std::size_t i = 0; i<args.size(); i++) {
                         ostr() << args[i].name();
@@ -637,8 +651,6 @@
                            
                     ostr() << ");" << std::endl;
                     ostr() << "    }" << std::endl;
-                    
-                    
                 } 
             }                           
         }
@@ -672,7 +684,6 @@
         }
         
     private:
-        std::string scope_;
         std::string includeDir_;
         bool hasCppInterface_;
     };
@@ -680,10 +691,12 @@
     // Class which manages generator RcppExports.R
     class RExportsGenerator : public ExportsGenerator {
     public:
-        explicit RExportsGenerator(const std::string& packageDir, 
+        explicit RExportsGenerator(const std::string& packageDir,
+                                   const std::string& package,
                                    const std::string& fileSep)
             : ExportsGenerator(
                 packageDir + fileSep + "R" +  fileSep + "RcppExports.R", 
+                package,
                 "#")
         {
         }
@@ -712,7 +725,7 @@
         
         virtual void writeEnd() { 
             
-            ostr() << "Rcpp::loadModule(\"RcppExports\", ";
+            ostr() << "Rcpp::loadModule(\"" << package() << "_RcppExports\", ";
             
             if (rExports_.size() > 0) {
                 ostr() << "what = c(";
@@ -894,9 +907,9 @@
      
     // initialize generators and namespace/prototype vectors
     ExportsGenerators generators;
-    generators.add(new CppExportsGenerator(packageDir, fileSep));
-    generators.add(new RExportsGenerator(packageDir, fileSep));
-    generators.add(new CppIncludeGenerator(packageDir, fileSep, packageName));
+    generators.add(new CppExportsGenerator(packageDir, packageName, fileSep));
+    generators.add(new RExportsGenerator(packageDir, packageName, fileSep));
+    generators.add(new CppIncludeGenerator(packageDir, packageName, fileSep));
     std::vector<std::string> prototypes;
     
     // write begin

Modified: pkg/Rcpp/src/Module.cpp
===================================================================
--- pkg/Rcpp/src/Module.cpp	2012-11-06 21:37:58 UTC (rev 3902)
+++ pkg/Rcpp/src/Module.cpp	2012-11-06 23:05:43 UTC (rev 3903)
@@ -464,7 +464,9 @@
 	    }
 	}
 	
-	FunctionProxy GetCppCallable( const std::string& mod, const std::string& fun){
+	FunctionProxy GetCppCallable( const std::string& pkg, const std::string& mod, const std::string& fun){
+        Rcpp::Function require = Rcpp::Environment::base_env()["require"];
+        require("PackageA", Rcpp::Named("quietly") = true);
         std::string pack( "Rcpp_module_" ) ;
         pack += mod ;
         return R_GetCCallable( pack.c_str(), fun.c_str() ) ;     



More information about the Rcpp-commits mailing list