xti streaming extensions
[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 template<> const wxTypeInfo* wxGetTypeInfo( void * )
102 {
103 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
104 return &s_typeInfo ;
105 }
106
107 template<> const wxTypeInfo* wxGetTypeInfo( bool * )
108 {
109 static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL ) ;
110 return &s_typeInfo ;
111 }
112
113 template<> const wxTypeInfo* wxGetTypeInfo( char * )
114 {
115 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
116 return &s_typeInfo ;
117 }
118
119 template<> const wxTypeInfo* wxGetTypeInfo( unsigned char * )
120 {
121 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
122 return &s_typeInfo ;
123 }
124
125 template<> const wxTypeInfo* wxGetTypeInfo( int * )
126 {
127 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
128 return &s_typeInfo ;
129 }
130
131 template<> const wxTypeInfo* wxGetTypeInfo( unsigned int * )
132 {
133 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
134 return &s_typeInfo ;
135 }
136
137 template<> const wxTypeInfo* wxGetTypeInfo( long * )
138 {
139 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
140 return &s_typeInfo ;
141 }
142
143 template<> const wxTypeInfo* wxGetTypeInfo( unsigned long * )
144 {
145 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
146 return &s_typeInfo ;
147 }
148
149 template<> const wxTypeInfo* wxGetTypeInfo( float * )
150 {
151 static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT ) ;
152 return &s_typeInfo ;
153 }
154
155 template<> const wxTypeInfo* wxGetTypeInfo( double * )
156 {
157 static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE ) ;
158 return &s_typeInfo ;
159 }
160
161 template<> const wxTypeInfo* wxGetTypeInfo( wxString * )
162 {
163 static wxBuiltInTypeInfo s_typeInfo( wxT_STRING ) ;
164 return &s_typeInfo ;
165 }
166
167 // this are compiler induced specialization which are never used anywhere
168
169 // char const *
170
171 template<> const wxTypeInfo* wxGetTypeInfo( char const ** )
172 {
173 assert(0) ;
174 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
175 return &s_typeInfo ;
176 }
177
178 template<> void wxStringReadValue(const wxString & , const char* & )
179 {
180 assert(0) ;
181 }
182
183 template<> void wxStringWriteValue(wxString & , char const * const & )
184 {
185 assert(0) ;
186 }
187
188 // char *
189
190 template<> const wxTypeInfo* wxGetTypeInfo( char ** )
191 {
192 assert(0) ;
193 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
194 return &s_typeInfo ;
195 }
196
197 template<> void wxStringReadValue(const wxString & , char* & )
198 {
199 assert(0) ;
200 }
201
202 template<> void wxStringWriteValue(wxString & , char * const & )
203 {
204 assert(0) ;
205 }
206
207 // ----------------------------------------------------------------------------
208 // value streaming
209 // ----------------------------------------------------------------------------
210
211 // convenience function (avoids including xml headers in users code)
212
213 void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data )
214 {
215 node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "value", data ) );
216 }
217
218 wxString wxXmlGetContentFromNode( wxXmlNode *node )
219 {
220 if ( node->GetChildren() )
221 return node->GetChildren()->GetContent() ;
222 else
223 return wxEmptyString ;
224 }
225
226 // streamer specializations
227 // for all built-in types
228
229 // bool
230
231 template<> void wxStringReadValue(const wxString &s , bool &data )
232 {
233 int intdata ;
234 wxSscanf(s, _T("%d"), &intdata ) ;
235 data = bool(intdata) ;
236 }
237
238 template<> void wxStringWriteValue(wxString &s , const bool &data )
239 {
240 s = wxString::Format("%d", data ) ;
241 }
242
243 // char
244
245 template<> void wxStringReadValue(const wxString &s , char &data )
246 {
247 int intdata ;
248 wxSscanf(s, _T("%d"), &intdata ) ;
249 data = char(intdata) ;
250 }
251
252 template<> void wxStringWriteValue(wxString &s , const char &data )
253 {
254 s = wxString::Format("%d", data ) ;
255 }
256
257 // unsigned char
258
259 template<> void wxStringReadValue(const wxString &s , unsigned char &data )
260 {
261 int intdata ;
262 wxSscanf(s, _T("%d"), &intdata ) ;
263 data = unsigned char(intdata) ;
264 }
265
266 template<> void wxStringWriteValue(wxString &s , const unsigned char &data )
267 {
268 s = wxString::Format("%d", data ) ;
269 }
270
271 // int
272
273 template<> void wxStringReadValue(const wxString &s , int &data )
274 {
275 wxSscanf(s, _T("%d"), &data ) ;
276 }
277
278 template<> void wxStringWriteValue(wxString &s , const int &data )
279 {
280 s = wxString::Format("%d", data ) ;
281 }
282
283 // unsigned int
284
285 template<> void wxStringReadValue(const wxString &s , unsigned int &data )
286 {
287 wxSscanf(s, _T("%d"), &data ) ;
288 }
289
290 template<> void wxStringWriteValue(wxString &s , const unsigned int &data )
291 {
292 s = wxString::Format("%d", data ) ;
293 }
294
295 // long
296
297 template<> void wxStringReadValue(const wxString &s , long &data )
298 {
299 wxSscanf(s, _T("%ld"), &data ) ;
300 }
301
302 template<> void wxStringWriteValue(wxString &s , const long &data )
303 {
304 s = wxString::Format("%ld", data ) ;
305 }
306
307 // unsigned long
308
309 template<> void wxStringReadValue(const wxString &s , unsigned long &data )
310 {
311 wxSscanf(s, _T("%ld"), &data ) ;
312 }
313
314 template<> void wxStringWriteValue(wxString &s , const unsigned long &data )
315 {
316 s = wxString::Format("%ld", data ) ;
317 }
318
319 // float
320
321 template<> void wxStringReadValue(const wxString &s , float &data )
322 {
323 wxSscanf(s, _T("%f"), &data ) ;
324 }
325
326 template<> void wxStringWriteValue(wxString &s , const float &data )
327 {
328 s = wxString::Format("%f", data ) ;
329 }
330
331 // double
332
333 template<> void wxStringReadValue(const wxString &s , double &data )
334 {
335 wxSscanf(s, _T("%lf"), &data ) ;
336 }
337
338 template<> void wxStringWriteValue(wxString &s , const double &data )
339 {
340 s = wxString::Format("%lf", data ) ;
341 }
342
343 // wxString
344
345 template<> void wxStringReadValue(const wxString &s , wxString &data )
346 {
347 data = s ;
348 }
349
350 template<> void wxStringWriteValue(wxString &s , const wxString &data )
351 {
352 s = data ;
353 }
354
355 /*
356 Custom Data Streaming / Type Infos
357 we will have to add this for all wx non object types, but it is also an example
358 for custom data structures
359 */
360
361 // wxPoint
362
363 template<> void wxStringReadValue(const wxString &s , wxPoint &data )
364 {
365 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
366 }
367
368 template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
369 {
370 s = wxString::Format("%d,%d", data.x , data.y ) ;
371 }
372
373 WX_CUSTOM_TYPE_INFO(wxPoint)
374
375 template<> void wxStringReadValue(const wxString &s , wxSize &data )
376 {
377 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
378 }
379
380 template<> void wxStringWriteValue(wxString &s , const wxSize &data )
381 {
382 s = wxString::Format("%d,%d", data.x , data.y ) ;
383 }
384
385 WX_CUSTOM_TYPE_INFO(wxSize)
386
387 /*
388
389 template<> void wxStringReadValue(const wxString &s , wxColour &data )
390 {
391 // copied from VS xrc
392 unsigned long tmp = 0;
393
394 if (s.Length() != 7 || s[0u] != wxT('#') ||
395 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
396 {
397 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
398 s.c_str() );
399 data = wxNullColour;
400 }
401 else
402 {
403 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
404 (unsigned char) ((tmp & 0x00FF00) >> 8),
405 (unsigned char) ((tmp & 0x0000FF)));
406 }
407 }
408
409 template<> void wxStringWriteValue(wxString &s , const wxColour &data )
410 {
411 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
412 }
413
414 WX_CUSTOM_TYPE_INFO(wxColour)
415
416 */
417
418 // removing header dependancy on string tokenizer
419
420 void wxSetStringToArray( const wxString &s , wxArrayString &array )
421 {
422 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
423 wxString flag;
424 array.Clear() ;
425 while (tokenizer.HasMoreTokens())
426 {
427 array.Add(tokenizer.GetNextToken()) ;
428 }
429 }
430
431 // ----------------------------------------------------------------------------
432 // wxClassInfo
433 // ----------------------------------------------------------------------------
434
435
436 void wxClassInfo::Register(const char *WXUNUSED(name), wxClassInfo *WXUNUSED(info))
437 {
438 /*
439 if (!ExtendedTypeMap)
440 ExtendedTypeMap = new ClassMap;
441 (*ExtendedTypeMap)[string(Name)] = Info;
442 */
443 }
444
445 void wxClassInfo::Unregister(const char *WXUNUSED(name))
446 {
447 /*
448 assert(ExtendedTypeMap);
449 ExtendedTypeMap->erase(Name);
450 */
451 }
452
453 const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName)
454 {
455 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
456
457 if ( info )
458 return info->GetAccessor() ;
459
460 return NULL ;
461 }
462
463 const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
464 {
465 const wxPropertyInfo* info = GetFirstProperty() ;
466
467 while( info )
468 {
469 if ( strcmp( info->GetName() , PropertyName ) == 0 )
470 return info ;
471 info = info->GetNext() ;
472 }
473
474 const wxClassInfo** parents = GetParents() ;
475 for ( int i = 0 ; parents[i] ; ++ i )
476 {
477 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
478 return info ;
479 }
480
481 return 0;
482 }
483
484 const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
485 {
486 const wxHandlerInfo* info = GetFirstHandler() ;
487
488 while( info )
489 {
490 if ( strcmp( info->GetName() , PropertyName ) == 0 )
491 return info ;
492 info = info->GetNext() ;
493 }
494
495 const wxClassInfo** parents = GetParents() ;
496 for ( int i = 0 ; parents[i] ; ++ i )
497 {
498 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
499 return info ;
500 }
501
502 return 0;
503 }
504
505
506 void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value)
507 {
508 const wxPropertyAccessor *accessor;
509
510 accessor = FindAccessor(propertyName);
511 wxASSERT(accessor->HasSetter());
512 accessor->SetProperty( object , value ) ;
513 }
514
515 wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName)
516 {
517 const wxPropertyAccessor *accessor;
518
519 accessor = FindAccessor(propertyName);
520 wxASSERT(accessor->HasGetter());
521 return accessor->GetProperty(object);
522 }
523
524 /*
525 VARIANT TO OBJECT
526 */
527
528 wxObject* wxxVariant::GetAsObject()
529 {
530 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
531 if ( ti )
532 return ti->GetClassInfo()->VariantToInstance(*this) ;
533 else
534 return NULL ;
535 }
536
537
538 #endif