ScummVM API documentation
pool.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 GRIM_POOL_H
23 #define GRIM_POOL_H
24 
25 #include "common/hashmap.h"
26 #include "common/list.h"
27 
28 #include "engines/grim/savegame.h"
29 
30 namespace Grim {
31 
32 template<class T> class Pool;
33 
35 public:
36  virtual ~PoolObjectBase() { };
37  virtual int getId() const = 0;
38  virtual int32 getTag() const = 0;
39 };
40 
41 template<class T>
42 class PoolObject : public PoolObjectBase {
43 public:
44  class Pool {
45  public:
46  template<class it, class Type>
47  class Iterator {
48  public:
49  Iterator(const Iterator &i) : _i(i._i) { }
50  Iterator(const it &i) : _i(i) { }
51 
52  int32 getId() const { return _i->_key; }
53  Type &getValue() const { return _i->_value; }
54 
55  Type &operator*() const { return _i->_value; }
56 
57  Iterator &operator=(const Iterator &i) { _i = i._i; return *this; }
58 
59  bool operator==(const Iterator i) const { return _i == i._i; }
60  bool operator!=(const Iterator i) const { return _i != i._i; }
61 
62  Iterator &operator++() { ++_i; return *this; }
63  Iterator operator++(int) { Iterator iter = *this; ++_i; return iter; }
64 
65  Iterator &operator--() { --_i; return *this; }
66  Iterator operator--(int) { Iterator iter = *this; --_i; return iter; }
67 
68  private:
69  it _i;
70  };
71 
74 
75  Pool();
76  ~Pool();
77 
78  void addObject(T *obj);
79  void removeObject(int32 id);
80 
81  T *getObject(int32 id);
82  iterator begin();
83  const_iterator begin() const;
84  iterator end();
85  const_iterator end() const;
86  int getSize() const;
87  void deleteObjects();
88  void saveObjects(SaveGame *save);
89  void restoreObjects(SaveGame *save);
90 
91  private:
92  bool _restoring;
94  };
95 
102  class Ptr {
103  public:
104  Ptr() : _obj(NULL) { }
105  Ptr(T *obj) : _obj(obj) {
106  if (_obj)
107  _obj->addPointer(this);
108  }
109  Ptr(const Ptr &ptr) : _obj(ptr._obj) {
110  if (_obj)
111  _obj->addPointer(this);
112  }
113  ~Ptr() {
114  if (_obj)
115  _obj->removePointer(this);
116  }
117 
118  Ptr &operator=(T *obj);
119  Ptr &operator=(const Ptr &ptr);
120 
121  inline operator bool() const { return _obj; }
122  inline bool operator!() const { return !_obj; }
123  inline bool operator==(T *obj) const { return _obj == obj; }
124  inline bool operator!=(T *obj) const { return _obj != obj; }
125 
126  inline T *operator->() const { return _obj; }
127  inline T &operator*() const { return *_obj; }
128  inline operator T*() const { return _obj; }
129 
130  private:
131  inline void reset() { _obj = NULL; }
132 
133  T *_obj;
134 
135  friend class PoolObject;
136  };
137 
138  virtual ~PoolObject();
139 
140  void setId(int id);
141 
142  int getId() const override;
143  int32 getTag() const override { return T::getStaticTag(); }
144 
145  static Pool &getPool();
146 
147 protected:
148  PoolObject();
149 
150  static void saveStaticState(SaveGame *state) {}
151  static void restoreStaticState(SaveGame *state) {}
152 
153 private:
154  void addPointer(Ptr *pointer) { _pointers.push_back(pointer); }
155  void removePointer(Ptr *pointer) { _pointers.remove(pointer); }
156 
157  int _id;
158  static int s_id;
159  static Pool *s_pool;
160 
161  Common::List<Ptr *> _pointers;
162 
163  friend class Pool;
164  friend class Ptr;
165 };
166 
167 template <class T>
168 bool operator==(T *obj, const typename PoolObject<T>::Ptr &ptr) {
169  return obj == ptr._obj;
170 }
171 
172 template <class T>
173 bool operator!=(T *obj, const typename PoolObject<T>::Ptr &ptr) {
174  return obj != ptr._obj;
175 }
176 
177 template <class T>
178 int PoolObject<T>::s_id = 0;
179 
180 template <class T>
182 
183 template <class T>
185  ++s_id;
186  _id = s_id;
187 
188  if (!s_pool) {
189  s_pool = new Pool();
190  }
191  s_pool->addObject(static_cast<T *>(this));
192 }
193 
194 template <class T>
196  s_pool->removeObject(_id);
197 
198  for (typename Common::List<Ptr *>::iterator i = _pointers.begin(); i != _pointers.end(); ++i) {
199  (*i)->reset();
200  }
201 }
202 
203 template <class T>
204 void PoolObject<T>::setId(int id) {
205  _id = id;
206  if (id > s_id) {
207  s_id = id;
208  }
209 }
210 
211 template <class T>
212 int PoolObject<T>::getId() const {
213  return _id;
214 }
215 
216 template <class T>
218  if (!s_pool) {
219  s_pool = new Pool();
220  }
221  return *s_pool;
222 }
223 
228 template <class T>
230  _restoring(false) {
231 }
232 
233 template <class T>
235  PoolObject<T>::s_pool = NULL;
236 }
237 
238 template <class T>
239 void PoolObject<T>::Pool::addObject(T *obj) {
240  if (!_restoring) {
241  _map.setVal(obj->_id, obj);
242  }
243 }
244 
245 template <class T>
246 void PoolObject<T>::Pool::removeObject(int32 id) {
247  _map.erase(id);
248 }
249 
250 template <class T>
251 T *PoolObject<T>::Pool::getObject(int32 id) {
252  return _map.getValOrDefault(id, NULL);
253 }
254 
255 template <class T>
257  return iterator(_map.begin());
258 }
259 
260 template <class T>
262  return const_iterator(_map.begin());
263 }
264 
265 template <class T>
267  return iterator(_map.end());
268 }
269 
270 template <class T>
272  return const_iterator(_map.end());
273 }
274 
275 template <class T>
276 int PoolObject<T>::Pool::getSize() const {
277  return _map.size();
278 }
279 
280 template <class T>
282  while (!_map.empty()) {
283  delete *begin();
284  }
285  delete this;
286 }
287 
288 template <class T>
290  state->beginSection(T::getStaticTag());
291 
292  T::saveStaticState(state);
293 
294  state->writeLEUint32(_map.size());
295  for (iterator i = begin(); i != end(); ++i) {
296  T *a = *i;
297  state->writeLESint32(i.getId());
298 
299  a->saveState(state);
300  }
301 
302  state->endSection();
303 }
304 
305 template <class T>
307  state->beginSection(T::getStaticTag());
308 
309  T::restoreStaticState(state);
310 
311  int32 size = state->readLEUint32();
312  _restoring = true;
314  for (int32 i = 0; i < size; ++i) {
315  int32 id = state->readLESint32();
316  T *t = nullptr;
317  if (_map.tryGetVal(id, t))
318  _map.erase(id);
319  else {
320  t = new T();
321  t->setId(id);
322  }
323  tempMap[id] = t;
324  t->restoreState(state);
325  }
326  for (iterator i = begin(); i != end(); ++i) {
327  delete *i;
328  }
329  _map = tempMap;
330  _restoring = false;
331 
332  state->endSection();
333 }
334 
335 template<class T>
337  if (_obj) {
338  _obj->removePointer(this);
339  }
340  _obj = obj;
341  if (obj) {
342  obj->addPointer(this);
343  }
344 
345  return *this;
346 }
347 
348 template<class T>
350  if (_obj) {
351  _obj->removePointer(this);
352  }
353  _obj = ptr._obj;
354  if (_obj) {
355  _obj->addPointer(this);
356  }
357 
358  return *this;
359 }
360 
361 }
362 
363 #endif
Definition: pool.h:32
Smart pointer class This class wraps a C pointer to T, subclass of PoolObject, which gets reset to NU...
Definition: pool.h:102
Definition: list.h:44
Definition: savegame.h:33
Definition: pool.h:44
Definition: actor.h:33
Type
Definition: log.h:33
Definition: hashmap.h:85
Definition: pool.h:42
Definition: list_intern.h:51
Definition: pool.h:34