Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5807 was 5807, checked in by rgrieder, 15 years ago
  • Added OrxonoxClass include to the SmartPtr since that is needed in any case.
  • Inserted asserts for operators * and → to avoid segfaults and instead have a clean abort (in debug mode anyway).
  • Property svn:eol-style set to native
File size: 5.2 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 <cassert>
37#include <ostream>
38#include "OrxonoxClass.h"
39
40namespace orxonox
41{
42    template <class T>
43    class SmartPtr
44    {
45        public:
46            inline SmartPtr() : pointer_(0), base_(0)
47            {
48            }
49
50            inline SmartPtr(int) : pointer_(0), base_(0)
51            {
52            }
53
54            inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer)
55            {
56                if (this->base_ && bAddRef)
57                    this->base_->incrementReferenceCount();
58            }
59
60            inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_)
61            {
62                if (this->base_)
63                    this->base_->incrementReferenceCount();
64            }
65
66            template <class O>
67            inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_)
68            {
69                if (this->base_)
70                    this->base_->incrementReferenceCount();
71            }
72
73            inline ~SmartPtr()
74            {
75                if (this->base_)
76                    this->base_->decrementReferenceCount();
77            }
78           
79            inline const SmartPtr& operator=(int)
80            {
81                SmartPtr(0).swap(*this);
82                return *this;
83            }
84
85            inline const SmartPtr& operator=(T* pointer)
86            {
87                SmartPtr(pointer).swap(*this);
88                return *this;
89            }
90
91            inline const SmartPtr& operator=(const SmartPtr& other)
92            {
93                SmartPtr(other).swap(*this);
94                return *this;
95            }
96
97            template <class O>
98            inline const SmartPtr& operator=(const SmartPtr<O>& other)
99            {
100                SmartPtr(other).swap(*this);
101                return *this;
102            }
103
104            inline T* get() const
105            {
106                return this->pointer_;
107            }
108
109            inline operator T*() const
110            {
111                return this->pointer_;
112            }
113
114            inline T* operator->() const
115            {
116                assert(this->pointer_ != 0);
117                return this->pointer_;
118            }
119
120            inline T& operator*() const
121            {
122                assert(this->pointer_ != 0);
123                return *this->pointer_;
124            }
125
126            inline bool operator!() const
127            {
128                return (this->pointer_ == 0);
129            }
130
131            inline void swap(SmartPtr& other)
132            {
133                {
134                    T* temp = this->pointer_;
135                    this->pointer_ = other.pointer_;
136                    other.pointer_ = temp;
137                }
138                {
139                    OrxonoxClass* temp = this->base_;
140                    this->base_ = other.base_;
141                    other.base_ = temp;
142                }
143            }
144
145            inline void reset()
146            {
147                SmartPtr().swap(*this);
148            }
149
150        private:
151            T* pointer_;
152            OrxonoxClass* base_;
153    };
154
155    template <class A, class B>
156    inline bool operator==(const SmartPtr<A>& a, const SmartPtr<B>& b)
157    {
158        return (a.get() == b.get());
159    }
160
161    template <class A, class B>
162    inline bool operator!=(const SmartPtr<A>& a, const SmartPtr<B>& b)
163    {
164        return (a.get() != b.get());
165    }
166
167    template <class T>
168    inline bool operator<(const SmartPtr<T>& a, const SmartPtr<T>& b)
169    {
170        return std::less<T*>()(a.get(), b.get());
171    }
172
173    template <class T>
174    void swap(SmartPtr<T>& a, SmartPtr<T>& b)
175    {
176        a.swap(b);
177    }
178
179    template <class T, class U>
180    SmartPtr<T> static_pointer_cast(const SmartPtr<U>& p)
181    {
182        return static_cast<T*>(p.get());
183    }
184
185    template <class T, class U>
186    SmartPtr<T> const_pointer_cast(const SmartPtr<U>& p)
187    {
188        return const_cast<T*>(p.get());
189    }
190
191    template <class T, class U>
192    SmartPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p)
193    {
194        return dynamic_cast<T*>(p.get());
195    }
196
197    template <class T>
198    std::ostream& operator<<(std::ostream& os, const SmartPtr<T>& p)
199    {
200        os << p.get();
201        return os;
202    }
203}
204
205#endif /* _SmartPtr_H__ */
Note: See TracBrowser for help on using the repository browser.