1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | // Inspired by boost::intrusive_ptr by Peter Dimov |
---|
30 | |
---|
31 | #ifndef _WeakPtr_H__ |
---|
32 | #define _WeakPtr_H__ |
---|
33 | |
---|
34 | #include "CorePrereqs.h" |
---|
35 | |
---|
36 | #include <cassert> |
---|
37 | #include "OrxonoxClass.h" |
---|
38 | #include "Functor.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | template <class T> |
---|
43 | class WeakPtr |
---|
44 | { |
---|
45 | friend class OrxonoxClass; |
---|
46 | |
---|
47 | public: |
---|
48 | inline WeakPtr() : pointer_(0), base_(0), callback_(0) |
---|
49 | { |
---|
50 | } |
---|
51 | |
---|
52 | inline WeakPtr(int) : pointer_(0), base_(0), callback_(0) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0) |
---|
57 | { |
---|
58 | if (this->base_) |
---|
59 | this->base_->registerWeakPtr(this); |
---|
60 | } |
---|
61 | |
---|
62 | inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0) |
---|
63 | { |
---|
64 | if (this->base_) |
---|
65 | this->base_->registerWeakPtr(this); |
---|
66 | } |
---|
67 | |
---|
68 | template <class O> |
---|
69 | inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0) |
---|
70 | { |
---|
71 | if (this->base_) |
---|
72 | this->base_->registerWeakPtr(this); |
---|
73 | } |
---|
74 | |
---|
75 | inline ~WeakPtr() |
---|
76 | { |
---|
77 | if (this->base_) |
---|
78 | this->base_->unregisterWeakPtr(this); |
---|
79 | if (this->callback_) |
---|
80 | delete this->callback_; |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | inline const WeakPtr& operator=(int) |
---|
85 | { |
---|
86 | WeakPtr(0).swap(*this); |
---|
87 | return *this; |
---|
88 | } |
---|
89 | |
---|
90 | inline const WeakPtr& operator=(T* pointer) |
---|
91 | { |
---|
92 | WeakPtr(pointer).swap(*this); |
---|
93 | return *this; |
---|
94 | } |
---|
95 | |
---|
96 | inline const WeakPtr& operator=(const WeakPtr& other) |
---|
97 | { |
---|
98 | WeakPtr(other).swap(*this); |
---|
99 | return *this; |
---|
100 | } |
---|
101 | |
---|
102 | template <class O> |
---|
103 | inline const WeakPtr& operator=(const WeakPtr<O>& other) |
---|
104 | { |
---|
105 | WeakPtr(other).swap(*this); |
---|
106 | return *this; |
---|
107 | } |
---|
108 | |
---|
109 | inline T* get() const |
---|
110 | { |
---|
111 | return this->pointer_; |
---|
112 | } |
---|
113 | |
---|
114 | inline OrxonoxClass* getBase() const |
---|
115 | { |
---|
116 | return this->base_; |
---|
117 | } |
---|
118 | |
---|
119 | inline operator T*() const |
---|
120 | { |
---|
121 | return this->pointer_; |
---|
122 | } |
---|
123 | |
---|
124 | inline T* operator->() const |
---|
125 | { |
---|
126 | assert(this->pointer_ != 0); |
---|
127 | return this->pointer_; |
---|
128 | } |
---|
129 | |
---|
130 | inline T& operator*() const |
---|
131 | { |
---|
132 | assert(this->pointer_ != 0); |
---|
133 | return *this->pointer_; |
---|
134 | } |
---|
135 | |
---|
136 | inline bool operator!() const |
---|
137 | { |
---|
138 | return (this->pointer_ == 0); |
---|
139 | } |
---|
140 | |
---|
141 | inline void swap(WeakPtr& other) |
---|
142 | { |
---|
143 | if (this->base_) |
---|
144 | this->base_->unregisterWeakPtr(this); |
---|
145 | if (other.base_) |
---|
146 | other.base_->unregisterWeakPtr(&other); |
---|
147 | |
---|
148 | { |
---|
149 | T* temp = this->pointer_; |
---|
150 | this->pointer_ = other.pointer_; |
---|
151 | other.pointer_ = temp; |
---|
152 | } |
---|
153 | { |
---|
154 | OrxonoxClass* temp = this->base_; |
---|
155 | this->base_ = other.base_; |
---|
156 | other.base_ = temp; |
---|
157 | } |
---|
158 | |
---|
159 | if (this->base_) |
---|
160 | this->base_->registerWeakPtr(this); |
---|
161 | if (other.base_) |
---|
162 | other.base_->registerWeakPtr(&other); |
---|
163 | } |
---|
164 | |
---|
165 | inline void reset() |
---|
166 | { |
---|
167 | WeakPtr().swap(*this); |
---|
168 | } |
---|
169 | |
---|
170 | inline void addCallback(Functor* callback) |
---|
171 | { |
---|
172 | this->callback_ = callback; |
---|
173 | } |
---|
174 | |
---|
175 | inline Functor* getFunctor() const |
---|
176 | { |
---|
177 | return this->callback_; |
---|
178 | } |
---|
179 | |
---|
180 | private: |
---|
181 | inline void objectDeleted() |
---|
182 | { |
---|
183 | this->base_ = 0; |
---|
184 | this->pointer_ = 0; |
---|
185 | if (this->callback_) |
---|
186 | (*this->callback_)(); |
---|
187 | } |
---|
188 | |
---|
189 | T* pointer_; |
---|
190 | OrxonoxClass* base_; |
---|
191 | Functor* callback_; |
---|
192 | }; |
---|
193 | |
---|
194 | template <class T> |
---|
195 | void swap(WeakPtr<T>& a, WeakPtr<T>& b) |
---|
196 | { |
---|
197 | a.swap(b); |
---|
198 | } |
---|
199 | |
---|
200 | template <class T, class U> |
---|
201 | WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p) |
---|
202 | { |
---|
203 | return static_cast<T*>(p.get()); |
---|
204 | } |
---|
205 | |
---|
206 | template <class T, class U> |
---|
207 | WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p) |
---|
208 | { |
---|
209 | return const_cast<T*>(p.get()); |
---|
210 | } |
---|
211 | |
---|
212 | template <class T, class U> |
---|
213 | WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p) |
---|
214 | { |
---|
215 | return dynamic_cast<T*>(p.get()); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | #endif /* _WeakPtr_H__ */ |
---|