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