Skip to content

Commit

Permalink
second pass at global Rostreams (RcppCore#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Jan 28, 2021
1 parent d96d806 commit 5718020
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 0 additions & 2 deletions R/Attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ cppFunction <- function(code,
scaffolding <- c(scaffolding,
"",
"using namespace Rcpp;",
"Rostream<true>& Rcpp::Rcout = Rcpp_cout_get();",
"Rostream<false>& Rcpp::Rcerr = Rcpp_cerr_get();",
"",
includes,
"// [[Rcpp::export]]",
Expand Down
7 changes: 6 additions & 1 deletion inst/include/Rcpp/iostream/Rstreambuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ namespace Rcpp {
::R_FlushConsole();
return 0;
} // #nocov end

#ifdef RCPP_USE_GLOBAL_ROSTREAM
extern Rostream<true>& Rcout;
extern Rostream<false>& Rcerr;

#else
static Rostream<true> Rcout;
static Rostream<false> Rcerr;
#endif

}

Expand Down
1 change: 1 addition & 0 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.

#define COMPILING_RCPP
#define RCPP_USE_GLOBAL_ROSTREAM

#include <Rcpp.h>

Expand Down
19 changes: 16 additions & 3 deletions src/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,8 @@ namespace attributes {

std::string generateRArgList(const Function& function);

void initializeGlobals(std::ostream& ostr);

void generateCpp(std::ostream& ostr,
const SourceFileAttributes& attributes,
bool includePrototype,
Expand Down Expand Up @@ -2127,9 +2129,8 @@ namespace attributes {

// always bring in Rcpp
ostr << "using namespace Rcpp;" << std::endl << std::endl;
ostr << "Rostream<true> &Rcpp::Rcout = Rcpp_cout_get();" << std::endl;
ostr << "Rostream<false> &Rcpp::Rcerr = Rcpp_cerr_get();" << std::endl;
ostr << std::endl;
// initialize references to global Rostreams
initializeGlobals(ostr);

// commit with preamble
return ExportsGenerator::commit(ostr.str());
Expand Down Expand Up @@ -2745,6 +2746,16 @@ namespace attributes {
return argsOstr.str();
}

// Generate the C++ code required to initialize global objects
void initializeGlobals(std::ostream& ostr) {
ostr << "#ifdef RCPP_USE_GLOBAL_ROSTREAM" << std::endl;
ostr << "Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();";
ostr << std::endl;
ostr << "Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();";
ostr << std::endl;
ostr << "#endif" << std::endl << std::endl;
}

// Generate the C++ code required to make [[Rcpp::export]] functions
// available as C symbols with SEXP parameters and return
void generateCpp(std::ostream& ostr,
Expand Down Expand Up @@ -3191,6 +3202,8 @@ namespace {
// always include Rcpp.h in case the user didn't
ostr << std::endl << std::endl;
ostr << "#include <Rcpp.h>" << std::endl;
// initialize references to global Rostreams
initializeGlobals(ostr);
generateCpp(ostr, sourceAttributes, true, false, contextId_);
generatedCpp_ = ostr.str();
std::ofstream cppOfs(generatedCppSourcePath().c_str(),
Expand Down

0 comments on commit 5718020

Please sign in to comment.