]>
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 | ||
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 |
b2edef6f VZ |
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 | |
c801d85f | 43 | |
e1720942 VZ |
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 | |
fbae49ee | 47 | #if defined __VISUALC__ && __VISUALC__ >= 1300 |
e1720942 VZ |
48 | #pragma optimize("", off) |
49 | #endif | |
f6bcfd97 | 50 | |
30bfc425 | 51 | #if wxUSE_EXTENDED_RTTI |
05fa251a | 52 | const wxClassInfo* wxObject::ms_classParents[] = { NULL } ; |
431346ff | 53 | wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data ) |
2c4c3987 | 54 | { return data.wxTEMPLATED_MEMBER_CALL(Get , wxObject*) ; } |
431346ff | 55 | wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data ) |
2c4c3987 | 56 | { return &data.wxTEMPLATED_MEMBER_CALL(Get , wxObject) ; } |
431346ff | 57 | wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data ) |
a095505c | 58 | { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; } |
525d8583 | 59 | wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"), |
a095505c | 60 | (int) sizeof(wxObject), \ |
431346ff | 61 | (wxObjectConstructorFn) 0 , |
7e548f6b WS |
62 | (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 , |
63 | 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject); | |
a095505c SC |
64 | template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;} |
65 | template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;} | |
431346ff SC |
66 | template<> void wxStringReadValue(const wxString & , wxObject & ){assert(0) ;} |
67 | template<> void wxStringWriteValue(wxString & , wxObject const & ){assert(0) ;} | |
7e548f6b WS |
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() ) ; | |
a095505c | 70 | #else |
c0db9626 | 71 | wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0, |
0b9ab0bd RL |
72 | (int) sizeof(wxObject), |
73 | (wxObjectConstructorFn) 0 ); | |
a095505c | 74 | #endif |
c801d85f | 75 | |
e1720942 | 76 | // restore optimizations |
fbae49ee | 77 | #if defined __VISUALC__ && __VISUALC__ >= 1300 |
e1720942 VZ |
78 | #pragma optimize("", on) |
79 | #endif | |
80 | ||
b2edef6f VZ |
81 | wxClassInfo* wxClassInfo::sm_first = NULL; |
82 | wxHashTable* wxClassInfo::sm_classTable = NULL; | |
c801d85f | 83 | |
66d2fa18 VZ |
84 | // when using XTI, this method is already implemented inline inside |
85 | // DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function | |
86 | // non-inline because this allows us to have a non-inline virtual function in | |
87 | // all wx classes and this solves linking problems for HP-UX native toolchain | |
88 | // and possibly others (we could make dtor non-inline as well but it's more | |
89 | // useful to keep it inline than this function) | |
90 | #if !wxUSE_EXTENDED_RTTI | |
91 | ||
14ca93a0 VZ |
92 | wxClassInfo *wxObject::GetClassInfo() const |
93 | { | |
94 | return &wxObject::ms_classInfo; | |
95 | } | |
96 | ||
66d2fa18 VZ |
97 | #endif // wxUSE_EXTENDED_RTTI |
98 | ||
258e7b25 | 99 | // this variable exists only so that we can avoid 'always true/false' warnings |
7e548f6b | 100 | const bool wxFalse = false; |
c801d85f | 101 | |
b2edef6f VZ |
102 | // Is this object a kind of (a subclass of) 'info'? |
103 | // E.g. is wxWindow a kind of wxObject? | |
104 | // Go from this class to superclass, taking into account | |
105 | // two possible base classes. | |
341287bf | 106 | bool wxObject::IsKindOf(wxClassInfo *info) const |
c801d85f | 107 | { |
afb74891 | 108 | wxClassInfo *thisInfo = GetClassInfo(); |
7e548f6b | 109 | return (thisInfo) ? thisInfo->IsKindOf(info) : false ; |
c801d85f KB |
110 | } |
111 | ||
cf760e4c | 112 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new ) |
7e548f6b | 113 | #undef new |
cf760e4c VZ |
114 | #endif |
115 | ||
116 | ||
b2edef6f | 117 | #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT |
cf760e4c | 118 | void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum ) |
c801d85f | 119 | { |
7e548f6b | 120 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true); |
c801d85f | 121 | } |
b2edef6f | 122 | #endif |
c801d85f | 123 | |
b2edef6f VZ |
124 | #ifdef _WX_WANT_DELETE_VOID |
125 | void wxObject::operator delete ( void *buf ) | |
415ca026 DW |
126 | { |
127 | wxDebugFree(buf); | |
128 | } | |
b2edef6f VZ |
129 | #endif |
130 | ||
131 | #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET | |
132 | void wxObject::operator delete ( void *buf, const char *_fname, size_t _line ) | |
c801d85f | 133 | { |
afb74891 | 134 | wxDebugFree(buf); |
c801d85f | 135 | } |
0b9ab0bd RL |
136 | #endif |
137 | ||
b2edef6f | 138 | #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT |
cf760e4c | 139 | void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
76626af2 | 140 | { |
b2edef6f | 141 | wxDebugFree(buf); |
76626af2 JS |
142 | } |
143 | #endif | |
144 | ||
b2edef6f | 145 | #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT |
cf760e4c | 146 | void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum ) |
c801d85f | 147 | { |
7e548f6b | 148 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true); |
c801d85f | 149 | } |
b2edef6f | 150 | #endif |
c801d85f | 151 | |
b2edef6f VZ |
152 | #ifdef _WX_WANT_ARRAY_DELETE_VOID |
153 | void wxObject::operator delete[] ( void *buf ) | |
c801d85f | 154 | { |
7e548f6b | 155 | wxDebugFree(buf, true); |
c801d85f KB |
156 | } |
157 | #endif | |
158 | ||
b2edef6f | 159 | #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT |
cf760e4c | 160 | void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
b2edef6f | 161 | { |
7e548f6b | 162 | wxDebugFree(buf, true); |
b2edef6f VZ |
163 | } |
164 | #endif | |
afb74891 | 165 | |
afb74891 | 166 | |
0b9ab0bd RL |
167 | // ---------------------------------------------------------------------------- |
168 | // wxClassInfo | |
169 | // ---------------------------------------------------------------------------- | |
c801d85f | 170 | |
aa4b7ef9 VZ |
171 | wxClassInfo::~wxClassInfo() |
172 | { | |
173 | // remove this object from the linked list of all class infos: if we don't | |
174 | // do it, loading/unloading a DLL containing static wxClassInfo objects is | |
175 | // not going to work | |
176 | if ( this == sm_first ) | |
177 | { | |
178 | sm_first = m_next; | |
179 | } | |
180 | else | |
181 | { | |
182 | wxClassInfo *info = sm_first; | |
183 | while (info) | |
184 | { | |
185 | if ( info->m_next == this ) | |
186 | { | |
187 | info->m_next = m_next; | |
188 | break; | |
189 | } | |
190 | ||
191 | info = info->m_next; | |
192 | } | |
193 | } | |
7e548f6b | 194 | Unregister(); |
aa4b7ef9 VZ |
195 | } |
196 | ||
0b9ab0bd | 197 | wxClassInfo *wxClassInfo::FindClass(const wxChar *className) |
c801d85f | 198 | { |
80e8062f VZ |
199 | if ( sm_classTable ) |
200 | { | |
201 | return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className); | |
202 | } | |
203 | else | |
204 | { | |
205 | for ( wxClassInfo *info = sm_first; info ; info = info->m_next ) | |
206 | { | |
207 | if ( wxStrcmp(info->GetClassName(), className) == 0 ) | |
208 | return info; | |
209 | } | |
210 | ||
211 | return NULL; | |
212 | } | |
c801d85f KB |
213 | } |
214 | ||
cafc76a4 | 215 | void wxClassInfo::CleanUp() |
c801d85f | 216 | { |
cafc76a4 | 217 | if ( sm_classTable ) |
afb74891 | 218 | { |
cafc76a4 VS |
219 | delete sm_classTable; |
220 | sm_classTable = NULL; | |
afb74891 | 221 | } |
d1d738f1 | 222 | } |
afb74891 | 223 | |
d1d738f1 VS |
224 | void wxClassInfo::Register() |
225 | { | |
cafc76a4 | 226 | if ( !sm_classTable ) |
afb74891 | 227 | { |
cafc76a4 | 228 | sm_classTable = new wxHashTable(wxKEY_STRING); |
afb74891 | 229 | } |
cafc76a4 | 230 | |
d9f0e932 VS |
231 | // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you |
232 | // link any object module twice mistakenly, or link twice against wx shared | |
233 | // library) will break this function because it will enter an infinite loop | |
234 | // and eventually die with "out of memory" - as this is quite hard to | |
235 | // detect if you're unaware of this, try to do some checks here. | |
cafc76a4 | 236 | wxASSERT_MSG( sm_classTable->Get(m_className) == NULL, |
47547fbe VZ |
237 | wxString::Format |
238 | ( | |
f730d32b | 239 | _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"), |
47547fbe VZ |
240 | m_className |
241 | ) | |
242 | ); | |
7e548f6b | 243 | |
cafc76a4 | 244 | sm_classTable->Put(m_className, (wxObject *)this); |
c801d85f KB |
245 | } |
246 | ||
d1d738f1 | 247 | void wxClassInfo::Unregister() |
c801d85f | 248 | { |
d1d738f1 VS |
249 | if ( sm_classTable ) |
250 | { | |
251 | sm_classTable->Delete(m_className); | |
cafc76a4 VS |
252 | if ( sm_classTable->GetCount() == 0 ) |
253 | { | |
254 | delete sm_classTable; | |
255 | sm_classTable = NULL; | |
256 | } | |
d1d738f1 | 257 | } |
0c32066b | 258 | } |
f4a8c29f | 259 | |
cf2f341a | 260 | wxObject *wxCreateDynamicObject(const wxChar *name) |
0c32066b | 261 | { |
bbee61e5 | 262 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
b2edef6f | 263 | DEBUG_PRINTF(wxObject *wxCreateDynamicObject) |
bbee61e5 | 264 | #endif |
61cca9d2 | 265 | |
b2edef6f | 266 | if ( wxClassInfo::sm_classTable ) |
0c32066b JS |
267 | { |
268 | wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name); | |
b2edef6f | 269 | return info ? info->CreateObject() : NULL; |
0c32066b | 270 | } |
b2edef6f | 271 | else // no sm_classTable yet |
0c32066b | 272 | { |
b2edef6f VZ |
273 | for ( wxClassInfo *info = wxClassInfo::sm_first; |
274 | info; | |
275 | info = info->m_next ) | |
276 | { | |
cf2f341a | 277 | if (info->m_className && wxStrcmp(info->m_className, name) == 0) |
0c32066b | 278 | return info->CreateObject(); |
b2edef6f VZ |
279 | } |
280 | ||
281 | return NULL; | |
0c32066b | 282 | } |
c801d85f KB |
283 | } |
284 | ||
7a4b9130 | 285 | |
0b9ab0bd | 286 | // ---------------------------------------------------------------------------- |
a6391d30 | 287 | // wxObject |
0b9ab0bd | 288 | // ---------------------------------------------------------------------------- |
c801d85f | 289 | |
a6391d30 GD |
290 | // Initialize ref data from another object (needed for copy constructor and |
291 | // assignment operator) | |
292 | void wxObject::InitFrom(const wxObject& other) | |
293 | { | |
294 | m_refData = other.m_refData; | |
295 | if ( m_refData ) | |
296 | m_refData->m_count++; | |
297 | } | |
298 | ||
c801d85f KB |
299 | void wxObject::Ref(const wxObject& clone) |
300 | { | |
bbee61e5 | 301 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
807d8487 | 302 | DEBUG_PRINTF(wxObject::Ref) |
bbee61e5 | 303 | #endif |
0b9ab0bd | 304 | |
c89f5c02 RR |
305 | // nothing to be done |
306 | if (m_refData == clone.m_refData) | |
307 | return; | |
308 | ||
0b9ab0bd | 309 | // delete reference to old data |
c801d85f | 310 | UnRef(); |
0b9ab0bd | 311 | |
c801d85f | 312 | // reference new data |
807d8487 | 313 | if ( clone.m_refData ) |
0b9ab0bd | 314 | { |
c801d85f KB |
315 | m_refData = clone.m_refData; |
316 | ++(m_refData->m_count); | |
317 | } | |
318 | } | |
319 | ||
afb74891 | 320 | void wxObject::UnRef() |
c801d85f | 321 | { |
807d8487 | 322 | if ( m_refData ) |
4fe5383d VZ |
323 | { |
324 | wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") ); | |
325 | ||
326 | if ( !--m_refData->m_count ) | |
c801d85f | 327 | delete m_refData; |
807d8487 | 328 | m_refData = NULL; |
c801d85f | 329 | } |
c801d85f KB |
330 | } |
331 | ||
807d8487 VZ |
332 | void wxObject::AllocExclusive() |
333 | { | |
334 | if ( !m_refData ) | |
335 | { | |
336 | m_refData = CreateRefData(); | |
337 | } | |
338 | else if ( m_refData->GetRefCount() > 1 ) | |
339 | { | |
340 | // note that ref is not going to be destroyed in this case | |
a9bf8315 | 341 | const wxObjectRefData* ref = m_refData; |
807d8487 VZ |
342 | UnRef(); |
343 | ||
344 | // ... so we can still access it | |
345 | m_refData = CloneRefData(ref); | |
346 | } | |
347 | //else: ref count is 1, we are exclusive owners of m_refData anyhow | |
348 | ||
349 | wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1, | |
350 | _T("wxObject::AllocExclusive() failed.") ); | |
351 | } | |
352 | ||
353 | wxObjectRefData *wxObject::CreateRefData() const | |
354 | { | |
d1870078 VZ |
355 | // if you use AllocExclusive() you must override this method |
356 | wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") ); | |
357 | ||
807d8487 VZ |
358 | return NULL; |
359 | } | |
360 | ||
a9bf8315 VZ |
361 | wxObjectRefData * |
362 | wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const | |
807d8487 | 363 | { |
d1870078 VZ |
364 | // if you use AllocExclusive() you must override this method |
365 | wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") ); | |
366 | ||
807d8487 VZ |
367 | return NULL; |
368 | } |