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"
28 #if (DEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
29 #include "wx/memory.h"
32 #if DEBUG || USE_DEBUG_CONTEXT
37 #if !USE_SHARED_LIBRARY
38 wxClassInfo
wxObject::classwxObject("wxObject", NULL
, NULL
, sizeof(wxObject
), NULL
);
39 wxClassInfo
*wxClassInfo::first
= NULL
;
43 * wxWindows root object.
46 wxObject::wxObject(void)
51 wxObject::~wxObject(void)
57 * Is this object a kind of (a subclass of) 'info'?
58 * E.g. is wxWindow a kind of wxObject?
59 * Go from this class to superclass, taking into account
60 * two possible base classes.
63 bool wxObject::IsKindOf(wxClassInfo
*info
)
65 wxClassInfo
*thisInfo
= GetClassInfo();
67 return thisInfo
->IsKindOf(info
);
72 #if DEBUG || USE_DEBUG_CONTEXT
73 void wxObject::Dump(ostream
& str
)
75 if (GetClassInfo() && GetClassInfo()->GetClassName())
76 str
<< GetClassInfo()->GetClassName();
78 str
<< "unknown object class";
82 #if DEBUG && USE_MEMORY_TRACING
88 void * wxObject::operator new (size_t size
, char * fileName
, int lineNum
)
90 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
93 void wxObject::operator delete (void * buf
)
98 // Cause problems for VC++ - crashes
100 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
)
102 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
105 void wxObject::operator delete[] (void * buf
)
107 wxDebugFree(buf
, TRUE
);
114 * Class info: provides run-time class type information.
117 #ifdef USE_STORABLE_CLASSES
119 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn fn
,
120 wxStorableConstructorFn stoFn
)
123 baseClassName1
= baseName1
;
124 baseClassName2
= baseName2
;
127 objectConstructor
= fn
;
128 storableConstructor
= stoFn
;
137 wxObject
* wxClassInfo::CreateObject( istream
&stream
, char *data
)
139 if (storableConstructor
)
140 return (wxObject
*)(*storableConstructor
)( stream
, data
);
147 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
)
150 baseClassName1
= baseName1
;
151 baseClassName2
= baseName2
;
154 objectConstructor
= constr
;
165 wxObject
*wxClassInfo::CreateObject(void)
167 if (objectConstructor
)
168 return (wxObject
*)(*objectConstructor
)();
173 wxClassInfo
*wxClassInfo::FindClass(char *c
)
175 wxClassInfo
*p
= first
;
178 if (p
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0)
185 // Climb upwards through inheritance hierarchy.
186 // Dual inheritance is catered for.
187 bool wxClassInfo::IsKindOf(wxClassInfo
*info
)
192 // For some reason, when making/using a DLL, static data has to be included
193 // in both the DLL and the application. This can lead to duplicate
194 // wxClassInfo objects, so we have to test the name instead of the pointers.
196 if (GetClassName() && info
->GetClassName() && (strcmp(GetClassName(), info
->GetClassName()) == 0))
204 if (baseInfo1
->IsKindOf(info
))
208 return baseInfo2
->IsKindOf(info
);
213 // Set pointers to base class(es) to speed up IsKindOf
214 void wxClassInfo::InitializeClasses(void)
216 wxHashTable
table(wxKEY_STRING
);
218 // Index all class infos by their class name
219 wxClassInfo
*info
= first
;
223 table
.Put(info
->className
, (wxObject
*)info
);
227 // Set base pointers for each wxClassInfo
231 if (info
->GetBaseClassName1())
232 info
->baseInfo1
= (wxClassInfo
*)table
.Get(info
->GetBaseClassName1());
233 if (info
->GetBaseClassName2())
234 info
->baseInfo2
= (wxClassInfo
*)table
.Get(info
->GetBaseClassName2());
239 wxObject
*wxCreateDynamicObject(char *name
)
241 wxClassInfo
*info
= wxClassInfo::first
;
244 if (info
->className
&& strcmp(info
->className
, name
) == 0)
245 return info
->CreateObject();
251 #ifdef USE_STORABLE_CLASSES
253 wxObject
* wxCreateStoredObject( char *name
, istream
&stream
, char *data
)
255 wxClassInfo
*info
= wxClassInfo::first
;
258 if (info
->className
&& strcmp(info
->className
, name
) == 0)
259 return info
->CreateObject( stream
, data
);
268 * wxObject: cloning of objects
271 void wxObject::Ref(const wxObject
& clone
)
273 // delete reference to old data
275 // reference new data
276 if (clone
.m_refData
) {
277 m_refData
= clone
.m_refData
;
278 ++(m_refData
->m_count
);
282 void wxObject::UnRef(void)
285 assert(m_refData
->m_count
> 0);
286 --(m_refData
->m_count
);
287 if (m_refData
->m_count
== 0)
297 wxObjectRefData::wxObjectRefData(void) : m_count(1)
301 wxObjectRefData::~wxObjectRefData(void)