]>
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 /////////////////////////////////////////////////////////////////////////////
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
171 template<> const wxTypeInfo
* wxGetTypeInfo( char const ** )
174 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
178 template<> void wxStringReadValue(const wxString
& , const char* & )
183 template<> void wxStringWriteValue(wxString
& , char const * const & )
190 template<> const wxTypeInfo
* wxGetTypeInfo( char ** )
193 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
197 template<> void wxStringReadValue(const wxString
& , char* & )
202 template<> void wxStringWriteValue(wxString
& , char * const & )
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 // convenience function (avoids including xml headers in users code)
213 void wxXmlAddContentToNode( wxXmlNode
* node
, const wxString
& data
)
215 node
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, "value", data
) );
218 wxString
wxXmlGetContentFromNode( wxXmlNode
*node
)
220 if ( node
->GetChildren() )
221 return node
->GetChildren()->GetContent() ;
223 return wxEmptyString
;
226 // streamer specializations
227 // for all built-in types
231 template<> void wxStringReadValue(const wxString
&s
, bool &data
)
234 wxSscanf(s
, _T("%d"), &intdata
) ;
235 data
= bool(intdata
) ;
238 template<> void wxStringWriteValue(wxString
&s
, const bool &data
)
240 s
= wxString::Format("%d", data
) ;
245 template<> void wxStringReadValue(const wxString
&s
, char &data
)
248 wxSscanf(s
, _T("%d"), &intdata
) ;
249 data
= char(intdata
) ;
252 template<> void wxStringWriteValue(wxString
&s
, const char &data
)
254 s
= wxString::Format("%d", data
) ;
259 template<> void wxStringReadValue(const wxString
&s
, unsigned char &data
)
262 wxSscanf(s
, _T("%d"), &intdata
) ;
263 data
= (unsigned char)(intdata
) ;
266 template<> void wxStringWriteValue(wxString
&s
, const unsigned char &data
)
268 s
= wxString::Format("%d", data
) ;
273 template<> void wxStringReadValue(const wxString
&s
, int &data
)
275 wxSscanf(s
, _T("%d"), &data
) ;
278 template<> void wxStringWriteValue(wxString
&s
, const int &data
)
280 s
= wxString::Format("%d", data
) ;
285 template<> void wxStringReadValue(const wxString
&s
, unsigned int &data
)
287 wxSscanf(s
, _T("%d"), &data
) ;
290 template<> void wxStringWriteValue(wxString
&s
, const unsigned int &data
)
292 s
= wxString::Format("%d", data
) ;
297 template<> void wxStringReadValue(const wxString
&s
, long &data
)
299 wxSscanf(s
, _T("%ld"), &data
) ;
302 template<> void wxStringWriteValue(wxString
&s
, const long &data
)
304 s
= wxString::Format("%ld", data
) ;
309 template<> void wxStringReadValue(const wxString
&s
, unsigned long &data
)
311 wxSscanf(s
, _T("%ld"), &data
) ;
314 template<> void wxStringWriteValue(wxString
&s
, const unsigned long &data
)
316 s
= wxString::Format("%ld", data
) ;
321 template<> void wxStringReadValue(const wxString
&s
, float &data
)
323 wxSscanf(s
, _T("%f"), &data
) ;
326 template<> void wxStringWriteValue(wxString
&s
, const float &data
)
328 s
= wxString::Format("%f", data
) ;
333 template<> void wxStringReadValue(const wxString
&s
, double &data
)
335 wxSscanf(s
, _T("%lf"), &data
) ;
338 template<> void wxStringWriteValue(wxString
&s
, const double &data
)
340 s
= wxString::Format("%lf", data
) ;
345 template<> void wxStringReadValue(const wxString
&s
, wxString
&data
)
350 template<> void wxStringWriteValue(wxString
&s
, const wxString
&data
)
356 Custom Data Streaming / Type Infos
357 we will have to add this for all wx non object types, but it is also an example
358 for custom data structures
363 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
365 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
368 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
370 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
373 WX_CUSTOM_TYPE_INFO(wxPoint
)
375 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
377 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
380 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
382 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
385 WX_CUSTOM_TYPE_INFO(wxSize
)
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 // ----------------------------------------------------------------------------
436 void wxClassInfo::Register(const char *WXUNUSED(name
), wxClassInfo
*WXUNUSED(info
))
439 if (!ExtendedTypeMap)
440 ExtendedTypeMap = new ClassMap;
441 (*ExtendedTypeMap)[string(Name)] = Info;
445 void wxClassInfo::Unregister(const char *WXUNUSED(name
))
448 assert(ExtendedTypeMap);
449 ExtendedTypeMap->erase(Name);
453 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
)
455 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
458 return info
->GetAccessor() ;
463 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const char *PropertyName
) const
465 const wxPropertyInfo
* info
= GetFirstProperty() ;
469 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
471 info
= info
->GetNext() ;
474 const wxClassInfo
** parents
= GetParents() ;
475 for ( int i
= 0 ; parents
[i
] ; ++ i
)
477 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
484 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const char *PropertyName
) const
486 const wxHandlerInfo
* info
= GetFirstHandler() ;
490 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
492 info
= info
->GetNext() ;
495 const wxClassInfo
** parents
= GetParents() ;
496 for ( int i
= 0 ; parents
[i
] ; ++ i
)
498 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
506 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
)
508 const wxPropertyAccessor
*accessor
;
510 accessor
= FindAccessor(propertyName
);
511 wxASSERT(accessor
->HasSetter());
512 accessor
->SetProperty( object
, value
) ;
515 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
)
517 const wxPropertyAccessor
*accessor
;
519 accessor
= FindAccessor(propertyName
);
520 wxASSERT(accessor
->HasGetter());
521 return accessor
->GetProperty(object
);
528 wxObject
* wxxVariant::GetAsObject()
530 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
532 return ti
->GetClassInfo()->VariantToInstance(*this) ;