]> git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
Unicode compilation fixes
[wxWidgets.git] / src / common / object.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: object.cpp
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
5 // Modified by: Ron Lee
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart and Markus Holzem
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "object.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/hash.h"
27 #endif
28
29 #include <string.h>
30 #include <assert.h>
31
32 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
33 #include "wx/memory.h"
34 #endif
35
36 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
37 // for wxObject::Dump
38 #include "wx/ioswrap.h"
39
40 #if defined(__VISAGECPP__)
41 #define DEBUG_PRINTF(NAME) { static int raz=0; \
42 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
43 #else
44 #define DEBUG_PRINTF(NAME)
45 #endif
46
47 #endif
48
49
50 wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
51 (int) sizeof(wxObject),
52 (wxObjectConstructorFn) 0 );
53 wxClassInfo* wxClassInfo::sm_first = 0;
54 wxHashTable* wxClassInfo::sm_classTable = 0;
55
56 // These are here so we can avoid 'always true/false' warnings
57 // by referring to these instead of TRUE/FALSE
58
59 const bool wxTrue = TRUE;
60 const bool wxFalse = FALSE;
61
62 // Is this object a kind of (a subclass of) 'info'?
63 // E.g. is wxWindow a kind of wxObject?
64 // Go from this class to superclass, taking into account
65 // two possible base classes.
66
67 bool wxObject::IsKindOf(wxClassInfo *info) const
68 {
69 wxClassInfo *thisInfo = GetClassInfo();
70 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
71 }
72
73 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
74 void wxObject::Dump(wxSTD ostream& str)
75 {
76 if (GetClassInfo() && GetClassInfo()->GetClassName())
77 str << GetClassInfo()->GetClassName();
78 else
79 str << "unknown object class";
80 }
81 #endif
82
83 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
84
85 #ifdef new
86 #undef new
87 #endif
88
89 void *wxObject::operator new (size_t size, wxChar *fileName, int lineNum)
90 {
91 return wxDebugAlloc(size, fileName, lineNum, TRUE);
92 }
93
94 #ifndef __VISAGECPP__
95 void wxObject::operator delete (void *buf)
96 {
97 wxDebugFree(buf);
98 }
99 #elif __DEBUG_ALLOC__
100 void wxObject::operator delete (void *buf, const char *_fname, size_t _line)
101 {
102 wxDebugFree(buf);
103 }
104 #endif
105
106 // VC++ 6.0
107
108 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
109 void wxObject::operator delete(void *pData, wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum))
110 {
111 ::operator delete(pData);
112 }
113 #endif
114
115 // Cause problems for VC++ - crashes
116
117 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
118 void *wxObject::operator new[] (size_t size, wxChar *fileName, int lineNum)
119 {
120 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
121 }
122
123 void wxObject::operator delete[] (void *buf)
124 {
125 wxDebugFree(buf, TRUE);
126 }
127 #endif
128
129 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
130
131
132 // ----------------------------------------------------------------------------
133 // wxClassInfo
134 // ----------------------------------------------------------------------------
135
136 wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
137 {
138 if ( sm_classTable )
139 {
140 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
141 }
142 else
143 {
144 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
145 {
146 if ( wxStrcmp(info->GetClassName(), className) == 0 )
147 return info;
148 }
149
150 return NULL;
151 }
152 }
153
154 // Set pointers to base class(es) to speed up IsKindOf
155
156 void wxClassInfo::InitializeClasses()
157 {
158 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
159 // link any object module twice mistakenly) will break this function
160 // because it will enter an infinite loop and eventually die with "out of
161 // memory" - as this is quite hard to detect if you're unaware of this,
162 // try to do some checks here
163
164 #ifdef __WXDEBUG__
165 static const size_t nMaxClasses = 10000; // more than we'll ever have
166 size_t nClass = 0;
167 #endif
168
169 wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING);
170
171 // Index all class infos by their class name
172
173 wxClassInfo *info;
174 for(info = sm_first; info; info = info->m_next)
175 {
176 if (info->m_className)
177 {
178 wxASSERT_MSG( ++nClass < nMaxClasses,
179 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
180 sm_classTable->Put(info->m_className, (wxObject *)info);
181 }
182 }
183
184 // Set base pointers for each wxClassInfo
185
186 for(info = sm_first; info; info = info->m_next)
187 {
188 if (info->GetBaseClassName1())
189 info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1());
190 if (info->GetBaseClassName2())
191 info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2());
192 }
193 }
194
195 void wxClassInfo::CleanUpClasses()
196 {
197 delete wxClassInfo::sm_classTable;
198 wxClassInfo::sm_classTable = 0;
199 }
200
201
202 wxObject *wxCreateDynamicObject(const wxChar *name)
203 {
204 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
205 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
206 #endif
207
208 if (wxClassInfo::sm_classTable)
209 {
210 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
211 return info != 0 ? info->CreateObject() : 0;
212 }
213 else
214 {
215 for(wxClassInfo *info = wxClassInfo::sm_first; info; info = info->m_next)
216 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
217 return info->CreateObject();
218 return 0;
219 }
220 }
221
222
223 // ----------------------------------------------------------------------------
224 // wxClassInfo
225 // ----------------------------------------------------------------------------
226
227 void wxObject::Ref(const wxObject& clone)
228 {
229 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
230 DEBUG_PRINTF(wxObject::Ref)
231 #endif
232
233 // delete reference to old data
234 UnRef();
235
236 // reference new data
237 if ( clone.m_refData )
238 {
239 m_refData = clone.m_refData;
240 ++(m_refData->m_count);
241 }
242 }
243
244 void wxObject::UnRef()
245 {
246 if ( m_refData )
247 {
248 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
249
250 if ( !--m_refData->m_count )
251 delete m_refData;
252 m_refData = NULL;
253 }
254 }
255
256 void wxObject::AllocExclusive()
257 {
258 if ( !m_refData )
259 {
260 m_refData = CreateRefData();
261 }
262 else if ( m_refData->GetRefCount() > 1 )
263 {
264 // note that ref is not going to be destroyed in this case
265 wxObjectRefData* ref = m_refData;
266 UnRef();
267
268 // ... so we can still access it
269 m_refData = CloneRefData(ref);
270 }
271 //else: ref count is 1, we are exclusive owners of m_refData anyhow
272
273 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
274 _T("wxObject::AllocExclusive() failed.") );
275 }
276
277 wxObjectRefData *wxObject::CreateRefData() const
278 {
279 // if you use AllocExclusive() you must override this method
280 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
281
282 return NULL;
283 }
284
285 wxObjectRefData *wxObject::CloneRefData(wxObjectRefData * WXUNUSED(data)) const
286 {
287 // if you use AllocExclusive() you must override this method
288 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
289
290 return NULL;
291 }
292
293 // ----------------------------------------------------------------------------
294 // misc
295 // ----------------------------------------------------------------------------
296
297 #if defined(__DARWIN__) && defined(DYLIB_INIT)
298
299 extern "C" {
300 void __initialize_Cplusplus(void);
301 void wxWindowsDylibInit(void);
302 };
303
304 // Dynamic shared library (dylib) initialization routine
305 // required to initialize static C++ objects bacause of lazy dynamic linking
306 // http://developer.apple.com/techpubs/macosx/Essentials/
307 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
308
309 void wxWindowsDylibInit()
310 {
311 // The function __initialize_Cplusplus() must be called from the shared
312 // library initialization routine to cause the static C++ objects in
313 // the library to be initialized (reference number 2441683).
314
315 __initialize_Cplusplus();
316 }
317
318 #endif
319
320 // vi:sts=4:sw=4:et