Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/WeakPtr.h @ 7268

Last change on this file since 7268 was 7268, checked in by rgrieder, 14 years ago

operator=() should not return constant references.

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