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"
25 #include "wx/objstrm.h"
31 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
32 #include "wx/memory.h"
35 #if WXDEBUG || USE_DEBUG_CONTEXT
40 #if !USE_SHARED_LIBRARY
41 wxClassInfo
wxObject::classwxObject("wxObject", NULL
, NULL
, sizeof(wxObject
), NULL
);
42 wxClassInfo
*wxClassInfo::first
= NULL
;
46 * wxWindows root object.
49 wxObject::wxObject(void)
54 wxObject::~wxObject(void)
60 * Is this object a kind of (a subclass of) 'info'?
61 * E.g. is wxWindow a kind of wxObject?
62 * Go from this class to superclass, taking into account
63 * two possible base classes.
66 bool wxObject::IsKindOf(wxClassInfo
*info
)
68 wxClassInfo
*thisInfo
= GetClassInfo();
70 return thisInfo
->IsKindOf(info
);
75 #if WXDEBUG || USE_DEBUG_CONTEXT
76 void wxObject::Dump(ostream
& str
)
78 if (GetClassInfo() && GetClassInfo()->GetClassName())
79 str
<< GetClassInfo()->GetClassName();
81 str
<< "unknown object class";
85 #if WXDEBUG && USE_MEMORY_TRACING
91 void * wxObject::operator new (size_t size
, char * fileName
, int lineNum
)
93 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
96 void wxObject::operator delete (void * buf
)
101 // Cause problems for VC++ - crashes
103 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
)
105 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
108 void wxObject::operator delete[] (void * buf
)
110 wxDebugFree(buf
, TRUE
);
117 * Class info: provides run-time class type information.
120 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
)
123 baseClassName1
= baseName1
;
124 baseClassName2
= baseName2
;
127 objectConstructor
= constr
;
136 wxObject
*wxClassInfo::CreateObject(void)
138 if (objectConstructor
)
139 return (wxObject
*)(*objectConstructor
)();
144 wxClassInfo
*wxClassInfo::FindClass(char *c
)
146 wxClassInfo
*p
= first
;
149 if (p
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0)
156 // Climb upwards through inheritance hierarchy.
157 // Dual inheritance is catered for.
158 bool wxClassInfo::IsKindOf(wxClassInfo
*info
)
163 // For some reason, when making/using a DLL, static data has to be included
164 // in both the DLL and the application. This can lead to duplicate
165 // wxClassInfo objects, so we have to test the name instead of the pointers.
167 if (GetClassName() && info
->GetClassName() && (strcmp(GetClassName(), info
->GetClassName()) == 0))
175 if (baseInfo1
->IsKindOf(info
))
179 return baseInfo2
->IsKindOf(info
);
184 // Set pointers to base class(es) to speed up IsKindOf
185 void wxClassInfo::InitializeClasses(void)
187 wxHashTable
table(wxKEY_STRING
);
189 // Index all class infos by their class name
190 wxClassInfo
*info
= first
;
194 table
.Put(info
->className
, (wxObject
*)info
);
198 // Set base pointers for each wxClassInfo
202 if (info
->GetBaseClassName1())
203 info
->baseInfo1
= (wxClassInfo
*)table
.Get(info
->GetBaseClassName1());
204 if (info
->GetBaseClassName2())
205 info
->baseInfo2
= (wxClassInfo
*)table
.Get(info
->GetBaseClassName2());
210 wxObject
*wxCreateDynamicObject(char *name
)
212 wxClassInfo
*info
= wxClassInfo::first
;
215 if (info
->className
&& strcmp(info
->className
, name
) == 0)
216 return info
->CreateObject();
222 #ifdef USE_STORABLE_CLASSES
224 #include "wx/serbase.h"
225 #include "wx/dynlib.h"
226 #include "wx/msgdlg.h"
228 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
230 wxObjectInputStream
obj_s(stream
);
231 return obj_s
.LoadObject();
234 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
236 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
237 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
238 WXSERIAL(wxObject
) *serial
=
239 (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
244 message
.Printf("Can't find the serialization object (%s) for the object %s",
245 WXSTRINGCAST obj_name
, WXSTRINGCAST
GetClassInfo()->GetClassName());
246 wxMessageBox(message
, "Alert !");
250 serial
->SetObject(this);
251 serial
->StoreObject(stream
);
254 void wxObject::LoadObject( wxObjectInputStream
& stream
)
256 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
257 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
258 WXSERIAL(wxObject
) *serial
=
259 (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
264 message
.Printf("Can't find the serialization object (%s) for the object %s",
265 WXSTRINGCAST obj_name
,
266 WXSTRINGCAST
GetClassInfo()->GetClassName());
267 wxMessageBox(message
, "Alert !");
271 serial
->SetObject(this);
272 serial
->LoadObject(stream
);
278 * wxObject: cloning of objects
281 void wxObject::Ref(const wxObject
& clone
)
283 // delete reference to old data
285 // reference new data
286 if (clone
.m_refData
) {
287 m_refData
= clone
.m_refData
;
288 ++(m_refData
->m_count
);
292 void wxObject::UnRef(void)
295 assert(m_refData
->m_count
> 0);
296 --(m_refData
->m_count
);
297 if (m_refData
->m_count
== 0)
307 wxObjectRefData::wxObjectRefData(void) : m_count(1)
311 wxObjectRefData::~wxObjectRefData(void)