]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/object.cpp
Added simple instructions and autogen.mk to rebuild configure script.
[wxWidgets.git] / src / common / object.cpp
... / ...
CommitLineData
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
52const 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
71wxClassInfo 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
81wxClassInfo* wxClassInfo::sm_first = NULL;
82wxHashTable* 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
92wxClassInfo *wxObject::GetClassInfo() const
93{
94 return &wxObject::ms_classInfo;
95}
96
97#endif // wxUSE_EXTENDED_RTTI
98
99// this variable exists only so that we can avoid 'always true/false' warnings
100const bool wxFalse = false;
101
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.
106bool wxObject::IsKindOf(wxClassInfo *info) const
107{
108 wxClassInfo *thisInfo = GetClassInfo();
109 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
110}
111
112#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
113 #undef new
114#endif
115
116
117#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
118void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
119{
120 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
121}
122#endif
123
124#ifdef _WX_WANT_DELETE_VOID
125void wxObject::operator delete ( void *buf )
126{
127 wxDebugFree(buf);
128}
129#endif
130
131#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
132void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
133{
134 wxDebugFree(buf);
135}
136#endif
137
138#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
139void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
140{
141 wxDebugFree(buf);
142}
143#endif
144
145#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
146void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
147{
148 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
149}
150#endif
151
152#ifdef _WX_WANT_ARRAY_DELETE_VOID
153void wxObject::operator delete[] ( void *buf )
154{
155 wxDebugFree(buf, true);
156}
157#endif
158
159#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
160void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
161{
162 wxDebugFree(buf, true);
163}
164#endif
165
166
167// ----------------------------------------------------------------------------
168// wxClassInfo
169// ----------------------------------------------------------------------------
170
171wxClassInfo::~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 }
194 Unregister();
195}
196
197wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
198{
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 }
213}
214
215void wxClassInfo::CleanUp()
216{
217 if ( sm_classTable )
218 {
219 delete sm_classTable;
220 sm_classTable = NULL;
221 }
222}
223
224void wxClassInfo::Register()
225{
226 if ( !sm_classTable )
227 {
228 sm_classTable = new wxHashTable(wxKEY_STRING);
229 }
230
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.
236 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
237 wxString::Format
238 (
239 _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
240 m_className
241 )
242 );
243
244 sm_classTable->Put(m_className, (wxObject *)this);
245}
246
247void wxClassInfo::Unregister()
248{
249 if ( sm_classTable )
250 {
251 sm_classTable->Delete(m_className);
252 if ( sm_classTable->GetCount() == 0 )
253 {
254 delete sm_classTable;
255 sm_classTable = NULL;
256 }
257 }
258}
259
260wxObject *wxCreateDynamicObject(const wxChar *name)
261{
262#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
263 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
264#endif
265
266 if ( wxClassInfo::sm_classTable )
267 {
268 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
269 return info ? info->CreateObject() : NULL;
270 }
271 else // no sm_classTable yet
272 {
273 for ( wxClassInfo *info = wxClassInfo::sm_first;
274 info;
275 info = info->m_next )
276 {
277 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
278 return info->CreateObject();
279 }
280
281 return NULL;
282 }
283}
284
285
286// ----------------------------------------------------------------------------
287// wxObject
288// ----------------------------------------------------------------------------
289
290// Initialize ref data from another object (needed for copy constructor and
291// assignment operator)
292void wxObject::InitFrom(const wxObject& other)
293{
294 m_refData = other.m_refData;
295 if ( m_refData )
296 m_refData->m_count++;
297}
298
299void wxObject::Ref(const wxObject& clone)
300{
301#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
302 DEBUG_PRINTF(wxObject::Ref)
303#endif
304
305 // nothing to be done
306 if (m_refData == clone.m_refData)
307 return;
308
309 // delete reference to old data
310 UnRef();
311
312 // reference new data
313 if ( clone.m_refData )
314 {
315 m_refData = clone.m_refData;
316 ++(m_refData->m_count);
317 }
318}
319
320void wxObject::UnRef()
321{
322 if ( m_refData )
323 {
324 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
325
326 if ( !--m_refData->m_count )
327 delete m_refData;
328 m_refData = NULL;
329 }
330}
331
332void 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
341 const wxObjectRefData* ref = m_refData;
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
353wxObjectRefData *wxObject::CreateRefData() const
354{
355 // if you use AllocExclusive() you must override this method
356 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
357
358 return NULL;
359}
360
361wxObjectRefData *
362wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
363{
364 // if you use AllocExclusive() you must override this method
365 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
366
367 return NULL;
368}