[Scummvm-cvs-logs] SF.net SVN: scummvm:[54326] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Thu Nov 18 18:02:52 CET 2010


Revision: 54326
          http://scummvm.svn.sourceforge.net/scummvm/?rev=54326&view=rev
Author:   fingolfin
Date:     2010-11-18 17:02:51 +0000 (Thu, 18 Nov 2010)

Log Message:
-----------
COMMON: Make implementation of Buffered*Stream classes internal

Modified Paths:
--------------
    scummvm/trunk/backends/fs/psp/psp-fs.cpp
    scummvm/trunk/common/stream.cpp
    scummvm/trunk/common/stream.h
    scummvm/trunk/common/unarj.cpp
    scummvm/trunk/test/common/bufferedreadstream.h
    scummvm/trunk/test/common/bufferedseekablereadstream.h

Modified: scummvm/trunk/backends/fs/psp/psp-fs.cpp
===================================================================
--- scummvm/trunk/backends/fs/psp/psp-fs.cpp	2010-11-18 17:01:30 UTC (rev 54325)
+++ scummvm/trunk/backends/fs/psp/psp-fs.cpp	2010-11-18 17:02:51 UTC (rev 54326)
@@ -251,7 +251,7 @@
 
 	Common::SeekableReadStream *stream = PspIoStream::makeFromPath(getPath(), false);
 
-	return new Common::BufferedSeekableReadStream(stream, READ_BUFFER_SIZE, DisposeAfterUse::YES);
+	return Common::wrapBufferedSeekableReadStream(stream, READ_BUFFER_SIZE, DisposeAfterUse::YES);
 }
 
 Common::WriteStream *PSPFilesystemNode::createWriteStream() {
@@ -259,7 +259,7 @@
 
 	Common::WriteStream *stream = PspIoStream::makeFromPath(getPath(), true);
 
-	return new Common::BufferedWriteStream(stream, WRITE_BUFFER_SIZE, DisposeAfterUse::YES);
+	return Common::wrapBufferedWriteStream(stream, WRITE_BUFFER_SIZE, DisposeAfterUse::YES);
 }
 
 #endif //#ifdef __PSP__

Modified: scummvm/trunk/common/stream.cpp
===================================================================
--- scummvm/trunk/common/stream.cpp	2010-11-18 17:01:30 UTC (rev 54325)
+++ scummvm/trunk/common/stream.cpp	2010-11-18 17:02:51 UTC (rev 54326)
@@ -89,6 +89,33 @@
 	return true;	// FIXME: STREAM REWRITE
 }
 
+bool MemoryWriteStreamDynamic::seek(int32 offs, int whence) {
+	// Pre-Condition
+	assert(_pos <= _size);
+	switch (whence) {
+	case SEEK_END:
+		// SEEK_END works just like SEEK_SET, only 'reversed',
+		// i.e. from the end.
+		offs = _size + offs;
+		// Fall through
+	case SEEK_SET:
+		_ptr = _data + offs;
+		_pos = offs;
+		break;
+
+	case SEEK_CUR:
+		_ptr += offs;
+		_pos += offs;
+		break;
+	}
+	// Post-Condition
+	assert(_pos <= _size);
+
+	return true;	// FIXME: STREAM REWRITE
+}
+
+#pragma mark -
+
 enum {
 	LF = 0x0A,
 	CR = 0x0D
@@ -222,6 +249,37 @@
 	return ret;
 }
 
+
+#pragma mark -
+
+namespace {
+
+/**
+ * Wrapper class which adds buffering to any given ReadStream.
+ * Users can specify how big the buffer should be, and whether the
+ * wrapped stream should be disposed when the wrapper is disposed.
+ */
+class BufferedReadStream : virtual public ReadStream {
+protected:
+	ReadStream *_parentStream;
+	DisposeAfterUse::Flag _disposeParentStream;
+	byte *_buf;
+	uint32 _pos;
+	bool _eos; // end of stream
+	uint32 _bufSize;
+	uint32 _realBufSize;
+
+public:
+	BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream);
+	virtual ~BufferedReadStream();
+
+	virtual bool eos() const { return _eos; }
+	virtual bool err() const { return _parentStream->err(); }
+	virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
+
+	virtual uint32 read(void *dataPtr, uint32 dataSize);
+};
+
 BufferedReadStream::BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream)
 	: _parentStream(parentStream),
 	_disposeParentStream(disposeParentStream),
