1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/xti.cpp
3 // Purpose: runtime metadata information (extended class info
4 // Author: Stefan Csomor
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "xti.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/object.h"
29 #if wxUSE_EXTENDED_RTTI
32 #include "wx/xml/xml.h"
33 #include "wx/tokenzr.h"
37 #include "wx/beforestd.h"
40 #include "wx/afterstd.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxEnumData::wxEnumData( wxEnumMemberData
* data
)
51 for ( m_count
= 0; m_members
[m_count
].m_name
; m_count
++)
55 bool wxEnumData::HasEnumMemberValue(const wxChar
*name
, int *value
) const
58 for (i
= 0; m_members
[i
].m_name
; i
++ )
60 if (!wxStrcmp(name
, m_members
[i
].m_name
))
63 *value
= m_members
[i
].m_value
;
70 int wxEnumData::GetEnumMemberValue(const wxChar
*name
) const
73 for (i
= 0; m_members
[i
].m_name
; i
++ )
75 if (!wxStrcmp(name
, m_members
[i
].m_name
))
77 return m_members
[i
].m_value
;
83 const wxChar
*wxEnumData::GetEnumMemberName(int value
) const
86 for (i
= 0; m_members
[i
].m_name
; i
++)
87 if (value
== m_members
[i
].m_value
)
88 return m_members
[i
].m_name
;
93 int wxEnumData::GetEnumMemberValueByIndex( int idx
) const
95 // we should cache the count in order to avoid out-of-bounds errors
96 return m_members
[idx
].m_value
;
99 const wxChar
* wxEnumData::GetEnumMemberNameByIndex( int idx
) const
101 // we should cache the count in order to avoid out-of-bounds errors
102 return m_members
[idx
].m_name
;
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 // streamer specializations
113 // for all built-in types
117 template<> void wxStringReadValue(const wxString
&s
, bool &data
)
120 wxSscanf(s
, _T("%d"), &intdata
) ;
121 data
= (bool)intdata
;
124 template<> void wxStringWriteValue(wxString
&s
, const bool &data
)
126 s
= wxString::Format(_T("%d"), data
) ;
131 template<> void wxStringReadValue(const wxString
&s
, char &data
)
134 wxSscanf(s
, _T("%d"), &intdata
) ;
135 data
= char(intdata
) ;
138 template<> void wxStringWriteValue(wxString
&s
, const char &data
)
140 s
= wxString::Format(_T("%d"), data
) ;
145 template<> void wxStringReadValue(const wxString
&s
, unsigned char &data
)
148 wxSscanf(s
, _T("%d"), &intdata
) ;
149 data
= (unsigned char)(intdata
) ;
152 template<> void wxStringWriteValue(wxString
&s
, const unsigned char &data
)
154 s
= wxString::Format(_T("%d"), data
) ;
159 template<> void wxStringReadValue(const wxString
&s
, int &data
)
161 wxSscanf(s
, _T("%d"), &data
) ;
164 template<> void wxStringWriteValue(wxString
&s
, const int &data
)
166 s
= wxString::Format(_T("%d"), data
) ;
171 template<> void wxStringReadValue(const wxString
&s
, unsigned int &data
)
173 wxSscanf(s
, _T("%d"), &data
) ;
176 template<> void wxStringWriteValue(wxString
&s
, const unsigned int &data
)
178 s
= wxString::Format(_T("%d"), data
) ;
183 template<> void wxStringReadValue(const wxString
&s
, long &data
)
185 wxSscanf(s
, _T("%ld"), &data
) ;
188 template<> void wxStringWriteValue(wxString
&s
, const long &data
)
190 s
= wxString::Format(_T("%ld"), data
) ;
195 template<> void wxStringReadValue(const wxString
&s
, unsigned long &data
)
197 wxSscanf(s
, _T("%ld"), &data
) ;
200 template<> void wxStringWriteValue(wxString
&s
, const unsigned long &data
)
202 s
= wxString::Format(_T("%ld"), data
) ;
207 template<> void wxStringReadValue(const wxString
&s
, float &data
)
209 wxSscanf(s
, _T("%f"), &data
) ;
212 template<> void wxStringWriteValue(wxString
&s
, const float &data
)
214 s
= wxString::Format(_T("%f"), data
) ;
219 template<> void wxStringReadValue(const wxString
&s
, double &data
)
221 wxSscanf(s
, _T("%lf"), &data
) ;
224 template<> void wxStringWriteValue(wxString
&s
, const double &data
)
226 s
= wxString::Format(_T("%lf"), data
) ;
231 template<> void wxStringReadValue(const wxString
&s
, wxString
&data
)
236 template<> void wxStringWriteValue(wxString
&s
, const wxString
&data
)
244 wxBuiltInTypeInfo
s_typeInfovoid( wxT_VOID
, NULL
, NULL
, typeid(void).name() ) ;
245 wxBuiltInTypeInfo
s_typeInfobool( wxT_BOOL
, &wxToStringConverter
<bool> , &wxFromStringConverter
<bool>, typeid(bool).name()) ;
246 wxBuiltInTypeInfo
s_typeInfochar( wxT_CHAR
, &wxToStringConverter
<char> , &wxFromStringConverter
<char>, typeid(char).name()) ;
247 wxBuiltInTypeInfo
s_typeInfounsignedchar( wxT_UCHAR
, &wxToStringConverter
< unsigned char > , &wxFromStringConverter
<unsigned char>, typeid(unsigned char).name()) ;
248 wxBuiltInTypeInfo
s_typeInfoint( wxT_INT
, &wxToStringConverter
<int> , &wxFromStringConverter
<int>, typeid(int).name()) ;
249 wxBuiltInTypeInfo
s_typeInfounsignedint( wxT_UINT
, &wxToStringConverter
<unsigned int> , &wxFromStringConverter
<unsigned int>, typeid(unsigned int).name()) ;
250 wxBuiltInTypeInfo
s_typeInfolong( wxT_LONG
, &wxToStringConverter
<long> , &wxFromStringConverter
<long>, typeid(long).name()) ;
251 wxBuiltInTypeInfo
s_typeInfounsignedlong( wxT_ULONG
, &wxToStringConverter
<unsigned long> , &wxFromStringConverter
<unsigned long>, typeid(unsigned long).name()) ;
252 wxBuiltInTypeInfo
s_typeInfofloat( wxT_FLOAT
, &wxToStringConverter
<float> , &wxFromStringConverter
<float>, typeid(float).name()) ;
253 wxBuiltInTypeInfo
s_typeInfodouble( wxT_DOUBLE
, &wxToStringConverter
<double> , &wxFromStringConverter
<double>, typeid(double).name()) ;
254 wxBuiltInTypeInfo
s_typeInfowxString( wxT_STRING
, &wxToStringConverter
<wxString
> , &wxFromStringConverter
<wxString
>, typeid(wxString
).name()) ;
256 // this are compiler induced specialization which are never used anywhere
258 WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
259 WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
260 WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
261 WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
262 WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
263 WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
264 WX_ILLEGAL_TYPE_SPECIALIZATION( wxString
* )
266 WX_COLLECTION_TYPE_INFO( wxString
, wxArrayString
) ;
268 template<> void wxCollectionToVariantArray( wxArrayString
const &theArray
, wxxVariantArray
&value
)
270 wxArrayCollectionToVariantArray( theArray
, value
) ;
273 wxTypeInfoMap
*wxTypeInfo::sm_typeTable
= NULL
;
275 wxTypeInfo
*wxTypeInfo::FindType(const wxChar
*typeName
)
277 wxTypeInfoMap::iterator iter
= sm_typeTable
->find(typeName
) ;
278 wxASSERT_MSG( iter
!= sm_typeTable
->end() , wxT("lookup for a non-existent type-info") ) ;
279 return (wxTypeInfo
*)iter
->second
;
283 wxClassTypeInfo::wxClassTypeInfo( wxTypeKind kind
, wxClassInfo
* classInfo
, converterToString_t to
, converterFromString_t from
, const char *name
) :
284 wxTypeInfo( kind
, to
, from
, name
)
285 { wxASSERT_MSG( kind
== wxT_OBJECT_PTR
|| kind
== wxT_OBJECT
, wxT("Illegal Kind for Enum Type")) ; m_classInfo
= classInfo
;}
288 wxClassTypeInfo::wxClassTypeInfo( wxTypeKind kind
, wxClassInfo
* classInfo
, converterToString_t to
, converterFromString_t from
, const wxString
&name
) :
289 wxTypeInfo( kind
, to
, from
, name
)
290 { wxASSERT_MSG( kind
== wxT_OBJECT_PTR
|| kind
== wxT_OBJECT
, wxT("Illegal Kind for Enum Type")) ; m_classInfo
= classInfo
;}
292 wxDelegateTypeInfo::wxDelegateTypeInfo( int eventType
, wxClassInfo
* eventClass
, converterToString_t to
, converterFromString_t from
) :
293 wxTypeInfo ( wxT_DELEGATE
, to
, from
, wxEmptyString
)
294 { m_eventClass
= eventClass
; m_eventType
= eventType
; m_lastEventType
= -1 ;}
296 wxDelegateTypeInfo::wxDelegateTypeInfo( int eventType
, int lastEventType
, wxClassInfo
* eventClass
, converterToString_t to
, converterFromString_t from
) :
297 wxTypeInfo ( wxT_DELEGATE
, to
, from
, wxEmptyString
)
298 { m_eventClass
= eventClass
; m_eventType
= eventType
; m_lastEventType
= lastEventType
; }
300 void wxTypeInfo::Register()
302 if ( sm_typeTable
== NULL
)
303 sm_typeTable
= new wxTypeInfoMap() ;
305 if( !m_name
.IsEmpty() )
306 (*sm_typeTable
)[m_name
] = this ;
309 void wxTypeInfo::Unregister()
311 if( !m_name
.IsEmpty() )
312 sm_typeTable
->erase(m_name
);
315 // removing header dependancy on string tokenizer
317 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
319 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
322 while (tokenizer
.HasMoreTokens())
324 array
.Add(tokenizer
.GetNextToken()) ;
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 wxPropertyInfo::~wxPropertyInfo()
334 if ( this == m_itsClass
->m_firstProperty
)
336 m_itsClass
->m_firstProperty
= m_next
;
340 wxPropertyInfo
*info
= m_itsClass
->m_firstProperty
;
343 if ( info
->m_next
== this )
345 info
->m_next
= m_next
;
354 wxHandlerInfo::~wxHandlerInfo()
356 if ( this == m_itsClass
->m_firstHandler
)
358 m_itsClass
->m_firstHandler
= m_next
;
362 wxHandlerInfo
*info
= m_itsClass
->m_firstHandler
;
365 if ( info
->m_next
== this )
367 info
->m_next
= m_next
;
376 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const wxChar
*PropertyName
) const
378 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
381 return info
->GetAccessor() ;
386 wxPropertyInfo
*wxClassInfo::FindPropertyInfoInThisClass (const wxChar
*PropertyName
) const
388 wxPropertyInfo
* info
= m_firstProperty
;
392 if ( wxStrcmp( info
->GetName() , PropertyName
) == 0 )
394 info
= info
->GetNext() ;
400 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const wxChar
*PropertyName
) const
402 const wxPropertyInfo
* info
= FindPropertyInfoInThisClass( PropertyName
) ;
406 const wxClassInfo
** parents
= GetParents() ;
407 for ( int i
= 0 ; parents
[i
] ; ++ i
)
409 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
416 wxHandlerInfo
*wxClassInfo::FindHandlerInfoInThisClass (const wxChar
*PropertyName
) const
418 wxHandlerInfo
* info
= m_firstHandler
;
422 if ( wxStrcmp( info
->GetName() , PropertyName
) == 0 )
424 info
= info
->GetNext() ;
430 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const wxChar
*PropertyName
) const
432 const wxHandlerInfo
* info
= FindHandlerInfoInThisClass( PropertyName
) ;
437 const wxClassInfo
** parents
= GetParents() ;
438 for ( int i
= 0 ; parents
[i
] ; ++ i
)
440 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
447 wxObjectStreamingCallback
wxClassInfo::GetStreamingCallback() const
449 if ( m_streamingCallback
)
450 return m_streamingCallback
;
452 wxObjectStreamingCallback retval
= NULL
;
453 const wxClassInfo
** parents
= GetParents() ;
454 for ( int i
= 0 ; parents
[i
] && retval
== NULL
; ++ i
)
456 retval
= parents
[i
]->GetStreamingCallback() ;
461 bool wxClassInfo::BeforeWriteObject( const wxObject
*obj
, wxWriter
*streamer
, wxPersister
*persister
, wxxVariantArray
&metadata
) const
463 wxObjectStreamingCallback sb
= GetStreamingCallback() ;
465 return (*sb
)(obj
, streamer
, persister
, metadata
) ;
470 void wxClassInfo::SetProperty(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
&value
) const
472 const wxPropertyAccessor
*accessor
;
474 accessor
= FindAccessor(propertyName
);
475 wxASSERT(accessor
->HasSetter());
476 accessor
->SetProperty( object
, value
) ;
479 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const wxChar
*propertyName
) const
481 const wxPropertyAccessor
*accessor
;
483 accessor
= FindAccessor(propertyName
);
484 wxASSERT(accessor
->HasGetter());
486 accessor
->GetProperty(object
,result
);
490 wxxVariantArray
wxClassInfo::GetPropertyCollection(wxObject
*object
, const wxChar
*propertyName
) const
492 const wxPropertyAccessor
*accessor
;
494 accessor
= FindAccessor(propertyName
);
495 wxASSERT(accessor
->HasGetter());
496 wxxVariantArray result
;
497 accessor
->GetPropertyCollection(object
,result
);
501 void wxClassInfo::AddToPropertyCollection(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
& value
) const
503 const wxPropertyAccessor
*accessor
;
505 accessor
= FindAccessor(propertyName
);
506 wxASSERT(accessor
->HasAdder());
507 accessor
->AddToPropertyCollection( object
, value
) ;
510 void wxClassInfo::GetProperties( wxPropertyInfoMap
&map
) const
512 const wxPropertyInfo
*pi
= GetFirstProperty() ;
515 if ( map
.find( pi
->GetName() ) == map
.end() )
516 map
[pi
->GetName()] = (wxPropertyInfo
*) pi
;
521 const wxClassInfo
** parents
= GetParents() ;
522 for ( int i
= 0 ; parents
[i
] ; ++ i
)
524 parents
[i
]->GetProperties( map
) ;
532 wxObject
* wxxVariant::GetAsObject()
534 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
536 return ti
->GetClassInfo()->VariantToInstance(*this) ;
541 // ----------------------------------------------------------------------------
542 // wxDynamicObject support
543 // ----------------------------------------------------------------------------
545 // Dynamic Objects are objects that have a real superclass instance and carry their
546 // own attributes in a hash map. Like this it is possible to create the objects and
547 // stream them, as if their class information was already available from compiled data
549 struct wxDynamicObject::wxDynamicObjectInternal
552 map
<wstring
,wxxVariant
> m_properties
;
554 map
<string
,wxxVariant
> m_properties
;
558 // instantiates this object with an instance of its superclass
559 wxDynamicObject::wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
)
561 m_superClassInstance
= superClassInstance
;
563 m_data
= new wxDynamicObjectInternal
;
566 wxDynamicObject::~wxDynamicObject()
569 delete m_superClassInstance
;
572 void wxDynamicObject::SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
)
574 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
575 m_data
->m_properties
[propertyName
] = value
;
578 wxxVariant
wxDynamicObject::GetProperty (const wxChar
*propertyName
) const
580 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
581 return m_data
->m_properties
[propertyName
] ;
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 wxDynamicClassInfo::wxDynamicClassInfo( const wxChar
*unitName
, const wxChar
*className
, const wxClassInfo
* superClass
) :
589 wxClassInfo( unitName
, className
, new const wxClassInfo
*[2])
591 GetParents()[0] = superClass
;
592 GetParents()[1] = NULL
;
595 wxDynamicClassInfo::~wxDynamicClassInfo()
597 delete[] GetParents() ;
600 wxObject
*wxDynamicClassInfo::AllocateObject() const
602 wxObject
* parent
= GetParents()[0]->AllocateObject() ;
603 return new wxDynamicObject( parent
, this ) ;
606 void wxDynamicClassInfo::Create (wxObject
*object
, int paramCount
, wxxVariant
*params
) const
608 wxDynamicObject
*dynobj
= dynamic_cast< wxDynamicObject
*>( object
) ;
609 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::Create on an object other than wxDynamicObject") ) ;
610 GetParents()[0]->Create( dynobj
->GetSuperClassInstance() , paramCount
, params
) ;
613 // get number of parameters for constructor
614 int wxDynamicClassInfo::GetCreateParamCount() const
616 return GetParents()[0]->GetCreateParamCount() ;
619 // get i-th constructor parameter
620 const wxChar
* wxDynamicClassInfo::GetCreateParamName(int i
) const
622 return GetParents()[0]->GetCreateParamName( i
) ;
625 void wxDynamicClassInfo::SetProperty(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
&value
) const
627 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
628 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
629 if ( FindPropertyInfoInThisClass(propertyName
) )
630 dynobj
->SetProperty( propertyName
, value
) ;
632 GetParents()[0]->SetProperty( dynobj
->GetSuperClassInstance() , propertyName
, value
) ;
635 wxxVariant
wxDynamicClassInfo::GetProperty(wxObject
*object
, const wxChar
*propertyName
) const
637 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
638 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
639 if ( FindPropertyInfoInThisClass(propertyName
) )
640 return dynobj
->GetProperty( propertyName
) ;
642 return GetParents()[0]->GetProperty( dynobj
->GetSuperClassInstance() , propertyName
) ;
645 void wxDynamicClassInfo::AddProperty( const wxChar
*propertyName
, const wxTypeInfo
* typeInfo
)
647 new wxPropertyInfo( m_firstProperty
, this , propertyName
, typeInfo
->GetTypeName() , new wxGenericPropertyAccessor( propertyName
) , wxxVariant() ) ;
650 void wxDynamicClassInfo::AddHandler( const wxChar
*handlerName
, wxObjectEventFunction address
, const wxClassInfo
* eventClassInfo
)
652 new wxHandlerInfo( m_firstHandler
, this , handlerName
, address
, eventClassInfo
) ;
655 // removes an existing runtime-property
656 void wxDynamicClassInfo::RemoveProperty( const wxChar
*propertyName
)
658 delete FindPropertyInfoInThisClass(propertyName
) ;
661 // removes an existing runtime-handler
662 void wxDynamicClassInfo::RemoveHandler( const wxChar
*handlerName
)
664 delete FindHandlerInfoInThisClass(handlerName
) ;
667 // renames an existing runtime-property
668 void wxDynamicClassInfo::RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
)
670 wxPropertyInfo
* pi
= FindPropertyInfoInThisClass(oldPropertyName
) ;
671 wxASSERT_MSG( pi
,wxT("not existing property") ) ;
672 pi
->m_name
= newPropertyName
;
673 dynamic_cast<wxGenericPropertyAccessor
*>(pi
->GetAccessor())->RenameProperty( oldPropertyName
, newPropertyName
) ;
676 // renames an existing runtime-handler
677 void wxDynamicClassInfo::RenameHandler( const wxChar
*oldHandlerName
, const wxChar
*newHandlerName
)
679 wxASSERT_MSG(FindHandlerInfoInThisClass(oldHandlerName
),wxT("not existing handler") ) ;
680 FindHandlerInfoInThisClass(oldHandlerName
)->m_name
= newHandlerName
;
683 // ----------------------------------------------------------------------------
684 // wxGenericPropertyAccessor
685 // ----------------------------------------------------------------------------
687 struct wxGenericPropertyAccessor::wxGenericPropertyAccessorInternal
692 wxGenericPropertyAccessor::wxGenericPropertyAccessor( const wxString
& propertyName
)
693 : wxPropertyAccessor( NULL
, NULL
, NULL
, NULL
)
695 m_data
= new wxGenericPropertyAccessorInternal
;
696 m_propertyName
= propertyName
;
697 m_getterName
= wxT("Get")+propertyName
;
698 m_setterName
= wxT("Set")+propertyName
;
701 wxGenericPropertyAccessor::~wxGenericPropertyAccessor()
705 void wxGenericPropertyAccessor::SetProperty(wxObject
*object
, const wxxVariant
&value
) const
707 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
708 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
709 dynobj
->SetProperty(m_propertyName
, value
) ;
712 void wxGenericPropertyAccessor::GetProperty(const wxObject
*object
, wxxVariant
& value
) const
714 const wxDynamicObject
* dynobj
= dynamic_cast< const wxDynamicObject
* >( object
) ;
715 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
716 value
= dynobj
->GetProperty( m_propertyName
) ;