MCPcopy Create free account
hub / github.com/RcppCore/Rcpp / generateCpp

Function generateCpp

src/attributes.cpp:2840–2964  ·  view source on GitHub ↗

Generate the C++ code required to make [[Rcpp::export]] functions available as C symbols with SEXP parameters and return

Source from the content-addressed store, hash-verified

2838 // Generate the C++ code required to make [[Rcpp::export]] functions
2839 // available as C symbols with SEXP parameters and return
2840 void generateCpp(std::ostream& ostr,
2841 const SourceFileAttributes& attributes,
2842 bool includePrototype,
2843 bool cppInterface,
2844 const std::string& contextId) {
2845
2846 // process each attribute
2847 for(std::vector<Attribute>::const_iterator
2848 it = attributes.begin(); it != attributes.end(); ++it) {
2849
2850 // alias the attribute and function (bail if not export)
2851 const Attribute& attribute = *it;
2852 if (!attribute.isExportedFunction())
2853 continue;
2854 const Function& function = attribute.function();
2855
2856 // include prototype if requested
2857 if (includePrototype) {
2858 ostr << "// " << function.name() << std::endl;
2859 printFunction(ostr, function, false);
2860 ostr << ";";
2861 }
2862
2863 // write the C++ callable SEXP-based function (this version
2864 // returns errors via "try-error")
2865 ostr << std::endl;
2866 ostr << (cppInterface ? "static" : "RcppExport");
2867 ostr << " SEXP ";
2868 std::string funcName = contextId + "_" + function.name();
2869 ostr << funcName;
2870 if (cppInterface)
2871 ostr << kTrySuffix; // #nocov
2872 ostr << "(";
2873 std::ostringstream ostrArgs;
2874 const std::vector<Argument>& arguments = function.arguments();
2875 for (size_t i = 0; i<arguments.size(); i++) {
2876 const Argument& argument = arguments[i];
2877 ostrArgs << "SEXP " << argument.name() << "SEXP";
2878 if (i != (arguments.size()-1))
2879 ostrArgs << ", ";
2880 }
2881 std::string args = ostrArgs.str();
2882 ostr << args << ") {" << std::endl;
2883 ostr << "BEGIN_RCPP" << std::endl;
2884 if (!function.type().isVoid())
2885 ostr << " Rcpp::RObject rcpp_result_gen;" << std::endl;
2886 if (!cppInterface && attribute.rng())
2887 ostr << " Rcpp::RNGScope rcpp_rngScope_gen;" << std::endl;
2888 for (size_t i = 0; i<arguments.size(); i++) {
2889 const Argument& argument = arguments[i];
2890
2891 ostr << " Rcpp::traits::input_parameter< "
2892 << argument.type().full_name() << " >::type " << argument.name()
2893 << "(" << argument.name() << "SEXP);" << std::endl;
2894 }
2895
2896 ostr << " ";
2897 if (!function.type().isVoid())

Callers 2

doWriteFunctionsMethod · 0.85
regenerateSourceMethod · 0.85

Calls 8

printFunctionFunction · 0.85
isExportedFunctionMethod · 0.80
isVoidMethod · 0.80
rngMethod · 0.80
full_nameMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected