source: box/trunk/lib/common/SelfFlushingStream.h @ 2254

Revision 2254, 1.6 KB checked in by chris, 4 years ago (diff)

Add a Flush() method to IOStream to read and discard all remaining data,
and a SelfFlushingStream? class which can be used to ensure that protocol
streams are always flushed, to avoid breaking protocol.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    SelfFlushingStream.h
5//              Purpose: A stream wrapper that always flushes the underlying
6//                       stream, to ensure protocol safety.
7//              Created: 2008/08/20
8//
9// --------------------------------------------------------------------------
10
11#ifndef SELFFLUSHINGSTREAM__H
12#define SELFFLUSHINGSTREAM__H
13
14#include "IOStream.h"
15
16// --------------------------------------------------------------------------
17//
18// Class
19//              Name:    SelfFlushingStream
20//              Purpose: A stream wrapper that always flushes the underlying
21//                       stream, to ensure protocol safety.
22//              Created: 2008/08/20
23//
24// --------------------------------------------------------------------------
25class SelfFlushingStream : public IOStream
26{
27public:
28        SelfFlushingStream(IOStream &rSource)
29        : mrSource(rSource) { }
30
31        SelfFlushingStream(const SelfFlushingStream &rToCopy)
32        : mrSource(rToCopy.mrSource) { }
33       
34        ~SelfFlushingStream()
35        {
36                Flush();
37        }
38
39private:
40        // no copying from IOStream allowed
41        SelfFlushingStream(const IOStream& rToCopy);
42       
43public:
44        virtual int Read(void *pBuffer, int NBytes,
45                int Timeout = IOStream::TimeOutInfinite)
46        {
47                return mrSource.Read(pBuffer, NBytes, Timeout);
48        }
49        virtual pos_type BytesLeftToRead()
50        {
51                return mrSource.BytesLeftToRead();
52        }
53        virtual void Write(const void *pBuffer, int NBytes)
54        {
55                mrSource.Write(pBuffer, NBytes);
56        }
57        virtual bool StreamDataLeft()
58        {
59                return mrSource.StreamDataLeft();
60        }
61        virtual bool StreamClosed()
62        {
63                return mrSource.StreamClosed();
64        }
65
66private:
67        IOStream &mrSource;
68};
69
70#endif // SELFFLUSHINGSTREAM__H
71
Note: See TracBrowser for help on using the repository browser.