Stream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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_SRC_SBUF_STREAM_H
10 #define SQUID_SRC_SBUF_STREAM_H
11 
12 #include "base/PackableStream.h"
13 #include "sbuf/SBuf.h"
14 
20 class SBufStream : public std::ostream
21 {
22 public:
28  SBufStream(const SBuf &aBuf):
29  std::ostream(nullptr), // initialize the parent; no stream buffer yet
30  sink_(aBuf),
31  streamBuffer_(sink_) // initialize the stream buffer
32  {
33  rdbuf(&streamBuffer_); // supply the now-initialized stream buffer
34  clear(); // clear the badbit that a nullptr stream buffer has triggered
35  }
36 
39 
41  SBuf buf() {
42  flush();
43  return sink_;
44  }
45 
48  flush();
49  sink_.clear();
50  return *this;
51  }
52 
53 private:
58 };
59 
61 template <typename... Args>
62 inline
63 SBuf ToSBuf(Args&&... args)
64 {
65  SBufStream out;
66  (out << ... << args);
67  return out.buf();
68 }
69 
70 #endif /* SQUID_SRC_SBUF_STREAM_H */
71 
Definition: SBuf.h:93
void clear()
Definition: SBuf.cc:175
SBuf buf()
bytes written so far
Definition: Stream.h:41
SBufStream()
Create an empty SBufStream.
Definition: Stream.h:38
SBufStream & clearBuf()
Clear the stream's backing store.
Definition: Stream.h:47
AppendingStreamBuf< SBuf > streamBuffer_
writes raw (post-formatting) bytes to the sink_
Definition: Stream.h:57
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:63
SBufStream(const SBuf &aBuf)
Definition: Stream.h:28
SBuf sink_
buffer for (flushed) bytes written to the stream
Definition: Stream.h:55

 

Introduction

Documentation

Support

Miscellaneous