]>
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 | ||
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 | */ | |
a095505c | 221 | // ---------------------------------------------------------------------------- |
30fd71e6 | 222 | // value streaming |
a095505c SC |
223 | // ---------------------------------------------------------------------------- |
224 | ||
225 | // convenience function (avoids including xml headers in users code) | |
226 | ||
30fd71e6 | 227 | void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data ) |
a095505c | 228 | { |
30fd71e6 | 229 | node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, "value", data ) ); |
a095505c SC |
230 | } |
231 | ||
30fd71e6 | 232 | wxString wxXmlGetContentFromNode( wxXmlNode *node ) |
a095505c | 233 | { |
fbbdc52c SC |
234 | if ( node->GetChildren() ) |
235 | return node->GetChildren()->GetContent() ; | |
236 | else | |
237 | return wxEmptyString ; | |
a095505c SC |
238 | } |
239 | ||
240 | // streamer specializations | |
241 | ||
242 | // TODO for all built-in types | |
243 | ||
244 | // long | |
245 | ||
30fd71e6 | 246 | template<> void wxStringReadValue(const wxString &s , long &data ) |
a095505c SC |
247 | { |
248 | wxSscanf(s, _T("%ld"), &data ) ; | |
249 | } | |
250 | ||
30fd71e6 | 251 | template<> void wxStringWriteValue(wxString &s , const long &data ) |
a095505c SC |
252 | { |
253 | s = wxString::Format("%ld", data ) ; | |
254 | } | |
255 | ||
30fd71e6 | 256 | // int |
a095505c | 257 | |
30fd71e6 | 258 | template<> void wxStringReadValue(const wxString &s , int &data ) |
a095505c SC |
259 | { |
260 | wxSscanf(s, _T("%d"), &data ) ; | |
261 | } | |
262 | ||
30fd71e6 | 263 | template<> void wxStringWriteValue(wxString &s , const int &data ) |
a095505c SC |
264 | { |
265 | s = wxString::Format("%d", data ) ; | |
266 | } | |
267 | ||
268 | // wxString | |
269 | ||
30fd71e6 | 270 | template<> void wxStringReadValue(const wxString &s , wxString &data ) |
a095505c SC |
271 | { |
272 | data = s ; | |
273 | } | |
274 | ||
30fd71e6 | 275 | template<> void wxStringWriteValue(wxString &s , const wxString &data ) |
a095505c SC |
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 | ||
30fd71e6 | 288 | template<> void wxStringReadValue(const wxString &s , wxPoint &data ) |
a095505c SC |
289 | { |
290 | wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ; | |
291 | } | |
292 | ||
30fd71e6 | 293 | template<> void wxStringWriteValue(wxString &s , const wxPoint &data ) |
a095505c SC |
294 | { |
295 | s = wxString::Format("%d,%d", data.x , data.y ) ; | |
296 | } | |
297 | ||
298 | WX_CUSTOM_TYPE_INFO(wxPoint) | |
299 | ||
30fd71e6 | 300 | template<> void wxStringReadValue(const wxString &s , wxSize &data ) |
fbbdc52c SC |
301 | { |
302 | wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ; | |
303 | } | |
304 | ||
30fd71e6 | 305 | template<> void wxStringWriteValue(wxString &s , const wxSize &data ) |
fbbdc52c SC |
306 | { |
307 | s = wxString::Format("%d,%d", data.x , data.y ) ; | |
308 | } | |
309 | ||
310 | WX_CUSTOM_TYPE_INFO(wxSize) | |
311 | ||
a095505c SC |
312 | // removing header dependancy on string tokenizer |
313 | ||
30fd71e6 | 314 | void wxSetStringToArray( const wxString &s , wxArrayString &array ) |
a095505c SC |
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 | // ---------------------------------------------------------------------------- | |
30fd71e6 | 326 | // wxClassInfo |
a095505c SC |
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 | { | |
fbbdc52c | 349 | const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ; |
30fd71e6 | 350 | |
a095505c SC |
351 | if ( info ) |
352 | return info->GetAccessor() ; | |
353 | ||
354 | return NULL ; | |
355 | } | |
356 | ||
fbbdc52c | 357 | const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const |
a095505c SC |
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 | { | |
fbbdc52c | 371 | if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL ) |
a095505c SC |
372 | return info ; |
373 | } | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
fbbdc52c SC |
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 | ||
a095505c SC |
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 |