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" 
  34 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT 
  35 #include "wx/memory.h" 
  38 #if WXDEBUG || USE_DEBUG_CONTEXT 
  43 #if !USE_SHARED_LIBRARY 
  44 wxClassInfo 
wxObject::sm_classwxObject((char *) "wxObject", (char *) NULL
, (char *) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
); 
  45 wxClassInfo
* wxClassInfo::sm_first 
= (wxClassInfo 
*) NULL
; 
  46 wxHashTable
* wxClassInfo::sm_classTable 
= (wxHashTable
*) NULL
; 
  50  * wxWindows root object. 
  53 wxObject::wxObject(void) 
  55   m_refData 
= (wxObjectRefData 
*) NULL
; 
  57   m_serialObj 
= (wxObject_Serialize 
*)NULL
; 
  61 wxObject::~wxObject(void) 
  71  * Is this object a kind of (a subclass of) 'info'? 
  72  * E.g. is wxWindow a kind of wxObject? 
  73  * Go from this class to superclass, taking into account 
  74  * two possible base classes. 
  77 bool wxObject::IsKindOf(wxClassInfo 
*info
) 
  79   wxClassInfo 
*thisInfo 
= GetClassInfo(); 
  81     return thisInfo
->IsKindOf(info
); 
  86 #if WXDEBUG || USE_DEBUG_CONTEXT 
  87 void wxObject::Dump(ostream
& str
) 
  89   if (GetClassInfo() && GetClassInfo()->GetClassName()) 
  90     str 
<< GetClassInfo()->GetClassName(); 
  92     str 
<< "unknown object class"; 
  96 #if WXDEBUG && USE_MEMORY_TRACING 
 102 void * wxObject::operator new (size_t size
, char * fileName
, int lineNum
) 
 104   return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
); 
 107 void wxObject::operator delete (void * buf
) 
 112 // Cause problems for VC++ - crashes 
 114 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
) 
 116   return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
); 
 119 void wxObject::operator delete[] (void * buf
) 
 121   wxDebugFree(buf
, TRUE
); 
 128  * Class info: provides run-time class type information. 
 131 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
) 
 134   m_baseClassName1 
= baseName1
; 
 135   m_baseClassName2 
= baseName2
; 
 138   m_objectConstructor 
= constr
; 
 143   m_baseInfo1 
= (wxClassInfo 
*) NULL
; 
 144   m_baseInfo2 
= (wxClassInfo 
*) NULL
; 
 147 wxObject 
*wxClassInfo::CreateObject(void) 
 149   if (m_objectConstructor
) 
 150     return (wxObject 
*)(*m_objectConstructor
)(); 
 152     return (wxObject 
*) NULL
; 
 155 wxClassInfo 
*wxClassInfo::FindClass(char *c
) 
 157   wxClassInfo 
*p 
= sm_first
; 
 160     if (p 
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0) 
 164   return (wxClassInfo 
*) NULL
; 
 167 // Climb upwards through inheritance hierarchy. 
 168 // Dual inheritance is catered for. 
 169 bool wxClassInfo::IsKindOf(wxClassInfo 
*info
) 
 174   // For some reason, when making/using a DLL, static data has to be included 
 175   // in both the DLL and the application. This can lead to duplicate 
 176   // wxClassInfo objects, so we have to test the name instead of the pointers. 
 177   // PROBABLY NO LONGER TRUE now I've done DLL creation right. 
 180   if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0)) 
 188     if (m_baseInfo1
->IsKindOf(info
)) 
 192     return m_baseInfo2
->IsKindOf(info
); 
 197 // Set pointers to base class(es) to speed up IsKindOf 
 198 void wxClassInfo::InitializeClasses(void) 
 200   wxClassInfo::sm_classTable 
= new wxHashTable(wxKEY_STRING
); 
 202   // Index all class infos by their class name 
 203   wxClassInfo 
*info 
= sm_first
; 
 206     if (info
->m_className
) 
 207       sm_classTable
