root / CCV-HAND / libs / poco / include / Poco / UnbufferedStreamBuf.h @ 59
View | Annotate | Download (4.6 KB)
| 1 | //
|
|---|---|
| 2 | // UnufferedStreamBuf.h
|
| 3 | //
|
| 4 | // $Id: //poco/1.3/Foundation/include/Poco/UnbufferedStreamBuf.h#3 $
|
| 5 | //
|
| 6 | // Library: Foundation
|
| 7 | // Package: Streams
|
| 8 | // Module: StreamBuf
|
| 9 | //
|
| 10 | // Definition of template BasicUnbufferedStreamBuf and class UnbufferedStreamBuf.
|
| 11 | //
|
| 12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
| 13 | // and Contributors.
|
| 14 | //
|
| 15 | // Permission is hereby granted, free of charge, to any person or organization
|
| 16 | // obtaining a copy of the software and accompanying documentation covered by
|
| 17 | // this license (the "Software") to use, reproduce, display, distribute,
|
| 18 | // execute, and transmit the Software, and to prepare derivative works of the
|
| 19 | // Software, and to permit third-parties to whom the Software is furnished to
|
| 20 | // do so, all subject to the following:
|
| 21 | //
|
| 22 | // The copyright notices in the Software and this entire statement, including
|
| 23 | // the above license grant, this restriction and the following disclaimer,
|
| 24 | // must be included in all copies of the Software, in whole or in part, and
|
| 25 | // all derivative works of the Software, unless such copies or derivative
|
| 26 | // works are solely in the form of machine-executable object code generated by
|
| 27 | // a source language processor.
|
| 28 | //
|
| 29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
| 32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
| 33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
| 34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
| 35 | // DEALINGS IN THE SOFTWARE.
|
| 36 | //
|
| 37 | |
| 38 | |
| 39 | #ifndef Foundation_UnbufferedStreamBuf_INCLUDED
|
| 40 | #define Foundation_UnbufferedStreamBuf_INCLUDED
|
| 41 | |
| 42 | |
| 43 | #include "Poco/Foundation.h" |
| 44 | #include "Poco/StreamUtil.h" |
| 45 | #include <streambuf> |
| 46 | #include <iosfwd> |
| 47 | #include <ios> |
| 48 | |
| 49 | |
| 50 | namespace Poco {
|
| 51 | |
| 52 | |
| 53 | template <typename ch, typename tr> |
| 54 | class BasicUnbufferedStreamBuf: public std::basic_streambuf<ch, tr>
|
| 55 | /// This is an implementation of an unbuffered streambuf
|
| 56 | /// that greatly simplifies the implementation of
|
| 57 | /// custom streambufs of various kinds.
|
| 58 | /// Derived classes only have to override the methods
|
| 59 | /// readFromDevice() or writeToDevice().
|
| 60 | {
|
| 61 | protected:
|
| 62 | typedef std::basic_streambuf<ch, tr> Base;
|
| 63 | typedef std::basic_ios<ch, tr> IOS;
|
| 64 | typedef ch char_type;
|
| 65 | typedef tr char_traits;
|
| 66 | typedef typename Base::int_type int_type;
|
| 67 | typedef typename Base::pos_type pos_type;
|
| 68 | typedef typename Base::off_type off_type;
|
| 69 | typedef typename IOS::openmode openmode;
|
| 70 | |
| 71 | public:
|
| 72 | BasicUnbufferedStreamBuf(): |
| 73 | _pb(char_traits::eof()), |
| 74 | _ispb(false)
|
| 75 | {
|
| 76 | this->setg(0, 0, 0); |
| 77 | this->setp(0, 0); |
| 78 | } |
| 79 | |
| 80 | ~BasicUnbufferedStreamBuf() |
| 81 | {
|
| 82 | } |
| 83 | |
| 84 | virtual int_type overflow(int_type c) |
| 85 | {
|
| 86 | if (c != char_traits::eof())
|
| 87 | return writeToDevice(char_traits::to_char_type(c));
|
| 88 | else
|
| 89 | return c;
|
| 90 | } |
| 91 | |
| 92 | virtual int_type underflow() |
| 93 | {
|
| 94 | if (_ispb)
|
| 95 | {
|
| 96 | return _pb;
|
| 97 | } |
| 98 | else
|
| 99 | {
|
| 100 | int_type c = readFromDevice(); |
| 101 | if (c != char_traits::eof())
|
| 102 | {
|
| 103 | _ispb = true;
|
| 104 | _pb = c; |
| 105 | } |
| 106 | return c;
|
| 107 | } |
| 108 | } |
| 109 | |
| 110 | virtual int_type uflow() |
| 111 | {
|
| 112 | if (_ispb)
|
| 113 | {
|
| 114 | _ispb = false;
|
| 115 | return _pb;
|
| 116 | } |
| 117 | else
|
| 118 | {
|
| 119 | int_type c = readFromDevice(); |
| 120 | if (c != char_traits::eof())
|
| 121 | {
|
| 122 | _pb = c; |
| 123 | } |
| 124 | return c;
|
| 125 | } |
| 126 | } |
| 127 | |
| 128 | virtual int_type pbackfail(int_type c) |
| 129 | {
|
| 130 | if (_ispb)
|
| 131 | {
|
| 132 | return char_traits::eof();
|
| 133 | } |
| 134 | else
|
| 135 | {
|
| 136 | _ispb = true;
|
| 137 | _pb = c; |
| 138 | return c;
|
| 139 | } |
| 140 | } |
| 141 | |
| 142 | virtual std::streamsize xsgetn(char_type* p, std::streamsize count) |
| 143 | /// Some platforms (for example, Compaq C++) have buggy implementations of
|
| 144 | /// xsgetn that handle null buffers incorrectly.
|
| 145 | /// Anyway, it does not hurt to provide an optimized implementation
|
| 146 | /// of xsgetn for this streambuf implementation.
|
| 147 | {
|
| 148 | std::streamsize copied = 0;
|
| 149 | while (count > 0) |
| 150 | {
|
| 151 | int_type c = uflow(); |
| 152 | if (c == char_traits::eof()) break; |
| 153 | *p++ = char_traits::to_char_type(c); |
| 154 | ++copied; |
| 155 | --count; |
| 156 | } |
| 157 | return copied;
|
| 158 | } |
| 159 | |
| 160 | protected:
|
| 161 | static int_type charToInt(char_type c)
|
| 162 | {
|
| 163 | return char_traits::to_int_type(c);
|
| 164 | } |
| 165 | |
| 166 | private:
|
| 167 | virtual int_type readFromDevice() |
| 168 | {
|
| 169 | return char_traits::eof();
|
| 170 | } |
| 171 | |
| 172 | virtual int_type writeToDevice(char_type) |
| 173 | {
|
| 174 | return char_traits::eof();
|
| 175 | } |
| 176 | |
| 177 | int_type _pb; |
| 178 | bool _ispb;
|
| 179 | |
| 180 | BasicUnbufferedStreamBuf(const BasicUnbufferedStreamBuf&);
|
| 181 | BasicUnbufferedStreamBuf& operator = (const BasicUnbufferedStreamBuf&);
|
| 182 | }; |
| 183 | |
| 184 | |
| 185 | //
|
| 186 | // We provide an instantiation for char
|
| 187 | //
|
| 188 | typedef BasicUnbufferedStreamBuf<char, std::char_traits<char> > UnbufferedStreamBuf; |
| 189 | |
| 190 | |
| 191 | } // namespace Poco
|
| 192 | |
| 193 | |
| 194 | #endif // Foundation_UnbufferedStreamBuf_INCLUDED |
