Nebula
Loading...
Searching...
No Matches
refcounted.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
23#include "core/types.h"
24#include "core/rtti.h"
25#include "core/factory.h"
27#include "core/refcountedlist.h"
28
29#if NEBULA_DEBUG
31#include "util/dictionary.h"
32#endif
33
34//------------------------------------------------------------------------------
35namespace Core
36{
38{
40public:
42 RefCounted();
44 int GetRefCount() const;
46 void AddRef();
48 void Release();
50 bool IsInstanceOf(const Rtti& rtti) const;
52 bool IsInstanceOf(const Util::String& className) const;
54 bool IsInstanceOf(const Util::FourCC& classFourCC) const;
56 bool IsA(const Rtti& rtti) const;
58 bool IsA(const Util::String& rttiName) const;
60 bool IsA(const Util::FourCC& rttiFourCC) const;
62 const Util::String& GetClassName() const;
66 static void DumpRefCountingLeaks();
67
68 #if NEBULA_DEBUG
69 struct Stats
70 {
71 Util::String className;
72 Util::FourCC classFourCC;
73 SizeT numObjects;
74 SizeT overallRefCount;
75 SizeT instanceSize;
76 };
78 static Util::Dictionary<Util::String,Stats> GetOverallStats();
79 #endif
80
81protected:
83 virtual ~RefCounted();
84
85private:
86 volatile int refCount;
87
88 #if NEBULA_DEBUG
89protected:
90 static ThreadLocal bool isInCreate;
91 static Threading::CriticalSection criticalSection;
92private:
93 bool destroyed;
94 static RefCountedList list;
95 RefCountedList::Iterator listIterator;
96public:
98 void SetDebugName(const Util::String& name);
99 #endif
100};
101
102//------------------------------------------------------------------------------
105inline
107 refCount(0)
108{
109 #if NEBULA_DEBUG
110 n_assert2(this->isInCreate, "RefCounted objects must be created with Create()!");
111 this->listIterator = list.AddBack(this);
112 this->destroyed = false;
113 #endif
114}
115
116//------------------------------------------------------------------------------
120inline void
125
126//------------------------------------------------------------------------------
130inline void
132{
134 {
135 delete this;
136 }
137}
138
139//------------------------------------------------------------------------------
143inline int
145{
146 return this->refCount;
147}
148
149//------------------------------------------------------------------------------
152inline bool
154{
155 return this->GetRtti() == &other;
156}
157
158//------------------------------------------------------------------------------
161inline bool
163{
164 return this->GetRtti()->GetName() == other;
165}
166
167//------------------------------------------------------------------------------
170inline bool
172{
173 return this->GetRtti()->GetFourCC() == other;
174}
175
176//------------------------------------------------------------------------------
179inline bool
180RefCounted::IsA(const Rtti& other) const
181{
182 return this->GetRtti()->IsDerivedFrom(other);
183}
184
185//------------------------------------------------------------------------------
188inline bool
189RefCounted::IsA(const Util::String& other) const
190{
191 return this->GetRtti()->IsDerivedFrom(other);
192}
193
194//------------------------------------------------------------------------------
197inline bool
198RefCounted::IsA(const Util::FourCC& other) const
199{
200 return this->GetRtti()->IsDerivedFrom(other);
201}
202
203//------------------------------------------------------------------------------
207inline const Util::String&
209{
210 return this->GetRtti()->GetName();
211}
212
213//------------------------------------------------------------------------------
217inline Util::FourCC
219{
220 return this->GetRtti()->GetFourCC();
221}
222
223#if NEBULA_DEBUG
224//------------------------------------------------------------------------------
227inline void
228RefCounted::SetDebugName(const Util::String& name)
229{
230 list.SetDebugName(this, name);
231}
232#endif
233
234} // namespace Core
235//------------------------------------------------------------------------------
236
237
238
239
The common base class of Nebula.
Definition refcounted.h:38
void AddRef()
increment refcount by one
Definition refcounted.h:121
RefCounted()
constructor
Definition refcounted.h:106
bool IsA(const Rtti &rtti) const
return true if this object is instance of given class, or a derived class
Definition refcounted.h:180
int GetRefCount() const
get the current refcount
Definition refcounted.h:144
void Release()
decrement refcount and destroy object if refcount is zero
Definition refcounted.h:131
static void DumpRefCountingLeaks()
dump refcounting leaks, call at end of application (NEBULA_DEBUG builds only!)
Definition refcounted.cc:45
bool IsInstanceOf(const Rtti &rtti) const
return true if this object is instance of given class
Definition refcounted.h:153
virtual ~RefCounted()
destructor (called when refcount reaches zero)
Definition refcounted.cc:25
__DeclareClass(RefCounted)
volatile int refCount
Definition refcounted.h:86
Util::FourCC GetClassFourCC() const
get the class FourCC code
Definition refcounted.h:218
const Util::String & GetClassName() const
get the class name
Definition refcounted.h:208
Implements a static list which keeps track of all refcounted objects to detect refcounting leaks at a...
Definition refcountedlist.h:24
Nebula's runtime type information for one class.
Definition rtti.h:27
const Util::String & GetName() const
get class name
Definition rtti.h:100
Critical section objects are used to protect a portion of code from parallel execution.
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
A four-character-code is a quasi-human-readable 32-bit-id.
Definition fourcc.h:19
#define n_assert2(exp, msg)
Definition debug.h:51
Definition coreserver.cc:11
int Decrement(int volatile *var)
interlocked decrement, return result
Definition gccinterlocked.cc:157
int Increment(int volatile *var)
interlocked increment, return result
Definition gccinterlocked.cc:148
Nebula's universal string class.
Definition string.h:50
int SizeT
Definition types.h:49