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
30namespace Common {
31
49class SubReadStream : virtual public ReadStream {
50protected:
51 DisposablePtr<ReadStream> _parentStream;
52 uint32 _pos;
53 uint32 _end;
54 bool _eos;
55public:
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 bool eos() const override { return _eos || _parentStream->eos(); }
65 bool err() const override { return _parentStream->err(); }
66 void clearErr() override { _eos = false; _parentStream->clearErr(); }
67 uint32 read(void *dataPtr, uint32 dataSize) override;
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 */
79protected:
80 SeekableReadStream *_parentStream;
81 uint32 _begin;
82public:
83 SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
84
85 int64 pos() const override { return _pos - _begin; }
86 int64 size() const override { return _end - _begin; }
87
88 bool seek(int64 offset, int whence = SEEK_SET) override;
89};
90
105public:
106 SafeSeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
107 : SeekableSubReadStream(parentStream, begin, end, disposeParentStream) {
108 }
109
110 uint32 read(void *dataPtr, uint32 dataSize) override;
111};
112
121public:
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;
127protected:
128 Common::Mutex &_mutex;
129};
130
133} // End of namespace Common
134
135#endif
Definition: ptr.h:656
Definition: mutex.h:67
Definition: stream.h:385
Definition: substream.h:120
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: substream.h:104
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: stream.h:745
Definition: substream.h:78
int64 pos() const override
Definition: substream.h:85
int64 size() const override
Definition: substream.h:86
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: substream.h:49
bool eos() const override
Definition: substream.h:64
void clearErr() override
Definition: substream.h:66
bool err() const override
Definition: substream.h:65
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: algorithm.h:29