Stream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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 #include "base/PackableStream.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  // TODO: Make this code readable after requiring C++17.
66  SBufStream out;
67  using expander = int[];
68  (void)expander {0, (void(out << std::forward<Args>(args)),0)...};
69  return out.buf();
70 }
71 
72 #endif /* SQUID_SBUFSTREAM_H */
73 
Definition: SBuf.h:87
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

Web Site Translations

Mirrors