]> git.saurik.com Git - wxWidgets.git/blame - src/common/xti.cpp
check memory leaks sooner, when wxLog target is still available
[wxWidgets.git] / src / common / xti.cpp
CommitLineData
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 40wxEnumData::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
47bool 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
62int 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
75const 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 85int 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 91const 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 101template<> const wxTypeInfo* wxGetTypeInfo( void * )
a095505c
SC
102{
103 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
104 return &s_typeInfo ;
105}
106
0c03c79e 107template<> const wxTypeInfo* wxGetTypeInfo( bool * )
a095505c
SC
108{
109 static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL ) ;
110 return &s_typeInfo ;
111}
112
0c03c79e 113template<> const wxTypeInfo* wxGetTypeInfo( char * )
a095505c
SC
114{
115 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
116 return &s_typeInfo ;
117}
118
0c03c79e 119template<> const wxTypeInfo* wxGetTypeInfo( unsigned char * )
a095505c
SC
120{
121 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
122 return &s_typeInfo ;
123}
124
0c03c79e 125template<> const wxTypeInfo* wxGetTypeInfo( int * )
a095505c
SC
126{
127 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
128 return &s_typeInfo ;
129}
130
0c03c79e 131template<> const wxTypeInfo* wxGetTypeInfo( unsigned int * )
a095505c
SC
132{
133 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
134 return &s_typeInfo ;
135}
136
0c03c79e 137template<> const wxTypeInfo* wxGetTypeInfo( long * )
a095505c
SC
138{
139 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
140 return &s_typeInfo ;
141}
142
0c03c79e 143template<> const wxTypeInfo* wxGetTypeInfo( unsigned long * )
a095505c
SC
144{
145 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
146 return &s_typeInfo ;
147}
148
0c03c79e 149template<> const wxTypeInfo* wxGetTypeInfo( float * )
a095505c
SC
150{
151 static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT ) ;
152 return &s_typeInfo ;
153}
154
0c03c79e 155template<> const wxTypeInfo* wxGetTypeInfo( double * )
a095505c
SC
156{
157 static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE ) ;
158 return &s_typeInfo ;
159}
160
0c03c79e 161template<> 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
171template<> 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 178template<> void wxStringReadValue(const wxString & , const char* & )
a095505c
SC
179{
180 assert(0) ;
181}
182
0c03c79e 183template<> void wxStringWriteValue(wxString & , char const * const & )
a095505c
SC
184{
185 assert(0) ;
186}
187
0c03c79e
SC
188// char *
189
190template<> const wxTypeInfo* wxGetTypeInfo( char ** )
191{
192 assert(0) ;
193 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
194 return &s_typeInfo ;
195}
196
197template<> void wxStringReadValue(const wxString & , char* & )
198{
199 assert(0) ;
200}
a095505c 201
0c03c79e
SC
202template<> void wxStringWriteValue(wxString & , char * const & )
203{
204 assert(0) ;
205}
206
f0b7eadf
SC
207// unsigned char *
208
209template<> const wxTypeInfo* wxGetTypeInfo( unsigned char ** )
210{
211 assert(0) ;
212 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
213 return &s_typeInfo ;
214}
215
216template<> void wxStringReadValue(const wxString & , unsigned char* & )
217{
218 assert(0) ;
219}
220
221template<> void wxStringWriteValue(wxString & , unsigned char * const & )
222{
223 assert(0) ;
224}
225
226// int *
227
228template<> const wxTypeInfo* wxGetTypeInfo( int ** )
229{
230 assert(0) ;
231 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
232 return &s_typeInfo ;
233}
234
235template<> void wxStringReadValue(const wxString & , int* & )
236{
237 assert(0) ;
238}
239
240template<> void wxStringWriteValue(wxString & , int * const & )
241{
242 assert(0) ;
243}
244
245// bool *
246
247template<> const wxTypeInfo* wxGetTypeInfo( bool ** )
248{
249 assert(0) ;
250 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
251 return &s_typeInfo ;
252}
253
254template<> void wxStringReadValue(const wxString & , bool* & )
255{
256 assert(0) ;
257}
258
259template<> void wxStringWriteValue(wxString & , bool * const & )
260{
261 assert(0) ;
262}
263
264// long *
265
266template<> const wxTypeInfo* wxGetTypeInfo( long ** )
267{
268 assert(0) ;
269 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
270 return &s_typeInfo ;
271}
272
273template<> void wxStringReadValue(const wxString & , long* & )
274{
275 assert(0) ;
276}
277
278template<> void wxStringWriteValue(wxString & , long * const & )
279{
280 assert(0) ;
281}
282
283// wxString *
284
285template<> const wxTypeInfo* wxGetTypeInfo( wxString ** )
286{
287 assert(0) ;
288 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
289 return &s_typeInfo ;
290}
291
292template<> void wxStringReadValue(const wxString & , wxString* & )
293{
294 assert(0) ;
295}
296
297template<> void wxStringWriteValue(wxString & , wxString * const & )
298{
299 assert(0) ;
300}
301
302
a095505c 303// ----------------------------------------------------------------------------
30fd71e6 304// value streaming
a095505c
SC
305// ----------------------------------------------------------------------------
306
307// convenience function (avoids including xml headers in users code)
308
30fd71e6 309void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data )
a095505c 310{
30fd71e6 311 node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "value", data ) );
a095505c
SC
312}
313
30fd71e6 314wxString wxXmlGetContentFromNode( wxXmlNode *node )
a095505c 315{
fbbdc52c
SC
316 if ( node->GetChildren() )
317 return node->GetChildren()->GetContent() ;
318 else
319 return wxEmptyString ;
a095505c
SC
320}
321
322// streamer specializations
45212047 323// for all built-in types
a095505c 324
45212047 325// bool
a095505c 326
45212047
SC
327template<> void wxStringReadValue(const wxString &s , bool &data )
328{
329 int intdata ;
330 wxSscanf(s, _T("%d"), &intdata ) ;
331 data = bool(intdata) ;
332}
a095505c 333
45212047 334template<> void wxStringWriteValue(wxString &s , const bool &data )
a095505c 335{
45212047 336 s = wxString::Format("%d", data ) ;
a095505c
SC
337}
338
45212047
SC
339// char
340
341template<> void wxStringReadValue(const wxString &s , char &data )
a095505c 342{
45212047
SC
343 int intdata ;
344 wxSscanf(s, _T("%d"), &intdata ) ;
345 data = char(intdata) ;
346}
347
348template<> void wxStringWriteValue(wxString &s , const char &data )
349{
350 s = wxString::Format("%d", data ) ;
351}
352
353// unsigned char
354
355template<> void wxStringReadValue(const wxString &s , unsigned char &data )
356{
357 int intdata ;
358 wxSscanf(s, _T("%d"), &intdata ) ;
66c57129 359 data = (unsigned char)(intdata) ;
45212047
SC
360}
361
362template<> void wxStringWriteValue(wxString &s , const unsigned char &data )
363{
364 s = wxString::Format("%d", data ) ;
a095505c
SC
365}
366
30fd71e6 367// int
a095505c 368
30fd71e6 369template<> void wxStringReadValue(const wxString &s , int &data )
a095505c
SC
370{
371 wxSscanf(s, _T("%d"), &data ) ;
372}
373
30fd71e6 374template<> void wxStringWriteValue(wxString &s , const int &data )
a095505c
SC
375{
376 s = wxString::Format("%d", data ) ;
377}
378
45212047
SC
379// unsigned int
380
381template<> void wxStringReadValue(const wxString &s , unsigned int &data )
382{
383 wxSscanf(s, _T("%d"), &data ) ;
384}
385
386template<> void wxStringWriteValue(wxString &s , const unsigned int &data )
387{
388 s = wxString::Format("%d", data ) ;
389}
390
391// long
392
393template<> void wxStringReadValue(const wxString &s , long &data )
394{
395 wxSscanf(s, _T("%ld"), &data ) ;
396}
397
398template<> void wxStringWriteValue(wxString &s , const long &data )
399{
400 s = wxString::Format("%ld", data ) ;
401}
402
403// unsigned long
404
405template<> void wxStringReadValue(const wxString &s , unsigned long &data )
406{
407 wxSscanf(s, _T("%ld"), &data ) ;
408}
409
410template<> void wxStringWriteValue(wxString &s , const unsigned long &data )
411{
412 s = wxString::Format("%ld", data ) ;
413}
414
415// float
416
417template<> void wxStringReadValue(const wxString &s , float &data )
418{
419 wxSscanf(s, _T("%f"), &data ) ;
420}
421
422template<> void wxStringWriteValue(wxString &s , const float &data )
423{
424 s = wxString::Format("%f", data ) ;
425}
426
427// double
428
429template<> void wxStringReadValue(const wxString &s , double &data )
430{
431 wxSscanf(s, _T("%lf"), &data ) ;
432}
433
434template<> void wxStringWriteValue(wxString &s , const double &data )
435{
436 s = wxString::Format("%lf", data ) ;
437}
438
a095505c
SC
439// wxString
440
30fd71e6 441template<> void wxStringReadValue(const wxString &s , wxString &data )
a095505c
SC
442{
443 data = s ;
444}
445
30fd71e6 446template<> void wxStringWriteValue(wxString &s , const wxString &data )
a095505c
SC
447{
448 s = data ;
449}
450
451/*
452 Custom Data Streaming / Type Infos
453 we will have to add this for all wx non object types, but it is also an example
454 for custom data structures
455*/
456
457// wxPoint
458
30fd71e6 459template<> void wxStringReadValue(const wxString &s , wxPoint &data )
a095505c
SC
460{
461 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
462}
463
30fd71e6 464template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
a095505c
SC
465{
466 s = wxString::Format("%d,%d", data.x , data.y ) ;
467}
468
f0b7eadf
SC
469template<> void wxStringReadValue(const wxString & , wxPoint* & )
470{
471 assert(0) ;
472}
473
474template<> void wxStringWriteValue(wxString & , wxPoint* const & )
475{
476 assert(0) ;
477}
478
a095505c
SC
479WX_CUSTOM_TYPE_INFO(wxPoint)
480
30fd71e6 481template<> void wxStringReadValue(const wxString &s , wxSize &data )
fbbdc52c
SC
482{
483 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
484}
485
30fd71e6 486template<> void wxStringWriteValue(wxString &s , const wxSize &data )
fbbdc52c
SC
487{
488 s = wxString::Format("%d,%d", data.x , data.y ) ;
489}
490
f0b7eadf
SC
491template<> void wxStringReadValue(const wxString & , wxSize* & )
492{
493 assert(0) ;
494}
495
496template<> void wxStringWriteValue(wxString & , wxSize * const & )
497{
498 assert(0) ;
499}
500
fbbdc52c
SC
501WX_CUSTOM_TYPE_INFO(wxSize)
502
45212047
SC
503/*
504
505template<> void wxStringReadValue(const wxString &s , wxColour &data )
506{
507 // copied from VS xrc
508 unsigned long tmp = 0;
509
510 if (s.Length() != 7 || s[0u] != wxT('#') ||
511 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
512 {
513 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
514 s.c_str() );
515 data = wxNullColour;
516 }
517 else
518 {
519 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
520 (unsigned char) ((tmp & 0x00FF00) >> 8),
521 (unsigned char) ((tmp & 0x0000FF)));
522 }
523}
524
525template<> void wxStringWriteValue(wxString &s , const wxColour &data )
526{
527 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
528}
529
530WX_CUSTOM_TYPE_INFO(wxColour)
531
532*/
533
a095505c
SC
534// removing header dependancy on string tokenizer
535
30fd71e6 536void wxSetStringToArray( const wxString &s , wxArrayString &array )
a095505c
SC
537{
538 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
539 wxString flag;
540 array.Clear() ;
541 while (tokenizer.HasMoreTokens())
542 {
543 array.Add(tokenizer.GetNextToken()) ;
544 }
545}
546
547// ----------------------------------------------------------------------------
30fd71e6 548// wxClassInfo
a095505c
SC
549// ----------------------------------------------------------------------------
550
a095505c
SC
551const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName)
552{
fbbdc52c 553 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
30fd71e6 554
a095505c
SC
555 if ( info )
556 return info->GetAccessor() ;
557
558 return NULL ;
559}
560
fbbdc52c 561const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
a095505c
SC
562{
563 const wxPropertyInfo* info = GetFirstProperty() ;
564
565 while( info )
566 {
567 if ( strcmp( info->GetName() , PropertyName ) == 0 )
568 return info ;
569 info = info->GetNext() ;
570 }
571
572 const wxClassInfo** parents = GetParents() ;
573 for ( int i = 0 ; parents[i] ; ++ i )
574 {
fbbdc52c 575 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
a095505c
SC
576 return info ;
577 }
578
579 return 0;
580}
581
fbbdc52c
SC
582const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
583{
584 const wxHandlerInfo* info = GetFirstHandler() ;
585
586 while( info )
587 {
588 if ( strcmp( info->GetName() , PropertyName ) == 0 )
589 return info ;
590 info = info->GetNext() ;
591 }
592
593 const wxClassInfo** parents = GetParents() ;
594 for ( int i = 0 ; parents[i] ; ++ i )
595 {
596 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
597 return info ;
598 }
599
600 return 0;
601}
602
603
a095505c
SC
604void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value)
605{
606 const wxPropertyAccessor *accessor;
607
608 accessor = FindAccessor(propertyName);
609 wxASSERT(accessor->HasSetter());
610 accessor->SetProperty( object , value ) ;
611}
612
613wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName)
614{
615 const wxPropertyAccessor *accessor;
616
617 accessor = FindAccessor(propertyName);
618 wxASSERT(accessor->HasGetter());
619 return accessor->GetProperty(object);
620}
621
622/*
623VARIANT TO OBJECT
624*/
625
66c57129 626wxObject* wxxVariant::GetAsObject()
a095505c
SC
627{
628 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
629 if ( ti )
630 return ti->GetClassInfo()->VariantToInstance(*this) ;
631 else
632 return NULL ;
633}
634
635
636#endif