Skip to content

Commit d96d806

Browse files
committed
first pass at global Rostreams (#928)
1 parent 98173e5 commit d96d806

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

R/Attributes.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ 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();",
294296
"",
295297
includes,
296298
"// [[Rcpp::export]]",

inst/include/Rcpp/iostream/Rstreambuf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ namespace Rcpp {
8080
::R_FlushConsole();
8181
return 0;
8282
} // #nocov end
83-
static Rostream<true> Rcout;
84-
static Rostream<false> Rcerr;
83+
extern Rostream<true>& Rcout;
84+
extern Rostream<false>& Rcerr;
8585

8686

8787
}

inst/include/Rcpp/routines.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#ifndef RCPP_ROUTINE_H
2424
#define RCPP_ROUTINE_H
2525

26+
#include <Rcpp/iostream/Rstreambuf.h>
27+
2628
#if defined(COMPILING_RCPP)
2729

2830
// the idea is that this file should be generated automatically by Rcpp::register
@@ -45,6 +47,9 @@ namespace Rcpp{
4547
void Rcpp_precious_teardown();
4648
SEXP Rcpp_precious_preserve(SEXP object);
4749
void Rcpp_precious_remove(SEXP token);
50+
51+
Rostream<true>& Rcpp_cout_get();
52+
Rostream<false>& Rcpp_cerr_get();
4853
}
4954

5055
SEXP rcpp_get_stack_trace();
@@ -155,6 +160,17 @@ namespace Rcpp {
155160
fun(token);
156161
}
157162

163+
inline attribute_hidden Rostream<true>& Rcpp_cout_get() {
164+
typedef Rostream<true>& (*Fun)();
165+
static Fun fun = GET_CALLABLE("Rcpp_cout_get");
166+
return fun();
167+
}
168+
inline attribute_hidden Rostream<false>& Rcpp_cerr_get() {
169+
typedef Rostream<false>& (*Fun)();
170+
static Fun fun = GET_CALLABLE("Rcpp_cerr_get");
171+
return fun();
172+
}
173+
158174
}
159175

160176
// The 'attribute_hidden' used here is a simple precessor defined from

src/api.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ using namespace Rcpp;
3333
#endif
3434

3535
namespace Rcpp {
36+
// [[Rcpp::register]]
37+
Rostream<true>& Rcpp_cout_get() {
38+
static Rostream<true> Rcpp_cout;
39+
return Rcpp_cout;
40+
}
41+
// [[Rcpp::register]]
42+
Rostream<false>& Rcpp_cerr_get() {
43+
static Rostream<false> Rcpp_cerr;
44+
return Rcpp_cerr;
45+
}
46+
Rostream<true>& Rcout = Rcpp_cout_get();
47+
Rostream<false>& Rcerr = Rcpp_cerr_get();
3648

3749
namespace internal {
3850

src/attributes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,9 @@ namespace attributes {
21272127

21282128
// always bring in Rcpp
21292129
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;
21302133

21312134
// commit with preamble
21322135
return ExportsGenerator::commit(ostr.str());

src/rcpp_init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void registerFunctions(){
124124
RCPP_REGISTER(Rcpp_precious_teardown)
125125
RCPP_REGISTER(Rcpp_precious_preserve)
126126
RCPP_REGISTER(Rcpp_precious_remove)
127+
RCPP_REGISTER(Rcpp_cout_get)
128+
RCPP_REGISTER(Rcpp_cerr_get)
127129
#undef RCPP_REGISTER
128130
}
129131

0 commit comments

Comments
 (0)