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
)
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
)
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
)
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
)
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
)
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
;
282 wxClassTypeInfo::wxClassTypeInfo( wxTypeKind kind
, wxClassInfo
* classInfo
, converterToString_t to
, converterFromString_t from
, const wxString
&name
) :
283 wxTypeInfo( kind
, to
, from
, name
)
284 { wxASSERT_MSG( kind
== wxT_OBJECT_PTR
|| kind
== wxT_OBJECT
, wxT("Illegal Kind for Enum Type")) ; m_classInfo
= classInfo
;}
286 wxDelegateTypeInfo::wxDelegateTypeInfo( int eventType
, wxClassInfo
* eventClass
, converterToString_t to
, converterFromString_t from
) :
287 wxTypeInfo ( wxT_DELEGATE
, to
, from
, wxEmptyString
)
288 { m_eventClass
= eventClass
; m_eventType
= eventType
;}
290 void wxTypeInfo::Register()
292 if ( sm_typeTable
== NULL
)
293 sm_typeTable
= new wxTypeInfoMap() ;
295 if( !m_name
.IsEmpty() )
296 (*sm_typeTable
)[m_name
] = this ;
299 void wxTypeInfo::Unregister()
301 if( !m_name
.IsEmpty() )
302 sm_typeTable
->erase(m_name
);
305 // removing header dependancy on string tokenizer
307 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
309 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
312 while (tokenizer
.HasMoreTokens())
314 array
.Add(tokenizer
.GetNextToken()) ;
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 wxPropertyInfo::~wxPropertyInfo()
324 if ( this == m_itsClass
->m_firstProperty
)
326 m_itsClass
->m_firstProperty
= m_next
;
330 wxPropertyInfo
*info
= m_itsClass
->m_firstProperty
;
333 if ( info
->m_next
== this )
335 info
->m_next
= m_next
;
344 wxHandlerInfo::~wxHandlerInfo()
346 if ( this == m_itsClass
->m_firstHandler
)
348 m_itsClass
->m_firstHandler
= m_next
;
352 wxHandlerInfo
*info
= m_itsClass
->m_firstHandler
;
355 if ( info
->m_next
== this )
357 info
->m_next
= m_next
;
366 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const wxChar
*PropertyName
) const
368 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
371 return info
->GetAccessor() ;
376 wxPropertyInfo
*wxClassInfo::FindPropertyInfoInThisClass (const wxChar
*PropertyName
) const
378 wxPropertyInfo
* info
= m_firstProperty
;
382 if ( wxStrcmp( info
->GetName() , PropertyName
) == 0 )
384 info
= info
->GetNext() ;
390 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const wxChar
*PropertyName
) const
392 const wxPropertyInfo
* info
= FindPropertyInfoInThisClass( PropertyName
) ;
396 const wxClassInfo
** parents
= GetParents() ;
397 for ( int i
= 0 ; parents
[i
] ; ++ i
)
399 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
406 wxHandlerInfo
*wxClassInfo::FindHandlerInfoInThisClass (const wxChar
*PropertyName
) const
408 wxHandlerInfo
* info
= m_firstHandler
;
412 if ( wxStrcmp( info
->GetName() , PropertyName
) == 0 )
414 info
= info
->GetNext() ;
420 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const wxChar
*PropertyName
) const
422 const wxHandlerInfo
* info
= FindHandlerInfoInThisClass( PropertyName
) ;
427 const wxClassInfo
** parents
= GetParents() ;
428 for ( int i
= 0 ; parents
[i
] ; ++ i
)
430 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
437 wxObjectStreamingCallback
wxClassInfo::GetStreamingCallback() const
439 if ( m_streamingCallback
)
440 return m_streamingCallback
;
442 wxObjectStreamingCallback retval
= NULL
;
443 const wxClassInfo
** parents
= GetParents() ;
444 for ( int i
= 0 ; parents
[i
] && retval
== NULL
; ++ i
)
446 retval
= parents
[i
]->GetStreamingCallback() ;
451 bool wxClassInfo::BeforeWriteObject( const wxObject
*obj
, wxWriter
*streamer
, wxPersister
*persister
, wxxVariantArray
&metadata
) const
453 wxObjectStreamingCallback sb
= GetStreamingCallback() ;
455 return (*sb
)(obj
, streamer
, persister
, metadata
) ;
460 void wxClassInfo::SetProperty(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
&value
) const
462 const wxPropertyAccessor
*accessor
;
464 accessor
= FindAccessor(propertyName
);
465 wxASSERT(accessor
->HasSetter());
466 accessor
->SetProperty( object
, value
) ;
469 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const wxChar
*propertyName
) const
471 const wxPropertyAccessor
*accessor
;
473 accessor
= FindAccessor(propertyName
);
474 wxASSERT(accessor
->HasGetter());
476 accessor
->GetProperty(object
,result
);
480 wxxVariantArray
wxClassInfo::GetPropertyCollection(wxObject
*object
, const wxChar
*propertyName
) const
482 const wxPropertyAccessor
*accessor
;
484 accessor
= FindAccessor(propertyName
);
485 wxASSERT(accessor
->HasGetter());
486 wxxVariantArray result
;
487 accessor
->GetPropertyCollection(object
,result
);
491 void wxClassInfo::AddToPropertyCollection(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
& value
) const
493 const wxPropertyAccessor
*accessor
;
495 accessor
= FindAccessor(propertyName
);
496 wxASSERT(accessor
->HasAdder());
497 accessor
->AddToPropertyCollection( object
, value
) ;
500 void wxClassInfo::GetProperties( wxPropertyInfoMap
&map
) const
502 const wxPropertyInfo
*pi
= GetFirstProperty() ;
505 if ( map
.find( pi
->GetName() ) == map
.end() )
506 map
[pi
->GetName()] = (wxPropertyInfo
*) pi
;
511 const wxClassInfo
** parents
= GetParents() ;
512 for ( int i
= 0 ; parents
[i
] ; ++ i
)
514 parents
[i
]->GetProperties( map
) ;
522 wxObject
* wxxVariant::GetAsObject()
524 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
526 return ti
->GetClassInfo()->VariantToInstance(*this) ;
531 // ----------------------------------------------------------------------------
532 // wxDynamicObject support
533 // ----------------------------------------------------------------------------
535 // Dynamic Objects are objects that have a real superclass instance and carry their
536 // own attributes in a hash map. Like this it is possible to create the objects and
537 // stream them, as if their class information was already available from compiled data
539 struct wxDynamicObject::wxDynamicObjectInternal
542 map
<wstring
,wxxVariant
> m_properties
;
544 map
<string
,wxxVariant
> m_properties
;
548 // instantiates this object with an instance of its superclass
549 wxDynamicObject::wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
)
551 m_superClassInstance
= superClassInstance
;
553 m_data
= new wxDynamicObjectInternal
;
556 wxDynamicObject::~wxDynamicObject()
559 delete m_superClassInstance
;
562 void wxDynamicObject::SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
)
564 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
565 m_data
->m_properties
[propertyName
] = value
;
568 wxxVariant
wxDynamicObject::GetProperty (const wxChar
*propertyName
) const
570 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
571 return m_data
->m_properties
[propertyName
] ;
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 wxDynamicClassInfo::wxDynamicClassInfo( const wxChar
*unitName
, const wxChar
*className
, const wxClassInfo
* superClass
) :
579 wxClassInfo( unitName
, className
, new const wxClassInfo
*[2])
581 GetParents()[0] = superClass
;
582 GetParents()[1] = NULL
;
585 wxDynamicClassInfo::~wxDynamicClassInfo()
587 delete[] GetParents() ;
590 wxObject
*wxDynamicClassInfo::AllocateObject() const
592 wxObject
* parent
= GetParents()[0]->AllocateObject() ;
593 return new wxDynamicObject( parent
, this ) ;
596 void wxDynamicClassInfo::Create (wxObject
*object
, int paramCount
, wxxVariant
*params
) const
598 wxDynamicObject
*dynobj
= dynamic_cast< wxDynamicObject
*>( object
) ;
599 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::Create on an object other than wxDynamicObject") ) ;
600 GetParents()[0]->Create( dynobj
->GetSuperClassInstance() , paramCount
, params
) ;
603 // get number of parameters for constructor
604 int wxDynamicClassInfo::GetCreateParamCount() const
606 return GetParents()[0]->GetCreateParamCount() ;
609 // get i-th constructor parameter
610 const wxChar
* wxDynamicClassInfo::GetCreateParamName(int i
) const
612 return GetParents()[0]->GetCreateParamName( i
) ;
615 void wxDynamicClassInfo::SetProperty(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
&value
) const
617 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
618 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
619 if ( FindPropertyInfoInThisClass(propertyName
) )
620 dynobj
->SetProperty( propertyName
, value
) ;
622 GetParents()[0]->SetProperty( dynobj
->GetSuperClassInstance() , propertyName
, value
) ;
625 wxxVariant
wxDynamicClassInfo::GetProperty(wxObject
*object
, const wxChar
*propertyName
) 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 return dynobj
->GetProperty( propertyName
) ;
632 return GetParents()[0]->GetProperty( dynobj
->GetSuperClassInstance() , propertyName
) ;
635 void wxDynamicClassInfo::AddProperty( const wxChar
*propertyName
, const wxTypeInfo
* typeInfo
)
637 new wxPropertyInfo( m_firstProperty
, this , propertyName
, typeInfo
->GetTypeName() , new wxGenericPropertyAccessor( propertyName
) , wxxVariant() ) ;
640 void wxDynamicClassInfo::AddHandler( const wxChar
*handlerName
, wxObjectEventFunction address
, const wxClassInfo
* eventClassInfo
)
642 new wxHandlerInfo( m_firstHandler
, this , handlerName
, address
, eventClassInfo
) ;
645 // removes an existing runtime-property
646 void wxDynamicClassInfo::RemoveProperty( const wxChar
*propertyName
)
648 delete FindPropertyInfoInThisClass(propertyName
) ;
651 // removes an existing runtime-handler
652 void wxDynamicClassInfo::RemoveHandler( const wxChar
*handlerName
)
654 delete FindHandlerInfoInThisClass(handlerName
) ;
657 // renames an existing runtime-property
658 void wxDynamicClassInfo::RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
)
660 wxPropertyInfo
* pi
= FindPropertyInfoInThisClass(oldPropertyName
) ;
661 wxASSERT_MSG( pi
,wxT("not existing property") ) ;
662 pi
->m_name
= newPropertyName
;
663 dynamic_cast<wxGenericPropertyAccessor
*>(pi
->GetAccessor())->RenameProperty( oldPropertyName
, newPropertyName
) ;
666 // renames an existing runtime-handler
667 void wxDynamicClassInfo::RenameHandler( const wxChar
*oldHandlerName
, const wxChar
*newHandlerName
)
669 wxASSERT_MSG(FindHandlerInfoInThisClass(oldHandlerName
),wxT("not existing handler") ) ;
670 FindHandlerInfoInThisClass(oldHandlerName
)->m_name
= newHandlerName
;
673 // ----------------------------------------------------------------------------
674 // wxGenericPropertyAccessor
675 // ----------------------------------------------------------------------------
677 struct wxGenericPropertyAccessor::wxGenericPropertyAccessorInternal
682 wxGenericPropertyAccessor::wxGenericPropertyAccessor( const wxString
& propertyName
)
683 : wxPropertyAccessor( NULL
, NULL
, NULL
, NULL
)
685 m_data
= new wxGenericPropertyAccessorInternal
;
686 m_propertyName
= propertyName
;
687 m_getterName
= wxT("Get")+propertyName
;
688 m_setterName
= wxT("Set")+propertyName
;
691 wxGenericPropertyAccessor::~wxGenericPropertyAccessor()
695 void wxGenericPropertyAccessor::SetProperty(wxObject
*object
, const wxxVariant
&value
) const
697 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
698 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
699 dynobj
->SetProperty(m_propertyName
, value
) ;
702 void wxGenericPropertyAccessor::GetProperty(const wxObject
*object
, wxxVariant
& value
) const
704 const wxDynamicObject
* dynobj
= dynamic_cast< const wxDynamicObject
* >( object
) ;
705 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
706 value
= dynobj
->GetProperty( m_propertyName
) ;