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