Skip to content

Commit fe4adf1

Browse files
rework Rstreambuf / Rostream
1 parent c5c6bfb commit fe4adf1

6 files changed

Lines changed: 64 additions & 75 deletions

File tree

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* src/cache.cpp: added get_cache
44
* include/Rcpp/hash/IndexHash.h: use a cached integer vector for
55
the hash table payload. The cache vector increases as needed.
6+
* include/Rcpp/iostream/Rostream.h: make Rostream a template
7+
* include/Rcpp/iostream/Rstreambuf.h: make Rstreambuf a template
8+
* src/AttributesGen.cpp : include exceptions.h which was included
9+
implicitely by Rostream.h before
610

711
2012-12-05 JJ Allaire <jj@rstudio.org>
812

inst/include/Rcpp/iostream/Rostream.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@
1919
// You should have received a copy of the GNU General Public License
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

22-
#ifndef __ROSTREAM_H__
23-
#define __ROSTREAM_H__
24-
25-
#include <iomanip> // USES setw
26-
27-
#include "Rstreambuf.h" // to permit direct inclusion of Rostream header
22+
#ifndef RCPP__IOSTREAM__ROSTREAM_H
23+
#define RCPP__IOSTREAM__ROSTREAM_H
2824

2925
// modified from
3026
// http://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file
3127

32-
namespace Rcpp {
28+
#include <Rcpp/iostream/Rstreambuf.h>
29+
#include <iostream>
30+
31+
namespace Rcpp {
3332

33+
template <bool OUTPUT>
3434
class Rostream : public std::ostream {
35-
Rstreambuf* buf ;
35+
typedef Rstreambuf<OUTPUT> Buffer ;
36+
Buffer* buf ;
3637
public:
37-
Rostream(bool output) :
38-
std::ostream( new Rstreambuf(output) ),
39-
buf( static_cast<Rstreambuf*>( rdbuf() ) )
38+
Rostream() :
39+
std::ostream( new Buffer ),
40+
buf( static_cast<Buffer*>( rdbuf() ) )
4041
{}
4142
~Rostream() {
4243
if (buf != NULL) {
@@ -47,10 +48,10 @@ namespace Rcpp {
4748
};
4849

4950
// declare global variable
50-
extern Rostream Rcout;
51+
extern Rostream<true> Rcout;
5152

5253
// declare global variable
53-
extern Rostream Rcerr;
54+
extern Rostream<false> Rcerr;
5455
}
5556

5657
#endif

inst/include/Rcpp/iostream/Rstreambuf.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
22
//
3-
// Rostream.h: Rcpp R/C++ interface class library -- stream buffer
3+
// Rstreambuf.h: Rcpp R/C++ interface class library -- stream buffer
44
//
55
// Copyright (C) 2011 - 2012 Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
66
//
@@ -19,31 +19,30 @@
1919
// You should have received a copy of the GNU General Public License
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

22-
#ifndef __RSTREAMBUF_H__
23-
#define __RSTREAMBUF_H__
22+
#ifndef RCPP__IOSTREAM__RSTREAMBUF_H
23+
#define RCPP__IOSTREAM__RSTREAMBUF_H
2424

2525
#include <streambuf>
26-
#include <RcppCommon.h>
2726

2827
// modified from
2928
// http://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file
3029

3130
namespace Rcpp {
3231

32+
template <bool OUTPUT>
3333
class Rstreambuf : public std::streambuf {
3434
public:
35-
Rstreambuf(bool output_): output(output_){}
36-
35+
Rstreambuf(){}
36+
3737
protected:
3838
virtual std::streamsize xsputn(const char *s, std::streamsize n );
3939

4040
virtual int overflow(int c = EOF );
4141

42-
virtual int sync() ;
43-
private:
44-
bool output ;
45-
};
42+
virtual int sync() ;
43+
};
4644

45+
4746
}
4847

4948
#endif

src/AttributesGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <fstream>
2727

2828
#include <Rcpp/iostream/Rostream.h>
29+
#include <Rcpp/exceptions.h>
2930

3031
namespace Rcpp {
3132
namespace attributes {

src/Rostream.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Rostream.cpp: Rcpp R/C++ interface class library -- output stream
44
//
5-
// Copyright (C) 2011 Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
5+
// Copyright (C) 2011 - 2012 Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
66
//
77
// This file is part of Rcpp.
88
//
@@ -19,10 +19,42 @@
1919
// You should have received a copy of the GNU General Public License
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

22-
#include <RcppCommon.h>
22+
#include <Rcpp/iostream/Rstreambuf.h>
2323
#include <Rcpp/iostream/Rostream.h>
24+
#include <R.h>
25+
26+
namespace Rcpp{
27+
template <> inline std::streamsize Rstreambuf<true>::xsputn(const char *s, std::streamsize num ) {
28+
Rprintf( "%.*s", num, s ) ;
29+
return num ;
30+
}
31+
template <> inline std::streamsize Rstreambuf<false>::xsputn(const char *s, std::streamsize num ) {
32+
REprintf( "%.*s", num, s ) ;
33+
return num ;
34+
}
35+
36+
template <> inline int Rstreambuf<true>::overflow(int c ) {
37+
if (c != EOF) Rprintf( "%.1s", &c ) ;
38+
return c ;
39+
}
40+
template <> inline int Rstreambuf<false>::overflow(int c ) {
41+
if (c != EOF) REprintf( "%.1s", &c ) ;
42+
return c ;
43+
}
44+
45+
template <> inline int Rstreambuf<true>::sync(){
46+
::R_FlushConsole() ;
47+
return 0 ;
48+
}
49+
template <> inline int Rstreambuf<false>::sync(){
50+
::R_FlushConsole() ;
51+
return 0 ;
52+
}
53+
54+
55+
}
2456

2557
// define global variable Rcout
26-
Rcpp::Rostream Rcpp::Rcout(true);
27-
Rcpp::Rostream Rcpp::Rcerr(false);
58+
Rcpp::Rostream<true> Rcpp::Rcout;
59+
Rcpp::Rostream<false> Rcpp::Rcerr;
2860

src/Rstreambuf.cpp

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)