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
38 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
39 #include "wx/memory.h"
42 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
44 #include "wx/ioswrap.h"
47 #if !USE_SHARED_LIBRARY
48 wxClassInfo
wxObject::sm_classwxObject((char *) "wxObject", (char *) NULL
, (char *) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
);
49 wxClassInfo
* wxClassInfo::sm_first
= (wxClassInfo
*) NULL
;
50 wxHashTable
* wxClassInfo::sm_classTable
= (wxHashTable
*) NULL
;
54 * wxWindows root object.
59 m_refData
= (wxObjectRefData
*) NULL
;
61 m_serialObj
= (wxObject_Serialize
*)NULL
;
75 * Is this object a kind of (a subclass of) 'info'?
76 * E.g. is wxWindow a kind of wxObject?
77 * Go from this class to superclass, taking into account
78 * two possible base classes.
81 bool wxObject::IsKindOf(wxClassInfo
*info
) const
83 wxClassInfo
*thisInfo
= GetClassInfo();
85 return thisInfo
->IsKindOf(info
);
90 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
91 void wxObject::Dump(ostream
& str
)
93 if (GetClassInfo() && GetClassInfo()->GetClassName())
94 str
<< GetClassInfo()->GetClassName();
96 str
<< "unknown object class";
100 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
106 void *wxObject::operator new (size_t size
, char * fileName
, int lineNum
)
108 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
111 void wxObject::operator delete (void * buf
)
117 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
118 void wxObject::operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
120 ::operator delete(pData
);
124 // Cause problems for VC++ - crashes
125 #if !defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS
126 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
)
128 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
131 void wxObject::operator delete[] (void * buf
)
133 wxDebugFree(buf
, TRUE
);
140 * Class info: provides run-time class type information.
143 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
)
146 m_baseClassName1
= baseName1
;
147 m_baseClassName2
= baseName2
;
150 m_objectConstructor
= constr
;
155 m_baseInfo1
= (wxClassInfo
*) NULL
;
156 m_baseInfo2
= (wxClassInfo
*) NULL
;
159 wxObject
*wxClassInfo::CreateObject()
161 if (m_objectConstructor
)
162 return (wxObject
*)(*m_objectConstructor
)();
164 return (wxObject
*) NULL
;
167 wxClassInfo
*wxClassInfo::FindClass(char *c
)
169 wxClassInfo
*p
= sm_first
;
172 if (p
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0)
176 return (wxClassInfo
*) NULL
;
179 // Climb upwards through inheritance hierarchy.
180 // Dual inheritance is catered for.
181 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
186 // For some reason, when making/using a DLL, static data has to be included
187 // in both the DLL and the application. This can lead to duplicate
188 // wxClassInfo objects, so we have to test the name instead of the pointers.
189 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
192 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
200 if (m_baseInfo1
->IsKindOf(info
))
204 return m_baseInfo2
->IsKindOf(info
);
209 // Set pointers to base class(es) to speed up IsKindOf
210 void wxClassInfo::InitializeClasses()
212 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
214 // Index all class infos by their class name
215 wxClassInfo
*info
= sm_first
;
218 if (info
->m_className
)
219 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
223 // Set base pointers for each wxClassInfo
227 if (info
->GetBaseClassName1())
228 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
229 if (info
->GetBaseClassName2())
230 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
235 void wxClassInfo::CleanUpClasses()
237 delete wxClassInfo::sm_classTable
;
238 wxClassInfo::sm_classTable
= NULL
;
241 wxObject
*wxCreateDynamicObject(const char *name
)
243 if (wxClassInfo::sm_classTable
)
245 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
247 return (wxObject
*)NULL
;
249 return info
->CreateObject();
253 wxClassInfo
*info
= wxClassInfo::sm_first
;
256 if (info
->m_className
&& strcmp(info
->m_className
, name
) == 0)
257 return info
->CreateObject();
260 return (wxObject
*) NULL
;
262 return (wxObject
*) NULL
;
267 #include "wx/serbase.h"
268 #include "wx/dynlib.h"
269 #include "wx/msgdlg.h"
271 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
273 wxObjectInputStream
obj_s(stream
);
274 return obj_s
.LoadObject();
277 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
279 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
280 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
283 wxLogError(_("Can't load wxSerial dynamic library."));
287 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
290 wxLogError(_("Can't find the serialization object '%s' "
291 "for the object '%s'."),
293 GetClassInfo()->GetClassName());
296 m_serialObj
->SetObject(this);
299 m_serialObj
->StoreObject(stream
);
302 void wxObject::LoadObject( wxObjectInputStream
& stream
)
304 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
305 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
308 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
311 wxLogError(_("Can't find the serialization object '%s' "
312 "for the object '%s'."),
314 GetClassInfo()->GetClassName());
317 m_serialObj
->SetObject(this);
320 m_serialObj
->LoadObject(stream
);
323 #endif // wxUSE_SERIAL
326 * wxObject: cloning of objects
329 void wxObject::Ref(const wxObject
& clone
)
331 // delete reference to old data
333 // reference new data
334 if (clone
.m_refData
) {
335 m_refData
= clone
.m_refData
;
336 ++(m_refData
->m_count
);
340 void wxObject::UnRef()
343 assert(m_refData
->m_count
> 0);
344 --(m_refData
->m_count
);
345 if (m_refData
->m_count
== 0)
348 m_refData
= (wxObjectRefData
*) NULL
;
355 wxObjectRefData::wxObjectRefData(void) : m_count(1)
359 wxObjectRefData::~wxObjectRefData()
363 // These are here so we can avoid 'always true/false' warnings
364 // by referring to these instead of TRUE/FALSE
365 const bool wxTrue
= TRUE
;
366 const bool wxFalse
= FALSE
;