| 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 "Identifier.h" | 
|---|
| 38 | #include "OrxonoxClass.h" | 
|---|
| 39 | #include "command/Functor.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 | template <class T> | 
|---|
| 44 | class WeakPtr | 
|---|
| 45 | { | 
|---|
| 46 | friend class OrxonoxClass; | 
|---|
| 47 |  | 
|---|
| 48 | public: | 
|---|
| 49 | inline WeakPtr() : pointer_(0), base_(0), callback_(0) | 
|---|
| 50 | { | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | inline WeakPtr(int) : pointer_(0), base_(0), callback_(0) | 
|---|
| 54 | { | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0) | 
|---|
| 58 | { | 
|---|
| 59 | if (this->base_) | 
|---|
| 60 | this->base_->registerWeakPtr(this); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0) | 
|---|
| 64 | { | 
|---|
| 65 | if (this->base_) | 
|---|
| 66 | this->base_->registerWeakPtr(this); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | template <class O> | 
|---|
| 70 | inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0) | 
|---|
| 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); | 
|---|
| 80 |  | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | inline WeakPtr& operator=(int) | 
|---|
| 84 | { | 
|---|
| 85 | WeakPtr(0).swap(*this); | 
|---|
| 86 | return *this; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | inline WeakPtr& operator=(T* pointer) | 
|---|
| 90 | { | 
|---|
| 91 | WeakPtr(pointer).swap(*this); | 
|---|
| 92 | return *this; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | inline WeakPtr& operator=(const WeakPtr& other) | 
|---|
| 96 | { | 
|---|
| 97 | WeakPtr(other).swap(*this); | 
|---|
| 98 | return *this; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | template <class O> | 
|---|
| 102 | inline WeakPtr& operator=(const WeakPtr<O>& other) | 
|---|
| 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 | { | 
|---|
| 142 | if (this->base_) | 
|---|
| 143 | this->base_->unregisterWeakPtr(this); | 
|---|
| 144 | if (other.base_) | 
|---|
| 145 | other.base_->unregisterWeakPtr(&other); | 
|---|
| 146 |  | 
|---|
| 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 | } | 
|---|
| 157 |  | 
|---|
| 158 | if (this->base_) | 
|---|
| 159 | this->base_->registerWeakPtr(this); | 
|---|
| 160 | if (other.base_) | 
|---|
| 161 | other.base_->registerWeakPtr(&other); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | inline void reset() | 
|---|
| 165 | { | 
|---|
| 166 | WeakPtr().swap(*this); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | inline void setCallback(const FunctorPtr& callback) | 
|---|
| 170 | { | 
|---|
| 171 | this->callback_ = callback; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | inline const FunctorPtr& getCallback() const | 
|---|
| 175 | { | 
|---|
| 176 | return this->callback_; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | private: | 
|---|
| 180 | inline void objectDeleted() | 
|---|
| 181 | { | 
|---|
| 182 | this->base_ = 0; | 
|---|
| 183 | this->pointer_ = 0; | 
|---|
| 184 | if (this->callback_) | 
|---|
| 185 | (*this->callback_)(); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | T* pointer_; | 
|---|
| 189 | OrxonoxClass* base_; | 
|---|
| 190 | FunctorPtr callback_; | 
|---|
| 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 | { | 
|---|
| 214 | return orxonox_cast<T*>(p.get()); | 
|---|
| 215 | } | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | #endif /* _WeakPtr_H__ */ | 
|---|