Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/SmartPtr.h @ 5805

Last change on this file since 5805 was 5805, checked in by landauf, 15 years ago
  • Enhanced SmartPtr: a) It stores now two pointers, one OrxonoxClass* (for reference counting) and one T* (for normal use). b) Incrementing the reference is now optional in the constructor for raw pointers. This is needed because Scene has a selfreference but should still be destroyable.
  • Changed BaseObject to store a SmartPtr to it's Scene instead of a normal pointer.
  • Changed setScene and getScene to deal directly with SmartPtr instead of a Scene* pointer to avoid casting-to-OrxonoxClass issues.
  • Fixed two problems with lost SceneManagers in CameraManger: a) added a SmartPtr to the Scene for the fallback camera b) added a call to GUIManager in the destructor to release the scene manager
  • Enabled unloading in GSLevel again (after years). Works if playing without bots.
  • Property svn:eol-style set to native
File size: 5.1 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 _SmartPtr_H__
32#define _SmartPtr_H__
33
34#include "CorePrereqs.h"
35
36#include <ostream>
37
38namespace orxonox
39{
40    template <class T>
41    class SmartPtr
42    {
43        public:
44            inline SmartPtr() : pointer_(0), base_(0)
45            {
46            }
47
48            inline SmartPtr(int) : pointer_(0), base_(0)
49            {
50            }
51
52            inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer)
53            {
54                if (this->base_ && bAddRef)
55                    this->base_->incrementReferenceCount();
56            }
57
58            inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_)
59            {
60                if (this->base_)
61                    this->base_->incrementReferenceCount();
62            }
63
64            template <class O>
65            inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_)
66            {
67                if (this->base_)
68                    this->base_->incrementReferenceCount();
69            }
70
71            inline ~SmartPtr()
72            {
73                if (this->base_)
74                    this->base_->decrementReferenceCount();
75            }
76           
77            inline const SmartPtr& operator=(int)
78            {
79                SmartPtr(0).swap(*this);
80                return *this;
81            }
82
83            inline const SmartPtr& operator=(T* pointer)
84            {
85                SmartPtr(pointer).swap(*this);
86                return *this;
87            }
88
89            inline const SmartPtr& operator=(const SmartPtr& other)
90            {
91                SmartPtr(other).swap(*this);
92                return *this;
93            }
94
95            template <class O>
96            inline const SmartPtr& operator=(const SmartPtr<O>& other)
97            {
98                SmartPtr(other).swap(*this);
99                return *this;
100            }
101
102            inline T* get() const
103            {
104                return this->pointer_;
105            }
106
107            inline operator T*() const
108            {
109                return this->pointer_;
110            }
111
112            inline T* operator->() const
113            {
114                return this->pointer_;
115            }
116
117            inline T& operator*() const
118            {
119                return *this->pointer_;
120            }
121
122            inline bool operator!() const
123            {
124                return (this->pointer_ == 0);
125            }
126
127            inline void swap(SmartPtr& other)
128            {
129                {
130                    T* temp = this->pointer_;
131                    this->pointer_ = other.pointer_;
132                    other.pointer_ = temp;
133                }
134                {
135                    OrxonoxClass* temp = this->base_;
136                    this->base_ = other.base_;
137                    other.base_ = temp;
138                }
139            }
140
141            inline void reset()
142            {
143                SmartPtr().swap(*this);
144            }
145
146        private:
147            T* pointer_;
148            OrxonoxClass* base_;
149    };
150
151    template <class A, class B>
152    inline bool operator==(const SmartPtr<A>& a, const SmartPtr<B>& b)
153    {
154        return (a.get() == b.get());
155    }
156
157    template <class A, class B>
158    inline bool operator!=(const SmartPtr<A>& a, const SmartPtr<B>& b)
159    {
160        return (a.get() != b.get());
161    }
162
163    template <class T>
164    inline bool operator<(const SmartPtr<T>& a, const SmartPtr<T>& b)
165    {
166        return std::less<T*>()(a.get(), b.get());
167    }
168
169    template <class T>
170    void swap(SmartPtr<T>& a, SmartPtr<T>& b)
171    {
172        a.swap(b);
173    }
174
175    template <class T, class U>
176    SmartPtr<T> static_pointer_cast(const SmartPtr<U>& p)
177    {
178        return static_cast<T*>(p.get());
179    }
180
181    template <class T, class U>
182    SmartPtr<T> const_pointer_cast(const SmartPtr<U>& p)
183    {
184        return const_cast<T*>(p.get());
185    }
186
187    template <class T, class U>
188    SmartPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p)
189    {
190        return dynamic_cast<T*>(p.get());
191    }
192
193    template <class T>
194    std::ostream& operator<<(std::ostream& os, const SmartPtr<T>& p)
195    {
196        os << p.get();
197        return os;
198    }
199}
200
201#endif /* _SmartPtr_H__ */
Note: See TracBrowser for help on using the repository browser.