ScummVM API documentation
Handle.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
23#ifndef QDENGINE_QDCORE_UTIL_HANDLE_H
24#define QDENGINE_QDCORE_UTIL_HANDLE_H
25
26
27namespace QDEngine {
28
30// Автоматически удаляемый указатель
32template<class T>
33class PtrHandle {
34public:
35 PtrHandle(T *p = 0) : ptr(p) {}
36 PtrHandle(PtrHandle &p) : ptr(p.release()) {}
37 ~PtrHandle() {
38 delete ptr;
39 }
40
41 void set(T *p) {
42 ptr = p;
43 }
44
45 PtrHandle &operator=(PtrHandle &p) {
46 if (get() != p.get()) {
47 delete ptr;
48 ptr = p.release();
49 }
50 return *this;
51 }
52
53 PtrHandle &operator=(T *p) {
54 if (get() != p)
55 delete ptr;
56 set(p);
57 return *this;
58 }
59
60 T *get() const {
61 return ptr;
62 }
63 T *release() {
64 T *tmp = ptr;
65 ptr = 0;
66 return tmp;
67 }
68
69 T *operator->() const {
70 return ptr;
71 }
72 T &operator*() const {
73 return *ptr;
74 }
75 T *operator()() const {
76 return ptr;
77 }
78 operator T *() const {
79 return ptr;
80 }
81
82private:
83 T *ptr;
84};
85
87// Автоматически удаляемый указатель
88// с отслеживанием владельцев.
89// Обекты должны наследовать ShareHandleBase
91template<class T>
93public:
94 ShareHandle(T *p = 0) {
95 set(p);
96 }
97 ShareHandle(const ShareHandle &orig) {
98 set(orig.ptr);
99 }
100
101 ~ShareHandle() {
102 if (ptr && !ptr->decrRef())
103 delete ptr;
104 }
105
106 void set(T *p) {
107 ptr = p;
108 if (p)
109 p->addRef();
110 }
111
112 ShareHandle &operator=(const ShareHandle &orig) {
113 if (ptr && !ptr->decrRef() && ptr != orig.ptr)
114 delete ptr;
115 set(orig.ptr);
116 return *this;
117 }
118
119 ShareHandle &operator=(T *p) {
120 if (ptr && !ptr->decrRef() && ptr != p)
121 delete ptr;
122 set(p);
123 return *this;
124 }
125
126 T *get() const {
127 return ptr;
128 }
129 T *release() {
130 T *tmp = ptr;
131 if (ptr) ptr->decrRef();
132 ptr = 0;
133 return tmp;
134 }
135
136 T *operator->() const {
137 return ptr;
138 }
139 T &operator*() const {
140 return *ptr;
141 }
142 T *operator()() const {
143 return ptr;
144 }
145 operator T *() const {
146 return ptr;
147 }
148
149private:
150 T *ptr;
151};
152
154public:
156 handleCount = 0;
157 }
159 handleCount = 0;
160 }
162 void addRef() {
163 ++handleCount;
164 }
165 int decrRef() {
166 return --handleCount;
167 }
168 int numRef() const {
169 return handleCount;
170 }
171
172private:
173 mutable int handleCount;
174};
175
176} // namespace QDEngine
177
178#endif // QDENGINE_QDCORE_UTIL_HANDLE_H
Definition: Handle.h:33
Definition: Handle.h:153
Definition: Handle.h:92
Базовый класс для игровых ресурсов.
Definition: console.h:28