->Put(info
->m_className
, (wxObject 
*)info
); 
 211   // Set base pointers for each wxClassInfo 
 215     if (info
->GetBaseClassName1()) 
 216       info
->m_baseInfo1 
= (wxClassInfo 
*)sm_classTable
->Get(info
->GetBaseClassName1()); 
 217     if (info
->GetBaseClassName2()) 
 218       info
->m_baseInfo2 
= (wxClassInfo 
*)sm_classTable
->Get(info
->GetBaseClassName2()); 
 223 // Clean up hash table 
 224 void wxClassInfo::CleanUpClasses(void) 
 226     delete wxClassInfo::sm_classTable
; 
 227     wxClassInfo::sm_classTable 
= NULL
; 
 230 wxObject 
*wxCreateDynamicObject(const char *name
) 
 232     if (wxClassInfo::sm_classTable
) 
 234         wxClassInfo 
*info 
= (wxClassInfo 
*)wxClassInfo::sm_classTable
->Get(name
); 
 236             return (wxObject 
*)NULL
; 
 238         return info
->CreateObject(); 
 242         wxClassInfo 
*info 
= wxClassInfo::sm_first
; 
 245             if (info
->m_className 
&& strcmp(info
->m_className
, name
) == 0) 
 246                 return info
->CreateObject(); 
 249         return (wxObject
*) NULL
; 
 251     return (wxObject
*) NULL
; 
 256 #include "wx/serbase.h" 
 257 #include "wx/dynlib.h" 
 258 #include "wx/msgdlg.h" 
 260 wxObject
* wxCreateStoredObject( wxInputStream 
&stream 
) 
 262   wxObjectInputStream 
obj_s(stream
); 
 263   return obj_s
.LoadObject(); 
 266 void wxObject::StoreObject( wxObjectOutputStream
& stream 
) 
 268   wxString obj_name 
= wxString(GetClassInfo()->GetClassName()) + "_Serialize"; 
 269   wxLibrary 
*lib 
= wxTheLibraries
.LoadLibrary("wxserial"); 
 272     wxMessageBox("Can't load wxSerial dynamic library.", "Alert !"); 
 276     m_serialObj 
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name 
); 
 281       message
.Printf("Can't find the serialization object (%s) for the object %s", 
 282                      WXSTRINGCAST obj_name
, 
 283                      WXSTRINGCAST 
GetClassInfo()->GetClassName()); 
 284       wxMessageBox(message
, "Alert !"); 
 287     m_serialObj
->SetObject(this); 
 290   m_serialObj
->StoreObject(stream
); 
 293 void wxObject::LoadObject( wxObjectInputStream
& stream 
) 
 295   wxString obj_name 
= wxString(GetClassInfo()->GetClassName()) + "_Serialize"; 
 296   wxLibrary 
*lib 
= wxTheLibraries
.LoadLibrary("wxserial"); 
 299     m_serialObj 
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name 
); 
 304       message
.Printf("Can't find the serialization object (%s) for the object %s", 
 305                      WXSTRINGCAST obj_name
, 
 306                      WXSTRINGCAST 
GetClassInfo()->GetClassName()); 
 307       wxMessageBox(message
, "Alert !"); 
 310     m_serialObj
->SetObject(this); 
 313   m_serialObj
->LoadObject(stream
); 
 319  * wxObject: cloning of objects 
 322 void wxObject::Ref(const wxObject
& clone
) 
 324     // delete reference to old data 
 326     // reference new data 
 327     if (clone
.m_refData
) { 
 328         m_refData 
= clone
.m_refData
; 
 329         ++(m_refData
->m_count
); 
 333 void wxObject::UnRef(void) 
 336         assert(m_refData
->m_count 
> 0); 
 337         --(m_refData
->m_count
); 
 338         if (m_refData
->m_count 
== 0) 
 341     m_refData 
= (wxObjectRefData 
*) NULL
; 
 348 wxObjectRefData::wxObjectRefData(void) : m_count(1) 
 352 wxObjectRefData::~wxObjectRefData(void)