Orxonox  0.0.5 Codename: Arcturus
Destroyable.h
Go to the documentation of this file.
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 
35 #ifndef _Destroyable_H__
36 #define _Destroyable_H__
37 
38 #include "core/CorePrereqs.h"
39 
40 #include <set>
41 
42 namespace orxonox
43 {
48  {
49  template <class T>
50  friend class StrongPtr;
51 
52  friend class DestructionListener;
53 
54  public:
55  Destroyable();
56  virtual ~Destroyable();
57 
58  void destroy();
59  void destroyLater();
60 
62  inline unsigned int getReferenceCount() const
63  { return this->referenceCount_; }
64 
65  protected:
68  virtual void preDestroy() {}
69 
70  private:
73  { ++this->referenceCount_; }
76  {
77  --this->referenceCount_;
78  if (this->referenceCount_ == 0 && this->requestedDestruction_)
79  this->destroy();
80  }
81 
84  { this->destructionListeners_.insert(pointer); }
87  { this->destructionListeners_.erase(pointer); }
88 
91  std::set<DestructionListener*> destructionListeners_;
92  };
93 
98  {
99  friend class Destroyable;
100 
101  protected:
102  virtual ~DestructionListener() {}
103 
105  { if (object) { object->registerDestructionListener(this); } }
107  { if (object) { object->unregisterDestructionListener(this); } }
108 
109  virtual void objectDeleted() = 0;
110  };
111 
112 }
113 
114 #endif /* _Destroyable_H__ */
std::set< DestructionListener * > destructionListeners_
All destruction listeners (for example weak pointers which point to this object and like to get notif...
Definition: Destroyable.h:91
void decrementReferenceCount()
Decrements the reference counter (for strong pointers).
Definition: Destroyable.h:75
virtual void preDestroy()
This virtual function is called if destroy() is called and no StrongPtr points to this object...
Definition: Destroyable.h:68
Shared library macros, enums, constants and forward declarations for the core library ...
A strong pointer which wraps a pointer to an object and keeps this object alive as long as the strong...
Definition: CorePrereqs.h:227
void unregisterAsDestructionListener(Destroyable *object)
Definition: Destroyable.h:106
void registerAsDestructionListener(Destroyable *object)
Definition: Destroyable.h:104
void registerDestructionListener(DestructionListener *pointer)
Register a destruction listener (for example a weak pointer which points to this object).
Definition: Destroyable.h:83
void unregisterDestructionListener(DestructionListener *pointer)
Unegister a destruction listener (for example a weak pointer which pointed to this object before)...
Definition: Destroyable.h:86
bool requestedDestruction_
Becomes true after someone called delete on this object.
Definition: Destroyable.h:90
The MetaPickup destroys all the PickupCarriers&#39; Pickupables.
unsigned int getReferenceCount() const
Returns the number of strong pointers that point to this object.
Definition: Destroyable.h:62
void incrementReferenceCount()
Increments the reference counter (for strong pointers).
Definition: Destroyable.h:72
virtual ~DestructionListener()
Definition: Destroyable.h:102
int referenceCount_
Counts the references from strong pointers to this object.
Definition: Destroyable.h:89
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
#define _CoreExport
Definition: CorePrereqs.h:61
This listener is used to inform weak pointers if an object of type Destroyable gets destroyed...
Definition: Destroyable.h:97
Classes must inherit from this class if they should be used with StrongPtr or WeakPtr.
Definition: Destroyable.h:47