Skip to content

Commit

Permalink
first pass at global Rostreams (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Jan 28, 2021
1 parent 98173e5 commit d96d806
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions R/Attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ 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
4 changes: 2 additions & 2 deletions inst/include/Rcpp/iostream/Rstreambuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ namespace Rcpp {
::R_FlushConsole();
return 0;
} // #nocov end
static Rostream<true> Rcout;
static Rostream<false> Rcerr;
extern Rostream<true>& Rcout;
extern Rostream<false>& Rcerr;


}
Expand Down
16 changes: 16 additions & 0 deletions inst/include/Rcpp/routines.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifndef RCPP_ROUTINE_H
#define RCPP_ROUTINE_H

#include <Rcpp/iostream/Rstreambuf.h>

#if defined(COMPILING_RCPP)

// the idea is that this file should be generated automatically by Rcpp::register
Expand All @@ -45,6 +47,9 @@ namespace Rcpp{
void Rcpp_precious_teardown();
SEXP Rcpp_precious_preserve(SEXP object);
void Rcpp_precious_remove(SEXP token);

Rostream<true>& Rcpp_cout_get();
Rostream<false>& Rcpp_cerr_get();
}

SEXP rcpp_get_stack_trace();
Expand Down Expand Up @@ -155,6 +160,17 @@ namespace Rcpp {
fun(token);
}

inline attribute_hidden Rostream<true>& Rcpp_cout_get() {
typedef Rostream<true>& (*Fun)();
static Fun fun = GET_CALLABLE("Rcpp_cout_get");
return fun();
}
inline attribute_hidden Rostream<false>& Rcpp_cerr_get() {
typedef Rostream<false>& (*Fun)();
static Fun fun = GET_CALLABLE("Rcpp_cerr_get");
return fun();
}

}

// The 'attribute_hidden' used here is a simple precessor defined from
Expand Down
12 changes: 12 additions & 0 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ using namespace Rcpp;
#endif

namespace Rcpp {
// [[Rcpp::register]]
Rostream<true>& Rcpp_cout_get() {
static Rostream<true> Rcpp_cout;
return Rcpp_cout;
}
// [[Rcpp::register]]
Rostream<false>& Rcpp_cerr_get() {
static Rostream<false> Rcpp_cerr;
return Rcpp_cerr;
}
Rostream<true>& Rcout = Rcpp_cout_get();
Rostream<false>& Rcerr = Rcpp_cerr_get();

namespace internal {

Expand Down
3 changes: 3 additions & 0 deletions src/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,9 @@ 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;

// commit with preamble
return ExportsGenerator::commit(ostr.str());
Expand Down
2 changes: 2 additions & 0 deletions src/rcpp_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ void registerFunctions(){
RCPP_REGISTER(Rcpp_precious_teardown)
RCPP_REGISTER(Rcpp_precious_preserve)
RCPP_REGISTER(Rcpp_precious_remove)
RCPP_REGISTER(Rcpp_cout_get)
RCPP_REGISTER(Rcpp_cerr_get)
#undef RCPP_REGISTER
}

Expand Down

0 comments on commit d96d806

Please sign in to comment.