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