xti additions / changes, trying to reduce dependencies
[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 #include "wx/list.h"
32 #include <string.h>
33
34 #if wxUSE_EXTENDED_RTTI
35
36 #include "wx/beforestd.h"
37 #include <map>
38 #include <string>
39 #include "wx/afterstd.h"
40
41 using namespace std ;
42
43 // ----------------------------------------------------------------------------
44 // Enum Support
45 // ----------------------------------------------------------------------------
46
47 wxEnumData::wxEnumData( wxEnumMemberData* data )
48 {
49 m_members = data ;
50 for ( m_count = 0; m_members[m_count].m_name ; m_count++)
51 {} ;
52 }
53
54 bool wxEnumData::HasEnumMemberValue(const wxChar *name, int *value)
55 {
56 int i;
57 for (i = 0; m_members[i].m_name ; i++ )
58 {
59 if (!strcmp(name, m_members[i].m_name))
60 {
61 if ( value )
62 *value = m_members[i].m_value;
63 return true ;
64 }
65 }
66 return false ;
67 }
68
69 int wxEnumData::GetEnumMemberValue(const wxChar *name)
70 {
71 int i;
72 for (i = 0; m_members[i].m_name ; i++ )
73 {
74 if (!strcmp(name, m_members[i].m_name))
75 {
76 return m_members[i].m_value;
77 }
78 }
79 return 0 ;
80 }
81
82 const wxChar *wxEnumData::GetEnumMemberName(int value)
83 {
84 int i;
85 for (i = 0; m_members[i].m_name ; i++)
86 if (value == m_members[i].m_value)
87 return m_members[i].m_name;
88
89 return wxT("") ;
90 }
91
92 int wxEnumData::GetEnumMemberValueByIndex( int idx )
93 {
94 // we should cache the count in order to avoid out-of-bounds errors
95 return m_members[idx].m_value ;
96 }
97
98 const char * wxEnumData::GetEnumMemberNameByIndex( int idx )
99 {
100 // we should cache the count in order to avoid out-of-bounds errors
101 return m_members[idx].m_name ;
102 }
103
104 // ----------------------------------------------------------------------------
105 // Type Information
106 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
108 // value streaming
109 // ----------------------------------------------------------------------------
110
111 // streamer specializations
112 // for all built-in types
113
114 // bool
115
116 template<> void wxStringReadValue(const wxString &s , bool &data )
117 {
118 int intdata ;
119 wxSscanf(s, _T("%d"), &intdata ) ;
120 data = bool(intdata) ;
121 }
122
123 template<> void wxStringWriteValue(wxString &s , const bool &data )
124 {
125 s = wxString::Format("%d", data ) ;
126 }
127
128 // char
129
130 template<> void wxStringReadValue(const wxString &s , char &data )
131 {
132 int intdata ;
133 wxSscanf(s, _T("%d"), &intdata ) ;
134 data = char(intdata) ;
135 }
136
137 template<> void wxStringWriteValue(wxString &s , const char &data )
138 {
139 s = wxString::Format("%d", data ) ;
140 }
141
142 // unsigned char
143
144 template<> void wxStringReadValue(const wxString &s , unsigned char &data )
145 {
146 int intdata ;
147 wxSscanf(s, _T("%d"), &intdata ) ;
148 data = (unsigned char)(intdata) ;
149 }
150
151 template<> void wxStringWriteValue(wxString &s , const unsigned char &data )
152 {
153 s = wxString::Format("%d", data ) ;
154 }
155
156 // int
157
158 template<> void wxStringReadValue(const wxString &s , int &data )
159 {
160 wxSscanf(s, _T("%d"), &data ) ;
161 }
162
163 template<> void wxStringWriteValue(wxString &s , const int &data )
164 {
165 s = wxString::Format("%d", data ) ;
166 }
167
168 // unsigned int
169
170 template<> void wxStringReadValue(const wxString &s , unsigned int &data )
171 {
172 wxSscanf(s, _T("%d"), &data ) ;
173 }
174
175 template<> void wxStringWriteValue(wxString &s , const unsigned int &data )
176 {
177 s = wxString::Format("%d", data ) ;
178 }
179
180 // long
181
182 template<> void wxStringReadValue(const wxString &s , long &data )
183 {
184 wxSscanf(s, _T("%ld"), &data ) ;
185 }
186
187 template<> void wxStringWriteValue(wxString &s , const long &data )
188 {
189 s = wxString::Format("%ld", data ) ;
190 }
191
192 // unsigned long
193
194 template<> void wxStringReadValue(const wxString &s , unsigned long &data )
195 {
196 wxSscanf(s, _T("%ld"), &data ) ;
197 }
198
199 template<> void wxStringWriteValue(wxString &s , const unsigned long &data )
200 {
201 s = wxString::Format("%ld", data ) ;
202 }
203
204 // float
205
206 template<> void wxStringReadValue(const wxString &s , float &data )
207 {
208 wxSscanf(s, _T("%f"), &data ) ;
209 }
210
211 template<> void wxStringWriteValue(wxString &s , const float &data )
212 {
213 s = wxString::Format("%f", data ) ;
214 }
215
216 // double
217
218 template<> void wxStringReadValue(const wxString &s , double &data )
219 {
220 wxSscanf(s, _T("%lf"), &data ) ;
221 }
222
223 template<> void wxStringWriteValue(wxString &s , const double &data )
224 {
225 s = wxString::Format("%lf", data ) ;
226 }
227
228 // wxString
229
230 template<> void wxStringReadValue(const wxString &s , wxString &data )
231 {
232 data = s ;
233 }
234
235 template<> void wxStringWriteValue(wxString &s , const wxString &data )
236 {
237 s = data ;
238 }
239
240 // built-ins
241 //
242
243 template<> const wxTypeInfo* wxGetTypeInfo( void * )
244 {
245 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
246 return &s_typeInfo ;
247 }
248
249 template<> const wxTypeInfo* wxGetTypeInfo( bool * )
250 {
251 static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL , &wxToStringConverter<bool> , &wxFromStringConverter<bool>) ;
252 return &s_typeInfo ;
253 }
254
255 template<> const wxTypeInfo* wxGetTypeInfo( char * )
256 {
257 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR , &wxToStringConverter<char> , &wxFromStringConverter<char>) ;
258 return &s_typeInfo ;
259 }
260
261 template<> const wxTypeInfo* wxGetTypeInfo( unsigned char * )
262 {
263 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR , &wxToStringConverter< unsigned char > , &wxFromStringConverter<unsigned char>) ;
264 return &s_typeInfo ;
265 }
266
267 template<> const wxTypeInfo* wxGetTypeInfo( int * )
268 {
269 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR , &wxToStringConverter<int> , &wxFromStringConverter<int>) ;
270 return &s_typeInfo ;
271 }
272
273 template<> const wxTypeInfo* wxGetTypeInfo( unsigned int * )
274 {
275 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR , &wxToStringConverter<unsigned int> , &wxFromStringConverter<unsigned int>) ;
276 return &s_typeInfo ;
277 }
278
279 template<> const wxTypeInfo* wxGetTypeInfo( long * )
280 {
281 static wxBuiltInTypeInfo s_typeInfo( wxT_LONG , &wxToStringConverter<long> , &wxFromStringConverter<long>) ;
282 return &s_typeInfo ;
283 }
284
285 template<> const wxTypeInfo* wxGetTypeInfo( unsigned long * )
286 {
287 static wxBuiltInTypeInfo s_typeInfo( wxT_ULONG , &wxToStringConverter<unsigned long> , &wxFromStringConverter<unsigned long>) ;
288 return &s_typeInfo ;
289 }
290
291 template<> const wxTypeInfo* wxGetTypeInfo( float * )
292 {
293 static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT , &wxToStringConverter<float> , &wxFromStringConverter<float>) ;
294 return &s_typeInfo ;
295 }
296
297 template<> const wxTypeInfo* wxGetTypeInfo( double * )
298 {
299 static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE , &wxToStringConverter<double> , &wxFromStringConverter<double>) ;
300 return &s_typeInfo ;
301 }
302
303 template<> const wxTypeInfo* wxGetTypeInfo( wxString * )
304 {
305 static wxBuiltInTypeInfo s_typeInfo( wxT_STRING , &wxToStringConverter<wxString> , &wxFromStringConverter<wxString>) ;
306 return &s_typeInfo ;
307 }
308
309 // this are compiler induced specialization which are never used anywhere
310
311 WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
312 WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
313 WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
314 WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
315 WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
316 WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
317 WX_ILLEGAL_TYPE_SPECIALIZATION( wxString * )
318
319 //
320
321 // make wxWindowList known
322
323 WX_COLLECTION_TYPE_INFO( wxString , wxArrayString ) ;
324
325 template<> void wxCollectionToVariantArray( wxArrayString const &theArray, wxxVariantArray &value)
326 {
327 wxArrayCollectionToVariantArray( theArray , value ) ;
328 }
329
330 wxTypeInfoMap *wxTypeInfo::sm_typeTable = NULL ;
331
332 wxTypeInfo *wxTypeInfo::FindType(const wxChar *typeName)
333 {
334 return (wxTypeInfo *)sm_typeTable->find(typeName)->second;
335 }
336
337 wxClassTypeInfo::wxClassTypeInfo( wxTypeKind kind , wxClassInfo* classInfo , converterToString_t to , converterFromString_t from ) :
338 wxTypeInfo( kind , to , from , classInfo->GetClassName() )
339 { wxASSERT_MSG( kind == wxT_OBJECT_PTR || kind == wxT_OBJECT , wxT("Illegal Kind for Enum Type")) ; m_classInfo = classInfo ;}
340
341 wxDelegateTypeInfo::wxDelegateTypeInfo( int eventType , wxClassInfo* eventClass , converterToString_t to , converterFromString_t from ) :
342 wxTypeInfo ( wxT_DELEGATE , to , from , wxEmptyString )
343 { m_eventClass = eventClass ; m_eventType = eventType ;}
344
345 void wxTypeInfo::Register()
346 {
347 if ( sm_typeTable == NULL )
348 sm_typeTable = new wxTypeInfoMap() ;
349
350 if( !m_name.IsEmpty() )
351 (*sm_typeTable)[m_name] = this ;
352 }
353
354 void wxTypeInfo::Unregister()
355 {
356 if( !m_name.IsEmpty() )
357 sm_typeTable->erase(m_name);
358 }
359
360 // removing header dependancy on string tokenizer
361
362 void wxSetStringToArray( const wxString &s , wxArrayString &array )
363 {
364 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
365 wxString flag;
366 array.Clear() ;
367 while (tokenizer.HasMoreTokens())
368 {
369 array.Add(tokenizer.GetNextToken()) ;
370 }
371 }
372
373 // ----------------------------------------------------------------------------
374 // wxClassInfo
375 // ----------------------------------------------------------------------------
376
377 const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName) const
378 {
379 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
380
381 if ( info )
382 return info->GetAccessor() ;
383
384 return NULL ;
385 }
386
387 const wxPropertyInfo *wxClassInfo::FindPropertyInfoInThisClass (const char *PropertyName) const
388 {
389 const wxPropertyInfo* info = GetFirstProperty() ;
390
391 while( info )
392 {
393 if ( strcmp( info->GetName() , PropertyName ) == 0 )
394 return info ;
395 info = info->GetNext() ;
396 }
397
398 return 0;
399 }
400
401 const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
402 {
403 const wxPropertyInfo* info = FindPropertyInfoInThisClass( PropertyName ) ;
404 if ( info )
405 return info ;
406
407 const wxClassInfo** parents = GetParents() ;
408 for ( int i = 0 ; parents[i] ; ++ i )
409 {
410 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
411 return info ;
412 }
413
414 return 0;
415 }
416
417 const wxHandlerInfo *wxClassInfo::FindHandlerInfoInThisClass (const char *PropertyName) const
418 {
419 const wxHandlerInfo* info = GetFirstHandler() ;
420
421 while( info )
422 {
423 if ( strcmp( info->GetName() , PropertyName ) == 0 )
424 return info ;
425 info = info->GetNext() ;
426 }
427
428 return 0;
429 }
430
431 const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
432 {
433 const wxHandlerInfo* info = FindHandlerInfoInThisClass( PropertyName ) ;
434
435 if ( info )
436 return info ;
437
438 const wxClassInfo** parents = GetParents() ;
439 for ( int i = 0 ; parents[i] ; ++ i )
440 {
441 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
442 return info ;
443 }
444
445 return 0;
446 }
447
448
449 void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value) const
450 {
451 const wxPropertyAccessor *accessor;
452
453 accessor = FindAccessor(propertyName);
454 wxASSERT(accessor->HasSetter());
455 accessor->SetProperty( object , value ) ;
456 }
457
458 wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName) const
459 {
460 const wxPropertyAccessor *accessor;
461
462 accessor = FindAccessor(propertyName);
463 wxASSERT(accessor->HasGetter());
464 wxxVariant result ;
465 accessor->GetProperty(object,result);
466 return result ;
467 }
468
469 wxxVariantArray wxClassInfo::GetPropertyCollection(wxObject *object, const wxChar *propertyName) const
470 {
471 const wxPropertyAccessor *accessor;
472
473 accessor = FindAccessor(propertyName);
474 wxASSERT(accessor->HasGetter());
475 wxxVariantArray result ;
476 accessor->GetPropertyCollection(object,result);
477 return result ;
478 }
479
480 void wxClassInfo::AddToPropertyCollection(wxObject *object, const wxChar *propertyName , const wxxVariant& value) const
481 {
482 const wxPropertyAccessor *accessor;
483
484 accessor = FindAccessor(propertyName);
485 wxASSERT(accessor->HasAdder());
486 accessor->AddToPropertyCollection( object , value ) ;
487 }
488
489 void wxClassInfo::GetProperties( wxPropertyInfoMap &map ) const
490 {
491 const wxPropertyInfo *pi = GetFirstProperty() ;
492 while( pi )
493 {
494 if ( map.find( pi->GetName() ) == map.end() )
495 map[pi->GetName()] = (wxPropertyInfo*) pi ;
496
497 pi = pi->GetNext() ;
498 }
499
500 const wxClassInfo** parents = GetParents() ;
501 for ( int i = 0 ; parents[i] ; ++ i )
502 {
503 parents[i]->GetProperties( map ) ;
504 }
505 }
506
507 /*
508 VARIANT TO OBJECT
509 */
510
511 wxObject* wxxVariant::GetAsObject()
512 {
513 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
514 if ( ti )
515 return ti->GetClassInfo()->VariantToInstance(*this) ;
516 else
517 return NULL ;
518 }
519
520 // ----------------------------------------------------------------------------
521 // wxDynamicObject support
522 // ----------------------------------------------------------------------------
523 //
524 // Dynamic Objects are objects that have a real superclass instance and carry their
525 // own attributes in a hash map. Like this it is possible to create the objects and
526 // stream them, as if their class information was already available from compiled data
527
528 struct wxDynamicObject::wxDynamicObjectInternal
529 {
530 map<string,wxxVariant> m_properties ;
531 } ;
532
533 // instantiates this object with an instance of its superclass
534 wxDynamicObject::wxDynamicObject(wxObject* superClassInstance, const wxDynamicClassInfo *info)
535 {
536 m_superClassInstance = superClassInstance ;
537 m_classInfo = info ;
538 m_data = new wxDynamicObjectInternal ;
539 }
540
541 wxDynamicObject::~wxDynamicObject()
542 {
543 delete m_data ;
544 delete m_superClassInstance ;
545 }
546
547 void wxDynamicObject::SetProperty (const wxChar *propertyName, const wxxVariant &value)
548 {
549 wxASSERT_MSG(m_classInfo->FindPropertyInfoInThisClass(propertyName),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
550 m_data->m_properties[propertyName] = value ;
551 }
552
553 wxxVariant wxDynamicObject::GetProperty (const wxChar *propertyName) const
554 {
555 wxASSERT_MSG(m_classInfo->FindPropertyInfoInThisClass(propertyName),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
556 return m_data->m_properties[propertyName] ;
557 }
558
559 // ----------------------------------------------------------------------------
560 // wxDynamiClassInfo
561 // ----------------------------------------------------------------------------
562
563 wxDynamicClassInfo::wxDynamicClassInfo( const wxChar *unitName, const wxChar *className , const wxClassInfo* superClass ) :
564 wxClassInfo( unitName, className , new const wxClassInfo*[2])
565 {
566 GetParents()[0] = superClass ;
567 GetParents()[1] = NULL ;
568 }
569
570 wxDynamicClassInfo::~wxDynamicClassInfo()
571 {
572 delete[] GetParents() ;
573 }
574
575 wxObject *wxDynamicClassInfo::AllocateObject() const
576 {
577 wxObject* parent = GetParents()[0]->AllocateObject() ;
578 return new wxDynamicObject( parent , this ) ;
579 }
580
581 void wxDynamicClassInfo::Create (wxObject *object, int paramCount, wxxVariant *params) const
582 {
583 wxDynamicObject *dynobj = dynamic_cast< wxDynamicObject *>( object ) ;
584 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::Create on an object other than wxDynamicObject") ) ;
585 GetParents()[0]->Create( dynobj->GetSuperClassInstance() , paramCount , params ) ;
586 }
587
588 // get number of parameters for constructor
589 int wxDynamicClassInfo::GetCreateParamCount() const
590 {
591 return GetParents()[0]->GetCreateParamCount() ;
592 }
593
594 // get i-th constructor parameter
595 const wxChar* wxDynamicClassInfo::GetCreateParamName(int i) const
596 {
597 return GetParents()[0]->GetCreateParamName( i ) ;
598 }
599
600 void wxDynamicClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value) const
601 {
602 wxDynamicObject* dynobj = dynamic_cast< wxDynamicObject * >( object ) ;
603 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
604 if ( FindPropertyInfoInThisClass(propertyName) )
605 dynobj->SetProperty( propertyName , value ) ;
606 else
607 GetParents()[0]->SetProperty( dynobj->GetSuperClassInstance() , propertyName , value ) ;
608 }
609
610 wxxVariant wxDynamicClassInfo::GetProperty(wxObject *object, const char *propertyName) const
611 {
612 wxDynamicObject* dynobj = dynamic_cast< wxDynamicObject * >( object ) ;
613 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
614 if ( FindPropertyInfoInThisClass(propertyName) )
615 return dynobj->GetProperty( propertyName ) ;
616 else
617 return GetParents()[0]->GetProperty( dynobj->GetSuperClassInstance() , propertyName ) ;
618 }
619
620 void wxDynamicClassInfo::AddProperty( const wxChar *propertyName , const wxTypeInfo* typeInfo )
621 {
622 new wxPropertyInfo( m_firstProperty , this , propertyName , typeInfo , new wxGenericPropertyAccessor( propertyName ) , wxxVariant() ) ;
623 }
624
625 void wxDynamicClassInfo::AddHandler( const wxChar *handlerName , wxObjectEventFunction address , const wxClassInfo* eventClassInfo )
626 {
627 new wxHandlerInfo( m_firstHandler , handlerName , address , eventClassInfo ) ;
628 }
629
630 // ----------------------------------------------------------------------------
631 // wxGenericPropertyAccessor
632 // ----------------------------------------------------------------------------
633
634 struct wxGenericPropertyAccessor::wxGenericPropertyAccessorInternal
635 {
636 char filler ;
637 } ;
638
639 wxGenericPropertyAccessor::wxGenericPropertyAccessor( const wxString& propertyName )
640 : wxPropertyAccessor( NULL , NULL , NULL , NULL )
641 {
642 m_data = new wxGenericPropertyAccessorInternal ;
643 m_propertyName = propertyName ;
644 m_getterName = wxT("Get")+propertyName ;
645 m_setterName = wxT("Set")+propertyName ;
646 }
647
648 wxGenericPropertyAccessor::~wxGenericPropertyAccessor()
649 {
650 delete m_data ;
651 }
652 void wxGenericPropertyAccessor::SetProperty(wxObject *object, const wxxVariant &value) const
653 {
654 wxDynamicObject* dynobj = dynamic_cast< wxDynamicObject * >( object ) ;
655 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
656 dynobj->SetProperty(m_propertyName , value ) ;
657 }
658
659 void wxGenericPropertyAccessor::GetProperty(const wxObject *object, wxxVariant& value) const
660 {
661 const wxDynamicObject* dynobj = dynamic_cast< const wxDynamicObject * >( object ) ;
662 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
663 value = dynobj->GetProperty( m_propertyName ) ;
664 }
665 #endif