Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/surfaceraceHS14/src/libraries/core/object/Destroyable.h @ 10158

Last change on this file since 10158 was 9944, checked in by landauf, 11 years ago

no Thilo, we don't want to call preDestroy()
made documentation of destroy() and preDestroy() more explicit.

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