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