aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMax Horn2010-11-18 16:08:56 +0000
committerMax Horn2010-11-18 16:08:56 +0000
commit4707b610fca45c0762e25994594fc58557945a02 (patch)
tree01437dc021de6452b231058d2bb7845c38534e42 /common
parentbac018a3aa1dea0282f5ba49f9eb2001737e8bee (diff)
downloadscummvm-rg350-4707b610fca45c0762e25994594fc58557945a02.tar.gz
scummvm-rg350-4707b610fca45c0762e25994594fc58557945a02.tar.bz2
scummvm-rg350-4707b610fca45c0762e25994594fc58557945a02.zip
COMMON: Fix incorrect use of assert() macro
The assert() macro may be compiled to be empty. In that case, its arguments are *NOT* evaluated. Hence, things like assert(doSomething()) must not be used whenever doSomething() has important side effects. Also document BufferedWriteStream::flushBuffer() and explain why it exists parallel to BufferedWriteStream::flush(). svn-id: r54322
Diffstat (limited to 'common')
-rw-r--r--common/stream.cpp17
-rw-r--r--common/stream.h14
2 files changed, 18 insertions, 13 deletions
diff --git a/common/stream.cpp b/common/stream.cpp
index 9f8f6127f1..0e8e578e86 100644
--- a/common/stream.cpp
+++ b/common/stream.cpp
@@ -331,7 +331,8 @@ BufferedWriteStream::BufferedWriteStream(WriteStream *parentStream, uint32 bufSi
}
BufferedWriteStream::~BufferedWriteStream() {
- assert(flush());
+ const bool flushResult = flushBuffer();
+ assert(flushResult);
if (_disposeParentStream)
delete _parentStream;
@@ -345,20 +346,20 @@ uint32 BufferedWriteStream::write(const void *dataPtr, uint32 dataSize) {
memcpy(_buf + _pos, dataPtr, dataSize);
_pos += dataSize;
} else if (_bufSize >= dataSize) { // check if we can flush the buffer and load the data
- // flush the buffer
- assert(flushBuffer());
+ const bool flushResult = flushBuffer();
+ assert(flushResult);
memcpy(_buf, dataPtr, dataSize);
_pos += dataSize;
} else { // too big for our buffer
- // flush the buffer
- assert(flushBuffer());
+ const bool flushResult = flushBuffer();
+ assert(flushResult);
return _parentStream->write(dataPtr, dataSize);
}
return dataSize;
}
bool BufferedWriteStream::flushBuffer() {
- uint32 bytesToWrite = _pos;
+ const uint32 bytesToWrite = _pos;
if (bytesToWrite) {
_pos = 0;
@@ -368,10 +369,6 @@ bool BufferedWriteStream::flushBuffer() {
return true;
}
-bool BufferedWriteStream::flush() {
- return flushBuffer();
-}
-
bool MemoryWriteStreamDynamic::seek(int32 offs, int whence) {
// Pre-Condition
assert(_pos <= _size);
diff --git a/common/stream.h b/common/stream.h
index c6605cb42d..d497cabe2b 100644
--- a/common/stream.h
+++ b/common/stream.h
@@ -534,15 +534,23 @@ protected:
DisposeAfterUse::Flag _disposeParentStream;
byte *_buf;
uint32 _pos;
- uint32 _bufSize;
- bool flushBuffer(); // write out the data in the buffer
+ 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();
+ virtual bool flush() { return flushBuffer(); }
};
/**