[7192] | 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 | |
---|
[7205] | 32 | #include "UtilPrereqs.h" |
---|
[7196] | 33 | #include <algorithm> |
---|
[7197] | 34 | #include <cassert> |
---|
[7192] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
[7196] | 38 | class SharedPtrDestroyer |
---|
| 39 | { |
---|
| 40 | public: |
---|
| 41 | virtual void destroy() = 0; |
---|
| 42 | }; |
---|
| 43 | |
---|
[7192] | 44 | template <class T> |
---|
[7196] | 45 | class SharedPtrDestroyerImpl : public SharedPtrDestroyer |
---|
| 46 | { |
---|
| 47 | public: |
---|
| 48 | SharedPtrDestroyerImpl(T* pointer) : pointer_(pointer) {} |
---|
| 49 | |
---|
| 50 | void destroy() |
---|
| 51 | { |
---|
| 52 | delete this->pointer_; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | private: |
---|
| 56 | T* pointer_; |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | template <class T> |
---|
[7192] | 60 | class SharedPtr |
---|
| 61 | { |
---|
[7196] | 62 | template <class O> |
---|
| 63 | friend class SharedPtr; |
---|
| 64 | |
---|
[7192] | 65 | public: |
---|
[7196] | 66 | inline SharedPtr() : pointer_(0), counter_(0), destroyer_(0) |
---|
[7192] | 67 | { |
---|
| 68 | } |
---|
| 69 | |
---|
[7196] | 70 | inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0), destroyer_(0) |
---|
[7192] | 71 | { |
---|
| 72 | if (this->pointer_) |
---|
[7196] | 73 | { |
---|
| 74 | this->counter_ = new int(1); |
---|
| 75 | this->destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_); |
---|
| 76 | } |
---|
[7192] | 77 | } |
---|
| 78 | |
---|
[7196] | 79 | inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_) |
---|
[7192] | 80 | { |
---|
| 81 | if (this->pointer_) |
---|
[7196] | 82 | ++(*this->counter_); |
---|
[7192] | 83 | } |
---|
| 84 | |
---|
[7196] | 85 | template <class O> |
---|
| 86 | inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_) |
---|
| 87 | { |
---|
| 88 | if (this->pointer_) |
---|
| 89 | ++(*this->counter_); |
---|
| 90 | } |
---|
| 91 | |
---|
[7192] | 92 | inline ~SharedPtr() |
---|
| 93 | { |
---|
| 94 | if (this->pointer_) |
---|
[7196] | 95 | { |
---|
| 96 | --(*this->counter_); |
---|
| 97 | |
---|
| 98 | if (*this->counter_ == 0) |
---|
| 99 | { |
---|
| 100 | this->destroyer_->destroy(); |
---|
| 101 | delete this->destroyer_; |
---|
| 102 | delete this->counter_; |
---|
| 103 | } |
---|
| 104 | } |
---|
[7192] | 105 | } |
---|
| 106 | |
---|
| 107 | inline const SharedPtr& operator=(const SharedPtr& other) |
---|
| 108 | { |
---|
| 109 | SharedPtr(other).swap(*this); |
---|
| 110 | return *this; |
---|
| 111 | } |
---|
| 112 | |
---|
[7196] | 113 | template <class O> |
---|
| 114 | inline const SharedPtr& operator=(const SharedPtr<O>& other) |
---|
[7192] | 115 | { |
---|
| 116 | SharedPtr(other).swap(*this); |
---|
[7196] | 117 | return *this; |
---|
[7192] | 118 | } |
---|
| 119 | |
---|
[7196] | 120 | template <class O> |
---|
| 121 | inline SharedPtr<O> cast() const |
---|
| 122 | { |
---|
| 123 | O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast |
---|
| 124 | return SharedPtr<O>(temp, this->counter_, this->destroyer_); |
---|
| 125 | } |
---|
| 126 | |
---|
[7192] | 127 | inline T* operator->() const |
---|
| 128 | { |
---|
| 129 | assert(this->pointer_ != 0); |
---|
| 130 | return this->pointer_; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | inline T& operator*() const |
---|
| 134 | { |
---|
| 135 | assert(this->pointer_ != 0); |
---|
| 136 | return *this->pointer_; |
---|
| 137 | } |
---|
| 138 | |
---|
[7212] | 139 | inline T* get() const |
---|
| 140 | { |
---|
| 141 | return this->pointer_; |
---|
| 142 | } |
---|
| 143 | |
---|
[7192] | 144 | inline operator bool() const |
---|
| 145 | { |
---|
| 146 | return (this->pointer_ != 0); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | inline void swap(SharedPtr& other) |
---|
| 150 | { |
---|
[7196] | 151 | std::swap(this->pointer_, other.pointer_); |
---|
| 152 | std::swap(this->counter_, other.counter_); |
---|
| 153 | std::swap(this->destroyer_, other.destroyer_); |
---|
[7192] | 154 | } |
---|
| 155 | |
---|
| 156 | private: |
---|
[7196] | 157 | inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer) |
---|
| 158 | { |
---|
| 159 | if (this->pointer_) |
---|
| 160 | ++(*this->counter_); |
---|
| 161 | } |
---|
| 162 | |
---|
[7192] | 163 | T* pointer_; |
---|
[7196] | 164 | int* counter_; |
---|
| 165 | SharedPtrDestroyer* destroyer_; |
---|
[7192] | 166 | }; |
---|
[7197] | 167 | |
---|
[7192] | 168 | template <class T, class Parent> |
---|
[7197] | 169 | class SharedChildPtr : public Parent |
---|
[7192] | 170 | { |
---|
| 171 | public: |
---|
[7197] | 172 | inline SharedChildPtr() : Parent() {} |
---|
| 173 | inline SharedChildPtr(T* pointer) : Parent(pointer) {} |
---|
| 174 | inline SharedChildPtr(const SharedPtr<T>& other) : Parent(other) {} |
---|
| 175 | |
---|
| 176 | inline T* operator->() const { return static_cast<T*>(Parent::operator->()); } |
---|
| 177 | inline T& operator*() const { return *static_cast<T*>(Parent::operator->()); } |
---|
[7192] | 178 | }; |
---|
[7197] | 179 | |
---|
[7192] | 180 | } |
---|
| 181 | |
---|
| 182 | #endif /* _SharedPtr_H__ */ |
---|