two changes and some cleanup:
[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::ms_classParents[] = { NULL } ;
53 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
54 { return data.wxTEMPLATED_MEMBER_CALL(Get , wxObject*) ; }
55 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
56 { return &data.wxTEMPLATED_MEMBER_CALL(Get , wxObject) ; }
57 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
58 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
59 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , 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 wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject*).name() ) ;
69 wxClassTypeInfo s_typeInfowxObject(wxT_OBJECT , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject).name() ) ;
70 #else
71 wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
72 (int) sizeof(wxObject),
73 (wxObjectConstructorFn) 0 );
74 #endif
75
76 // restore optimizations
77 #if defined __VISUALC__ && __VISUALC__ >= 1300
78 #pragma optimize("", on)
79 #endif
80
81 wxClassInfo* wxClassInfo::sm_first = NULL;
82 wxHashTable* wxClassInfo::sm_classTable = NULL;
83
84 wxClassInfo *wxObject::GetClassInfo() const
85 {
86 return &wxObject::ms_classInfo;
87 }
88
89 // These are here so we can avoid 'always true/false' warnings
90 // by referring to these instead of true/false
91 const bool wxTrue = true;
92 const bool wxFalse = false;
93
94 // Is this object a kind of (a subclass of) 'info'?
95 // E.g. is wxWindow a kind of wxObject?
96 // Go from this class to superclass, taking into account
97 // two possible base classes.
98 bool wxObject::IsKindOf(wxClassInfo *info) const
99 {
100 wxClassInfo *thisInfo = GetClassInfo();
101 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
102 }
103
104 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
105 #undef new
106 #endif
107
108
109 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
110 void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
111 {
112 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
113 }
114 #endif
115
116 #ifdef _WX_WANT_DELETE_VOID
117 void wxObject::operator delete ( void *buf )
118 {
119 wxDebugFree(buf);
120 }
121 #endif
122
123 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
124 void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
125 {
126 wxDebugFree(buf);
127 }
128 #endif
129
130 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
131 void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
132 {
133 wxDebugFree(buf);
134 }
135 #endif
136
137 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
138 void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
139 {
140 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
141 }
142 #endif
143
144 #ifdef _WX_WANT_ARRAY_DELETE_VOID
145 void wxObject::operator delete[] ( void *buf )
146 {
147 wxDebugFree(buf, true);
148 }
149 #endif
150
151 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
152 void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
153 {
154 wxDebugFree(buf, true);
155 }
156 #endif
157
158
159 // ----------------------------------------------------------------------------
160 // wxClassInfo
161 // ----------------------------------------------------------------------------
162
163 wxClassInfo::~wxClassInfo()
164 {
165 // remove this object from the linked list of all class infos: if we don't
166 // do it, loading/unloading a DLL containing static wxClassInfo objects is
167 // not going to work
168 if ( this == sm_first )
169 {
170 sm_first = m_next;
171 }
172 else
173 {
174 wxClassInfo *info = sm_first;
175 while (info)
176 {
177 if ( info->m_next == this )
178 {
179 info->m_next = m_next;
180 break;
181 }
182
183 info = info->m_next;
184 }
185 }
186 Unregister();
187 }
188
189 wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
190 {
191 if ( sm_classTable )
192 {
193 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
194 }
195 else
196 {
197 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
198 {
199 if ( wxStrcmp(info->GetClassName(), className) == 0 )
200 return info;
201 }
202
203 return NULL;
204 }
205 }
206
207 void wxClassInfo::CleanUp()
208 {
209 if ( sm_classTable )
210 {
211 delete sm_classTable;
212 sm_classTable = NULL;
213 }
214 }
215
216 void wxClassInfo::Register()
217 {
218 if ( !sm_classTable )
219 {
220 sm_classTable = new wxHashTable(wxKEY_STRING);
221 }
222
223 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
224 // link any object module twice mistakenly) will break this function
225 // because it will enter an infinite loop and eventually die with "out of
226 // memory" - as this is quite hard to detect if you're unaware of this,
227 // try to do some checks here
228 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
229 _T("class already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
230
231 sm_classTable->Put(m_className, (wxObject *)this);
232 }
233
234 void wxClassInfo::Unregister()
235 {
236 if ( sm_classTable )
237 {
238 sm_classTable->Delete(m_className);
239 if ( sm_classTable->GetCount() == 0 )
240 {
241 delete sm_classTable;
242 sm_classTable = NULL;
243 }
244 }
245 }
246
247 wxObject *wxCreateDynamicObject(const wxChar *name)
248 {
249 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
250 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
251 #endif
252
253 if ( wxClassInfo::sm_classTable )
254 {
255 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
256 return info ? info->CreateObject() : NULL;
257 }
258 else // no sm_classTable yet
259 {
260 for ( wxClassInfo *info = wxClassInfo::sm_first;
261 info;
262 info = info->m_next )
263 {
264 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
265 return info->CreateObject();
266 }
267
268 return NULL;
269 }
270 }
271
272
273 // ----------------------------------------------------------------------------
274 // wxObject
275 // ----------------------------------------------------------------------------
276
277 // Initialize ref data from another object (needed for copy constructor and
278 // assignment operator)
279 void wxObject::InitFrom(const wxObject& other)
280 {
281 m_refData = other.m_refData;
282 if ( m_refData )
283 m_refData->m_count++;
284 }
285
286 void wxObject::Ref(const wxObject& clone)
287 {
288 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
289 DEBUG_PRINTF(wxObject::Ref)
290 #endif
291
292 // nothing to be done
293 if (m_refData == clone.m_refData)
294 return;
295
296 // delete reference to old data
297 UnRef();
298
299 // reference new data
300 if ( clone.m_refData )
301 {
302 m_refData = clone.m_refData;
303 ++(m_refData->m_count);
304 }
305 }
306
307 void wxObject::UnRef()
308 {
309 if ( m_refData )
310 {
311 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
312
313 if ( !--m_refData->m_count )
314 delete m_refData;
315 m_refData = NULL;
316 }
317 }
318
319 void wxObject::AllocExclusive()
320 {
321 if ( !m_refData )
322 {
323 m_refData = CreateRefData();
324 }
325 else if ( m_refData->GetRefCount() > 1 )
326 {
327 // note that ref is not going to be destroyed in this case
328 const wxObjectRefData* ref = m_refData;
329 UnRef();
330
331 // ... so we can still access it
332 m_refData = CloneRefData(ref);
333 }
334 //else: ref count is 1, we are exclusive owners of m_refData anyhow
335
336 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
337 _T("wxObject::AllocExclusive() failed.") );
338 }
339
340 wxObjectRefData *wxObject::CreateRefData() const
341 {
342 // if you use AllocExclusive() you must override this method
343 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
344
345 return NULL;
346 }
347
348 wxObjectRefData *
349 wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
350 {
351 // if you use AllocExclusive() you must override this method
352 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
353
354 return NULL;
355 }