xti updates
[wxWidgets.git] / src / common / xti.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/xti.cpp
3 // Purpose: runtime metadata information (extended class info
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 27/07/03
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "xti.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/hash.h"
26 #include "wx/object.h"
27 #endif
28
29 #include "wx/xml/xml.h"
30 #include "wx/tokenzr.h"
31
32 #include <string.h>
33
34 #if wxUSE_EXTENDED_RTTI
35
36 // ----------------------------------------------------------------------------
37 // Enum Support
38 // ----------------------------------------------------------------------------
39
40 wxEnumData::wxEnumData( wxEnumMemberData* data )
41 {
42 m_members = data ;
43 for ( m_count = 0; m_members[m_count].m_name ; m_count++)
44 {} ;
45 }
46
47 bool wxEnumData::HasEnumMemberValue(const wxChar *name, int *value)
48 {
49 int i;
50 for (i = 0; m_members[i].m_name ; i++ )
51 {
52 if (!strcmp(name, m_members[i].m_name))
53 {
54 if ( value )
55 *value = m_members[i].m_value;
56 return true ;
57 }
58 }
59 return false ;
60 }
61
62 int wxEnumData::GetEnumMemberValue(const wxChar *name)
63 {
64 int i;
65 for (i = 0; m_members[i].m_name ; i++ )
66 {
67 if (!strcmp(name, m_members[i].m_name))
68 {
69 return m_members[i].m_value;
70 }
71 }
72 return 0 ;
73 }
74
75 const wxChar *wxEnumData::GetEnumMemberName(int value)
76 {
77 int i;
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;
81
82 return wxT("") ;
83 }
84
85 int wxEnumData::GetEnumMemberValueByIndex( int idx )
86 {
87 // we should cache the count in order to avoid out-of-bounds errors
88 return m_members[idx].m_value ;
89 }
90
91 const char * wxEnumData::GetEnumMemberNameByIndex( int idx )
92 {
93 // we should cache the count in order to avoid out-of-bounds errors
94 return m_members[idx].m_name ;
95 }
96
97 // ----------------------------------------------------------------------------
98 // Type Information
99 // ----------------------------------------------------------------------------
100
101 const wxTypeInfo* wxGetTypeInfo( void * )
102 {
103 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
104 return &s_typeInfo ;
105 }
106
107 const wxTypeInfo* wxGetTypeInfo( bool * )
108 {
109 static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL ) ;
110 return &s_typeInfo ;
111 }
112
113 const wxTypeInfo* wxGetTypeInfo( char * )
114 {
115 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
116 return &s_typeInfo ;
117 }
118
119 const wxTypeInfo* wxGetTypeInfo( unsigned char * )
120 {
121 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
122 return &s_typeInfo ;
123 }
124
125 const wxTypeInfo* wxGetTypeInfo( int * )
126 {
127 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
128 return &s_typeInfo ;
129 }
130
131 const wxTypeInfo* wxGetTypeInfo( unsigned int * )
132 {
133 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
134 return &s_typeInfo ;
135 }
136
137 const wxTypeInfo* wxGetTypeInfo( long * )
138 {
139 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
140 return &s_typeInfo ;
141 }
142
143 const wxTypeInfo* wxGetTypeInfo( unsigned long * )
144 {
145 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
146 return &s_typeInfo ;
147 }
148
149 const wxTypeInfo* wxGetTypeInfo( float * )
150 {
151 static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT ) ;
152 return &s_typeInfo ;
153 }
154
155 const wxTypeInfo* wxGetTypeInfo( double * )
156 {
157 static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE ) ;
158 return &s_typeInfo ;
159 }
160
161 const wxTypeInfo* wxGetTypeInfo( wxString * )
162 {
163 static wxBuiltInTypeInfo s_typeInfo( wxT_STRING ) ;
164 return &s_typeInfo ;
165 }
166
167 // this is a compiler induced specialization which is never used anywhere
168 // const char * should never be active
169
170 const wxTypeInfo* wxGetTypeInfo( char const ** )
171 {
172 assert(0) ;
173 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
174 return &s_typeInfo ;
175 }
176
177 void wxStringReadValue(const wxString & , const char* & )
178 {
179 assert(0) ;
180 }
181
182 void wxStringWriteValue(wxString & , char const * const & )
183 {
184 assert(0) ;
185 }
186
187
188 // ----------------------------------------------------------------------------
189 // value streaming
190 // ----------------------------------------------------------------------------
191
192 // convenience function (avoids including xml headers in users code)
193
194 void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data )
195 {
196 node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "value", data ) );
197 }
198
199 wxString wxXmlGetContentFromNode( wxXmlNode *node )
200 {
201 if ( node->GetChildren() )
202 return node->GetChildren()->GetContent() ;
203 else
204 return wxEmptyString ;
205 }
206
207 // streamer specializations
208
209 // TODO for all built-in types
210
211 // long
212
213 void wxStringReadValue(const wxString &s , long &data )
214 {
215 wxSscanf(s, _T("%ld"), &data ) ;
216 }
217
218 void wxStringWriteValue(wxString &s , const long &data )
219 {
220 s = wxString::Format("%ld", data ) ;
221 }
222
223 // long
224
225 void wxStringReadValue(const wxString &s , int &data )
226 {
227 wxSscanf(s, _T("%d"), &data ) ;
228 }
229
230 void wxStringWriteValue(wxString &s , const int &data )
231 {
232 s = wxString::Format("%d", data ) ;
233 }
234
235 // wxString
236
237 void wxStringReadValue(const wxString &s , wxString &data )
238 {
239 data = s ;
240 }
241
242 void wxStringWriteValue(wxString &s , const wxString &data )
243 {
244 s = data ;
245 }
246
247 /*
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
251 */
252
253 // wxPoint
254
255 void wxStringReadValue(const wxString &s , wxPoint &data )
256 {
257 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
258 }
259
260 void wxStringWriteValue(wxString &s , const wxPoint &data )
261 {
262 s = wxString::Format("%d,%d", data.x , data.y ) ;
263 }
264
265 WX_CUSTOM_TYPE_INFO(wxPoint)
266
267 void wxStringReadValue(const wxString &s , wxSize &data )
268 {
269 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
270 }
271
272 void wxStringWriteValue(wxString &s , const wxSize &data )
273 {
274 s = wxString::Format("%d,%d", data.x , data.y ) ;
275 }
276
277 WX_CUSTOM_TYPE_INFO(wxSize)
278
279 // removing header dependancy on string tokenizer
280
281 void wxSetStringToArray( const wxString &s , wxArrayString &array )
282 {
283 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
284 wxString flag;
285 array.Clear() ;
286 while (tokenizer.HasMoreTokens())
287 {
288 array.Add(tokenizer.GetNextToken()) ;
289 }
290 }
291
292 // ----------------------------------------------------------------------------
293 // wxClassInfo
294 // ----------------------------------------------------------------------------
295
296
297 void wxClassInfo::Register(const char *WXUNUSED(name), wxClassInfo *WXUNUSED(info))
298 {
299 /*
300 if (!ExtendedTypeMap)
301 ExtendedTypeMap = new ClassMap;
302 (*ExtendedTypeMap)[string(Name)] = Info;
303 */
304 }
305
306 void wxClassInfo::Unregister(const char *WXUNUSED(name))
307 {
308 /*
309 assert(ExtendedTypeMap);
310 ExtendedTypeMap->erase(Name);
311 */
312 }
313
314 const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName)
315 {
316 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
317
318 if ( info )
319 return info->GetAccessor() ;
320
321 return NULL ;
322 }
323
324 const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
325 {
326 const wxPropertyInfo* info = GetFirstProperty() ;
327
328 while( info )
329 {
330 if ( strcmp( info->GetName() , PropertyName ) == 0 )
331 return info ;
332 info = info->GetNext() ;
333 }
334
335 const wxClassInfo** parents = GetParents() ;
336 for ( int i = 0 ; parents[i] ; ++ i )
337 {
338 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
339 return info ;
340 }
341
342 return 0;
343 }
344
345 const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
346 {
347 const wxHandlerInfo* info = GetFirstHandler() ;
348
349 while( info )
350 {
351 if ( strcmp( info->GetName() , PropertyName ) == 0 )
352 return info ;
353 info = info->GetNext() ;
354 }
355
356 const wxClassInfo** parents = GetParents() ;
357 for ( int i = 0 ; parents[i] ; ++ i )
358 {
359 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
360 return info ;
361 }
362
363 return 0;
364 }
365
366
367 void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value)
368 {
369 const wxPropertyAccessor *accessor;
370
371 accessor = FindAccessor(propertyName);
372 wxASSERT(accessor->HasSetter());
373 accessor->SetProperty( object , value ) ;
374 }
375
376 wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName)
377 {
378 const wxPropertyAccessor *accessor;
379
380 accessor = FindAccessor(propertyName);
381 wxASSERT(accessor->HasGetter());
382 return accessor->GetProperty(object);
383 }
384
385 /*
386 VARIANT TO OBJECT
387 */
388
389 wxObject* wxxVariant::GetAsObject() const
390 {
391 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
392 if ( ti )
393 return ti->GetClassInfo()->VariantToInstance(*this) ;
394 else
395 return NULL ;
396 }
397
398
399 #endif