[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 | |
---|
[7268] | 38 | #include "Identifier.h" |
---|
[5807] | 39 | #include "OrxonoxClass.h" |
---|
[5823] | 40 | #include "WeakPtr.h" |
---|
[5804] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | template <class T> |
---|
| 45 | class SmartPtr |
---|
| 46 | { |
---|
| 47 | public: |
---|
[5805] | 48 | inline SmartPtr() : pointer_(0), base_(0) |
---|
[5804] | 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
[5805] | 52 | inline SmartPtr(int) : pointer_(0), base_(0) |
---|
[5804] | 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
[5805] | 56 | inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer) |
---|
[5804] | 57 | { |
---|
[5805] | 58 | if (this->base_ && bAddRef) |
---|
| 59 | this->base_->incrementReferenceCount(); |
---|
[5804] | 60 | } |
---|
| 61 | |
---|
[5805] | 62 | inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_) |
---|
| 63 | { |
---|
| 64 | if (this->base_) |
---|
| 65 | this->base_->incrementReferenceCount(); |
---|
| 66 | } |
---|
| 67 | |
---|
[5804] | 68 | template <class O> |
---|
[5805] | 69 | inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_) |
---|
[5804] | 70 | { |
---|
[5805] | 71 | if (this->base_) |
---|
| 72 | this->base_->incrementReferenceCount(); |
---|
[5804] | 73 | } |
---|
| 74 | |
---|
[5823] | 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 | |
---|
[5804] | 82 | inline ~SmartPtr() |
---|
| 83 | { |
---|
[5805] | 84 | if (this->base_) |
---|
| 85 | this->base_->decrementReferenceCount(); |
---|
[5804] | 86 | } |
---|
[6417] | 87 | |
---|
[7268] | 88 | inline SmartPtr& operator=(int) |
---|
[5805] | 89 | { |
---|
| 90 | SmartPtr(0).swap(*this); |
---|
| 91 | return *this; |
---|
| 92 | } |
---|
[5804] | 93 | |
---|
[7268] | 94 | inline SmartPtr& operator=(T* pointer) |
---|
[5804] | 95 | { |
---|
| 96 | SmartPtr(pointer).swap(*this); |
---|
| 97 | return *this; |
---|
| 98 | } |
---|
| 99 | |
---|
[7268] | 100 | inline SmartPtr& operator=(const SmartPtr& other) |
---|
[5804] | 101 | { |
---|
| 102 | SmartPtr(other).swap(*this); |
---|
| 103 | return *this; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | template <class O> |
---|
[7268] | 107 | inline SmartPtr& operator=(const SmartPtr<O>& other) |
---|
[5804] | 108 | { |
---|
| 109 | SmartPtr(other).swap(*this); |
---|
| 110 | return *this; |
---|
| 111 | } |
---|
| 112 | |
---|
[5823] | 113 | template <class O> |
---|
[7268] | 114 | inline SmartPtr& operator=(const WeakPtr<O>& other) |
---|
[5823] | 115 | { |
---|
| 116 | SmartPtr(other).swap(*this); |
---|
| 117 | return *this; |
---|
| 118 | } |
---|
| 119 | |
---|
[5804] | 120 | inline T* get() const |
---|
| 121 | { |
---|
| 122 | return this->pointer_; |
---|
| 123 | } |
---|
| 124 | |
---|
[5823] | 125 | inline OrxonoxClass* getBase() const |
---|
| 126 | { |
---|
| 127 | return this->base_; |
---|
| 128 | } |
---|
| 129 | |
---|
[5804] | 130 | inline operator T*() const |
---|
[5805] | 131 | { |
---|
[5804] | 132 | return this->pointer_; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | inline T* operator->() const |
---|
| 136 | { |
---|
[5807] | 137 | assert(this->pointer_ != 0); |
---|
[5804] | 138 | return this->pointer_; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | inline T& operator*() const |
---|
| 142 | { |
---|
[5807] | 143 | assert(this->pointer_ != 0); |
---|
[5804] | 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 | { |
---|
[5805] | 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 | } |
---|
[5804] | 164 | } |
---|
| 165 | |
---|
| 166 | inline void reset() |
---|
| 167 | { |
---|
| 168 | SmartPtr().swap(*this); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | private: |
---|
| 172 | T* pointer_; |
---|
[5805] | 173 | OrxonoxClass* base_; |
---|
[5804] | 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 | { |
---|
[7268] | 197 | return orxonox_cast<T*>(p.get()); |
---|
[5804] | 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | #endif /* _SmartPtr_H__ */ |
---|