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