]> git.saurik.com Git - wxWidgets.git/blob - include/wx/object.h
* Added wxSerial DLL support for Borland 32
[wxWidgets.git] / include / wx / object.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: object.h
3 // Purpose: wxObject class, plus run-time type information macros
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_OBJECTH__
13 #define _WX_OBJECTH__
14
15 #ifdef __GNUG__
16 #pragma interface "object.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/memory.h"
21
22 class WXDLLEXPORT wxObject;
23
24 #if USE_DYNAMIC_CLASSES
25
26 #ifdef __GNUWIN32__
27 #ifdef GetClassName
28 #undef GetClassName
29 #endif
30 #endif
31
32 class WXDLLEXPORT wxClassInfo;
33 class WXDLLIMPORT ostream;
34 class WXDLLEXPORT wxInputStream;
35 class WXDLLEXPORT wxObjectInputStream;
36 class WXDLLEXPORT wxObjectOutputStream;
37 class WXDLLEXPORT wxHashTable;
38 class WXDLLEXPORT wxObject_Serialize;
39
40 /*
41 * Dynamic object system declarations
42 */
43
44 typedef wxObject * (*wxObjectConstructorFn) (void);
45
46 class WXDLLEXPORT wxClassInfo
47 {
48 public:
49 wxClassInfo(char *cName, char *baseName1, char *baseName2, int sz, wxObjectConstructorFn fn);
50
51 wxObject *CreateObject(void);
52
53 inline char *GetClassName(void) const { return m_className; }
54 inline char *GetBaseClassName1(void) const { return m_baseClassName1; }
55 inline char *GetBaseClassName2(void) const { return m_baseClassName2; }
56 inline wxClassInfo* GetBaseClass1() const { return m_baseInfo1; }
57 inline wxClassInfo* GetBaseClass2() const { return m_baseInfo2; }
58 inline int GetSize(void) const { return m_objectSize; }
59 inline wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; }
60 static inline wxClassInfo* GetFirst() { return sm_first; }
61 inline wxClassInfo* GetNext() const { return m_next; }
62 bool IsKindOf(wxClassInfo *info) const;
63
64 static wxClassInfo *FindClass(char *c);
65
66 // Initializes parent pointers and hash table for fast searching.
67 static void InitializeClasses(void);
68
69 // Cleans up hash table used for fast searching.
70 static void CleanUpClasses(void);
71
72 public:
73 char* m_className;
74 char* m_baseClassName1;
75 char* m_baseClassName2;
76 int m_objectSize;
77 wxObjectConstructorFn m_objectConstructor;
78
79 // Pointers to base wxClassInfos: set in InitializeClasses
80 // called from wx_main.cc
81 wxClassInfo* m_baseInfo1;
82 wxClassInfo* m_baseInfo2;
83
84 static wxClassInfo* sm_first;
85 wxClassInfo* m_next;
86
87 static wxHashTable* sm_classTable;
88 };
89
90 wxObject* WXDLLEXPORT wxCreateDynamicObject(const char *name);
91
92 #ifdef USE_SERIAL
93 wxObject* WXDLLEXPORT wxCreateStoredObject( wxInputStream& stream );
94 #endif
95
96 #define DECLARE_DYNAMIC_CLASS(name) \
97 public:\
98 static wxClassInfo sm_class##name;\
99 wxClassInfo *GetClassInfo() const \
100 { return &name::sm_class##name; }
101
102 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
103 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
104
105 //////
106 ////// for concrete classes
107 //////
108
109 // Single inheritance with one base class
110 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
111 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
112 { return new name; }\
113 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename, (char *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
114
115 // Multiple inheritance with two base classes
116 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
117 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
118 { return new name; }\
119 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename1, (char *) #basename2, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
120
121 //////
122 ////// for abstract classes
123 //////
124
125 // Single inheritance with one base class
126 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
127 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename, \
128 (char *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL);
129
130 // Multiple inheritance with two base classes
131 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
132 wxClassInfo name::sm_class##name((char *) #name, (char *) #basename1, (char *) #basename2, (int) sizeof(name), (wxObjectConstructorFn) NULL);
133
134 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
135 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
136
137 #define CLASSINFO(name) (&name::sm_class##name)
138
139 #else
140
141 // No dynamic class system: so stub out the macros
142 #define DECLARE_DYNAMIC_CLASS(name)
143 #define DECLARE_ABSTRACT_CLASS(name)
144 #define DECLARE_CLASS(name)
145 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
146 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
147 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
148 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
149 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
150 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
151
152 #endif
153
154 #define IS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
155
156 // Unfortunately Borland seems to need this include.
157 #ifdef __BORLANDC__
158 #if USE_IOSTREAMH
159 #include <iostream.h>
160 #else
161 #include <iostream>
162 #endif
163 #endif
164
165 class WXDLLEXPORT wxObjectRefData;
166
167 class WXDLLEXPORT wxObject
168 {
169 public:
170
171 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
172 static wxClassInfo sm_classwxObject;
173
174 wxObject(void);
175 virtual ~wxObject(void);
176
177 virtual wxClassInfo *GetClassInfo(void) const { return &sm_classwxObject; }
178
179 bool IsKindOf(wxClassInfo *info) const;
180
181 #if WXDEBUG && USE_MEMORY_TRACING
182 void * operator new (size_t size, char * fileName = NULL, int lineNum = 0);
183 void operator delete (void * buf);
184
185 // Cause problems for VC++
186 #ifndef _MSC_VER
187 void * operator new[] (size_t size, char * fileName = NULL, int lineNum = 0);
188 void operator delete[] (void * buf);
189 #endif
190
191 #endif
192
193 #if WXDEBUG || USE_DEBUG_CONTEXT
194 virtual void Dump(ostream& str);
195 #endif
196
197 #ifdef USE_SERIAL
198 virtual void StoreObject( wxObjectOutputStream &stream );
199 virtual void LoadObject( wxObjectInputStream &stream );
200 #endif
201
202 // make a 'clone' of the object
203 void Ref(const wxObject& clone);
204
205 // destroy a reference
206 void UnRef(void);
207
208 inline wxObjectRefData *GetRefData(void) const { return m_refData; }
209 inline void SetRefData(wxObjectRefData *data) { m_refData = data; }
210
211 protected:
212 wxObjectRefData* m_refData;
213 #ifdef USE_SERIAL
214 wxObject_Serialize* m_serialObj;
215 #endif
216 };
217
218 /*
219 * wxObjectData
220 */
221
222 class WXDLLEXPORT wxObjectRefData {
223
224 friend class wxObject;
225
226 public:
227 wxObjectRefData(void);
228 virtual ~wxObjectRefData(void);
229
230 inline int GetRefCount(void) const { return m_count; }
231 private:
232 int m_count;
233 };
234
235 #if WXDEBUG && USE_GLOBAL_MEMORY_OPERATORS
236 //#ifndef WXDEBUG_NEW
237 //#define WXDEBUG_NEW new(__FILE__,__LINE__)
238 //#endif
239 #define new new(__FILE__,__LINE__)
240 #endif
241
242 #endif
243 // _WX_OBJECTH__
244
245