]>
git.saurik.com Git - wxWidgets.git/blob - src/common/xti.cpp
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
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 wxEnumData::wxEnumData( wxEnumMemberData
* data
)
43 for ( m_count
= 0; m_members
[m_count
].m_name
; m_count
++)
47 bool wxEnumData::HasEnumMemberValue(const wxChar
*name
, int *value
)
50 for (i
= 0; m_members
[i
].m_name
; i
++ )
52 if (!strcmp(name
, m_members
[i
].m_name
))
55 *value
= m_members
[i
].m_value
;
62 int wxEnumData::GetEnumMemberValue(const wxChar
*name
)
65 for (i
= 0; m_members
[i
].m_name
; i
++ )
67 if (!strcmp(name
, m_members
[i
].m_name
))
69 return m_members
[i
].m_value
;
75 const wxChar
*wxEnumData::GetEnumMemberName(int value
)
78 for (i
= 0; m_members
[i
].m_name
; i
++)
79 if (value
== m_members
[i
].m_value
)
80 return m_members
[i
].m_name
;
85 int wxEnumData::GetEnumMemberValueByIndex( int idx
)
87 // we should cache the count in order to avoid out-of-bounds errors
88 return m_members
[idx
].m_value
;
91 const char * wxEnumData::GetEnumMemberNameByIndex( int idx
)
93 // we should cache the count in order to avoid out-of-bounds errors
94 return m_members
[idx
].m_name
;
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 template<> const wxTypeInfo
* wxGetTypeInfo( void * )
103 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
107 template<> const wxTypeInfo
* wxGetTypeInfo( bool * )
109 static wxBuiltInTypeInfo
s_typeInfo( wxT_BOOL
) ;
113 template<> const wxTypeInfo
* wxGetTypeInfo( char * )
115 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
119 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned char * )
121 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
125 template<> const wxTypeInfo
* wxGetTypeInfo( int * )
127 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
131 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned int * )
133 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
137 template<> const wxTypeInfo
* wxGetTypeInfo( long * )
139 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
143 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned long * )
145 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
149 template<> const wxTypeInfo
* wxGetTypeInfo( float * )
151 static wxBuiltInTypeInfo
s_typeInfo( wxT_FLOAT
) ;
155 template<> const wxTypeInfo
* wxGetTypeInfo( double * )
157 static wxBuiltInTypeInfo
s_typeInfo( wxT_DOUBLE
) ;
161 template<> const wxTypeInfo
* wxGetTypeInfo( wxString
* )
163 static wxBuiltInTypeInfo
s_typeInfo( wxT_STRING
) ;
167 // this are compiler induced specialization which are never used anywhere
169 WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
170 WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
171 WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
172 WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
173 WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
174 WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
175 WX_ILLEGAL_TYPE_SPECIALIZATION( wxString
* )
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 // streamer specializations
182 // for all built-in types
186 template<> void wxStringReadValue(const wxString
&s
, bool &data
)
189 wxSscanf(s
, _T("%d"), &intdata
) ;
190 data
= bool(intdata
) ;
193 template<> void wxStringWriteValue(wxString
&s
, const bool &data
)
195 s
= wxString::Format("%d", data
) ;
200 template<> void wxStringReadValue(const wxString
&s
, char &data
)
203 wxSscanf(s
, _T("%d"), &intdata
) ;
204 data
= char(intdata
) ;
207 template<> void wxStringWriteValue(wxString
&s
, const char &data
)
209 s
= wxString::Format("%d", data
) ;
214 template<> void wxStringReadValue(const wxString
&s
, unsigned char &data
)
217 wxSscanf(s
, _T("%d"), &intdata
) ;
218 data
= (unsigned char)(intdata
) ;
221 template<> void wxStringWriteValue(wxString
&s
, const unsigned char &data
)
223 s
= wxString::Format("%d", data
) ;
228 template<> void wxStringReadValue(const wxString
&s
, int &data
)
230 wxSscanf(s
, _T("%d"), &data
) ;
233 template<> void wxStringWriteValue(wxString
&s
, const int &data
)
235 s
= wxString::Format("%d", data
) ;
240 template<> void wxStringReadValue(const wxString
&s
, unsigned int &data
)
242 wxSscanf(s
, _T("%d"), &data
) ;
245 template<> void wxStringWriteValue(wxString
&s
, const unsigned int &data
)
247 s
= wxString::Format("%d", data
) ;
252 template<> void wxStringReadValue(const wxString
&s
, long &data
)
254 wxSscanf(s
, _T("%ld"), &data
) ;
257 template<> void wxStringWriteValue(wxString
&s
, const long &data
)
259 s
= wxString::Format("%ld", data
) ;
264 template<> void wxStringReadValue(const wxString
&s
, unsigned long &data
)
266 wxSscanf(s
, _T("%ld"), &data
) ;
269 template<> void wxStringWriteValue(wxString
&s
, const unsigned long &data
)
271 s
= wxString::Format("%ld", data
) ;
276 template<> void wxStringReadValue(const wxString
&s
, float &data
)
278 wxSscanf(s
, _T("%f"), &data
) ;
281 template<> void wxStringWriteValue(wxString
&s
, const float &data
)
283 s
= wxString::Format("%f", data
) ;
288 template<> void wxStringReadValue(const wxString
&s
, double &data
)
290 wxSscanf(s
, _T("%lf"), &data
) ;
293 template<> void wxStringWriteValue(wxString
&s
, const double &data
)
295 s
= wxString::Format("%lf", data
) ;
300 template<> void wxStringReadValue(const wxString
&s
, wxString
&data
)
305 template<> void wxStringWriteValue(wxString
&s
, const wxString
&data
)
311 Custom Data Streaming / Type Infos
312 we will have to add this for all wx non object types, but it is also an example
313 for custom data structures
318 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
320 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
323 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
325 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
328 template<> void wxStringReadValue(const wxString
& , wxPoint
* & )
333 template<> void wxStringWriteValue(wxString
& , wxPoint
* const & )
338 WX_CUSTOM_TYPE_INFO(wxPoint
)
340 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
342 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
345 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
347 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
350 template<> void wxStringReadValue(const wxString
& , wxSize
* & )
355 template<> void wxStringWriteValue(wxString
& , wxSize
* const & )
360 WX_CUSTOM_TYPE_INFO(wxSize
)
364 template<> void wxStringReadValue(const wxString &s , wxColour &data )
366 // copied from VS xrc
367 unsigned long tmp = 0;
369 if (s.Length() != 7 || s[0u] != wxT('#') ||
370 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
372 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
378 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
379 (unsigned char) ((tmp & 0x00FF00) >> 8),
380 (unsigned char) ((tmp & 0x0000FF)));
384 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
386 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
389 WX_CUSTOM_TYPE_INFO(wxColour)
393 // removing header dependancy on string tokenizer
395 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
397 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
400 while (tokenizer
.HasMoreTokens())
402 array
.Add(tokenizer
.GetNextToken()) ;
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
)
412 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
415 return info
->GetAccessor() ;
420 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const char *PropertyName
) const
422 const wxPropertyInfo
* info
= GetFirstProperty() ;
426 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
428 info
= info
->GetNext() ;
431 const wxClassInfo
** parents
= GetParents() ;
432 for ( int i
= 0 ; parents
[i
] ; ++ i
)
434 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
441 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const char *PropertyName
) const
443 const wxHandlerInfo
* info
= GetFirstHandler() ;
447 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
449 info
= info
->GetNext() ;
452 const wxClassInfo
** parents
= GetParents() ;
453 for ( int i
= 0 ; parents
[i
] ; ++ i
)
455 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
463 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
)
465 const wxPropertyAccessor
*accessor
;
467 accessor
= FindAccessor(propertyName
);
468 wxASSERT(accessor
->HasSetter());
469 accessor
->SetProperty( object
, value
) ;
472 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
)
474 const wxPropertyAccessor
*accessor
;
476 accessor
= FindAccessor(propertyName
);
477 wxASSERT(accessor
->HasGetter());
478 return accessor
->GetProperty(object
);
485 wxObject
* wxxVariant::GetAsObject()
487 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
489 return ti
->GetClassInfo()->VariantToInstance(*this) ;