removed unused wxObject::Dump
[wxWidgets.git] / src / common / object.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "object.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/hash.h"
26 #include "wx/object.h"
27 #endif
28
29 #include <string.h>
30
31 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
32 #include "wx/memory.h"
33 #endif
34
35 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
36 #if defined(__VISAGECPP__)
37 #define DEBUG_PRINTF(NAME) { static int raz=0; \
38 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
39 #else
40 #define DEBUG_PRINTF(NAME)
41 #endif
42 #endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
43
44 // we must disable optimizations for VC.NET because otherwise its too eager
45 // linker discards wxClassInfo objects in release build thus breaking many,
46 // many things
47 #if defined __VISUALC__ && __VISUALC__ >= 1300
48 #pragma optimize("", off)
49 #endif
50
51 #if wxUSE_EXTENDED_RTTI
52 const wxClassInfo* wxObject::sm_classParentswxObject[] = { NULL } ;
53 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
54 { return data.Get<wxObject*>() ; }
55 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
56 { return &data.Get<wxObject>() ; }
57 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
58 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
59 wxClassInfo wxObject::sm_classwxObject(sm_classParentswxObject , wxT("") , wxT("wxObject"),
60 (int) sizeof(wxObject), \
61 (wxObjectConstructorFn) 0 ,
62 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
63 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
64 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
65 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
66 template<> void wxStringReadValue(const wxString & , wxObject & ){assert(0) ;}
67 template<> void wxStringWriteValue(wxString & , wxObject const & ){assert(0) ;}
68 template<> const wxTypeInfo* wxGetTypeInfo( wxObject ** )
69 { static wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::sm_classwxObject) ; return &s_typeInfo ; }
70 template<> const wxTypeInfo* wxGetTypeInfo( wxObject * )
71 { static wxClassTypeInfo s_typeInfo(wxT_OBJECT , &wxObject::sm_classwxObject) ; return &s_typeInfo ; }
72 #else
73 wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
74 (int) sizeof(wxObject),
75 (wxObjectConstructorFn) 0 );
76 #endif
77
78 // restore optimizations
79 #if defined __VISUALC__ && __VISUALC__ >= 1300
80 #pragma optimize("", on)
81 #endif
82
83 wxClassInfo* wxClassInfo::sm_first = NULL;
84 wxHashTable* wxClassInfo::sm_classTable = NULL;
85
86 // These are here so we can avoid 'always true/false' warnings
87 // by referring to these instead of TRUE/FALSE
88 const bool wxTrue = TRUE;
89 const bool wxFalse = FALSE;
90
91 // Is this object a kind of (a subclass of) 'info'?
92 // E.g. is wxWindow a kind of wxObject?
93 // Go from this class to superclass, taking into account
94 // two possible base classes.
95 bool wxObject::IsKindOf(wxClassInfo *info) const
96 {
97 wxClassInfo *thisInfo = GetClassInfo();
98 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
99 }
100
101 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
102 #undef new
103 #endif
104
105
106 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
107 void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
108 {
109 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
110 }
111 #endif
112
113 #ifdef _WX_WANT_DELETE_VOID
114 void wxObject::operator delete ( void *buf )
115 {
116 wxDebugFree(buf);
117 }
118 #endif
119
120 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
121 void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
122 {
123 wxDebugFree(buf);
124 }
125 #endif
126
127 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
128 void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
129 {
130 wxDebugFree(buf);
131 }
132 #endif
133
134 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
135 void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
136 {
137 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
138 }
139 #endif
140
141 #ifdef _WX_WANT_ARRAY_DELETE_VOID
142 void wxObject::operator delete[] ( void *buf )
143 {
144 wxDebugFree(buf, TRUE);
145 }
146 #endif
147
148 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
149 void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
150 {
151 wxDebugFree(buf, TRUE);
152 }
153 #endif
154
155
156 // ----------------------------------------------------------------------------
157 // wxClassInfo
158 // ----------------------------------------------------------------------------
159
160 wxClassInfo::~wxClassInfo()
161 {
162 // remove this object from the linked list of all class infos: if we don't
163 // do it, loading/unloading a DLL containing static wxClassInfo objects is
164 // not going to work
165 if ( this == sm_first )
166 {
167 sm_first = m_next;
168 }
169 else
170 {
171 wxClassInfo *info = sm_first;
172 while (info)
173 {
174 if ( info->m_next == this )
175 {
176 info->m_next = m_next;
177 break;
178 }
179
180 info = info->m_next;
181 }
182 }
183 Unregister();
184 }
185
186 wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
187 {
188 if ( sm_classTable )
189 {
190 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
191 }
192 else
193 {
194 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
195 {
196 if ( wxStrcmp(info->GetClassName(), className) == 0 )
197 return info;
198 }
199
200 return NULL;
201 }
202 }
203
204 void wxClassInfo::CleanUp()
205 {
206 if ( sm_classTable )
207 {
208 delete sm_classTable;
209 sm_classTable = NULL;
210 }
211 }
212
213 void wxClassInfo::Register()
214 {
215 if ( !sm_classTable )
216 {
217 sm_classTable = new wxHashTable(wxKEY_STRING);
218 }
219
220 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
221 // link any object module twice mistakenly) will break this function
222 // because it will enter an infinite loop and eventually die with "out of
223 // memory" - as this is quite hard to detect if you're unaware of this,
224 // try to do some checks here
225 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
226 _T("class already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
227
228 sm_classTable->Put(m_className, (wxObject *)this);
229 }
230
231 void wxClassInfo::Unregister()
232 {
233 if ( sm_classTable )
234 {
235 sm_classTable->Delete(m_className);
236 if ( sm_classTable->GetCount() == 0 )
237 {
238 delete sm_classTable;
239 sm_classTable = NULL;
240 }
241 }
242 }
243
244 wxObject *wxCreateDynamicObject(const wxChar *name)
245 {
246 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
247 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
248 #endif
249
250 if ( wxClassInfo::sm_classTable )
251 {
252 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
253 return info ? info->CreateObject() : NULL;
254 }
255 else // no sm_classTable yet
256 {
257 for ( wxClassInfo *info = wxClassInfo::sm_first;
258 info;
259 info = info->m_next )
260 {
261 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
262 return info->CreateObject();
263 }
264
265 return NULL;
266 }
267 }
268
269
270 // ----------------------------------------------------------------------------
271 // wxObject
272 // ----------------------------------------------------------------------------
273
274 // Initialize ref data from another object (needed for copy constructor and
275 // assignment operator)
276 void wxObject::InitFrom(const wxObject& other)
277 {
278 m_refData = other.m_refData;
279 if ( m_refData )
280 m_refData->m_count++;
281 }
282
283 void wxObject::Ref(const wxObject& clone)
284 {
285 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
286 DEBUG_PRINTF(wxObject::Ref)
287 #endif
288
289 // nothing to be done
290 if (m_refData == clone.m_refData)
291 return;
292
293 // delete reference to old data
294 UnRef();
295
296 // reference new data
297 if ( clone.m_refData )
298 {
299 m_refData = clone.m_refData;
300 ++(m_refData->m_count);
301 }
302 }
303
304 void wxObject::UnRef()
305 {
306 if ( m_refData )
307 {
308 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
309
310 if ( !--m_refData->m_count )
311 delete m_refData;
312 m_refData = NULL;
313 }
314 }
315
316 void wxObject::AllocExclusive()
317 {
318 if ( !m_refData )
319 {
320 m_refData = CreateRefData();
321 }
322 else if ( m_refData->GetRefCount() > 1 )
323 {
324 // note that ref is not going to be destroyed in this case
325 const wxObjectRefData* ref = m_refData;
326 UnRef();
327
328 // ... so we can still access it
329 m_refData = CloneRefData(ref);
330 }
331 //else: ref count is 1, we are exclusive owners of m_refData anyhow
332
333 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
334 _T("wxObject::AllocExclusive() failed.") );
335 }
336
337 wxObjectRefData *wxObject::CreateRefData() const
338 {
339 // if you use AllocExclusive() you must override this method
340 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
341
342 return NULL;
343 }
344
345 wxObjectRefData *
346 wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
347 {
348 // if you use AllocExclusive() you must override this method
349 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
350
351 return NULL;
352 }