@@ -290,6 +348,35 @@
 	return alreadyRead + dataSize;
 }
 
+}	// End of nameless namespace
+
+
+ReadStream *wrapBufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) {
+	if (parentStream)
+		return new BufferedReadStream(parentStream, bufSize, disposeParentStream);
+	return 0;
+}
+
+#pragma mark -
+
+namespace {
+
+/**
+ * Wrapper class which adds buffering to any given SeekableReadStream.
+ * @see BufferedReadStream
+ */
+class BufferedSeekableReadStream : public BufferedReadStream, public SeekableReadStream {
+protected:
+	SeekableReadStream *_parentStream;
+public:
+	BufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
+
+	virtual int32 pos() const { return _parentStream->pos() - (_bufSize - _pos); }
+	virtual int32 size() const { return _parentStream->size(); }
+
+	virtual bool seek(int32 offset, int whence = SEEK_SET);
+};
+
 BufferedSeekableReadStream::BufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream)
 	: BufferedReadStream(parentStream, bufSize, disposeParentStream),
 	_parentStream(parentStream) {
@@ -319,6 +406,46 @@
 	return true;
 }
 
+}	// End of nameless namespace
+
+SeekableReadStream *wrapBufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) {
+	if (parentStream)
+		return new BufferedSeekableReadStream(parentStream, bufSize, disposeParentStream);
+	return 0;
+}
+
+#pragma mark -
+
+namespace {
+
+/**
+ * Wrapper class which adds buffering to any WriteStream.
+ */
+class BufferedWriteStream : public WriteStream {
+protected:
+	WriteStream *_parentStream;
+	DisposeAfterUse::Flag _disposeParentStream;
+	byte *_buf;
+	uint32 _pos;
+	const uint32 _bufSize;
+
+	/**
+	 * Write out the data in the buffer.
+	 *
+	 * @note This method is identical to flush() (which actually is
+	 * 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(); }
+};
+
 BufferedWriteStream::BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream)
 	: _parentStream(parentStream),
 	_disposeParentStream(disposeParentStream),
@@ -369,29 +496,12 @@
 	return true;
 }
 
-bool MemoryWriteStreamDynamic::seek(int32 offs, int whence) {
-	// Pre-Condition
-	assert(_pos <= _size);
-	switch (whence) {
-	case SEEK_END:
-		// SEEK_END works just like SEEK_SET, only 'reversed',
-		// i.e. from the end.
-		offs = _size + offs;
-		// Fall through
-	case SEEK_SET:
-		_ptr = _data + offs;
-		_pos = offs;
-		break;
+}	// End of nameless namespace
 
-	case SEEK_CUR:
-		_ptr += offs;
-		_pos += offs;
-		break;
-	}
-	// Post-Condition
-	assert(_pos <= _size);
-
-	return true;	// FIXME: STREAM REWRITE
+WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) {
+	if (parentStream)
+		return new BufferedWriteStream(parentStream, bufSize, disposeParentStream);
+	return 0;
 }
 
 }	// End of namespace Common

Modified: scummvm/trunk/common/stream.h
===================================================================
--- scummvm/trunk/common/stream.h	2010-11-18 17:01:30 UTC (rev 54325)
+++ scummvm/trunk/common/stream.h	2010-11-18 17:02:51 UTC (rev 54326)
@@ -484,75 +484,38 @@
 };
 
 /**
- * Wrapper class which adds buffering to any given ReadStream.
- * Users can specify how big the buffer should be, and whether the
- * wrapped stream should be disposed when the wrapper is disposed.
+ * Take an arbitrary ReadStream and wrap it in a custom stream which
+ * transparently provides buffering.
+ * Users can specify how big the buffer should be, and whether the wrapped
+ * stream should be disposed when the wrapper is disposed.
+ *
+ * It is safe to call this with a NULL parameter (in this case, NULL is
+ * returned).
  */
-class BufferedReadStream : virtual public ReadStream {
-protected:
-	ReadStream *_parentStream;
-	DisposeAfterUse::Flag _disposeParentStream;
-	byte *_buf;
-	uint32 _pos;
-	bool _eos; // end of stream
-	uint32 _bufSize;
-	uint32 _realBufSize;
+ReadStream *wrapBufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream);
 
-public:
-	BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
-	virtual ~BufferedReadStream();
-
-	virtual bool eos() const { return _eos; }
-	virtual bool err() const { return _parentStream->err(); }
-	virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
-
-	virtual uint32 read(void *dataPtr, uint32 dataSize);
-};
-
 /**
- * Wrapper class which adds buffering to any given SeekableReadStream.
- * @see BufferedReadStream
+ * Take an arbitrary SeekableReadStream and wrap it in a custom stream which
+ * transparently provides buffering.
+ * Users can specify how big the buffer should be, and whether the wrapped
+ * stream should be disposed when the wrapper is disposed.
+ *
+ * It is safe to call this with a NULL parameter (in this case, NULL is
+ * returned).
  */
-class BufferedSeekableReadStream : public BufferedReadStream, public SeekableReadStream {
-protected:
-	SeekableReadStream *_parentStream;
-public:
-	BufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
+SeekableReadStream *wrapBufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream);
 
-	virtual int32 pos() const { return _parentStream->pos() - (_bufSize - _pos); }
-	virtual int32 size() const { return _parentStream->size(); }
-
-	virtual bool seek(int32 offset, int whence = SEEK_SET);
-};
-
 /**
- * Wrapper class which adds buffering to any WriteStream.
+ * Take an arbitrary WriteStream and wrap it in a custom stream which
+ * transparently provides buffering.
+ * Users can specify how big the buffer should be, and whether the wrapped
+ * stream should be disposed when the wrapper is disposed.
+ *
+ * It is safe to call this with a NULL parameter (in this case, NULL is
+ * returned).
  */
