]>
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 return node
->GetChildren()->GetContent() ;
204 // streamer specializations
206 // TODO for all built-in types
210 void wxStringReadValue(const wxString
&s
, long &data
)
212 wxSscanf(s
, _T("%ld"), &data
) ;
215 void wxStringWriteValue(wxString
&s
, const long &data
)
217 s
= wxString::Format("%ld", data
) ;
222 void wxStringReadValue(const wxString
&s
, int &data
)
224 wxSscanf(s
, _T("%d"), &data
) ;
227 void wxStringWriteValue(wxString
&s
, const int &data
)
229 s
= wxString::Format("%d", data
) ;
234 void wxStringReadValue(const wxString
&s
, wxString
&data
)
239 void wxStringWriteValue(wxString
&s
, const wxString
&data
)
245 Custom Data Streaming / Type Infos
246 we will have to add this for all wx non object types, but it is also an example
247 for custom data structures
252 void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
254 wxSscanf(s
, _T("%d,%d"), &data
.x
, &data
.y
) ;
257 void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
259 s
= wxString::Format("%d,%d", data
.x
, data
.y
) ;
262 WX_CUSTOM_TYPE_INFO(wxPoint
)
264 // removing header dependancy on string tokenizer
266 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
)
268 wxStringTokenizer
tokenizer(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
271 while (tokenizer
.HasMoreTokens())
273 array
.Add(tokenizer
.GetNextToken()) ;
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
282 void wxClassInfo::Register(const char *WXUNUSED(name
), wxClassInfo
*WXUNUSED(info
))
285 if (!ExtendedTypeMap)
286 ExtendedTypeMap = new ClassMap;
287 (*ExtendedTypeMap)[string(Name)] = Info;
291 void wxClassInfo::Unregister(const char *WXUNUSED(name
))
294 assert(ExtendedTypeMap);
295 ExtendedTypeMap->erase(Name);
299 const wxPropertyAccessor
*wxClassInfo::FindAccessor(const char *PropertyName
)
301 const wxPropertyInfo
* info
= FindPropInfo( PropertyName
) ;
304 return info
->GetAccessor() ;
309 const wxPropertyInfo
*wxClassInfo::FindPropInfo (const char *PropertyName
) const
311 const wxPropertyInfo
* info
= GetFirstProperty() ;
315 if ( strcmp( info
->GetName() , PropertyName
) == 0 )
317 info
= info
->GetNext() ;
320 const wxClassInfo
** parents
= GetParents() ;
321 for ( int i
= 0 ; parents
[i
] ; ++ i
)
323 if ( ( info
= parents
[i
]->FindPropInfo( PropertyName
) ) != NULL
)
330 void wxClassInfo::SetProperty(wxObject
*object
, const char *propertyName
, const wxxVariant
&value
)
332 const wxPropertyAccessor
*accessor
;
334 accessor
= FindAccessor(propertyName
);
335 wxASSERT(accessor
->HasSetter());
336 accessor
->SetProperty( object
, value
) ;
339 wxxVariant
wxClassInfo::GetProperty(wxObject
*object
, const char *propertyName
)
341 const wxPropertyAccessor
*accessor
;
343 accessor
= FindAccessor(propertyName
);
344 wxASSERT(accessor
->HasGetter());
345 return accessor
->GetProperty(object
);
352 wxObject
* wxxVariant::GetAsObject() const
354 const wxClassTypeInfo
*ti
= dynamic_cast<const wxClassTypeInfo
*>( m_data
->GetTypeInfo() ) ;
356 return ti
->GetClassInfo()->VariantToInstance(*this) ;