Stream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SBUFSTREAM_H
10 #define SQUID_SBUFSTREAM_H
11 
12 #include "sbuf/SBuf.h"
13 
14 #include <ostream>
15 
21 class SBufStreamBuf : public std::streambuf
22 {
23 public:
25  explicit SBufStreamBuf(SBuf aBuf) : theBuf(aBuf) {}
26 
29  return theBuf;
30  }
31 
33  void clearBuf() {
34  theBuf.clear();
35  }
36 
37 protected:
38  virtual int_type overflow(int_type aChar = traits_type::eof()) {
39  std::streamsize pending(pptr() - pbase());
40 
41  if (pending && sync())
42  return traits_type::eof();
43 
44  if (aChar != traits_type::eof()) {
45  char chars[1] = {static_cast<char>(aChar)};
46 
47  if (aChar != traits_type::eof())
48  theBuf.append(chars, 1);
49  }
50 
51  pbump(-pending); // Reset pptr().
52  return aChar;
53  }
54 
56  virtual int sync() {
57  std::streamsize pending(pptr() - pbase());
58 
59  if (pending)
60  theBuf.append(pbase(), pending);
61 
62  return 0;
63  }
64 
68  virtual std::streamsize xsputn(const char * chars, std::streamsize number) {
69  if (number)
70  theBuf.append(chars, number);
71 
72  return number;
73  }
74 
75 private:
77  SBufStreamBuf(); // no default constructor
78 };
79 
85 class SBufStream : public std::ostream
86 {
87 public:
93  SBufStream(SBuf aBuf): std::ostream(0), theBuffer(aBuf) {
94  rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
95  clear(); //clear badbit set by calling init(0)
96  }
97 
99  SBufStream(): std::ostream(0), theBuffer(SBuf()) {
100  rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
101  clear(); //clear badbit set by calling init(0)
102  }
103 
105  SBuf buf() {
106  flush();
107  return theBuffer.getBuf();
108  }
109 
112  flush();
113  theBuffer.clearBuf();
114  return *this;
115  }
116 
117 private:
119 };
120 
122 template <typename... Args>
123 inline
124 SBuf ToSBuf(Args&&... args)
125 {
126  // TODO: Make this code readable after requiring C++17.
127  SBufStream out;
128  using expander = int[];
129  (void)expander {0, (void(out << std::forward<Args>(args)),0)...};
130  return out.buf();
131 }
132 
133 #endif /* SQUID_SBUFSTREAM_H */
134 
SBufStream()
Create an empty SBufStream.
Definition: Stream.h:99
Definition: SBuf.h:87
SBuf getBuf()
get a copy of the stream's contents
Definition: Stream.h:28
SBuf & append(const SBuf &S)
Definition: SBuf.cc:207
virtual int sync()
push the streambuf to the backing SBuf
Definition: Stream.h:56
void clear()
Definition: SBuf.cc:190
virtual std::streamsize xsputn(const char *chars, std::streamsize number)
Definition: Stream.h:68
void clearBuf()
clear the stream's store
Definition: Stream.h:33
SBufStream & clearBuf()
Clear the stream's backing store.
Definition: Stream.h:111
SBuf theBuf
Definition: Stream.h:76
SBufStream(SBuf aBuf)
Definition: Stream.h:93
number
Definition: testStatHist.cc:16
SBufStreamBuf theBuffer
Definition: Stream.h:118
SBuf ToSBuf(Args &&...args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:124
SBufStreamBuf(SBuf aBuf)
initialize streambuf; use supplied SBuf as backing store
Definition: Stream.h:25
virtual int_type overflow(int_type aChar=traits_type::eof())
Definition: Stream.h:38
SBuf buf()
Retrieve a copy of the current stream status.
Definition: Stream.h:105

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors