]>
Commit | Line | Data |
---|---|---|
a095505c SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/xti.cpp | |
3 | // Purpose: runtime metadata information (extended class info | |
4 | // Author: Stefan Csomor | |
30fd71e6 | 5 | // Modified by: |
a095505c SC |
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 | ||
30bfc425 | 34 | #if wxUSE_EXTENDED_RTTI |
a095505c SC |
35 | |
36 | // ---------------------------------------------------------------------------- | |
37 | // Enum Support | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
30fd71e6 | 40 | wxEnumData::wxEnumData( wxEnumMemberData* data ) |
a095505c | 41 | { |
30fd71e6 | 42 | m_members = data ; |
a095505c SC |
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 | } | |
0c03c79e | 72 | return 0 ; |
a095505c SC |
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 | ||
30fd71e6 | 85 | int wxEnumData::GetEnumMemberValueByIndex( int idx ) |
a095505c SC |
86 | { |
87 | // we should cache the count in order to avoid out-of-bounds errors | |
88 | return m_members[idx].m_value ; | |
89 | } | |
90 | ||
30fd71e6 | 91 | const char * wxEnumData::GetEnumMemberNameByIndex( int idx ) |
a095505c SC |
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 | ||
0c03c79e | 101 | template<> const wxTypeInfo* wxGetTypeInfo( void * ) |
a095505c SC |
102 | { |
103 | static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ; | |
104 | return &s_typeInfo ; | |
105 | } | |
106 | ||
0c03c79e | 107 | template<> const wxTypeInfo* wxGetTypeInfo( bool * ) |
a095505c SC |
108 | { |
109 | static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL ) ; | |
110 | return &s_typeInfo ; | |
111 | } | |
112 | ||
0c03c79e | 113 | template<> const wxTypeInfo* wxGetTypeInfo( char * ) |
a095505c SC |
114 | { |
115 | static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ; | |
116 | return &s_typeInfo ; | |
117 | } | |
118 | ||
0c03c79e | 119 | template<> const wxTypeInfo* wxGetTypeInfo( unsigned char * ) |
a095505c SC |
120 | { |
121 | static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ; | |
122 | return &s_typeInfo ; | |
123 | } | |
124 | ||
0c03c79e | 125 | template<> const wxTypeInfo* wxGetTypeInfo( int * ) |
a095505c SC |
126 | { |
127 | static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ; | |
128 | return &s_typeInfo ; | |
129 | } | |
130 | ||
0c03c79e | 131 | template<> const wxTypeInfo* wxGetTypeInfo( unsigned int * ) |
a095505c SC |
132 | { |
133 | static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ; | |
134 | return &s_typeInfo ; | |
135 | } | |
136 | ||
0c03c79e | 137 | template<> const wxTypeInfo* wxGetTypeInfo( long * ) |
a095505c SC |
138 | { |
139 | static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ; | |
140 | return &s_typeInfo ; | |
141 | } | |
142 | ||
0c03c79e | 143 | template<> const wxTypeInfo* wxGetTypeInfo( unsigned long * ) |
a095505c SC |
144 | { |
145 | static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ; | |
146 | return &s_typeInfo ; | |
147 | } | |
148 | ||
0c03c79e | 149 | template<> const wxTypeInfo* wxGetTypeInfo( float * ) |
a095505c SC |
150 | { |
151 | static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT ) ; | |
152 | return &s_typeInfo ; | |
153 | } | |
154 | ||
0c03c79e | 155 | template<> const wxTypeInfo* wxGetTypeInfo( double * ) |
a095505c SC |
156 | { |
157 | static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE ) ; | |
158 | return &s_typeInfo ; | |
159 | } | |
160 | ||
0c03c79e | 161 | template<> const wxTypeInfo* wxGetTypeInfo( wxString * ) |
a095505c SC |
162 | { |
163 | static wxBuiltInTypeInfo s_typeInfo( wxT_STRING ) ; | |
164 | return &s_typeInfo ; | |
165 | } | |
166 | ||
0c03c79e | 167 | // this are compiler induced specialization which are never used anywhere |
a095505c | 168 | |
0c03c79e SC |
169 | // char const * |
170 | ||
171 | template<> const wxTypeInfo* wxGetTypeInfo( char const ** ) | |
a095505c SC |
172 | { |
173 | assert(0) ; | |
174 | static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ; | |
175 | return &s_typeInfo ; | |
176 | } | |
177 | ||
0c03c79e | 178 | template<> void wxStringReadValue(const wxString & , const char* & ) |
a095505c SC |
179 | { |
180 | assert(0) ; | |
181 | } | |
182 | ||
0c03c79e | 183 | template<> void wxStringWriteValue(wxString & , char const * const & ) |
a095505c SC |
184 | { |
185 | assert(0) ; | |
186 | } | |
187 | ||
0c03c79e SC |
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 | } | |
a095505c | 201 | |
0c03c79e SC |
202 | template<> void wxStringWriteValue(wxString & , char * const & ) |
203 | { | |
204 | assert(0) ; | |
205 | } | |
206 | ||
a095505c | 207 | // ---------------------------------------------------------------------------- |
30fd71e6 | 208 | // value streaming |
a095505c SC |
209 | // ---------------------------------------------------------------------------- |
210 | ||
211 | // convenience function (avoids including xml headers in users code) | |
212 | ||
30fd71e6 | 213 | void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data ) |
a095505c | 214 | { |
30fd71e6 | 215 | node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "value", data ) ); |
a095505c SC |
216 | } |
217 | ||
30fd71e6 | 218 | wxString wxXmlGetContentFromNode( wxXmlNode *node ) |
a095505c | 219 | { |
fbbdc52c SC |
220 | if ( node->GetChildren() ) |
221 | return node->GetChildren()->GetContent() ; | |
222 | else | |
223 | return wxEmptyString ; | |
a095505c SC |
224 | } |
225 | ||
226 | // streamer specializations | |
45212047 | 227 | // for all built-in types |
a095505c | 228 | |
45212047 | 229 | // bool |
a095505c | 230 | |
45212047 SC |
231 | template<> void wxStringReadValue(const wxString &s , bool &data ) |
232 | { | |
233 | int intdata ; | |
234 | wxSscanf(s, _T("%d"), &intdata ) ; | |
235 | data = bool(intdata) ; | |
236 | } | |
a095505c | 237 | |
45212047 | 238 | template<> void wxStringWriteValue(wxString &s , const bool &data ) |
a095505c | 239 | { |
45212047 | 240 | s = wxString::Format("%d", data ) ; |
a095505c SC |
241 | } |
242 | ||
45212047 SC |
243 | // char |
244 | ||
245 | template<> void wxStringReadValue(const wxString &s , char &data ) | |
a095505c | 246 | { |
45212047 SC |
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 ) ; | |
66c57129 | 263 | data = (unsigned char)(intdata) ; |
45212047 SC |
264 | } |
265 | ||
266 | template<> void wxStringWriteValue(wxString &s , const unsigned char &data ) | |
267 | { | |
268 | s = wxString::Format("%d", data ) ; | |
a095505c SC |
269 | } |
270 | ||
30fd71e6 | 271 | // int |
a095505c | 272 | |
30fd71e6 | 273 | template<> void wxStringReadValue(const wxString &s , int &data ) |
a095505c SC |
274 | { |
275 | wxSscanf(s, _T("%d"), &data ) ; | |
276 | } | |
277 | ||
30fd71e6 | 278 | template<> void wxStringWriteValue(wxString &s , const int &data ) |
a095505c SC |
279 | { |
280 | s = wxString::Format("%d", data ) ; | |
281 | } | |
282 | ||
45212047 SC |
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 | ||
a095505c SC |
343 | // wxString |
344 | ||
30fd71e6 | 345 | template<> void wxStringReadValue(const wxString &s , wxString &data ) |
a095505c SC |
346 | { |
347 | data = s ; | |
348 | } | |
349 | ||
30fd71e6 | 350 | template<> void wxStringWriteValue(wxString &s , const wxString &data ) |
a095505c SC |
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 | ||
30fd71e6 | 363 | template<> void wxStringReadValue(const wxString &s , wxPoint &data ) |
a095505c SC |
364 | { |
365 | wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ; | |
366 | } | |
367 | ||
30fd71e6 | 368 | template<> void wxStringWriteValue(wxString &s , const wxPoint &data ) |
a095505c SC |
369 | { |
370 | s = wxString::Format("%d,%d", data.x , data.y ) ; | |
371 | } | |
372 | ||
373 | WX_CUSTOM_TYPE_INFO(wxPoint) | |
374 | ||
30fd71e6 | 375 | template<> void wxStringReadValue(const wxString &s , wxSize &data ) |
fbbdc52c SC |
376 | { |
377 | wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ; | |
378 | } | |
379 | ||
30fd71e6 | 380 | template<> void wxStringWriteValue(wxString &s , const wxSize &data ) |
fbbdc52c SC |
381 | { |
382 | s = wxString::Format("%d,%d", data.x , data.y ) ; | |
383 | } | |
384 | ||
385 | WX_CUSTOM_TYPE_INFO(wxSize) | |
386 | ||
45212047 SC |
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 | ||
a095505c SC |
418 | // removing header dependancy on string tokenizer |
419 | ||
30fd71e6 | 420 | void wxSetStringToArray( const wxString &s , wxArrayString &array ) |
a095505c SC |
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 | // ---------------------------------------------------------------------------- | |
30fd71e6 | 432 | // wxClassInfo |
a095505c SC |
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 | { | |
fbbdc52c | 455 | const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ; |
30fd71e6 | 456 | |
a095505c SC |
457 | if ( info ) |
458 | return info->GetAccessor() ; | |
459 | ||
460 | return NULL ; | |
461 | } | |
462 | ||
fbbdc52c | 463 | const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const |
a095505c SC |
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 | { | |
fbbdc52c | 477 | if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL ) |
a095505c SC |
478 | return info ; |
479 | } | |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
fbbdc52c SC |
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 | ||
a095505c SC |
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 | ||
66c57129 | 528 | wxObject* wxxVariant::GetAsObject() |
a095505c SC |
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 |