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
45 # include <iostream.h>
54 #if !USE_SHARED_LIBRARY
55 wxClassInfo
wxObject::sm_classwxObject((char *) "wxObject", (char *) NULL
, (char *) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
);
56 wxClassInfo
* wxClassInfo::sm_first
= (wxClassInfo
*) NULL
;
57 wxHashTable
* wxClassInfo::sm_classTable
= (wxHashTable
*) NULL
;
61 * wxWindows root object.
64 wxObject::wxObject(void)
66 m_refData
= (wxObjectRefData
*) NULL
;
68 m_serialObj
= (wxObject_Serialize
*)NULL
;
72 wxObject::~wxObject(void)
82 * Is this object a kind of (a subclass of) 'info'?
83 * E.g. is wxWindow a kind of wxObject?
84 * Go from this class to superclass, taking into account
85 * two possible base classes.
88 bool wxObject::IsKindOf(wxClassInfo
*info
) const
90 wxClassInfo
*thisInfo
= GetClassInfo();
92 return thisInfo
->IsKindOf(info
);
97 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
98 void wxObject::Dump(ostream
& str
)
100 if (GetClassInfo() && GetClassInfo()->GetClassName())
101 str
<< GetClassInfo()->GetClassName();
103 str
<< "unknown object class";
107 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
113 void * wxObject::operator new (size_t size
, char * fileName
, int lineNum
)
115 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
118 void wxObject::operator delete (void * buf
)
125 void wxObject::operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
127 ::operator delete(pData
);
131 // Cause problems for VC++ - crashes
132 #if !defined(_MSC_VER) && wxUSE_ARRAY_MEMORY_OPERATORS
133 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
)
135 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
138 void wxObject::operator delete[] (void * buf
)
140 wxDebugFree(buf
, TRUE
);
147 * Class info: provides run-time class type information.
150 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
)
153 m_baseClassName1
= baseName1
;
154 m_baseClassName2
= baseName2
;
157 m_objectConstructor
= constr
;
162 m_baseInfo1
= (wxClassInfo
*) NULL
;
163 m_baseInfo2
= (wxClassInfo
*) NULL
;
166 wxObject
*wxClassInfo::CreateObject(void)
168 if (m_objectConstructor
)
169 return (wxObject
*)(*m_objectConstructor
)();
171 return (wxObject
*) NULL
;
174 wxClassInfo
*wxClassInfo::FindClass(char *c
)
176 wxClassInfo
*p
= sm_first
;
179 if (p
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0)
183 return (wxClassInfo
*) NULL
;
186 // Climb upwards through inheritance hierarchy.
187 // Dual inheritance is catered for.
188 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
193 // For some reason, when making/using a DLL, static data has to be included
194 // in both the DLL and the application. This can lead to duplicate
195 // wxClassInfo objects, so we have to test the name instead of the pointers.
196 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
199 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
207 if (m_baseInfo1
->IsKindOf(info
))
211 return m_baseInfo2
->IsKindOf(info
);
216 // Set pointers to base class(es) to speed up IsKindOf
217 void wxClassInfo::InitializeClasses(void)
219 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
221 // Index all class infos by their class name
222 wxClassInfo
*info
= sm_first
;
225 if (info
->m_className
)
226 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
230 // Set base pointers for each wxClassInfo
234 if (info
->GetBaseClassName1())
235 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
236 if (info
->GetBaseClassName2())
237 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
242 void wxClassInfo::CleanUpClasses(void)
244 delete wxClassInfo::sm_classTable
;
245 wxClassInfo::sm_classTable
= NULL
;
248 wxObject
*wxCreateDynamicObject(const char *name
)
250 if (wxClassInfo::sm_classTable
)
252 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
254 return (wxObject
*)NULL
;
256 return info
->CreateObject();
260 wxClassInfo
*info
= wxClassInfo::sm_first
;
263 if (info
->m_className
&& strcmp(info
->m_className
, name
) == 0)
264 return info
->CreateObject();
267 return (wxObject
*) NULL
;
269 return (wxObject
*) NULL
;
274 #include "wx/serbase.h"
275 #include "wx/dynlib.h"
276 #include "wx/msgdlg.h"
278 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
280 wxObjectInputStream
obj_s(stream
);
281 return obj_s
.LoadObject();
284 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
286 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
287 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
290 wxLogError(_("Can't load wxSerial dynamic library."));
294 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
297 wxLogError(_("Can't find the serialization object '%s' "
298 "for the object '%s'."),
300 GetClassInfo()->GetClassName());
303 m_serialObj
->SetObject(this);
306 m_serialObj
->StoreObject(stream
);
309 void wxObject::LoadObject( wxObjectInputStream
& stream
)
311 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
312 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
315 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
318 wxLogError(_("Can't find the serialization object '%s' "
319 "for the object '%s'."),
321 GetClassInfo()->GetClassName());
324 m_serialObj
->SetObject(this);
327 m_serialObj
->LoadObject(stream
);
333 * wxObject: cloning of objects
336 void wxObject::Ref(const wxObject
& clone
)
338 // delete reference to old data
340 // reference new data
341 if (clone
.m_refData
) {
342 m_refData
= clone
.m_refData
;
343 ++(m_refData
->m_count
);
347 void wxObject::UnRef(void)
350 assert(m_refData
->m_count
> 0);
351 --(m_refData
->m_count
);
352 if (m_refData
->m_count
== 0)
355 m_refData
= (wxObjectRefData
*) NULL
;
362 wxObjectRefData::wxObjectRefData(void) : m_count(1)
366 wxObjectRefData::~wxObjectRefData(void)
370 // These are here so we can avoid 'always true/false' warnings
371 // by referring to these instead of TRUE/FALSE
372 const bool wxTrue
= TRUE
;
373 const bool wxFalse
= FALSE
;