Irrlicht 3D Engine
IReferenceCounted.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __I_IREFERENCE_COUNTED_H_INCLUDED__
6 #define __I_IREFERENCE_COUNTED_H_INCLUDED__
7 
8 #include "irrTypes.h"
9 
10 #ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_
11  #include "leakHunter.h"
12 #endif
13 
14 namespace irr
15 {
16 
18 
46  {
47  public:
48 
51  : DebugName(0), ReferenceCounter(1)
52  {
53 #ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_
54  LeakHunter::addObject(this);
55 #endif
56  }
57 
60  {
61  #ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_
62  LeakHunter::removeObject(this);
63  #endif
64  }
65 
67 
96  void grab() const { ++ReferenceCounter; }
97 
99 
126  bool drop() const
127  {
128  // someone is doing bad reference counting.
129  _IRR_DEBUG_BREAK_IF(ReferenceCounter <= 0)
130 
131  --ReferenceCounter;
132  if (!ReferenceCounter)
133  {
134  delete this;
135  return true;
136  }
137 
138  return false;
139  }
140 
142 
144  {
145  return ReferenceCounter;
146  }
147 
149 
152  const c8* getDebugName() const
153  {
154  return DebugName;
155  }
156 
157  protected:
158 
160 
163  void setDebugName(const c8* newName)
164  {
165  DebugName = newName;
166  }
167 
168  private:
169 
171  const c8* DebugName;
172 
174  mutable s32 ReferenceCounter;
175  };
176 
177 } // end namespace irr
178 
179 #endif
180 
bool drop() const
Drops the object. Decrements the reference counter by one.
virtual ~IReferenceCounted()
Destructor.
char c8
8 bit character variable.
Definition: irrTypes.h:35
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
s32 getReferenceCount() const
Get the reference count.
IReferenceCounted()
Constructor.
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185
const c8 * getDebugName() const
Returns the debug name of the object.
void grab() const
Grabs the object. Increments the reference counter by one.
void setDebugName(const c8 *newName)
Sets the debug name of the object.
Base class of most objects of the Irrlicht Engine.