Orxonox  0.0.5 Codename: Arcturus
StrongPtr.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 
118 #ifndef _StrongPtr_H__
119 #define _StrongPtr_H__
120 
121 #include "core/CorePrereqs.h"
122 
123 #include <cassert>
124 
125 #include "core/object/Destroyable.h"
126 #include "WeakPtr.h"
127 
128 namespace orxonox
129 {
135  template <class T>
136  class StrongPtr
137  {
138  public:
140  inline StrongPtr() : pointer_(nullptr), base_(nullptr)
141  {
142  }
143 
145  inline StrongPtr(T* pointer) : pointer_(pointer), base_(pointer)
146  {
147  if (this->base_)
149  }
150 
152  inline StrongPtr(const StrongPtr& other) : pointer_(other.pointer_), base_(other.base_)
153  {
154  if (this->base_)
156  }
157 
159  template <class O>
160  inline StrongPtr(const StrongPtr<O>& other) : pointer_(other.get()), base_(other.getBase())
161  {
162  if (this->base_)
164  }
165 
167  template <class O>
168  inline StrongPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase())
169  {
170  if (this->base_)
172  }
173 
175  inline StrongPtr(StrongPtr&& other) : pointer_(other.pointer_), base_(other.base_)
176  {
177  other.pointer_ = nullptr;
178  other.base_ = nullptr;
179  }
180 
182  inline ~StrongPtr()
183  {
184  if (this->base_)
186  }
187 
189  inline StrongPtr& operator=(T* pointer)
190  {
191  StrongPtr(pointer).swap(*this);
192  return *this;
193  }
194 
197  {
198  other.swap(*this);
199  return *this;
200  }
201 
203  template <class O>
204  inline StrongPtr& operator=(const StrongPtr<O>& other)
205  {
206  StrongPtr(other).swap(*this);
207  return *this;
208  }
209 
211  template <class O>
212  inline StrongPtr& operator=(const WeakPtr<O>& other)
213  {
214  StrongPtr(other).swap(*this);
215  return *this;
216  }
217 
219  inline T* get() const
220  {
221  return this->pointer_;
222  }
223 
225  inline Destroyable* getBase() const
226  {
227  return this->base_;
228  }
229 
231  inline operator T*() const
232  {
233  return this->pointer_;
234  }
235 
237  inline T* operator->() const
238  {
239  assert(this->pointer_ != nullptr);
240  return this->pointer_;
241  }
242 
244  inline T& operator*() const
245  {
246  assert(this->pointer_ != nullptr);
247  return *this->pointer_;
248  }
249 
251  inline explicit operator bool() const
252  {
253  return (this->pointer_ != nullptr);
254  }
255 
257  inline void swap(StrongPtr& other)
258  {
259  {
260  T* temp = this->pointer_;
261  this->pointer_ = other.pointer_;
262  other.pointer_ = temp;
263  }
264  {
265  Destroyable* temp = this->base_;
266  this->base_ = other.base_;
267  other.base_ = temp;
268  }
269  }
270 
272  inline void reset()
273  {
274  StrongPtr().swap(*this);
275  }
276 
277  private:
280  };
281 
283  template <class T>
285  {
286  a.swap(b);
287  }
288 
290  template <class T, class U>
292  {
293  return static_cast<T*>(p.get());
294  }
295 
297  template <class T, class U>
299  {
300  return const_cast<T*>(p.get());
301  }
302 
304  template <class T, class U>
306  {
307  return orxonox_cast<T*>(p.get());
308  }
309 }
310 
311 #endif /* _StrongPtr_H__ */
void decrementReferenceCount()
Decrements the reference counter (for strong pointers).
Definition: Destroyable.h:75
StrongPtr(const StrongPtr< O > &other)
Copy-constructor for strong pointers to objects of another class.
Definition: StrongPtr.h:160
T * operator->() const
Overloaded operator, returns a pointer to the stored object.
Definition: StrongPtr.h:237
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
Shared library macros, enums, constants and forward declarations for the core library ...
A strong pointer which wraps a pointer to an object and keeps this object alive as long as the strong...
Definition: CorePrereqs.h:227
StrongPtr & operator=(const StrongPtr< O > &other)
Assigns the wrapped pointer of a StrongPtr of another class.
Definition: StrongPtr.h:204
void swap(StrongPtr &other)
Swaps the contents of two strong pointers.
Definition: StrongPtr.h:257
~StrongPtr()
Destructor: Decrements the reference counter.
Definition: StrongPtr.h:182
Definition of WeakPtr<T>, wraps a pointer to an object.
StrongPtr()
Constructor: Initializes the strong pointer with a null pointer.
Definition: StrongPtr.h:140
WeakPtr wraps a pointer to an object, which becomes nullptr if the object is deleted.
Definition: CorePrereqs.h:236
StrongPtr(const WeakPtr< O > &other)
Constructor: Initializes the strong pointer with the pointer that is stored in a WeakPtr.
Definition: StrongPtr.h:168
void incrementReferenceCount()
Increments the reference counter (for strong pointers).
Definition: Destroyable.h:72
Destroyable * base_
The wrapped pointer, casted up to Destroyable (this is needed because with just a T* pointer...
Definition: StrongPtr.h:279
StrongPtr(T *pointer)
Constructor: Initializes the strong pointer with a pointer to an object.
Definition: StrongPtr.h:145
T * get() const
Returns the wrapped pointer as T*.
Definition: StrongPtr.h:219
Destroyable * getBase() const
Returns the wrapped pointer as Destroyable*.
Definition: StrongPtr.h:225
T * pointer_
The wrapped pointer to an object of type T.
Definition: StrongPtr.h:278
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
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
T & operator*() const
Overloaded operator, returns a reference to the stored object.
Definition: StrongPtr.h:244
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
StrongPtr & operator=(T *pointer)
Assigns a new pointer.
Definition: StrongPtr.h:189
Classes must inherit from this class if they should be used with StrongPtr or WeakPtr.
Definition: Destroyable.h:47
StrongPtr(StrongPtr &&other)
Move-constructor.
Definition: StrongPtr.h:175
StrongPtr & operator=(const WeakPtr< O > &other)
Assigns the wrapped pointer of a WeakPtr.
Definition: StrongPtr.h:212
void reset()
Resets the strong pointer (equivalent to assigning a nullptr).
Definition: StrongPtr.h:272
StrongPtr(const StrongPtr &other)
Copy-constructor.
Definition: StrongPtr.h:152
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
StrongPtr & operator=(StrongPtr other)
Assigns the wrapped pointer of another StrongPtr.
Definition: StrongPtr.h:196