1 | // |
---|
2 | // MemoryStream.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/MemoryStream.h#3 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: MemoryStream |
---|
9 | // |
---|
10 | // Definition of MemoryStreamBuf, MemoryInputStream, MemoryOutputStream |
---|
11 | // |
---|
12 | // Copyright (c) 2009, 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_MemoryStream_INCLUDED |
---|
40 | #define Foundation_MemoryStream_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/StreamUtil.h" |
---|
45 | #include <streambuf> |
---|
46 | #include <iosfwd> |
---|
47 | #include <ios> |
---|
48 | #include <istream> |
---|
49 | #include <ostream> |
---|
50 | |
---|
51 | |
---|
52 | namespace Poco { |
---|
53 | |
---|
54 | |
---|
55 | template <typename ch, typename tr> |
---|
56 | class BasicMemoryStreamBuf: public std::basic_streambuf<ch, tr> |
---|
57 | /// BasicMemoryStreamBuf is a simple implementation of a |
---|
58 | /// stream buffer for reading and writing from a memory area. |
---|
59 | /// |
---|
60 | /// This streambuf only supports unidirectional streams. |
---|
61 | /// In other words, the BasicBufferedStreamBuf can be |
---|
62 | /// used for the implementation of an istream or an |
---|
63 | /// ostream, but not for an iostream. |
---|
64 | { |
---|
65 | protected: |
---|
66 | typedef std::basic_streambuf<ch, tr> Base; |
---|
67 | typedef std::basic_ios<ch, tr> IOS; |
---|
68 | typedef ch char_type; |
---|
69 | typedef tr char_traits; |
---|
70 | typedef typename Base::int_type int_type; |
---|
71 | typedef typename Base::pos_type pos_type; |
---|
72 | typedef typename Base::off_type off_type; |
---|
73 | |
---|
74 | public: |
---|
75 | BasicMemoryStreamBuf(char_type* pBuffer, std::streamsize bufferSize): |
---|
76 | _pBuffer(pBuffer), |
---|
77 | _bufferSize(bufferSize) |
---|
78 | { |
---|
79 | this->setg(_pBuffer, _pBuffer, _pBuffer + _bufferSize); |
---|
80 | this->setp(_pBuffer, _pBuffer + _bufferSize); |
---|
81 | } |
---|
82 | |
---|
83 | ~BasicMemoryStreamBuf() |
---|
84 | { |
---|
85 | } |
---|
86 | |
---|
87 | virtual int_type overflow(int_type c) |
---|
88 | { |
---|
89 | return char_traits::eof(); |
---|
90 | } |
---|
91 | |
---|
92 | virtual int_type underflow() |
---|
93 | { |
---|
94 | return char_traits::eof(); |
---|
95 | } |
---|
96 | |
---|
97 | virtual int sync() |
---|
98 | { |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | std::streamsize charsWritten() const |
---|
103 | { |
---|
104 | return static_cast<std::streamsize>(this->pptr() - this->pbase()); |
---|
105 | } |
---|
106 | |
---|
107 | private: |
---|
108 | char_type* _pBuffer; |
---|
109 | std::streamsize _bufferSize; |
---|
110 | |
---|
111 | BasicMemoryStreamBuf(); |
---|
112 | BasicMemoryStreamBuf(const BasicMemoryStreamBuf&); |
---|
113 | BasicMemoryStreamBuf& operator = (const BasicMemoryStreamBuf&); |
---|
114 | }; |
---|
115 | |
---|
116 | |
---|
117 | // |
---|
118 | // We provide an instantiation for char |
---|
119 | // |
---|
120 | typedef BasicMemoryStreamBuf<char, std::char_traits<char> > MemoryStreamBuf; |
---|
121 | |
---|
122 | |
---|
123 | class Foundation_API MemoryIOS: public virtual std::ios |
---|
124 | /// The base class for MemoryInputStream and MemoryOutputStream. |
---|
125 | /// |
---|
126 | /// This class is needed to ensure the correct initialization |
---|
127 | /// order of the stream buffer and base classes. |
---|
128 | { |
---|
129 | public: |
---|
130 | MemoryIOS(char* pBuffer, std::streamsize bufferSize); |
---|
131 | /// Creates the basic stream. |
---|
132 | |
---|
133 | ~MemoryIOS(); |
---|
134 | /// Destroys the stream. |
---|
135 | |
---|
136 | MemoryStreamBuf* rdbuf(); |
---|
137 | /// Returns a pointer to the underlying streambuf. |
---|
138 | |
---|
139 | protected: |
---|
140 | MemoryStreamBuf _buf; |
---|
141 | }; |
---|
142 | |
---|
143 | |
---|
144 | class Foundation_API MemoryInputStream: public MemoryIOS, public std::istream |
---|
145 | /// An input stream for reading from a memory area. |
---|
146 | { |
---|
147 | public: |
---|
148 | MemoryInputStream(const char* pBuffer, std::streamsize bufferSize); |
---|
149 | /// Creates a MemoryInputStream for the given memory area, |
---|
150 | /// ready for reading. |
---|
151 | |
---|
152 | ~MemoryInputStream(); |
---|
153 | /// Destroys the MemoryInputStream. |
---|
154 | }; |
---|
155 | |
---|
156 | |
---|
157 | class Foundation_API MemoryOutputStream: public MemoryIOS, public std::ostream |
---|
158 | /// An input stream for reading from a memory area. |
---|
159 | { |
---|
160 | public: |
---|
161 | MemoryOutputStream(char* pBuffer, std::streamsize bufferSize); |
---|
162 | /// Creates a MemoryOutputStream for the given memory area, |
---|
163 | /// ready for writing. |
---|
164 | |
---|
165 | ~MemoryOutputStream(); |
---|
166 | /// Destroys the MemoryInputStream. |
---|
167 | |
---|
168 | std::streamsize charsWritten() const; |
---|
169 | /// Returns the number of chars written to the buffer. |
---|
170 | }; |
---|
171 | |
---|
172 | |
---|
173 | // |
---|
174 | // inlines |
---|
175 | // |
---|
176 | inline std::streamsize MemoryOutputStream::charsWritten() const |
---|
177 | { |
---|
178 | return _buf.charsWritten(); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | } // namespace Poco |
---|
183 | |
---|
184 | |
---|
185 | #endif // Foundation_MemoryStream_INCLUDED |
---|