1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "object.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/objstrm.h"
27 #include "wx/serbase.h"
32 #endif // wxUSE_SERIAL
36 #include "wx/objstrm.h"
37 #include "wx/serbase.h"
43 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
44 #include "wx/memory.h"
47 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
49 #include "wx/ioswrap.h"
50 #if defined(__VISAGECPP__)
51 // help with VA debugging
52 #define DEBUG_PRINTF(NAME) { static int raz=0; \
53 printf( #NAME " %i\n",raz); fflush(stdout); \
57 #define DEBUG_PRINTF(NAME)
61 wxClassInfo
wxObject::sm_classwxObject((wxChar
*) wxT("wxObject"), (wxChar
*) NULL
, (wxChar
*) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
);
62 wxClassInfo
* wxClassInfo::sm_first
= (wxClassInfo
*) NULL
;
63 wxHashTable
* wxClassInfo::sm_classTable
= (wxHashTable
*) NULL
;
65 // These are here so we can avoid 'always true/false' warnings
66 // by referring to these instead of TRUE/FALSE
67 const bool wxTrue
= TRUE
;
68 const bool wxFalse
= FALSE
;
71 * wxWindows root object.
76 m_refData
= (wxObjectRefData
*) NULL
;
78 m_serialObj
= (wxObject_Serialize
*)NULL
;
92 * Is this object a kind of (a subclass of) 'info'?
93 * E.g. is wxWindow a kind of wxObject?
94 * Go from this class to superclass, taking into account
95 * two possible base classes.
98 bool wxObject::IsKindOf(wxClassInfo
*info
) const
100 wxClassInfo
*thisInfo
= GetClassInfo();
102 return thisInfo
->IsKindOf(info
);
107 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
108 void wxObject::Dump(wxSTD ostream
& str
)
110 if (GetClassInfo() && GetClassInfo()->GetClassName())
111 str
<< GetClassInfo()->GetClassName();
113 str
<< "unknown object class";
117 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
123 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
125 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
128 #if defined(__VISAGECPP__)
130 void wxObject::operator delete (void * buf
,const char * _fname
, size_t _line
)
134 # endif //__DEBUG_ALLOC__
136 void wxObject::operator delete (void * buf
)
140 #endif // __VISAGECPP__
143 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
144 void wxObject::operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
146 ::operator delete(pData
);
150 // Cause problems for VC++ - crashes
151 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
152 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
154 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
157 void wxObject::operator delete[] (void * buf
)
159 wxDebugFree(buf
, TRUE
);
166 * Class info: provides run-time class type information.
169 wxClassInfo::wxClassInfo(const wxChar
*cName
,
170 const wxChar
*baseName1
,
171 const wxChar
*baseName2
,
173 wxObjectConstructorFn constr
)
176 m_baseClassName1
= baseName1
;
177 m_baseClassName2
= baseName2
;
180 m_objectConstructor
= constr
;
185 m_baseInfo1
= (wxClassInfo
*) NULL
;
186 m_baseInfo2
= (wxClassInfo
*) NULL
;
189 wxObject
*wxClassInfo::CreateObject()
191 if (m_objectConstructor
)
192 return (wxObject
*)(*m_objectConstructor
)();
194 return (wxObject
*) NULL
;
197 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*c
)
199 wxClassInfo
*p
= sm_first
;
202 if ( wxStrcmp(p
->GetClassName(), c
) == 0 )
211 // Climb upwards through inheritance hierarchy.
212 // Dual inheritance is catered for.
213 bool wxClassInfo::IsKindOf(const wxClassInfo
*info
) const
222 if (m_baseInfo1
->IsKindOf(info
))
226 return m_baseInfo2
->IsKindOf(info
);
231 // Set pointers to base class(es) to speed up IsKindOf
232 void wxClassInfo::InitializeClasses()
234 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
235 // link any object module twice mistakenly) will break this function
236 // because it will enter an infinite loop and eventually die with "out of
237 // memory" - as this is quite hard to detect if you're unaware of this,
238 // try to do some checks here
240 // more classes than we'll ever have
241 static const size_t nMaxClasses
= 10000;
245 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
247 // Index all class infos by their class name
248 wxClassInfo
*info
= sm_first
;
251 if (info
->m_className
)
253 wxASSERT_MSG( ++nClass
< nMaxClasses
,
254 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
256 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
262 // Set base pointers for each wxClassInfo
266 if (info
->GetBaseClassName1())
267 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
268 if (info
->GetBaseClassName2())
269 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
274 void wxClassInfo::CleanUpClasses()
276 delete wxClassInfo::sm_classTable
;
277 wxClassInfo::sm_classTable
= NULL
;
280 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
282 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
283 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
286 if (wxClassInfo::sm_classTable
)
288 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
290 return (wxObject
*)NULL
;
292 return info
->CreateObject();
296 wxClassInfo
*info
= wxClassInfo::sm_first
;
299 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
300 return info
->CreateObject();
303 return (wxObject
*) NULL
;
309 #include "wx/serbase.h"
310 #include "wx/dynlib.h"
312 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
314 wxObjectInputStream
obj_s(stream
);
315 return obj_s
.LoadObject();
318 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
320 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
321 DEBUG_PRINTF(wxObject::StoreObject
)
324 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
325 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
328 wxLogError(_("Can't load wxSerial dynamic library."));
332 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
335 wxLogError(_("Can't find the serialization object '%s' "
336 "for the object '%s'."),
338 GetClassInfo()->GetClassName());
341 m_serialObj
->SetObject(this);
344 m_serialObj
->StoreObject(stream
);
347 void wxObject::LoadObject( wxObjectInputStream
& stream
)
349 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
350 DEBUG_PRINTF(wxObject::LoadObject
)
353 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
354 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
357 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
360 wxLogError(_("Can't find the serialization object '%s' "
361 "for the object '%s'."),
363 GetClassInfo()->GetClassName());
366 m_serialObj
->SetObject(this);
369 m_serialObj
->LoadObject(stream
);
372 #endif // wxUSE_SERIAL
375 * wxObject: cloning of objects
378 void wxObject::Ref(const wxObject
& clone
)
380 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
381 DEBUG_PRINTF(wxObject::Ref
)
383 // delete reference to old data
385 // reference new data
386 if (clone
.m_refData
) {
387 m_refData
= clone
.m_refData
;
388 ++(m_refData
->m_count
);
392 void wxObject::UnRef()
396 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
398 if ( !--m_refData
->m_count
)
400 m_refData
= (wxObjectRefData
*) NULL
;
408 wxObjectRefData::wxObjectRefData(void) : m_count(1)
412 wxObjectRefData::~wxObjectRefData()
416 #if defined(__DARWIN__) && defined(DYLIB_INIT)
419 void __initialize_Cplusplus(void);
420 void wxWindowsDylibInit(void);
423 // Dynamic shared library (dylib) initialization routine
424 // required to initialize static C++ objects bacause of lazy dynamic linking
425 // http://developer.apple.com/techpubs/macosx/Essentials/
426 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
428 void wxWindowsDylibInit()
430 // The function __initialize_Cplusplus() must be called from the shared
431 // library initialization routine to cause the static C++ objects in
432 // the library to be initialized (reference number 2441683).
434 __initialize_Cplusplus();