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 | |
---|
38 | #include "Identifier.h" |
---|
39 | #include "OrxonoxClass.h" |
---|
40 | #include "WeakPtr.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | template <class T> |
---|
45 | class SmartPtr |
---|
46 | { |
---|
47 | public: |
---|
48 | inline SmartPtr() : pointer_(0), base_(0) |
---|
49 | { |
---|
50 | } |
---|
51 | |
---|
52 | inline SmartPtr(int) : pointer_(0), base_(0) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer) |
---|
57 | { |
---|
58 | if (this->base_ && bAddRef) |
---|
59 | this->base_->incrementReferenceCount(); |
---|
60 | } |
---|
61 | |
---|
62 | inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_) |
---|
63 | { |
---|
64 | if (this->base_) |
---|
65 | this->base_->incrementReferenceCount(); |
---|
66 | } |
---|
67 | |
---|
68 | template <class O> |
---|
69 | inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_) |
---|
70 | { |
---|
71 | if (this->base_) |
---|
72 | this->base_->incrementReferenceCount(); |
---|
73 | } |
---|
74 | |
---|
75 | template <class O> |
---|
76 | inline SmartPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase()) |
---|
77 | { |
---|
78 | if (this->base_) |
---|
79 | this->base_->incrementReferenceCount(); |
---|
80 | } |
---|
81 | |
---|
82 | inline ~SmartPtr() |
---|
83 | { |
---|
84 | if (this->base_) |
---|
85 | this->base_->decrementReferenceCount(); |
---|
86 | } |
---|
87 | |
---|
88 | inline SmartPtr& operator=(int) |
---|
89 | { |
---|
90 | SmartPtr(0).swap(*this); |
---|
91 | return *this; |
---|
92 | } |
---|
93 | |
---|
94 | inline SmartPtr& operator=(T* pointer) |
---|
95 | { |
---|
96 | SmartPtr(pointer).swap(*this); |
---|
97 | return *this; |
---|
98 | } |
---|
99 | |
---|
100 | inline SmartPtr& operator=(const SmartPtr& other) |
---|
101 | { |
---|
102 | SmartPtr(other).swap(*this); |
---|
103 | return *this; |
---|
104 | } |
---|
105 | |
---|
106 | template <class O> |
---|
107 | inline SmartPtr& operator=(const SmartPtr<O>& other) |
---|
108 | { |
---|
109 | SmartPtr(other).swap(*this); |
---|
110 | return *this; |
---|
111 | } |
---|
112 | |
---|
113 | template <class O> |
---|
114 | inline SmartPtr& operator=(const WeakPtr<O>& other) |
---|
115 | { |
---|
116 | SmartPtr(other).swap(*this); |
---|
117 | return *this; |
---|
118 | } |
---|
119 | |
---|
120 | inline T* get() const |
---|
121 | { |
---|
122 | return this->pointer_; |
---|
123 | } |
---|
124 | |
---|
125 | inline OrxonoxClass* getBase() const |
---|
126 | { |
---|
127 | return this->base_; |
---|
128 | } |
---|
129 | |
---|
130 | inline operator T*() const |
---|
131 | { |
---|
132 | return this->pointer_; |
---|
133 | } |
---|
134 | |
---|
135 | inline T* operator->() const |
---|
136 | { |
---|
137 | assert(this->pointer_ != 0); |
---|
138 | return this->pointer_; |
---|
139 | } |
---|
140 | |
---|
141 | inline T& operator*() const |
---|
142 | { |
---|
143 | assert(this->pointer_ != 0); |
---|
144 | return *this->pointer_; |
---|
145 | } |
---|
146 | |
---|
147 | inline bool operator!() const |
---|
148 | { |
---|
149 | return (this->pointer_ == 0); |
---|
150 | } |
---|
151 | |
---|
152 | inline void swap(SmartPtr& other) |
---|
153 | { |
---|
154 | { |
---|
155 | T* temp = this->pointer_; |
---|
156 | this->pointer_ = other.pointer_; |
---|
157 | other.pointer_ = temp; |
---|
158 | } |
---|
159 | { |
---|
160 | OrxonoxClass* temp = this->base_; |
---|
161 | this->base_ = other.base_; |
---|
162 | other.base_ = temp; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | inline void reset() |
---|
167 | { |
---|
168 | SmartPtr().swap(*this); |
---|
169 | } |
---|
170 | |
---|
171 | private: |
---|
172 | T* pointer_; |
---|
173 | OrxonoxClass* base_; |
---|
174 | }; |
---|
175 | |
---|
176 | template <class T> |
---|
177 | void swap(SmartPtr<T>& a, SmartPtr<T>& b) |
---|
178 | { |
---|
179 | a.swap(b); |
---|
180 | } |
---|
181 | |
---|
182 | template <class T, class U> |
---|
183 | SmartPtr<T> static_pointer_cast(const SmartPtr<U>& p) |
---|
184 | { |
---|
185 | return static_cast<T*>(p.get()); |
---|
186 | } |
---|
187 | |
---|
188 | template <class T, class U> |
---|
189 | SmartPtr<T> const_pointer_cast(const SmartPtr<U>& p) |
---|
190 | { |
---|
191 | return const_cast<T*>(p.get()); |
---|
192 | } |
---|
193 | |
---|
194 | template <class T, class U> |
---|
195 | SmartPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p) |
---|
196 | { |
---|
197 | return orxonox_cast<T*>(p.get()); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | #endif /* _SmartPtr_H__ */ |
---|