ScummVM API documentation
dgRef.h
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 "dgDebug.h"
28 #include "dgMemory.h"
29 
30 
31 struct dgRefFlags {
32  dgRefFlags();
33  inline dgInt32 operator = (dgInt32 val);
34 
35  dgUnsigned8 m_alive;
36  dgUnsigned8 m_userFlag0;
37  dgUnsigned8 m_userFlag1;
38  dgUnsigned8 m_userFlag2;
39 // dgUnsigned32 m_userFlag3 : 1;
40 // dgUnsigned32 m_userFlag4 : 1;
41 // dgUnsigned32 m_userFlag5 : 1;
42 // dgUnsigned32 m_userFlag6 : 1;
43 
44  dgUnsigned32 m_ref;
45 };
46 
47 
48 class dgRef: public dgRefFlags {
49 public:
50  dgRef();
51  dgRef(const char *name);
52  dgRef(dgUnsigned32 idArg);
53  dgRef(const dgRef &Clone);
54  dgRef *AddRef() ;
55  dgInt32 Release();
56  dgInt32 GetRefCount() const;
57 
58  DG_CLASS_ALLOCATOR(allocator)
59 
60  virtual dgRef *CreateClone() const;
61 
62  bool GetUserFlag0() const;
63  bool GetUserFlag1() const;
64  void SetUserFlag0(bool flags);
65  void SetUserFlag1(bool flags);
66 
67  bool IsAlive() const;
68  virtual void Kill();
69  virtual void Unkill();
70 
71  const char *GetName() const;
72  dgUnsigned32 GetNameID() const;
73  inline void SetNameID(dgUnsigned32 newID);
74  virtual inline void SetName(const char *name);
75 
76  void AttachRef(dgRef **oldRef, dgRef *newRef);
77 
78  static dgUnsigned32 GetRttiType();
79 
80 protected:
81  virtual ~dgRef();
82 
83 private:
84  dgUnsigned32 m_id;
85 };
86 
87 
88 
89 inline dgRefFlags::dgRefFlags() {
90  *this = 0;
91  m_alive = true;
92  m_ref = 1;
93 }
94 
95 inline dgInt32 dgRefFlags::operator = (dgInt32 val) {
96  dgInt32 *ptr;
97  ptr = &(*(dgInt32 *)this);
98  *ptr = val;
99  return val;
100 }
101 
102 
103 
104 inline dgRef::dgRef() {
105  m_id = 0;
106 }
107 
108 inline dgRef::dgRef(const char *name) {
109  SetName(name);
110 }
111 
112 inline dgRef::dgRef(dgUnsigned32 idArg) {
113  SetNameID(idArg);
114 }
115 
116 inline dgRef::dgRef(const dgRef &Clone) {
117  m_id = Clone.m_id;
118 }
119 
120 inline dgRef::~dgRef() {
121 }
122 
123 inline dgRef *dgRef::AddRef() {
124  m_ref ++;
125  NEWTON_ASSERT(m_ref < ((1 << 24) - 1));
126  return this;
127 }
128 
129 inline dgInt32 dgRef::Release() {
130  m_ref --;
131  if (m_ref) {
132  return dgInt32(m_ref);
133  }
134  delete this;
135  return 0;
136 }
137 
138 inline dgRef *dgRef::CreateClone() const {
139  NEWTON_ASSERT(0);
140  return NULL;
141 }
142 
143 
144 inline bool dgRef::GetUserFlag0() const {
145  return m_userFlag0 ? true : false;
146 }
147 
148 inline bool dgRef::GetUserFlag1() const {
149  return m_userFlag1 ? true : false;
150 }
151 
152 
153 inline void dgRef::SetUserFlag0(bool flags) {
154  m_userFlag0 = dgUnsigned8(flags);
155 }
156 
157 inline void dgRef::SetUserFlag1(bool flags) {
158  m_userFlag1 = dgUnsigned8(flags);
159 }
160 
161 
162 inline bool dgRef::IsAlive() const {
163  return m_alive ? true : false;
164 }
165 
166 inline void dgRef::Kill() {
167  m_alive = false;
168 }
169 
170 inline void dgRef::Unkill() {
171  m_alive = true;
172 }
173 
174 inline void dgRef::SetNameID(dgUnsigned32 newID) {
175  m_id = newID;
176 }
177 
178 inline dgUnsigned32 dgRef::GetNameID() const {
179  return m_id;
180 }
181 
182 inline void dgRef::SetName(const char *name) {
183  SetNameID(0);
184  if (name) {
185  SetNameID(dgCRC(name));
186  }
187 }
188 
189 inline const char *dgRef::GetName() const {
190  return dgInverseCRC(GetNameID());
191 }
192 
193 inline dgInt32 dgRef::GetRefCount() const {
194  return dgInt32(m_ref);
195 }
196 
197 #endif
198 
Definition: dgRef.h:31
Definition: dgRef.h:48