25 #include "common/hashmap.h" 26 #include "common/list.h" 28 #include "engines/grim/savegame.h" 32 template<
class T>
class Pool;
37 virtual int getId()
const = 0;
38 virtual int32 getTag()
const = 0;
46 template<
class it,
class Type>
52 int32 getId()
const {
return _i->_key; }
53 Type &getValue()
const {
return _i->_value; }
55 Type &operator*()
const {
return _i->_value; }
59 bool operator==(
const Iterator i)
const {
return _i == i._i; }
60 bool operator!=(
const Iterator i)
const {
return _i != i._i; }
62 Iterator &operator++() { ++_i;
return *
this; }
65 Iterator &operator--() { --_i;
return *
this; }
78 void addObject(T *obj);
79 void removeObject(int32
id);
81 T *getObject(int32
id);
83 const_iterator begin()
const;
85 const_iterator end()
const;
104 Ptr() : _obj(NULL) { }
105 Ptr(T *obj) : _obj(obj) {
107 _obj->addPointer(
this);
109 Ptr(
const Ptr &ptr) : _obj(ptr._obj) {
111 _obj->addPointer(
this);
115 _obj->removePointer(
this);
118 Ptr &operator=(T *obj);
119 Ptr &operator=(
const Ptr &ptr);
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; }
126 inline T *operator->()
const {
return _obj; }
127 inline T &operator*()
const {
return *_obj; }
128 inline operator T*()
const {
return _obj; }
131 inline void reset() { _obj = NULL; }
142 int getId()
const override;
143 int32 getTag()
const override {
return T::getStaticTag(); }
145 static Pool &getPool();
150 static void saveStaticState(
SaveGame *state) {}
151 static void restoreStaticState(
SaveGame *state) {}
154 void addPointer(
Ptr *pointer) { _pointers.push_back(pointer); }
155 void removePointer(
Ptr *pointer) { _pointers.remove(pointer); }
169 return obj == ptr._obj;
174 return obj != ptr._obj;
191 s_pool->addObject(static_cast<T *>(
this));
196 s_pool->removeObject(_id);
235 PoolObject<T>::s_pool = NULL;
241 _map.setVal(obj->_id, obj);
252 return _map.getValOrDefault(
id, NULL);
257 return iterator(_map.begin());
262 return const_iterator(_map.begin());
267 return iterator(_map.end());
272 return const_iterator(_map.end());
282 while (!_map.empty()) {
290 state->beginSection(T::getStaticTag());
292 T::saveStaticState(state);
294 state->writeLEUint32(_map.size());
295 for (iterator i = begin(); i != end(); ++i) {
297 state->writeLESint32(i.getId());
307 state->beginSection(T::getStaticTag());
309 T::restoreStaticState(state);
311 int32 size = state->readLEUint32();
314 for (int32 i = 0; i < size; ++i) {
315 int32
id = state->readLESint32();
317 if (_map.tryGetVal(
id, t))
324 t->restoreState(state);
326 for (iterator i = begin(); i != end(); ++i) {
338 _obj->removePointer(
this);
342 obj->addPointer(
this);
351 _obj->removePointer(
this);
355 _obj->addPointer(
this);
Smart pointer class This class wraps a C pointer to T, subclass of PoolObject, which gets reset to NU...
Definition: pool.h:102
Definition: savegame.h:33
Definition: list_intern.h:51