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
26 // #ifdef __GNUWIN32__
35 class WXDLLEXPORT wxClassInfo
;
36 class WXDLLEXPORT wxInputStream
;
37 class WXDLLEXPORT wxObjectInputStream
;
38 class WXDLLEXPORT wxObjectOutputStream
;
39 class WXDLLEXPORT wxHashTable
;
40 class WXDLLEXPORT wxObject_Serialize
;
43 // N.B. BC++ doesn't have istream.h, ostream.h
44 # include <iostream.h>
47 # if defined(__VISUALC__) || defined(__MWERKS__)
53 * Dynamic object system declarations
56 typedef wxObject
* (*wxObjectConstructorFn
) (void);
58 class WXDLLEXPORT wxClassInfo
61 wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn fn
);
63 wxObject
*CreateObject(void);
65 inline char *GetClassName(void) const { return m_className
; }
66 inline char *GetBaseClassName1(void) const { return m_baseClassName1
; }
67 inline char *GetBaseClassName2(void) const { return m_baseClassName2
; }
68 inline wxClassInfo
* GetBaseClass1() const { return m_baseInfo1
; }
69 inline wxClassInfo
* GetBaseClass2() const { return m_baseInfo2
; }
70 inline int GetSize(void) const { return m_objectSize
; }
71 inline wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
72 static inline wxClassInfo
* GetFirst() { return sm_first
; }
73 inline wxClassInfo
* GetNext() const { return m_next
; }
74 bool IsKindOf(wxClassInfo
*info
) const;
76 static wxClassInfo
*FindClass(char *c
);
78 // Initializes parent pointers and hash table for fast searching.
79 static void InitializeClasses(void);
81 // Cleans up hash table used for fast searching.
82 static void CleanUpClasses(void);
86 char* m_baseClassName1
;
87 char* m_baseClassName2
;
89 wxObjectConstructorFn m_objectConstructor
;
91 // Pointers to base wxClassInfos: set in InitializeClasses
92 // called from wx_main.cc
93 wxClassInfo
* m_baseInfo1
;
94 wxClassInfo
* m_baseInfo2
;
96 static wxClassInfo
* sm_first
;
99 static wxHashTable
* sm_classTable
;
102 WXDLLEXPORT wxObject
* wxCreateDynamicObject(const char *name
);
105 WXDLLEXPORT wxObject
* wxCreateStoredObject( wxInputStream
& stream
);
108 #define DECLARE_DYNAMIC_CLASS(name) \
110 static wxClassInfo sm_class##name;\
111 wxClassInfo *GetClassInfo() const \
112 { return &name::sm_class##name; }
114 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
115 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
118 ////// for concrete classes
121 // Single inheritance with one base class
122 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
123 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
124 { return new name; }\
125 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename, (char *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
127 // Multiple inheritance with two base classes
128 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
129 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
130 { return new name; }\
131 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename1, (char *) #basename2, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
134 ////// for abstract classes
137 // Single inheritance with one base class
138 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
139 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename, \
140 (char *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL);
142 // Multiple inheritance with two base classes
143 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
144 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename1, (char *) #basename2, (int) sizeof(name), (wxObjectConstructorFn) NULL);
146 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
147 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
149 #define CLASSINFO(name) (&name::sm_class##name)
153 // No dynamic class system: so stub out the macros
154 #define DECLARE_DYNAMIC_CLASS(name)
155 #define DECLARE_ABSTRACT_CLASS(name)
156 #define DECLARE_CLASS(name)
157 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
158 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
159 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
160 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
161 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
162 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
166 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
168 // Just seems a bit nicer-looking (pretend it's not a macro)
169 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
171 // Unfortunately Borland seems to need this include.
174 #include <iostream.h>
180 class WXDLLEXPORT wxObjectRefData
;
182 class WXDLLEXPORT wxObject
186 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
187 static wxClassInfo sm_classwxObject
;
190 virtual ~wxObject(void);
192 virtual wxClassInfo
*GetClassInfo(void) const { return &sm_classwxObject
; }
194 bool IsKindOf(wxClassInfo
*info
) const;
196 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
197 void * operator new (size_t size
, char * fileName
= NULL
, int lineNum
= 0);
198 void operator delete (void * buf
);
201 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
202 void operator delete(void *buf
, char*, int);
205 // Causes problems for VC++
206 #if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__)
207 void * operator new[] (size_t size
, char * fileName
= NULL
, int lineNum
= 0);
208 void operator delete[] (void * buf
);
213 void * operator new[] (size_t size, char * fileName , int lineNum = 0);
214 void operator delete[] (void * buf);
218 #endif // Debug & memory tracing
220 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
221 virtual void Dump(ostream
& str
);
225 virtual void StoreObject( wxObjectOutputStream
&stream
);
226 virtual void LoadObject( wxObjectInputStream
&stream
);
229 // make a 'clone' of the object
230 void Ref(const wxObject
& clone
);
232 // destroy a reference
235 inline wxObjectRefData
*GetRefData(void) const { return m_refData
; }
236 inline void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
239 wxObjectRefData
* m_refData
;
241 wxObject_Serialize
* m_serialObj
;
249 class WXDLLEXPORT wxObjectRefData
{
251 friend class wxObject
;
254 wxObjectRefData(void);
255 virtual ~wxObjectRefData(void);
257 inline int GetRefCount(void) const { return m_count
; }
264 #define WXDEBUG_NEW new(__FILE__,__LINE__)
267 #define WXDEBUG_NEW new
270 // Redefine new to be the debugging version. This doesn't
271 // work with all compilers, in which case you need to
272 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
274 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
275 #define new new(__FILE__,__LINE__)