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
18template <class Buffer>
19class AppendingStreamBuf : public std::streambuf
20{
21public:
22 explicit AppendingStreamBuf(Buffer &p): buf_(p) { postInit(); }
23 virtual ~AppendingStreamBuf() = default;
24
25protected:
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
55private:
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
72template <> inline void PackableStreamBuf::postInit() { buf_.buffer(); }
73template <> inline void PackableStreamBuf::postSync() { buf_.flush(); }
74
75class PackableStream : public std::ostream
76{
77public:
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(nullptr), theBuffer(p) {
81 rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
82 clear(); //clear badbit set by calling init(0)
83 }
84
85private:
87};
88
89#endif /* SQUID_SRC_BASE_PACKABLESTREAM_H */
90
write-only std::streambuf that append()s all writes to a given Buffer
virtual std::streamsize xsputn(const char *chars, std::streamsize number) override
AppendingStreamBuf(Buffer &p)
void postSync()
for specializations that must customize the last sync() step
virtual int_type overflow(int_type aChar=traits_type::eof()) override
void lowAppend(const char *s, const std::streamsize n)
void postInit()
for specializations that must customize the last construction step
virtual ~AppendingStreamBuf()=default
Buffer & buf_
the associated character sequence (a.k.a. the sink)
virtual int sync() override
PackableStreamBuf theBuffer
PackableStream(Packable &p)
static uint32 C
Definition: md4.c:43
STL namespace.
number
Definition: testStatHist.cc:16

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors