initialize wxClassInfo::sm_classTable automatically
[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 #ifdef __GNUG__
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 // char const *
170
171 template<> const wxTypeInfo* wxGetTypeInfo( char const ** )
172 {
173 assert(0) ;
174 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
175 return &s_typeInfo ;
176 }
177
178 template<> void wxStringReadValue(const wxString & , const char* & )
179 {
180 assert(0) ;
181 }
182
183 template<> void wxStringWriteValue(wxString & , char const * const & )
184 {
185 assert(0) ;
186 }
187
188 // char *
189
190 template<> const wxTypeInfo* wxGetTypeInfo( char ** )
191 {
192 assert(0) ;
193 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
194 return &s_typeInfo ;
195 }
196
197 template<> void wxStringReadValue(const wxString & , char* & )
198 {
199 assert(0) ;
200 }
201
202 template<> void wxStringWriteValue(wxString & , char * const & )
203 {
204 assert(0) ;
205 }
206
207 // unsigned char *
208
209 template<> const wxTypeInfo* wxGetTypeInfo( unsigned char ** )
210 {
211 assert(0) ;
212 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
213 return &s_typeInfo ;
214 }
215
216 template<> void wxStringReadValue(const wxString & , unsigned char* & )
217 {
218 assert(0) ;
219 }
220
221 template<> void wxStringWriteValue(wxString & , unsigned char * const & )
222 {
223 assert(0) ;
224 }
225
226 // int *
227
228 template<> const wxTypeInfo* wxGetTypeInfo( int ** )
229 {
230 assert(0) ;
231 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
232 return &s_typeInfo ;
233 }
234
235 template<> void wxStringReadValue(const wxString & , int* & )
236 {
237 assert(0) ;
238 }
239
240 template<> void wxStringWriteValue(wxString & , int * const & )
241 {
242 assert(0) ;
243 }
244
245 // bool *
246
247 template<> const wxTypeInfo* wxGetTypeInfo( bool ** )
248 {
249 assert(0) ;
250 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
251 return &s_typeInfo ;
252 }
253
254 template<> void wxStringReadValue(const wxString & , bool* & )
255 {
256 assert(0) ;
257 }
258
259 template<> void wxStringWriteValue(wxString & , bool * const & )
260 {
261 assert(0) ;
262 }
263
264 // long *
265
266 template<> const wxTypeInfo* wxGetTypeInfo( long ** )
267 {
268 assert(0) ;
269 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
270 return &s_typeInfo ;
271 }
272
273 template<> void wxStringReadValue(const wxString & , long* & )
274 {
275 assert(0) ;
276 }
277
278 template<> void wxStringWriteValue(wxString & , long * const & )
279 {
280 assert(0) ;
281 }
282
283 // wxString *
284
285 template<> const wxTypeInfo* wxGetTypeInfo( wxString ** )
286 {
287 assert(0) ;
288 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
289 return &s_typeInfo ;
290 }
291
292 template<> void wxStringReadValue(const wxString & , wxString* & )
293 {
294 assert(0) ;
295 }
296
297 template<> void wxStringWriteValue(wxString & , wxString * const & )
298 {
299 assert(0) ;
300 }
301
302
303 // ----------------------------------------------------------------------------
304 // value streaming
305 // ----------------------------------------------------------------------------
306
307 // convenience function (avoids including xml headers in users code)
308
309 void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data )
310 {
311 node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "value", data ) );
312 }
313
314 wxString wxXmlGetContentFromNode( wxXmlNode *node )
315 {
316 if ( node->GetChildren() )
317 return node->GetChildren()->GetContent() ;
318 else
319 return wxEmptyString ;
320 }
321
322 // streamer specializations
323 // for all built-in types
324
325 // bool
326
327 template<> void wxStringReadValue(const wxString &s , bool &data )
328 {
329 int intdata ;
330 wxSscanf(s, _T("%d"), &intdata ) ;
331 data = bool(intdata) ;
332 }
333
334 template<> void wxStringWriteValue(wxString &s , const bool &data )
335 {
336 s = wxString::Format("%d", data ) ;
337 }
338
339 // char
340
341 template<> void wxStringReadValue(const wxString &s , char &data )
342 {
343 int intdata ;
344 wxSscanf(s, _T("%d"), &intdata ) ;
345 data = char(intdata) ;
346 }
347
348 template<> void wxStringWriteValue(wxString &s , const char &data )
349 {
350 s = wxString::Format("%d", data ) ;
351 }
352
353 // unsigned char
354
355 template<> void wxStringReadValue(const wxString &s , unsigned char &data )
356 {
357 int intdata ;
358 wxSscanf(s, _T("%d"), &intdata ) ;
359 data = (unsigned char)(intdata) ;
360 }
361
362 template<> void wxStringWriteValue(wxString &s , const unsigned char &data )
363 {
364 s = wxString::Format("%d", data ) ;
365 }
366
367 // int
368
369 template<> void wxStringReadValue(const wxString &s , int &data )
370 {
371 wxSscanf(s, _T("%d"), &data ) ;
372 }
373
374 template<> void wxStringWriteValue(wxString &s , const int &data )
375 {
376 s = wxString::Format("%d", data ) ;
377 }
378
379 // unsigned int
380
381 template<> void wxStringReadValue(const wxString &s , unsigned int &data )
382 {
383 wxSscanf(s, _T("%d"), &data ) ;
384 }
385
386 template<> void wxStringWriteValue(wxString &s , const unsigned int &data )
387 {
388 s = wxString::Format("%d", data ) ;
389 }
390
391 // long
392
393 template<> void wxStringReadValue(const wxString &s , long &data )
394 {
395 wxSscanf(s, _T("%ld"), &data ) ;
396 }
397
398 template<> void wxStringWriteValue(wxString &s , const long &data )
399 {
400 s = wxString::Format("%ld", data ) ;
401 }
402
403 // unsigned long
404
405 template<> void wxStringReadValue(const wxString &s , unsigned long &data )
406 {
407 wxSscanf(s, _T("%ld"), &data ) ;
408 }
409
410 template<> void wxStringWriteValue(wxString &s , const unsigned long &data )
411 {
412 s = wxString::Format("%ld", data ) ;
413 }
414
415 // float
416
417 template<> void wxStringReadValue(const wxString &s , float &data )
418 {
419 wxSscanf(s, _T("%f"), &data ) ;
420 }
421
422 template<> void wxStringWriteValue(wxString &s , const float &data )
423 {
424 s = wxString::Format("%f", data ) ;
425 }
426
427 // double
428
429 template<> void wxStringReadValue(const wxString &s , double &data )
430 {
431 wxSscanf(s, _T("%lf"), &data ) ;
432 }
433
434 template<> void wxStringWriteValue(wxString &s , const double &data )
435 {
436 s = wxString::Format("%lf", data ) ;
437 }
438
439 // wxString
440
441 template<> void wxStringReadValue(const wxString &s , wxString &data )
442 {
443 data = s ;
444 }
445
446 template<> void wxStringWriteValue(wxString &s , const wxString &data )
447 {
448 s = data ;
449 }
450
451 /*
452 Custom Data Streaming / Type Infos
453 we will have to add this for all wx non object types, but it is also an example
454 for custom data structures
455 */
456
457 // wxPoint
458
459 template<> void wxStringReadValue(const wxString &s , wxPoint &data )
460 {
461 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
462 }
463
464 template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
465 {
466 s = wxString::Format("%d,%d", data.x , data.y ) ;
467 }
468
469 template<> void wxStringReadValue(const wxString & , wxPoint* & )
470 {
471 assert(0) ;
472 }
473
474 template<> void wxStringWriteValue(wxString & , wxPoint* const & )
475 {
476 assert(0) ;
477 }
478
479 WX_CUSTOM_TYPE_INFO(wxPoint)
480
481 template<> void wxStringReadValue(const wxString &s , wxSize &data )
482 {
483 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
484 }
485
486 template<> void wxStringWriteValue(wxString &s , const wxSize &data )
487 {
488 s = wxString::Format("%d,%d", data.x , data.y ) ;
489 }
490
491 template<> void wxStringReadValue(const wxString & , wxSize* & )
492 {
493 assert(0) ;
494 }
495
496 template<> void wxStringWriteValue(wxString & , wxSize * const & )
497 {
498 assert(0) ;
499 }
500
501 WX_CUSTOM_TYPE_INFO(wxSize)
502
503 /*
504
505 template<> void wxStringReadValue(const wxString &s , wxColour &data )
506 {
507 // copied from VS xrc
508 unsigned long tmp = 0;
509
510 if (s.Length() != 7 || s[0u] != wxT('#') ||
511 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
512 {
513 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
514 s.c_str() );
515 data = wxNullColour;
516 }
517 else
518 {
519 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
520 (unsigned char) ((tmp & 0x00FF00) >> 8),
521 (unsigned char) ((tmp & 0x0000FF)));
522 }
523 }
524
525 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
526 {
527 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
528 }
529
530 WX_CUSTOM_TYPE_INFO(wxColour)
531
532 */
533
534 // removing header dependancy on string tokenizer
535
536 void wxSetStringToArray( const wxString &s , wxArrayString &array )
537 {
538 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
539 wxString flag;
540 array.Clear() ;
541 while (tokenizer.HasMoreTokens())
542 {
543 array.Add(tokenizer.GetNextToken()) ;
544 }
545 }
546
547 // ----------------------------------------------------------------------------
548 // wxClassInfo
549 // ----------------------------------------------------------------------------
550
551 const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName)
552 {
553 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
554
555 if ( info )
556 return info->GetAccessor() ;
557
558 return NULL ;
559 }
560
561 const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
562 {
563 const wxPropertyInfo* info = GetFirstProperty() ;
564
565 while( info )
566 {
567 if ( strcmp( info->GetName() , PropertyName ) == 0 )
568 return info ;
569 info = info->GetNext() ;
570 }
571
572 const wxClassInfo** parents = GetParents() ;
573 for ( int i = 0 ; parents[i] ; ++ i )
574 {
575 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
576 return info ;
577 }
578
579 return 0;
580 }
581
582 const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
583 {
584 const wxHandlerInfo* info = GetFirstHandler() ;
585
586 while( info )
587 {
588 if ( strcmp( info->GetName() , PropertyName ) == 0 )
589 return info ;
590 info = info->GetNext() ;
591 }
592
593 const wxClassInfo** parents = GetParents() ;
594 for ( int i = 0 ; parents[i] ; ++ i )
595 {
596 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
597 return info ;
598 }
599
600 return 0;
601 }
602
603
604 void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value)
605 {
606 const wxPropertyAccessor *accessor;
607
608 accessor = FindAccessor(propertyName);
609 wxASSERT(accessor->HasSetter());
610 accessor->SetProperty( object , value ) ;
611 }
612
613 wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName)
614 {
615 const wxPropertyAccessor *accessor;
616
617 accessor = FindAccessor(propertyName);
618 wxASSERT(accessor->HasGetter());
619 return accessor->GetProperty(object);
620 }
621
622 /*
623 VARIANT TO OBJECT
624 */
625
626 wxObject* wxxVariant::GetAsObject()
627 {
628 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
629 if ( ti )
630 return ti->GetClassInfo()->VariantToInstance(*this) ;
631 else
632 return NULL ;
633 }
634
635
636 #endif