Rcpp Version 1.0.14
Loading...
Searching...
No Matches
Rstreambuf.h
Go to the documentation of this file.
1//
2// Rstreambuf.h: Rcpp R/C++ interface class library -- stream buffer
3//
4// Copyright (C) 2011 - 2020 Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
5// Copyright (C) 2021 - 2023 Dirk Eddelbuettel, Romain Francois, Jelmer Ypma and IƱaki Ucar
6//
7// This file is part of Rcpp.
8//
9// Rcpp is free software: you can redistribute it and/or modify it
10// under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 2 of the License, or
12// (at your option) any later version.
13//
14// Rcpp is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21
22#ifndef RCPP__IOSTREAM__RSTREAMBUF_H
23#define RCPP__IOSTREAM__RSTREAMBUF_H
24
25#include <cstdio>
26#include <streambuf>
27
28namespace Rcpp {
29
30 template <bool OUTPUT>
31 class Rstreambuf : public std::streambuf {
32 public:
34
35 protected:
36 virtual std::streamsize xsputn(const char *s, std::streamsize n);
37
38 virtual int overflow(int c = traits_type::eof());
39
40 virtual int sync();
41 };
42
43 template <bool OUTPUT>
44 class Rostream : public std::ostream {
47 public:
48 Rostream() : std::ostream( &buf ) {}
49 };
50 // #nocov start
51 template <> inline std::streamsize Rstreambuf<true>::xsputn(const char *s, std::streamsize num) {
52 Rprintf("%.*s", static_cast<int>(num), s);
53 return num;
54 }
55 template <> inline std::streamsize Rstreambuf<false>::xsputn(const char *s, std::streamsize num) {
56 REprintf("%.*s", static_cast<int>(num), s);
57 return num;
58 }
59
60 template <> inline int Rstreambuf<true>::overflow(int c) {
61 if (c != traits_type::eof()) {
62 char_type ch = traits_type::to_char_type(c);
63 return xsputn(&ch, 1) == 1 ? c : traits_type::eof();
64 }
65 return c;
66 }
67 template <> inline int Rstreambuf<false>::overflow(int c) {
68 if (c != traits_type::eof()) {
69 char_type ch = traits_type::to_char_type(c);
70 return xsputn(&ch, 1) == 1 ? c : traits_type::eof();
71 }
72 return c;
73 }
74
75 template <> inline int Rstreambuf<true>::sync() {
77 return 0;
78 }
79 template <> inline int Rstreambuf<false>::sync() {
81 return 0;
82 } // #nocov end
83
84#ifdef RCPP_USE_GLOBAL_ROSTREAM
85 extern Rostream<true>& Rcout;
86 extern Rostream<false>& Rcerr;
87#else
90#endif
91
92}
93
94#endif
Rstreambuf< OUTPUT > Buffer
Definition Rstreambuf.h:45
virtual int overflow(int c=traits_type::eof())
virtual int sync()
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Rcpp API.
Definition algo.h:28
static Rostream< false > Rcerr
Definition Rstreambuf.h:89
static Rostream< true > Rcout
Definition Rstreambuf.h:88
T as(SEXP x)
Definition as.h:151
Definition swap.h:25