[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> |
---|
[5804] | 37 | #include <ostream> |
---|
[5807] | 38 | #include "OrxonoxClass.h" |
---|
[5804] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | template <class T> |
---|
| 43 | class SmartPtr |
---|
| 44 | { |
---|
| 45 | public: |
---|
[5805] | 46 | inline SmartPtr() : pointer_(0), base_(0) |
---|
[5804] | 47 | { |
---|
| 48 | } |
---|
| 49 | |
---|
[5805] | 50 | inline SmartPtr(int) : pointer_(0), base_(0) |
---|
[5804] | 51 | { |
---|
| 52 | } |
---|
| 53 | |
---|
[5805] | 54 | inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer) |
---|
[5804] | 55 | { |
---|
[5805] | 56 | if (this->base_ && bAddRef) |
---|
| 57 | this->base_->incrementReferenceCount(); |
---|
[5804] | 58 | } |
---|
| 59 | |
---|
[5805] | 60 | inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_) |
---|
| 61 | { |
---|
| 62 | if (this->base_) |
---|
| 63 | this->base_->incrementReferenceCount(); |
---|
| 64 | } |
---|
| 65 | |
---|
[5804] | 66 | template <class O> |
---|
[5805] | 67 | inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_) |
---|
[5804] | 68 | { |
---|
[5805] | 69 | if (this->base_) |
---|
| 70 | this->base_->incrementReferenceCount(); |
---|
[5804] | 71 | } |
---|
| 72 | |
---|
| 73 | inline ~SmartPtr() |
---|
| 74 | { |
---|
[5805] | 75 | if (this->base_) |
---|
| 76 | this->base_->decrementReferenceCount(); |
---|
[5804] | 77 | } |
---|
[5805] | 78 | |
---|
| 79 | inline const SmartPtr& operator=(int) |
---|
| 80 | { |
---|
| 81 | SmartPtr(0).swap(*this); |
---|
| 82 | return *this; |
---|
| 83 | } |
---|
[5804] | 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 |
---|
[5805] | 110 | { |
---|
[5804] | 111 | return this->pointer_; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | inline T* operator->() const |
---|
| 115 | { |
---|
[5807] | 116 | assert(this->pointer_ != 0); |
---|
[5804] | 117 | return this->pointer_; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | inline T& operator*() const |
---|
| 121 | { |
---|
[5807] | 122 | assert(this->pointer_ != 0); |
---|
[5804] | 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 | { |
---|
[5805] | 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 | } |
---|
[5804] | 143 | } |
---|
| 144 | |
---|
| 145 | inline void reset() |
---|
| 146 | { |
---|
| 147 | SmartPtr().swap(*this); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | private: |
---|
| 151 | T* pointer_; |
---|
[5805] | 152 | OrxonoxClass* base_; |
---|
[5804] | 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__ */ |
---|