]>
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( const wxPoint * )
212 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
216 template<> void wxStringReadValue(const wxString & , const wxPoint & )
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 // convenience function (avoids including xml headers in users code)
227 void wxXmlAddContentToNode( wxXmlNode
* node
, const wxString
& data
)
229 node
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, "value", data
) );
232 wxString
wxXmlGetContentFromNode( wxXmlNode
*node
)
234 if ( node
->GetChildren() )
235 return node
->GetChildren()->GetContent() ;
237 return wxEmptyString
;
240 // streamer specializations
242 // TODO for all built-in types
246 template<> void wxStringReadValue(const wxString
&s
, long &data
)
248 wxSscanf(s
, _T("%ld"), &data
) ;
251 template<> void wxStringWriteValue(wxString
&s
, const long &data
)
253 s
= wxString::Format("%ld", data
) ;
258 template<> void wxStringReadValue(const wxString
&s
, int &data
)
260 wxSscanf(s
, _T("%d"), &data
) ;
263 template<> void wxStringWriteValue(wxString
&s
, const int &data
)
265 s
= wxString::Format("%d", data
) ;
270 template<> void wxStringReadValue(const wxString
&s
, wxString
&data
)
275 template<> void wxStringWriteValue(wxString
&s
, const wxString
&data
)
281 Custom Data Streaming / Type Infos
282 we will have to add this for all wx non object types, but it is also an example
283 for custom data structures
288 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
290 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
293 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
295 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
298 WX_CUSTOM_TYPE_INFO(wxPoint
)
300 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
302 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
305 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
307 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
310 WX_CUSTOM_TYPE_INFO(wxSize
)
312 // removing header dependancy on string tokenizer
314 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
316 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
319 while (tokenizer
.HasMoreTokens())
321 array
.Add(tokenizer
.GetNextToken()) ;
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
330 void wxClassInfo::Register(const char *WXUNUSED(name
), wxClassInfo
*WXUNUSED(info
))
333 if (!ExtendedTypeMap)
334 ExtendedTypeMap = new ClassMap;
335 (*ExtendedTypeMap)[string(Name)] = Info;
339 void wxClassInfo::Unregister(const char *WXUNUSED(name
))
342 assert(ExtendedTypeMap);
343 ExtendedTypeMap->erase(Name);
347 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
)
349 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
352 return info
->GetAccessor() ;
357 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const char *PropertyName
) const
359 const wxPropertyInfo
* info
= GetFirstProperty() ;
363 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
365 info
= info
->GetNext() ;
368 const wxClassInfo
** parents
= GetParents() ;
369 for ( int i
= 0 ; parents
[i
] ; ++ i
)
371 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
378 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const char *PropertyName
) const
380 const wxHandlerInfo
* info
= GetFirstHandler() ;
384 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
386 info
= info
->GetNext() ;
389 const wxClassInfo
** parents
= GetParents() ;
390 for ( int i
= 0 ; parents
[i
] ; ++ i
)
392 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
400 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
)
402 const wxPropertyAccessor
*accessor
;
404 accessor
= FindAccessor(propertyName
);
405 wxASSERT(accessor
->HasSetter());
406 accessor
->SetProperty( object
, value
) ;
409 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
)
411 const wxPropertyAccessor
*accessor
;
413 accessor
= FindAccessor(propertyName
);
414 wxASSERT(accessor
->HasGetter());
415 return accessor
->GetProperty(object
);
422 wxObject
* wxxVariant::GetAsObject() const
424 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
426 return ti
->GetClassInfo()->VariantToInstance(*this) ;