]>
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 & )
209 template<> const wxTypeInfo
* wxGetTypeInfo( unsigned char ** )
212 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
216 template<> void wxStringReadValue(const wxString
& , unsigned char* & )
221 template<> void wxStringWriteValue(wxString
& , unsigned char * const & )
228 template<> const wxTypeInfo
* wxGetTypeInfo( int ** )
231 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
235 template<> void wxStringReadValue(const wxString
& , int* & )
240 template<> void wxStringWriteValue(wxString
& , int * const & )
247 template<> const wxTypeInfo
* wxGetTypeInfo( bool ** )
250 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
254 template<> void wxStringReadValue(const wxString
& , bool* & )
259 template<> void wxStringWriteValue(wxString
& , bool * const & )
266 template<> const wxTypeInfo
* wxGetTypeInfo( long ** )
269 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
273 template<> void wxStringReadValue(const wxString
& , long* & )
278 template<> void wxStringWriteValue(wxString
& , long * const & )
285 template<> const wxTypeInfo
* wxGetTypeInfo( wxString
** )
288 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
292 template<> void wxStringReadValue(const wxString
& , wxString
* & )
297 template<> void wxStringWriteValue(wxString
& , wxString
* const & )
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 // convenience function (avoids including xml headers in users code)
309 void wxXmlAddContentToNode( wxXmlNode
* node
, const wxString
& data
)
311 node
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, "value", data
) );
314 wxString
wxXmlGetContentFromNode( wxXmlNode
*node
)
316 if ( node
->GetChildren() )
317 return node
->GetChildren()->GetContent() ;
319 return wxEmptyString
;
322 // streamer specializations
323 // for all built-in types
327 template<> void wxStringReadValue(const wxString
&s
, bool &data
)
330 wxSscanf(s
, _T("%d"), &intdata
) ;
331 data
= bool(intdata
) ;
334 template<> void wxStringWriteValue(wxString
&s
, const bool &data
)
336 s
= wxString::Format("%d", data
) ;
341 template<> void wxStringReadValue(const wxString
&s
, char &data
)
344 wxSscanf(s
, _T("%d"), &intdata
) ;
345 data
= char(intdata
) ;
348 template<> void wxStringWriteValue(wxString
&s
, const char &data
)
350 s
= wxString::Format("%d", data
) ;
355 template<> void wxStringReadValue(const wxString
&s
, unsigned char &data
)
358 wxSscanf(s
, _T("%d"), &intdata
) ;
359 data
= (unsigned char)(intdata
) ;
362 template<> void wxStringWriteValue(wxString
&s
, const unsigned char &data
)
364 s
= wxString::Format("%d", data
) ;
369 template<> void wxStringReadValue(const wxString
&s
, int &data
)
371 wxSscanf(s
, _T("%d"), &data
) ;
374 template<> void wxStringWriteValue(wxString
&s
, const int &data
)
376 s
= wxString::Format("%d", data
) ;
381 template<> void wxStringReadValue(const wxString
&s
, unsigned int &data
)
383 wxSscanf(s
, _T("%d"), &data
) ;
386 template<> void wxStringWriteValue(wxString
&s
, const unsigned int &data
)
388 s
= wxString::Format("%d", data
) ;
393 template<> void wxStringReadValue(const wxString
&s
, long &data
)
395 wxSscanf(s
, _T("%ld"), &data
) ;
398 template<> void wxStringWriteValue(wxString
&s
, const long &data
)
400 s
= wxString::Format("%ld", data
) ;
405 template<> void wxStringReadValue(const wxString
&s
, unsigned long &data
)
407 wxSscanf(s
, _T("%ld"), &data
) ;
410 template<> void wxStringWriteValue(wxString
&s
, const unsigned long &data
)
412 s
= wxString::Format("%ld", data
) ;
417 template<> void wxStringReadValue(const wxString
&s
, float &data
)
419 wxSscanf(s
, _T("%f"), &data
) ;
422 template<> void wxStringWriteValue(wxString
&s
, const float &data
)
424 s
= wxString::Format("%f", data
) ;
429 template<> void wxStringReadValue(const wxString
&s
, double &data
)
431 wxSscanf(s
, _T("%lf"), &data
) ;
434 template<> void wxStringWriteValue(wxString
&s
, const double &data
)
436 s
= wxString::Format("%lf", data
) ;
441 template<> void wxStringReadValue(const wxString
&s
, wxString
&data
)
446 template<> void wxStringWriteValue(wxString
&s
, const wxString
&data
)
452 Custom Data Streaming / Type Infos
453 we will have to add this for all wx non object types, but it is also an example
454 for custom data structures
459 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
461 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
464 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
466 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
469 template<> void wxStringReadValue(const wxString
& , wxPoint
* & )
474 template<> void wxStringWriteValue(wxString
& , wxPoint
* const & )
479 WX_CUSTOM_TYPE_INFO(wxPoint
)
481 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
483 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
486 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
488 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
491 template<> void wxStringReadValue(const wxString
& , wxSize
* & )
496 template<> void wxStringWriteValue(wxString
& , wxSize
* const & )
501 WX_CUSTOM_TYPE_INFO(wxSize
)
505 template<> void wxStringReadValue(const wxString &s , wxColour &data )
507 // copied from VS xrc
508 unsigned long tmp = 0;
510 if (s.Length() != 7 || s[0u] != wxT('#') ||
511 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
513 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
519 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
520 (unsigned char) ((tmp & 0x00FF00) >> 8),
521 (unsigned char) ((tmp & 0x0000FF)));
525 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
527 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
530 WX_CUSTOM_TYPE_INFO(wxColour)
534 // removing header dependancy on string tokenizer
536 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
538 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
541 while (tokenizer
.HasMoreTokens())
543 array
.Add(tokenizer
.GetNextToken()) ;
547 // ----------------------------------------------------------------------------
549 // ----------------------------------------------------------------------------
551 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
)
553 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
556 return info
->GetAccessor() ;
561 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const char *PropertyName
) const
563 const wxPropertyInfo
* info
= GetFirstProperty() ;
567 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
569 info
= info
->GetNext() ;
572 const wxClassInfo
** parents
= GetParents() ;
573 for ( int i
= 0 ; parents
[i
] ; ++ i
)
575 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
582 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const char *PropertyName
) const
584 const wxHandlerInfo
* info
= GetFirstHandler() ;
588 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
590 info
= info
->GetNext() ;
593 const wxClassInfo
** parents
= GetParents() ;
594 for ( int i
= 0 ; parents
[i
] ; ++ i
)
596 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
604 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
)
606 const wxPropertyAccessor
*accessor
;
608 accessor
= FindAccessor(propertyName
);
609 wxASSERT(accessor
->HasSetter());
610 accessor
->SetProperty( object
, value
) ;
613 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
)
615 const wxPropertyAccessor
*accessor
;
617 accessor
= FindAccessor(propertyName
);
618 wxASSERT(accessor
->HasGetter());
619 return accessor
->GetProperty(object
);
626 wxObject
* wxxVariant::GetAsObject()
628 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
630 return ti
->GetClassInfo()->VariantToInstance(*this) ;