-class BufferedWriteStream : public WriteStream {
-protected:
-	WriteStream *_parentStream;
-	DisposeAfterUse::Flag _disposeParentStream;
-	byte *_buf;
-	uint32 _pos;
-	const uint32 _bufSize;
+WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream);
 
-	/**
-	 * Write out the data in the buffer.
-	 *
-	 * @note This method is identical to flush() (which actually is
-	 * 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(); }
-};
-
 /**
  * Simple memory based 'stream', which implements the ReadStream interface for
  * a plain memory block.
@@ -606,7 +569,7 @@
  * This is a wrapper around MemoryReadStream, but it adds non-endian
  * read methods whose endianness is set on the stream creation.
  */
-class MemoryReadStreamEndian : public Common::MemoryReadStream {
+class MemoryReadStreamEndian : public MemoryReadStream {
 private:
 	const bool _bigEndian;
 
@@ -664,7 +627,7 @@
  * A sort of hybrid between MemoryWriteStream and Array classes. A stream
  * that grows as it's written to.
  */
-class MemoryWriteStreamDynamic : public Common::WriteStream {
+class MemoryWriteStreamDynamic : public WriteStream {
 private:
 	uint32 _capacity;
 	uint32 _size;

Modified: scummvm/trunk/common/unarj.cpp
===================================================================
--- scummvm/trunk/common/unarj.cpp	2010-11-18 17:01:30 UTC (rev 54325)
+++ scummvm/trunk/common/unarj.cpp	2010-11-18 17:02:51 UTC (rev 54326)
@@ -108,7 +108,7 @@
 	void decode(int32 origsize);
 	void decode_f(int32 origsize);
 
-	BufferedReadStream *_compressed;
+	ReadStream *_compressed;
 	MemoryWriteStream *_outstream;
 
 //protected:
@@ -817,7 +817,7 @@
 		// If reading from archiveFile directly is too slow to be usable,
 		// maybe the filesystem code should instead wrap its files
 		// in a BufferedReadStream.
-		decoder->_compressed = new Common::BufferedReadStream(&archiveFile, 4096, DisposeAfterUse::NO);
+		decoder->_compressed = Common::wrapBufferedReadStream(&archiveFile, 4096, DisposeAfterUse::NO);
 		decoder->_outstream = new Common::MemoryWriteStream(uncompressedData, hdr->origSize);
 
 		if (hdr->method == 1 || hdr->method == 2 || hdr->method == 3)

Modified: scummvm/trunk/test/common/bufferedreadstream.h
===================================================================
--- scummvm/trunk/test/common/bufferedreadstream.h	2010-11-18 17:01:30 UTC (rev 54325)
+++ scummvm/trunk/test/common/bufferedreadstream.h	2010-11-18 17:02:51 UTC (rev 54326)
@@ -11,7 +11,7 @@
 		// Use a buffer size of 4 -- note that 10 % 4 != 0,
 		// so we test what happens if the cache can't be completely
 		// refilled.
-		Common::BufferedReadStream srs(&ms, 4);
+		Common::ReadStream &srs = *Common::wrapBufferedReadStream(&ms, 4, DisposeAfterUse::NO);
 
 		byte i, b;
 		for (i = 0; i < 10; ++i) {
@@ -26,13 +26,15 @@
 		b = srs.readByte();
 
 		TS_ASSERT(srs.eos());
+
+		delete &srs;
 	}
 
 	void test_traverse2() {
 		byte contents[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
 		Common::MemoryReadStream ms(contents, 9);
 
-		Common::BufferedReadStream brs(&ms, 4);
+		Common::ReadStream &brs = *Common::wrapBufferedReadStream(&ms, 4, DisposeAfterUse::NO);
 
 		// Traverse the stream with reads of 2 bytes. The size is not
 		// a multiple of 2, so we can test the final partial read.
@@ -51,5 +53,7 @@
 		TS_ASSERT_EQUALS(n, 1);
 
 		TS_ASSERT(brs.eos());
+
+		delete &brs;
 	}
 };

Modified: scummvm/trunk/test/common/bufferedseekablereadstream.h
===================================================================
--- scummvm/trunk/test/common/bufferedseekablereadstream.h	2010-11-18 17:01:30 UTC (rev 54325)
+++ scummvm/trunk/test/common/bufferedseekablereadstream.h	2010-11-18 17:02:51 UTC (rev 54326)
@@ -8,7 +8,8 @@
 		byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 		Common::MemoryReadStream ms(contents, 10);
 
-		Common::BufferedSeekableReadStream ssrs(&ms, 4);
+		Common::SeekableReadStream &ssrs
+			= *Common::wrapBufferedSeekableReadStream(&ms, 4, DisposeAfterUse::NO);
 
 		byte i, b;
 		for (i = 0; i < 10; ++i) {
@@ -24,13 +25,16 @@
 
 		TS_ASSERT_EQUALS((uint)0, ssrs.read(&b, 1));
 		TS_ASSERT(ssrs.eos());
+
+		delete &ssrs;
 	}
 
 	void test_seek() {
 		byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 		Common::MemoryReadStream ms(contents, 10);
 
-		Common::BufferedSeekableReadStream ssrs(&ms, 4);
+		Common::SeekableReadStream &ssrs
+			= *Common::wrapBufferedSeekableReadStream(&ms, 4, DisposeAfterUse::NO);
 		byte b;
 
 		TS_ASSERT_EQUALS(ssrs.pos(), 0);
@@ -66,5 +70,7 @@
 		TS_ASSERT_EQUALS(ssrs.pos(), 2);
 		b = ssrs.readByte();
 		TS_ASSERT_EQUALS(b, 2);
+
+		delete &ssrs;
 	}
 };


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list