My Project: dynamics.mod/NewtonDynamics/source/core/dgRef.h Source File

My Project

dgRef.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 #ifndef __dgRef__
23 #define __dgRef__
24
25 #include "dgStdafx.h"
26 #include "dgCRC.h"
27 #include "dgRtti.h"
28 #include "dgDebug.h"
29 #include "dgMemory.h"
30
31
32 struct dgRefFlags
33 {
34 dgRefFlags ();
35 inline dgInt32 operator = (dgInt32 val);
36
37 dgUnsigned8 m_alive;
38 dgUnsigned8 m_userFlag0;
39 dgUnsigned8 m_userFlag1;
40 dgUnsigned8 m_userFlag2;
41 // dgUnsigned32 m_userFlag3 : 1;
42 // dgUnsigned32 m_userFlag4 : 1;
43 // dgUnsigned32 m_userFlag5 : 1;
44 // dgUnsigned32 m_userFlag6 : 1;
45
46 dgUnsigned32 m_ref;
47 };
48
49
50 class dgRef: public dgRefFlags
51 {
52 public:
53 dgRef ();
54 dgRef (const char *name);
55 dgRef (dgUnsigned32 idArg);
56 dgRef(const dgRef &Clone);
57 dgRef *AddRef () ;
58 dgInt32 Release ();
59 dgInt32 GetRefCount() const;
60
61 DG_CLASS_ALLOCATOR(allocator)
62
63 virtual dgRef *CreateClone () const;
64 virtual dgUnsigned32 GetTypeId () const;
65 virtual bool IsType (dgUnsigned32 typeId) const;
66
67 bool GetUserFlag0 () const;
68 bool GetUserFlag1 () const;
69 void SetUserFlag0 (bool flags);
70 void SetUserFlag1 (bool flags);
71
72 bool IsAlive() const;
73 virtual void Kill();
74 virtual void Unkill();
75
76 const char* GetName () const;
77 dgUnsigned32 GetNameID () const;
78 inline void SetNameID (dgUnsigned32 newID);
79 virtual inline void SetName (const char *name);
80
81 void AttachRef (dgRef **oldRef, dgRef *newRef);
82
83
84 bool IsTypeByName (const char *typeName) const;
85 static dgUnsigned32 GetRttiType();
86
87 protected:
88 virtual ~dgRef ();
89
90 private:
91 dgUnsigned32 m_id;
92 static dgRtti m_rtti;
93 };
94
95
96
97 inline dgRefFlags::dgRefFlags ()
98 {
99 *this = 0;
100 m_alive = true;
101 m_ref = 1;
102 }
103
104 inline dgInt32 dgRefFlags::operator = (dgInt32 val)
105 {
106 dgInt32* ptr;
107 ptr = &(*(dgInt32*)this);
108 *ptr = val;
109 return val;
110 }
111
112
113
114 inline dgRef::dgRef ()
115 {
116 m_id = 0;
117 }
118
119 inline dgRef::dgRef (const char *name)
120 {
121 SetName (name);
122 }
123
124 inline dgRef::dgRef (dgUnsigned32 idArg)
125 {
126 SetNameID (idArg);
127 }
128
129 inline dgRef::dgRef(const dgRef &Clone)
130 {
131 m_id = Clone.m_id;
132 }
133
134 inline dgRef::~dgRef ()
135 {
136 }
137
138 inline dgRef *dgRef::AddRef ()
139 {
140 m_ref ++;
141 dgAssert (m_ref < ((1<<24) - 1));
142 return this;
143 }
144
145 inline dgInt32 dgRef::Release ()
146 {
147 m_ref --;
148 if (m_ref) {
149 return dgInt32 (m_ref);
150 }
151 delete this;
152 return 0;
153 }
154
155 inline dgRef *dgRef::CreateClone () const
156 {
157 dgAssert (0);
158 return NULL;
159 }
160
161
162 inline dgUnsigned32 dgRef::GetTypeId () const
163 {
164 return m_rtti.GetTypeId ();
165 }
166
167 inline bool dgRef::IsType (dgUnsigned32 typeId) const
168 {
169 return m_rtti.IsTypeID (typeId);
170 }
171
172 inline dgUnsigned32 dgRef::GetRttiType()
173 {
174 return m_rtti.GetTypeId();
175 }
176
177 inline bool dgRef::IsTypeByName (const char *typeName) const
178 {
179 return IsType (dgCRC (typeName, (dgInt32) strlen (typeName)));
180 }
181
182
183 inline bool dgRef::GetUserFlag0 () const
184 {
185 return m_userFlag0 ? true : false;
186 }
187
188 inline bool dgRef::GetUserFlag1 () const
189 {
190 return m_userFlag1 ? true : false;
191 }
192
193
194 inline void dgRef::SetUserFlag0 (bool flags)
195 {
196 m_userFlag0 = dgUnsigned8 (flags);
197 }
198
199 inline void dgRef::SetUserFlag1 (bool flags)
200 {
201 m_userFlag1 = dgUnsigned8 (flags);
202 }
203
204
205 inline bool dgRef::IsAlive() const
206 {
207 return m_alive ? true : false;
208 }
209
210 inline void dgRef::Kill()
211 {
212 m_alive = false;
213 }
214
215 inline void dgRef::Unkill()
216 {
217 m_alive = true;
218 }
219
220 inline void dgRef::SetNameID (dgUnsigned32 newID)
221 {
222 m_id = newID;
223 }
224
225 inline dgUnsigned32 dgRef::GetNameID () const
226 {
227 return m_id;
228 }
229
230 inline void dgRef::SetName (const char *name)
231 {
232 SetNameID (0);
233 if (name) {
234 SetNameID (dgCRC (name));
235 }
236 }
237
238 inline const char* dgRef::GetName () const
239 {
240 return dgInverseCRC (GetNameID ());
241 }
242
243 inline dgInt32 dgRef::GetRefCount() const
244 {
245 return dgInt32 (m_ref);
246 }
247
248 #endif
249
dgRefFlags::m_alive
dgUnsigned8 m_alive
Definition: dgRef.h:37
dgUnsigned8
unsigned char dgUnsigned8
Definition: dgTypes.h:213
dgMemory.h
dgRef::SetUserFlag0
void SetUserFlag0(bool flags)
Definition: dgRef.h:194
dgCRC
dgUnsigned32 dgApi dgCRC(const char *const name)
Definition: dgCRC.cpp:340
dgRefFlags::dgRefFlags
dgRefFlags()
Definition: dgRef.h:97
dgRef::AddRef
dgRef * AddRef()
Definition: dgRef.h:138
dgRefFlags::operator=
dgInt32 operator=(dgInt32 val)
Definition: dgRef.h:104
dgDebug.h
dgAssert
#define dgAssert(x)
Definition: dgTypes.h:158
dgRefFlags::m_userFlag2
dgUnsigned8 m_userFlag2
Definition: dgRef.h:40
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgRef::GetRefCount
dgInt32 GetRefCount() const
Definition: dgRef.h:243
dgRef::GetNameID
dgUnsigned32 GetNameID() const
Definition: dgRef.h:225
dgRef::GetRttiType
static dgUnsigned32 GetRttiType()
Definition: dgRef.h:172
dgRefFlags
Definition: dgRef.h:32
DG_CLASS_ALLOCATOR
#define DG_CLASS_ALLOCATOR(allocator)
Definition: dgMemory.h:60
dgRef::GetName
const char * GetName() const
Definition: dgRef.h:238
dgRef::Release
dgInt32 Release()
Definition: dgRef.h:145
dgCRC.h
dgRef::SetUserFlag1
void SetUserFlag1(bool flags)
Definition: dgRef.h:199
dgRef::Kill
virtual void Kill()
Definition: dgRef.h:210
dgRef::GetUserFlag1
bool GetUserFlag1() const
Definition: dgRef.h:188
dgRef::SetNameID
void SetNameID(dgUnsigned32 newID)
Definition: dgRef.h:220
dgStdafx.h
dgRef
Definition: dgRef.h:50
dgRef::Unkill
virtual void Unkill()
Definition: dgRef.h:215
dgRef::IsTypeByName
bool IsTypeByName(const char *typeName) const
Definition: dgRef.h:177
dgRef::CreateClone
virtual dgRef * CreateClone() const
Definition: dgRef.h:155
dgRef::~dgRef
virtual ~dgRef()
Definition: dgRef.h:134
dgRefFlags::m_userFlag0
dgUnsigned8 m_userFlag0
Definition: dgRef.h:38
dgRef::GetUserFlag0
bool GetUserFlag0() const
Definition: dgRef.h:183
dgRef::dgRef
dgRef()
Definition: dgRef.h:114
dgUnsigned32
unsigned dgUnsigned32
Definition: dgTypes.h:219
dgRtti.h
dgRef::SetName
virtual void SetName(const char *name)
Definition: dgRef.h:230
dgRefFlags::m_ref
dgUnsigned32 m_ref
Definition: dgRef.h:46
dgRef::IsType
virtual bool IsType(dgUnsigned32 typeId) const
Definition: dgRef.h:167
dgRef::GetTypeId
virtual dgUnsigned32 GetTypeId() const
Definition: dgRef.h:162
dgRef::IsAlive
bool IsAlive() const
Definition: dgRef.h:205
dgRtti
Definition: dgRtti.h:28
dgInverseCRC
const char *dgApi dgInverseCRC(dgUnsigned32 crc)
Definition: dgCRC.cpp:378
dgRefFlags::m_userFlag1
dgUnsigned8 m_userFlag1
Definition: dgRef.h:39