]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: object.cpp | |
3 | // Purpose: wxObject implementation | |
4 | // Author: Julian Smart | |
0b9ab0bd | 5 | // Modified by: Ron Lee |
c801d85f KB |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
0b9ab0bd RL |
8 | // Copyright: (c) 1998 Julian Smart and Markus Holzem |
9 | // (c) 2001 Ron Lee <ron@debian.org> | |
3f4a0c5b | 10 | // Licence: wxWindows license |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | #ifdef __GNUG__ | |
14 | #pragma implementation "object.h" | |
15 | #endif | |
16 | ||
0b9ab0bd RL |
17 | // For compilers that support precompilation, includes "wx.h". |
18 | ||
c801d85f KB |
19 | #include "wx/wxprec.h" |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
1678ad78 | 25 | #ifndef WX_PRECOMP |
0b9ab0bd | 26 | #include "wx/hash.h" |
24eb81cb DW |
27 | #endif |
28 | ||
c801d85f KB |
29 | #include <string.h> |
30 | #include <assert.h> | |
31 | ||
ea57084d | 32 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
c801d85f KB |
33 | #include "wx/memory.h" |
34 | #endif | |
35 | ||
ea57084d | 36 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
3f4a0c5b | 37 | // for wxObject::Dump |
0b9ab0bd | 38 | #include "wx/ioswrap.h" |
c801d85f | 39 | |
0b9ab0bd RL |
40 | #if defined(__VISAGECPP__) |
41 | #define DEBUG_PRINTF(NAME) { static int raz=0; \ | |
42 | printf( #NAME " %i\n",raz); fflush(stdout); raz++; } | |
43 | #else | |
44 | #define DEBUG_PRINTF(NAME) | |
45 | #endif | |
c801d85f | 46 | |
0b9ab0bd | 47 | #endif |
f6bcfd97 | 48 | |
0b9ab0bd RL |
49 | |
50 | wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0, | |
51 | (int) sizeof(wxObject), | |
52 | (wxObjectConstructorFn) 0 ); | |
53 | wxClassInfo* wxClassInfo::sm_first = 0; | |
54 | wxHashTable* wxClassInfo::sm_classTable = 0; | |
c801d85f | 55 | |
0b9ab0bd RL |
56 | // These are here so we can avoid 'always true/false' warnings |
57 | // by referring to these instead of TRUE/FALSE | |
c801d85f | 58 | |
0b9ab0bd RL |
59 | const bool wxTrue = TRUE; |
60 | const bool wxFalse = FALSE; | |
c801d85f | 61 | |
0b9ab0bd RL |
62 | // Is this object a kind of (a subclass of) 'info'? |
63 | // E.g. is wxWindow a kind of wxObject? | |
64 | // Go from this class to superclass, taking into account | |
65 | // two possible base classes. | |
afb74891 | 66 | |
341287bf | 67 | bool wxObject::IsKindOf(wxClassInfo *info) const |
c801d85f | 68 | { |
afb74891 | 69 | wxClassInfo *thisInfo = GetClassInfo(); |
0b9ab0bd | 70 | return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ; |
c801d85f KB |
71 | } |
72 | ||
38830220 | 73 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
dd107c50 | 74 | void wxObject::Dump(wxSTD ostream& str) |
c801d85f | 75 | { |
afb74891 VZ |
76 | if (GetClassInfo() && GetClassInfo()->GetClassName()) |
77 | str << GetClassInfo()->GetClassName(); | |
78 | else | |
79 | str << "unknown object class"; | |
c801d85f KB |
80 | } |
81 | #endif | |
82 | ||
ea57084d | 83 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING |
c801d85f KB |
84 | |
85 | #ifdef new | |
86 | #undef new | |
87 | #endif | |
88 | ||
0b9ab0bd | 89 | void *wxObject::operator new (size_t size, wxChar *fileName, int lineNum) |
c801d85f | 90 | { |
afb74891 | 91 | return wxDebugAlloc(size, fileName, lineNum, TRUE); |
c801d85f KB |
92 | } |
93 | ||
0b9ab0bd RL |
94 | #ifndef __VISAGECPP__ |
95 | void wxObject::operator delete (void *buf) | |
415ca026 DW |
96 | { |
97 | wxDebugFree(buf); | |
98 | } | |
0b9ab0bd RL |
99 | #elif __DEBUG_ALLOC__ |
100 | void wxObject::operator delete (void *buf, const char *_fname, size_t _line) | |
c801d85f | 101 | { |
afb74891 | 102 | wxDebugFree(buf); |
c801d85f | 103 | } |
0b9ab0bd RL |
104 | #endif |
105 | ||
106 | // VC++ 6.0 | |
c801d85f | 107 | |
3f4a0c5b | 108 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
0b9ab0bd | 109 | void wxObject::operator delete(void *pData, wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum)) |
76626af2 | 110 | { |
afb74891 | 111 | ::operator delete(pData); |
76626af2 JS |
112 | } |
113 | #endif | |
114 | ||
0b9ab0bd RL |
115 | // Cause problems for VC++ - crashes |
116 | ||
7c74e7fe | 117 | #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__) |
0b9ab0bd | 118 | void *wxObject::operator new[] (size_t size, wxChar *fileName, int lineNum) |
c801d85f | 119 | { |
afb74891 | 120 | return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE); |
c801d85f KB |
121 | } |
122 | ||
0b9ab0bd | 123 | void wxObject::operator delete[] (void *buf) |
c801d85f | 124 | { |
afb74891 | 125 | wxDebugFree(buf, TRUE); |
c801d85f KB |
126 | } |
127 | #endif | |
128 | ||
0b9ab0bd | 129 | #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING |
afb74891 | 130 | |
afb74891 | 131 | |
0b9ab0bd RL |
132 | // ---------------------------------------------------------------------------- |
133 | // wxClassInfo | |
134 | // ---------------------------------------------------------------------------- | |
c801d85f | 135 | |
0b9ab0bd | 136 | wxClassInfo *wxClassInfo::FindClass(const wxChar *className) |
c801d85f | 137 | { |
80e8062f VZ |
138 | if ( sm_classTable ) |
139 | { | |
140 | return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className); | |
141 | } | |
142 | else | |
143 | { | |
144 | for ( wxClassInfo *info = sm_first; info ; info = info->m_next ) | |
145 | { | |
146 | if ( wxStrcmp(info->GetClassName(), className) == 0 ) | |
147 | return info; | |
148 | } | |
149 | ||
150 | return NULL; | |
151 | } | |
c801d85f KB |
152 | } |
153 | ||
0b9ab0bd | 154 | // Set pointers to base class(es) to speed up IsKindOf |
c801d85f | 155 | |
afb74891 | 156 | void wxClassInfo::InitializeClasses() |
c801d85f | 157 | { |
309689b2 VZ |
158 | // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you |
159 | // link any object module twice mistakenly) will break this function | |
160 | // because it will enter an infinite loop and eventually die with "out of | |
161 | // memory" - as this is quite hard to detect if you're unaware of this, | |
162 | // try to do some checks here | |
0b9ab0bd | 163 | |
309689b2 | 164 | #ifdef __WXDEBUG__ |
0b9ab0bd | 165 | static const size_t nMaxClasses = 10000; // more than we'll ever have |
309689b2 | 166 | size_t nClass = 0; |
0b9ab0bd | 167 | #endif |
309689b2 | 168 | |
afb74891 VZ |
169 | wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING); |
170 | ||
0b9ab0bd RL |
171 | // Index all class infos by their class name |
172 | ||
173 | wxClassInfo *info; | |
174 | for(info = sm_first; info; info = info->m_next) | |
afb74891 VZ |
175 | { |
176 | if (info->m_className) | |
309689b2 VZ |
177 | { |
178 | wxASSERT_MSG( ++nClass < nMaxClasses, | |
f6bcfd97 | 179 | _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") ); |
afb74891 | 180 | sm_classTable->Put(info->m_className, (wxObject *)info); |
309689b2 | 181 | } |
afb74891 VZ |
182 | } |
183 | ||
0b9ab0bd RL |
184 | // Set base pointers for each wxClassInfo |
185 | ||
186 | for(info = sm_first; info; info = info->m_next) | |
afb74891 VZ |
187 | { |
188 | if (info->GetBaseClassName1()) | |
189 | info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1()); | |
190 | if (info->GetBaseClassName2()) | |
191 | info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2()); | |
afb74891 | 192 | } |
c801d85f KB |
193 | } |
194 | ||
afb74891 | 195 | void wxClassInfo::CleanUpClasses() |
c801d85f | 196 | { |
0c32066b | 197 | delete wxClassInfo::sm_classTable; |
0b9ab0bd | 198 | wxClassInfo::sm_classTable = 0; |
0c32066b | 199 | } |
f4a8c29f | 200 | |
0b9ab0bd | 201 | |
cf2f341a | 202 | wxObject *wxCreateDynamicObject(const wxChar *name) |
0c32066b | 203 | { |
bbee61e5 | 204 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
415ca026 | 205 | DEBUG_PRINTF(wxObject *wxCreateDynamicObject) |
bbee61e5 | 206 | #endif |
61cca9d2 | 207 | |
0c32066b JS |
208 | if (wxClassInfo::sm_classTable) |
209 | { | |
210 | wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name); | |
0b9ab0bd | 211 | return info != 0 ? info->CreateObject() : 0; |
0c32066b JS |
212 | } |
213 | else | |
214 | { | |
0b9ab0bd | 215 | for(wxClassInfo *info = wxClassInfo::sm_first; info; info = info->m_next) |
cf2f341a | 216 | if (info->m_className && wxStrcmp(info->m_className, name) == 0) |
0c32066b | 217 | return info->CreateObject(); |
0b9ab0bd | 218 | return 0; |
0c32066b | 219 | } |
c801d85f KB |
220 | } |
221 | ||
7a4b9130 | 222 | |
0b9ab0bd RL |
223 | // ---------------------------------------------------------------------------- |
224 | // wxClassInfo | |
225 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
226 | |
227 | void wxObject::Ref(const wxObject& clone) | |
228 | { | |
bbee61e5 | 229 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
415ca026 | 230 | DEBUG_PRINTF(wxObject::Ref) |
bbee61e5 | 231 | #endif |
0b9ab0bd RL |
232 | |
233 | // delete reference to old data | |
c801d85f | 234 | UnRef(); |
0b9ab0bd | 235 | |
c801d85f | 236 | // reference new data |
0b9ab0bd RL |
237 | if( clone.m_refData ) |
238 | { | |
c801d85f KB |
239 | m_refData = clone.m_refData; |
240 | ++(m_refData->m_count); | |
241 | } | |
242 | } | |
243 | ||
afb74891 | 244 | void wxObject::UnRef() |
c801d85f | 245 | { |
0b9ab0bd | 246 | if( m_refData ) |
4fe5383d VZ |
247 | { |
248 | wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") ); | |
249 | ||
250 | if ( !--m_refData->m_count ) | |
c801d85f | 251 | delete m_refData; |
0b9ab0bd | 252 | m_refData = 0; |
c801d85f | 253 | } |
c801d85f KB |
254 | } |
255 | ||
ce28b4d7 GD |
256 | |
257 | #if defined(__DARWIN__) && defined(DYLIB_INIT) | |
258 | ||
259 | extern "C" { | |
260 | void __initialize_Cplusplus(void); | |
261 | void wxWindowsDylibInit(void); | |
262 | }; | |
263 | ||
0b9ab0bd RL |
264 | // Dynamic shared library (dylib) initialization routine |
265 | // required to initialize static C++ objects bacause of lazy dynamic linking | |
266 | // http://developer.apple.com/techpubs/macosx/Essentials/ | |
267 | // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html | |
268 | ||
ce28b4d7 GD |
269 | void wxWindowsDylibInit() |
270 | { | |
271 | // The function __initialize_Cplusplus() must be called from the shared | |
272 | // library initialization routine to cause the static C++ objects in | |
273 | // the library to be initialized (reference number 2441683). | |
274 | ||
275 | __initialize_Cplusplus(); | |
276 | } | |
277 | ||
278 | #endif | |
0b9ab0bd RL |
279 | |
280 | // vi:sts=4:sw=4:et |