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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef GRIM_POOL_H
#define GRIM_POOL_H
#include "common/hashmap.h"
#include "common/list.h"
#include "engines/grim/savegame.h"
namespace Grim {
template<class T> class Pool;
class PoolObjectBase {
public:
virtual ~PoolObjectBase() { };
virtual int getId() const = 0;
virtual int32 getTag() const = 0;
};
template<class T>
class PoolObject : public PoolObjectBase {
public:
class Pool {
public:
template<class it, class Type>
class Iterator {
public:
Iterator(const Iterator &i) : _i(i._i) { }
Iterator(const it &i) : _i(i) { }
int32 getId() const { return _i->_key; }
Type &getValue() const { return _i->_value; }
Type &operator*() const { return _i->_value; }
Iterator &operator=(const Iterator &i) { _i = i._i; return *this; }
bool operator==(const Iterator i) const { return _i == i._i; }
bool operator!=(const Iterator i) const { return _i != i._i; }
Iterator &operator++() { ++_i; return *this; }
Iterator operator++(int) { Iterator iter = *this; ++_i; return iter; }
Iterator &operator--() { --_i; return *this; }
Iterator operator--(int) { Iterator iter = *this; --_i; return iter; }
private:
it _i;
};
typedef Iterator<typename Common::HashMap<int32, T *>::iterator, T *> iterator;
typedef Iterator<typename Common::HashMap<int32, T *>::const_iterator, T *const> const_iterator;
Pool();
~Pool();
void addObject(T *obj);
void removeObject(int32 id);
T *getObject(int32 id);
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
int getSize() const;
void deleteObjects();
void saveObjects(SaveGame *save);
void restoreObjects(SaveGame *save);
private:
bool _restoring;
Common::HashMap<int32, T*> _map;
};
/**
* @short Smart pointer class
* This class wraps a C pointer to T, subclass of PoolObject, which gets reset to NULL as soon as
* the object is deleted, e.g by Pool::restoreObjects().
* Its operator overloads allows the Ptr class to be used as if it was a raw C pointer.
*/
class Ptr {
public:
Ptr() : _obj(NULL) { }
Ptr(T *obj) : _obj(obj) {
if (_obj)
_obj->addPointer(this);
}
Ptr(const Ptr &ptr) : _obj(ptr._obj) {
if (_obj)
_obj->addPointer(this);
}
~Ptr() {
if (_obj)
_obj->removePointer(this);
}
Ptr &operator=(T *obj);
Ptr &operator=(const Ptr &ptr);
inline operator bool() const { return _obj; }
inline bool operator!() const { return !_obj; }
inline bool operator==(T *obj) const { return _obj == obj; }
inline bool operator!=(T *obj) const { return _obj != obj; }
inline T *operator->() const { return _obj; }
inline T &operator*() const { return *_obj; }
inline operator T*() const { return _obj; }
private:
inline void reset() { _obj = NULL; }
T *_obj;
friend class PoolObject;
};
virtual ~PoolObject();
void setId(int id);
int getId() const override;
int32 getTag() const override { return T::getStaticTag(); }
static Pool &getPool();
protected:
PoolObject();
static void saveStaticState(SaveGame *state) {}
static void restoreStaticState(SaveGame *state) {}
private:
void addPointer(Ptr *pointer) { _pointers.push_back(pointer); }
void removePointer(Ptr *pointer) { _pointers.remove(pointer); }
int _id;
static int s_id;
static Pool *s_pool;
Common::List<Ptr *> _pointers;
friend class Pool;
friend class Ptr;
};
template <class T>
bool operator==(T *obj, const typename PoolObject<T>::Ptr &ptr) {
return obj == ptr._obj;
}
template <class T>
bool operator!=(T *obj, const typename PoolObject<T>::Ptr &ptr) {
return obj != ptr._obj;
}
template <class T>
int PoolObject<T>::s_id = 0;
template <class T>
typename PoolObject<T>::Pool *PoolObject<T>::s_pool = NULL;
template <class T>
PoolObject<T>::PoolObject() {
++s_id;
_id = s_id;
if (!s_pool) {
s_pool = new Pool();
}
s_pool->addObject(static_cast<T *>(this));
}
template <class T>
PoolObject<T>::~PoolObject() {
s_pool->removeObject(_id);
for (typename Common::List<Ptr *>::iterator i = _pointers.begin(); i != _pointers.end(); ++i) {
(*i)->reset();
}
}
template <class T>
void PoolObject<T>::setId(int id) {
_id = id;
if (id > s_id) {
s_id = id;
}
}
template <class T>
int PoolObject<T>::getId() const {
return _id;
}
template <class T>
typename PoolObject<T>::Pool &PoolObject<T>::getPool() {
if (!s_pool) {
s_pool = new Pool();
}
return *s_pool;
}
/**
* @class Pool
*/
template <class T>
PoolObject<T>::Pool::Pool() :
_restoring(false) {
}
template <class T>
PoolObject<T>::Pool::~Pool() {
PoolObject<T>::s_pool = NULL;
}
template <class T>
void PoolObject<T>::Pool::addObject(T *obj) {
if (!_restoring) {
_map.setVal(obj->_id, obj);
}
}
template <class T>
void PoolObject<T>::Pool::removeObject(int32 id) {
_map.erase(id);
}
template <class T>
T *PoolObject<T>::Pool::getObject(int32 id) {
return _map.getValOrDefault(id, NULL);
}
template <class T>
typename PoolObject<T>::Pool::iterator PoolObject<T>::Pool::begin() {
return iterator(_map.begin());
}
template <class T>
typename PoolObject<T>::Pool::const_iterator PoolObject<T>::Pool::begin() const {
return const_iterator(_map.begin());
}
template <class T>
typename PoolObject<T>::Pool::iterator PoolObject<T>::Pool::end() {
return iterator(_map.end());
}
template <class T>
typename PoolObject<T>::Pool::const_iterator PoolObject<T>::Pool::end() const {
return const_iterator(_map.end());
}
template <class T>
int PoolObject<T>::Pool::getSize() const {
return _map.size();
}
template <class T>
void PoolObject<T>::Pool::deleteObjects() {
while (!_map.empty()) {
delete *begin();
}
delete this;
}
template <class T>
void PoolObject<T>::Pool::saveObjects(SaveGame *state) {
state->beginSection(T::getStaticTag());
T::saveStaticState(state);
state->writeLEUint32(_map.size());
for (iterator i = begin(); i != end(); ++i) {
T *a = *i;
state->writeLESint32(i.getId());
a->saveState(state);
}
state->endSection();
}
template <class T>
void PoolObject<T>::Pool::restoreObjects(SaveGame *state) {
state->beginSection(T::getStaticTag());
T::restoreStaticState(state);
int32 size = state->readLEUint32();
_restoring = true;
Common::HashMap<int32, T *> tempMap;
for (int32 i = 0; i < size; ++i) {
int32 id = state->readLESint32();
T *t = nullptr;
if (_map.tryGetVal(id, t))
_map.erase(id);
else {
t = new T();
t->setId(id);
}
tempMap[id] = t;
t->restoreState(state);
}
for (iterator i = begin(); i != end(); ++i) {
delete *i;
}
_map = tempMap;
_restoring = false;
state->endSection();
}
template<class T>
typename PoolObject<T>::Ptr &PoolObject<T>::Ptr::operator=(T *obj) {
if (_obj) {
_obj->removePointer(this);
}
_obj = obj;
if (obj) {
obj->addPointer(this);
}
return *this;
}
template<class T>
typename PoolObject<T>::Ptr &PoolObject<T>::Ptr::operator=(const Ptr &ptr) {
if (_obj) {
_obj->removePointer(this);
}
_obj = ptr._obj;
if (_obj) {
_obj->addPointer(this);
}
return *this;
}
}
#endif
|