]>
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 const wxTypeInfo
* wxGetTypeInfo( void * )
103 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
107 const wxTypeInfo
* wxGetTypeInfo( bool * )
109 static wxBuiltInTypeInfo
s_typeInfo( wxT_BOOL
) ;
113 const wxTypeInfo
* wxGetTypeInfo( char * )
115 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
119 const wxTypeInfo
* wxGetTypeInfo( unsigned char * )
121 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
125 const wxTypeInfo
* wxGetTypeInfo( int * )
127 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
131 const wxTypeInfo
* wxGetTypeInfo( unsigned int * )
133 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
137 const wxTypeInfo
* wxGetTypeInfo( long * )
139 static wxBuiltInTypeInfo
s_typeInfo( wxT_CHAR
) ;
143 const wxTypeInfo
* wxGetTypeInfo( unsigned long * )
145 static wxBuiltInTypeInfo
s_typeInfo( wxT_UCHAR
) ;
149 const wxTypeInfo
* wxGetTypeInfo( float * )
151 static wxBuiltInTypeInfo
s_typeInfo( wxT_FLOAT
) ;
155 const wxTypeInfo
* wxGetTypeInfo( double * )
157 static wxBuiltInTypeInfo
s_typeInfo( wxT_DOUBLE
) ;
161 const wxTypeInfo
* wxGetTypeInfo( wxString
* )
163 static wxBuiltInTypeInfo
s_typeInfo( wxT_STRING
) ;
167 // this is a compiler induced specialization which is never used anywhere
168 // const char * should never be active
170 const wxTypeInfo
* wxGetTypeInfo( char const ** )
173 static wxBuiltInTypeInfo
s_typeInfo( wxT_VOID
) ;
177 void wxStringReadValue(const wxString
& , const char* & )
182 void wxStringWriteValue(wxString
& , char const * const & )
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 // convenience function (avoids including xml headers in users code)
194 void wxXmlAddContentToNode( wxXmlNode
* node
, const wxString
& data
)
196 node
->AddChild(new wxXmlNode(wxXML_TEXT_NODE
, "value", data
) );
199 wxString
wxXmlGetContentFromNode( wxXmlNode
*node
)
201 if ( node
->GetChildren() )
202 return node
->GetChildren()->GetContent() ;
204 return wxEmptyString
;
207 // streamer specializations
209 // TODO for all built-in types
213 void wxStringReadValue(const wxString
&s
, long &data
)
215 wxSscanf(s
, _T("%ld"), &data
) ;
218 void wxStringWriteValue(wxString
&s
, const long &data
)
220 s
= wxString::Format("%ld", data
) ;
225 void wxStringReadValue(const wxString
&s
, int &data
)
227 wxSscanf(s
, _T("%d"), &data
) ;
230 void wxStringWriteValue(wxString
&s
, const int &data
)
232 s
= wxString::Format("%d", data
) ;
237 void wxStringReadValue(const wxString
&s
, wxString
&data
)
242 void wxStringWriteValue(wxString
&s
, const wxString
&data
)
248 Custom Data Streaming / Type Infos
249 we will have to add this for all wx non object types, but it is also an example
250 for custom data structures
255 void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
257 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
260 void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
262 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
265 WX_CUSTOM_TYPE_INFO(wxPoint
)
267 void wxStringReadValue(const wxString
&s
, wxSize
&data
)
269 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
272 void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
274 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
277 WX_CUSTOM_TYPE_INFO(wxSize
)
279 // removing header dependancy on string tokenizer
281 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
283 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
286 while (tokenizer
.HasMoreTokens())
288 array
.Add(tokenizer
.GetNextToken()) ;
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
297 void wxClassInfo::Register(const char *WXUNUSED(name
), wxClassInfo
*WXUNUSED(info
))
300 if (!ExtendedTypeMap)
301 ExtendedTypeMap = new ClassMap;
302 (*ExtendedTypeMap)[string(Name)] = Info;
306 void wxClassInfo::Unregister(const char *WXUNUSED(name
))
309 assert(ExtendedTypeMap);
310 ExtendedTypeMap->erase(Name);
314 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
)
316 const wxPropertyInfo
* info
= FindPropertyInfo( PropertyName
) ;
319 return info
->GetAccessor() ;
324 const wxPropertyInfo
*wxClassInfo::FindPropertyInfo (const char *PropertyName
) const
326 const wxPropertyInfo
* info
= GetFirstProperty() ;
330 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
332 info
= info
->GetNext() ;
335 const wxClassInfo
** parents
= GetParents() ;
336 for ( int i
= 0 ; parents
[i
] ; ++ i
)
338 if ( ( info
= parents
[i
]->FindPropertyInfo( PropertyName
) ) != NULL
)
345 const wxHandlerInfo
*wxClassInfo::FindHandlerInfo (const char *PropertyName
) const
347 const wxHandlerInfo
* info
= GetFirstHandler() ;
351 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
353 info
= info
->GetNext() ;
356 const wxClassInfo
** parents
= GetParents() ;
357 for ( int i
= 0 ; parents
[i
] ; ++ i
)
359 if ( ( info
= parents
[i
]->FindHandlerInfo( PropertyName
) ) != NULL
)
367 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
)
369 const wxPropertyAccessor
*accessor
;
371 accessor
= FindAccessor(propertyName
);
372 wxASSERT(accessor
->HasSetter());
373 accessor
->SetProperty( object
, value
) ;
376 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
)
378 const wxPropertyAccessor
*accessor
;
380 accessor
= FindAccessor(propertyName
);
381 wxASSERT(accessor
->HasGetter());
382 return accessor
->GetProperty(object
);
389 wxObject
* wxxVariant::GetAsObject() const
391 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
393 return ti
->GetClassInfo()->VariantToInstance(*this) ;