properties as collection
[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 #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 template<> const wxTypeInfo* wxGetTypeInfo( void * )
109 {
110 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
111 return &s_typeInfo ;
112 }
113
114 template<> const wxTypeInfo* wxGetTypeInfo( bool * )
115 {
116 static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL ) ;
117 return &s_typeInfo ;
118 }
119
120 template<> const wxTypeInfo* wxGetTypeInfo( char * )
121 {
122 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
123 return &s_typeInfo ;
124 }
125
126 template<> const wxTypeInfo* wxGetTypeInfo( unsigned char * )
127 {
128 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
129 return &s_typeInfo ;
130 }
131
132 template<> const wxTypeInfo* wxGetTypeInfo( int * )
133 {
134 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
135 return &s_typeInfo ;
136 }
137
138 template<> const wxTypeInfo* wxGetTypeInfo( unsigned int * )
139 {
140 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
141 return &s_typeInfo ;
142 }
143
144 template<> const wxTypeInfo* wxGetTypeInfo( long * )
145 {
146 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
147 return &s_typeInfo ;
148 }
149
150 template<> const wxTypeInfo* wxGetTypeInfo( unsigned long * )
151 {
152 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
153 return &s_typeInfo ;
154 }
155
156 template<> const wxTypeInfo* wxGetTypeInfo( float * )
157 {
158 static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT ) ;
159 return &s_typeInfo ;
160 }
161
162 template<> const wxTypeInfo* wxGetTypeInfo( double * )
163 {
164 static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE ) ;
165 return &s_typeInfo ;
166 }
167
168 template<> const wxTypeInfo* wxGetTypeInfo( wxString * )
169 {
170 static wxBuiltInTypeInfo s_typeInfo( wxT_STRING ) ;
171 return &s_typeInfo ;
172 }
173
174 template<> const wxTypeInfo* wxGetTypeInfo( wxWindowList * )
175 {
176 static wxCollectionTypeInfo s_typeInfo( (wxTypeInfo*) wxGetTypeInfo( (wxWindow **) NULL) ) ;
177 return &s_typeInfo ;
178 }
179
180 // this are compiler induced specialization which are never used anywhere
181
182 WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
183 WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
184 WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
185 WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
186 WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
187 WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
188 WX_ILLEGAL_TYPE_SPECIALIZATION( wxString * )
189
190 // ----------------------------------------------------------------------------
191 // value streaming
192 // ----------------------------------------------------------------------------
193
194 // streamer specializations
195 // for all built-in types
196
197 // bool
198
199 template<> void wxStringReadValue(const wxString &s , bool &data )
200 {
201 int intdata ;
202 wxSscanf(s, _T("%d"), &intdata ) ;
203 data = bool(intdata) ;
204 }
205
206 template<> void wxStringWriteValue(wxString &s , const bool &data )
207 {
208 s = wxString::Format("%d", data ) ;
209 }
210
211 // char
212
213 template<> void wxStringReadValue(const wxString &s , char &data )
214 {
215 int intdata ;
216 wxSscanf(s, _T("%d"), &intdata ) ;
217 data = char(intdata) ;
218 }
219
220 template<> void wxStringWriteValue(wxString &s , const char &data )
221 {
222 s = wxString::Format("%d", data ) ;
223 }
224
225 // unsigned char
226
227 template<> void wxStringReadValue(const wxString &s , unsigned char &data )
228 {
229 int intdata ;
230 wxSscanf(s, _T("%d"), &intdata ) ;
231 data = (unsigned char)(intdata) ;
232 }
233
234 template<> void wxStringWriteValue(wxString &s , const unsigned char &data )
235 {
236 s = wxString::Format("%d", data ) ;
237 }
238
239 // int
240
241 template<> void wxStringReadValue(const wxString &s , int &data )
242 {
243 wxSscanf(s, _T("%d"), &data ) ;
244 }
245
246 template<> void wxStringWriteValue(wxString &s , const int &data )
247 {
248 s = wxString::Format("%d", data ) ;
249 }
250
251 // unsigned int
252
253 template<> void wxStringReadValue(const wxString &s , unsigned int &data )
254 {
255 wxSscanf(s, _T("%d"), &data ) ;
256 }
257
258 template<> void wxStringWriteValue(wxString &s , const unsigned int &data )
259 {
260 s = wxString::Format("%d", data ) ;
261 }
262
263 // long
264
265 template<> void wxStringReadValue(const wxString &s , long &data )
266 {
267 wxSscanf(s, _T("%ld"), &data ) ;
268 }
269
270 template<> void wxStringWriteValue(wxString &s , const long &data )
271 {
272 s = wxString::Format("%ld", data ) ;
273 }
274
275 // unsigned long
276
277 template<> void wxStringReadValue(const wxString &s , unsigned long &data )
278 {
279 wxSscanf(s, _T("%ld"), &data ) ;
280 }
281
282 template<> void wxStringWriteValue(wxString &s , const unsigned long &data )
283 {
284 s = wxString::Format("%ld", data ) ;
285 }
286
287 // float
288
289 template<> void wxStringReadValue(const wxString &s , float &data )
290 {
291 wxSscanf(s, _T("%f"), &data ) ;
292 }
293
294 template<> void wxStringWriteValue(wxString &s , const float &data )
295 {
296 s = wxString::Format("%f", data ) ;
297 }
298
299 // double
300
301 template<> void wxStringReadValue(const wxString &s , double &data )
302 {
303 wxSscanf(s, _T("%lf"), &data ) ;
304 }
305
306 template<> void wxStringWriteValue(wxString &s , const double &data )
307 {
308 s = wxString::Format("%lf", data ) ;
309 }
310
311 // wxString
312
313 template<> void wxStringReadValue(const wxString &s , wxString &data )
314 {
315 data = s ;
316 }
317
318 template<> void wxStringWriteValue(wxString &s , const wxString &data )
319 {
320 s = data ;
321 }
322
323 /*
324 Custom Data Streaming / Type Infos
325 we will have to add this for all wx non object types, but it is also an example
326 for custom data structures
327 */
328
329 // wxPoint
330
331 template<> void wxStringReadValue(const wxString &s , wxPoint &data )
332 {
333 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
334 }
335
336 template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
337 {
338 s = wxString::Format("%d,%d", data.x , data.y ) ;
339 }
340
341 template<> void wxStringReadValue(const wxString & , wxPoint* & )
342 {
343 assert(0) ;
344 }
345
346 template<> void wxStringWriteValue(wxString & , wxPoint* const & )
347 {
348 assert(0) ;
349 }
350
351 WX_CUSTOM_TYPE_INFO(wxPoint)
352
353 template<> void wxStringReadValue(const wxString &s , wxSize &data )
354 {
355 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
356 }
357
358 template<> void wxStringWriteValue(wxString &s , const wxSize &data )
359 {
360 s = wxString::Format("%d,%d", data.x , data.y ) ;
361 }
362
363 template<> void wxStringReadValue(const wxString & , wxSize* & )
364 {
365 assert(0) ;
366 }
367
368 template<> void wxStringWriteValue(wxString & , wxSize * const & )
369 {
370 assert(0) ;
371 }
372
373 WX_CUSTOM_TYPE_INFO(wxSize)
374
375 /*
376
377 template<> void wxStringReadValue(const wxString &s , wxColour &data )
378 {
379 // copied from VS xrc
380 unsigned long tmp = 0;
381
382 if (s.Length() != 7 || s[0u] != wxT('#') ||
383 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
384 {
385 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
386 s.c_str() );
387 data = wxNullColour;
388 }
389 else
390 {
391 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
392 (unsigned char) ((tmp & 0x00FF00) >> 8),
393 (unsigned char) ((tmp & 0x0000FF)));
394 }
395 }
396
397 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
398 {
399 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
400 }
401
402 WX_CUSTOM_TYPE_INFO(wxColour)
403
404 */
405
406 // removing header dependancy on string tokenizer
407
408 void wxSetStringToArray( const wxString &s , wxArrayString &array )
409 {
410 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
411 wxString flag;
412 array.Clear() ;
413 while (tokenizer.HasMoreTokens())
414 {
415 array.Add(tokenizer.GetNextToken()) ;
416 }
417 }
418
419 // ----------------------------------------------------------------------------
420 // wxClassInfo
421 // ----------------------------------------------------------------------------
422
423 const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName) const
424 {
425 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
426
427 if ( info )
428 return info->GetAccessor() ;
429
430 return NULL ;
431 }
432
433 const wxPropertyInfo *wxClassInfo::FindPropertyInfoInThisClass (const char *PropertyName) const
434 {
435 const wxPropertyInfo* info = GetFirstProperty() ;
436
437 while( info )
438 {
439 if ( strcmp( info->GetName() , PropertyName ) == 0 )
440 return info ;
441 info = info->GetNext() ;
442 }
443
444 return 0;
445 }
446
447 const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
448 {
449 const wxPropertyInfo* info = FindPropertyInfoInThisClass( PropertyName ) ;
450 if ( info )
451 return info ;
452
453 const wxClassInfo** parents = GetParents() ;
454 for ( int i = 0 ; parents[i] ; ++ i )
455 {
456 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
457 return info ;
458 }
459
460 return 0;
461 }
462
463 const wxHandlerInfo *wxClassInfo::FindHandlerInfoInThisClass (const char *PropertyName) const
464 {
465 const wxHandlerInfo* info = GetFirstHandler() ;
466
467 while( info )
468 {
469 if ( strcmp( info->GetName() , PropertyName ) == 0 )
470 return info ;
471 info = info->GetNext() ;
472 }
473
474 return 0;
475 }
476
477 const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
478 {
479 const wxHandlerInfo* info = FindHandlerInfoInThisClass( PropertyName ) ;
480
481 if ( info )
482 return info ;
483
484 const wxClassInfo** parents = GetParents() ;
485 for ( int i = 0 ; parents[i] ; ++ i )
486 {
487 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
488 return info ;
489 }
490
491 return 0;
492 }
493
494
495 void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value) const
496 {
497 const wxPropertyAccessor *accessor;
498
499 accessor = FindAccessor(propertyName);
500 wxASSERT(accessor->HasSetter());
501 accessor->SetProperty( object , value ) ;
502 }
503
504 wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName) const
505 {
506 const wxPropertyAccessor *accessor;
507
508 accessor = FindAccessor(propertyName);
509 wxASSERT(accessor->HasGetter());
510 return accessor->GetProperty(object);
511 }
512
513 wxxVariantArray wxClassInfo::GetPropertyCollection(wxObject *object, const wxChar *propertyName) const
514 {
515 const wxPropertyAccessor *accessor;
516
517 accessor = FindAccessor(propertyName);
518 wxASSERT(accessor->HasGetter());
519 return accessor->GetPropertyCollection(object);
520 }
521
522 void wxClassInfo::AddToPropertyCollection(wxObject *object, const wxChar *propertyName , const wxxVariant& value) const
523 {
524 const wxPropertyAccessor *accessor;
525
526 accessor = FindAccessor(propertyName);
527 wxASSERT(accessor->HasAdder());
528 accessor->AddToPropertyCollection( object , value ) ;
529 }
530
531 /*
532 VARIANT TO OBJECT
533 */
534
535 wxObject* wxxVariant::GetAsObject()
536 {
537 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
538 if ( ti )
539 return ti->GetClassInfo()->VariantToInstance(*this) ;
540 else
541 return NULL ;
542 }
543
544 // ----------------------------------------------------------------------------
545 // wxDynamicObject support
546 // ----------------------------------------------------------------------------
547 //
548 // Dynamic Objects are objects that have a real superclass instance and carry their
549 // own attributes in a hash map. Like this it is possible to create the objects and
550 // stream them, as if their class information was already available from compiled data
551
552 struct wxDynamicObject::wxDynamicObjectInternal
553 {
554 map<string,wxxVariant> m_properties ;
555 } ;
556
557 // instantiates this object with an instance of its superclass
558 wxDynamicObject::wxDynamicObject(wxObject* superClassInstance, const wxDynamicClassInfo *info)
559 {
560 m_superClassInstance = superClassInstance ;
561 m_classInfo = info ;
562 m_data = new wxDynamicObjectInternal ;
563 }
564
565 wxDynamicObject::~wxDynamicObject()
566 {
567 delete m_data ;
568 delete m_superClassInstance ;
569 }
570
571 void wxDynamicObject::SetProperty (const wxChar *propertyName, const wxxVariant &value)
572 {
573 wxASSERT_MSG(m_classInfo->FindPropertyInfoInThisClass(propertyName),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
574 m_data->m_properties[propertyName] = value ;
575 }
576
577 wxxVariant wxDynamicObject::GetProperty (const wxChar *propertyName) const
578 {
579 wxASSERT_MSG(m_classInfo->FindPropertyInfoInThisClass(propertyName),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
580 return m_data->m_properties[propertyName] ;
581 }
582
583 // ----------------------------------------------------------------------------
584 // wxDynamiClassInfo
585 // ----------------------------------------------------------------------------
586
587 wxDynamicClassInfo::wxDynamicClassInfo( const wxChar *unitName, const wxChar *className , const wxClassInfo* superClass ) :
588 wxClassInfo( unitName, className , new const wxClassInfo*[2])
589 {
590 GetParents()[0] = superClass ;
591 GetParents()[1] = NULL ;
592 }
593
594 wxDynamicClassInfo::~wxDynamicClassInfo()
595 {
596 delete[] GetParents() ;
597 }
598
599 wxObject *wxDynamicClassInfo::AllocateObject() const
600 {
601 wxObject* parent = GetParents()[0]->AllocateObject() ;
602 return new wxDynamicObject( parent , this ) ;
603 }
604
605 void wxDynamicClassInfo::Create (wxObject *object, int paramCount, wxxVariant *params) const
606 {
607 wxDynamicObject *dynobj = dynamic_cast< wxDynamicObject *>( object ) ;
608 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::Create on an object other than wxDynamicObject") ) ;
609 GetParents()[0]->Create( dynobj->GetSuperClassInstance() , paramCount , params ) ;
610 }
611
612 // get number of parameters for constructor
613 int wxDynamicClassInfo::GetCreateParamCount() const
614 {
615 return GetParents()[0]->GetCreateParamCount() ;
616 }
617
618 // get i-th constructor parameter
619 const wxChar* wxDynamicClassInfo::GetCreateParamName(int i) const
620 {
621 return GetParents()[0]->GetCreateParamName( i ) ;
622 }
623
624 void wxDynamicClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value) const
625 {
626 wxDynamicObject* dynobj = dynamic_cast< wxDynamicObject * >( object ) ;
627 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
628 if ( FindPropertyInfoInThisClass(propertyName) )
629 dynobj->SetProperty( propertyName , value ) ;
630 else
631 GetParents()[0]->SetProperty( dynobj->GetSuperClassInstance() , propertyName , value ) ;
632 }
633
634 wxxVariant wxDynamicClassInfo::GetProperty(wxObject *object, const char *propertyName) const
635 {
636 wxDynamicObject* dynobj = dynamic_cast< wxDynamicObject * >( object ) ;
637 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
638 if ( FindPropertyInfoInThisClass(propertyName) )
639 return dynobj->GetProperty( propertyName ) ;
640 else
641 return GetParents()[0]->GetProperty( dynobj->GetSuperClassInstance() , propertyName ) ;
642 }
643
644 void wxDynamicClassInfo::AddProperty( const wxChar *propertyName , const wxTypeInfo* typeInfo )
645 {
646 new wxPropertyInfo( m_firstProperty , propertyName , typeInfo , new wxGenericPropertyAccessor( propertyName ) , wxxVariant() ) ;
647 }
648
649 void wxDynamicClassInfo::AddHandler( const wxChar *handlerName , wxObjectEventFunction address , const wxClassInfo* eventClassInfo )
650 {
651 new wxHandlerInfo( m_firstHandler , handlerName , address , eventClassInfo ) ;
652 }
653
654 // ----------------------------------------------------------------------------
655 // wxGenericPropertyAccessor
656 // ----------------------------------------------------------------------------
657
658 struct wxGenericPropertyAccessor::wxGenericPropertyAccessorInternal
659 {
660 wxString m_propertyName ;
661 } ;
662
663 wxGenericPropertyAccessor::wxGenericPropertyAccessor( const wxChar* propertyName )
664 {
665 m_data = new wxGenericPropertyAccessorInternal ;
666 m_data->m_propertyName = propertyName ;
667 }
668
669 wxGenericPropertyAccessor::~wxGenericPropertyAccessor()
670 {
671 delete m_data ;
672 }
673 void wxGenericPropertyAccessor::SetProperty(wxObject *object, const wxxVariant &value) const
674 {
675 wxDynamicObject* dynobj = dynamic_cast< wxDynamicObject * >( object ) ;
676 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
677 dynobj->SetProperty(m_data->m_propertyName , value ) ;
678 }
679
680 wxxVariant wxGenericPropertyAccessor::GetProperty(const wxObject *object) const
681 {
682 const wxDynamicObject* dynobj = dynamic_cast< const wxDynamicObject * >( object ) ;
683 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
684 return dynobj->GetProperty( m_data->m_propertyName ) ;
685 }
686
687 wxxVariant wxGenericPropertyAccessor::ReadValue( const wxString &value ) const
688 {
689 return wxxVariant(value) ;
690 }
691
692 void wxGenericPropertyAccessor::WriteValue( wxString& value , const wxObject *object ) const
693 {
694 const wxDynamicObject* dynobj = dynamic_cast< const wxDynamicObject * >( object ) ;
695 wxASSERT_MSG( dynobj , wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
696 wxxVariant val = dynobj->GetProperty( m_data->m_propertyName ) ;
697 value = val.GetAsString() ;
698 }
699
700 #endif