[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 | |
---|
[7401] | 31 | /** |
---|
| 32 | @file |
---|
| 33 | @ingroup Object SmartPtr |
---|
| 34 | @brief Definition of WeakPtr<T>, wraps a pointer to an object. |
---|
| 35 | |
---|
| 36 | @anchor WeakPtrExample |
---|
| 37 | |
---|
| 38 | A WeakPtr wraps a pointer to an object. If the object gets deleted, the WeakPtr becomes |
---|
[11071] | 39 | nullptr. This can be used to store pointers to objects without knowing when they will be |
---|
[7401] | 40 | destroyed. |
---|
| 41 | |
---|
[9571] | 42 | WeakPtr works only with objects that are derived from orxonox::Destroyable, because |
---|
[7401] | 43 | WeakPtr is intrusive and registers itself in the stored object, to get a notification if |
---|
| 44 | the object is being deleted. |
---|
| 45 | |
---|
| 46 | Example: |
---|
| 47 | @code |
---|
| 48 | MyClass* object = new MyClass(); // create an instance of MyClass |
---|
| 49 | |
---|
| 50 | WeakPtr<MyClass> pointer = object; // create a WeakPtr and assign the object |
---|
| 51 | |
---|
[11071] | 52 | if (pointer) // checks if pointer is not nullptr (which is true) |
---|
[7401] | 53 | pointer->someFunction(); // calls MyClass::someFunction() |
---|
| 54 | |
---|
| 55 | object->destroy(); // calls destroy() which deletes the object |
---|
| 56 | |
---|
[11071] | 57 | if (pointer) // checks if pointer is not nullptr (which is now false) |
---|
[7401] | 58 | pointer->someFunction(); // this will not be executed |
---|
| 59 | @endcode |
---|
[9571] | 60 | In this example we assumed that MyClass is derived of Destroyable (otherwise it couldn't |
---|
[7401] | 61 | be used with a WeakPtr). |
---|
| 62 | |
---|
| 63 | A callback can be registerd with the WeakPtr that will be called if the object gets deleted. |
---|
| 64 | @code |
---|
| 65 | void myCallback() // definition of the callback function |
---|
| 66 | { |
---|
[8858] | 67 | orxout() << "Object destroyed" << endl; |
---|
[7401] | 68 | } |
---|
| 69 | |
---|
| 70 | MyClass* object = new MyClass(); // create an instance of MyClass |
---|
| 71 | |
---|
| 72 | WeakPtr<MyClass> pointer = object; // create a WeakPtr and assign the object |
---|
| 73 | |
---|
| 74 | pointer.setCallback(createFunctor(&myCallback)); // defines a callback |
---|
| 75 | |
---|
| 76 | object->destroy(); // calls destroy() which deletes the object. prints "Object destroyed" to the console |
---|
| 77 | @endcode |
---|
| 78 | */ |
---|
| 79 | |
---|
[5823] | 80 | #ifndef _WeakPtr_H__ |
---|
| 81 | #define _WeakPtr_H__ |
---|
| 82 | |
---|
[9557] | 83 | #include "core/CorePrereqs.h" |
---|
[5823] | 84 | |
---|
| 85 | #include <cassert> |
---|
| 86 | |
---|
[9571] | 87 | #include "core/object/Destroyable.h" |
---|
[9557] | 88 | #include "core/command/Functor.h" |
---|
| 89 | |
---|
[5823] | 90 | namespace orxonox |
---|
| 91 | { |
---|
[7401] | 92 | /** |
---|
[11071] | 93 | @brief WeakPtr wraps a pointer to an object, which becomes nullptr if the object is deleted. |
---|
[7401] | 94 | |
---|
| 95 | @see See @ref WeakPtrExample "this description" for more information and an example. |
---|
| 96 | */ |
---|
[5823] | 97 | template <class T> |
---|
[7849] | 98 | class WeakPtr : public DestructionListener |
---|
[5823] | 99 | { |
---|
| 100 | public: |
---|
[7401] | 101 | /// Constructor: Initializes the weak pointer with a null pointer. |
---|
[11071] | 102 | inline WeakPtr() : pointer_(nullptr), base_(nullptr), callback_(nullptr) |
---|
[5823] | 103 | { |
---|
| 104 | } |
---|
| 105 | |
---|
[7401] | 106 | /// Constructor: Initializes the weak pointer with a pointer to an object. |
---|
[11071] | 107 | inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(nullptr) |
---|
[5823] | 108 | { |
---|
[7850] | 109 | this->registerAsDestructionListener(this->base_); |
---|
[5823] | 110 | } |
---|
| 111 | |
---|
[7401] | 112 | /// Copy-constructor |
---|
[11071] | 113 | inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(nullptr) |
---|
[5823] | 114 | { |
---|
[7850] | 115 | this->registerAsDestructionListener(this->base_); |
---|
[5823] | 116 | } |
---|
| 117 | |
---|
[7401] | 118 | /// Copy-constructor for weak pointers to objects of another class. |
---|
[5823] | 119 | template <class O> |
---|
[11071] | 120 | inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase()), callback_(nullptr) |
---|
[5823] | 121 | { |
---|
[7850] | 122 | this->registerAsDestructionListener(this->base_); |
---|
[5823] | 123 | } |
---|
| 124 | |
---|
[7401] | 125 | /// Destructor |
---|
[11071] | 126 | virtual inline ~WeakPtr() |
---|
[5823] | 127 | { |
---|
[7850] | 128 | this->unregisterAsDestructionListener(this->base_); |
---|
[5823] | 129 | } |
---|
[6417] | 130 | |
---|
[7401] | 131 | /// Assigns a new pointer. |
---|
[7268] | 132 | inline WeakPtr& operator=(T* pointer) |
---|
[5823] | 133 | { |
---|
| 134 | WeakPtr(pointer).swap(*this); |
---|
| 135 | return *this; |
---|
| 136 | } |
---|
| 137 | |
---|
[7401] | 138 | /// Assigns the wrapped pointer of another WeakPtr. |
---|
[11071] | 139 | inline WeakPtr& operator=(WeakPtr other) |
---|
[5823] | 140 | { |
---|
[11071] | 141 | other.swap(*this); |
---|
[5823] | 142 | return *this; |
---|
| 143 | } |
---|
| 144 | |
---|
[7401] | 145 | /// Assigns the wrapped pointer of a WeakPtr of another class |
---|
[5823] | 146 | template <class O> |
---|
[7268] | 147 | inline WeakPtr& operator=(const WeakPtr<O>& other) |
---|
[5823] | 148 | { |
---|
| 149 | WeakPtr(other).swap(*this); |
---|
| 150 | return *this; |
---|
| 151 | } |
---|
| 152 | |
---|
[7401] | 153 | /// Returns the wrapped pointer as @c T* |
---|
[5823] | 154 | inline T* get() const |
---|
| 155 | { |
---|
| 156 | return this->pointer_; |
---|
| 157 | } |
---|
| 158 | |
---|
[9571] | 159 | /// Returns the wrapped pointer as @c Destroyable* |
---|
| 160 | inline Destroyable* getBase() const |
---|
[5823] | 161 | { |
---|
| 162 | return this->base_; |
---|
| 163 | } |
---|
| 164 | |
---|
[7401] | 165 | /// Implicitly converts the WeakPtr to a pointer of type @c T* |
---|
[5823] | 166 | inline operator T*() const |
---|
| 167 | { |
---|
| 168 | return this->pointer_; |
---|
| 169 | } |
---|
| 170 | |
---|
[7401] | 171 | /// Overloaded operator, returns a pointer to the stored object. |
---|
[5823] | 172 | inline T* operator->() const |
---|
| 173 | { |
---|
[11071] | 174 | assert(this->pointer_ != nullptr); |
---|
[5823] | 175 | return this->pointer_; |
---|
| 176 | } |
---|
| 177 | |
---|
[7401] | 178 | /// Overloaded operator, returns a reference to the stored object. |
---|
[5823] | 179 | inline T& operator*() const |
---|
| 180 | { |
---|
[11071] | 181 | assert(this->pointer_ != nullptr); |
---|
[5823] | 182 | return *this->pointer_; |
---|
| 183 | } |
---|
| 184 | |
---|
[11071] | 185 | /// Returns true if the pointer is not nullptr. |
---|
| 186 | inline explicit operator bool() const |
---|
[5823] | 187 | { |
---|
[11071] | 188 | return (this->pointer_ != nullptr); |
---|
[5823] | 189 | } |
---|
| 190 | |
---|
[7401] | 191 | /// Swaps the contents of two weak pointers. |
---|
[5823] | 192 | inline void swap(WeakPtr& other) |
---|
| 193 | { |
---|
[7850] | 194 | this->unregisterAsDestructionListener(this->base_); |
---|
| 195 | other.unregisterAsDestructionListener(other.base_); |
---|
[6417] | 196 | |
---|
[5823] | 197 | { |
---|
| 198 | T* temp = this->pointer_; |
---|
| 199 | this->pointer_ = other.pointer_; |
---|
| 200 | other.pointer_ = temp; |
---|
| 201 | } |
---|
| 202 | { |
---|
[9571] | 203 | Destroyable* temp = this->base_; |
---|
[5823] | 204 | this->base_ = other.base_; |
---|
| 205 | other.base_ = temp; |
---|
| 206 | } |
---|
[5825] | 207 | |
---|
[7850] | 208 | this->registerAsDestructionListener(this->base_); |
---|
| 209 | other.registerAsDestructionListener(other.base_); |
---|
[5823] | 210 | } |
---|
| 211 | |
---|
[11071] | 212 | /// Resets the weak pointer (equivalent to assigning a nullptr). |
---|
[5823] | 213 | inline void reset() |
---|
| 214 | { |
---|
| 215 | WeakPtr().swap(*this); |
---|
| 216 | } |
---|
[6417] | 217 | |
---|
[7401] | 218 | /// Registers a callback that will be executed if the stored object is destroyed. |
---|
[7284] | 219 | inline void setCallback(const FunctorPtr& callback) |
---|
[5824] | 220 | { |
---|
| 221 | this->callback_ = callback; |
---|
| 222 | } |
---|
[6417] | 223 | |
---|
[7401] | 224 | /// Returns the registered callback. |
---|
[7284] | 225 | inline const FunctorPtr& getCallback() const |
---|
[5824] | 226 | { |
---|
| 227 | return this->callback_; |
---|
| 228 | } |
---|
[5823] | 229 | |
---|
| 230 | private: |
---|
[9571] | 231 | /// Will be called by Destroyable::~Destroyable() if the stored object is deleted. Resets the wrapped pointer and executes the callback. |
---|
[11071] | 232 | virtual inline void objectDeleted() override |
---|
[5824] | 233 | { |
---|
[11071] | 234 | this->base_ = nullptr; |
---|
| 235 | this->pointer_ = nullptr; |
---|
[5824] | 236 | if (this->callback_) |
---|
| 237 | (*this->callback_)(); |
---|
| 238 | } |
---|
[6417] | 239 | |
---|
[7401] | 240 | T* pointer_; ///< The wrapped pointer to an object of type @a T |
---|
[9571] | 241 | Destroyable* base_; ///< The wrapped pointer, casted up to Destroyable (this is needed because with just a T* pointer, WeakPtr couln't be used with forward declarations) |
---|
[7401] | 242 | FunctorPtr callback_; ///< This callback will be executed if the stored object is deleted |
---|
[5823] | 243 | }; |
---|
| 244 | |
---|
[7401] | 245 | /// Swaps the contents of two weak pointers. |
---|
[5823] | 246 | template <class T> |
---|
| 247 | void swap(WeakPtr<T>& a, WeakPtr<T>& b) |
---|
| 248 | { |
---|
| 249 | a.swap(b); |
---|
| 250 | } |
---|
| 251 | |
---|
[7401] | 252 | /// Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>. |
---|
[5823] | 253 | template <class T, class U> |
---|
| 254 | WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p) |
---|
| 255 | { |
---|
| 256 | return static_cast<T*>(p.get()); |
---|
| 257 | } |
---|
| 258 | |
---|
[7401] | 259 | /// Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>. |
---|
[5823] | 260 | template <class T, class U> |
---|
| 261 | WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p) |
---|
| 262 | { |
---|
| 263 | return const_cast<T*>(p.get()); |
---|
| 264 | } |
---|
| 265 | |
---|
[7401] | 266 | /// Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>. |
---|
[5823] | 267 | template <class T, class U> |
---|
| 268 | WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p) |
---|
| 269 | { |
---|
[7268] | 270 | return orxonox_cast<T*>(p.get()); |
---|
[5823] | 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | #endif /* _WeakPtr_H__ */ |
---|