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
;
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
;}
296 void wxTypeInfo::Register()
298 if ( sm_typeTable
== NULL
)
299 sm_typeTable
= new wxTypeInfoMap() ;
301 if( !m_name
.IsEmpty() )
302 (*sm_typeTable
)[m_name
] = this ;
305 void wxTypeInfo::Unregister()
307 if( !m_name
.IsEmpty() )
308 sm_typeTable
->erase(m_name
);
311 // removing header dependancy on string tokenizer
313 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
315 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
318 while (tokenizer
.HasMoreTokens())
320 array
.Add(tokenizer
.GetNextToken()) ;
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 wxPropertyInfo::~wxPropertyInfo()
330 if ( this == m_itsClass
->m_firstProperty
)
332 m_itsClass
->m_firstProperty
= m_next
;
336 wxPropertyInfo
*info
= m_itsClass
->m_firstProperty
;
339 if ( info
->m_next
== this )
341 info
->m_next
= m_next
;
350 wxHandlerInfo::~wxHandlerInfo()
352 if ( this == m_itsClass
->m_firstHandler
)
354 m_itsClass
->m_firstHandler
= m_next
;
358 wxHandlerInfo
*info
= m_itsClass
->m_firstHandler
;
361 if ( info
->m_next
== this )
363 info
->m_next
= m_next
;
372 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const wxChar
*PropertyName
) const
374 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
377 return info
->GetAccessor() ;
382 wxPropertyInfo
*wxClassInfo::FindPropertyInfoInThisClass (const wxChar
*PropertyName
) const
384 wxPropertyInfo
* info
= m_firstProperty
;
388 if ( wxStrcmp( info
->GetName() , PropertyName
) == 0 )
390 info
= info
->GetNext() ;
396 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const wxChar
*PropertyName
) const
398 const wxPropertyInfo
* info
= FindPropertyInfoInThisClass( PropertyName
) ;
402 const wxClassInfo
** parents
= GetParents() ;
403 for ( int i
= 0 ; parents
[i
] ; ++ i
)
405 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
412 wxHandlerInfo
*wxClassInfo::FindHandlerInfoInThisClass (const wxChar
*PropertyName
) const
414 wxHandlerInfo
* info
= m_firstHandler
;
418 if ( wxStrcmp( info
->GetName() , PropertyName
) == 0 )
420 info
= info
->GetNext() ;
426 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const wxChar
*PropertyName
) const
428 const wxHandlerInfo
* info
= FindHandlerInfoInThisClass( PropertyName
) ;
433 const wxClassInfo
** parents
= GetParents() ;
434 for ( int i
= 0 ; parents
[i
] ; ++ i
)
436 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
443 wxObjectStreamingCallback
wxClassInfo::GetStreamingCallback() const
445 if ( m_streamingCallback
)
446 return m_streamingCallback
;
448 wxObjectStreamingCallback retval
= NULL
;
449 const wxClassInfo
** parents
= GetParents() ;
450 for ( int i
= 0 ; parents
[i
] && retval
== NULL
; ++ i
)
452 retval
= parents
[i
]->GetStreamingCallback() ;
457 bool wxClassInfo::BeforeWriteObject( const wxObject
*obj
, wxWriter
*streamer
, wxPersister
*persister
, wxxVariantArray
&metadata
) const
459 wxObjectStreamingCallback sb
= GetStreamingCallback() ;
461 return (*sb
)(obj
, streamer
, persister
, metadata
) ;
466 void wxClassInfo::SetProperty(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
&value
) const
468 const wxPropertyAccessor
*accessor
;
470 accessor
= FindAccessor(propertyName
);
471 wxASSERT(accessor
->HasSetter());
472 accessor
->SetProperty( object
, value
) ;
475 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const wxChar
*propertyName
) const
477 const wxPropertyAccessor
*accessor
;
479 accessor
= FindAccessor(propertyName
);
480 wxASSERT(accessor
->HasGetter());
482 accessor
->GetProperty(object
,result
);
486 wxxVariantArray
wxClassInfo::GetPropertyCollection(wxObject
*object
, const wxChar
*propertyName
) const
488 const wxPropertyAccessor
*accessor
;
490 accessor
= FindAccessor(propertyName
);
491 wxASSERT(accessor
->HasGetter());
492 wxxVariantArray result
;
493 accessor
->GetPropertyCollection(object
,result
);
497 void wxClassInfo::AddToPropertyCollection(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
& value
) const
499 const wxPropertyAccessor
*accessor
;
501 accessor
= FindAccessor(propertyName
);
502 wxASSERT(accessor
->HasAdder());
503 accessor
->AddToPropertyCollection( object
, value
) ;
506 void wxClassInfo::GetProperties( wxPropertyInfoMap
&map
) const
508 const wxPropertyInfo
*pi
= GetFirstProperty() ;
511 if ( map
.find( pi
->GetName() ) == map
.end() )
512 map
[pi
->GetName()] = (wxPropertyInfo
*) pi
;
517 const wxClassInfo
** parents
= GetParents() ;
518 for ( int i
= 0 ; parents
[i
] ; ++ i
)
520 parents
[i
]->GetProperties( map
) ;
528 wxObject
* wxxVariant::GetAsObject()
530 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
532 return ti
->GetClassInfo()->VariantToInstance(*this) ;
537 // ----------------------------------------------------------------------------
538 // wxDynamicObject support
539 // ----------------------------------------------------------------------------
541 // Dynamic Objects are objects that have a real superclass instance and carry their
542 // own attributes in a hash map. Like this it is possible to create the objects and
543 // stream them, as if their class information was already available from compiled data
545 struct wxDynamicObject::wxDynamicObjectInternal
548 map
<wstring
,wxxVariant
> m_properties
;
550 map
<string
,wxxVariant
> m_properties
;
554 // instantiates this object with an instance of its superclass
555 wxDynamicObject::wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
)
557 m_superClassInstance
= superClassInstance
;
559 m_data
= new wxDynamicObjectInternal
;
562 wxDynamicObject::~wxDynamicObject()
565 delete m_superClassInstance
;
568 void wxDynamicObject::SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
)
570 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
571 m_data
->m_properties
[propertyName
] = value
;
574 wxxVariant
wxDynamicObject::GetProperty (const wxChar
*propertyName
) const
576 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
577 return m_data
->m_properties
[propertyName
] ;
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 wxDynamicClassInfo::wxDynamicClassInfo( const wxChar
*unitName
, const wxChar
*className
, const wxClassInfo
* superClass
) :
585 wxClassInfo( unitName
, className
, new const wxClassInfo
*[2])
587 GetParents()[0] = superClass
;
588 GetParents()[1] = NULL
;
591 wxDynamicClassInfo::~wxDynamicClassInfo()
593 delete[] GetParents() ;
596 wxObject
*wxDynamicClassInfo::AllocateObject() const
598 wxObject
* parent
= GetParents()[0]->AllocateObject() ;
599 return new wxDynamicObject( parent
, this ) ;
602 void wxDynamicClassInfo::Create (wxObject
*object
, int paramCount
, wxxVariant
*params
) const
604 wxDynamicObject
*dynobj
= dynamic_cast< wxDynamicObject
*>( object
) ;
605 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::Create on an object other than wxDynamicObject") ) ;
606 GetParents()[0]->Create( dynobj
->GetSuperClassInstance() , paramCount
, params
) ;
609 // get number of parameters for constructor
610 int wxDynamicClassInfo::GetCreateParamCount() const
612 return GetParents()[0]->GetCreateParamCount() ;
615 // get i-th constructor parameter
616 const wxChar
* wxDynamicClassInfo::GetCreateParamName(int i
) const
618 return GetParents()[0]->GetCreateParamName( i
) ;
621 void wxDynamicClassInfo::SetProperty(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
&value
) const
623 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
624 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
625 if ( FindPropertyInfoInThisClass(propertyName
) )
626 dynobj
->SetProperty( propertyName
, value
) ;
628 GetParents()[0]->SetProperty( dynobj
->GetSuperClassInstance() , propertyName
, value
) ;
631 wxxVariant
wxDynamicClassInfo::GetProperty(wxObject
*object
, const wxChar
*propertyName
) const
633 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
634 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
635 if ( FindPropertyInfoInThisClass(propertyName
) )
636 return dynobj
->GetProperty( propertyName
) ;
638 return GetParents()[0]->GetProperty( dynobj
->GetSuperClassInstance() , propertyName
) ;
641 void wxDynamicClassInfo::AddProperty( const wxChar
*propertyName
, const wxTypeInfo
* typeInfo
)
643 new wxPropertyInfo( m_firstProperty
, this , propertyName
, typeInfo
->GetTypeName() , new wxGenericPropertyAccessor( propertyName
) , wxxVariant() ) ;
646 void wxDynamicClassInfo::AddHandler( const wxChar
*handlerName
, wxObjectEventFunction address
, const wxClassInfo
* eventClassInfo
)
648 new wxHandlerInfo( m_firstHandler
, this , handlerName
, address
, eventClassInfo
) ;
651 // removes an existing runtime-property
652 void wxDynamicClassInfo::RemoveProperty( const wxChar
*propertyName
)
654 delete FindPropertyInfoInThisClass(propertyName
) ;
657 // removes an existing runtime-handler
658 void wxDynamicClassInfo::RemoveHandler( const wxChar
*handlerName
)
660 delete FindHandlerInfoInThisClass(handlerName
) ;
663 // renames an existing runtime-property
664 void wxDynamicClassInfo::RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
)
666 wxPropertyInfo
* pi
= FindPropertyInfoInThisClass(oldPropertyName
) ;
667 wxASSERT_MSG( pi
,wxT("not existing property") ) ;
668 pi
->m_name
= newPropertyName
;
669 dynamic_cast<wxGenericPropertyAccessor
*>(pi
->GetAccessor())->RenameProperty( oldPropertyName
, newPropertyName
) ;
672 // renames an existing runtime-handler
673 void wxDynamicClassInfo::RenameHandler( const wxChar
*oldHandlerName
, const wxChar
*newHandlerName
)
675 wxASSERT_MSG(FindHandlerInfoInThisClass(oldHandlerName
),wxT("not existing handler") ) ;
676 FindHandlerInfoInThisClass(oldHandlerName
)->m_name
= newHandlerName
;
679 // ----------------------------------------------------------------------------
680 // wxGenericPropertyAccessor
681 // ----------------------------------------------------------------------------
683 struct wxGenericPropertyAccessor::wxGenericPropertyAccessorInternal
688 wxGenericPropertyAccessor::wxGenericPropertyAccessor( const wxString
& propertyName
)
689 : wxPropertyAccessor( NULL
, NULL
, NULL
, NULL
)
691 m_data
= new wxGenericPropertyAccessorInternal
;
692 m_propertyName
= propertyName
;
693 m_getterName
= wxT("Get")+propertyName
;
694 m_setterName
= wxT("Set")+propertyName
;
697 wxGenericPropertyAccessor::~wxGenericPropertyAccessor()
701 void wxGenericPropertyAccessor::SetProperty(wxObject
*object
, const wxxVariant
&value
) const
703 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
704 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
705 dynobj
->SetProperty(m_propertyName
, value
) ;
708 void wxGenericPropertyAccessor::GetProperty(const wxObject
*object
, wxxVariant
& value
) const
710 const wxDynamicObject
* dynobj
= dynamic_cast< const wxDynamicObject
* >( object
) ;
711 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
712 value
= dynobj
->GetProperty( m_propertyName
) ;