]>
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 |
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 |
431346ff SC |
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 ) | |
a095505c | 58 | { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; } |
431346ff | 59 | wxClassInfo wxObject::sm_classwxObject(sm_classParentswxObject , wxT("") , wxT("wxObject"), |
a095505c | 60 | (int) sizeof(wxObject), \ |
431346ff SC |
61 | (wxObjectConstructorFn) 0 , |
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) ;} | |
a095505c | 68 | template<> const wxTypeInfo* wxGetTypeInfo( wxObject ** ) |
066f1b7a | 69 | { static wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::sm_classwxObject) ; return &s_typeInfo ; } |
431346ff SC |
70 | template<> const wxTypeInfo* wxGetTypeInfo( wxObject * ) |
71 | { static wxClassTypeInfo s_typeInfo(wxT_OBJECT , &wxObject::sm_classwxObject) ; return &s_typeInfo ; } | |
a095505c | 72 | #else |
0b9ab0bd RL |
73 | wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0, |
74 | (int) sizeof(wxObject), | |
75 | (wxObjectConstructorFn) 0 ); | |
a095505c | 76 | #endif |
c801d85f | 77 | |
e1720942 | 78 | // restore optimizations |
fbae49ee | 79 | #if defined __VISUALC__ && __VISUALC__ >= 1300 |
e1720942 VZ |
80 | #pragma optimize("", on) |
81 | #endif | |
82 | ||
b2edef6f VZ |
83 | wxClassInfo* wxClassInfo::sm_first = NULL; |
84 | wxHashTable* wxClassInfo::sm_classTable = NULL; | |
c801d85f | 85 | |
b2edef6f VZ |
86 | // These are here so we can avoid 'always true/false' warnings |
87 | // by referring to these instead of TRUE/FALSE | |
0b9ab0bd RL |
88 | const bool wxTrue = TRUE; |
89 | const bool wxFalse = FALSE; | |
c801d85f | 90 | |
b2edef6f VZ |
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. | |
341287bf | 95 | bool wxObject::IsKindOf(wxClassInfo *info) const |
c801d85f | 96 | { |
afb74891 | 97 | wxClassInfo *thisInfo = GetClassInfo(); |
0b9ab0bd | 98 | return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ; |
c801d85f KB |
99 | } |
100 | ||
cf760e4c VZ |
101 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new ) |
102 | #undef new | |
103 | #endif | |
104 | ||
105 | ||
b2edef6f | 106 | #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT |
cf760e4c | 107 | void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum ) |
c801d85f | 108 | { |
cf760e4c | 109 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE); |
c801d85f | 110 | } |
b2edef6f | 111 | #endif |
c801d85f | 112 | |
b2edef6f VZ |
113 | #ifdef _WX_WANT_DELETE_VOID |
114 | void wxObject::operator delete ( void *buf ) | |
415ca026 DW |
115 | { |
116 | wxDebugFree(buf); | |
117 | } | |
b2edef6f VZ |
118 | #endif |
119 | ||
120 | #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET | |
121 | void wxObject::operator delete ( void *buf, const char *_fname, size_t _line ) | |
c801d85f | 122 | { |
afb74891 | 123 | wxDebugFree(buf); |
c801d85f | 124 | } |
0b9ab0bd RL |
125 | #endif |
126 | ||
b2edef6f | 127 | #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT |
cf760e4c | 128 | void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
76626af2 | 129 | { |
b2edef6f | 130 | wxDebugFree(buf); |
76626af2 JS |
131 | } |
132 | #endif | |
133 | ||
b2edef6f | 134 | #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT |
cf760e4c | 135 | void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum ) |
c801d85f | 136 | { |
cf760e4c | 137 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE); |
c801d85f | 138 | } |
b2edef6f | 139 | #endif |
c801d85f | 140 | |
b2edef6f VZ |
141 | #ifdef _WX_WANT_ARRAY_DELETE_VOID |
142 | void wxObject::operator delete[] ( void *buf ) | |
c801d85f | 143 | { |
afb74891 | 144 | wxDebugFree(buf, TRUE); |
c801d85f KB |
145 | } |
146 | #endif | |
147 | ||
b2edef6f | 148 | #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT |
cf760e4c | 149 | void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
b2edef6f VZ |
150 | { |
151 | wxDebugFree(buf, TRUE); | |
152 | } | |
153 | #endif | |
afb74891 | 154 | |
afb74891 | 155 | |
0b9ab0bd RL |
156 | // ---------------------------------------------------------------------------- |
157 | // wxClassInfo | |
158 | // ---------------------------------------------------------------------------- | |
c801d85f | 159 | |
aa4b7ef9 VZ |
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 | } | |
d1d738f1 | 183 | Unregister(); |
aa4b7ef9 VZ |
184 | } |
185 | ||
0b9ab0bd | 186 | wxClassInfo *wxClassInfo::FindClass(const wxChar *className) |
c801d85f | 187 | { |
80e8062f VZ |
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 | } | |
c801d85f KB |
202 | } |
203 | ||
cafc76a4 | 204 | void wxClassInfo::CleanUp() |
c801d85f | 205 | { |
cafc76a4 | 206 | if ( sm_classTable ) |
afb74891 | 207 | { |
cafc76a4 VS |
208 | delete sm_classTable; |
209 | sm_classTable = NULL; | |
afb74891 | 210 | } |
d1d738f1 | 211 | } |
afb74891 | 212 | |
d1d738f1 VS |
213 | void wxClassInfo::Register() |
214 | { | |
cafc76a4 | 215 | if ( !sm_classTable ) |
afb74891 | 216 | { |
cafc76a4 | 217 | sm_classTable = new wxHashTable(wxKEY_STRING); |
afb74891 | 218 | } |
cafc76a4 VS |
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); | |
c801d85f KB |
229 | } |
230 | ||
d1d738f1 | 231 | void wxClassInfo::Unregister() |
c801d85f | 232 | { |
d1d738f1 VS |
233 | if ( sm_classTable ) |
234 | { | |
235 | sm_classTable->Delete(m_className); | |
cafc76a4 VS |
236 | if ( sm_classTable->GetCount() == 0 ) |
237 | { | |
238 | delete sm_classTable; | |
239 | sm_classTable = NULL; | |
240 | } | |
d1d738f1 | 241 | } |
0c32066b | 242 | } |
f4a8c29f | 243 | |
cf2f341a | 244 | wxObject *wxCreateDynamicObject(const wxChar *name) |
0c32066b | 245 | { |
bbee61e5 | 246 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
b2edef6f | 247 | DEBUG_PRINTF(wxObject *wxCreateDynamicObject) |
bbee61e5 | 248 | #endif |
61cca9d2 | 249 | |
b2edef6f | 250 | if ( wxClassInfo::sm_classTable ) |
0c32066b JS |
251 | { |
252 | wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name); | |
b2edef6f | 253 | return info ? info->CreateObject() : NULL; |
0c32066b | 254 | } |
b2edef6f | 255 | else // no sm_classTable yet |
0c32066b | 256 | { |
b2edef6f VZ |
257 | for ( wxClassInfo *info = wxClassInfo::sm_first; |
258 | info; | |
259 | info = info->m_next ) | |
260 | { | |
cf2f341a | 261 | if (info->m_className && wxStrcmp(info->m_className, name) == 0) |
0c32066b | 262 | return info->CreateObject(); |
b2edef6f VZ |
263 | } |
264 | ||
265 | return NULL; | |
0c32066b | 266 | } |
c801d85f KB |
267 | } |
268 | ||
7a4b9130 | 269 | |
0b9ab0bd | 270 | // ---------------------------------------------------------------------------- |
a6391d30 | 271 | // wxObject |
0b9ab0bd | 272 | // ---------------------------------------------------------------------------- |
c801d85f | 273 | |
a6391d30 GD |
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 | ||
c801d85f KB |
283 | void wxObject::Ref(const wxObject& clone) |
284 | { | |
bbee61e5 | 285 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
807d8487 | 286 | DEBUG_PRINTF(wxObject::Ref) |
bbee61e5 | 287 | #endif |
0b9ab0bd | 288 | |
c89f5c02 RR |
289 | // nothing to be done |
290 | if (m_refData == clone.m_refData) | |
291 | return; | |
292 | ||
0b9ab0bd | 293 | // delete reference to old data |
c801d85f | 294 | UnRef(); |
0b9ab0bd | 295 | |
c801d85f | 296 | // reference new data |
807d8487 | 297 | if ( clone.m_refData ) |
0b9ab0bd | 298 | { |
c801d85f KB |
299 | m_refData = clone.m_refData; |
300 | ++(m_refData->m_count); | |
301 | } | |
302 | } | |
303 | ||
afb74891 | 304 | void wxObject::UnRef() |
c801d85f | 305 | { |
807d8487 | 306 | if ( m_refData ) |
4fe5383d VZ |
307 | { |
308 | wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") ); | |
309 | ||
310 | if ( !--m_refData->m_count ) | |
c801d85f | 311 | delete m_refData; |
807d8487 | 312 | m_refData = NULL; |
c801d85f | 313 | } |
c801d85f KB |
314 | } |
315 | ||
807d8487 VZ |
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 | |
a9bf8315 | 325 | const wxObjectRefData* ref = m_refData; |
807d8487 VZ |
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 | { | |
d1870078 VZ |
339 | // if you use AllocExclusive() you must override this method |
340 | wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") ); | |
341 | ||
807d8487 VZ |
342 | return NULL; |
343 | } | |
344 | ||
a9bf8315 VZ |
345 | wxObjectRefData * |
346 | wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const | |
807d8487 | 347 | { |
d1870078 VZ |
348 | // if you use AllocExclusive() you must override this method |
349 | wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") ); | |
350 | ||
807d8487 VZ |
351 | return NULL; |
352 | } |