Nebula
Loading...
Searching...
No Matches
refcounted.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
30#include "core/types.h"
31#include "core/rtti.h"
32#include "core/factory.h"
34#include "core/refcountedlist.h"
35
36#if NEBULA_DEBUG
38#include "util/dictionary.h"
39#endif
40
41//------------------------------------------------------------------------------
42namespace Core
43{
45{
47public:
49 RefCounted();
51 int GetRefCount() const;
53 void AddRef();
55 void Release();
57 bool IsInstanceOf(const Rtti& rtti) const;
59 bool IsInstanceOf(const Util::String& className) const;
61 bool IsInstanceOf(const Util::FourCC& classFourCC) const;
63 bool IsA(const Rtti& rtti) const;
65 bool IsA(const Util::String& rttiName) const;
67 bool IsA(const Util::FourCC& rttiFourCC) const;
69 const Util::String& GetClassName() const;
73 static void DumpRefCountingLeaks();
74
75 #if NEBULA_DEBUG
76 struct Stats
77 {
78 Util::String className;
79 Util::FourCC classFourCC;
80 SizeT numObjects;
81 SizeT overallRefCount;
82 SizeT instanceSize;
83 };
85 static Util::Dictionary<Util::String,Stats> GetOverallStats();
86 #endif
87
88protected:
90 virtual ~RefCounted();
91
92private:
93 volatile int refCount;
94
95 #if NEBULA_DEBUG
96protected:
97 static ThreadLocal bool isInCreate;
98 static Threading::CriticalSection criticalSection;
99private:
100 bool destroyed;
101 static RefCountedList list;
102 RefCountedList::Iterator listIterator;
105 void RegisterInDebugList();
106public:
108 void SetDebugName(const Util::String& name);
109 #endif
110};
111
112//------------------------------------------------------------------------------
115inline
117 refCount(0)
118{
119 #if NEBULA_DEBUG
120 n_assert2(this->isInCreate, "RefCounted objects must be created with Create()!");
121 this->destroyed = false;
122 this->RegisterInDebugList();
123 #endif
124}
125
126//------------------------------------------------------------------------------
130inline void
135
136//------------------------------------------------------------------------------
140inline void
142{
144 {
145 delete this;
146 }
147}
148
149//------------------------------------------------------------------------------
153inline int
155{
156 return this->refCount;
157}
158
159//------------------------------------------------------------------------------
162inline bool
164{
165 return this->GetRtti() == &other;
166}
167
168//------------------------------------------------------------------------------
171inline bool
173{
174 return this->GetRtti()->GetName() == other;
175}
176
177//------------------------------------------------------------------------------
180inline bool
182{
183 return this->GetRtti()->GetFourCC() == other;
184}
185
186//------------------------------------------------------------------------------
189inline bool
190RefCounted::IsA(const Rtti& other) const
191{
192 return this->GetRtti()->IsDerivedFrom(other);
193}
194
195//------------------------------------------------------------------------------
198inline bool
199RefCounted::IsA(const Util::String& other) const
200{
201 return this->GetRtti()->IsDerivedFrom(other);
202}
203
204//------------------------------------------------------------------------------
207inline bool
208RefCounted::IsA(const Util::FourCC& other) const
209{
210 return this->GetRtti()->IsDerivedFrom(other);
211}
212
213//------------------------------------------------------------------------------
217inline const Util::String&
219{
220 return this->GetRtti()->GetName();
221}
222
223//------------------------------------------------------------------------------
227inline Util::FourCC
229{
230 return this->GetRtti()->GetFourCC();
231}
232
233#if NEBULA_DEBUG
234//------------------------------------------------------------------------------
237inline void
238RefCounted::SetDebugName(const Util::String& name)
239{
240 criticalSection.Enter();
241 list.SetDebugName(this, name);
242 criticalSection.Leave();
243}
244#endif
245
246} // namespace Core
247//------------------------------------------------------------------------------
248
249
250
251
The common base class of Nebula.
Definition refcounted.h:45
void AddRef()
increment refcount by one
Definition refcounted.h:131
RefCounted()
constructor
Definition refcounted.h:116
bool IsA(const Rtti &rtti) const
return true if this object is instance of given class, or a derived class
Definition refcounted.h:190
int GetRefCount() const
get the current refcount
Definition refcounted.h:154
void Release()
decrement refcount and destroy object if refcount is zero
Definition refcounted.h:141
static void DumpRefCountingLeaks()
dump refcounting leaks, call at end of application (NEBULA_DEBUG builds only!)
Definition refcounted.cc:58
bool IsInstanceOf(const Rtti &rtti) const
return true if this object is instance of given class
Definition refcounted.h:163
__DeclareClass(RefCounted)
volatile int refCount
Definition refcounted.h:93
Util::FourCC GetClassFourCC() const
get the class FourCC code
Definition refcounted.h:228
const Util::String & GetClassName() const
get the class name
Definition refcounted.h:218
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
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:35
A four-character-code is a quasi-human-readable 32-bit-id.
Definition fourcc.h:19
Nebula's universal string class.
Definition string.h:50
#define n_assert2(exp, msg)
Definition debug.h:59
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
int SizeT
Definition types.h:50