[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" |
---|
[7284] | 39 | #include "command/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); |
---|
[6417] | 80 | |
---|
[5823] | 81 | } |
---|
[6417] | 82 | |
---|
[7268] | 83 | inline WeakPtr& operator=(int) |
---|
[5823] | 84 | { |
---|
| 85 | WeakPtr(0).swap(*this); |
---|
| 86 | return *this; |
---|
| 87 | } |
---|
| 88 | |
---|
[7268] | 89 | inline WeakPtr& operator=(T* pointer) |
---|
[5823] | 90 | { |
---|
| 91 | WeakPtr(pointer).swap(*this); |
---|
| 92 | return *this; |
---|
| 93 | } |
---|
| 94 | |
---|
[7268] | 95 | inline WeakPtr& operator=(const WeakPtr& other) |
---|
[5823] | 96 | { |
---|
| 97 | WeakPtr(other).swap(*this); |
---|
| 98 | return *this; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | template <class O> |
---|
[7268] | 102 | inline WeakPtr& operator=(const WeakPtr<O>& other) |
---|
[5823] | 103 | { |
---|
| 104 | WeakPtr(other).swap(*this); |
---|
| 105 | return *this; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | inline T* get() const |
---|
| 109 | { |
---|
| 110 | return this->pointer_; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | inline OrxonoxClass* getBase() const |
---|
| 114 | { |
---|
| 115 | return this->base_; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | inline operator T*() const |
---|
| 119 | { |
---|
| 120 | return this->pointer_; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | inline T* operator->() const |
---|
| 124 | { |
---|
| 125 | assert(this->pointer_ != 0); |
---|
| 126 | return this->pointer_; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | inline T& operator*() const |
---|
| 130 | { |
---|
| 131 | assert(this->pointer_ != 0); |
---|
| 132 | return *this->pointer_; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | inline bool operator!() const |
---|
| 136 | { |
---|
| 137 | return (this->pointer_ == 0); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | inline void swap(WeakPtr& other) |
---|
| 141 | { |
---|
[5825] | 142 | if (this->base_) |
---|
| 143 | this->base_->unregisterWeakPtr(this); |
---|
| 144 | if (other.base_) |
---|
| 145 | other.base_->unregisterWeakPtr(&other); |
---|
[6417] | 146 | |
---|
[5823] | 147 | { |
---|
| 148 | T* temp = this->pointer_; |
---|
| 149 | this->pointer_ = other.pointer_; |
---|
| 150 | other.pointer_ = temp; |
---|
| 151 | } |
---|
| 152 | { |
---|
| 153 | OrxonoxClass* temp = this->base_; |
---|
| 154 | this->base_ = other.base_; |
---|
| 155 | other.base_ = temp; |
---|
| 156 | } |
---|
[5825] | 157 | |
---|
| 158 | if (this->base_) |
---|
| 159 | this->base_->registerWeakPtr(this); |
---|
| 160 | if (other.base_) |
---|
| 161 | other.base_->registerWeakPtr(&other); |
---|
[5823] | 162 | } |
---|
| 163 | |
---|
| 164 | inline void reset() |
---|
| 165 | { |
---|
| 166 | WeakPtr().swap(*this); |
---|
| 167 | } |
---|
[6417] | 168 | |
---|
[7284] | 169 | inline void setCallback(const FunctorPtr& callback) |
---|
[5824] | 170 | { |
---|
| 171 | this->callback_ = callback; |
---|
| 172 | } |
---|
[6417] | 173 | |
---|
[7284] | 174 | inline const FunctorPtr& getCallback() const |
---|
[5824] | 175 | { |
---|
| 176 | return this->callback_; |
---|
| 177 | } |
---|
[5823] | 178 | |
---|
| 179 | private: |
---|
[5824] | 180 | inline void objectDeleted() |
---|
| 181 | { |
---|
[5825] | 182 | this->base_ = 0; |
---|
| 183 | this->pointer_ = 0; |
---|
[5824] | 184 | if (this->callback_) |
---|
| 185 | (*this->callback_)(); |
---|
| 186 | } |
---|
[6417] | 187 | |
---|
[5823] | 188 | T* pointer_; |
---|
| 189 | OrxonoxClass* base_; |
---|
[7284] | 190 | FunctorPtr callback_; |
---|
[5823] | 191 | }; |
---|
| 192 | |
---|
| 193 | template <class T> |
---|
| 194 | void swap(WeakPtr<T>& a, WeakPtr<T>& b) |
---|
| 195 | { |
---|
| 196 | a.swap(b); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | template <class T, class U> |
---|
| 200 | WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p) |
---|
| 201 | { |
---|
| 202 | return static_cast<T*>(p.get()); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | template <class T, class U> |
---|
| 206 | WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p) |
---|
| 207 | { |
---|
| 208 | return const_cast<T*>(p.get()); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | template <class T, class U> |
---|
| 212 | WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p) |
---|
| 213 | { |
---|
[7268] | 214 | return orxonox_cast<T*>(p.get()); |
---|
[5823] | 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | #endif /* _WeakPtr_H__ */ |
---|