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