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