Orxonox  0.0.5 Codename: Arcturus
WeakPtr.h
Go to the documentation of this file.
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 
80 #ifndef _WeakPtr_H__
81 #define _WeakPtr_H__
82 
83 #include "core/CorePrereqs.h"
84 
85 #include <cassert>
86 
88 #include "core/command/Functor.h"
89 
90 namespace orxonox
91 {
97  template <class T>
98  class WeakPtr : public DestructionListener
99  {
100  public:
102  inline WeakPtr() : pointer_(nullptr), base_(nullptr), callback_(nullptr)
103  {
104  }
105 
107  inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(nullptr)
108  {
110  }
111 
113  inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(nullptr)
114  {
116  }
117 
119  template <class O>
120  inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase()), callback_(nullptr)
121  {
123  }
124 
126  virtual inline ~WeakPtr()
127  {
129  }
130 
132  inline WeakPtr& operator=(T* pointer)
133  {
134  WeakPtr(pointer).swap(*this);
135  return *this;
136  }
137 
139  inline WeakPtr& operator=(WeakPtr other)
140  {
141  other.swap(*this);
142  return *this;
143  }
144 
146  template <class O>
147  inline WeakPtr& operator=(const WeakPtr<O>& other)
148  {
149  WeakPtr(other).swap(*this);
150  return *this;
151  }
152 
154  inline T* get() const
155  {
156  return this->pointer_;
157  }
158 
160  inline Destroyable* getBase() const
161  {
162  return this->base_;
163  }
164 
166  inline operator T*() const
167  {
168  return this->pointer_;
169  }
170 
172  inline T* operator->() const
173  {
174  assert(this->pointer_ != nullptr);
175  return this->pointer_;
176  }
177 
179  inline T& operator*() const
180  {
181  assert(this->pointer_ != nullptr);
182  return *this->pointer_;
183  }
184 
186  inline explicit operator bool() const
187  {
188  return (this->pointer_ != nullptr);
189  }
190 
192  inline void swap(WeakPtr& other)
193  {
195  other.unregisterAsDestructionListener(other.base_);
196 
197  {
198  T* temp = this->pointer_;
199  this->pointer_ = other.pointer_;
200  other.pointer_ = temp;
201  }
202  {
203  Destroyable* temp = this->base_;
204  this->base_ = other.base_;
205  other.base_ = temp;
206  }
207 
209  other.registerAsDestructionListener(other.base_);
210  }
211 
213  inline void reset()
214  {
215  WeakPtr().swap(*this);
216  }
217 
219  inline void setCallback(const FunctorPtr& callback)
220  {
221  this->callback_ = callback;
222  }
223 
225  inline const FunctorPtr& getCallback() const
226  {
227  return this->callback_;
228  }
229 
230  private:
232  virtual inline void objectDeleted() override
233  {
234  this->base_ = nullptr;
235  this->pointer_ = nullptr;
236  if (this->callback_)
237  (*this->callback_)();
238  }
239 
243  };
244 
246  template <class T>
248  {
249  a.swap(b);
250  }
251 
253  template <class T, class U>
255  {
256  return static_cast<T*>(p.get());
257  }
258 
260  template <class T, class U>
262  {
263  return const_cast<T*>(p.get());
264  }
265 
267  template <class T, class U>
269  {
270  return orxonox_cast<T*>(p.get());
271  }
272 }
273 
274 #endif /* _WeakPtr_H__ */
WeakPtr & operator=(WeakPtr other)
Assigns the wrapped pointer of another WeakPtr.
Definition: WeakPtr.h:139
std::shared_ptr< Functor > FunctorPtr
Definition: FunctorPtr.h:57
WeakPtr(const WeakPtr &other)
Copy-constructor.
Definition: WeakPtr.h:113
virtual void objectDeleted() override
Will be called by Destroyable::~Destroyable() if the stored object is deleted. Resets the wrapped poi...
Definition: WeakPtr.h:232
Definition of orxonox::Functor and its specialized subclasses, as well as the createFunctor() functio...
StrongPtr< T > static_pointer_cast(const StrongPtr< U > &p)
Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new Stron...
Definition: StrongPtr.h:291
WeakPtr(T *pointer)
Constructor: Initializes the weak pointer with a pointer to an object.
Definition: WeakPtr.h:107
Shared library macros, enums, constants and forward declarations for the core library ...
void unregisterAsDestructionListener(Destroyable *object)
Definition: Destroyable.h:106
void registerAsDestructionListener(Destroyable *object)
Definition: Destroyable.h:104
virtual ~WeakPtr()
Destructor.
Definition: WeakPtr.h:126
FunctorPtr callback_
This callback will be executed if the stored object is deleted.
Definition: WeakPtr.h:242
WeakPtr wraps a pointer to an object, which becomes nullptr if the object is deleted.
Definition: CorePrereqs.h:236
Destroyable * getBase() const
Returns the wrapped pointer as Destroyable*.
Definition: WeakPtr.h:160
T * get() const
Returns the wrapped pointer as T*.
Definition: WeakPtr.h:154
void reset()
Resets the weak pointer (equivalent to assigning a nullptr).
Definition: WeakPtr.h:213
void swap(WeakPtr &other)
Swaps the contents of two weak pointers.
Definition: WeakPtr.h:192
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
T * pointer_
The wrapped pointer to an object of type T.
Definition: WeakPtr.h:240
const FunctorPtr & getCallback() const
Returns the registered callback.
Definition: WeakPtr.h:225
Declaration of Destroyable, the base class of all objects which can be used with StrongPtr and WeakPt...
ORX_FORCEINLINE T orxonox_cast(U *source)
Casts on object of type Identifiable to any derived type that is registered in the class hierarchy...
Definition: Identifier.h:485
Definition: InputPrereqs.h:78
StrongPtr< T > dynamic_pointer_cast(const StrongPtr< U > &p)
Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new Stro...
Definition: StrongPtr.h:305
void setCallback(const FunctorPtr &callback)
Registers a callback that will be executed if the stored object is destroyed.
Definition: WeakPtr.h:219
Classes must inherit from this class if they should be used with StrongPtr or WeakPtr.
Definition: Destroyable.h:47
WeakPtr & operator=(const WeakPtr< O > &other)
Assigns the wrapped pointer of a WeakPtr of another class.
Definition: WeakPtr.h:147
T & operator*() const
Overloaded operator, returns a reference to the stored object.
Definition: WeakPtr.h:179
Destroyable * base_
The wrapped pointer, casted up to Destroyable (this is needed because with just a T* pointer...
Definition: WeakPtr.h:241
WeakPtr(const WeakPtr< O > &other)
Copy-constructor for weak pointers to objects of another class.
Definition: WeakPtr.h:120
WeakPtr()
Constructor: Initializes the weak pointer with a null pointer.
Definition: WeakPtr.h:102
StrongPtr< T > const_pointer_cast(const StrongPtr< U > &p)
Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new Strong...
Definition: StrongPtr.h:298
T * operator->() const
Overloaded operator, returns a pointer to the stored object.
Definition: WeakPtr.h:172
WeakPtr & operator=(T *pointer)
Assigns a new pointer.
Definition: WeakPtr.h:132