PackableStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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_BASE_PACKABLESTREAM_H
10 #define SQUID_SRC_BASE_PACKABLESTREAM_H
11 
12 #include "base/Packable.h"
13 
14 #include <ostream>
15 
16 // TODO: Move to src/base/AppendingStreamBuf.h
18 template <class Buffer>
19 class AppendingStreamBuf : public std::streambuf
20 {
21 public:
22  explicit AppendingStreamBuf(Buffer &p): buf_(p) { postInit(); }
23  virtual ~AppendingStreamBuf() = default;
24 
25 protected:
26  /* std::streambuf API */
27 
28  virtual int_type overflow(int_type aChar = traits_type::eof()) override {
29  std::streamsize pending(pptr() - pbase());
30 
31  if (pending && sync())
32  return traits_type::eof();
33 
34  if (aChar != traits_type::eof()) {
35  const char C = static_cast<char>(aChar);
36  lowAppend(&C, 1);
37  }
38 
39  pbump(-pending); // Reset pptr().
40  return aChar;
41  }
42 
43  virtual int sync() override {
44  std::streamsize pending(pptr() - pbase());
45  lowAppend(pbase(), pending);
46  postSync();
47  return 0;
48  }
49 
50  virtual std::streamsize xsputn(const char * chars, std::streamsize number) override {
51  lowAppend(chars, number);
52  return number;
53  }
54 
55 private:
57  void postInit() {}
58 
60  void postSync() {}
61 
62  void lowAppend(const char *s, const std::streamsize n) {buf_.append(s,n);}
63 
64  Buffer &buf_;
65 };
66 
72 template <> inline void PackableStreamBuf::postInit() { buf_.buffer(); }
73 template <> inline void PackableStreamBuf::postSync() { buf_.flush(); }
74 
75 class PackableStream : public std::ostream
76 {
77 public:
78  /* create a stream for writing text etc into theBuffer */
79  // See http://www.codecomments.com/archive292-2005-2-396222.html
80  explicit PackableStream(Packable &p) : std::ostream(0), theBuffer(p) {
81  rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
82  clear(); //clear badbit set by calling init(0)
83  }
84 
85 private:
87 };
88 
89 #endif /* SQUID_SRC_BASE_PACKABLESTREAM_H */
90 
virtual ~AppendingStreamBuf()=default
write-only std::streambuf that append()s all writes to a given Buffer
PackableStreamBuf theBuffer
virtual int_type overflow(int_type aChar=traits_type::eof()) override
AppendingStreamBuf(Buffer &p)
Buffer & buf_
the associated character sequence (a.k.a. the sink)
number
Definition: testStatHist.cc:16
virtual int sync() override
PackableStream(Packable &p)
void lowAppend(const char *s, const std::streamsize n)
static uint32 C
Definition: md4.c:43
virtual std::streamsize xsputn(const char *chars, std::streamsize number) override
void postInit()
for specializations that must customize the last construction step
void postSync()
for specializations that must customize the last sync() step

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors