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 | #ifndef _SharedPtr_H__ |
---|
30 | #define _SharedPtr_H__ |
---|
31 | |
---|
32 | #include "CorePrereqs.h" |
---|
33 | |
---|
34 | namespace orxonox |
---|
35 | { |
---|
36 | template <class T> |
---|
37 | class SharedPtr |
---|
38 | { |
---|
39 | public: |
---|
40 | inline SharedPtr() : pointer_(0) |
---|
41 | { |
---|
42 | // COUT(0) << "SharedPtr (1): " << this->pointer_ << std::endl; |
---|
43 | } |
---|
44 | |
---|
45 | inline SharedPtr(T* pointer) : pointer_(pointer) |
---|
46 | { |
---|
47 | // COUT(0) << "SharedPtr (2): " << this->pointer_ << std::endl; |
---|
48 | if (this->pointer_) |
---|
49 | this->pointer_->incrementReferenceCount(); |
---|
50 | } |
---|
51 | |
---|
52 | inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_) |
---|
53 | { |
---|
54 | // COUT(0) << "SharedPtr (3): " << this->pointer_ << std::endl; |
---|
55 | if (this->pointer_) |
---|
56 | this->pointer_->incrementReferenceCount(); |
---|
57 | } |
---|
58 | |
---|
59 | inline ~SharedPtr() |
---|
60 | { |
---|
61 | // COUT(0) << "~SharedPtr: " << this->pointer_ << std::endl; |
---|
62 | if (this->pointer_) |
---|
63 | this->pointer_->decrementReferenceCount(); |
---|
64 | } |
---|
65 | |
---|
66 | inline const SharedPtr& operator=(const SharedPtr& other) |
---|
67 | { |
---|
68 | // COUT(0) << "SharedPtr=" << std::endl; |
---|
69 | SharedPtr(other).swap(*this); |
---|
70 | return *this; |
---|
71 | } |
---|
72 | |
---|
73 | inline void cast(const SharedPtr& other) |
---|
74 | { |
---|
75 | // COUT(0) << "SharedPtr cast" << std::endl; |
---|
76 | SharedPtr(other).swap(*this); |
---|
77 | } |
---|
78 | |
---|
79 | inline T* operator->() const |
---|
80 | { |
---|
81 | assert(this->pointer_ != 0); |
---|
82 | return this->pointer_; |
---|
83 | } |
---|
84 | |
---|
85 | inline T& operator*() const |
---|
86 | { |
---|
87 | assert(this->pointer_ != 0); |
---|
88 | return *this->pointer_; |
---|
89 | } |
---|
90 | |
---|
91 | inline operator bool() const |
---|
92 | { |
---|
93 | return (this->pointer_ != 0); |
---|
94 | } |
---|
95 | |
---|
96 | inline void swap(SharedPtr& other) |
---|
97 | { |
---|
98 | T* temp = this->pointer_; |
---|
99 | this->pointer_ = other.pointer_; |
---|
100 | other.pointer_ = temp; |
---|
101 | } |
---|
102 | |
---|
103 | private: |
---|
104 | T* pointer_; |
---|
105 | }; |
---|
106 | |
---|
107 | template <class T, class Parent> |
---|
108 | class SharedChildPtr : public SharedPtr<Parent> |
---|
109 | { |
---|
110 | public: |
---|
111 | inline SharedChildPtr() : SharedPtr<Parent>() {} |
---|
112 | inline SharedChildPtr(T* pointer) : SharedPtr<Parent>(pointer) {} |
---|
113 | inline SharedChildPtr(const SharedChildPtr& other) : SharedPtr<Parent>(other) {} |
---|
114 | inline const SharedChildPtr& operator=(const SharedChildPtr& other) { SharedPtr<Parent>::operator=(other); return *this; } |
---|
115 | inline T* operator->() const { return static_cast<T*>(SharedPtr<Parent>::operator->()); } |
---|
116 | inline T& operator*() const { return *static_cast<T*>(SharedPtr<Parent>::operator->()); } |
---|
117 | }; |
---|
118 | } |
---|
119 | |
---|
120 | #endif /* _SharedPtr_H__ */ |
---|