template<> added for some compilers
[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 // const wxPoint
208 /*
209 template<> const wxTypeInfo* wxGetTypeInfo( const wxPoint * )
210 {
211 assert(0) ;
212 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
213 return &s_typeInfo ;
214 }
215
216 template<> void wxStringReadValue(const wxString & , const wxPoint & )
217 {
218 assert(0) ;
219 }
220 */
221 // ----------------------------------------------------------------------------
222 // value streaming
223 // ----------------------------------------------------------------------------
224
225 // convenience function (avoids including xml headers in users code)
226
227 void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data )
228 {
229 node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "value", data ) );
230 }
231
232 wxString wxXmlGetContentFromNode( wxXmlNode *node )
233 {
234 if ( node->GetChildren() )
235 return node->GetChildren()->GetContent() ;
236 else
237 return wxEmptyString ;
238 }
239
240 // streamer specializations
241
242 // TODO for all built-in types
243
244 // long
245
246 template<> void wxStringReadValue(const wxString &s , long &data )
247 {
248 wxSscanf(s, _T("%ld"), &data ) ;
249 }
250
251 template<> void wxStringWriteValue(wxString &s , const long &data )
252 {
253 s = wxString::Format("%ld", data ) ;
254 }
255
256 // int
257
258 template<> void wxStringReadValue(const wxString &s , int &data )
259 {
260 wxSscanf(s, _T("%d"), &data ) ;
261 }
262
263 template<> void wxStringWriteValue(wxString &s , const int &data )
264 {
265 s = wxString::Format("%d", data ) ;
266 }
267
268 // wxString
269
270 template<> void wxStringReadValue(const wxString &s , wxString &data )
271 {
272 data = s ;
273 }
274
275 template<> void wxStringWriteValue(wxString &s , const wxString &data )
276 {
277 s = data ;
278 }
279
280 /*
281 Custom Data Streaming / Type Infos
282 we will have to add this for all wx non object types, but it is also an example
283 for custom data structures
284 */
285
286 // wxPoint
287
288 template<> void wxStringReadValue(const wxString &s , wxPoint &data )
289 {
290 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
291 }
292
293 template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
294 {
295 s = wxString::Format("%d,%d", data.x , data.y ) ;
296 }
297
298 WX_CUSTOM_TYPE_INFO(wxPoint)
299
300 template<> void wxStringReadValue(const wxString &s , wxSize &data )
301 {
302 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
303 }
304
305 template<> void wxStringWriteValue(wxString &s , const wxSize &data )
306 {
307 s = wxString::Format("%d,%d", data.x , data.y ) ;
308 }
309
310 WX_CUSTOM_TYPE_INFO(wxSize)
311
312 // removing header dependancy on string tokenizer
313
314 void wxSetStringToArray( const wxString &s , wxArrayString &array )
315 {
316 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
317 wxString flag;
318 array.Clear() ;
319 while (tokenizer.HasMoreTokens())
320 {
321 array.Add(tokenizer.GetNextToken()) ;
322 }
323 }
324
325 // ----------------------------------------------------------------------------
326 // wxClassInfo
327 // ----------------------------------------------------------------------------
328
329
330 void wxClassInfo::Register(const char *WXUNUSED(name), wxClassInfo *WXUNUSED(info))
331 {
332 /*
333 if (!ExtendedTypeMap)
334 ExtendedTypeMap = new ClassMap;
335 (*ExtendedTypeMap)[string(Name)] = Info;
336 */
337 }
338
339 void wxClassInfo::Unregister(const char *WXUNUSED(name))
340 {
341 /*
342 assert(ExtendedTypeMap);
343 ExtendedTypeMap->erase(Name);
344 */
345 }
346
347 const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName)
348 {
349 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
350
351 if ( info )
352 return info->GetAccessor() ;
353
354 return NULL ;
355 }
356
357 const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
358 {
359 const wxPropertyInfo* info = GetFirstProperty() ;
360
361 while( info )
362 {
363 if ( strcmp( info->GetName() , PropertyName ) == 0 )
364 return info ;
365 info = info->GetNext() ;
366 }
367
368 const wxClassInfo** parents = GetParents() ;
369 for ( int i = 0 ; parents[i] ; ++ i )
370 {
371 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
372 return info ;
373 }
374
375 return 0;
376 }
377
378 const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
379 {
380 const wxHandlerInfo* info = GetFirstHandler() ;
381
382 while( info )
383 {
384 if ( strcmp( info->GetName() , PropertyName ) == 0 )
385 return info ;
386 info = info->GetNext() ;
387 }
388
389 const wxClassInfo** parents = GetParents() ;
390 for ( int i = 0 ; parents[i] ; ++ i )
391 {
392 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
393 return info ;
394 }
395
396 return 0;
397 }
398
399
400 void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value)
401 {
402 const wxPropertyAccessor *accessor;
403
404 accessor = FindAccessor(propertyName);
405 wxASSERT(accessor->HasSetter());
406 accessor->SetProperty( object , value ) ;
407 }
408
409 wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName)
410 {
411 const wxPropertyAccessor *accessor;
412
413 accessor = FindAccessor(propertyName);
414 wxASSERT(accessor->HasGetter());
415 return accessor->GetProperty(object);
416 }
417
418 /*
419 VARIANT TO OBJECT
420 */
421
422 wxObject* wxxVariant::GetAsObject() const
423 {
424 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
425 if ( ti )
426 return ti->GetClassInfo()->VariantToInstance(*this) ;
427 else
428 return NULL ;
429 }
430
431
432 #endif