ScummVM API documentation
util.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 SCI_UTIL_H
23#define SCI_UTIL_H
24
25#include "common/span.h"
26#include "common/endian.h"
27#include "common/file.h"
28#include "common/memstream.h"
29#include "common/scummsys.h"
30#include "common/str.h"
31#include "common/textconsole.h"
32
33namespace Sci {
34
35// Wrappers for reading/writing 16-bit values in the endianness
36// of the original game platform.
37uint16 READ_SCIENDIAN_UINT16(const void *ptr);
38void WRITE_SCIENDIAN_UINT16(void *ptr, uint16 val);
39
40// Wrappers for reading integer values for SCI1.1+.
41// Mac versions have big endian data for some fields.
42uint16 READ_SCI11ENDIAN_UINT16(const void *ptr);
43uint32 READ_SCI11ENDIAN_UINT32(const void *ptr);
44void WRITE_SCI11ENDIAN_UINT16(void *ptr, uint16 val);
45void WRITE_SCI11ENDIAN_UINT32(void *ptr, uint32 val);
46// Wrappers for reading integer values in resources that are
47// LE in SCI1.1 Mac, but BE in SCI32 Mac
48uint16 READ_SCI32ENDIAN_UINT16(const void *ptr);
49
50#pragma mark -
51#pragma mark SciSpanImpl - SciSpanIterator
52
53namespace SciSpanInternal {
54 template <typename Span, bool IsConst>
57 typedef typename Span::value_type span_value_type;
59
60 public:
61 typedef typename Span::difference_type difference_type;
62 typedef typename Common::remove_const<span_value_type>::type value_type;
65
66 inline SciSpanIterator() : super_type() {}
67
68 inline SciSpanIterator(span_type *span, const difference_type index) : super_type(span, index) {}
69
70 inline SciSpanIterator(const SciSpanIterator &other) : super_type(other) {}
71
72 inline SciSpanIterator &operator=(const SciSpanIterator &other) {
73 super_type::operator=(other);
74 return *this;
75 }
76
77 inline SciSpanIterator operator+(const difference_type delta) const {
78 SciSpanIterator it(*this);
79 it += delta;
80 return it;
81 }
82
83 inline SciSpanIterator operator-(const difference_type delta) const {
84 return operator+(-delta);
85 }
86
87 inline difference_type operator-(const SciSpanIterator &other) const {
88 assert(this->_span == other._span);
89 return this->_index - other._index;
90 }
91
92 inline int16 getInt16SE() const {
93 return this->_span->getInt16SEAt(this->_index);
94 }
95
96 inline uint16 getUint16SE() const {
97 return this->_span->getUint16SEAt(this->_index);
98 }
99
100 inline uint16 getUint16SE32() const {
101 return this->_span->getUint16SE32At(this->_index);
102 }
103
104 inline int32 getInt32SE() const {
105 return this->_span->getInt32SEAt(this->_index);
106 }
107
108 inline uint32 getUint32SE() const {
109 return this->_span->getUint32SEAt(this->_index);
110 }
111
112 inline void setUint16SE(uint16 value) const {
113 this->_span->setUint16SEAt(this->_index, value);
114 }
115
116#ifdef ENABLE_SCI32
117 inline void setUint32SE(uint32 value) const {
118 this->_span->setUint32SEAt(this->_index, value);
119 }
120#endif
121 };
122} // End of namespace SciSpanInternal
123
129template <typename ValueType, template <typename> class Derived>
130class SciSpanImpl : public Common::NamedSpanImpl<ValueType, Derived> {
132 typedef Derived<ValueType> derived_type;
133
134 template <typename T, template <typename> class U> friend class SciSpanImpl;
135#if defined(CXXTEST_RUNNING) && CXXTEST_RUNNING
136 friend class ::SpanTestSuite;
137#endif
138
139public:
140 typedef typename super_type::value_type value_type;
141 typedef typename super_type::difference_type difference_type;
142 typedef typename super_type::index_type index_type;
143 typedef typename super_type::size_type size_type;
146 typedef typename super_type::pointer pointer;
147 typedef typename super_type::const_pointer const_pointer;
148 typedef typename super_type::reference reference;
149 typedef typename super_type::const_reference const_reference;
150
151 inline SciSpanImpl() : super_type() {}
152
153 inline SciSpanImpl(const pointer data_,
154 const size_type size_,
155 const Common::String &name_ = Common::String(),
156 const size_type sourceByteOffset_ = 0) :
157 super_type(data_, size_, name_, sourceByteOffset_) {}
158
159 template <typename Other>
160 inline SciSpanImpl(const Other &other) : super_type(other) {}
161
162 inline const_iterator cbegin() const { return const_iterator(&this->impl(), 0); }
163 inline const_iterator cend() const { return const_iterator(&this->impl(), this->size()); }
164 inline const_iterator begin() const { return const_iterator(&this->impl(), 0); }
165 inline const_iterator end() const { return const_iterator(&this->impl(), this->size()); }
166 inline iterator begin() { return iterator(&this->impl(), 0); }
167 inline iterator end() { return iterator(&this->impl(), this->size()); }
168
169#pragma mark -
170#pragma mark SciSpan - Data access
171
172public:
173 inline int16 getInt16SEAt(const size_type index) const {
174 return (int16)getUint16SEAt(index);
175 }
176
177 inline uint16 getUint16SEAt(const size_type index) const {
178 this->validate(index, sizeof(uint16));
179 return READ_SCI11ENDIAN_UINT16(this->data() + index);
180 }
181
182 inline uint16 getUint16SE32At(const size_type index) const {
183 this->validate(index, sizeof(uint16));
184 return READ_SCI32ENDIAN_UINT16(this->data() + index);
185 }
186
187 inline int32 getInt32SEAt(const size_type index) const {
188 return (int32)getUint32SEAt(index);
189 }
190
191 inline uint32 getUint32SEAt(const size_type index) const {
192 this->validate(index, sizeof(uint32));
193 return READ_SCI11ENDIAN_UINT32(this->data() + index);
194 }
195
196 inline void setUint16SEAt(const size_type index, uint16 value) {
197 this->validate(index, sizeof(uint16), Common::kValidateWrite);
198 WRITE_SCI11ENDIAN_UINT16(this->data() + index, value);
199 }
200
201 inline void setUint32SEAt(const size_type index, uint32 value) {
202 this->validate(index, sizeof(uint32), Common::kValidateWrite);
203 WRITE_SCI11ENDIAN_UINT32(this->data() + index, value);
204 }
205
206#pragma mark -
207#pragma mark SciSpanImpl - ForwardIterator
208
209// Spans that are used as ForwardIterators must not be allowed inside of
210// SpanOwner, since this will result in the wrong pointer to memory to be
211// deleted
212private:
213 typedef typename Common::remove_const<Derived<ValueType> >::type mutable_derived_type;
214
215public:
216 inline const_reference operator*() const {
217 this->validate(0, sizeof(value_type));
218 return *this->_data;
219 }
220
221 inline reference operator*() {
222 this->validate(0, sizeof(value_type));
223 return *this->_data;
224 }
225
226 inline mutable_derived_type &operator+=(const difference_type delta) {
227 this->validate(0, delta * sizeof(value_type), Common::kValidateSeek);
228 this->_data += delta;
229 this->_size -= delta;
230 return this->impl();
231 }
232
233 inline mutable_derived_type &operator++() {
234 return operator+=(1);
235 }
236
237 inline mutable_derived_type operator++(int) {
238 mutable_derived_type span(this->impl());
239 operator+=(1);
240 return span;
241 }
242
243 inline mutable_derived_type operator+(const difference_type delta) const {
244 mutable_derived_type span(this->impl());
245 span += delta;
246 return span;
247 }
248};
249
250template <typename ValueType>
251class SciSpan : public SciSpanImpl<ValueType, SciSpan> {
253
254public:
255 COMMON_SPAN_TYPEDEFS;
256
257 inline SciSpan() = default;
258
259 inline SciSpan(const pointer data_,
260 const size_type size_,
261 const Common::String &name_ = Common::String(),
262 const size_type sourceByteOffset_ = 0) :
263 super_type(data_, size_, name_, sourceByteOffset_) {}
264
265 template <typename Other>
266 inline SciSpan(const Other &other) : super_type(other) {}
267};
268
269} // End of namespace Sci
270
271#endif
Definition: span.h:714
Definition: span.h:254
Definition: span.h:691
Definition: str.h:59
Definition: util.h:251
Definition: util.h:130
Definition: console.h:28
Definition: type_traits.h:44