added support for gcc precompiled headers
[wxWidgets.git] / src / common / xti.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/xti.cpp
3 // Purpose: runtime metadata information (extended class info
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 27/07/03
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "xti.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/hash.h"
26 #include "wx/object.h"
27 #endif
28
29 #include "wx/xml/xml.h"
30 #include "wx/tokenzr.h"
31
32 #include <string.h>
33
34 #if wxUSE_EXTENDED_RTTI
35
36 // ----------------------------------------------------------------------------
37 // Enum Support
38 // ----------------------------------------------------------------------------
39
40 wxEnumData::wxEnumData( wxEnumMemberData* data )
41 {
42 m_members = data ;
43 for ( m_count = 0; m_members[m_count].m_name ; m_count++)
44 {} ;
45 }
46
47 bool wxEnumData::HasEnumMemberValue(const wxChar *name, int *value)
48 {
49 int i;
50 for (i = 0; m_members[i].m_name ; i++ )
51 {
52 if (!strcmp(name, m_members[i].m_name))
53 {
54 if ( value )
55 *value = m_members[i].m_value;
56 return true ;
57 }
58 }
59 return false ;
60 }
61
62 int wxEnumData::GetEnumMemberValue(const wxChar *name)
63 {
64 int i;
65 for (i = 0; m_members[i].m_name ; i++ )
66 {
67 if (!strcmp(name, m_members[i].m_name))
68 {
69 return m_members[i].m_value;
70 }
71 }
72 return 0 ;
73 }
74
75 const wxChar *wxEnumData::GetEnumMemberName(int value)
76 {
77 int i;
78 for (i = 0; m_members[i].m_name ; i++)
79 if (value == m_members[i].m_value)
80 return m_members[i].m_name;
81
82 return wxT("") ;
83 }
84
85 int wxEnumData::GetEnumMemberValueByIndex( int idx )
86 {
87 // we should cache the count in order to avoid out-of-bounds errors
88 return m_members[idx].m_value ;
89 }
90
91 const char * wxEnumData::GetEnumMemberNameByIndex( int idx )
92 {
93 // we should cache the count in order to avoid out-of-bounds errors
94 return m_members[idx].m_name ;
95 }
96
97 // ----------------------------------------------------------------------------
98 // Type Information
99 // ----------------------------------------------------------------------------
100
101 template<> const wxTypeInfo* wxGetTypeInfo( void * )
102 {
103 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
104 return &s_typeInfo ;
105 }
106
107 template<> const wxTypeInfo* wxGetTypeInfo( bool * )
108 {
109 static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL ) ;
110 return &s_typeInfo ;
111 }
112
113 template<> const wxTypeInfo* wxGetTypeInfo( char * )
114 {
115 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
116 return &s_typeInfo ;
117 }
118
119 template<> const wxTypeInfo* wxGetTypeInfo( unsigned char * )
120 {
121 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
122 return &s_typeInfo ;
123 }
124
125 template<> const wxTypeInfo* wxGetTypeInfo( int * )
126 {
127 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
128 return &s_typeInfo ;
129 }
130
131 template<> const wxTypeInfo* wxGetTypeInfo( unsigned int * )
132 {
133 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
134 return &s_typeInfo ;
135 }
136
137 template<> const wxTypeInfo* wxGetTypeInfo( long * )
138 {
139 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
140 return &s_typeInfo ;
141 }
142
143 template<> const wxTypeInfo* wxGetTypeInfo( unsigned long * )
144 {
145 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
146 return &s_typeInfo ;
147 }
148
149 template<> const wxTypeInfo* wxGetTypeInfo( float * )
150 {
151 static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT ) ;
152 return &s_typeInfo ;
153 }
154
155 template<> const wxTypeInfo* wxGetTypeInfo( double * )
156 {
157 static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE ) ;
158 return &s_typeInfo ;
159 }
160
161 template<> const wxTypeInfo* wxGetTypeInfo( wxString * )
162 {
163 static wxBuiltInTypeInfo s_typeInfo( wxT_STRING ) ;
164 return &s_typeInfo ;
165 }
166
167 // this are compiler induced specialization which are never used anywhere
168
169 WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
170 WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
171 WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
172 WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
173 WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
174 WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
175 WX_ILLEGAL_TYPE_SPECIALIZATION( wxString * )
176
177 // ----------------------------------------------------------------------------
178 // value streaming
179 // ----------------------------------------------------------------------------
180
181 // streamer specializations
182 // for all built-in types
183
184 // bool
185
186 template<> void wxStringReadValue(const wxString &s , bool &data )
187 {
188 int intdata ;
189 wxSscanf(s, _T("%d"), &intdata ) ;
190 data = bool(intdata) ;
191 }
192
193 template<> void wxStringWriteValue(wxString &s , const bool &data )
194 {
195 s = wxString::Format("%d", data ) ;
196 }
197
198 // char
199
200 template<> void wxStringReadValue(const wxString &s , char &data )
201 {
202 int intdata ;
203 wxSscanf(s, _T("%d"), &intdata ) ;
204 data = char(intdata) ;
205 }
206
207 template<> void wxStringWriteValue(wxString &s , const char &data )
208 {
209 s = wxString::Format("%d", data ) ;
210 }
211
212 // unsigned char
213
214 template<> void wxStringReadValue(const wxString &s , unsigned char &data )
215 {
216 int intdata ;
217 wxSscanf(s, _T("%d"), &intdata ) ;
218 data = (unsigned char)(intdata) ;
219 }
220
221 template<> void wxStringWriteValue(wxString &s , const unsigned char &data )
222 {
223 s = wxString::Format("%d", data ) ;
224 }
225
226 // int
227
228 template<> void wxStringReadValue(const wxString &s , int &data )
229 {
230 wxSscanf(s, _T("%d"), &data ) ;
231 }
232
233 template<> void wxStringWriteValue(wxString &s , const int &data )
234 {
235 s = wxString::Format("%d", data ) ;
236 }
237
238 // unsigned int
239
240 template<> void wxStringReadValue(const wxString &s , unsigned int &data )
241 {
242 wxSscanf(s, _T("%d"), &data ) ;
243 }
244
245 template<> void wxStringWriteValue(wxString &s , const unsigned int &data )
246 {
247 s = wxString::Format("%d", data ) ;
248 }
249
250 // long
251
252 template<> void wxStringReadValue(const wxString &s , long &data )
253 {
254 wxSscanf(s, _T("%ld"), &data ) ;
255 }
256
257 template<> void wxStringWriteValue(wxString &s , const long &data )
258 {
259 s = wxString::Format("%ld", data ) ;
260 }
261
262 // unsigned long
263
264 template<> void wxStringReadValue(const wxString &s , unsigned long &data )
265 {
266 wxSscanf(s, _T("%ld"), &data ) ;
267 }
268
269 template<> void wxStringWriteValue(wxString &s , const unsigned long &data )
270 {
271 s = wxString::Format("%ld", data ) ;
272 }
273
274 // float
275
276 template<> void wxStringReadValue(const wxString &s , float &data )
277 {
278 wxSscanf(s, _T("%f"), &data ) ;
279 }
280
281 template<> void wxStringWriteValue(wxString &s , const float &data )
282 {
283 s = wxString::Format("%f", data ) ;
284 }
285
286 // double
287
288 template<> void wxStringReadValue(const wxString &s , double &data )
289 {
290 wxSscanf(s, _T("%lf"), &data ) ;
291 }
292
293 template<> void wxStringWriteValue(wxString &s , const double &data )
294 {
295 s = wxString::Format("%lf", data ) ;
296 }
297
298 // wxString
299
300 template<> void wxStringReadValue(const wxString &s , wxString &data )
301 {
302 data = s ;
303 }
304
305 template<> void wxStringWriteValue(wxString &s , const wxString &data )
306 {
307 s = data ;
308 }
309
310 /*
311 Custom Data Streaming / Type Infos
312 we will have to add this for all wx non object types, but it is also an example
313 for custom data structures
314 */
315
316 // wxPoint
317
318 template<> void wxStringReadValue(const wxString &s , wxPoint &data )
319 {
320 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
321 }
322
323 template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
324 {
325 s = wxString::Format("%d,%d", data.x , data.y ) ;
326 }
327
328 template<> void wxStringReadValue(const wxString & , wxPoint* & )
329 {
330 assert(0) ;
331 }
332
333 template<> void wxStringWriteValue(wxString & , wxPoint* const & )
334 {
335 assert(0) ;
336 }
337
338 WX_CUSTOM_TYPE_INFO(wxPoint)
339
340 template<> void wxStringReadValue(const wxString &s , wxSize &data )
341 {
342 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
343 }
344
345 template<> void wxStringWriteValue(wxString &s , const wxSize &data )
346 {
347 s = wxString::Format("%d,%d", data.x , data.y ) ;
348 }
349
350 template<> void wxStringReadValue(const wxString & , wxSize* & )
351 {
352 assert(0) ;
353 }
354
355 template<> void wxStringWriteValue(wxString & , wxSize * const & )
356 {
357 assert(0) ;
358 }
359
360 WX_CUSTOM_TYPE_INFO(wxSize)
361
362 /*
363
364 template<> void wxStringReadValue(const wxString &s , wxColour &data )
365 {
366 // copied from VS xrc
367 unsigned long tmp = 0;
368
369 if (s.Length() != 7 || s[0u] != wxT('#') ||
370 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
371 {
372 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
373 s.c_str() );
374 data = wxNullColour;
375 }
376 else
377 {
378 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
379 (unsigned char) ((tmp & 0x00FF00) >> 8),
380 (unsigned char) ((tmp & 0x0000FF)));
381 }
382 }
383
384 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
385 {
386 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
387 }
388
389 WX_CUSTOM_TYPE_INFO(wxColour)
390
391 */
392
393 // removing header dependancy on string tokenizer
394
395 void wxSetStringToArray( const wxString &s , wxArrayString &array )
396 {
397 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
398 wxString flag;
399 array.Clear() ;
400 while (tokenizer.HasMoreTokens())
401 {
402 array.Add(tokenizer.GetNextToken()) ;
403 }
404 }
405
406 // ----------------------------------------------------------------------------
407 // wxClassInfo
408 // ----------------------------------------------------------------------------
409
410 const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName)
411 {
412 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
413
414 if ( info )
415 return info->GetAccessor() ;
416
417 return NULL ;
418 }
419
420 const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
421 {
422 const wxPropertyInfo* info = GetFirstProperty() ;
423
424 while( info )
425 {
426 if ( strcmp( info->GetName() , PropertyName ) == 0 )
427 return info ;
428 info = info->GetNext() ;
429 }
430
431 const wxClassInfo** parents = GetParents() ;
432 for ( int i = 0 ; parents[i] ; ++ i )
433 {
434 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
435 return info ;
436 }
437
438 return 0;
439 }
440
441 const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
442 {
443 const wxHandlerInfo* info = GetFirstHandler() ;
444
445 while( info )
446 {
447 if ( strcmp( info->GetName() , PropertyName ) == 0 )
448 return info ;
449 info = info->GetNext() ;
450 }
451
452 const wxClassInfo** parents = GetParents() ;
453 for ( int i = 0 ; parents[i] ; ++ i )
454 {
455 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
456 return info ;
457 }
458
459 return 0;
460 }
461
462
463 void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value)
464 {
465 const wxPropertyAccessor *accessor;
466
467 accessor = FindAccessor(propertyName);
468 wxASSERT(accessor->HasSetter());
469 accessor->SetProperty( object , value ) ;
470 }
471
472 wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName)
473 {
474 const wxPropertyAccessor *accessor;
475
476 accessor = FindAccessor(propertyName);
477 wxASSERT(accessor->HasGetter());
478 return accessor->GetProperty(object);
479 }
480
481 /*
482 VARIANT TO OBJECT
483 */
484
485 wxObject* wxxVariant::GetAsObject()
486 {
487 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
488 if ( ti )
489 return ti->GetClassInfo()->VariantToInstance(*this) ;
490 else
491 return NULL ;
492 }
493
494
495 #endif