]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
b2edef6f | 2 | // Name: wx/object.h |
c801d85f KB |
3 | // Purpose: wxObject class, plus run-time type information macros |
4 | // Author: Julian Smart | |
0b9ab0bd | 5 | // Modified by: Ron Lee |
c801d85f KB |
6 | // Created: 01/02/97 |
7 | // RCS-ID: $Id$ | |
371a5b4e | 8 | // Copyright: (c) 1997 Julian Smart |
0b9ab0bd | 9 | // (c) 2001 Ron Lee <ron@debian.org> |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
34138703 JS |
13 | #ifndef _WX_OBJECTH__ |
14 | #define _WX_OBJECTH__ | |
c801d85f | 15 | |
0b9ab0bd RL |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
e55ad60e | 20 | #include "wx/memory.h" |
c801d85f | 21 | |
b5dbe15d VS |
22 | class WXDLLIMPEXP_FWD_BASE wxObject; |
23 | class WXDLLIMPEXP_FWD_BASE wxString; | |
c801d85f | 24 | |
4393b50c | 25 | #ifndef wxUSE_EXTENDED_RTTI |
6d553b5f | 26 | #define wxUSE_EXTENDED_RTTI 0 |
a095505c SC |
27 | #endif |
28 | ||
b19b28c8 | 29 | #define wxDECLARE_CLASS_INFO_ITERATORS() \ |
644cb537 MB |
30 | class WXDLLIMPEXP_BASE const_iterator \ |
31 | { \ | |
32 | typedef wxHashTable_Node Node; \ | |
33 | public: \ | |
34 | typedef const wxClassInfo* value_type; \ | |
35 | typedef const value_type& const_reference; \ | |
36 | typedef const_iterator itor; \ | |
37 | typedef value_type* ptr_type; \ | |
38 | \ | |
39 | Node* m_node; \ | |
40 | wxHashTable* m_table; \ | |
41 | public: \ | |
42 | typedef const_reference reference_type; \ | |
43 | typedef ptr_type pointer_type; \ | |
44 | \ | |
45 | const_iterator(Node* node, wxHashTable* table) \ | |
46 | : m_node(node), m_table(table) { } \ | |
47 | const_iterator() : m_node(NULL), m_table(NULL) { } \ | |
48 | value_type operator*() const; \ | |
49 | itor& operator++(); \ | |
50 | const itor operator++(int); \ | |
51 | bool operator!=(const itor& it) const \ | |
52 | { return it.m_node != m_node; } \ | |
53 | bool operator==(const itor& it) const \ | |
54 | { return it.m_node == m_node; } \ | |
55 | }; \ | |
56 | \ | |
57 | static const_iterator begin_classinfo(); \ | |
b19b28c8 | 58 | static const_iterator end_classinfo() |
644cb537 | 59 | |
6d553b5f | 60 | #if wxUSE_EXTENDED_RTTI |
a095505c | 61 | #include "wx/xti.h" |
a095505c SC |
62 | #else |
63 | ||
0b9ab0bd RL |
64 | // ---------------------------------------------------------------------------- |
65 | // conditional compilation | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
b5dbe15d VS |
68 | class WXDLLIMPEXP_FWD_BASE wxClassInfo; |
69 | class WXDLLIMPEXP_FWD_BASE wxHashTable; | |
70 | class WXDLLIMPEXP_FWD_BASE wxObject; | |
71 | class WXDLLIMPEXP_FWD_BASE wxPluginLibrary; | |
b5dbe15d | 72 | class WXDLLIMPEXP_FWD_BASE wxHashTable_Node; |
c801d85f | 73 | |
0b9ab0bd RL |
74 | // ---------------------------------------------------------------------------- |
75 | // wxClassInfo | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | typedef wxObject *(*wxObjectConstructorFn)(void); | |
c801d85f | 79 | |
bddd7a8d | 80 | class WXDLLIMPEXP_BASE wxClassInfo |
c801d85f | 81 | { |
b5dbe15d | 82 | friend class WXDLLIMPEXP_FWD_BASE wxObject; |
c1e10a22 | 83 | friend WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name); |
aac65598 | 84 | public: |
38befbee | 85 | wxClassInfo( const wxChar *className, |
d1d738f1 VS |
86 | const wxClassInfo *baseInfo1, |
87 | const wxClassInfo *baseInfo2, | |
38befbee RL |
88 | int size, |
89 | wxObjectConstructorFn ctor ) | |
0b9ab0bd | 90 | : m_className(className) |
0b9ab0bd RL |
91 | , m_objectSize(size) |
92 | , m_objectConstructor(ctor) | |
d1d738f1 VS |
93 | , m_baseInfo1(baseInfo1) |
94 | , m_baseInfo2(baseInfo2) | |
0b9ab0bd | 95 | , m_next(sm_first) |
7e548f6b | 96 | { |
d1d738f1 VS |
97 | sm_first = this; |
98 | Register(); | |
99 | } | |
0b9ab0bd | 100 | |
aa4b7ef9 | 101 | ~wxClassInfo(); |
1d2eddff | 102 | |
4e124582 VZ |
103 | wxObject *CreateObject() const |
104 | { return m_objectConstructor ? (*m_objectConstructor)() : 0; } | |
0c06c983 | 105 | bool IsDynamic() const { return (NULL != m_objectConstructor); } |
0b9ab0bd RL |
106 | |
107 | const wxChar *GetClassName() const { return m_className; } | |
469349b5 MB |
108 | const wxChar *GetBaseClassName1() const |
109 | { return m_baseInfo1 ? m_baseInfo1->GetClassName() : NULL; } | |
110 | const wxChar *GetBaseClassName2() const | |
111 | { return m_baseInfo2 ? m_baseInfo2->GetClassName() : NULL; } | |
0b9ab0bd RL |
112 | const wxClassInfo *GetBaseClass1() const { return m_baseInfo1; } |
113 | const wxClassInfo *GetBaseClass2() const { return m_baseInfo2; } | |
114 | int GetSize() const { return m_objectSize; } | |
115 | ||
4e124582 VZ |
116 | wxObjectConstructorFn GetConstructor() const |
117 | { return m_objectConstructor; } | |
0b9ab0bd RL |
118 | static const wxClassInfo *GetFirst() { return sm_first; } |
119 | const wxClassInfo *GetNext() const { return m_next; } | |
c1e10a22 | 120 | static wxClassInfo *FindClass(const wxString& className); |
807d8487 | 121 | |
0b9ab0bd RL |
122 | // Climb upwards through inheritance hierarchy. |
123 | // Dual inheritance is catered for. | |
124 | ||
125 | bool IsKindOf(const wxClassInfo *info) const | |
126 | { | |
127 | return info != 0 && | |
128 | ( info == this || | |
129 | ( m_baseInfo1 && m_baseInfo1->IsKindOf(info) ) || | |
130 | ( m_baseInfo2 && m_baseInfo2->IsKindOf(info) ) ); | |
131 | } | |
3f4a0c5b | 132 | |
b19b28c8 | 133 | wxDECLARE_CLASS_INFO_ITERATORS(); |
ce00f59b | 134 | |
644cb537 | 135 | private: |
0b9ab0bd | 136 | const wxChar *m_className; |
0b9ab0bd RL |
137 | int m_objectSize; |
138 | wxObjectConstructorFn m_objectConstructor; | |
139 | ||
1125dc3f | 140 | // Pointers to base wxClassInfos |
0b9ab0bd RL |
141 | |
142 | const wxClassInfo *m_baseInfo1; | |
143 | const wxClassInfo *m_baseInfo2; | |
144 | ||
145 | // class info object live in a linked list: | |
146 | // pointers to its head and the next element in it | |
147 | ||
148 | static wxClassInfo *sm_first; | |
149 | wxClassInfo *m_next; | |
150 | ||
151 | static wxHashTable *sm_classTable; | |
1f428942 | 152 | |
7e548f6b | 153 | protected: |
d1d738f1 VS |
154 | // registers the class |
155 | void Register(); | |
156 | void Unregister(); | |
1125dc3f | 157 | |
c0c133e1 | 158 | wxDECLARE_NO_COPY_CLASS(wxClassInfo); |
c801d85f KB |
159 | }; |
160 | ||
c1e10a22 | 161 | WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name); |
c801d85f | 162 | |
0b9ab0bd RL |
163 | // ---------------------------------------------------------------------------- |
164 | // Dynamic class macros | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
b19b28c8 | 167 | #define wxDECLARE_ABSTRACT_CLASS(name) \ |
14ca93a0 VZ |
168 | public: \ |
169 | static wxClassInfo ms_classInfo; \ | |
b19b28c8 | 170 | virtual wxClassInfo *GetClassInfo() const |
14ca93a0 | 171 | |
b19b28c8 FM |
172 | #define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \ |
173 | wxDECLARE_NO_ASSIGN_CLASS(name); \ | |
174 | wxDECLARE_DYNAMIC_CLASS(name) | |
fc7a2a60 | 175 | |
b19b28c8 FM |
176 | #define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \ |
177 | wxDECLARE_NO_COPY_CLASS(name); \ | |
178 | wxDECLARE_DYNAMIC_CLASS(name) | |
fc7a2a60 | 179 | |
b19b28c8 FM |
180 | #define wxDECLARE_DYNAMIC_CLASS(name) \ |
181 | wxDECLARE_ABSTRACT_CLASS(name); \ | |
182 | static wxObject* wxCreateObject() | |
14ca93a0 | 183 | |
b19b28c8 FM |
184 | #define wxDECLARE_CLASS(name) \ |
185 | wxDECLARE_DYNAMIC_CLASS(name) | |
c801d85f | 186 | |
14ca93a0 VZ |
187 | |
188 | // common part of the macros below | |
189 | #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \ | |
190 | wxClassInfo name::ms_classInfo(wxT(#name), \ | |
191 | &basename::ms_classInfo, \ | |
192 | baseclsinfo2, \ | |
193 | (int) sizeof(name), \ | |
32bb5309 | 194 | func); \ |
14ca93a0 VZ |
195 | \ |
196 | wxClassInfo *name::GetClassInfo() const \ | |
197 | { return &name::ms_classInfo; } | |
198 | ||
199 | #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \ | |
200 | wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func) | |
201 | ||
202 | #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \ | |
2b199a38 | 203 | wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func) |
14ca93a0 | 204 | |
0b9ab0bd RL |
205 | // ----------------------------------- |
206 | // for concrete classes | |
207 | // ----------------------------------- | |
208 | ||
209 | // Single inheritance with one base class | |
b19b28c8 | 210 | #define wxIMPLEMENT_DYNAMIC_CLASS(name, basename) \ |
14ca93a0 VZ |
211 | wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \ |
212 | wxObject* name::wxCreateObject() \ | |
213 | { return new name; } | |
c801d85f | 214 | |
0b9ab0bd | 215 | // Multiple inheritance with two base classes |
b19b28c8 | 216 | #define wxIMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \ |
14ca93a0 VZ |
217 | wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \ |
218 | name::wxCreateObject) \ | |
219 | wxObject* name::wxCreateObject() \ | |
220 | { return new name; } | |
c801d85f | 221 | |
0b9ab0bd RL |
222 | // ----------------------------------- |
223 | // for abstract classes | |
224 | // ----------------------------------- | |
c801d85f | 225 | |
0b9ab0bd | 226 | // Single inheritance with one base class |
b19b28c8 | 227 | #define wxIMPLEMENT_ABSTRACT_CLASS(name, basename) \ |
14ca93a0 | 228 | wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL) |
0b9ab0bd RL |
229 | |
230 | // Multiple inheritance with two base classes | |
b19b28c8 | 231 | #define wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \ |
14ca93a0 | 232 | wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL) |
c801d85f | 233 | |
b19b28c8 FM |
234 | #define wxIMPLEMENT_CLASS(name, basename) \ |
235 | wxIMPLEMENT_ABSTRACT_CLASS(name, basename) | |
ce00f59b | 236 | |
b19b28c8 FM |
237 | #define wxIMPLEMENT_CLASS2(name, basename1, basename2) \ |
238 | IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) | |
c801d85f | 239 | |
6d553b5f | 240 | #endif // !wxUSE_EXTENDED_RTTI |
a095505c SC |
241 | |
242 | ||
0b9ab0bd RL |
243 | // ----------------------------------- |
244 | // for pluggable classes | |
245 | // ----------------------------------- | |
246 | ||
247 | // NOTE: this should probably be the very first statement | |
248 | // in the class declaration so wxPluginSentinel is | |
249 | // the first member initialised and the last destroyed. | |
250 | ||
251 | // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel; | |
252 | ||
253 | #if wxUSE_NESTED_CLASSES | |
254 | ||
60b73526 RL |
255 | #define _DECLARE_DL_SENTINEL(name, exportdecl) \ |
256 | class exportdecl name##PluginSentinel { \ | |
257 | private: \ | |
258 | static const wxString sm_className; \ | |
259 | public: \ | |
260 | name##PluginSentinel(); \ | |
2e0b1b11 | 261 | ~name##PluginSentinel(); \ |
60b73526 | 262 | }; \ |
b19b28c8 | 263 | name##PluginSentinel m_pluginsentinel |
0b9ab0bd RL |
264 | |
265 | #define _IMPLEMENT_DL_SENTINEL(name) \ | |
266 | const wxString name::name##PluginSentinel::sm_className(#name); \ | |
267 | name::name##PluginSentinel::name##PluginSentinel() { \ | |
4f89dbc4 | 268 | wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \ |
7c1e2b44 | 269 | if( e != 0 ) { e->RefObj(); } \ |
0b9ab0bd | 270 | } \ |
abad5367 | 271 | name::name##PluginSentinel::~name##PluginSentinel() { \ |
4f89dbc4 | 272 | wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \ |
7c1e2b44 | 273 | if( e != 0 ) { e->UnrefObj(); } \ |
0b9ab0bd RL |
274 | } |
275 | #else | |
276 | ||
277 | #define _DECLARE_DL_SENTINEL(name) | |
278 | #define _IMPLEMENT_DL_SENTINEL(name) | |
279 | ||
280 | #endif // wxUSE_NESTED_CLASSES | |
281 | ||
b19b28c8 FM |
282 | #define wxDECLARE_PLUGGABLE_CLASS(name) \ |
283 | wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE) | |
284 | #define wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \ | |
285 | wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE) | |
286 | ||
287 | #define wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \ | |
288 | wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo) | |
289 | #define wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \ | |
290 | wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo) | |
291 | ||
292 | #define wxIMPLEMENT_PLUGGABLE_CLASS(name, basename) \ | |
293 | wxIMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name) | |
294 | #define wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
295 | wxIMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name) | |
296 | #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \ | |
297 | wxIMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name) | |
298 | #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
299 | wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name) | |
300 | ||
301 | #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \ | |
302 | wxIMPLEMENT_PLUGGABLE_CLASS(name, basename) | |
303 | #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
304 | wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) | |
305 | #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \ | |
306 | wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) | |
307 | #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
308 | wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) | |
309 | ||
310 | #define wxCLASSINFO(name) (&name::ms_classInfo) | |
c801d85f | 311 | |
c0db9626 | 312 | #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo) |
3013b6f4 | 313 | |
b2edef6f | 314 | // Just seems a bit nicer-looking (pretend it's not a macro) |
c0db9626 | 315 | #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo) |
c801d85f | 316 | |
73fbb031 VZ |
317 | // this cast does some more checks at compile time as it uses static_cast |
318 | // internally | |
319 | // | |
320 | // note that it still has different semantics from dynamic_cast<> and so can't | |
321 | // be replaced by it as long as there are any compilers not supporting it | |
34636400 | 322 | #define wxDynamicCast(obj, className) \ |
5232d996 | 323 | ((className *) wxCheckDynamicCast( \ |
5c33522f VZ |
324 | const_cast<wxObject *>(static_cast<const wxObject *>(\ |
325 | const_cast<className *>(static_cast<const className *>(obj)))), \ | |
5232d996 | 326 | &className::ms_classInfo)) |
0b9ab0bd | 327 | |
b2edef6f VZ |
328 | // The 'this' pointer is always true, so use this version |
329 | // to cast the this pointer and avoid compiler warnings. | |
f7637829 | 330 | #define wxDynamicCastThis(className) \ |
73fbb031 | 331 | (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0) |
33ac7e6f | 332 | |
657a8a35 VZ |
333 | // FIXME-VC6: dummy argument needed because VC6 doesn't support explicitly |
334 | // choosing the template function to call | |
335 | template <class T> | |
336 | inline T *wxCheckCast(const void *ptr, T * = NULL) | |
0b9ab0bd | 337 | { |
657a8a35 VZ |
338 | wxASSERT_MSG( wxDynamicCast(ptr, T), "wxStaticCast() used incorrectly" ); |
339 | return const_cast<T *>(static_cast<const T *>(ptr)); | |
0b9ab0bd | 340 | } |
f6bcfd97 | 341 | |
657a8a35 | 342 | #define wxStaticCast(obj, className) wxCheckCast((obj), (className *)NULL) |
f6bcfd97 | 343 | |
b2edef6f VZ |
344 | // ---------------------------------------------------------------------------- |
345 | // set up memory debugging macros | |
346 | // ---------------------------------------------------------------------------- | |
347 | ||
348 | /* | |
349 | Which new/delete operator variants do we want? | |
350 | ||
351 | _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0) | |
352 | _WX_WANT_DELETE_VOID = void operator delete (void * buf) | |
353 | _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line) | |
354 | _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int) | |
355 | _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0) | |
356 | _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf) | |
357 | _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int ) | |
358 | */ | |
359 | ||
657a8a35 | 360 | #if wxUSE_MEMORY_TRACING |
b2edef6f VZ |
361 | |
362 | // All compilers get this one | |
363 | #define _WX_WANT_NEW_SIZET_WXCHAR_INT | |
364 | ||
365 | // Everyone except Visage gets the next one | |
366 | #ifndef __VISAGECPP__ | |
367 | #define _WX_WANT_DELETE_VOID | |
c801d85f | 368 | #endif |
b2edef6f VZ |
369 | |
370 | // Only visage gets this one under the correct circumstances | |
371 | #if defined(__VISAGECPP__) && __DEBUG_ALLOC__ | |
372 | #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET | |
373 | #endif | |
374 | ||
4e124582 VZ |
375 | // Only VC++ 6 and CodeWarrior get overloaded delete that matches new |
376 | #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \ | |
377 | (defined(__MWERKS__) && (__MWERKS__ >= 0x2400)) | |
b2edef6f | 378 | #define _WX_WANT_DELETE_VOID_WXCHAR_INT |
fd85b064 | 379 | #endif |
c801d85f | 380 | |
b2edef6f VZ |
381 | // Now see who (if anyone) gets the array memory operators |
382 | #if wxUSE_ARRAY_MEMORY_OPERATORS | |
383 | ||
384 | // Everyone except Visual C++ (cause problems for VC++ - crashes) | |
385 | #if !defined(__VISUALC__) | |
386 | #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT | |
387 | #endif | |
388 | ||
389 | // Everyone except Visual C++ (cause problems for VC++ - crashes) | |
390 | #if !defined(__VISUALC__) | |
391 | #define _WX_WANT_ARRAY_DELETE_VOID | |
392 | #endif | |
393 | ||
394 | // Only CodeWarrior 6 or higher | |
395 | #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400) | |
396 | #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT | |
397 | #endif | |
398 | ||
399 | #endif // wxUSE_ARRAY_MEMORY_OPERATORS | |
400 | ||
657a8a35 | 401 | #endif // wxUSE_MEMORY_TRACING |
b2edef6f | 402 | |
f8855e47 | 403 | // ---------------------------------------------------------------------------- |
6d37c1b7 | 404 | // wxRefCounter: ref counted data "manager" |
f8855e47 VZ |
405 | // ---------------------------------------------------------------------------- |
406 | ||
6d37c1b7 | 407 | class WXDLLIMPEXP_BASE wxRefCounter |
f8855e47 | 408 | { |
f8855e47 | 409 | public: |
6d37c1b7 | 410 | wxRefCounter() { m_count = 1; } |
f8855e47 VZ |
411 | |
412 | int GetRefCount() const { return m_count; } | |
413 | ||
4a11340a RR |
414 | void IncRef() { m_count++; } |
415 | void DecRef(); | |
416 | ||
417 | protected: | |
418 | // this object should never be destroyed directly but only as a | |
419 | // result of a DecRef() call: | |
6d37c1b7 | 420 | virtual ~wxRefCounter() { } |
4a11340a | 421 | |
f8855e47 | 422 | private: |
4a11340a | 423 | // our refcount: |
f8855e47 | 424 | int m_count; |
fd08ccd2 VZ |
425 | |
426 | // It doesn't make sense to copy the reference counted objects, a new ref | |
427 | // counter should be created for a new object instead and compilation | |
428 | // errors in the code using wxRefCounter due to the lack of copy ctor often | |
429 | // indicate a problem, e.g. a forgotten copy ctor implementation somewhere. | |
430 | wxDECLARE_NO_COPY_CLASS(wxRefCounter); | |
f8855e47 VZ |
431 | }; |
432 | ||
6d37c1b7 RR |
433 | // ---------------------------------------------------------------------------- |
434 | // wxObjectRefData: ref counted data meant to be stored in wxObject | |
435 | // ---------------------------------------------------------------------------- | |
436 | ||
437 | typedef wxRefCounter wxObjectRefData; | |
438 | ||
439 | ||
4a11340a RR |
440 | // ---------------------------------------------------------------------------- |
441 | // wxObjectDataPtr: helper class to avoid memleaks because of missing calls | |
442 | // to wxObjectRefData::DecRef | |
443 | // ---------------------------------------------------------------------------- | |
444 | ||
445 | template <class T> | |
446 | class wxObjectDataPtr | |
447 | { | |
448 | public: | |
449 | typedef T element_type; | |
450 | ||
451 | wxEXPLICIT wxObjectDataPtr(T *ptr = NULL) : m_ptr(ptr) {} | |
452 | ||
453 | // copy ctor | |
53a2db12 | 454 | wxObjectDataPtr(const wxObjectDataPtr<T> &tocopy) |
4a11340a | 455 | : m_ptr(tocopy.m_ptr) |
53a2db12 | 456 | { |
4a11340a | 457 | if (m_ptr) |
53a2db12 | 458 | m_ptr->IncRef(); |
4a11340a RR |
459 | } |
460 | ||
53a2db12 FM |
461 | ~wxObjectDataPtr() |
462 | { | |
463 | if (m_ptr) | |
464 | m_ptr->DecRef(); | |
4a11340a RR |
465 | } |
466 | ||
467 | T *get() const { return m_ptr; } | |
53a2db12 | 468 | |
a60b0ddc RR |
469 | // test for pointer validity: defining conversion to unspecified_bool_type |
470 | // and not more obvious bool to avoid implicit conversions to integer types | |
471 | typedef T *(wxObjectDataPtr<T>::*unspecified_bool_type)() const; | |
472 | operator unspecified_bool_type() const | |
473 | { | |
474 | return m_ptr ? &wxObjectDataPtr<T>::get : NULL; | |
475 | } | |
476 | ||
477 | T& operator*() const | |
53a2db12 FM |
478 | { |
479 | wxASSERT(m_ptr != NULL); | |
a60b0ddc RR |
480 | return *(m_ptr); |
481 | } | |
53a2db12 | 482 | |
e39d30c0 | 483 | T *operator->() const |
53a2db12 FM |
484 | { |
485 | wxASSERT(m_ptr != NULL); | |
486 | return get(); | |
e39d30c0 | 487 | } |
4a11340a RR |
488 | |
489 | void reset(T *ptr) | |
490 | { | |
491 | if (m_ptr) | |
492 | m_ptr->DecRef(); | |
493 | m_ptr = ptr; | |
494 | } | |
495 | ||
496 | wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy) | |
53a2db12 FM |
497 | { |
498 | if (m_ptr) | |
499 | m_ptr->DecRef(); | |
500 | m_ptr = tocopy.m_ptr; | |
4a11340a | 501 | if (m_ptr) |
53a2db12 | 502 | m_ptr->IncRef(); |
4a11340a RR |
503 | return *this; |
504 | } | |
505 | ||
506 | wxObjectDataPtr& operator=(T *ptr) | |
53a2db12 FM |
507 | { |
508 | if (m_ptr) | |
509 | m_ptr->DecRef(); | |
510 | m_ptr = ptr; | |
4a11340a RR |
511 | return *this; |
512 | } | |
513 | ||
514 | private: | |
515 | T *m_ptr; | |
516 | }; | |
517 | ||
0b9ab0bd | 518 | // ---------------------------------------------------------------------------- |
77ffb593 | 519 | // wxObject: the root class of wxWidgets object hierarchy |
0b9ab0bd RL |
520 | // ---------------------------------------------------------------------------- |
521 | ||
bddd7a8d | 522 | class WXDLLIMPEXP_BASE wxObject |
c801d85f | 523 | { |
b19b28c8 | 524 | wxDECLARE_ABSTRACT_CLASS(wxObject); |
c801d85f | 525 | |
0b9ab0bd | 526 | public: |
b2edef6f | 527 | wxObject() { m_refData = NULL; } |
0b9ab0bd | 528 | virtual ~wxObject() { UnRef(); } |
4393b50c | 529 | |
a6391d30 | 530 | wxObject(const wxObject& other) |
66e9a9f0 | 531 | { |
f8855e47 VZ |
532 | m_refData = other.m_refData; |
533 | if (m_refData) | |
6d37c1b7 | 534 | m_refData->IncRef(); |
66e9a9f0 | 535 | } |
4393b50c | 536 | |
a6391d30 GD |
537 | wxObject& operator=(const wxObject& other) |
538 | { | |
539 | if ( this != &other ) | |
540 | { | |
f8855e47 | 541 | Ref(other); |
a6391d30 GD |
542 | } |
543 | return *this; | |
544 | } | |
545 | ||
303c6f20 | 546 | bool IsKindOf(const wxClassInfo *info) const; |
c801d85f | 547 | |
0b9ab0bd | 548 | |
b2edef6f VZ |
549 | // Turn on the correct set of new and delete operators |
550 | ||
551 | #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT | |
cf760e4c | 552 | void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 ); |
0b9ab0bd | 553 | #endif |
27198be4 | 554 | |
b2edef6f VZ |
555 | #ifdef _WX_WANT_DELETE_VOID |
556 | void operator delete ( void * buf ); | |
557 | #endif | |
0b9ab0bd | 558 | |
b2edef6f VZ |
559 | #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET |
560 | void operator delete ( void *buf, const char *_fname, size_t _line ); | |
0b9ab0bd | 561 | #endif |
76626af2 | 562 | |
b2edef6f | 563 | #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT |
cf760e4c | 564 | void operator delete ( void *buf, const wxChar*, int ); |
b2edef6f | 565 | #endif |
8cfc5426 | 566 | |
b2edef6f | 567 | #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT |
cf760e4c | 568 | void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 ); |
0b9ab0bd RL |
569 | #endif |
570 | ||
b2edef6f VZ |
571 | #ifdef _WX_WANT_ARRAY_DELETE_VOID |
572 | void operator delete[] ( void *buf ); | |
0b9ab0bd | 573 | #endif |
27198be4 | 574 | |
b2edef6f | 575 | #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT |
cf760e4c | 576 | void operator delete[] (void* buf, const wxChar*, int ); |
b2edef6f | 577 | #endif |
c801d85f | 578 | |
807d8487 VZ |
579 | // ref counted data handling methods |
580 | ||
581 | // get/set | |
582 | wxObjectRefData *GetRefData() const { return m_refData; } | |
583 | void SetRefData(wxObjectRefData *data) { m_refData = data; } | |
584 | ||
585 | // make a 'clone' of the object | |
0b9ab0bd | 586 | void Ref(const wxObject& clone); |
c801d85f | 587 | |
807d8487 | 588 | // destroy a reference |
0b9ab0bd | 589 | void UnRef(); |
c801d85f | 590 | |
93c5f755 PC |
591 | // Make sure this object has only one reference |
592 | void UnShare() { AllocExclusive(); } | |
593 | ||
a3ab1c18 VZ |
594 | // check if this object references the same data as the other one |
595 | bool IsSameAs(const wxObject& o) const { return m_refData == o.m_refData; } | |
55ccdb93 | 596 | |
c801d85f | 597 | protected: |
807d8487 VZ |
598 | // ensure that our data is not shared with anybody else: if we have no |
599 | // data, it is created using CreateRefData() below, if we have shared data | |
600 | // it is copied using CloneRefData(), otherwise nothing is done | |
601 | void AllocExclusive(); | |
602 | ||
4e124582 VZ |
603 | // both methods must be implemented if AllocExclusive() is used, not pure |
604 | // virtual only because of the backwards compatibility reasons | |
807d8487 VZ |
605 | |
606 | // create a new m_refData | |
607 | virtual wxObjectRefData *CreateRefData() const; | |
608 | ||
609 | // create a new m_refData initialized with the given one | |
b8027888 | 610 | virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; |
807d8487 | 611 | |
0b9ab0bd | 612 | wxObjectRefData *m_refData; |
c801d85f KB |
613 | }; |
614 | ||
ea1e6c4b RL |
615 | inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo) |
616 | { | |
b2edef6f | 617 | return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL; |
ea1e6c4b RL |
618 | } |
619 | ||
2d51f067 SC |
620 | #if wxUSE_EXTENDED_RTTI |
621 | class WXDLLIMPEXP_BASE wxDynamicObject : public wxObject | |
622 | { | |
b5dbe15d | 623 | friend class WXDLLIMPEXP_FWD_BASE wxDynamicClassInfo ; |
2d51f067 SC |
624 | public: |
625 | // instantiates this object with an instance of its superclass | |
626 | wxDynamicObject(wxObject* superClassInstance, const wxDynamicClassInfo *info) ; | |
d3c7fc99 | 627 | virtual ~wxDynamicObject(); |
2d51f067 | 628 | |
8f2b1cfd SC |
629 | void SetProperty (const wxChar *propertyName, const wxxVariant &value); |
630 | wxxVariant GetProperty (const wxChar *propertyName) const ; | |
2d51f067 SC |
631 | |
632 | // get the runtime identity of this object | |
633 | wxClassInfo *GetClassInfo() const | |
634 | { | |
f525dc35 JS |
635 | #ifdef _MSC_VER |
636 | return (wxClassInfo*) m_classInfo; | |
637 | #else | |
5c33522f VZ |
638 | wxDynamicClassInfo *nonconst = const_cast<wxDynamicClassInfo *>(m_classInfo); |
639 | return static_cast<wxClassInfo *>(nonconst); | |
f525dc35 | 640 | #endif |
2d51f067 SC |
641 | } |
642 | ||
643 | wxObject* GetSuperClassInstance() const | |
644 | { | |
645 | return m_superClassInstance ; | |
646 | } | |
647 | private : | |
8f2b1cfd SC |
648 | // removes an existing runtime-property |
649 | void RemoveProperty( const wxChar *propertyName ) ; | |
650 | ||
651 | // renames an existing runtime-property | |
652 | void RenameProperty( const wxChar *oldPropertyName , const wxChar *newPropertyName ) ; | |
653 | ||
2d51f067 SC |
654 | wxObject *m_superClassInstance ; |
655 | const wxDynamicClassInfo *m_classInfo; | |
656 | struct wxDynamicObjectInternal; | |
657 | wxDynamicObjectInternal *m_data; | |
658 | }; | |
659 | #endif | |
660 | ||
b2edef6f VZ |
661 | // ---------------------------------------------------------------------------- |
662 | // more debugging macros | |
663 | // ---------------------------------------------------------------------------- | |
664 | ||
657a8a35 | 665 | #if wxUSE_DEBUG_NEW_ALWAYS |
70bf6180 VZ |
666 | #define WXDEBUG_NEW new(__TFILE__,__LINE__) |
667 | ||
657a8a35 VZ |
668 | #if wxUSE_GLOBAL_MEMORY_OPERATORS |
669 | #define new WXDEBUG_NEW | |
670 | #elif defined(__VISUALC__) | |
671 | // Including this file redefines new and allows leak reports to | |
672 | // contain line numbers | |
673 | #include "wx/msw/msvcrt.h" | |
674 | #endif | |
675 | #endif // wxUSE_DEBUG_NEW_ALWAYS | |
c801d85f | 676 | |
b19b28c8 FM |
677 | // ---------------------------------------------------------------------------- |
678 | // Compatibility macro aliases | |
679 | // ---------------------------------------------------------------------------- | |
680 | ||
681 | // deprecated variants _not_ requiring a semicolon after them and without wx prefix. | |
682 | // (note that also some wx-prefixed macro do _not_ require a semicolon because | |
683 | // it's not always possible to force the compire to require it) | |
684 | ||
685 | #define DECLARE_CLASS_INFO_ITERATORS() wxDECLARE_CLASS_INFO_ITERATORS(); | |
686 | #define DECLARE_ABSTRACT_CLASS(n) wxDECLARE_ABSTRACT_CLASS(n); | |
687 | #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(n) wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(n); | |
688 | #define DECLARE_DYNAMIC_CLASS_NO_COPY(n) wxDECLARE_DYNAMIC_CLASS_NO_COPY(n); | |
689 | #define DECLARE_DYNAMIC_CLASS(n) wxDECLARE_DYNAMIC_CLASS(n); | |
690 | #define DECLARE_CLASS(n) wxDECLARE_CLASS(n); | |
691 | ||
692 | #define IMPLEMENT_DYNAMIC_CLASS(n,b) wxIMPLEMENT_DYNAMIC_CLASS(n,b) | |
693 | #define IMPLEMENT_DYNAMIC_CLASS2(n,b1,b2) wxIMPLEMENT_DYNAMIC_CLASS2(n,b1,b2) | |
694 | #define IMPLEMENT_ABSTRACT_CLASS(n,b) wxIMPLEMENT_ABSTRACT_CLASS(n,b) | |
695 | #define IMPLEMENT_ABSTRACT_CLASS2(n,b1,b2) wxIMPLEMENT_ABSTRACT_CLASS2(n,b1,b2) | |
696 | #define IMPLEMENT_CLASS(n,b) wxIMPLEMENT_CLASS(n,b) | |
697 | #define IMPLEMENT_CLASS2(n,b1,b2) wxIMPLEMENT_CLASS2(n,b1,b2) | |
698 | ||
699 | #define DECLARE_PLUGGABLE_CLASS(n) wxDECLARE_PLUGGABLE_CLASS(n); | |
700 | #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(n) wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(n); | |
701 | #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u); | |
702 | #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u); | |
703 | ||
704 | #define IMPLEMENT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_PLUGGABLE_CLASS(n,b) | |
705 | #define IMPLEMENT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_PLUGGABLE_CLASS2(n,b,b2) | |
706 | #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b) | |
707 | #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) | |
708 | #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b) | |
709 | #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2) | |
710 | #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b) | |
711 | #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) | |
712 | ||
713 | #define CLASSINFO(n) wxCLASSINFO(n) | |
714 | ||
c0089c96 | 715 | #endif // _WX_OBJECTH__ |