ScummVM API documentation
substream.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_SUBSTREAM_H
23 #define COMMON_SUBSTREAM_H
24 
25 #include "common/mutex.h"
26 #include "common/ptr.h"
27 #include "common/stream.h"
28 #include "common/types.h"
29 
30 namespace Common {
31 
49 class SubReadStream : virtual public ReadStream {
50 protected:
51  DisposablePtr<ReadStream> _parentStream;
52  uint32 _pos;
53  uint32 _end;
54  bool _eos;
55 public:
56  SubReadStream(ReadStream *parentStream, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
57  : _parentStream(parentStream, disposeParentStream),
58  _pos(0),
59  _end(end),
60  _eos(false) {
61  assert(parentStream);
62  }
63 
64  virtual bool eos() const { return _eos || _parentStream->eos(); }
65  virtual bool err() const { return _parentStream->err(); }
66  virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
67  virtual uint32 read(void *dataPtr, uint32 dataSize);
68 };
69 
70 /*
71  * SeekableSubReadStream provides access to a SeekableReadStream restricted to
72  * the range [begin, end).
73  * The same caveats apply to SeekableSubReadStream as do to SeekableReadStream.
74  *
75  * Manipulating the parent stream directly /will/ mess up a substream.
76  * @see SubReadStream
77  */
78 class SeekableSubReadStream : public SubReadStream, virtual public SeekableReadStream {
79 protected:
80  SeekableReadStream *_parentStream;
81  uint32 _begin;
82 public:
83  SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
84 
85  virtual int64 pos() const { return _pos - _begin; }
86  virtual int64 size() const { return _end - _begin; }
87 
88  virtual bool seek(int64 offset, int whence = SEEK_SET);
89 };
90 
105 public:
106  SafeSeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
107  : SeekableSubReadStream(parentStream, begin, end, disposeParentStream) {
108  }
109 
110  virtual uint32 read(void *dataPtr, uint32 dataSize);
111 };
112 
121 public:
122  SafeMutexedSeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream,
123  Common::Mutex &mutex)
124  : SafeSeekableSubReadStream(parentStream, begin, end, disposeParentStream), _mutex(mutex) {
125  }
126  uint32 read(void *dataPtr, uint32 dataSize) override;
127 protected:
128  Common::Mutex &_mutex;
129 };
130 
133 } // End of namespace Common
134 
135 #endif
Definition: substream.h:120
Definition: substream.h:104
virtual int64 pos() const
Definition: substream.h:85
Definition: stream.h:745
virtual bool eos() const
Definition: substream.h:64
Definition: substream.h:78
Definition: algorithm.h:29
Definition: mutex.h:67
Definition: stream.h:385
virtual int64 size() const
Definition: substream.h:86
Definition: ptr.h:655
virtual uint32 read(void *dataPtr, uint32 dataSize)
Definition: substream.h:49
virtual void clearErr()
Definition: substream.h:66
virtual bool err() const
Definition: substream.h:65