1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObject class, plus run-time type information macros
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "object.h"
20 #include "wx/memory.h"
22 class WXDLLEXPORT wxObject
;
24 #if wxUSE_DYNAMIC_CLASSES
32 class WXDLLEXPORT wxClassInfo
;
33 class WXDLLEXPORT wxInputStream
;
34 class WXDLLEXPORT wxObjectInputStream
;
35 class WXDLLEXPORT wxObjectOutputStream
;
36 class WXDLLEXPORT wxHashTable
;
37 class WXDLLEXPORT wxObject_Serialize
;
40 // N.B. BC++ doesn't have istream.h, ostream.h
41 # include <iostream.h>
50 * Dynamic object system declarations
53 typedef wxObject
* (*wxObjectConstructorFn
) (void);
55 class WXDLLEXPORT wxClassInfo
58 wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn fn
);
60 wxObject
*CreateObject(void);
62 inline char *GetClassName(void) const { return m_className
; }
63 inline char *GetBaseClassName1(void) const { return m_baseClassName1
; }
64 inline char *GetBaseClassName2(void) const { return m_baseClassName2
; }
65 inline wxClassInfo
* GetBaseClass1() const { return m_baseInfo1
; }
66 inline wxClassInfo
* GetBaseClass2() const { return m_baseInfo2
; }
67 inline int GetSize(void) const { return m_objectSize
; }
68 inline wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
69 static inline wxClassInfo
* GetFirst() { return sm_first
; }
70 inline wxClassInfo
* GetNext() const { return m_next
; }
71 bool IsKindOf(wxClassInfo
*info
) const;
73 static wxClassInfo
*FindClass(char *c
);
75 // Initializes parent pointers and hash table for fast searching.
76 static void InitializeClasses(void);
78 // Cleans up hash table used for fast searching.
79 static void CleanUpClasses(void);
83 char* m_baseClassName1
;
84 char* m_baseClassName2
;
86 wxObjectConstructorFn m_objectConstructor
;
88 // Pointers to base wxClassInfos: set in InitializeClasses
89 // called from wx_main.cc
90 wxClassInfo
* m_baseInfo1
;
91 wxClassInfo
* m_baseInfo2
;
93 static wxClassInfo
* sm_first
;
96 static wxHashTable
* sm_classTable
;
99 WXDLLEXPORT wxObject
* wxCreateDynamicObject(const char *name
);
102 WXDLLEXPORT wxObject
* wxCreateStoredObject( wxInputStream
& stream
);
105 #define DECLARE_DYNAMIC_CLASS(name) \
107 static wxClassInfo sm_class##name;\
108 wxClassInfo *GetClassInfo() const \
109 { return &name::sm_class##name; }
111 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
112 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
115 ////// for concrete classes
118 // Single inheritance with one base class
119 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
120 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
121 { return new name; }\
122 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename, (char *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
124 // Multiple inheritance with two base classes
125 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
126 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
127 { return new name; }\
128 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename1, (char *) #basename2, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
131 ////// for abstract classes
134 // Single inheritance with one base class
135 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
136 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename, \
137 (char *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL);
139 // Multiple inheritance with two base classes
140 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
141 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename1, (char *) #basename2, (int) sizeof(name), (wxObjectConstructorFn) NULL);
143 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
144 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
146 #define CLASSINFO(name) (&name::sm_class##name)
150 // No dynamic class system: so stub out the macros
151 #define DECLARE_DYNAMIC_CLASS(name)
152 #define DECLARE_ABSTRACT_CLASS(name)
153 #define DECLARE_CLASS(name)
154 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
155 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
156 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
157 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
158 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
159 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
163 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
165 // Just seems a bit nicer-looking (pretend it's not a macro)
166 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
168 // Unfortunately Borland seems to need this include.
171 #include <iostream.h>
177 class WXDLLEXPORT wxObjectRefData
;
179 class WXDLLEXPORT wxObject
183 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
184 static wxClassInfo sm_classwxObject
;
187 virtual ~wxObject(void);
189 virtual wxClassInfo
*GetClassInfo(void) const { return &sm_classwxObject
; }
191 bool IsKindOf(wxClassInfo
*info
) const;
193 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
194 void * operator new (size_t size
, char * fileName
= NULL
, int lineNum
= 0);
195 void operator delete (void * buf
);
199 void operator delete(void *buf
, char*, int);
202 // Cause problems for VC++
204 #if !defined(_MSC_VER) && wxUSE_ARRAY_MEMORY_OPERATORS
205 void * operator new[] (size_t size
, char * fileName
= NULL
, int lineNum
= 0);
206 void operator delete[] (void * buf
);
211 void * operator new[] (size_t size, char * fileName , int lineNum = 0);
212 void operator delete[] (void * buf);
218 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
219 virtual void Dump(ostream
& str
);
223 virtual void StoreObject( wxObjectOutputStream
&stream
);
224 virtual void LoadObject( wxObjectInputStream
&stream
);
227 // make a 'clone' of the object
228 void Ref(const wxObject
& clone
);
230 // destroy a reference
233 inline wxObjectRefData
*GetRefData(void) const { return m_refData
; }
234 inline void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
237 wxObjectRefData
* m_refData
;
239 wxObject_Serialize
* m_serialObj
;
247 class WXDLLEXPORT wxObjectRefData
{
249 friend class wxObject
;
252 wxObjectRefData(void);
253 virtual ~wxObjectRefData(void);
255 inline int GetRefCount(void) const { return m_count
; }
262 #define WXDEBUG_NEW new(__FILE__,__LINE__)
265 #define WXDEBUG_NEW new
268 // Redefine new to be the debugging version. This doesn't
269 // work with all compilers, in which case you need to
270 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
272 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
273 #define new new(__FILE__,__LINE__)