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