aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMax Horn2010-11-18 17:56:21 +0000
committerMax Horn2010-11-18 17:56:21 +0000
commita9dcb11c54274053152ca7e657bc4d19afb26e92 (patch)
treebb6581f897597854280bfae08d32189422a0685c /common
parentc02a2330849946b77070af8aa955ab1fef810f88 (diff)
downloadscummvm-rg350-a9dcb11c54274053152ca7e657bc4d19afb26e92.tar.gz
scummvm-rg350-a9dcb11c54274053152ca7e657bc4d19afb26e92.tar.bz2
scummvm-rg350-a9dcb11c54274053152ca7e657bc4d19afb26e92.zip
COMMON: Turn BufferedWriteStream into a template class, with disposeParentStream as param
svn-id: r54336
Diffstat (limited to 'common')
-rw-r--r--common/stream.cpp106
1 files changed, 53 insertions, 53 deletions
diff --git a/common/stream.cpp b/common/stream.cpp
index 7a2eb2e23e..b82a70409a 100644
--- a/common/stream.cpp
+++ b/common/stream.cpp
@@ -421,10 +421,10 @@ namespace {
/**
* Wrapper class which adds buffering to any WriteStream.
*/
+template <DisposeAfterUse::Flag _disposeParentStream>
class BufferedWriteStream : public WriteStream {
protected:
WriteStream *_parentStream;
- DisposeAfterUse::Flag _disposeParentStream;
byte *_buf;
uint32 _pos;
const uint32 _bufSize;
@@ -436,71 +436,71 @@ protected:
* implemented by calling this method), except that it is not
* virtual, hence there is less overhead calling it.
*/
- bool flushBuffer();
-
-public:
- BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
- virtual ~BufferedWriteStream();
-
- virtual uint32 write(const void *dataPtr, uint32 dataSize);
- virtual bool flush() { return flushBuffer(); }
-};
+ bool flushBuffer() {
+ const uint32 bytesToWrite = _pos;
-BufferedWriteStream::BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream)
- : _parentStream(parentStream),
- _disposeParentStream(disposeParentStream),
- _pos(0),
- _bufSize(bufSize) {
+ if (bytesToWrite) {
+ _pos = 0;
+ if (_parentStream->write(_buf, bytesToWrite) != bytesToWrite)
+ return false;
+ }
+ return true;
+ }
- assert(parentStream);
- _buf = new byte[bufSize];
- assert(_buf);
-}
+public:
+ BufferedWriteStream(WriteStream *parentStream, uint32 bufSize)
+ : _parentStream(parentStream),
+ _pos(0),
+ _bufSize(bufSize) {
+
+ assert(parentStream);
+ _buf = new byte[bufSize];
+ assert(_buf);
+ }
-BufferedWriteStream::~BufferedWriteStream() {
- const bool flushResult = flushBuffer();
- assert(flushResult);
+ virtual ~BufferedWriteStream() {
+ const bool flushResult = flushBuffer();
+ assert(flushResult);
- if (_disposeParentStream)
- delete _parentStream;
+ if (_disposeParentStream)
+ delete _parentStream;
- delete[] _buf;
-}
+ delete[] _buf;
+ }
-uint32 BufferedWriteStream::write(const void *dataPtr, uint32 dataSize) {
- // check if we have enough space for writing to the buffer
- if (_bufSize - _pos >= dataSize) {
- memcpy(_buf + _pos, dataPtr, dataSize);
- _pos += dataSize;
- } else if (_bufSize >= dataSize) { // check if we can flush the buffer and load the data
- const bool flushResult = flushBuffer();
- assert(flushResult);
- memcpy(_buf, dataPtr, dataSize);
- _pos += dataSize;
- } else { // too big for our buffer
- const bool flushResult = flushBuffer();
- assert(flushResult);
- return _parentStream->write(dataPtr, dataSize);
+ virtual uint32 write(const void *dataPtr, uint32 dataSize) {
+ // check if we have enough space for writing to the buffer
+ if (_bufSize - _pos >= dataSize) {
+ memcpy(_buf + _pos, dataPtr, dataSize);
+ _pos += dataSize;
+ } else if (_bufSize >= dataSize) { // check if we can flush the buffer and load the data
+ const bool flushResult = flushBuffer();
+ assert(flushResult);
+ memcpy(_buf, dataPtr, dataSize);
+ _pos += dataSize;
+ } else { // too big for our buffer
+ const bool flushResult = flushBuffer();
+ assert(flushResult);
+ return _parentStream->write(dataPtr, dataSize);
+ }
+ return dataSize;
}
- return dataSize;
-}
-bool BufferedWriteStream::flushBuffer() {
- const uint32 bytesToWrite = _pos;
+ virtual bool flush() { return flushBuffer(); }
- if (bytesToWrite) {
- _pos = 0;
- if (_parentStream->write(_buf, bytesToWrite) != bytesToWrite)
- return false;
- }
- return true;
-}
+};
} // End of nameless namespace
WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) {
- if (parentStream)
- return new BufferedWriteStream(parentStream, bufSize, disposeParentStream);
+ if (parentStream) {
+ switch (disposeParentStream) {
+ case DisposeAfterUse::YES:
+ return new BufferedWriteStream<DisposeAfterUse::YES>(parentStream, bufSize);
+ case DisposeAfterUse::NO:
+ return new BufferedWriteStream<DisposeAfterUse::NO>(parentStream, bufSize);
+ }
+ }
return 0;
}