]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/object.h
converted to 16 colors
[wxWidgets.git] / include / wx / object.h
... / ...
CommitLineData
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
22class WXDLLEXPORT wxObject;
23
24#if wxUSE_DYNAMIC_CLASSES
25
26// #ifdef __GNUWIN32__
27#ifdef GetClassName
28#undef GetClassName
29#endif
30#ifdef GetClassInfo
31#undef GetClassInfo
32#endif
33// #endif
34
35class WXDLLEXPORT wxClassInfo;
36class WXDLLEXPORT wxInputStream;
37class WXDLLEXPORT wxOutputStream;
38class WXDLLEXPORT wxObjectInputStream;
39class WXDLLEXPORT wxObjectOutputStream;
40class WXDLLEXPORT wxHashTable;
41class WXDLLEXPORT wxObject_Serialize;
42
43#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
44 #include "wx/ioswrap.h"
45#endif
46
47/*
48 * Dynamic object system declarations
49 */
50
51typedef wxObject * (*wxObjectConstructorFn) (void);
52
53class WXDLLEXPORT wxClassInfo
54{
55 public:
56 wxClassInfo(wxChar *cName, wxChar *baseName1, wxChar *baseName2, int sz, wxObjectConstructorFn fn);
57
58 wxObject *CreateObject(void);
59
60 inline wxChar *GetClassName(void) const { return m_className; }
61 inline wxChar *GetBaseClassName1(void) const { return m_baseClassName1; }
62 inline wxChar *GetBaseClassName2(void) const { return m_baseClassName2; }
63 inline wxClassInfo* GetBaseClass1() const { return m_baseInfo1; }
64 inline wxClassInfo* GetBaseClass2() const { return m_baseInfo2; }
65 inline int GetSize(void) const { return m_objectSize; }
66 inline wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; }
67 static inline wxClassInfo* GetFirst() { return sm_first; }
68 inline wxClassInfo* GetNext() const { return m_next; }
69 bool IsKindOf(wxClassInfo *info) const;
70
71 static wxClassInfo *FindClass(wxChar *c);
72
73 // Initializes parent pointers and hash table for fast searching.
74 static void InitializeClasses(void);
75
76 // Cleans up hash table used for fast searching.
77 static void CleanUpClasses(void);
78
79public:
80 wxChar* m_className;
81 wxChar* m_baseClassName1;
82 wxChar* m_baseClassName2;
83 int m_objectSize;
84 wxObjectConstructorFn m_objectConstructor;
85
86 // Pointers to base wxClassInfos: set in InitializeClasses
87 // called from wx_main.cc
88 wxClassInfo* m_baseInfo1;
89 wxClassInfo* m_baseInfo2;
90
91 static wxClassInfo* sm_first;
92 wxClassInfo* m_next;
93
94 static wxHashTable* sm_classTable;
95};
96
97WXDLLEXPORT wxObject* wxCreateDynamicObject(const wxChar *name);
98
99#if wxUSE_SERIAL
100WXDLLEXPORT wxObject* wxCreateStoredObject( wxInputStream& stream );
101#endif
102
103#define DECLARE_DYNAMIC_CLASS(name) \
104 public:\
105 static wxClassInfo sm_class##name;\
106 wxClassInfo *GetClassInfo() const \
107 { return &name::sm_class##name; }
108
109#define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
110#define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
111
112//////
113////// for concrete classes
114//////
115
116// Single inheritance with one base class
117#define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
118wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
119 { return new name; }\
120 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
121
122// Multiple inheritance with two base classes
123#define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
124wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
125 { return new name; }\
126 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename1), (wxChar *) _T(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
127
128//////
129////// for abstract classes
130//////
131
132// Single inheritance with one base class
133#define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
134 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename), \
135 (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL);
136
137// Multiple inheritance with two base classes
138#define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
139 wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename1), \
140 (wxChar *) _T(#basename2), (int) sizeof(name), (wxObjectConstructorFn) NULL);
141
142#define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
143#define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
144
145#define CLASSINFO(name) (&name::sm_class##name)
146
147#else // !wxUSE_DYNAMIC_CLASSES
148
149// No dynamic class system: so stub out the macros
150#define DECLARE_DYNAMIC_CLASS(name)
151#define DECLARE_ABSTRACT_CLASS(name)
152#define DECLARE_CLASS(name)
153#define IMPLEMENT_DYNAMIC_CLASS(name, basename)
154#define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
155#define IMPLEMENT_ABSTRACT_CLASS(name, basename)
156#define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
157#define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
158#define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
159
160#endif // wxUSE_DYNAMIC_CLASSES/!wxUSE_DYNAMIC_CLASSES
161
162#define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
163
164// Just seems a bit nicer-looking (pretend it's not a macro)
165#define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
166
167// to be replaced by dynamic_cast<> in the future
168#define wxDynamicCast(obj, className) \
169 ((obj) && ((obj)->IsKindOf(&className::sm_class##className)) \
170 ? (className *)(obj) \
171 : (className *)0)
172
173// Unfortunately Borland seems to need this include.
174#ifdef __BORLANDC__
175 #if wxUSE_IOSTREAMH
176 #include <iostream.h>
177 #else
178 #include <iostream>
179 #endif
180#endif
181
182class WXDLLEXPORT wxObjectRefData;
183
184class WXDLLEXPORT wxObject
185{
186 public:
187
188 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
189 static wxClassInfo sm_classwxObject;
190
191 wxObject(void);
192 virtual ~wxObject(void);
193
194 virtual wxClassInfo *GetClassInfo(void) const { return &sm_classwxObject; }
195 wxObject *Clone(void) const;
196 virtual void CopyObject(wxObject& object_dest) const;
197
198 bool IsKindOf(wxClassInfo *info) const;
199
200#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
201 void * operator new (size_t size, wxChar * fileName = NULL, int lineNum = 0);
202 void operator delete (void * buf);
203
204// VC++ 6.0
205#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
206 void operator delete(void *buf, char*, int);
207#endif
208
209 // Causes problems for VC++
210#if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
211 void * operator new[] (size_t size, wxChar * fileName = NULL, int lineNum = 0);
212 void operator delete[] (void * buf);
213#endif
214
215#ifdef __MWERKS__
216 void * operator new[] (size_t size, wxChar * fileName , int lineNum = 0);
217 void operator delete[] (void * buf);
218#endif
219
220#endif // Debug & memory tracing
221
222#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
223 virtual void Dump(ostream& str);
224#endif
225
226#if wxUSE_SERIAL
227 virtual void StoreObject( wxObjectOutputStream &stream );
228 virtual void LoadObject( wxObjectInputStream &stream );
229#endif
230
231 // make a 'clone' of the object
232 void Ref(const wxObject& clone);
233
234 // destroy a reference
235 void UnRef(void);
236
237 inline wxObjectRefData *GetRefData(void) const { return m_refData; }
238 inline void SetRefData(wxObjectRefData *data) { m_refData = data; }
239
240protected:
241 wxObjectRefData* m_refData;
242#if wxUSE_SERIAL
243 wxObject_Serialize* m_serialObj;
244#endif
245};
246
247/*
248 * wxObjectData
249 */
250
251class WXDLLEXPORT wxObjectRefData {
252
253 friend class wxObject;
254
255public:
256 wxObjectRefData(void);
257 virtual ~wxObjectRefData(void);
258
259 inline int GetRefCount(void) const { return m_count; }
260private:
261 int m_count;
262};
263
264#ifdef __WXDEBUG__
265#ifndef WXDEBUG_NEW
266#define WXDEBUG_NEW new(__TFILE__,__LINE__)
267#endif
268#else
269#define WXDEBUG_NEW new
270#endif
271
272// Redefine new to be the debugging version. This doesn't
273// work with all compilers, in which case you need to
274// use WXDEBUG_NEW explicitly if you wish to use the debugging version.
275
276#if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
277#define new new(__TFILE__,__LINE__)
278#endif
279
280#endif
281 // _WX_OBJECTH__
282
283