SGL
forwardingstreambuf.h
1 /*
2  * File: forwardingstreambuf.h
3  * ---------------------------
4  * This file defines the <code>ForwardingStreambuf</code> class, which
5  * represents a stream buffer that just wraps another stream buffer.
6  * We mostly use this to merge cout and cerr into a single output target.
7  *
8  * @version 2016/10/04
9  * - initial version
10  */
11 
12 #ifndef _forwardingstreambuf_h
13 #define _forwardingstreambuf_h
14 
15 #include <ios>
16 #include <iostream>
17 #include <locale>
18 #include <streambuf>
19 
20 #include "consolestreambuf.h"
21 
22 namespace sgl {
23 /*
24  * A stream buffer that just "wraps" another stream buffer.
25  * This is used here to distinguish cout (black text) from cerr (red text).
26  */
27 class ForwardingStreambuf : public std::streambuf {
28 private:
29  ConsoleStreambuf& delegate;
30  bool isStderr;
31 
32 public:
33  ForwardingStreambuf(ConsoleStreambuf& del, bool err = false)
34  : delegate(del), isStderr(err) {
35  // empty
36  }
37 
38  virtual bool isBlocked() {
39  return delegate.isBlocked();
40  }
41 
42  virtual int overflow(int ch = EOF) {
43  return delegate.overflow(ch, isStderr);
44  }
45 
46  virtual int overflow(int ch, bool isStderr) {
47  return delegate.overflow(ch, isStderr);
48  }
49 
50  virtual int sync() {
51  return delegate.sync(isStderr);
52  }
53 
54  virtual int sync(bool isStderr) {
55  return delegate.sync(isStderr);
56  }
57 
58  virtual int underflow() {
59  return delegate.underflow();
60  }
61 
62  // functions below are overridden for completeness,
63  // but all just delegate to underlying ConsoleStreambuf
64  virtual std::streamsize in_avail() {
65  return delegate.in_avail();
66  }
67 
68  virtual int snextc() {
69  return delegate.snextc();
70  }
71 
72  virtual int sbumpc() {
73  return delegate.sbumpc();
74  }
75 
76  virtual int sgetc() {
77  return delegate.sgetc();
78  }
79 
80  virtual std::streamsize sgetn(char* s, std::streamsize n) {
81  return delegate.sgetn(s, n);
82  }
83 
84  virtual int sputbackc(char c) {
85  return delegate.sputbackc(c);
86  }
87 
88  virtual int sungetc() {
89  return delegate.sungetc();
90  }
91 
92  virtual int sputc(char c) {
93  return delegate.sputc(c);
94  }
95 
96  virtual std::streamsize sputn(const char* s, std::streamsize n) {
97  return delegate.sputn(s, n);
98  }
99 
100  virtual std::locale pubimbue(const std::locale& loc) {
101  return delegate.pubimbue(loc);
102  }
103 
104  virtual std::locale getloc() const {
105  return delegate.getloc();
106  }
107 
108  virtual std::streambuf* pubsetbuf(char* s, std::streamsize n) {
109  return delegate.pubsetbuf(s, n);
110  }
111 
112  virtual std::streampos pubseekoff(std::streamoff off, std::ios_base::seekdir way,
113  std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) {
114  return delegate.pubseekoff(off, way, which);
115  }
116 
117  virtual std::streampos pubseekpos(std::streampos pos, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) {
118  return delegate.pubseekpos(pos, which);
119  }
120 
121  virtual int pubsync() {
122  return delegate.pubsync();
123  }
124 };
125 } // namespace sgl
126 
127 #endif // _forwardingstreambuf_h
Definition: console.h:293