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 #include "wx/xml/xml.h"
30 #include "wx/tokenzr.h"
34 #if wxUSE_EXTENDED_RTTI
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 wxEnumData::wxEnumData( wxEnumMemberData
* data
)
48 for ( m_count
= 0; m_members
[m_count
].m_name
; m_count
++)
52 bool wxEnumData::HasEnumMemberValue(const wxChar
*name
, int *value
)
55 for (i
= 0; m_members
[i
].m_name
; i
++ )
57 if (!strcmp(name
, m_members
[i
].m_name
))
60 *value
= m_members
[i
].m_value
;
67 int wxEnumData::GetEnumMemberValue(const wxChar
*name
)
70 for (i
= 0; m_members
[i
].m_name
; i
++ )
72 if (!strcmp(name
, m_members
[i
].m_name
))
74 return m_members
[i
].m_value
;
80 const wxChar
*wxEnumData::GetEnumMemberName(int value
)
83 for (i
= 0; m_members
[i
].m_name
; i
++)
84 if (value
== m_members
[i
].m_value
)
85 return m_members
[i
].m_name
;
90 int wxEnumData::GetEnumMemberValueByIndex( int idx
)
92 // we should cache the count in order to avoid out-of-bounds errors
93 return m_members
[idx
].m_value
;
96 const char * wxEnumData::GetEnumMemberNameByIndex( int idx
)
98 // we should cache the count in order to avoid out-of-bounds errors
99 return m_members
[idx
].m_name
;
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 template<> const wxTypeInfo
* wxGetTypeInfo( void * )
108 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
112 template<> const wxTypeInfo
* wxGetTypeInfo( bool * )
114 static wxBuiltInTypeInfo
s_typeInfo( wxT_BOOL
) ;
118 template<> const wxTypeInfo
* wxGetTypeInfo( char * )
120 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
124 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned char * )
126 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
130 template<> const wxTypeInfo
* wxGetTypeInfo( int * )
132 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
136 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned int * )
138 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
142 template<> const wxTypeInfo
* wxGetTypeInfo( long * )
144 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
148 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned long * )
150 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
154 template<> const wxTypeInfo
* wxGetTypeInfo( float * )
156 static wxBuiltInTypeInfo
s_typeInfo( wxT_FLOAT
) ;
160 template<> const wxTypeInfo
* wxGetTypeInfo( double * )
162 static wxBuiltInTypeInfo
s_typeInfo( wxT_DOUBLE
) ;
166 template<> const wxTypeInfo
* wxGetTypeInfo( wxString
* )
168 static wxBuiltInTypeInfo
s_typeInfo( wxT_STRING
) ;
172 // this are compiler induced specialization which are never used anywhere
174 WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
175 WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
176 WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
177 WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
178 WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
179 WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
180 WX_ILLEGAL_TYPE_SPECIALIZATION( wxString
* )
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 // streamer specializations
187 // for all built-in types
191 template<> void wxStringReadValue(const wxString
&s
, bool &data
)
194 wxSscanf(s
, _T("%d"), &intdata
) ;
195 data
= bool(intdata
) ;
198 template<> void wxStringWriteValue(wxString
&s
, const bool &data
)
200 s
= wxString::Format("%d", data
) ;
205 template<> void wxStringReadValue(const wxString
&s
, char &data
)
208 wxSscanf(s
, _T("%d"), &intdata
) ;
209 data
= char(intdata
) ;
212 template<> void wxStringWriteValue(wxString
&s
, const char &data
)
214 s
= wxString::Format("%d", data
) ;
219 template<> void wxStringReadValue(const wxString
&s
, unsigned char &data
)
222 wxSscanf(s
, _T("%d"), &intdata
) ;
223 data
= (unsigned char)(intdata
) ;
226 template<> void wxStringWriteValue(wxString
&s
, const unsigned char &data
)
228 s
= wxString::Format("%d", data
) ;
233 template<> void wxStringReadValue(const wxString
&s
, int &data
)
235 wxSscanf(s
, _T("%d"), &data
) ;
238 template<> void wxStringWriteValue(wxString
&s
, const int &data
)
240 s
= wxString::Format("%d", data
) ;
245 template<> void wxStringReadValue(const wxString
&s
, unsigned int &data
)
247 wxSscanf(s
, _T("%d"), &data
) ;
250 template<> void wxStringWriteValue(wxString
&s
, const unsigned int &data
)
252 s
= wxString::Format("%d", data
) ;
257 template<> void wxStringReadValue(const wxString
&s
, long &data
)
259 wxSscanf(s
, _T("%ld"), &data
) ;
262 template<> void wxStringWriteValue(wxString
&s
, const long &data
)
264 s
= wxString::Format("%ld", data
) ;
269 template<> void wxStringReadValue(const wxString
&s
, unsigned long &data
)
271 wxSscanf(s
, _T("%ld"), &data
) ;
274 template<> void wxStringWriteValue(wxString
&s
, const unsigned long &data
)
276 s
= wxString::Format("%ld", data
) ;
281 template<> void wxStringReadValue(const wxString
&s
, float &data
)
283 wxSscanf(s
, _T("%f"), &data
) ;
286 template<> void wxStringWriteValue(wxString
&s
, const float &data
)
288 s
= wxString::Format("%f", data
) ;
293 template<> void wxStringReadValue(const wxString
&s
, double &data
)
295 wxSscanf(s
, _T("%lf"), &data
) ;
298 template<> void wxStringWriteValue(wxString
&s
, const double &data
)
300 s
= wxString::Format("%lf", data
) ;
305 template<> void wxStringReadValue(const wxString
&s
, wxString
&data
)
310 template<> void wxStringWriteValue(wxString
&s
, const wxString
&data
)
316 Custom Data Streaming / Type Infos
317 we will have to add this for all wx non object types, but it is also an example
318 for custom data structures
323 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
325 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
328 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
330 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
333 template<> void wxStringReadValue(const wxString
& , wxPoint
* & )
338 template<> void wxStringWriteValue(wxString
& , wxPoint
* const & )
343 WX_CUSTOM_TYPE_INFO(wxPoint
)
345 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
347 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
350 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
352 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
355 template<> void wxStringReadValue(const wxString
& , wxSize
* & )
360 template<> void wxStringWriteValue(wxString
& , wxSize
* const & )
365 WX_CUSTOM_TYPE_INFO(wxSize
)
369 template<> void wxStringReadValue(const wxString &s , wxColour &data )
371 // copied from VS xrc
372 unsigned long tmp = 0;
374 if (s.Length() != 7 || s[0u] != wxT('#') ||
375 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
377 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
383 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
384 (unsigned char) ((tmp & 0x00FF00) >> 8),
385 (unsigned char) ((tmp & 0x0000FF)));
389 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
391 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
394 WX_CUSTOM_TYPE_INFO(wxColour)
398 // removing header dependancy on string tokenizer
400 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
402 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
405 while (tokenizer
.HasMoreTokens())
407 array
.Add(tokenizer
.GetNextToken()) ;
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
) const
417 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
420 return info
->GetAccessor() ;
425 const wxPropertyInfo
*wxClassInfo::FindPropertyInfoInThisClass (const char *PropertyName
) const
427 const wxPropertyInfo
* info
= GetFirstProperty() ;
431 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
433 info
= info
->GetNext() ;
439 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const char *PropertyName
) const
441 const wxPropertyInfo
* info
= FindPropertyInfoInThisClass( PropertyName
) ;
445 const wxClassInfo
** parents
= GetParents() ;
446 for ( int i
= 0 ; parents
[i
] ; ++ i
)
448 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
455 const wxHandlerInfo
*wxClassInfo::FindHandlerInfoInThisClass (const char *PropertyName
) const
457 const wxHandlerInfo
* info
= GetFirstHandler() ;
461 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
463 info
= info
->GetNext() ;
469 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const char *PropertyName
) const
471 const wxHandlerInfo
* info
= FindHandlerInfoInThisClass( PropertyName
) ;
476 const wxClassInfo
** parents
= GetParents() ;
477 for ( int i
= 0 ; parents
[i
] ; ++ i
)
479 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
487 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
) const
489 const wxPropertyAccessor
*accessor
;
491 accessor
= FindAccessor(propertyName
);
492 wxASSERT(accessor
->HasSetter());
493 accessor
->SetProperty( object
, value
) ;
496 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
) const
498 const wxPropertyAccessor
*accessor
;
500 accessor
= FindAccessor(propertyName
);
501 wxASSERT(accessor
->HasGetter());
502 return accessor
->GetProperty(object
);
509 wxObject
* wxxVariant::GetAsObject()
511 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
513 return ti
->GetClassInfo()->VariantToInstance(*this) ;
518 // ----------------------------------------------------------------------------
519 // wxDynamicObject support
520 // ----------------------------------------------------------------------------
522 // Dynamic Objects are objects that have a real superclass instance and carry their
523 // own attributes in a hash map. Like this it is possible to create the objects and
524 // stream them, as if their class information was already available from compiled data
526 struct wxDynamicObject::wxDynamicObjectInternal
528 map
<string
,wxxVariant
> m_properties
;
531 // instantiates this object with an instance of its superclass
532 wxDynamicObject::wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
)
534 m_superClassInstance
= superClassInstance
;
536 m_data
= new wxDynamicObjectInternal
;
539 wxDynamicObject::~wxDynamicObject()
542 delete m_superClassInstance
;
545 void wxDynamicObject::SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
)
547 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
548 m_data
->m_properties
[propertyName
] = value
;
551 wxxVariant
wxDynamicObject::GetProperty (const wxChar
*propertyName
) const
553 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
554 return m_data
->m_properties
[propertyName
] ;
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
561 wxDynamicClassInfo::wxDynamicClassInfo( const wxChar
*unitName
, const wxChar
*className
, const wxClassInfo
* superClass
) :
562 wxClassInfo( unitName
, className
, new const wxClassInfo
*[2])
564 GetParents()[0] = superClass
;
565 GetParents()[1] = NULL
;
568 wxDynamicClassInfo::~wxDynamicClassInfo()
570 delete[] GetParents() ;
573 wxObject
*wxDynamicClassInfo::CreateObject() const
575 wxObject
* parent
= GetParents()[0]->CreateObject() ;
576 return new wxDynamicObject( parent
, this ) ;
579 void wxDynamicClassInfo::Create (wxObject
*object
, int paramCount
, wxxVariant
*params
) const
581 wxDynamicObject
*dynobj
= dynamic_cast< wxDynamicObject
*>( object
) ;
582 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::Create on an object other than wxDynamicObject") ) ;
583 GetParents()[0]->Create( dynobj
->GetSuperClassInstance() , paramCount
, params
) ;
586 // get number of parameters for constructor
587 int wxDynamicClassInfo::GetCreateParamCount() const
589 return GetParents()[0]->GetCreateParamCount() ;
592 // get i-th constructor parameter
593 const wxChar
* wxDynamicClassInfo::GetCreateParamName(int i
) const
595 return GetParents()[0]->GetCreateParamName( i
) ;
598 void wxDynamicClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
) const
600 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
601 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
602 if ( FindPropertyInfoInThisClass(propertyName
) )
603 dynobj
->SetProperty( propertyName
, value
) ;
605 GetParents()[0]->SetProperty( dynobj
->GetSuperClassInstance() , propertyName
, value
) ;
608 wxxVariant
wxDynamicClassInfo::GetProperty(wxObject
*object
, const char *propertyName
) const
610 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
611 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
612 if ( FindPropertyInfoInThisClass(propertyName
) )
613 return dynobj
->GetProperty( propertyName
) ;
615 return GetParents()[0]->GetProperty( dynobj
->GetSuperClassInstance() , propertyName
) ;
618 void wxDynamicClassInfo::AddProperty( const wxChar
*propertyName
, const wxTypeInfo
* typeInfo
)
620 new wxPropertyInfo( m_firstProperty
, propertyName
, "" , typeInfo
, new wxGenericPropertyAccessor( propertyName
) , wxxVariant() ) ;
623 void wxDynamicClassInfo::AddHandler( const wxChar
*handlerName
, wxObjectEventFunction address
, const wxClassInfo
* eventClassInfo
)
625 new wxHandlerInfo( m_firstHandler
, handlerName
, address
, eventClassInfo
) ;
628 // ----------------------------------------------------------------------------
629 // wxGenericPropertyAccessor
630 // ----------------------------------------------------------------------------
632 struct wxGenericPropertyAccessor::wxGenericPropertyAccessorInternal
634 wxString m_propertyName
;
637 wxGenericPropertyAccessor::wxGenericPropertyAccessor( const wxChar
* propertyName
)
639 m_data
= new wxGenericPropertyAccessorInternal
;
640 m_data
->m_propertyName
= propertyName
;
643 wxGenericPropertyAccessor::~wxGenericPropertyAccessor()
647 void wxGenericPropertyAccessor::SetProperty(wxObject
*object
, const wxxVariant
&value
) const
649 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
650 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
651 dynobj
->SetProperty(m_data
->m_propertyName
, value
) ;
654 wxxVariant
wxGenericPropertyAccessor::GetProperty(const wxObject
*object
) const
656 const wxDynamicObject
* dynobj
= dynamic_cast< const wxDynamicObject
* >( object
) ;
657 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
658 return dynobj
->GetProperty( m_data
->m_propertyName
) ;
661 wxxVariant
wxGenericPropertyAccessor::ReadValue( const wxString
&value
) const
663 return wxxVariant(value
) ;
666 void wxGenericPropertyAccessor::WriteValue( wxString
& value
, const wxObject
*object
) const
668 const wxDynamicObject
* dynobj
= dynamic_cast< const wxDynamicObject
* >( object
) ;
669 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
670 wxxVariant val
= dynobj
->GetProperty( m_data
->m_propertyName
) ;
671 value
= val
.GetAsString() ;