Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/WeakPtr.h @ 7204

Last change on this file since 7204 was 7204, checked in by landauf, 14 years ago

adjusted includes in all other files

  • Property svn:eol-style set to native
File size: 5.6 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 "OrxonoxClass.h"
38#include "command/Functor.h"
39
40namespace orxonox
41{
42    template <class T>
43    class WeakPtr
44    {
45        friend class OrxonoxClass;
46
47        public:
48            inline WeakPtr() : pointer_(0), base_(0), callback_(0)
49            {
50            }
51
52            inline WeakPtr(int) : pointer_(0), base_(0), callback_(0)
53            {
54            }
55
56            inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0)
57            {
58                if (this->base_)
59                    this->base_->registerWeakPtr(this);
60            }
61
62            inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0)
63            {
64                if (this->base_)
65                    this->base_->registerWeakPtr(this);
66            }
67
68            template <class O>
69            inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0)
70            {
71                if (this->base_)
72                    this->base_->registerWeakPtr(this);
73            }
74
75            inline ~WeakPtr()
76            {
77                if (this->base_)
78                    this->base_->unregisterWeakPtr(this);
79
80            }
81
82            inline const WeakPtr& operator=(int)
83            {
84                WeakPtr(0).swap(*this);
85                return *this;
86            }
87
88            inline const WeakPtr& operator=(T* pointer)
89            {
90                WeakPtr(pointer).swap(*this);
91                return *this;
92            }
93
94            inline const WeakPtr& operator=(const WeakPtr& other)
95            {
96                WeakPtr(other).swap(*this);
97                return *this;
98            }
99
100            template <class O>
101            inline const WeakPtr& operator=(const WeakPtr<O>& other)
102            {
103                WeakPtr(other).swap(*this);
104                return *this;
105            }
106
107            inline T* get() const
108            {
109                return this->pointer_;
110            }
111
112            inline OrxonoxClass* getBase() const
113            {
114                return this->base_;
115            }
116
117            inline operator T*() const
118            {
119                return this->pointer_;
120            }
121
122            inline T* operator->() const
123            {
124                assert(this->pointer_ != 0);
125                return this->pointer_;
126            }
127
128            inline T& operator*() const
129            {
130                assert(this->pointer_ != 0);
131                return *this->pointer_;
132            }
133
134            inline bool operator!() const
135            {
136                return (this->pointer_ == 0);
137            }
138
139            inline void swap(WeakPtr& other)
140            {
141                if (this->base_)
142                    this->base_->unregisterWeakPtr(this);
143                if (other.base_)
144                    other.base_->unregisterWeakPtr(&other);
145
146                {
147                    T* temp = this->pointer_;
148                    this->pointer_ = other.pointer_;
149                    other.pointer_ = temp;
150                }
151                {
152                    OrxonoxClass* temp = this->base_;
153                    this->base_ = other.base_;
154                    other.base_ = temp;
155                }
156
157                if (this->base_)
158                    this->base_->registerWeakPtr(this);
159                if (other.base_)
160                    other.base_->registerWeakPtr(&other);
161            }
162
163            inline void reset()
164            {
165                WeakPtr().swap(*this);
166            }
167
168            inline void setCallback(const FunctorPtr& callback)
169            {
170                this->callback_ = callback;
171            }
172
173            inline const FunctorPtr& getCallback() const
174            {
175                return this->callback_;
176            }
177
178        private:
179            inline void objectDeleted()
180            {
181                this->base_ = 0;
182                this->pointer_ = 0;
183                if (this->callback_)
184                    (*this->callback_)();
185            }
186
187            T* pointer_;
188            OrxonoxClass* base_;
189            FunctorPtr callback_;
190    };
191
192    template <class T>
193    void swap(WeakPtr<T>& a, WeakPtr<T>& b)
194    {
195        a.swap(b);
196    }
197
198    template <class T, class U>
199    WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p)
200    {
201        return static_cast<T*>(p.get());
202    }
203
204    template <class T, class U>
205    WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p)
206    {
207        return const_cast<T*>(p.get());
208    }
209
210    template <class T, class U>
211    WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p)
212    {
213        return dynamic_cast<T*>(p.get());
214    }
215}
216
217#endif /* _WeakPtr_H__ */
Note: See TracBrowser for help on using the repository browser.