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 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
226 wxObjectInputStream
obj_s(stream
);
227 return obj_s
.LoadObject();
233 * wxObject: cloning of objects
236 void wxObject::Ref(const wxObject
& clone
)
238 // delete reference to old data
240 // reference new data
241 if (clone
.m_refData
) {
242 m_refData
= clone
.m_refData
;
243 ++(m_refData
->m_count
);
247 void wxObject::UnRef(void)
250 assert(m_refData
->m_count
> 0);
251 --(m_refData
->m_count
);
252 if (m_refData
->m_count
== 0)
262 wxObjectRefData::wxObjectRefData(void) : m_count(1)
266 wxObjectRefData::~wxObjectRefData(void)