Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/WeakPtr.h @ 5824

Last change on this file since 5824 was 5824, checked in by landauf, 15 years ago

added callback functionality to WeakPtr

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