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