[5823] | 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> |
---|
[7268] | 37 | #include "Identifier.h" |
---|
[5823] | 38 | #include "OrxonoxClass.h" |
---|
[5824] | 39 | #include "Functor.h" |
---|
[5823] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | template <class T> |
---|
| 44 | class WeakPtr |
---|
| 45 | { |
---|
[5824] | 46 | friend class OrxonoxClass; |
---|
[6417] | 47 | |
---|
[5823] | 48 | public: |
---|
[5824] | 49 | inline WeakPtr() : pointer_(0), base_(0), callback_(0) |
---|
[5823] | 50 | { |
---|
| 51 | } |
---|
| 52 | |
---|
[5824] | 53 | inline WeakPtr(int) : pointer_(0), base_(0), callback_(0) |
---|
[5823] | 54 | { |
---|
| 55 | } |
---|
| 56 | |
---|
[5824] | 57 | inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0) |
---|
[5823] | 58 | { |
---|
| 59 | if (this->base_) |
---|
| 60 | this->base_->registerWeakPtr(this); |
---|
| 61 | } |
---|
| 62 | |
---|
[5824] | 63 | inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0) |
---|
[5823] | 64 | { |
---|
| 65 | if (this->base_) |
---|
| 66 | this->base_->registerWeakPtr(this); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | template <class O> |
---|
[5824] | 70 | inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0) |
---|
[5823] | 71 | { |
---|
| 72 | if (this->base_) |
---|
| 73 | this->base_->registerWeakPtr(this); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | inline ~WeakPtr() |
---|
| 77 | { |
---|
| 78 | if (this->base_) |
---|
| 79 | this->base_->unregisterWeakPtr(this); |
---|
[5824] | 80 | if (this->callback_) |
---|
| 81 | delete this->callback_; |
---|
[6417] | 82 | |
---|
[5823] | 83 | } |
---|
[6417] | 84 | |
---|
[7268] | 85 | inline WeakPtr& operator=(int) |
---|
[5823] | 86 | { |
---|
| 87 | WeakPtr(0).swap(*this); |
---|
| 88 | return *this; |
---|
| 89 | } |
---|
| 90 | |
---|
[7268] | 91 | inline WeakPtr& operator=(T* pointer) |
---|
[5823] | 92 | { |
---|
| 93 | WeakPtr(pointer).swap(*this); |
---|
| 94 | return *this; |
---|
| 95 | } |
---|
| 96 | |
---|
[7268] | 97 | inline WeakPtr& operator=(const WeakPtr& other) |
---|
[5823] | 98 | { |
---|
| 99 | WeakPtr(other).swap(*this); |
---|
| 100 | return *this; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | template <class O> |
---|
[7268] | 104 | inline WeakPtr& operator=(const WeakPtr<O>& other) |
---|
[5823] | 105 | { |
---|
| 106 | WeakPtr(other).swap(*this); |
---|
| 107 | return *this; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | inline T* get() const |
---|
| 111 | { |
---|
| 112 | return this->pointer_; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | inline OrxonoxClass* getBase() const |
---|
| 116 | { |
---|
| 117 | return this->base_; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | inline operator T*() const |
---|
| 121 | { |
---|
| 122 | return this->pointer_; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | inline T* operator->() const |
---|
| 126 | { |
---|
| 127 | assert(this->pointer_ != 0); |
---|
| 128 | return this->pointer_; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | inline T& operator*() const |
---|
| 132 | { |
---|
| 133 | assert(this->pointer_ != 0); |
---|
| 134 | return *this->pointer_; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | inline bool operator!() const |
---|
| 138 | { |
---|
| 139 | return (this->pointer_ == 0); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | inline void swap(WeakPtr& other) |
---|
| 143 | { |
---|
[5825] | 144 | if (this->base_) |
---|
| 145 | this->base_->unregisterWeakPtr(this); |
---|
| 146 | if (other.base_) |
---|
| 147 | other.base_->unregisterWeakPtr(&other); |
---|
[6417] | 148 | |
---|
[5823] | 149 | { |
---|
| 150 | T* temp = this->pointer_; |
---|
| 151 | this->pointer_ = other.pointer_; |
---|
| 152 | other.pointer_ = temp; |
---|
| 153 | } |
---|
| 154 | { |
---|
| 155 | OrxonoxClass* temp = this->base_; |
---|
| 156 | this->base_ = other.base_; |
---|
| 157 | other.base_ = temp; |
---|
| 158 | } |
---|
[5825] | 159 | |
---|
| 160 | if (this->base_) |
---|
| 161 | this->base_->registerWeakPtr(this); |
---|
| 162 | if (other.base_) |
---|
| 163 | other.base_->registerWeakPtr(&other); |
---|
[5823] | 164 | } |
---|
| 165 | |
---|
| 166 | inline void reset() |
---|
| 167 | { |
---|
| 168 | WeakPtr().swap(*this); |
---|
| 169 | } |
---|
[6417] | 170 | |
---|
[5861] | 171 | inline void setCallback(Functor* callback) |
---|
[5824] | 172 | { |
---|
| 173 | this->callback_ = callback; |
---|
| 174 | } |
---|
[6417] | 175 | |
---|
[5824] | 176 | inline Functor* getFunctor() const |
---|
| 177 | { |
---|
| 178 | return this->callback_; |
---|
| 179 | } |
---|
[5823] | 180 | |
---|
| 181 | private: |
---|
[5824] | 182 | inline void objectDeleted() |
---|
| 183 | { |
---|
[5825] | 184 | this->base_ = 0; |
---|
| 185 | this->pointer_ = 0; |
---|
[5824] | 186 | if (this->callback_) |
---|
| 187 | (*this->callback_)(); |
---|
| 188 | } |
---|
[6417] | 189 | |
---|
[5823] | 190 | T* pointer_; |
---|
| 191 | OrxonoxClass* base_; |
---|
[5824] | 192 | Functor* callback_; |
---|
[5823] | 193 | }; |
---|
| 194 | |
---|
| 195 | template <class T> |
---|
| 196 | void swap(WeakPtr<T>& a, WeakPtr<T>& b) |
---|
| 197 | { |
---|
| 198 | a.swap(b); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | template <class T, class U> |
---|
| 202 | WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p) |
---|
| 203 | { |
---|
| 204 | return static_cast<T*>(p.get()); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | template <class T, class U> |
---|
| 208 | WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p) |
---|
| 209 | { |
---|
| 210 | return const_cast<T*>(p.get()); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | template <class T, class U> |
---|
| 214 | WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p) |
---|
| 215 | { |
---|
[7268] | 216 | return orxonox_cast<T*>(p.get()); |
---|
[5823] | 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | #endif /* _WeakPtr_H__ */ |
---|