]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
b2edef6f | 2 | // Name: src/common/object.cpp |
c801d85f KB |
3 | // Purpose: wxObject implementation |
4 | // Author: Julian Smart | |
0b9ab0bd | 5 | // Modified by: Ron Lee |
c801d85f KB |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
55d99c7a | 8 | // Copyright: (c) 1998 Julian Smart |
0b9ab0bd | 9 | // (c) 2001 Ron Lee <ron@debian.org> |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
b2edef6f | 13 | // For compilers that support precompilation, includes "wx.h". |
c801d85f KB |
14 | #include "wx/wxprec.h" |
15 | ||
16 | #ifdef __BORLANDC__ | |
8e3f3880 | 17 | #pragma hdrstop |
c801d85f KB |
18 | #endif |
19 | ||
1678ad78 | 20 | #ifndef WX_PRECOMP |
df5168c4 | 21 | #include "wx/object.h" |
8e3f3880 | 22 | #include "wx/hash.h" |
5b56bffb | 23 | #include "wx/memory.h" |
0bf751e7 | 24 | #include "wx/crt.h" |
24eb81cb DW |
25 | #endif |
26 | ||
c801d85f | 27 | #include <string.h> |
c801d85f | 28 | |
4b6a582b | 29 | #if wxUSE_DEBUG_CONTEXT |
b2edef6f VZ |
30 | #if defined(__VISAGECPP__) |
31 | #define DEBUG_PRINTF(NAME) { static int raz=0; \ | |
32 | printf( #NAME " %i\n",raz); fflush(stdout); raz++; } | |
33 | #else | |
34 | #define DEBUG_PRINTF(NAME) | |
35 | #endif | |
4b6a582b | 36 | #endif // wxUSE_DEBUG_CONTEXT |
c801d85f | 37 | |
e1720942 VZ |
38 | // we must disable optimizations for VC.NET because otherwise its too eager |
39 | // linker discards wxClassInfo objects in release build thus breaking many, | |
40 | // many things | |
fbae49ee | 41 | #if defined __VISUALC__ && __VISUALC__ >= 1300 |
e1720942 VZ |
42 | #pragma optimize("", off) |
43 | #endif | |
f6bcfd97 | 44 | |
30bfc425 | 45 | #if wxUSE_EXTENDED_RTTI |
05fa251a | 46 | const wxClassInfo* wxObject::ms_classParents[] = { NULL } ; |
431346ff | 47 | wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data ) |
2c4c3987 | 48 | { return data.wxTEMPLATED_MEMBER_CALL(Get , wxObject*) ; } |
431346ff | 49 | wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data ) |
2c4c3987 | 50 | { return &data.wxTEMPLATED_MEMBER_CALL(Get , wxObject) ; } |
431346ff | 51 | wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data ) |
a095505c | 52 | { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; } |
525d8583 | 53 | wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"), |
a095505c | 54 | (int) sizeof(wxObject), \ |
431346ff | 55 | (wxObjectConstructorFn) 0 , |
d3b9f782 | 56 | NULL,NULL,0 , 0 , |
7e548f6b | 57 | 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject); |
2e587bb9 VZ |
58 | template<> void wxStringReadValue(const wxString & , wxObject * & ){ wxFAIL_MSG("unreachable"); } |
59 | template<> void wxStringWriteValue(wxString & , wxObject* const & ){ wxFAIL_MSG("unreachable"); } | |
60 | template<> void wxStringReadValue(const wxString & , wxObject & ){ wxFAIL_MSG("unreachable"); } | |
61 | template<> void wxStringWriteValue(wxString & , wxObject const & ){ wxFAIL_MSG("unreachable"); } | |
7e548f6b WS |
62 | wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject*).name() ) ; |
63 | wxClassTypeInfo s_typeInfowxObject(wxT_OBJECT , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject).name() ) ; | |
a095505c | 64 | #else |
c0db9626 | 65 | wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0, |
0b9ab0bd RL |
66 | (int) sizeof(wxObject), |
67 | (wxObjectConstructorFn) 0 ); | |
a095505c | 68 | #endif |
c801d85f | 69 | |
e1720942 | 70 | // restore optimizations |
fbae49ee | 71 | #if defined __VISUALC__ && __VISUALC__ >= 1300 |
e1720942 VZ |
72 | #pragma optimize("", on) |
73 | #endif | |
74 | ||
b2edef6f VZ |
75 | wxClassInfo* wxClassInfo::sm_first = NULL; |
76 | wxHashTable* wxClassInfo::sm_classTable = NULL; | |
c801d85f | 77 | |
66d2fa18 VZ |
78 | // when using XTI, this method is already implemented inline inside |
79 | // DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function | |
80 | // non-inline because this allows us to have a non-inline virtual function in | |
81 | // all wx classes and this solves linking problems for HP-UX native toolchain | |
82 | // and possibly others (we could make dtor non-inline as well but it's more | |
83 | // useful to keep it inline than this function) | |
84 | #if !wxUSE_EXTENDED_RTTI | |
85 | ||
14ca93a0 VZ |
86 | wxClassInfo *wxObject::GetClassInfo() const |
87 | { | |
88 | return &wxObject::ms_classInfo; | |
89 | } | |
90 | ||
66d2fa18 VZ |
91 | #endif // wxUSE_EXTENDED_RTTI |
92 | ||
258e7b25 | 93 | // this variable exists only so that we can avoid 'always true/false' warnings |
7e548f6b | 94 | const bool wxFalse = false; |
c801d85f | 95 | |
b2edef6f VZ |
96 | // Is this object a kind of (a subclass of) 'info'? |
97 | // E.g. is wxWindow a kind of wxObject? | |
98 | // Go from this class to superclass, taking into account | |
99 | // two possible base classes. | |
303c6f20 | 100 | bool wxObject::IsKindOf(const wxClassInfo *info) const |
c801d85f | 101 | { |
303c6f20 | 102 | const wxClassInfo *thisInfo = GetClassInfo(); |
7e548f6b | 103 | return (thisInfo) ? thisInfo->IsKindOf(info) : false ; |
c801d85f KB |
104 | } |
105 | ||
4b6a582b | 106 | #if wxUSE_MEMORY_TRACING && defined( new ) |
7e548f6b | 107 | #undef new |
cf760e4c VZ |
108 | #endif |
109 | ||
110 | ||
b2edef6f | 111 | #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT |
cf760e4c | 112 | void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum ) |
c801d85f | 113 | { |
7e548f6b | 114 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true); |
c801d85f | 115 | } |
b2edef6f | 116 | #endif |
c801d85f | 117 | |
b2edef6f VZ |
118 | #ifdef _WX_WANT_DELETE_VOID |
119 | void wxObject::operator delete ( void *buf ) | |
415ca026 DW |
120 | { |
121 | wxDebugFree(buf); | |
122 | } | |
b2edef6f VZ |
123 | #endif |
124 | ||
125 | #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET | |
126 | void wxObject::operator delete ( void *buf, const char *_fname, size_t _line ) | |
c801d85f | 127 | { |
afb74891 | 128 | wxDebugFree(buf); |
c801d85f | 129 | } |
0b9ab0bd RL |
130 | #endif |
131 | ||
b2edef6f | 132 | #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT |
cf760e4c | 133 | void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
76626af2 | 134 | { |
b2edef6f | 135 | wxDebugFree(buf); |
76626af2 JS |
136 | } |
137 | #endif | |
138 | ||
b2edef6f | 139 | #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT |
cf760e4c | 140 | void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum ) |
c801d85f | 141 | { |
7e548f6b | 142 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true); |
c801d85f | 143 | } |
b2edef6f | 144 | #endif |
c801d85f | 145 | |
b2edef6f VZ |
146 | #ifdef _WX_WANT_ARRAY_DELETE_VOID |
147 | void wxObject::operator delete[] ( void *buf ) | |
c801d85f | 148 | { |
7e548f6b | 149 | wxDebugFree(buf, true); |
c801d85f KB |
150 | } |
151 | #endif | |
152 | ||
b2edef6f | 153 | #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT |
cf760e4c | 154 | void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
b2edef6f | 155 | { |
7e548f6b | 156 | wxDebugFree(buf, true); |
b2edef6f VZ |
157 | } |
158 | #endif | |
afb74891 | 159 | |
afb74891 | 160 | |
0b9ab0bd RL |
161 | // ---------------------------------------------------------------------------- |
162 | // wxClassInfo | |
163 | // ---------------------------------------------------------------------------- | |
c801d85f | 164 | |
aa4b7ef9 VZ |
165 | wxClassInfo::~wxClassInfo() |
166 | { | |
167 | // remove this object from the linked list of all class infos: if we don't | |
168 | // do it, loading/unloading a DLL containing static wxClassInfo objects is | |
169 | // not going to work | |
170 | if ( this == sm_first ) | |
171 | { | |
172 | sm_first = m_next; | |
173 | } | |
174 | else | |
175 | { | |
176 | wxClassInfo *info = sm_first; | |
177 | while (info) | |
178 | { | |
179 | if ( info->m_next == this ) | |
180 | { | |
181 | info->m_next = m_next; | |
182 | break; | |
183 | } | |
184 | ||
185 | info = info->m_next; | |
186 | } | |
187 | } | |
7e548f6b | 188 | Unregister(); |
aa4b7ef9 VZ |
189 | } |
190 | ||
c1e10a22 | 191 | wxClassInfo *wxClassInfo::FindClass(const wxString& className) |
c801d85f | 192 | { |
80e8062f VZ |
193 | if ( sm_classTable ) |
194 | { | |
195 | return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className); | |
196 | } | |
197 | else | |
198 | { | |
199 | for ( wxClassInfo *info = sm_first; info ; info = info->m_next ) | |
200 | { | |
c1e10a22 | 201 | if ( className == info->GetClassName() ) |
80e8062f VZ |
202 | return info; |
203 | } | |
204 | ||
205 | return NULL; | |
206 | } | |
c801d85f KB |
207 | } |
208 | ||
52798331 MW |
209 | // Reentrance can occur on some platforms (Solaris for one), as the use of hash |
210 | // and string objects can cause other modules to load and register classes | |
211 | // before the original call returns. This is handled by keeping the hash table | |
212 | // local when it is first created and only assigning it to the global variable | |
213 | // when the function is ready to return. | |
214 | // | |
215 | // That does make the assumption that after the function has completed the | |
216 | // first time the problem will no longer happen; all the modules it depends on | |
217 | // will have been loaded. The assumption is checked using the 'entry' variable | |
218 | // as a reentrance guard, it checks that once the hash table is global it is | |
219 | // not accessed multiple times simulateously. | |
f5256c59 | 220 | |
d1d738f1 VS |
221 | void wxClassInfo::Register() |
222 | { | |
4b6a582b | 223 | #if wxDEBUG_LEVEL |
f5256c59 MW |
224 | // reentrance guard - see note above |
225 | static int entry = 0; | |
4b6a582b | 226 | #endif // wxDEBUG_LEVEL |
f5256c59 | 227 | |
52798331 MW |
228 | wxHashTable *classTable; |
229 | ||
230 | if ( !sm_classTable ) | |
231 | { | |
232 | // keep the hash local initially, reentrance is possible | |
233 | classTable = new wxHashTable(wxKEY_STRING); | |
234 | } | |
235 | else | |
236 | { | |
237 | // guard againt reentrance once the global has been created | |
9a83f860 | 238 | wxASSERT_MSG(++entry == 1, wxT("wxClassInfo::Register() reentrance")); |
52798331 MW |
239 | classTable = sm_classTable; |
240 | } | |
241 | ||
d9f0e932 VS |
242 | // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you |
243 | // link any object module twice mistakenly, or link twice against wx shared | |
244 | // library) will break this function because it will enter an infinite loop | |
245 | // and eventually die with "out of memory" - as this is quite hard to | |
246 | // detect if you're unaware of this, try to do some checks here. | |
52798331 | 247 | wxASSERT_MSG( classTable->Get(m_className) == NULL, |
47547fbe VZ |
248 | wxString::Format |
249 | ( | |
9a83f860 | 250 | wxT("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"), |
47547fbe VZ |
251 | m_className |
252 | ) | |
253 | ); | |
7e548f6b | 254 | |
52798331 MW |
255 | classTable->Put(m_className, (wxObject *)this); |
256 | ||
257 | // if we're using a local hash we need to try to make it global | |
258 | if ( sm_classTable != classTable ) | |
259 | { | |
260 | if ( !sm_classTable ) | |
261 | { | |
262 | // make the hash global | |
263 | sm_classTable = classTable; | |
264 | } | |
265 | else | |
266 | { | |
267 | // the gobal hash has already been created by a reentrant call, | |
268 | // so delete the local hash and try again | |
269 | delete classTable; | |
270 | Register(); | |
271 | } | |
272 | } | |
f5256c59 | 273 | |
4b6a582b | 274 | #if wxDEBUG_LEVEL |
52798331 | 275 | entry = 0; |
4b6a582b | 276 | #endif // wxDEBUG_LEVEL |
c801d85f KB |
277 | } |
278 | ||
d1d738f1 | 279 | void wxClassInfo::Unregister() |
c801d85f | 280 | { |
d1d738f1 VS |
281 | if ( sm_classTable ) |
282 | { | |
283 | sm_classTable->Delete(m_className); | |
cafc76a4 VS |
284 | if ( sm_classTable->GetCount() == 0 ) |
285 | { | |
5276b0a5 | 286 | wxDELETE(sm_classTable); |
cafc76a4 | 287 | } |
d1d738f1 | 288 | } |
0c32066b | 289 | } |
f4a8c29f | 290 | |
c1e10a22 | 291 | wxObject *wxCreateDynamicObject(const wxString& name) |
0c32066b | 292 | { |
4b6a582b | 293 | #if wxUSE_DEBUG_CONTEXT |
b2edef6f | 294 | DEBUG_PRINTF(wxObject *wxCreateDynamicObject) |
bbee61e5 | 295 | #endif |
61cca9d2 | 296 | |
b2edef6f | 297 | if ( wxClassInfo::sm_classTable ) |
0c32066b JS |
298 | { |
299 | wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name); | |
b2edef6f | 300 | return info ? info->CreateObject() : NULL; |
0c32066b | 301 | } |
b2edef6f | 302 | else // no sm_classTable yet |
0c32066b | 303 | { |
b2edef6f VZ |
304 | for ( wxClassInfo *info = wxClassInfo::sm_first; |
305 | info; | |
306 | info = info->m_next ) | |
307 | { | |
cf2f341a | 308 | if (info->m_className && wxStrcmp(info->m_className, name) == 0) |
0c32066b | 309 | return info->CreateObject(); |
b2edef6f VZ |
310 | } |
311 | ||
312 | return NULL; | |
0c32066b | 313 | } |
c801d85f KB |
314 | } |
315 | ||
644cb537 MB |
316 | // iterator interface |
317 | wxClassInfo::const_iterator::value_type | |
318 | wxClassInfo::const_iterator::operator*() const | |
319 | { | |
320 | return (wxClassInfo*)m_node->GetData(); | |
321 | } | |
322 | ||
323 | wxClassInfo::const_iterator& wxClassInfo::const_iterator::operator++() | |
324 | { | |
325 | m_node = m_table->Next(); | |
326 | return *this; | |
327 | } | |
328 | ||
329 | const wxClassInfo::const_iterator wxClassInfo::const_iterator::operator++(int) | |
330 | { | |
331 | wxClassInfo::const_iterator tmp = *this; | |
332 | m_node = m_table->Next(); | |
333 | return tmp; | |
334 | } | |
335 | ||
336 | wxClassInfo::const_iterator wxClassInfo::begin_classinfo() | |
337 | { | |
338 | sm_classTable->BeginFind(); | |
339 | ||
340 | return const_iterator(sm_classTable->Next(), sm_classTable); | |
341 | } | |
342 | ||
343 | wxClassInfo::const_iterator wxClassInfo::end_classinfo() | |
344 | { | |
345 | return const_iterator(NULL, NULL); | |
346 | } | |
7a4b9130 | 347 | |
4a11340a RR |
348 | // ---------------------------------------------------------------------------- |
349 | // wxObjectRefData | |
350 | // ---------------------------------------------------------------------------- | |
351 | ||
deff6d72 | 352 | void wxRefCounter::DecRef() |
4a11340a | 353 | { |
deff6d72 VZ |
354 | wxASSERT_MSG( m_count > 0, "invalid ref data count" ); |
355 | ||
4a11340a RR |
356 | if ( --m_count == 0 ) |
357 | delete this; | |
358 | } | |
359 | ||
360 | ||
0b9ab0bd | 361 | // ---------------------------------------------------------------------------- |
a6391d30 | 362 | // wxObject |
0b9ab0bd | 363 | // ---------------------------------------------------------------------------- |
c801d85f KB |
364 | |
365 | void wxObject::Ref(const wxObject& clone) | |
366 | { | |
4b6a582b | 367 | #if wxUSE_DEBUG_CONTEXT |
807d8487 | 368 | DEBUG_PRINTF(wxObject::Ref) |
bbee61e5 | 369 | #endif |
0b9ab0bd | 370 | |
c89f5c02 RR |
371 | // nothing to be done |
372 | if (m_refData == clone.m_refData) | |
373 | return; | |
374 | ||
0b9ab0bd | 375 | // delete reference to old data |
c801d85f | 376 | UnRef(); |
0b9ab0bd | 377 | |
c801d85f | 378 | // reference new data |
807d8487 | 379 | if ( clone.m_refData ) |
0b9ab0bd | 380 | { |
c801d85f | 381 | m_refData = clone.m_refData; |
4a11340a | 382 | m_refData->IncRef(); |
c801d85f KB |
383 | } |
384 | } | |
385 | ||
afb74891 | 386 | void wxObject::UnRef() |
c801d85f | 387 | { |
807d8487 | 388 | if ( m_refData ) |
4fe5383d | 389 | { |
4a11340a | 390 | m_refData->DecRef(); |
807d8487 | 391 | m_refData = NULL; |
c801d85f | 392 | } |
c801d85f KB |
393 | } |
394 | ||
807d8487 VZ |
395 | void wxObject::AllocExclusive() |
396 | { | |
397 | if ( !m_refData ) | |
398 | { | |
399 | m_refData = CreateRefData(); | |
400 | } | |
401 | else if ( m_refData->GetRefCount() > 1 ) | |
402 | { | |
403 | // note that ref is not going to be destroyed in this case | |
a9bf8315 | 404 | const wxObjectRefData* ref = m_refData; |
807d8487 VZ |
405 | UnRef(); |
406 | ||
407 | // ... so we can still access it | |
408 | m_refData = CloneRefData(ref); | |
409 | } | |
410 | //else: ref count is 1, we are exclusive owners of m_refData anyhow | |
411 | ||
412 | wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1, | |
9a83f860 | 413 | wxT("wxObject::AllocExclusive() failed.") ); |
807d8487 VZ |
414 | } |
415 | ||
416 | wxObjectRefData *wxObject::CreateRefData() const | |
417 | { | |
d1870078 | 418 | // if you use AllocExclusive() you must override this method |
9a83f860 | 419 | wxFAIL_MSG( wxT("CreateRefData() must be overridden if called!") ); |
d1870078 | 420 | |
807d8487 VZ |
421 | return NULL; |
422 | } | |
423 | ||
a9bf8315 VZ |
424 | wxObjectRefData * |
425 | wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const | |
807d8487 | 426 | { |
d1870078 | 427 | // if you use AllocExclusive() you must override this method |
9a83f860 | 428 | wxFAIL_MSG( wxT("CloneRefData() must be overridden if called!") ); |
d1870078 | 429 | |
807d8487 VZ |
430 | return NULL; |
431 | } |