[1505] | 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 | /** |
---|
[2171] | 30 | @file |
---|
[9570] | 31 | @ingroup Object |
---|
[10624] | 32 | @brief Declaration of Destroyable, the base class of all objects which can be used with StrongPtr and WeakPtr. |
---|
[1505] | 33 | */ |
---|
| 34 | |
---|
[9570] | 35 | #ifndef _Destroyable_H__ |
---|
| 36 | #define _Destroyable_H__ |
---|
[1505] | 37 | |
---|
[9563] | 38 | #include "core/CorePrereqs.h" |
---|
[3327] | 39 | |
---|
[1505] | 40 | #include <set> |
---|
| 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | /** |
---|
[10624] | 45 | @brief Classes must inherit from this class if they should be used with StrongPtr or WeakPtr. |
---|
[1505] | 46 | */ |
---|
[9570] | 47 | class _CoreExport Destroyable |
---|
[1505] | 48 | { |
---|
[3325] | 49 | template <class T> |
---|
[10624] | 50 | friend class StrongPtr; |
---|
[5929] | 51 | |
---|
[7849] | 52 | friend class DestructionListener; |
---|
[5929] | 53 | |
---|
[1505] | 54 | public: |
---|
[9570] | 55 | Destroyable(); |
---|
| 56 | virtual ~Destroyable(); |
---|
[1505] | 57 | |
---|
[5929] | 58 | void destroy(); |
---|
[10624] | 59 | void destroyLater(); |
---|
[5929] | 60 | |
---|
[10624] | 61 | /// Returns the number of @ref orxonox::StrongPtr "strong pointers" that point to this object. |
---|
[5929] | 62 | inline unsigned int getReferenceCount() const |
---|
| 63 | { return this->referenceCount_; } |
---|
| 64 | |
---|
[6417] | 65 | protected: |
---|
[10624] | 66 | /// This virtual function is called if destroy() is called and no StrongPtr points to this object. Used in some cases to create a new StrongPtr to |
---|
[9944] | 67 | /// prevent destruction. Don't call this function directly - use destroy() instead. |
---|
[6417] | 68 | virtual void preDestroy() {} |
---|
| 69 | |
---|
[5929] | 70 | private: |
---|
[10624] | 71 | /// Increments the reference counter (for strong pointers). |
---|
[5929] | 72 | inline void incrementReferenceCount() |
---|
| 73 | { ++this->referenceCount_; } |
---|
[10624] | 74 | /// Decrements the reference counter (for strong pointers). |
---|
[5929] | 75 | inline void decrementReferenceCount() |
---|
[6417] | 76 | { |
---|
| 77 | --this->referenceCount_; |
---|
| 78 | if (this->referenceCount_ == 0 && this->requestedDestruction_) |
---|
| 79 | this->destroy(); |
---|
| 80 | } |
---|
| 81 | |
---|
[7849] | 82 | /// Register a destruction listener (for example a weak pointer which points to this object). |
---|
| 83 | inline void registerDestructionListener(DestructionListener* pointer) |
---|
| 84 | { this->destructionListeners_.insert(pointer); } |
---|
| 85 | /// Unegister a destruction listener (for example a weak pointer which pointed to this object before). |
---|
| 86 | inline void unregisterDestructionListener(DestructionListener* pointer) |
---|
| 87 | { this->destructionListeners_.erase(pointer); } |
---|
[3325] | 88 | |
---|
[10624] | 89 | int referenceCount_; //!< Counts the references from strong pointers to this object |
---|
[7849] | 90 | bool requestedDestruction_; //!< Becomes true after someone called delete on this object |
---|
| 91 | std::set<DestructionListener*> destructionListeners_; //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies) |
---|
[1505] | 92 | }; |
---|
[7163] | 93 | |
---|
[7849] | 94 | /** |
---|
[9570] | 95 | @brief This listener is used to inform weak pointers if an object of type Destroyable gets destroyed. |
---|
[7849] | 96 | */ |
---|
| 97 | class _CoreExport DestructionListener |
---|
| 98 | { |
---|
[9570] | 99 | friend class Destroyable; |
---|
[7849] | 100 | |
---|
| 101 | protected: |
---|
[8079] | 102 | virtual ~DestructionListener() {} |
---|
| 103 | |
---|
[9570] | 104 | inline void registerAsDestructionListener(Destroyable* object) |
---|
[7850] | 105 | { if (object) { object->registerDestructionListener(this); } } |
---|
[9570] | 106 | inline void unregisterAsDestructionListener(Destroyable* object) |
---|
[7850] | 107 | { if (object) { object->unregisterDestructionListener(this); } } |
---|
[7849] | 108 | |
---|
| 109 | virtual void objectDeleted() = 0; |
---|
| 110 | }; |
---|
| 111 | |
---|
[1505] | 112 | } |
---|
| 113 | |
---|
[9570] | 114 | #endif /* _Destroyable_H__ */ |
---|