1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef SCI_GRAPHICS_LISTS32_H
#define SCI_GRAPHICS_LISTS32_H
#include "common/array.h"
namespace Sci {
/**
* StablePointerArray holds pointers in a fixed-size array
* that maintains position of erased items until `pack` is
* called. It is used by DrawList, RectList, and
* ScreenItemList. StablePointerArray takes ownership of
* all pointers that are passed to it and deletes them when
* calling `erase` or when destroying the
* StablePointerArray.
*/
template<class T, uint N>
class StablePointerArray {
uint _size;
T *_items[N];
public:
typedef T **iterator;
typedef T *const *const_iterator;
typedef T *value_type;
typedef uint size_type;
StablePointerArray() : _size(0), _items() {}
StablePointerArray(const StablePointerArray &other) : _size(other._size) {
for (size_type i = 0; i < _size; ++i) {
if (other._items[i] == nullptr) {
_items[i] = nullptr;
} else {
_items[i] = new T(*other._items[i]);
}
}
}
~StablePointerArray() {
for (size_type i = 0; i < _size; ++i) {
delete _items[i];
}
}
void operator=(const StablePointerArray &other) {
clear();
_size = other._size;
for (size_type i = 0; i < _size; ++i) {
if (other._items[i] == nullptr) {
_items[i] = nullptr;
} else {
_items[i] = new T(*other._items[i]);
}
}
}
T *const &operator[](size_type index) const {
assert(index < _size);
return _items[index];
}
T *&operator[](size_type index) {
assert(index < _size);
return _items[index];
}
/**
* Adds a new pointer to the array.
*/
void add(T *item) {
assert(_size < N);
_items[_size++] = item;
}
iterator begin() {
return _items;
}
const_iterator begin() const {
return _items;
}
void clear() {
for (size_type i = 0; i < _size; ++i) {
delete _items[i];
_items[i] = nullptr;
}
_size = 0;
}
iterator end() {
return _items + _size;
}
const_iterator end() const {
return _items + _size;
}
/**
* Erases the object pointed to by the given iterator.
*/
void erase(T *item) {
for (iterator it = begin(); it != end(); ++it) {
if (*it == item) {
delete *it;
*it = nullptr;
break;
}
}
}
/**
* Erases the object pointed to by the given iterator.
*/
void erase(iterator &it) {
assert(it >= _items && it < _items + _size);
delete *it;
*it = nullptr;
}
/**
* Erases the object pointed to at the given index.
*/
void erase_at(size_type index) {
assert(index < _size);
delete _items[index];
_items[index] = nullptr;
}
/**
* Removes freed pointers from the pointer list.
*/
size_type pack() {
iterator freePtr = begin();
size_type newSize = 0;
for (iterator it = begin(), last = end(); it != last; ++it) {
if (*it != nullptr) {
*freePtr = *it;
++freePtr;
++newSize;
}
}
_size = newSize;
return newSize;
}
/**
* The maximum number of elements the container is able to hold.
*/
size_type max_size() const {
return N;
}
/**
* The number of populated slots in the array. The size
* of the array will only go down once `pack` is called.
*/
size_type size() const {
return _size;
}
};
template<typename T>
class FindByObject {
const reg_t &_object;
public:
FindByObject(const reg_t &object) : _object(object) {}
bool operator()(const T entry) const {
return entry && entry->_object == _object;
}
};
} // End of namespace Sci
#endif
|