]> git.saurik.com Git - wxWidgets.git/blob - include/wx/object.h
Fix for upside down bmp projections
[wxWidgets.git] / include / wx / object.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/object.h
3 // Purpose: wxObject class, plus run-time type information macros
4 // Author: Julian Smart
5 // Modified by: Ron Lee
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Julian Smart and Markus Holzem
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_OBJECTH__
14 #define _WX_OBJECTH__
15
16 #ifdef __GNUG__
17 #pragma interface "object.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 #include "wx/defs.h"
25 #include "wx/memory.h"
26
27 class WXDLLEXPORT wxObject;
28
29 #if wxUSE_DYNAMIC_CLASSES
30
31 // ----------------------------------------------------------------------------
32 // conditional compilation
33 // ----------------------------------------------------------------------------
34
35 // this shouldn't be needed any longer as <wx/msw/private.h> does it but it
36 // doesn't hurt neither
37 #ifdef GetClassName
38 #undef GetClassName
39 #endif
40 #ifdef GetClassInfo
41 #undef GetClassInfo
42 #endif
43
44 class WXDLLEXPORT wxClassInfo;
45 class WXDLLEXPORT wxHashTable;
46 class WXDLLEXPORT wxObjectRefData;
47
48 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
49 #include "wx/ioswrap.h"
50 #endif
51
52
53 // ----------------------------------------------------------------------------
54 // wxClassInfo
55 // ----------------------------------------------------------------------------
56
57 typedef wxObject *(*wxObjectConstructorFn)(void);
58
59 class WXDLLEXPORT wxClassInfo
60 {
61 public:
62 wxClassInfo( const wxChar *className,
63 const wxChar *baseName1,
64 const wxChar *baseName2,
65 int size,
66 wxObjectConstructorFn ctor )
67 : m_className(className)
68 , m_baseClassName1(baseName1)
69 , m_baseClassName2(baseName2)
70 , m_objectSize(size)
71 , m_objectConstructor(ctor)
72 , m_baseInfo1(0)
73 , m_baseInfo2(0)
74 , m_next(sm_first)
75 {
76 #ifdef __WXDEBUG__
77 if (sm_classTable != NULL) {
78 wxString msg(_T("too late binding of class info (lazy binding) for "));
79 msg += className;
80 wxFAIL_MSG(msg);
81 }
82 #endif
83 sm_first = this;
84 }
85
86 wxObject *CreateObject() { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
87
88 const wxChar *GetClassName() const { return m_className; }
89 const wxChar *GetBaseClassName1() const { return m_baseClassName1; }
90 const wxChar *GetBaseClassName2() const { return m_baseClassName2; }
91 const wxClassInfo *GetBaseClass1() const { return m_baseInfo1; }
92 const wxClassInfo *GetBaseClass2() const { return m_baseInfo2; }
93 int GetSize() const { return m_objectSize; }
94
95 wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; }
96 static const wxClassInfo *GetFirst() { return sm_first; }
97 const wxClassInfo *GetNext() const { return m_next; }
98 static wxClassInfo *FindClass(const wxChar *className);
99
100 // Climb upwards through inheritance hierarchy.
101 // Dual inheritance is catered for.
102
103 bool IsKindOf(const wxClassInfo *info) const
104 {
105 return info != 0 &&
106 ( info == this ||
107 ( m_baseInfo1 && m_baseInfo1->IsKindOf(info) ) ||
108 ( m_baseInfo2 && m_baseInfo2->IsKindOf(info) ) );
109 }
110
111 // Initializes parent pointers and hash table for fast searching.
112
113 static void InitializeClasses();
114
115 // Cleans up hash table used for fast searching.
116
117 static void CleanUpClasses();
118
119 public:
120 const wxChar *m_className;
121 const wxChar *m_baseClassName1;
122 const wxChar *m_baseClassName2;
123 int m_objectSize;
124 wxObjectConstructorFn m_objectConstructor;
125
126 // Pointers to base wxClassInfos: set in InitializeClasses
127
128 const wxClassInfo *m_baseInfo1;
129 const wxClassInfo *m_baseInfo2;
130
131 // class info object live in a linked list:
132 // pointers to its head and the next element in it
133
134 static wxClassInfo *sm_first;
135 wxClassInfo *m_next;
136
137 // FIXME: this should be private (currently used directly by way too
138 // many clients)
139 static wxHashTable *sm_classTable;
140
141 private:
142 // InitializeClasses() helper
143 static wxClassInfo *GetBaseByName(const wxChar *name);
144
145 DECLARE_NO_COPY_CLASS(wxClassInfo)
146 };
147
148 WXDLLEXPORT wxObject *wxCreateDynamicObject(const wxChar *name);
149
150 // ----------------------------------------------------------------------------
151 // Dynamic class macros
152 // ----------------------------------------------------------------------------
153
154 #define DECLARE_DYNAMIC_CLASS(name) \
155 public: \
156 static wxClassInfo sm_class##name; \
157 virtual wxClassInfo *GetClassInfo() const \
158 { return &name::sm_class##name; }
159
160 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
161 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
162
163 // -----------------------------------
164 // for concrete classes
165 // -----------------------------------
166
167 // Single inheritance with one base class
168
169 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
170 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
171 { return new name; } \
172 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
173 0, (int) sizeof(name), \
174 (wxObjectConstructorFn) wxConstructorFor##name);
175
176 // Multiple inheritance with two base classes
177
178 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
179 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
180 { return new name; } \
181 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
182 wxT(#basename2), (int) sizeof(name), \
183 (wxObjectConstructorFn) wxConstructorFor##name);
184
185 // -----------------------------------
186 // for abstract classes
187 // -----------------------------------
188
189 // Single inheritance with one base class
190
191 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
192 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
193 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
194
195 // Multiple inheritance with two base classes
196
197 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
198 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
199 wxT(#basename2), (int) sizeof(name), \
200 (wxObjectConstructorFn) 0);
201
202 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
203 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
204
205 // -----------------------------------
206 // for pluggable classes
207 // -----------------------------------
208
209 // NOTE: this should probably be the very first statement
210 // in the class declaration so wxPluginSentinel is
211 // the first member initialised and the last destroyed.
212
213 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
214
215 #if wxUSE_NESTED_CLASSES
216
217 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
218 class exportdecl name##PluginSentinel { \
219 private: \
220 static const wxString sm_className; \
221 public: \
222 name##PluginSentinel(); \
223 ~name##PluginSentinel(); \
224 }; \
225 name##PluginSentinel m_pluginsentinel;
226
227 #define _IMPLEMENT_DL_SENTINEL(name) \
228 const wxString name::name##PluginSentinel::sm_className(#name); \
229 name::name##PluginSentinel::name##PluginSentinel() { \
230 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
231 if( e != 0 ) { e->RefObj(); } \
232 } \
233 name::name##PluginSentinel::~name##PluginSentinel() { \
234 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
235 if( e != 0 ) { e->UnrefObj(); } \
236 }
237 #else
238
239 #define _DECLARE_DL_SENTINEL(name)
240 #define _IMPLEMENT_DL_SENTINEL(name)
241
242 #endif // wxUSE_NESTED_CLASSES
243
244 #define DECLARE_PLUGGABLE_CLASS(name) \
245 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
246 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
247 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
248
249 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
250 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
251 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
252 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
253
254 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
255 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
256 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
257 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
258 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
259 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
260 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
261 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
262
263 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
264 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
265 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
266 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
267 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
268 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
269 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
270 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
271
272
273 #define CLASSINFO(name) (&name::sm_class##name)
274
275 #else // !wxUSE_DYNAMIC_CLASSES
276
277 // No dynamic class system: so stub out the macros
278
279 #define DECLARE_DYNAMIC_CLASS(name)
280 #define DECLARE_ABSTRACT_CLASS(name)
281 #define DECLARE_CLASS(name)
282 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
283 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
284 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
285 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
286 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
287 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
288
289 #define DECLARE_PLUGGABLE_CLASS(name)
290 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
291 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
292 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
293 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
294 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
295
296 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
297 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
298 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
299 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
300 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
301 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
302
303 #endif // wxUSE_DYNAMIC_CLASSES
304
305
306 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
307
308 // Just seems a bit nicer-looking (pretend it's not a macro)
309 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
310
311 // to be replaced by dynamic_cast<> in the future
312 #define wxDynamicCast(obj, className) \
313 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
314
315 // The 'this' pointer is always true, so use this version
316 // to cast the this pointer and avoid compiler warnings.
317 #define wxDynamicCastThis(className) \
318 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
319
320 #define wxConstCast(obj, className) ((className *)(obj))
321
322
323 #ifdef __WXDEBUG__
324 inline void wxCheckCast(void *ptr)
325 {
326 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
327 }
328 #define wxStaticCast(obj, className) \
329 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
330
331 #else // !__WXDEBUG__
332 #define wxStaticCast(obj, className) ((className *)(obj))
333
334 #endif // __WXDEBUG__
335
336
337 // for some reason Borland seems to need this include.
338 #if wxUSE_STD_IOSTREAM \
339 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
340 && defined(__BORLANDC__)
341 #if wxUSE_IOSTREAMH
342 #include <iostream.h>
343 #else
344 #include <iostream>
345 #endif
346 #endif // wxUSE_IOSTREAMH
347
348 // ----------------------------------------------------------------------------
349 // set up memory debugging macros
350 // ----------------------------------------------------------------------------
351
352 /*
353 Which new/delete operator variants do we want?
354
355 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
356 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
357 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
358 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
359 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
360 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
361 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
362 */
363
364 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
365
366 // All compilers get this one
367 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
368
369 // Everyone except Visage gets the next one
370 #ifndef __VISAGECPP__
371 #define _WX_WANT_DELETE_VOID
372 #endif
373
374 // Only visage gets this one under the correct circumstances
375 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
376 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
377 #endif
378
379 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
380 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
381 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
382 #endif
383
384 // Now see who (if anyone) gets the array memory operators
385 #if wxUSE_ARRAY_MEMORY_OPERATORS
386
387 // Everyone except Visual C++ (cause problems for VC++ - crashes)
388 #if !defined(__VISUALC__)
389 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
390 #endif
391
392 // Everyone except Visual C++ (cause problems for VC++ - crashes)
393 #if !defined(__VISUALC__)
394 #define _WX_WANT_ARRAY_DELETE_VOID
395 #endif
396
397 // Only CodeWarrior 6 or higher
398 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
399 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
400 #endif
401
402 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
403
404 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
405
406
407 // ----------------------------------------------------------------------------
408 // wxObject: the root class of wxWindows object hierarchy
409 // ----------------------------------------------------------------------------
410
411 class WXDLLEXPORT wxObject
412 {
413 DECLARE_ABSTRACT_CLASS(wxObject)
414
415 private:
416 void InitFrom(const wxObject& other);
417
418 public:
419 wxObject() { m_refData = NULL; }
420 virtual ~wxObject() { UnRef(); }
421
422 wxObject(const wxObject& other)
423 {
424 InitFrom(other);
425 }
426
427 wxObject& operator=(const wxObject& other)
428 {
429 if ( this != &other )
430 {
431 UnRef();
432 InitFrom(other);
433 }
434 return *this;
435 }
436
437 bool IsKindOf(wxClassInfo *info) const;
438
439
440 // Turn on the correct set of new and delete operators
441
442 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
443 void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
444 #endif
445
446 #ifdef _WX_WANT_DELETE_VOID
447 void operator delete ( void * buf );
448 #endif
449
450 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
451 void operator delete ( void *buf, const char *_fname, size_t _line );
452 #endif
453
454 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
455 void operator delete ( void *buf, const wxChar*, int );
456 #endif
457
458 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
459 void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
460 #endif
461
462 #ifdef _WX_WANT_ARRAY_DELETE_VOID
463 void operator delete[] ( void *buf );
464 #endif
465
466 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
467 void operator delete[] (void* buf, const wxChar*, int );
468 #endif
469
470
471 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
472 virtual void Dump(wxSTD ostream& str);
473 #endif
474
475 // ref counted data handling methods
476
477 // get/set
478 wxObjectRefData *GetRefData() const { return m_refData; }
479 void SetRefData(wxObjectRefData *data) { m_refData = data; }
480
481 // make a 'clone' of the object
482 void Ref(const wxObject& clone);
483
484 // destroy a reference
485 void UnRef();
486
487 protected:
488 // ensure that our data is not shared with anybody else: if we have no
489 // data, it is created using CreateRefData() below, if we have shared data
490 // it is copied using CloneRefData(), otherwise nothing is done
491 void AllocExclusive();
492
493 // both methods must be implemented if Unshare() is used, not pure virtual
494 // only because of the backwards compatibility reasons
495
496 // create a new m_refData
497 virtual wxObjectRefData *CreateRefData() const;
498
499 // create a new m_refData initialized with the given one
500 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
501
502 wxObjectRefData *m_refData;
503 };
504
505 // ----------------------------------------------------------------------------
506 // wxObjectRefData: ref counted data meant to be stored in wxObject
507 // ----------------------------------------------------------------------------
508
509 class WXDLLEXPORT wxObjectRefData
510 {
511 friend class wxObject;
512
513 public:
514 wxObjectRefData() : m_count(1) { }
515 virtual ~wxObjectRefData() { }
516
517 int GetRefCount() const { return m_count; }
518
519 private:
520 int m_count;
521 };
522
523
524 inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
525 {
526 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL;
527 }
528
529 // ----------------------------------------------------------------------------
530 // more debugging macros
531 // ----------------------------------------------------------------------------
532
533 #ifdef __WXDEBUG__
534 #ifndef WXDEBUG_NEW
535 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
536 #endif
537 #else // !__WXDEBUG__
538 #define WXDEBUG_NEW new
539 #endif
540
541 // Redefine new to be the debugging version. This doesn't work with all
542 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
543 // to use the debugging version.
544
545 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
546 #define new new(__TFILE__,__LINE__)
547 #endif
548
549 #endif // _WX_OBJECTH__
550