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"
31 #include "wx/notebook.h"
35 #if wxUSE_EXTENDED_RTTI
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 (!strcmp(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 (!strcmp(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 char * 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("%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("%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("%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("%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("%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("%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("%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("%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("%lf", data
) ;
231 template<> void wxStringReadValue(const wxString
&s
, wxString
&data
)
236 template<> void wxStringWriteValue(wxString
&s
, const wxString
&data
)
242 Custom Data Streaming / Type Infos
243 we will have to add this for all wx non object types, but it is also an example
244 for custom data structures
249 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
251 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
254 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
256 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
259 template<> void wxStringReadValue(const wxString
& , wxPoint
* & )
264 template<> void wxStringWriteValue(wxString
& , wxPoint
* const & )
269 WX_CUSTOM_TYPE_INFO(wxPoint
)
271 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
273 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
276 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
278 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
281 template<> void wxStringReadValue(const wxString
& , wxSize
* & )
286 template<> void wxStringWriteValue(wxString
& , wxSize
* const & )
291 WX_CUSTOM_TYPE_INFO(wxSize
)
294 template<> const wxTypeInfo
* wxGetTypeInfo( void * )
296 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
300 template<> const wxTypeInfo
* wxGetTypeInfo( bool * )
302 static wxBuiltInTypeInfo
s_typeInfo( wxT_BOOL
, &wxToStringConverter
<bool> , &wxFromStringConverter
<bool>) ;
306 template<> const wxTypeInfo
* wxGetTypeInfo( char * )
308 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
, &wxToStringConverter
<char> , &wxFromStringConverter
<char>) ;
312 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned char * )
314 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
, &wxToStringConverter
< unsigned char > , &wxFromStringConverter
<unsigned char>) ;
318 template<> const wxTypeInfo
* wxGetTypeInfo( int * )
320 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
, &wxToStringConverter
<int> , &wxFromStringConverter
<int>) ;
324 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned int * )
326 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
, &wxToStringConverter
<unsigned int> , &wxFromStringConverter
<unsigned int>) ;
330 template<> const wxTypeInfo
* wxGetTypeInfo( long * )
332 static wxBuiltInTypeInfo
s_typeInfo( wxT_LONG
, &wxToStringConverter
<long> , &wxFromStringConverter
<long>) ;
336 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned long * )
338 static wxBuiltInTypeInfo
s_typeInfo( wxT_ULONG
, &wxToStringConverter
<unsigned long> , &wxFromStringConverter
<unsigned long>) ;
342 template<> const wxTypeInfo
* wxGetTypeInfo( float * )
344 static wxBuiltInTypeInfo
s_typeInfo( wxT_FLOAT
, &wxToStringConverter
<float> , &wxFromStringConverter
<float>) ;
348 template<> const wxTypeInfo
* wxGetTypeInfo( double * )
350 static wxBuiltInTypeInfo
s_typeInfo( wxT_DOUBLE
, &wxToStringConverter
<double> , &wxFromStringConverter
<double>) ;
354 template<> const wxTypeInfo
* wxGetTypeInfo( wxString
* )
356 static wxBuiltInTypeInfo
s_typeInfo( wxT_STRING
, &wxToStringConverter
<wxString
> , &wxFromStringConverter
<wxString
>) ;
360 // this are compiler induced specialization which are never used anywhere
362 WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
363 WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
364 WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
365 WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
366 WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
367 WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
368 WX_ILLEGAL_TYPE_SPECIALIZATION( wxString
* )
372 // make wxWindowList known
374 template<> const wxTypeInfo
* wxGetTypeInfo( wxArrayString
* )
376 static wxCollectionTypeInfo
s_typeInfo( (wxTypeInfo
*) wxGetTypeInfo( (wxString
*) NULL
) ) ;
380 template<> void wxCollectionToVariantArray( wxArrayString
const &theArray
, wxxVariantArray
&value
)
382 wxArrayCollectionToVariantArray( theArray
, value
) ;
389 template<> void wxStringReadValue(const wxString &s , wxColour &data )
391 // copied from VS xrc
392 unsigned long tmp = 0;
394 if (s.Length() != 7 || s[0u] != wxT('#') ||
395 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
397 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
403 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
404 (unsigned char) ((tmp & 0x00FF00) >> 8),
405 (unsigned char) ((tmp & 0x0000FF)));
409 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
411 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
414 WX_CUSTOM_TYPE_INFO(wxColour)
418 // removing header dependancy on string tokenizer
420 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
422 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
425 while (tokenizer
.HasMoreTokens())
427 array
.Add(tokenizer
.GetNextToken()) ;
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
) const
437 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
440 return info
->GetAccessor() ;
445 const wxPropertyInfo
*wxClassInfo::FindPropertyInfoInThisClass (const char *PropertyName
) const
447 const wxPropertyInfo
* info
= GetFirstProperty() ;
451 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
453 info
= info
->GetNext() ;
459 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const char *PropertyName
) const
461 const wxPropertyInfo
* info
= FindPropertyInfoInThisClass( PropertyName
) ;
465 const wxClassInfo
** parents
= GetParents() ;
466 for ( int i
= 0 ; parents
[i
] ; ++ i
)
468 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
475 const wxHandlerInfo
*wxClassInfo::FindHandlerInfoInThisClass (const char *PropertyName
) const
477 const wxHandlerInfo
* info
= GetFirstHandler() ;
481 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
483 info
= info
->GetNext() ;
489 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const char *PropertyName
) const
491 const wxHandlerInfo
* info
= FindHandlerInfoInThisClass( PropertyName
) ;
496 const wxClassInfo
** parents
= GetParents() ;
497 for ( int i
= 0 ; parents
[i
] ; ++ i
)
499 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
507 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
) const
509 const wxPropertyAccessor
*accessor
;
511 accessor
= FindAccessor(propertyName
);
512 wxASSERT(accessor
->HasSetter());
513 accessor
->SetProperty( object
, value
) ;
516 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
) const
518 const wxPropertyAccessor
*accessor
;
520 accessor
= FindAccessor(propertyName
);
521 wxASSERT(accessor
->HasGetter());
523 accessor
->GetProperty(object
,result
);
527 wxxVariantArray
wxClassInfo::GetPropertyCollection(wxObject
*object
, const wxChar
*propertyName
) const
529 const wxPropertyAccessor
*accessor
;
531 accessor
= FindAccessor(propertyName
);
532 wxASSERT(accessor
->HasGetter());
533 wxxVariantArray result
;
534 accessor
->GetPropertyCollection(object
,result
);
538 void wxClassInfo::AddToPropertyCollection(wxObject
*object
, const wxChar
*propertyName
, const wxxVariant
& value
) const
540 const wxPropertyAccessor
*accessor
;
542 accessor
= FindAccessor(propertyName
);
543 wxASSERT(accessor
->HasAdder());
544 accessor
->AddToPropertyCollection( object
, value
) ;
551 wxObject
* wxxVariant::GetAsObject()
553 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
555 return ti
->GetClassInfo()->VariantToInstance(*this) ;
560 // ----------------------------------------------------------------------------
561 // wxDynamicObject support
562 // ----------------------------------------------------------------------------
564 // Dynamic Objects are objects that have a real superclass instance and carry their
565 // own attributes in a hash map. Like this it is possible to create the objects and
566 // stream them, as if their class information was already available from compiled data
568 struct wxDynamicObject::wxDynamicObjectInternal
570 map
<string
,wxxVariant
> m_properties
;
573 // instantiates this object with an instance of its superclass
574 wxDynamicObject::wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
)
576 m_superClassInstance
= superClassInstance
;
578 m_data
= new wxDynamicObjectInternal
;
581 wxDynamicObject::~wxDynamicObject()
584 delete m_superClassInstance
;
587 void wxDynamicObject::SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
)
589 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
590 m_data
->m_properties
[propertyName
] = value
;
593 wxxVariant
wxDynamicObject::GetProperty (const wxChar
*propertyName
) const
595 wxASSERT_MSG(m_classInfo
->FindPropertyInfoInThisClass(propertyName
),wxT("Accessing Unknown Property in a Dynamic Object") ) ;
596 return m_data
->m_properties
[propertyName
] ;
599 // ----------------------------------------------------------------------------
601 // ----------------------------------------------------------------------------
603 wxDynamicClassInfo::wxDynamicClassInfo( const wxChar
*unitName
, const wxChar
*className
, const wxClassInfo
* superClass
) :
604 wxClassInfo( unitName
, className
, new const wxClassInfo
*[2])
606 GetParents()[0] = superClass
;
607 GetParents()[1] = NULL
;
610 wxDynamicClassInfo::~wxDynamicClassInfo()
612 delete[] GetParents() ;
615 wxObject
*wxDynamicClassInfo::AllocateObject() const
617 wxObject
* parent
= GetParents()[0]->AllocateObject() ;
618 return new wxDynamicObject( parent
, this ) ;
621 void wxDynamicClassInfo::Create (wxObject
*object
, int paramCount
, wxxVariant
*params
) const
623 wxDynamicObject
*dynobj
= dynamic_cast< wxDynamicObject
*>( object
) ;
624 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::Create on an object other than wxDynamicObject") ) ;
625 GetParents()[0]->Create( dynobj
->GetSuperClassInstance() , paramCount
, params
) ;
628 // get number of parameters for constructor
629 int wxDynamicClassInfo::GetCreateParamCount() const
631 return GetParents()[0]->GetCreateParamCount() ;
634 // get i-th constructor parameter
635 const wxChar
* wxDynamicClassInfo::GetCreateParamName(int i
) const
637 return GetParents()[0]->GetCreateParamName( i
) ;
640 void wxDynamicClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
) const
642 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
643 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
644 if ( FindPropertyInfoInThisClass(propertyName
) )
645 dynobj
->SetProperty( propertyName
, value
) ;
647 GetParents()[0]->SetProperty( dynobj
->GetSuperClassInstance() , propertyName
, value
) ;
650 wxxVariant
wxDynamicClassInfo::GetProperty(wxObject
*object
, const char *propertyName
) const
652 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
653 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
654 if ( FindPropertyInfoInThisClass(propertyName
) )
655 return dynobj
->GetProperty( propertyName
) ;
657 return GetParents()[0]->GetProperty( dynobj
->GetSuperClassInstance() , propertyName
) ;
660 void wxDynamicClassInfo::AddProperty( const wxChar
*propertyName
, const wxTypeInfo
* typeInfo
)
662 new wxPropertyInfo( m_firstProperty
, propertyName
, typeInfo
, new wxGenericPropertyAccessor( propertyName
) , wxxVariant() ) ;
665 void wxDynamicClassInfo::AddHandler( const wxChar
*handlerName
, wxObjectEventFunction address
, const wxClassInfo
* eventClassInfo
)
667 new wxHandlerInfo( m_firstHandler
, handlerName
, address
, eventClassInfo
) ;
670 // ----------------------------------------------------------------------------
671 // wxGenericPropertyAccessor
672 // ----------------------------------------------------------------------------
674 struct wxGenericPropertyAccessor::wxGenericPropertyAccessorInternal
679 wxGenericPropertyAccessor::wxGenericPropertyAccessor( const wxString
& propertyName
)
680 : wxPropertyAccessor( NULL
, NULL
, NULL
, NULL
)
682 m_data
= new wxGenericPropertyAccessorInternal
;
683 m_propertyName
= propertyName
;
684 m_getterName
= wxT("Get")+propertyName
;
685 m_setterName
= wxT("Set")+propertyName
;
688 wxGenericPropertyAccessor::~wxGenericPropertyAccessor()
692 void wxGenericPropertyAccessor::SetProperty(wxObject
*object
, const wxxVariant
&value
) const
694 wxDynamicObject
* dynobj
= dynamic_cast< wxDynamicObject
* >( object
) ;
695 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
696 dynobj
->SetProperty(m_propertyName
, value
) ;
699 void wxGenericPropertyAccessor::GetProperty(const wxObject
*object
, wxxVariant
& value
) const
701 const wxDynamicObject
* dynobj
= dynamic_cast< const wxDynamicObject
* >( object
) ;
702 wxASSERT_MSG( dynobj
, wxT("cannot call wxDynamicClassInfo::SetProperty on an object other than wxDynamicObject") ) ;
703 value
= dynobj
->GetProperty( m_propertyName
) ;