]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/sbuf/Stream.h
1eeaf8f2f8beaadd7eee01bfbb7437774636dd34
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_SBUFSTREAM_H
10 #define SQUID_SBUFSTREAM_H
12 #include "sbuf/SBuf.h"
16 /** streambuf class for a SBuf-backed stream interface.
18 * Auxiliary class to be able to leverage an ostream generating SBuf's
19 * analogous to std::ostrstream.
21 class SBufStreamBuf
: public std::streambuf
24 /// initialize streambuf; use supplied SBuf as backing store
25 explicit SBufStreamBuf(SBuf aBuf
) : theBuf(aBuf
) {}
27 /// get a copy of the stream's contents
32 /// clear the stream's store
38 virtual int_type
overflow(int_type aChar
= traits_type::eof()) {
39 std::streamsize
pending(pptr() - pbase());
41 if (pending
&& sync())
42 return traits_type::eof();
44 if (aChar
!= traits_type::eof()) {
45 char chars
[1] = {static_cast<char>(aChar
)};
47 if (aChar
!= traits_type::eof())
48 theBuf
.append(chars
, 1);
51 pbump(-pending
); // Reset pptr().
55 /// push the streambuf to the backing SBuf
57 std::streamsize
pending(pptr() - pbase());
60 theBuf
.append(pbase(), pending
);
65 /** write multiple characters to the store entry
66 * \note this is an optimisation consistent with std::streambuf API
68 virtual std::streamsize
xsputn(const char * chars
, std::streamsize number
) {
70 theBuf
.append(chars
, number
);
77 SBufStreamBuf(); // no default constructor
80 /** Stream interface to write to a SBuf.
82 * Data is appended using standard operator << semantics, and extracted
83 * using the buf() method, in analogy with std::strstream .
85 class SBufStream
: public std::ostream
88 /** Create a SBufStream preinitialized with the contents of a SBuf
90 * The supplied SBuf copied: in order to retrieve the written-to contents
91 * they must be later fetched using the buf() class method.
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)
98 /// Create an empty SBufStream
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)
104 /// Retrieve a copy of the current stream status
107 return theBuffer
.getBuf();
110 /// Clear the stream's backing store
111 SBufStream
& clearBuf() {
113 theBuffer
.clearBuf();
118 SBufStreamBuf theBuffer
;
121 #endif /* SQUID_SBUFSTREAM_H */