Skip to content

Commit 5718020

Browse files
committed
second pass at global Rostreams (#928)
1 parent d96d806 commit 5718020

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

R/Attributes.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ cppFunction <- function(code,
291291
scaffolding <- c(scaffolding,
292292
"",
293293
"using namespace Rcpp;",
294-
"Rostream<true>& Rcpp::Rcout = Rcpp_cout_get();",
295-
"Rostream<false>& Rcpp::Rcerr = Rcpp_cerr_get();",
296294
"",
297295
includes,
298296
"// [[Rcpp::export]]",

inst/include/Rcpp/iostream/Rstreambuf.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ namespace Rcpp {
8080
::R_FlushConsole();
8181
return 0;
8282
} // #nocov end
83+
84+
#ifdef RCPP_USE_GLOBAL_ROSTREAM
8385
extern Rostream<true>& Rcout;
8486
extern Rostream<false>& Rcerr;
85-
87+
#else
88+
static Rostream<true> Rcout;
89+
static Rostream<false> Rcerr;
90+
#endif
8691

8792
}
8893

src/api.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

2222
#define COMPILING_RCPP
23+
#define RCPP_USE_GLOBAL_ROSTREAM
2324

2425
#include <Rcpp.h>
2526

src/attributes.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ namespace attributes {
795795

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

798+
void initializeGlobals(std::ostream& ostr);
799+
798800
void generateCpp(std::ostream& ostr,
799801
const SourceFileAttributes& attributes,
800802
bool includePrototype,
@@ -2127,9 +2129,8 @@ namespace attributes {
21272129

21282130
// always bring in Rcpp
21292131
ostr << "using namespace Rcpp;" << std::endl << std::endl;
2130-
ostr << "Rostream<true> &Rcpp::Rcout = Rcpp_cout_get();" << std::endl;
2131-
ostr << "Rostream<false> &Rcpp::Rcerr = Rcpp_cerr_get();" << std::endl;
2132-
ostr << std::endl;
2132+
// initialize references to global Rostreams
2133+
initializeGlobals(ostr);
21332134

21342135
// commit with preamble
21352136
return ExportsGenerator::commit(ostr.str());
@@ -2745,6 +2746,16 @@ namespace attributes {
27452746
return argsOstr.str();
27462747
}
27472748

2749+
// Generate the C++ code required to initialize global objects
2750+
void initializeGlobals(std::ostream& ostr) {
2751+
ostr << "#ifdef RCPP_USE_GLOBAL_ROSTREAM" << std::endl;
2752+
ostr << "Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();";
2753+
ostr << std::endl;
2754+
ostr << "Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();";
2755+
ostr << std::endl;
2756+
ostr << "#endif" << std::endl << std::endl;
2757+
}
2758+
27482759
// Generate the C++ code required to make [[Rcpp::export]] functions
27492760
// available as C symbols with SEXP parameters and return
27502761
void generateCpp(std::ostream& ostr,
@@ -3191,6 +3202,8 @@ namespace {
31913202
// always include Rcpp.h in case the user didn't
31923203
ostr << std::endl << std::endl;
31933204
ostr << "#include <Rcpp.h>" << std::endl;
3205+
// initialize references to global Rostreams
3206+
initializeGlobals(ostr);
31943207
generateCpp(ostr, sourceAttributes, true, false, contextId_);
31953208
generatedCpp_ = ostr.str();
31963209
std::ofstream cppOfs(generatedCppSourcePath().c_str(),

0 commit comments

Comments
 (0)