Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added WeakPtr (a pointer which becomes 0 if the target object is deleted)

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