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