ScummVM API documentation
object.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_OBJECT_H
23#define GRIM_OBJECT_H
24
25#include "common/list.h"
26
27namespace Grim {
28
29class SaveGame;
30
31class Pointer;
32class Object {
33public:
34 Object();
35 virtual ~Object();
36
37 void reset() { };
38 void reference();
39 void dereference();
40
41 int32 getId() const;
42
43private:
44 void setId(int32 id);
45 int _refCount;
47 int32 _id;
48 static int32 s_id;
49
50 friend class Pointer;
51};
52
53class Pointer {
54protected:
55 virtual ~Pointer() {}
56
57 void addPointer(Object *obj) {
58 obj->_pointers.push_back(this);
59 }
60 void rmPointer(Object *obj) {
61 obj->_pointers.remove(this);
62 }
63
64 virtual void resetPointer() {}
65
66 friend class Object;
67};
68
69template<class T> class ObjectPtr : public Pointer {
70public:
71 ObjectPtr() :
72 _obj(nullptr) {
73
74 }
75 ObjectPtr(T *obj) :
76 _obj(obj) {
77 if (obj) {
78 Object *o = (Object *)_obj;
79 o->reference();
80 addPointer(o);
81 }
82 }
83 ObjectPtr(const ObjectPtr<T> &ptr) : Pointer() {
84 _obj = nullptr;
85 *this = ptr;
86 }
87 ~ObjectPtr() {
88 if (_obj) {
89 Object *o = (Object *)_obj;
90 rmPointer(o);
91 o->dereference();
92 }
93 }
94
95 ObjectPtr &operator=(T *obj) {
96 if (obj != _obj) {
97 if (_obj) {
98 rmPointer(_obj);
99 _obj->dereference();
100 _obj = nullptr;
101
102 }
103
104 if (obj) {
105 _obj = obj;
106 _obj->reference();
107 addPointer(obj);
108 }
109 }
110
111 return *this;
112 }
113 ObjectPtr &operator=(const ObjectPtr<T> &ptr) {
114 if (_obj != ptr._obj) {
115 if (_obj) {
116 rmPointer(_obj);
117 _obj->dereference();
118 _obj = nullptr;
119
120 }
121
122 if (ptr._obj) {
123 _obj = ptr._obj;
124 _obj->reference();
125 addPointer(_obj);
126 }
127 }
128
129 return *this;
130 }
131 bool operator==(const ObjectPtr &ptr) const {
132 return (_obj == ptr._obj);
133 }
134 bool operator==(Object *obj) const {
135 return (_obj == obj);
136 }
137 operator bool() const {
138 return (_obj);
139 }
140 bool operator!() const {
141 return (!_obj);
142 }
143
144 T *object() const {
145 return _obj;
146 }
147 T *operator->() const {
148 return _obj;
149 }
150 T &operator*() const {
151 return *_obj;
152 }
153 operator T*() const {
154 return _obj;
155 }
156
157protected:
158 void resetPointer() override {
159 _obj = nullptr;
160 }
161
162private:
163 T *_obj;
164};
165
166}
167
168#endif
Definition: list.h:44
Definition: object.h:32
Definition: object.h:69
Definition: object.h:53