[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 | |
---|
[7401] | 29 | /** |
---|
| 30 | @defgroup SharedPtr SharedPtr<T> |
---|
| 31 | @ingroup Util |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | @file |
---|
| 36 | @ingroup SharedPtr |
---|
| 37 | @brief Definition of the SharedPtr template that is used to manage pointers. |
---|
| 38 | |
---|
| 39 | @anchor SharedPtrExample |
---|
| 40 | |
---|
| 41 | The orxonox::SharedPtr template can be used to manage a pointer to an object |
---|
| 42 | that was created with new. The SharedPtr acts like the pointer itself, but it |
---|
| 43 | keeps track of the number of references to it. If all references are removed, |
---|
| 44 | SharedPtr deletes the managed object automatically. |
---|
| 45 | |
---|
| 46 | Example: |
---|
| 47 | |
---|
| 48 | Classic implementation using new and delete: |
---|
| 49 | @code |
---|
| 50 | void someFunction() |
---|
| 51 | { |
---|
| 52 | MyClass* object = new MyClass(); // Create a new instance of MyClass |
---|
| 53 | |
---|
| 54 | object->myFunction(); // Calls MyClass::myFunction() |
---|
| 55 | |
---|
| 56 | delete object; // Delete the object at the end of the scope |
---|
| 57 | } |
---|
| 58 | @endcode |
---|
| 59 | |
---|
| 60 | The same function using SharedPtr: |
---|
| 61 | @code |
---|
| 62 | void someFunction() |
---|
| 63 | { |
---|
| 64 | SharedPtr<MyClass> object = new MyClass(); // Create a new instance of MyClass and store its pointer in a SharedPtr |
---|
| 65 | |
---|
| 66 | object->myFunction(); // Calls MyClass::myFunction() |
---|
| 67 | |
---|
| 68 | } // At the end of the scope, the SharedPtr is destroyed. Because no other SharedPtrs |
---|
| 69 | // point at the object, the object itself is also destroyed automatically |
---|
| 70 | @endcode |
---|
| 71 | |
---|
| 72 | This is especially handy if you do not know what will happen with an object that was |
---|
| 73 | created with new, for example if you pass it to another object. If multiple instances |
---|
| 74 | share a pointer to the same object, none of these instances can delete the object |
---|
| 75 | without interfering with the other instances. But if none of the instances destroy the |
---|
| 76 | object, it will never be destroyed and results in a memory leak. With a SharedPtr |
---|
| 77 | however you don't have to think about destroying the object, because the SharedPtr |
---|
| 78 | itself keeps track of the references. |
---|
| 79 | |
---|
| 80 | Example: |
---|
| 81 | |
---|
| 82 | Classic implementation using new and delete: |
---|
| 83 | @code |
---|
| 84 | class OtherClass // Declaration of some class |
---|
| 85 | { |
---|
| 86 | public: |
---|
| 87 | OtherClass(MyClass* object) // Constructor |
---|
| 88 | { |
---|
| 89 | this->object_ = object; // Assigns the pointer to the member variable object_ |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | ~OtherClass() // Destructor |
---|
| 93 | { |
---|
| 94 | ??? // What to do with object_? |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | private: |
---|
| 98 | MyClass* object_; // A pointer to the object |
---|
| 99 | }; |
---|
| 100 | |
---|
| 101 | void someFunction() |
---|
| 102 | { |
---|
| 103 | MyClass* object = new MyClass(); // Create a new instance of MyClass |
---|
| 104 | |
---|
| 105 | OtherClass* other1 = new OtherClass(object); // Create an instance of OtherClass and pass the object pointer |
---|
| 106 | OtherClass* other2 = new OtherClass(object); // " |
---|
| 107 | OtherClass* other3 = new OtherClass(object); // " |
---|
| 108 | |
---|
| 109 | ??? // What happens with object now? |
---|
| 110 | } |
---|
| 111 | @endcode |
---|
| 112 | |
---|
| 113 | If you use SharedPtr<MyClass> instead of a classic MyClass* pointer, the instance of |
---|
| 114 | MyClass would be automatically destroyed if all instances of OtherClass are destroyed. |
---|
| 115 | You don't need any code in the destructor and you can completely forget about the |
---|
| 116 | object, because its managed by the SharedPtr. |
---|
| 117 | |
---|
| 118 | The same code using SharedPtr: |
---|
| 119 | @code |
---|
| 120 | class OtherClass // Declaration of some class |
---|
| 121 | { |
---|
| 122 | public: |
---|
| 123 | OtherClass(const SharedPtr<MyClass>& object) // Constructor |
---|
| 124 | { |
---|
| 125 | this->object_ = object; // Assigns the pointer to the member variable object_ |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | private: |
---|
| 129 | SharedPtr<MyClass> object_; // A SharedPtr to the object |
---|
| 130 | }; |
---|
| 131 | |
---|
| 132 | void someFunction() |
---|
| 133 | { |
---|
| 134 | SharedPtr<MyClass> object = new MyClass(); // Create a new instance of MyClass |
---|
| 135 | |
---|
| 136 | OtherClass* other1 = new OtherClass(object); // Create an instance of OtherClass and pass the object pointer |
---|
| 137 | OtherClass* other2 = new OtherClass(object); // " |
---|
| 138 | OtherClass* other3 = new OtherClass(object); // " |
---|
| 139 | |
---|
[10624] | 140 | } // The SharedPtr "object" is destroyed at the end of the scope, |
---|
[7401] | 141 | // but the three instances of OtherClass keep the object alive |
---|
| 142 | // until they are all destroyed. |
---|
| 143 | @endcode |
---|
| 144 | */ |
---|
| 145 | |
---|
[7192] | 146 | #ifndef _SharedPtr_H__ |
---|
| 147 | #define _SharedPtr_H__ |
---|
| 148 | |
---|
[7205] | 149 | #include "UtilPrereqs.h" |
---|
[7264] | 150 | |
---|
[7196] | 151 | #include <algorithm> |
---|
[7197] | 152 | #include <cassert> |
---|
[7192] | 153 | |
---|
[7264] | 154 | #include "SmallObjectAllocator.h" |
---|
| 155 | |
---|
[7192] | 156 | namespace orxonox |
---|
| 157 | { |
---|
[7401] | 158 | namespace detail |
---|
[7196] | 159 | { |
---|
[7401] | 160 | /// BaseClass of SharedCounterImpl, has a counter that is initialized with 1 |
---|
| 161 | class SharedCounter |
---|
| 162 | { |
---|
| 163 | public: |
---|
| 164 | SharedCounter() : count_(1) {} |
---|
[8351] | 165 | virtual ~SharedCounter() {} |
---|
[7401] | 166 | virtual void destroy() = 0; |
---|
[7264] | 167 | |
---|
[7401] | 168 | int count_; |
---|
| 169 | }; |
---|
[7196] | 170 | |
---|
[7401] | 171 | /// Child class of SharedCounter, keeps a pointer to an object of type T that can be destroyed with destroy() |
---|
| 172 | template <class T> |
---|
| 173 | class SharedCounterImpl : public SharedCounter |
---|
| 174 | { |
---|
| 175 | public: |
---|
| 176 | SharedCounterImpl(T* pointer) : pointer_(pointer) {} |
---|
[7196] | 177 | |
---|
[7401] | 178 | void destroy() |
---|
| 179 | { |
---|
| 180 | delete this->pointer_; |
---|
| 181 | } |
---|
[7196] | 182 | |
---|
[7401] | 183 | private: |
---|
| 184 | T* pointer_; |
---|
| 185 | }; |
---|
[7196] | 186 | |
---|
[7401] | 187 | _UtilExport SmallObjectAllocator& createSharedCounterPool(); |
---|
[7264] | 188 | |
---|
[8351] | 189 | ORX_FORCEINLINE SmallObjectAllocator& getSharedCounterPool() |
---|
[7401] | 190 | { |
---|
| 191 | static SmallObjectAllocator& instance = createSharedCounterPool(); |
---|
| 192 | return instance; |
---|
| 193 | } |
---|
[7264] | 194 | } |
---|
| 195 | |
---|
[7401] | 196 | /** |
---|
| 197 | @brief The SharedPtr template is a utility to manage pointers to an object. |
---|
| 198 | @param T The type of the managed object |
---|
| 199 | |
---|
| 200 | SharedPtr acts like a real pointer, except that it keeps track of the number of |
---|
| 201 | references to the object. If the the number of references drops to zero, the |
---|
| 202 | object is destroyed automatically. |
---|
| 203 | |
---|
| 204 | @see See @ref SharedPtrExample "this description" for some examples and more information. |
---|
| 205 | |
---|
| 206 | @note The number of references is stored in a separate object that is shared |
---|
| 207 | among all instances of SharedPtr that point to the same pointer. This object is |
---|
| 208 | also responsible for destroying the pointer if the reference counter becomes zero. |
---|
| 209 | */ |
---|
[7196] | 210 | template <class T> |
---|
[7192] | 211 | class SharedPtr |
---|
| 212 | { |
---|
[7196] | 213 | template <class O> |
---|
| 214 | friend class SharedPtr; |
---|
| 215 | |
---|
[7192] | 216 | public: |
---|
[10765] | 217 | /// Default constructor, the pointer is set to nullptr. |
---|
[10768] | 218 | inline SharedPtr() : pointer_(nullptr), counter_(nullptr) |
---|
[7192] | 219 | { |
---|
| 220 | } |
---|
| 221 | |
---|
[7401] | 222 | /// Constructor, creates a SharedPtr that points to @a pointer, increments the counter. |
---|
[10768] | 223 | inline SharedPtr(T* pointer) : pointer_(pointer), counter_(nullptr) |
---|
[7192] | 224 | { |
---|
| 225 | if (this->pointer_) |
---|
[7196] | 226 | { |
---|
[7401] | 227 | void* chunk = detail::getSharedCounterPool().alloc(); |
---|
| 228 | this->counter_ = new (chunk) detail::SharedCounterImpl<T>(this->pointer_); |
---|
[7196] | 229 | } |
---|
[7192] | 230 | } |
---|
| 231 | |
---|
[7401] | 232 | /// Copy-constructor, this SharedPtr now points to the same object like the other SharedPtr, increments the counter. |
---|
[7264] | 233 | inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_) |
---|
[7192] | 234 | { |
---|
| 235 | if (this->pointer_) |
---|
[7264] | 236 | ++this->counter_->count_; |
---|
[7192] | 237 | } |
---|
| 238 | |
---|
[7401] | 239 | /// Copy-constructor for SharedPtr with another template agument, increments the counter. |
---|
[7196] | 240 | template <class O> |
---|
[7264] | 241 | inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_) |
---|
[7196] | 242 | { |
---|
| 243 | if (this->pointer_) |
---|
[7264] | 244 | ++this->counter_->count_; |
---|
[7196] | 245 | } |
---|
| 246 | |
---|
[10745] | 247 | /// Move-constructor, this SharedPtr now points to the same object like the other SharedPtr did before |
---|
| 248 | inline SharedPtr(SharedPtr&& other) : pointer_(other.pointer_), counter_(other.counter_) |
---|
| 249 | { |
---|
| 250 | other.pointer_ = nullptr; |
---|
| 251 | other.counter_ = nullptr; |
---|
| 252 | } |
---|
| 253 | |
---|
[7401] | 254 | /// Destructor, decrements the counter and deletes the object if the counter becomes zero. |
---|
[7192] | 255 | inline ~SharedPtr() |
---|
| 256 | { |
---|
| 257 | if (this->pointer_) |
---|
[7196] | 258 | { |
---|
[7264] | 259 | --this->counter_->count_; |
---|
[7196] | 260 | |
---|
[7264] | 261 | if (this->counter_->count_ == 0) |
---|
[7196] | 262 | { |
---|
[7264] | 263 | this->counter_->destroy(); |
---|
[7401] | 264 | detail::getSharedCounterPool().free(this->counter_); |
---|
[7196] | 265 | } |
---|
| 266 | } |
---|
[7192] | 267 | } |
---|
| 268 | |
---|
[7401] | 269 | /// Assigns a new object, decrements the counter of the old object, increments the counter of the new object. |
---|
[10745] | 270 | inline SharedPtr& operator=(SharedPtr other) |
---|
[7192] | 271 | { |
---|
[10745] | 272 | other.swap(*this); |
---|
[7192] | 273 | return *this; |
---|
| 274 | } |
---|
| 275 | |
---|
[7401] | 276 | /// Assigns a new object with another template argument, decrements the counter of the old object, increments the counter of the new object. |
---|
[7196] | 277 | template <class O> |
---|
[7269] | 278 | inline SharedPtr& operator=(const SharedPtr<O>& other) |
---|
[7192] | 279 | { |
---|
| 280 | SharedPtr(other).swap(*this); |
---|
[7196] | 281 | return *this; |
---|
[7192] | 282 | } |
---|
| 283 | |
---|
[7401] | 284 | /// Casts the pointer to another type |
---|
[7196] | 285 | template <class O> |
---|
| 286 | inline SharedPtr<O> cast() const |
---|
| 287 | { |
---|
| 288 | O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast |
---|
[7264] | 289 | return SharedPtr<O>(temp, this->counter_); |
---|
[7196] | 290 | } |
---|
| 291 | |
---|
[7401] | 292 | /// Overloaded -> operator, returns the pointer to the managed object. |
---|
[7192] | 293 | inline T* operator->() const |
---|
| 294 | { |
---|
[10768] | 295 | assert(this->pointer_ != nullptr); |
---|
[7192] | 296 | return this->pointer_; |
---|
| 297 | } |
---|
| 298 | |
---|
[7401] | 299 | /// Overloaded * operator, returns a reference ot the managed object. |
---|
[7192] | 300 | inline T& operator*() const |
---|
| 301 | { |
---|
[10768] | 302 | assert(this->pointer_ != nullptr); |
---|
[7192] | 303 | return *this->pointer_; |
---|
| 304 | } |
---|
| 305 | |
---|
[7401] | 306 | /// Returns the pointer to the managed object. |
---|
[7212] | 307 | inline T* get() const |
---|
| 308 | { |
---|
| 309 | return this->pointer_; |
---|
| 310 | } |
---|
| 311 | |
---|
[10765] | 312 | /// Returns true if the pointer is not nullptr. |
---|
[10770] | 313 | inline explicit operator bool() const |
---|
[7192] | 314 | { |
---|
[10768] | 315 | return (this->pointer_ != nullptr); |
---|
[7192] | 316 | } |
---|
| 317 | |
---|
[7401] | 318 | /// Swaps the pointer and the counter of two instances of SharedPtr with the same template argument. |
---|
[7192] | 319 | inline void swap(SharedPtr& other) |
---|
| 320 | { |
---|
[7196] | 321 | std::swap(this->pointer_, other.pointer_); |
---|
| 322 | std::swap(this->counter_, other.counter_); |
---|
[7192] | 323 | } |
---|
| 324 | |
---|
| 325 | private: |
---|
[7401] | 326 | /// Private constructor, used by the cast() function. |
---|
| 327 | inline SharedPtr(T* pointer, detail::SharedCounter* counter) : pointer_(pointer), counter_(counter) |
---|
[7196] | 328 | { |
---|
| 329 | if (this->pointer_) |
---|
[7264] | 330 | ++this->counter_->count_; |
---|
[7196] | 331 | } |
---|
| 332 | |
---|
[7401] | 333 | T* pointer_; ///< A pointer to the managed object of type @a T |
---|
| 334 | detail::SharedCounter* counter_; ///< A pointer to the shared reference counter |
---|
[7192] | 335 | }; |
---|
[7197] | 336 | |
---|
[7401] | 337 | /** |
---|
| 338 | @brief A child class of SharedPtr, used to reflect the hierarchy of the underlying class @a T. |
---|
| 339 | @param T The type of the managed object |
---|
| 340 | @param Parent The type of the SharedPtr that manages the parent class of @a T |
---|
| 341 | |
---|
| 342 | This class is used to reflect the hierarchy of the underlying class @a T. |
---|
| 343 | For example the @c Functor classes: While a @c Functor* pointer would be managed by |
---|
| 344 | @c SharedPtr<Functor>, the child class @c FunctorStatic is managed by the class |
---|
[10769] | 345 | <tt>SharedChildPtr<FunctorStatic, SharedPtr<Functor>></tt>. |
---|
[7401] | 346 | |
---|
| 347 | The second template argument @a Parent is used as the parent class of |
---|
| 348 | SharedChildPtr. This means that each instance of <tt>SharedChildPtr<T, Parent></tt> |
---|
| 349 | can be upcasted to @c Parent. |
---|
| 350 | |
---|
| 351 | So for example this works: |
---|
| 352 | @code |
---|
[10769] | 353 | SharedChildPtr<FunctorStatic, SharedPtr<Functor>> functorStatic = createFunctor(&MyClass::myStaticFunction); |
---|
[7401] | 354 | SharedPtr<Functor> functor = functorStatic; |
---|
| 355 | @endcode |
---|
| 356 | |
---|
| 357 | @note There are some typedefs and more to make the usage of SharedChildPtr easier |
---|
| 358 | for the classes Functor and Executor. See FunctorPtr.h and ExecutorPtr.h. The above |
---|
| 359 | example could thus be simplified the following way: |
---|
| 360 | @code |
---|
| 361 | FunctorStaticPtr functorStatic = createFunctor(&MyClass::myStaticFunction); |
---|
| 362 | FunctorPtr functor = functorStatic; |
---|
| 363 | @endcode |
---|
| 364 | |
---|
| 365 | @see See SharedPtr for more information about the base class SharedPtr. |
---|
| 366 | @see See @ref SharedPtrExample "this description" for some examples about how to use SharedPtr. |
---|
| 367 | */ |
---|
[7192] | 368 | template <class T, class Parent> |
---|
[7197] | 369 | class SharedChildPtr : public Parent |
---|
[7192] | 370 | { |
---|
| 371 | public: |
---|
[7197] | 372 | inline SharedChildPtr() : Parent() {} |
---|
| 373 | inline SharedChildPtr(T* pointer) : Parent(pointer) {} |
---|
| 374 | inline SharedChildPtr(const SharedPtr<T>& other) : Parent(other) {} |
---|
| 375 | |
---|
| 376 | inline T* operator->() const { return static_cast<T*>(Parent::operator->()); } |
---|
| 377 | inline T& operator*() const { return *static_cast<T*>(Parent::operator->()); } |
---|
[7192] | 378 | }; |
---|
[7197] | 379 | |
---|
[7192] | 380 | } |
---|
| 381 | |
---|
| 382 | #endif /* _SharedPtr_H__ */ |
---|