Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/WeakPtr.h @ 7363

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

assigned a group to each header file in the core library

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