diff options
author | Christoph Mallon | 2011-08-06 16:30:52 +0200 |
---|---|---|
committer | Christoph Mallon | 2011-08-07 15:19:08 +0200 |
commit | a5a8833c059f28c85e392a3cd22c361d38ef95ff (patch) | |
tree | 3b3b69326019444c2c375c66f39cb3013a003591 /common | |
parent | 2f23ff72c1d804d9d0e3ac09c46f52fd6b23a68c (diff) | |
download | scummvm-rg350-a5a8833c059f28c85e392a3cd22c361d38ef95ff.tar.gz scummvm-rg350-a5a8833c059f28c85e392a3cd22c361d38ef95ff.tar.bz2 scummvm-rg350-a5a8833c059f28c85e392a3cd22c361d38ef95ff.zip |
COMMON: Add DisposablePtr<T>, which replaces many repeated implementations of a dispose flag.
Diffstat (limited to 'common')
-rw-r--r-- | common/ptr.h | 36 | ||||
-rw-r--r-- | common/stream.cpp | 9 | ||||
-rw-r--r-- | common/substream.h | 11 |
3 files changed, 42 insertions, 14 deletions
diff --git a/common/ptr.h b/common/ptr.h index d06a25edb8..2b0670caae 100644 --- a/common/ptr.h +++ b/common/ptr.h @@ -24,6 +24,7 @@ #include "common/scummsys.h" #include "common/noncopyable.h" +#include "common/types.h" namespace Common { @@ -273,6 +274,41 @@ private: PointerType _pointer; }; + +template<typename T> +class DisposablePtr : NonCopyable { +public: + typedef T ValueType; + typedef T *PointerType; + typedef T &ReferenceType; + + explicit DisposablePtr(PointerType o, DisposeAfterUse::Flag dispose) : _pointer(o), _dispose(dispose) {} + + ~DisposablePtr() { + if (_dispose) delete _pointer; + } + + ReferenceType operator*() const { return *_pointer; } + PointerType operator->() const { return _pointer; } + + /** + * Implicit conversion operator to bool for convenience, to make + * checks like "if (scopedPtr) ..." possible. + */ + operator bool() const { return _pointer; } + + /** + * Returns the plain pointer value. + * + * @return the pointer the DisposablePtr manages + */ + PointerType get() const { return _pointer; } + +private: + PointerType _pointer; + DisposeAfterUse::Flag _dispose; +}; + } // End of namespace Common #endif diff --git a/common/stream.cpp b/common/stream.cpp index 60b40d0df2..30b3bca497 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -20,6 +20,7 @@ * */ +#include "common/ptr.h" #include "common/stream.h" #include "common/memstream.h" #include "common/substream.h" @@ -258,8 +259,7 @@ namespace { */ class BufferedReadStream : virtual public ReadStream { protected: - ReadStream *_parentStream; - DisposeAfterUse::Flag _disposeParentStream; + DisposablePtr<ReadStream> _parentStream; byte *_buf; uint32 _pos; bool _eos; // end of stream @@ -278,8 +278,7 @@ public: }; BufferedReadStream::BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) - : _parentStream(parentStream), - _disposeParentStream(disposeParentStream), + : _parentStream(parentStream, disposeParentStream), _pos(0), _eos(false), _bufSize(0), @@ -291,8 +290,6 @@ BufferedReadStream::BufferedReadStream(ReadStream *parentStream, uint32 bufSize, } BufferedReadStream::~BufferedReadStream() { - if (_disposeParentStream) - delete _parentStream; delete[] _buf; } diff --git a/common/substream.h b/common/substream.h index f4f79ff02f..7e67389da1 100644 --- a/common/substream.h +++ b/common/substream.h @@ -23,6 +23,7 @@ #ifndef COMMON_SUBSTREAM_H #define COMMON_SUBSTREAM_H +#include "common/ptr.h" #include "common/stream.h" #include "common/types.h" @@ -38,24 +39,18 @@ namespace Common { */ class SubReadStream : virtual public ReadStream { protected: - ReadStream *_parentStream; - DisposeAfterUse::Flag _disposeParentStream; + DisposablePtr<ReadStream> _parentStream; uint32 _pos; uint32 _end; bool _eos; public: SubReadStream(ReadStream *parentStream, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO) - : _parentStream(parentStream), - _disposeParentStream(disposeParentStream), + : _parentStream(parentStream, disposeParentStream), _pos(0), _end(end), _eos(false) { assert(parentStream); } - ~SubReadStream() { - if (_disposeParentStream) - delete _parentStream; - } virtual bool eos() const { return _eos | _parentStream->eos(); } virtual bool err() const { return _parentStream->err(); } |