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 wxOutputStream
;
38 class WXDLLEXPORT wxObjectInputStream
;
39 class WXDLLEXPORT wxObjectOutputStream
;
40 class WXDLLEXPORT wxHashTable
;
41 class WXDLLEXPORT wxObject_Serialize
;
44 // N.B. BC++ doesn't have istream.h, ostream.h
45 # include <iostream.h>
48 # if defined(__VISUALC__) || defined(__MWERKS__)
54 * Dynamic object system declarations
57 typedef wxObject
* (*wxObjectConstructorFn
) (void);
59 class WXDLLEXPORT wxClassInfo
62 wxClassInfo(wxChar
*cName
, wxChar
*baseName1
, wxChar
*baseName2
, int sz
, wxObjectConstructorFn fn
);
64 wxObject
*CreateObject(void);
66 inline wxChar
*GetClassName(void) const { return m_className
; }
67 inline wxChar
*GetBaseClassName1(void) const { return m_baseClassName1
; }
68 inline wxChar
*GetBaseClassName2(void) const { return m_baseClassName2
; }
69 inline wxClassInfo
* GetBaseClass1() const { return m_baseInfo1
; }
70 inline wxClassInfo
* GetBaseClass2() const { return m_baseInfo2
; }
71 inline int GetSize(void) const { return m_objectSize
; }
72 inline wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
73 static inline wxClassInfo
* GetFirst() { return sm_first
; }
74 inline wxClassInfo
* GetNext() const { return m_next
; }
75 bool IsKindOf(wxClassInfo
*info
) const;
77 static wxClassInfo
*FindClass(wxChar
*c
);
79 // Initializes parent pointers and hash table for fast searching.
80 static void InitializeClasses(void);
82 // Cleans up hash table used for fast searching.
83 static void CleanUpClasses(void);
87 wxChar
* m_baseClassName1
;
88 wxChar
* m_baseClassName2
;
90 wxObjectConstructorFn m_objectConstructor
;
92 // Pointers to base wxClassInfos: set in InitializeClasses
93 // called from wx_main.cc
94 wxClassInfo
* m_baseInfo1
;
95 wxClassInfo
* m_baseInfo2
;
97 static wxClassInfo
* sm_first
;
100 static wxHashTable
* sm_classTable
;
103 WXDLLEXPORT wxObject
* wxCreateDynamicObject(const wxChar
*name
);
106 WXDLLEXPORT wxObject
* wxCreateStoredObject( wxInputStream
& stream
);
109 #define DECLARE_DYNAMIC_CLASS(name) \
111 static wxClassInfo sm_class##name;\
112 wxClassInfo *GetClassInfo() const \
113 { return &name::sm_class##name; }
115 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
116 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
119 ////// for concrete classes
122 // Single inheritance with one base class
123 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
124 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
125 { return new name; }\
126 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
128 // Multiple inheritance with two base classes
129 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
130 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
131 { return new name; }\
132 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename1), (wxChar *) _T(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
135 ////// for abstract classes
138 // Single inheritance with one base class
139 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
140 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename), \
141 (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL);
143 // Multiple inheritance with two base classes
144 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
145 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename1), \
146 (wxChar *) _T(#basename2), (int) sizeof(name), (wxObjectConstructorFn) NULL);
148 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
149 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
151 #define CLASSINFO(name) (&name::sm_class##name)
155 // No dynamic class system: so stub out the macros
156 #define DECLARE_DYNAMIC_CLASS(name)
157 #define DECLARE_ABSTRACT_CLASS(name)
158 #define DECLARE_CLASS(name)
159 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
160 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
161 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
162 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
163 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
164 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
168 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
170 // Just seems a bit nicer-looking (pretend it's not a macro)
171 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
173 // Unfortunately Borland seems to need this include.
176 #include <iostream.h>
182 class WXDLLEXPORT wxObjectRefData
;
184 class WXDLLEXPORT wxObject
188 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
189 static wxClassInfo sm_classwxObject
;
192 virtual ~wxObject(void);
194 virtual wxClassInfo
*GetClassInfo(void) const { return &sm_classwxObject
; }
196 bool IsKindOf(wxClassInfo
*info
) const;
198 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
199 void * operator new (size_t size
, wxChar
* fileName
= NULL
, int lineNum
= 0);
200 void operator delete (void * buf
);
203 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
204 void operator delete(void *buf
, char*, int);
207 // Causes problems for VC++
208 #if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
209 void * operator new[] (size_t size
, wxChar
* fileName
= NULL
, int lineNum
= 0);
210 void operator delete[] (void * buf
);
214 void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
= 0);
215 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(__TFILE__,__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(__TFILE__,__LINE__)