]>
Commit | Line | Data |
---|---|---|
341287bf JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: variant.cpp | |
3 | // Purpose: wxVariant class, container for any type | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 10/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) | |
3f4a0c5b | 9 | // Licence: wxWindows licence |
341287bf JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "variant.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
3f4a0c5b | 20 | #pragma hdrstop |
341287bf JS |
21 | #endif |
22 | ||
38830220 | 23 | #if wxUSE_IOSTREAMH && wxUSE_STD_IOSTREAM |
03f38c58 | 24 | # include <fstream.h> |
fbc535ff | 25 | #else |
03f38c58 | 26 | # include <fstream> |
fbc535ff | 27 | #endif |
341287bf | 28 | |
75ed1d15 | 29 | #include "wx/stream.h" |
341287bf JS |
30 | #include "wx/string.h" |
31 | #include "wx/variant.h" | |
32 | ||
33 | IMPLEMENT_ABSTRACT_CLASS(wxVariantData, wxObject) | |
34 | ||
3ba055ac | 35 | wxVariant WXDLLEXPORT wxNullVariant; |
341287bf JS |
36 | |
37 | /* | |
38 | * wxVariantDataList | |
39 | */ | |
40 | ||
41 | class WXDLLEXPORT wxVariantDataList: public wxVariantData | |
42 | { | |
43 | DECLARE_DYNAMIC_CLASS(wxVariantDataList) | |
44 | public: | |
45 | wxVariantDataList() {} | |
46 | wxVariantDataList(const wxList& list); | |
47 | ~wxVariantDataList(); | |
48 | ||
49 | wxList& GetValue() const { return (wxList&) m_value; } | |
50 | void SetValue(const wxList& value) ; | |
51 | ||
52 | virtual void Copy(wxVariantData& data); | |
53 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 54 | #if wxUSE_STD_IOSTREAM |
341287bf | 55 | virtual bool Write(ostream& str) const; |
38830220 | 56 | #endif |
341287bf | 57 | virtual bool Write(wxString& str) const; |
38830220 | 58 | #if wxUSE_STD_IOSTREAM |
341287bf | 59 | virtual bool Read(istream& str); |
38830220 | 60 | #endif |
341287bf | 61 | virtual bool Read(wxString& str); |
783b6cfd | 62 | virtual wxString GetType() const { return _T("list"); }; |
341287bf JS |
63 | |
64 | void Clear(); | |
65 | ||
66 | protected: | |
67 | wxList m_value; | |
68 | }; | |
69 | ||
70 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList, wxVariantData) | |
71 | ||
72 | wxVariantDataList::wxVariantDataList(const wxList& list) | |
73 | { | |
74 | SetValue(list); | |
75 | } | |
76 | ||
77 | wxVariantDataList::~wxVariantDataList() | |
78 | { | |
79 | Clear(); | |
80 | } | |
81 | ||
82 | void wxVariantDataList::SetValue(const wxList& value) | |
83 | { | |
84 | Clear(); | |
85 | wxNode* node = value.First(); | |
86 | while (node) | |
87 | { | |
88 | wxVariant* var = (wxVariant*) node->Data(); | |
89 | m_value.Append(new wxVariant(*var)); | |
90 | node = node->Next(); | |
91 | } | |
92 | } | |
93 | ||
94 | void wxVariantDataList::Clear() | |
95 | { | |
96 | wxNode* node = m_value.First(); | |
97 | while (node) | |
98 | { | |
99 | wxVariant* var = (wxVariant*) node->Data(); | |
100 | delete var; | |
101 | node = node->Next(); | |
102 | } | |
103 | m_value.Clear(); | |
104 | } | |
105 | ||
106 | void wxVariantDataList::Copy(wxVariantData& data) | |
107 | { | |
783b6cfd | 108 | wxASSERT_MSG( (data.GetType() == _T("list")), _T("wxVariantDataList::Copy: Can't copy to this type of data") ); |
341287bf JS |
109 | |
110 | wxVariantDataList& listData = (wxVariantDataList&) data; | |
111 | ||
112 | listData.Clear(); | |
113 | wxNode* node = m_value.First(); | |
114 | while (node) | |
115 | { | |
116 | wxVariant* var = (wxVariant*) node->Data(); | |
117 | listData.m_value.Append(new wxVariant(*var)); | |
118 | node = node->Next(); | |
119 | } | |
120 | } | |
121 | ||
122 | bool wxVariantDataList::Eq(wxVariantData& data) const | |
123 | { | |
783b6cfd | 124 | wxASSERT_MSG( (data.GetType() == _T("list")), _T("wxVariantDataList::Eq: argument mismatch") ); |
341287bf JS |
125 | |
126 | wxVariantDataList& listData = (wxVariantDataList&) data; | |
127 | wxNode* node1 = m_value.First(); | |
128 | wxNode* node2 = listData.GetValue().First(); | |
129 | while (node1 && node2) | |
130 | { | |
131 | wxVariant* var1 = (wxVariant*) node1->Data(); | |
132 | wxVariant* var2 = (wxVariant*) node2->Data(); | |
133 | if ((*var1) != (*var2)) | |
134 | return FALSE; | |
135 | node1 = node1->Next(); | |
136 | node2 = node2->Next(); | |
137 | } | |
138 | if (node1 || node2) return FALSE; | |
139 | return TRUE; | |
140 | } | |
141 | ||
38830220 | 142 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
143 | bool wxVariantDataList::Write(ostream& str) const |
144 | { | |
145 | wxString s; | |
146 | Write(s); | |
783b6cfd | 147 | str << (const char*) s.mb_str(); |
341287bf JS |
148 | return TRUE; |
149 | } | |
38830220 | 150 | #endif |
341287bf JS |
151 | |
152 | bool wxVariantDataList::Write(wxString& str) const | |
153 | { | |
783b6cfd | 154 | str = _T(""); |
341287bf JS |
155 | wxNode* node = m_value.First(); |
156 | while (node) | |
157 | { | |
158 | wxVariant* var = (wxVariant*) node->Data(); | |
159 | if (node != m_value.First()) | |
783b6cfd | 160 | str += _T(" "); |
341287bf JS |
161 | wxString str1; |
162 | str += var->MakeString(); | |
163 | node = node->Next(); | |
164 | } | |
165 | ||
166 | return TRUE; | |
167 | } | |
168 | ||
38830220 | 169 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
170 | bool wxVariantDataList::Read(istream& WXUNUSED(str)) |
171 | { | |
783b6cfd | 172 | wxFAIL_MSG(_T("Unimplemented")); |
341287bf JS |
173 | // TODO |
174 | return FALSE; | |
175 | } | |
38830220 | 176 | #endif |
341287bf JS |
177 | |
178 | bool wxVariantDataList::Read(wxString& WXUNUSED(str)) | |
179 | { | |
783b6cfd | 180 | wxFAIL_MSG(_T("Unimplemented")); |
341287bf JS |
181 | // TODO |
182 | return FALSE; | |
183 | } | |
184 | ||
185 | /* | |
186 | * wxVariantDataStringList | |
187 | */ | |
188 | ||
189 | class WXDLLEXPORT wxVariantDataStringList: public wxVariantData | |
190 | { | |
191 | DECLARE_DYNAMIC_CLASS(wxVariantDataStringList) | |
192 | public: | |
193 | wxVariantDataStringList() {} | |
194 | wxVariantDataStringList(const wxStringList& list) { m_value = list; } | |
195 | ||
196 | wxStringList& GetValue() const { return (wxStringList&) m_value; } | |
197 | void SetValue(const wxStringList& value); | |
198 | ||
199 | virtual void Copy(wxVariantData& data); | |
200 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 201 | #if wxUSE_STD_IOSTREAM |
341287bf | 202 | virtual bool Write(ostream& str) const; |
38830220 | 203 | #endif |
341287bf | 204 | virtual bool Write(wxString& str) const; |
38830220 | 205 | #if wxUSE_STD_IOSTREAM |
341287bf | 206 | virtual bool Read(istream& str); |
38830220 | 207 | #endif |
341287bf | 208 | virtual bool Read(wxString& str); |
783b6cfd | 209 | virtual wxString GetType() const { return _T("stringlist"); }; |
341287bf JS |
210 | |
211 | protected: | |
212 | wxStringList m_value; | |
213 | }; | |
214 | ||
215 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList, wxVariantData) | |
216 | ||
217 | void wxVariantDataStringList::SetValue(const wxStringList& value) | |
218 | { | |
219 | m_value = value; | |
220 | } | |
221 | ||
222 | void wxVariantDataStringList::Copy(wxVariantData& data) | |
223 | { | |
783b6cfd | 224 | wxASSERT_MSG( (data.GetType() == _T("stringlist")), _T("wxVariantDataStringList::Copy: Can't copy to this type of data") ); |
341287bf JS |
225 | |
226 | wxVariantDataStringList& listData = (wxVariantDataStringList&) data; | |
227 | ||
228 | listData.m_value = m_value ; | |
229 | } | |
230 | ||
231 | bool wxVariantDataStringList::Eq(wxVariantData& data) const | |
232 | { | |
783b6cfd | 233 | wxASSERT_MSG( (data.GetType() == _T("stringlist")), _T("wxVariantDataStringList::Eq: argument mismatch") ); |
341287bf JS |
234 | |
235 | wxVariantDataStringList& listData = (wxVariantDataStringList&) data; | |
236 | wxNode* node1 = m_value.First(); | |
237 | wxNode* node2 = listData.GetValue().First(); | |
238 | while (node1 && node2) | |
239 | { | |
783b6cfd OK |
240 | wxString str1 ((wxChar*) node1->Data()); |
241 | wxString str2 ((wxChar*) node2->Data()); | |
341287bf JS |
242 | if (str1 != str2) |
243 | return FALSE; | |
244 | node1 = node1->Next(); | |
245 | node2 = node2->Next(); | |
246 | } | |
247 | if (node1 || node2) return FALSE; | |
248 | return TRUE; | |
249 | } | |
250 | ||
38830220 | 251 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
252 | bool wxVariantDataStringList::Write(ostream& str) const |
253 | { | |
254 | wxString s; | |
255 | Write(s); | |
783b6cfd | 256 | str << (const char*) s.mb_str(); |
341287bf JS |
257 | return TRUE; |
258 | } | |
38830220 | 259 | #endif |
341287bf JS |
260 | |
261 | bool wxVariantDataStringList::Write(wxString& str) const | |
262 | { | |
783b6cfd | 263 | str = _T(""); |
341287bf JS |
264 | wxNode* node = m_value.First(); |
265 | while (node) | |
266 | { | |
783b6cfd | 267 | wxChar* s = (wxChar*) node->Data(); |
341287bf | 268 | if (node != m_value.First()) |
783b6cfd | 269 | str += _T(" "); |
341287bf JS |
270 | str += s; |
271 | node = node->Next(); | |
272 | } | |
273 | ||
274 | return TRUE; | |
275 | } | |
276 | ||
38830220 | 277 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
278 | bool wxVariantDataStringList::Read(istream& WXUNUSED(str)) |
279 | { | |
783b6cfd | 280 | wxFAIL_MSG(_T("Unimplemented")); |
341287bf JS |
281 | // TODO |
282 | return FALSE; | |
283 | } | |
38830220 | 284 | #endif |
341287bf JS |
285 | |
286 | bool wxVariantDataStringList::Read(wxString& WXUNUSED(str)) | |
287 | { | |
783b6cfd | 288 | wxFAIL_MSG(_T("Unimplemented")); |
341287bf JS |
289 | // TODO |
290 | return FALSE; | |
291 | } | |
292 | ||
293 | /* | |
294 | * wxVariantDataLong | |
295 | */ | |
296 | ||
297 | class WXDLLEXPORT wxVariantDataLong: public wxVariantData | |
298 | { | |
299 | DECLARE_DYNAMIC_CLASS(wxVariantDataLong) | |
300 | public: | |
301 | wxVariantDataLong() { m_value = 0; } | |
302 | wxVariantDataLong(long value) { m_value = value; } | |
303 | ||
304 | inline long GetValue() const { return m_value; } | |
305 | inline void SetValue(long value) { m_value = value; } | |
306 | ||
307 | virtual void Copy(wxVariantData& data); | |
308 | virtual bool Eq(wxVariantData& data) const; | |
1ccbb61a VZ |
309 | |
310 | virtual bool Read(wxString& str); | |
341287bf | 311 | virtual bool Write(wxString& str) const; |
38830220 | 312 | #if wxUSE_STD_IOSTREAM |
341287bf | 313 | virtual bool Read(istream& str); |
1ccbb61a | 314 | virtual bool Write(ostream& str) const; |
38830220 | 315 | #endif |
e02afc7a | 316 | #if wxUSE_STREAMS |
75ed1d15 | 317 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 318 | virtual bool Write(wxOutputStream &str) const; |
e02afc7a | 319 | #endif // wxUSE_STREAMS |
1ccbb61a | 320 | |
783b6cfd | 321 | virtual wxString GetType() const { return _T("long"); }; |
341287bf JS |
322 | |
323 | protected: | |
324 | long m_value; | |
325 | }; | |
326 | ||
327 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong, wxVariantData) | |
328 | ||
329 | void wxVariantDataLong::Copy(wxVariantData& data) | |
330 | { | |
783b6cfd | 331 | wxASSERT_MSG( (data.GetType() == _T("long")), _T("wxVariantDataLong::Copy: Can't copy to this type of data") ); |
341287bf JS |
332 | |
333 | wxVariantDataLong& otherData = (wxVariantDataLong&) data; | |
334 | ||
335 | otherData.m_value = m_value; | |
336 | } | |
337 | ||
338 | bool wxVariantDataLong::Eq(wxVariantData& data) const | |
339 | { | |
783b6cfd | 340 | wxASSERT_MSG( (data.GetType() == _T("long")), _T("wxVariantDataLong::Eq: argument mismatch") ); |
341287bf JS |
341 | |
342 | wxVariantDataLong& otherData = (wxVariantDataLong&) data; | |
343 | ||
344 | return (otherData.m_value == m_value); | |
345 | } | |
346 | ||
38830220 | 347 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
348 | bool wxVariantDataLong::Write(ostream& str) const |
349 | { | |
350 | wxString s; | |
351 | Write(s); | |
783b6cfd | 352 | str << (const char*) s.mb_str(); |
341287bf JS |
353 | return TRUE; |
354 | } | |
38830220 | 355 | #endif |
341287bf JS |
356 | |
357 | bool wxVariantDataLong::Write(wxString& str) const | |
358 | { | |
783b6cfd | 359 | str.Printf(_T("%ld"), m_value); |
341287bf JS |
360 | return TRUE; |
361 | } | |
362 | ||
38830220 | 363 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
364 | bool wxVariantDataLong::Read(istream& str) |
365 | { | |
366 | str >> m_value; | |
367 | return TRUE; | |
368 | } | |
38830220 | 369 | #endif |
341287bf | 370 | |
e02afc7a | 371 | #if wxUSE_STREAMS |
1ccbb61a VZ |
372 | bool wxVariantDataLong::Write(wxOutputStream& str) const |
373 | { | |
374 | str << m_value; | |
375 | return TRUE; | |
376 | } | |
377 | ||
75ed1d15 GL |
378 | bool wxVariantDataLong::Read(wxInputStream& str) |
379 | { | |
380 | str >> m_value; | |
381 | return TRUE; | |
382 | } | |
e02afc7a | 383 | #endif // wxUSE_STREAMS |
75ed1d15 | 384 | |
341287bf JS |
385 | bool wxVariantDataLong::Read(wxString& str) |
386 | { | |
783b6cfd | 387 | m_value = wxAtol((const wxChar*) str); |
341287bf JS |
388 | return TRUE; |
389 | } | |
390 | ||
391 | /* | |
392 | * wxVariantDataReal | |
393 | */ | |
394 | ||
395 | class WXDLLEXPORT wxVariantDataReal: public wxVariantData | |
396 | { | |
397 | DECLARE_DYNAMIC_CLASS(wxVariantDataReal) | |
398 | public: | |
399 | wxVariantDataReal() { m_value = 0.0; } | |
400 | wxVariantDataReal(double value) { m_value = value; } | |
401 | ||
402 | inline double GetValue() const { return m_value; } | |
403 | inline void SetValue(double value) { m_value = value; } | |
404 | ||
405 | virtual void Copy(wxVariantData& data); | |
406 | virtual bool Eq(wxVariantData& data) const; | |
1ccbb61a | 407 | virtual bool Read(wxString& str); |
38830220 | 408 | #if wxUSE_STD_IOSTREAM |
341287bf | 409 | virtual bool Write(ostream& str) const; |
38830220 | 410 | #endif |
341287bf | 411 | virtual bool Write(wxString& str) const; |
38830220 | 412 | #if wxUSE_STD_IOSTREAM |
341287bf | 413 | virtual bool Read(istream& str); |
38830220 | 414 | #endif |
e02afc7a | 415 | #if wxUSE_STREAMS |
75ed1d15 | 416 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 417 | virtual bool Write(wxOutputStream &str) const; |
e02afc7a | 418 | #endif // wxUSE_STREAMS |
783b6cfd | 419 | virtual wxString GetType() const { return _T("double"); }; |
341287bf JS |
420 | |
421 | protected: | |
422 | double m_value; | |
423 | }; | |
424 | ||
425 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal, wxVariantData) | |
426 | ||
427 | void wxVariantDataReal::Copy(wxVariantData& data) | |
428 | { | |
783b6cfd | 429 | wxASSERT_MSG( (data.GetType() == _T("double")), _T("wxVariantDataReal::Copy: Can't copy to this type of data") ); |
341287bf JS |
430 | |
431 | wxVariantDataReal& otherData = (wxVariantDataReal&) data; | |
432 | ||
433 | otherData.m_value = m_value; | |
434 | } | |
435 | ||
436 | bool wxVariantDataReal::Eq(wxVariantData& data) const | |
437 | { | |
783b6cfd | 438 | wxASSERT_MSG( (data.GetType() == _T("double")), _T("wxVariantDataReal::Eq: argument mismatch") ); |
341287bf JS |
439 | |
440 | wxVariantDataReal& otherData = (wxVariantDataReal&) data; | |
441 | ||
442 | return (otherData.m_value == m_value); | |
443 | } | |
444 | ||
38830220 | 445 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
446 | bool wxVariantDataReal::Write(ostream& str) const |
447 | { | |
448 | wxString s; | |
449 | Write(s); | |
783b6cfd | 450 | str << (const char*) s.mb_str(); |
341287bf JS |
451 | return TRUE; |
452 | } | |
38830220 | 453 | #endif |
341287bf JS |
454 | |
455 | bool wxVariantDataReal::Write(wxString& str) const | |
456 | { | |
783b6cfd | 457 | str.Printf(_T("%.4f"), m_value); |
341287bf JS |
458 | return TRUE; |
459 | } | |
460 | ||
38830220 | 461 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
462 | bool wxVariantDataReal::Read(istream& str) |
463 | { | |
464 | str >> m_value; | |
465 | return TRUE; | |
466 | } | |
38830220 | 467 | #endif |
341287bf | 468 | |
e02afc7a | 469 | #if wxUSE_STREAMS |
1ccbb61a VZ |
470 | bool wxVariantDataReal::Write(wxOutputStream& str) const |
471 | { | |
472 | str << m_value; | |
473 | return TRUE; | |
474 | } | |
475 | ||
75ed1d15 GL |
476 | bool wxVariantDataReal::Read(wxInputStream& str) |
477 | { | |
478 | str >> (float&)m_value; | |
479 | return TRUE; | |
480 | } | |
e02afc7a | 481 | #endif // wxUSE_STREAMS |
75ed1d15 | 482 | |
341287bf JS |
483 | bool wxVariantDataReal::Read(wxString& str) |
484 | { | |
783b6cfd | 485 | m_value = wxAtof((const wxChar*) str); |
341287bf JS |
486 | return TRUE; |
487 | } | |
488 | ||
57493f9f | 489 | #ifdef HAVE_BOOL |
341287bf JS |
490 | /* |
491 | * wxVariantDataBool | |
492 | */ | |
493 | ||
494 | class WXDLLEXPORT wxVariantDataBool: public wxVariantData | |
495 | { | |
496 | DECLARE_DYNAMIC_CLASS(wxVariantDataBool) | |
497 | public: | |
498 | wxVariantDataBool() { m_value = 0; } | |
499 | wxVariantDataBool(bool value) { m_value = value; } | |
500 | ||
501 | inline bool GetValue() const { return m_value; } | |
502 | inline void SetValue(bool value) { m_value = value; } | |
503 | ||
504 | virtual void Copy(wxVariantData& data); | |
505 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 506 | #if wxUSE_STD_IOSTREAM |
341287bf | 507 | virtual bool Write(ostream& str) const; |
38830220 | 508 | #endif |
341287bf | 509 | virtual bool Write(wxString& str) const; |
1ccbb61a | 510 | virtual bool Read(wxString& str); |
38830220 | 511 | #if wxUSE_STD_IOSTREAM |
341287bf | 512 | virtual bool Read(istream& str); |
38830220 | 513 | #endif |
e02afc7a | 514 | #if wxUSE_STREAMS |
75ed1d15 | 515 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 516 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 517 | #endif // wxUSE_STREAMS |
783b6cfd | 518 | virtual wxString GetType() const { return _T("bool"); }; |
341287bf JS |
519 | |
520 | protected: | |
521 | bool m_value; | |
522 | }; | |
523 | ||
524 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool, wxVariantData) | |
525 | ||
526 | void wxVariantDataBool::Copy(wxVariantData& data) | |
527 | { | |
783b6cfd | 528 | wxASSERT_MSG( (data.GetType() == _T("bool")), _T("wxVariantDataBool::Copy: Can't copy to this type of data") ); |
341287bf JS |
529 | |
530 | wxVariantDataBool& otherData = (wxVariantDataBool&) data; | |
531 | ||
532 | otherData.m_value = m_value; | |
533 | } | |
534 | ||
535 | bool wxVariantDataBool::Eq(wxVariantData& data) const | |
536 | { | |
783b6cfd | 537 | wxASSERT_MSG( (data.GetType() == _T("bool")), _T("wxVariantDataBool::Eq: argument mismatch") ); |
341287bf JS |
538 | |
539 | wxVariantDataBool& otherData = (wxVariantDataBool&) data; | |
540 | ||
541 | return (otherData.m_value == m_value); | |
542 | } | |
543 | ||
38830220 | 544 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
545 | bool wxVariantDataBool::Write(ostream& str) const |
546 | { | |
547 | wxString s; | |
548 | Write(s); | |
783b6cfd | 549 | str << (const char*) s.mb_str(); |
341287bf JS |
550 | return TRUE; |
551 | } | |
38830220 | 552 | #endif |
341287bf JS |
553 | |
554 | bool wxVariantDataBool::Write(wxString& str) const | |
555 | { | |
783b6cfd | 556 | str.Printf(_T("%d"), (int) m_value); |
341287bf JS |
557 | return TRUE; |
558 | } | |
559 | ||
38830220 | 560 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
561 | bool wxVariantDataBool::Read(istream& WXUNUSED(str)) |
562 | { | |
783b6cfd | 563 | wxFAIL_MSG(_T("Unimplemented")); |
341287bf JS |
564 | // str >> (long) m_value; |
565 | return FALSE; | |
566 | } | |
38830220 | 567 | #endif |
341287bf | 568 | |
e02afc7a | 569 | #if wxUSE_STREAMS |
1ccbb61a VZ |
570 | bool wxVariantDataBool::Write(wxOutputStream& str) const |
571 | { | |
572 | str << (char)m_value; | |
573 | return TRUE; | |
574 | } | |
575 | ||
75ed1d15 GL |
576 | bool wxVariantDataBool::Read(wxInputStream& str) |
577 | { | |
578 | str >> (char&)m_value; | |
579 | return TRUE; | |
580 | } | |
e02afc7a | 581 | #endif // wxUSE_STREAMS |
75ed1d15 | 582 | |
341287bf JS |
583 | bool wxVariantDataBool::Read(wxString& str) |
584 | { | |
783b6cfd | 585 | m_value = (wxAtol((const wxChar*) str) != 0); |
341287bf JS |
586 | return TRUE; |
587 | } | |
57493f9f | 588 | #endif // HAVE_BOOL |
341287bf JS |
589 | |
590 | /* | |
591 | * wxVariantDataChar | |
592 | */ | |
593 | ||
594 | class WXDLLEXPORT wxVariantDataChar: public wxVariantData | |
595 | { | |
596 | DECLARE_DYNAMIC_CLASS(wxVariantDataChar) | |
597 | public: | |
598 | wxVariantDataChar() { m_value = 0; } | |
599 | wxVariantDataChar(char value) { m_value = value; } | |
600 | ||
601 | inline char GetValue() const { return m_value; } | |
602 | inline void SetValue(char value) { m_value = value; } | |
603 | ||
604 | virtual void Copy(wxVariantData& data); | |
605 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 606 | #if wxUSE_STD_IOSTREAM |
1ccbb61a | 607 | virtual bool Read(istream& str); |
341287bf | 608 | virtual bool Write(ostream& str) const; |
38830220 | 609 | #endif |
1ccbb61a | 610 | virtual bool Read(wxString& str); |
341287bf | 611 | virtual bool Write(wxString& str) const; |
e02afc7a | 612 | #if wxUSE_STREAMS |
75ed1d15 | 613 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 614 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 615 | #endif // wxUSE_STREAMS |
783b6cfd | 616 | virtual wxString GetType() const { return _T("char"); }; |
341287bf JS |
617 | |
618 | protected: | |
619 | char m_value; | |
620 | }; | |
621 | ||
622 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar, wxVariantData) | |
623 | ||
624 | void wxVariantDataChar::Copy(wxVariantData& data) | |
625 | { | |
783b6cfd | 626 | wxASSERT_MSG( (data.GetType() == _T("char")), _T("wxVariantDataChar::Copy: Can't copy to this type of data") ); |
341287bf JS |
627 | |
628 | wxVariantDataChar& otherData = (wxVariantDataChar&) data; | |
629 | ||
630 | otherData.m_value = m_value; | |
631 | } | |
632 | ||
633 | bool wxVariantDataChar::Eq(wxVariantData& data) const | |
634 | { | |
783b6cfd | 635 | wxASSERT_MSG( (data.GetType() == _T("char")), _T("wxVariantDataChar::Eq: argument mismatch") ); |
341287bf JS |
636 | |
637 | wxVariantDataChar& otherData = (wxVariantDataChar&) data; | |
638 | ||
639 | return (otherData.m_value == m_value); | |
640 | } | |
641 | ||
38830220 | 642 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
643 | bool wxVariantDataChar::Write(ostream& str) const |
644 | { | |
645 | wxString s; | |
646 | Write(s); | |
783b6cfd | 647 | str << (const char*) s.mb_str(); |
341287bf JS |
648 | return TRUE; |
649 | } | |
38830220 | 650 | #endif |
341287bf JS |
651 | |
652 | bool wxVariantDataChar::Write(wxString& str) const | |
653 | { | |
783b6cfd | 654 | str.Printf(_T("%c"), m_value); |
341287bf JS |
655 | return TRUE; |
656 | } | |
657 | ||
38830220 | 658 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
659 | bool wxVariantDataChar::Read(istream& WXUNUSED(str)) |
660 | { | |
783b6cfd | 661 | wxFAIL_MSG(_T("Unimplemented")); |
341287bf JS |
662 | // str >> m_value; |
663 | return FALSE; | |
664 | } | |
38830220 | 665 | #endif |
341287bf | 666 | |
e02afc7a | 667 | #if wxUSE_STREAMS |
1ccbb61a VZ |
668 | bool wxVariantDataChar::Write(wxOutputStream& str) const |
669 | { | |
670 | str << m_value; | |
671 | return TRUE; | |
672 | } | |
673 | ||
75ed1d15 GL |
674 | bool wxVariantDataChar::Read(wxInputStream& str) |
675 | { | |
676 | str >> m_value; | |
677 | return TRUE; | |
678 | } | |
e02afc7a | 679 | #endif // wxUSE_STREAMS |
75ed1d15 | 680 | |
341287bf JS |
681 | bool wxVariantDataChar::Read(wxString& str) |
682 | { | |
683 | m_value = str[(size_t)0]; | |
684 | return TRUE; | |
685 | } | |
686 | ||
687 | /* | |
688 | * wxVariantDataString | |
689 | */ | |
690 | ||
62448488 JS |
691 | #if defined(__BORLANDC__) && defined(__WIN16__) |
692 | // Change name because of truncation | |
693 | #define wxVariantDataString wxVariantStringData | |
694 | #endif | |
695 | ||
341287bf JS |
696 | class WXDLLEXPORT wxVariantDataString: public wxVariantData |
697 | { | |
62448488 JS |
698 | #if defined(__BORLANDC__) && defined(__WIN16__) |
699 | DECLARE_DYNAMIC_CLASS(wxVariantStringData) | |
700 | #else | |
341287bf | 701 | DECLARE_DYNAMIC_CLASS(wxVariantDataString) |
62448488 | 702 | #endif |
341287bf JS |
703 | public: |
704 | wxVariantDataString() { } | |
705 | wxVariantDataString(const wxString& value) { m_value = value; } | |
706 | ||
707 | inline wxString GetValue() const { return m_value; } | |
708 | inline void SetValue(const wxString& value) { m_value = value; } | |
709 | ||
710 | virtual void Copy(wxVariantData& data); | |
711 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 712 | #if wxUSE_STD_IOSTREAM |
341287bf | 713 | virtual bool Write(ostream& str) const; |
38830220 | 714 | #endif |
1ccbb61a | 715 | virtual bool Read(wxString& str); |
341287bf | 716 | virtual bool Write(wxString& str) const; |
38830220 | 717 | #if wxUSE_STD_IOSTREAM |
341287bf | 718 | virtual bool Read(istream& str); |
38830220 | 719 | #endif |
e02afc7a | 720 | #if wxUSE_STREAMS |
75ed1d15 | 721 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 722 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 723 | #endif // wxUSE_STREAMS |
783b6cfd | 724 | virtual wxString GetType() const { return _T("string"); }; |
341287bf JS |
725 | |
726 | protected: | |
727 | wxString m_value; | |
728 | }; | |
729 | ||
730 | void wxVariantDataString::Copy(wxVariantData& data) | |
731 | { | |
783b6cfd | 732 | wxASSERT_MSG( (data.GetType() == _T("string")), _T("wxVariantDataString::Copy: Can't copy to this type of data") ); |
341287bf JS |
733 | |
734 | wxVariantDataString& otherData = (wxVariantDataString&) data; | |
735 | ||
736 | otherData.m_value = m_value; | |
737 | } | |
738 | ||
739 | bool wxVariantDataString::Eq(wxVariantData& data) const | |
740 | { | |
783b6cfd | 741 | wxASSERT_MSG( (data.GetType() == _T("string")), _T("wxVariantDataString::Eq: argument mismatch") ); |
341287bf JS |
742 | |
743 | wxVariantDataString& otherData = (wxVariantDataString&) data; | |
744 | ||
745 | return (otherData.m_value == m_value); | |
746 | } | |
747 | ||
38830220 | 748 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
749 | bool wxVariantDataString::Write(ostream& str) const |
750 | { | |
783b6cfd | 751 | str << (const char*) m_value.mb_str(); |
341287bf JS |
752 | return TRUE; |
753 | } | |
38830220 | 754 | #endif |
341287bf JS |
755 | |
756 | bool wxVariantDataString::Write(wxString& str) const | |
757 | { | |
758 | str = m_value; | |
759 | return TRUE; | |
760 | } | |
761 | ||
38830220 | 762 | #if wxUSE_STD_IOSTREAM |
341287bf JS |
763 | bool wxVariantDataString::Read(istream& str) |
764 | { | |
765 | str >> m_value; | |
766 | return TRUE; | |
767 | } | |
38830220 | 768 | #endif |
341287bf | 769 | |
e02afc7a | 770 | #if wxUSE_STREAMS |
1ccbb61a VZ |
771 | bool wxVariantDataString::Write(wxOutputStream& str) const |
772 | { | |
783b6cfd OK |
773 | // why doesn't wxOutputStream::operator<< take "const wxString&" |
774 | str << (const char*) m_value.mb_str(); | |
1ccbb61a VZ |
775 | return TRUE; |
776 | } | |
777 | ||
75ed1d15 GL |
778 | bool wxVariantDataString::Read(wxInputStream& str) |
779 | { | |
780 | str >> m_value; | |
781 | return TRUE; | |
782 | } | |
e02afc7a | 783 | #endif // wxUSE_STREAMS |
75ed1d15 | 784 | |
341287bf JS |
785 | bool wxVariantDataString::Read(wxString& str) |
786 | { | |
787 | m_value = str; | |
788 | return TRUE; | |
789 | } | |
790 | ||
62448488 JS |
791 | #if defined(__BORLANDC__) && defined(__WIN16__) |
792 | IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData, wxVariantData) | |
793 | #else | |
341287bf | 794 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString, wxVariantData) |
62448488 | 795 | #endif |
341287bf | 796 | |
a0a302dc JS |
797 | /* |
798 | * wxVariantDataTime | |
799 | */ | |
800 | ||
801 | #if wxUSE_TIMEDATE | |
802 | ||
803 | class wxVariantDataTime: public wxVariantData | |
804 | { | |
805 | DECLARE_DYNAMIC_CLASS(wxVariantDataTime) | |
806 | public: | |
807 | wxVariantDataTime() { } | |
808 | wxVariantDataTime(const wxTime& value) { m_value = value; } | |
809 | ||
810 | inline wxTime GetValue() const { return m_value; } | |
811 | inline void SetValue(const wxTime& value) { m_value = value; } | |
812 | ||
813 | virtual void Copy(wxVariantData& data); | |
814 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 815 | #if wxUSE_STD_IOSTREAM |
a0a302dc | 816 | virtual bool Write(ostream& str) const; |
38830220 | 817 | #endif |
a0a302dc | 818 | virtual bool Write(wxString& str) const; |
38830220 | 819 | #if wxUSE_STD_IOSTREAM |
a0a302dc | 820 | virtual bool Read(istream& str); |
38830220 | 821 | #endif |
a0a302dc | 822 | virtual bool Read(wxString& str); |
783b6cfd | 823 | virtual wxString GetType() const { return _T("time"); }; |
a0a302dc JS |
824 | virtual wxVariantData* Clone() { return new wxVariantDataTime; } |
825 | ||
826 | protected: | |
827 | wxTime m_value; | |
828 | }; | |
829 | ||
830 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime, wxVariantData) | |
831 | ||
832 | void wxVariantDataTime::Copy(wxVariantData& data) | |
833 | { | |
783b6cfd | 834 | wxASSERT_MSG( (data.GetType() == _T("time")), _T("wxVariantDataTime::Copy: Can't copy to this type of data") ); |
a0a302dc JS |
835 | |
836 | wxVariantDataTime& otherData = (wxVariantDataTime&) data; | |
837 | ||
838 | otherData.m_value = m_value; | |
839 | } | |
840 | ||
841 | bool wxVariantDataTime::Eq(wxVariantData& data) const | |
842 | { | |
783b6cfd | 843 | wxASSERT_MSG( (data.GetType() == _T("time")), _T("wxVariantDataTime::Eq: argument mismatch") ); |
a0a302dc JS |
844 | |
845 | wxVariantDataTime& otherData = (wxVariantDataTime&) data; | |
846 | ||
847 | return (otherData.m_value == m_value); | |
848 | } | |
849 | ||
38830220 | 850 | #if wxUSE_STD_IOSTREAM |
a0a302dc JS |
851 | bool wxVariantDataTime::Write(ostream& str) const |
852 | { | |
853 | wxString s; | |
854 | Write(s); | |
783b6cfd | 855 | str << (const char*) s.mb_str(); |
a0a302dc JS |
856 | return TRUE; |
857 | } | |
38830220 | 858 | #endif |
a0a302dc JS |
859 | |
860 | bool wxVariantDataTime::Write(wxString& str) const | |
861 | { | |
783b6cfd | 862 | wxChar*s = m_value.FormatTime(); |
a0a302dc JS |
863 | str = s; |
864 | return TRUE; | |
865 | } | |
866 | ||
38830220 | 867 | #if wxUSE_STD_IOSTREAM |
8b53e5a2 | 868 | bool wxVariantDataTime::Read(istream& WXUNUSED(str)) |
a0a302dc JS |
869 | { |
870 | // Not implemented | |
871 | return FALSE; | |
872 | } | |
38830220 | 873 | #endif |
a0a302dc | 874 | |
8b53e5a2 | 875 | bool wxVariantDataTime::Read(wxString& WXUNUSED(str)) |
a0a302dc JS |
876 | { |
877 | // Not implemented | |
878 | return FALSE; | |
879 | } | |
880 | ||
881 | /* | |
882 | * wxVariantDataDate | |
883 | */ | |
884 | ||
885 | class wxVariantDataDate: public wxVariantData | |
886 | { | |
887 | DECLARE_DYNAMIC_CLASS(wxVariantDataDate) | |
888 | public: | |
889 | wxVariantDataDate() { } | |
890 | wxVariantDataDate(const wxDate& value) { m_value = value; } | |
891 | ||
892 | inline wxDate GetValue() const { return m_value; } | |
893 | inline void SetValue(const wxDate& value) { m_value = value; } | |
894 | ||
895 | virtual void Copy(wxVariantData& data); | |
896 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 897 | #if wxUSE_STD_IOSTREAM |
a0a302dc | 898 | virtual bool Write(ostream& str) const; |
38830220 | 899 | #endif |
a0a302dc | 900 | virtual bool Write(wxString& str) const; |
38830220 | 901 | #if wxUSE_STD_IOSTREAM |
a0a302dc | 902 | virtual bool Read(istream& str); |
38830220 | 903 | #endif |
a0a302dc | 904 | virtual bool Read(wxString& str); |
783b6cfd | 905 | virtual wxString GetType() const { return _T("date"); }; |
a0a302dc JS |
906 | virtual wxVariantData* Clone() { return new wxVariantDataDate; } |
907 | ||
908 | protected: | |
909 | wxDate m_value; | |
910 | }; | |
911 | ||
912 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate, wxVariantData) | |
913 | ||
914 | void wxVariantDataDate::Copy(wxVariantData& data) | |
915 | { | |
783b6cfd | 916 | wxASSERT_MSG( (data.GetType() == _T("date")), _T("wxVariantDataDate::Copy: Can't copy to this type of data") ); |
a0a302dc JS |
917 | |
918 | wxVariantDataDate& otherData = (wxVariantDataDate&) data; | |
919 | ||
920 | otherData.m_value = m_value; | |
921 | } | |
922 | ||
923 | bool wxVariantDataDate::Eq(wxVariantData& data) const | |
924 | { | |
783b6cfd | 925 | wxASSERT_MSG( (data.GetType() == _T("date")), _T("wxVariantDataDate::Eq: argument mismatch") ); |
a0a302dc JS |
926 | |
927 | wxVariantDataDate& otherData = (wxVariantDataDate&) data; | |
928 | ||
929 | return (otherData.m_value == m_value); | |
930 | } | |
931 | ||
38830220 | 932 | #if wxUSE_STD_IOSTREAM |
a0a302dc JS |
933 | bool wxVariantDataDate::Write(ostream& str) const |
934 | { | |
935 | wxString s; | |
936 | Write(s); | |
783b6cfd | 937 | str << (const char*) s.mb_str(); |
a0a302dc JS |
938 | return TRUE; |
939 | } | |
38830220 | 940 | #endif |
a0a302dc JS |
941 | |
942 | bool wxVariantDataDate::Write(wxString& str) const | |
943 | { | |
944 | str = m_value.FormatDate(); | |
945 | return TRUE; | |
946 | } | |
947 | ||
38830220 | 948 | #if wxUSE_STD_IOSTREAM |
8b53e5a2 | 949 | bool wxVariantDataDate::Read(istream& WXUNUSED(str)) |
a0a302dc JS |
950 | { |
951 | // Not implemented | |
952 | return FALSE; | |
953 | } | |
38830220 | 954 | #endif |
a0a302dc | 955 | |
8b53e5a2 | 956 | bool wxVariantDataDate::Read(wxString& WXUNUSED(str)) |
a0a302dc JS |
957 | { |
958 | // Not implemented | |
959 | return FALSE; | |
960 | } | |
961 | #endif | |
962 | // wxUSE_TIMEDATE | |
963 | ||
964 | /* | |
965 | * wxVariantDataVoidPtr | |
966 | */ | |
967 | ||
968 | class wxVariantDataVoidPtr: public wxVariantData | |
969 | { | |
970 | DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr) | |
971 | public: | |
972 | wxVariantDataVoidPtr() { } | |
973 | wxVariantDataVoidPtr(void* value) { m_value = value; } | |
974 | ||
975 | inline void* GetValue() const { return m_value; } | |
976 | inline void SetValue(void* value) { m_value = value; } | |
977 | ||
978 | virtual void Copy(wxVariantData& data); | |
979 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 980 | #if wxUSE_STD_IOSTREAM |
a0a302dc | 981 | virtual bool Write(ostream& str) const; |
38830220 | 982 | #endif |
a0a302dc | 983 | virtual bool Write(wxString& str) const; |
38830220 | 984 | #if wxUSE_STD_IOSTREAM |
a0a302dc | 985 | virtual bool Read(istream& str); |
38830220 | 986 | #endif |
a0a302dc | 987 | virtual bool Read(wxString& str); |
783b6cfd | 988 | virtual wxString GetType() const { return _T("void*"); }; |
a0a302dc JS |
989 | virtual wxVariantData* Clone() { return new wxVariantDataVoidPtr; } |
990 | ||
991 | protected: | |
992 | void* m_value; | |
993 | }; | |
994 | ||
995 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr, wxVariantData) | |
996 | ||
997 | void wxVariantDataVoidPtr::Copy(wxVariantData& data) | |
998 | { | |
783b6cfd | 999 | wxASSERT_MSG( (data.GetType() == _T("void*")), _T("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") ); |
a0a302dc JS |
1000 | |
1001 | wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data; | |
1002 | ||
1003 | otherData.m_value = m_value; | |
1004 | } | |
1005 | ||
1006 | bool wxVariantDataVoidPtr::Eq(wxVariantData& data) const | |
1007 | { | |
783b6cfd | 1008 | wxASSERT_MSG( (data.GetType() == _T("void*")), _T("wxVariantDataVoidPtr::Eq: argument mismatch") ); |
a0a302dc JS |
1009 | |
1010 | wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data; | |
1011 | ||
1012 | return (otherData.m_value == m_value); | |
1013 | } | |
1014 | ||
38830220 | 1015 | #if wxUSE_STD_IOSTREAM |
a0a302dc JS |
1016 | bool wxVariantDataVoidPtr::Write(ostream& str) const |
1017 | { | |
1018 | wxString s; | |
1019 | Write(s); | |
783b6cfd | 1020 | str << (const char*) s.mb_str(); |
a0a302dc JS |
1021 | return TRUE; |
1022 | } | |
38830220 | 1023 | #endif |
a0a302dc JS |
1024 | |
1025 | bool wxVariantDataVoidPtr::Write(wxString& str) const | |
1026 | { | |
783b6cfd | 1027 | str.Printf(_T("%ld"), (long) m_value); |
a0a302dc JS |
1028 | return TRUE; |
1029 | } | |
1030 | ||
38830220 | 1031 | #if wxUSE_STD_IOSTREAM |
8b53e5a2 | 1032 | bool wxVariantDataVoidPtr::Read(istream& WXUNUSED(str)) |
a0a302dc JS |
1033 | { |
1034 | // Not implemented | |
1035 | return FALSE; | |
1036 | } | |
38830220 | 1037 | #endif |
a0a302dc | 1038 | |
8b53e5a2 | 1039 | bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str)) |
a0a302dc JS |
1040 | { |
1041 | // Not implemented | |
1042 | return FALSE; | |
1043 | } | |
1044 | ||
1045 | ||
341287bf JS |
1046 | /* |
1047 | * wxVariant | |
1048 | */ | |
1049 | ||
1050 | IMPLEMENT_DYNAMIC_CLASS(wxVariant, wxObject) | |
1051 | ||
1052 | // Construction & destruction | |
1053 | wxVariant::wxVariant() | |
1054 | { | |
1055 | m_data = (wxVariantData*) NULL; | |
1056 | } | |
1057 | ||
a0a302dc | 1058 | wxVariant::wxVariant(double val, const wxString& name) |
341287bf JS |
1059 | { |
1060 | m_data = new wxVariantDataReal(val); | |
a0a302dc | 1061 | m_name = name; |
341287bf JS |
1062 | } |
1063 | ||
a0a302dc | 1064 | wxVariant::wxVariant(long val, const wxString& name) |
341287bf JS |
1065 | { |
1066 | m_data = new wxVariantDataLong(val); | |
a0a302dc | 1067 | m_name = name; |
341287bf JS |
1068 | } |
1069 | ||
57493f9f | 1070 | #ifdef HAVE_BOOL |
a0a302dc | 1071 | wxVariant::wxVariant(bool val, const wxString& name) |
341287bf JS |
1072 | { |
1073 | m_data = new wxVariantDataBool(val); | |
a0a302dc | 1074 | m_name = name; |
341287bf | 1075 | } |
57493f9f | 1076 | #endif |
341287bf | 1077 | |
a0a302dc | 1078 | wxVariant::wxVariant(char val, const wxString& name) |
341287bf JS |
1079 | { |
1080 | m_data = new wxVariantDataChar(val); | |
a0a302dc | 1081 | m_name = name; |
341287bf JS |
1082 | } |
1083 | ||
a0a302dc | 1084 | wxVariant::wxVariant(const wxString& val, const wxString& name) |
341287bf JS |
1085 | { |
1086 | m_data = new wxVariantDataString(val); | |
a0a302dc | 1087 | m_name = name; |
341287bf JS |
1088 | } |
1089 | ||
783b6cfd | 1090 | wxVariant::wxVariant(const wxChar* val, const wxString& name) |
341287bf JS |
1091 | { |
1092 | m_data = new wxVariantDataString(wxString(val)); | |
a0a302dc | 1093 | m_name = name; |
341287bf JS |
1094 | } |
1095 | ||
a0a302dc | 1096 | wxVariant::wxVariant(const wxStringList& val, const wxString& name) |
341287bf JS |
1097 | { |
1098 | m_data = new wxVariantDataStringList(val); | |
a0a302dc | 1099 | m_name = name; |
341287bf | 1100 | } |
341287bf | 1101 | |
a0a302dc | 1102 | wxVariant::wxVariant(const wxList& val, const wxString& name) // List of variants |
341287bf JS |
1103 | { |
1104 | m_data = new wxVariantDataList(val); | |
a0a302dc JS |
1105 | m_name = name; |
1106 | } | |
1107 | ||
1108 | #if wxUSE_TIMEDATE | |
1109 | wxVariant::wxVariant(const wxTime& val, const wxString& name) // Time | |
1110 | { | |
1111 | m_data = new wxVariantDataTime(val); | |
1112 | m_name = name; | |
1113 | } | |
1114 | ||
1115 | wxVariant::wxVariant(const wxDate& val, const wxString& name) // Date | |
1116 | { | |
1117 | m_data = new wxVariantDataDate(val); | |
1118 | m_name = name; | |
1119 | } | |
1120 | #endif | |
1121 | ||
1122 | wxVariant::wxVariant(void* val, const wxString& name) // Void ptr | |
1123 | { | |
1124 | m_data = new wxVariantDataVoidPtr(val); | |
1125 | m_name = name; | |
341287bf JS |
1126 | } |
1127 | ||
1128 | wxVariant::wxVariant(const wxVariant& variant) | |
1129 | { | |
1130 | if (!variant.IsNull()) | |
1131 | { | |
1132 | m_data = (wxVariantData*) variant.GetData()->GetClassInfo()->CreateObject(); | |
1133 | variant.m_data->Copy(*m_data); | |
1134 | } | |
4fabb575 JS |
1135 | else |
1136 | m_data = (wxVariantData*) NULL; | |
a0a302dc | 1137 | m_name = variant.m_name; |
341287bf JS |
1138 | } |
1139 | ||
a0a302dc | 1140 | wxVariant::wxVariant(wxVariantData* data, const wxString& name) // User-defined data |
341287bf JS |
1141 | { |
1142 | m_data = data; | |
a0a302dc | 1143 | m_name = name; |
341287bf JS |
1144 | } |
1145 | ||
1146 | wxVariant::~wxVariant() | |
1147 | { | |
1148 | delete m_data; | |
1149 | } | |
1150 | ||
1151 | ||
1152 | // Make NULL (i.e. delete the data) | |
1153 | void wxVariant::MakeNull() | |
1154 | { | |
1155 | delete m_data; | |
1156 | m_data = NULL; | |
1157 | } | |
1158 | ||
1159 | // Generic operators | |
1160 | // Assignment | |
1161 | void wxVariant::operator= (const wxVariant& variant) | |
1162 | { | |
1163 | if (variant.IsNull()) | |
1164 | { | |
1165 | MakeNull(); | |
1166 | return; | |
1167 | } | |
1168 | ||
1169 | if (IsNull() || (GetType() != variant.GetType())) | |
1170 | { | |
1171 | if (m_data) | |
1172 | delete m_data; | |
1173 | m_data = (wxVariantData*) variant.GetData()->GetClassInfo()->CreateObject(); | |
1174 | } | |
4fabb575 JS |
1175 | // GetData()->Copy(* variant.GetData()); |
1176 | variant.GetData()->Copy(* GetData()); | |
341287bf JS |
1177 | } |
1178 | ||
1179 | // Assignment using data, e.g. | |
1180 | // myVariant = new wxStringVariantData("hello") | |
1181 | void wxVariant::operator= (wxVariantData* variantData) | |
1182 | { | |
1183 | MakeNull(); | |
1184 | m_data = variantData; | |
1185 | } | |
1186 | ||
1187 | bool wxVariant::operator== (const wxVariant& variant) const | |
1188 | { | |
1189 | if (IsNull() || variant.IsNull()) | |
1190 | return (IsNull() == variant.IsNull()); | |
1191 | ||
1192 | return (GetData()->Eq(* variant.GetData())); | |
1193 | } | |
1194 | ||
1195 | bool wxVariant::operator!= (const wxVariant& variant) const | |
1196 | { | |
1197 | return (!(*this == variant)); | |
1198 | } | |
1199 | ||
1200 | ||
1201 | // Specific operators | |
1202 | bool wxVariant::operator== (double value) const | |
1203 | { | |
1204 | double thisValue; | |
1205 | if (!Convert(&thisValue)) | |
1206 | return FALSE; | |
1207 | else | |
1208 | return (value == thisValue); | |
1209 | } | |
1210 | ||
1211 | bool wxVariant::operator!= (double value) const | |
1212 | { | |
1213 | return (!((*this) == value)); | |
1214 | } | |
1215 | ||
1216 | void wxVariant::operator= (double value) | |
1217 | { | |
783b6cfd | 1218 | if (GetType() == _T("double")) |
341287bf JS |
1219 | { |
1220 | ((wxVariantDataReal*)GetData())->SetValue(value); | |
1221 | } | |
1222 | else | |
1223 | { | |
1224 | if (m_data) | |
1225 | delete m_data; | |
1226 | m_data = new wxVariantDataReal(value); | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | bool wxVariant::operator== (long value) const | |
1231 | { | |
1232 | long thisValue; | |
1233 | if (!Convert(&thisValue)) | |
1234 | return FALSE; | |
1235 | else | |
1236 | return (value == thisValue); | |
1237 | } | |
1238 | ||
1239 | bool wxVariant::operator!= (long value) const | |
1240 | { | |
1241 | return (!((*this) == value)); | |
1242 | } | |
1243 | ||
1244 | void wxVariant::operator= (long value) | |
1245 | { | |
783b6cfd | 1246 | if (GetType() == _T("long")) |
341287bf JS |
1247 | { |
1248 | ((wxVariantDataLong*)GetData())->SetValue(value); | |
1249 | } | |
1250 | else | |
1251 | { | |
1252 | if (m_data) | |
1253 | delete m_data; | |
1254 | m_data = new wxVariantDataLong(value); | |
1255 | } | |
1256 | } | |
1257 | ||
1258 | bool wxVariant::operator== (char value) const | |
1259 | { | |
1260 | char thisValue; | |
1261 | if (!Convert(&thisValue)) | |
1262 | return FALSE; | |
1263 | else | |
1264 | return (value == thisValue); | |
1265 | } | |
1266 | ||
1267 | bool wxVariant::operator!= (char value) const | |
1268 | { | |
1269 | return (!((*this) == value)); | |
1270 | } | |
1271 | ||
1272 | void wxVariant::operator= (char value) | |
1273 | { | |
783b6cfd | 1274 | if (GetType() == _T("char")) |
341287bf JS |
1275 | { |
1276 | ((wxVariantDataChar*)GetData())->SetValue(value); | |
1277 | } | |
1278 | else | |
1279 | { | |
1280 | if (m_data) | |
1281 | delete m_data; | |
1282 | m_data = new wxVariantDataChar(value); | |
1283 | } | |
1284 | } | |
1285 | ||
57493f9f | 1286 | #ifdef HAVE_BOOL |
341287bf JS |
1287 | bool wxVariant::operator== (bool value) const |
1288 | { | |
1289 | bool thisValue; | |
1290 | if (!Convert(&thisValue)) | |
1291 | return FALSE; | |
1292 | else | |
1293 | return (value == thisValue); | |
1294 | } | |
1295 | ||
1296 | bool wxVariant::operator!= (bool value) const | |
1297 | { | |
1298 | return (!((*this) == value)); | |
1299 | } | |
1300 | ||
1301 | void wxVariant::operator= (bool value) | |
1302 | { | |
783b6cfd | 1303 | if (GetType() == _T("bool")) |
341287bf JS |
1304 | { |
1305 | ((wxVariantDataBool*)GetData())->SetValue(value); | |
1306 | } | |
1307 | else | |
1308 | { | |
1309 | if (m_data) | |
1310 | delete m_data; | |
1311 | m_data = new wxVariantDataBool(value); | |
1312 | } | |
1313 | } | |
57493f9f | 1314 | #endif // HAVE_BOOL |
341287bf JS |
1315 | |
1316 | bool wxVariant::operator== (const wxString& value) const | |
1317 | { | |
1318 | wxString thisValue; | |
1319 | if (!Convert(&thisValue)) | |
1320 | return FALSE; | |
1321 | else | |
1322 | return (value == thisValue); | |
1323 | } | |
1324 | ||
1325 | bool wxVariant::operator!= (const wxString& value) const | |
1326 | { | |
1327 | return (!((*this) == value)); | |
1328 | } | |
1329 | ||
1330 | void wxVariant::operator= (const wxString& value) | |
1331 | { | |
783b6cfd | 1332 | if (GetType() == _T("string")) |
341287bf JS |
1333 | { |
1334 | ((wxVariantDataString*)GetData())->SetValue(value); | |
1335 | } | |
1336 | else | |
1337 | { | |
1338 | if (m_data) | |
1339 | delete m_data; | |
1340 | m_data = new wxVariantDataString(value); | |
1341 | } | |
1342 | } | |
1343 | ||
783b6cfd | 1344 | void wxVariant::operator= (const wxChar* value) |
341287bf | 1345 | { |
783b6cfd | 1346 | if (GetType() == _T("string")) |
341287bf JS |
1347 | { |
1348 | ((wxVariantDataString*)GetData())->SetValue(wxString(value)); | |
1349 | } | |
1350 | else | |
1351 | { | |
1352 | if (m_data) | |
1353 | delete m_data; | |
1354 | m_data = new wxVariantDataString(wxString(value)); | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | bool wxVariant::operator== (const wxStringList& value) const | |
1359 | { | |
783b6cfd | 1360 | wxASSERT_MSG( (GetType() == _T("stringlist")), _T("Invalid type for == operator") ); |
341287bf JS |
1361 | |
1362 | wxVariantDataStringList other(value); | |
1363 | return (m_data->Eq(other)); | |
1364 | } | |
1365 | ||
1366 | bool wxVariant::operator!= (const wxStringList& value) const | |
1367 | { | |
1368 | return (!((*this) == value)); | |
1369 | } | |
1370 | ||
1371 | void wxVariant::operator= (const wxStringList& value) | |
1372 | { | |
783b6cfd | 1373 | if (GetType() == _T("stringlist")) |
341287bf JS |
1374 | { |
1375 | ((wxVariantDataStringList*)GetData())->SetValue(value); | |
1376 | } | |
1377 | else | |
1378 | { | |
1379 | if (m_data) | |
1380 | delete m_data; | |
1381 | m_data = new wxVariantDataStringList(value); | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | bool wxVariant::operator== (const wxList& value) const | |
1386 | { | |
783b6cfd | 1387 | wxASSERT_MSG( (GetType() == _T("list")), _T("Invalid type for == operator") ); |
341287bf JS |
1388 | |
1389 | wxVariantDataList other(value); | |
1390 | return (m_data->Eq(other)); | |
1391 | } | |
1392 | ||
1393 | bool wxVariant::operator!= (const wxList& value) const | |
1394 | { | |
1395 | return (!((*this) == value)); | |
1396 | } | |
1397 | ||
1398 | void wxVariant::operator= (const wxList& value) | |
1399 | { | |
783b6cfd | 1400 | if (GetType() == _T("list")) |
341287bf JS |
1401 | { |
1402 | ((wxVariantDataList*)GetData())->SetValue(value); | |
1403 | } | |
1404 | else | |
1405 | { | |
1406 | if (m_data) | |
1407 | delete m_data; | |
1408 | m_data = new wxVariantDataList(value); | |
1409 | } | |
1410 | } | |
1411 | ||
a0a302dc JS |
1412 | #if wxUSE_TIMEDATE |
1413 | bool wxVariant::operator== (const wxTime& value) const | |
1414 | { | |
1415 | wxTime thisValue; | |
1416 | if (!Convert(&thisValue)) | |
1417 | return FALSE; | |
1418 | else | |
1419 | return (value == thisValue); | |
1420 | } | |
1421 | ||
1422 | bool wxVariant::operator!= (const wxTime& value) const | |
1423 | { | |
1424 | return (!((*this) == value)); | |
1425 | } | |
1426 | ||
1427 | void wxVariant::operator= (const wxTime& value) | |
1428 | { | |
783b6cfd | 1429 | if (GetType() == _T("time")) |
a0a302dc JS |
1430 | { |
1431 | ((wxVariantDataTime*)GetData())->SetValue(value); | |
1432 | } | |
1433 | else | |
1434 | { | |
1435 | if (m_data) | |
1436 | delete m_data; | |
1437 | m_data = new wxVariantDataTime(value); | |
1438 | } | |
1439 | } | |
1440 | ||
1441 | bool wxVariant::operator== (const wxDate& value) const | |
1442 | { | |
1443 | wxDate thisValue; | |
1444 | if (!Convert(&thisValue)) | |
1445 | return FALSE; | |
1446 | else | |
1447 | return (value == thisValue); | |
1448 | } | |
1449 | ||
1450 | bool wxVariant::operator!= (const wxDate& value) const | |
1451 | { | |
1452 | return (!((*this) == value)); | |
1453 | } | |
1454 | ||
1455 | void wxVariant::operator= (const wxDate& value) | |
1456 | { | |
783b6cfd | 1457 | if (GetType() == _T("date")) |
a0a302dc JS |
1458 | { |
1459 | ((wxVariantDataTime*)GetData())->SetValue(value); | |
1460 | } | |
1461 | else | |
1462 | { | |
1463 | if (m_data) | |
1464 | delete m_data; | |
1465 | m_data = new wxVariantDataDate(value); | |
1466 | } | |
1467 | } | |
1468 | #endif | |
1469 | ||
1470 | bool wxVariant::operator== (void* value) const | |
1471 | { | |
1472 | return (value == ((wxVariantDataVoidPtr*)GetData())->GetValue()); | |
1473 | } | |
1474 | ||
1475 | bool wxVariant::operator!= (void* value) const | |
1476 | { | |
1477 | return (!((*this) == value)); | |
1478 | } | |
1479 | ||
1480 | void wxVariant::operator= (void* value) | |
1481 | { | |
783b6cfd | 1482 | if (GetType() == _T("void*")) |
a0a302dc JS |
1483 | { |
1484 | ((wxVariantDataVoidPtr*)GetData())->SetValue(value); | |
1485 | } | |
1486 | else | |
1487 | { | |
1488 | if (m_data) | |
1489 | delete m_data; | |
1490 | m_data = new wxVariantDataVoidPtr(value); | |
1491 | } | |
1492 | } | |
341287bf JS |
1493 | |
1494 | // Treat a list variant as an array | |
1495 | wxVariant wxVariant::operator[] (size_t idx) const | |
1496 | { | |
783b6cfd | 1497 | wxASSERT_MSG( (GetType() == _T("list") || GetType() == _T("stringlist")), _T("Invalid type for array operator") ); |
341287bf | 1498 | |
783b6cfd | 1499 | if (GetType() == _T("list")) |
341287bf JS |
1500 | { |
1501 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
783b6cfd | 1502 | wxASSERT_MSG( (idx < (size_t) data->GetValue().Number()), _T("Invalid index for array") ); |
341287bf JS |
1503 | return * (wxVariant*) (data->GetValue().Nth(idx)->Data()); |
1504 | } | |
783b6cfd | 1505 | else if (GetType() == _T("stringlist")) |
341287bf JS |
1506 | { |
1507 | wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; | |
783b6cfd | 1508 | wxASSERT_MSG( (idx < (size_t) data->GetValue().Number()), _T("Invalid index for array") ); |
341287bf | 1509 | |
783b6cfd | 1510 | wxVariant variant( wxString( (wxChar*) (data->GetValue().Nth(idx)->Data()) )); |
341287bf JS |
1511 | return variant; |
1512 | } | |
1513 | return wxNullVariant; | |
1514 | } | |
1515 | ||
1516 | wxVariant& wxVariant::operator[] (size_t idx) | |
1517 | { | |
1518 | // We can't return a reference to a variant for a string list, since the string | |
1519 | // is actually stored as a char*, not a variant. | |
1520 | ||
783b6cfd | 1521 | wxASSERT_MSG( (GetType() == _T("list")), _T("Invalid type for array operator") ); |
341287bf JS |
1522 | |
1523 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
783b6cfd | 1524 | wxASSERT_MSG( (idx < (size_t) data->GetValue().Number()), _T("Invalid index for array") ); |
341287bf JS |
1525 | |
1526 | return * (wxVariant*) (data->GetValue().Nth(idx)->Data()); | |
1527 | } | |
1528 | ||
1529 | // Return the number of elements in a list | |
1530 | int wxVariant::GetCount() const | |
1531 | { | |
783b6cfd | 1532 | wxASSERT_MSG( (GetType() == _T("list") || GetType() == _T("stringlist")), _T("Invalid type for GetCount()") ); |
341287bf | 1533 | |
783b6cfd | 1534 | if (GetType() == _T("list")) |
341287bf JS |
1535 | { |
1536 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
1537 | return data->GetValue().Number(); | |
1538 | } | |
783b6cfd | 1539 | else if (GetType() == _T("stringlist")) |
341287bf JS |
1540 | { |
1541 | wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; | |
1542 | return data->GetValue().Number(); | |
1543 | } | |
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | wxString wxVariant::MakeString() const | |
1548 | { | |
1549 | if (!IsNull()) | |
1550 | { | |
1551 | wxString str; | |
1552 | if (GetData()->Write(str)) | |
1553 | return str; | |
1554 | } | |
783b6cfd | 1555 | return wxString(_T("")); |
341287bf JS |
1556 | } |
1557 | ||
1558 | // Accessors | |
1559 | ||
1560 | void wxVariant::SetData(wxVariantData* data) | |
1561 | { | |
1562 | if (m_data) delete m_data; | |
1563 | m_data = data; | |
1564 | } | |
1565 | ||
1566 | ||
1567 | // Returns a string representing the type of the variant, | |
1568 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
1569 | wxString wxVariant::GetType() const | |
1570 | { | |
1571 | if (IsNull()) | |
783b6cfd | 1572 | return wxString(_T("null")); |
341287bf JS |
1573 | else |
1574 | return m_data->GetType(); | |
1575 | } | |
1576 | ||
1577 | ||
1578 | bool wxVariant::IsType(const wxString& type) const | |
1579 | { | |
1580 | return (GetType() == type); | |
1581 | } | |
1582 | ||
1583 | ||
1584 | // Value accessors | |
1585 | double wxVariant::GetReal() const | |
1586 | { | |
1587 | double value; | |
1588 | if (Convert(& value)) | |
1589 | return value; | |
1590 | else | |
1591 | { | |
783b6cfd | 1592 | wxFAIL_MSG(_T("Could not convert to a real number")); |
341287bf JS |
1593 | return 0.0; |
1594 | } | |
1595 | } | |
1596 | ||
1597 | long wxVariant::GetInteger() const | |
1598 | { | |
1599 | long value; | |
1600 | if (Convert(& value)) | |
1601 | return value; | |
1602 | else | |
1603 | { | |
783b6cfd | 1604 | wxFAIL_MSG(_T("Could not convert to an integer")); |
341287bf JS |
1605 | return 0; |
1606 | } | |
1607 | } | |
1608 | ||
1609 | char wxVariant::GetChar() const | |
1610 | { | |
1611 | char value; | |
1612 | if (Convert(& value)) | |
1613 | return value; | |
1614 | else | |
1615 | { | |
783b6cfd | 1616 | wxFAIL_MSG(_T("Could not convert to a char")); |
341287bf JS |
1617 | return 0; |
1618 | } | |
1619 | } | |
1620 | ||
1621 | bool wxVariant::GetBool() const | |
1622 | { | |
1623 | bool value; | |
1624 | if (Convert(& value)) | |
1625 | return value; | |
1626 | else | |
1627 | { | |
783b6cfd | 1628 | wxFAIL_MSG(_T("Could not convert to a bool")); |
341287bf JS |
1629 | return 0; |
1630 | } | |
1631 | } | |
1632 | ||
1633 | wxString wxVariant::GetString() const | |
1634 | { | |
1635 | wxString value; | |
1636 | if (Convert(& value)) | |
1637 | return value; | |
1638 | else | |
1639 | { | |
783b6cfd | 1640 | wxFAIL_MSG(_T("Could not convert to a string")); |
341287bf JS |
1641 | return wxString(""); |
1642 | } | |
1643 | } | |
1644 | ||
a0a302dc JS |
1645 | #if wxUSE_TIMEDATE |
1646 | wxTime wxVariant::GetTime() const | |
1647 | { | |
1648 | wxTime value; | |
1649 | if (Convert(& value)) | |
1650 | return value; | |
1651 | else | |
1652 | { | |
783b6cfd | 1653 | wxFAIL_MSG(_T("Could not convert to a time")); |
a0a302dc JS |
1654 | return wxTime(); |
1655 | } | |
1656 | } | |
1657 | ||
1658 | wxDate wxVariant::GetDate() const | |
1659 | { | |
1660 | wxDate value; | |
1661 | if (Convert(& value)) | |
1662 | return value; | |
1663 | else | |
1664 | { | |
783b6cfd | 1665 | wxFAIL_MSG(_T("Could not convert to a date")); |
a0a302dc JS |
1666 | return wxDate(); |
1667 | } | |
1668 | } | |
1669 | #endif | |
1670 | ||
1671 | void* wxVariant::GetVoidPtr() const | |
1672 | { | |
783b6cfd | 1673 | wxASSERT( (GetType() == _T("void*")) ); |
a0a302dc JS |
1674 | |
1675 | return (void*) ((wxVariantDataVoidPtr*) m_data)->GetValue(); | |
1676 | } | |
1677 | ||
341287bf JS |
1678 | wxList& wxVariant::GetList() const |
1679 | { | |
783b6cfd | 1680 | wxASSERT( (GetType() == _T("list")) ); |
341287bf JS |
1681 | |
1682 | return (wxList&) ((wxVariantDataList*) m_data)->GetValue(); | |
1683 | } | |
1684 | ||
1685 | wxStringList& wxVariant::GetStringList() const | |
1686 | { | |
783b6cfd | 1687 | wxASSERT( (GetType() == _T("stringlist")) ); |
341287bf JS |
1688 | |
1689 | return (wxStringList&) ((wxVariantDataStringList*) m_data)->GetValue(); | |
1690 | } | |
1691 | ||
1692 | // Append to list | |
1693 | void wxVariant::Append(const wxVariant& value) | |
1694 | { | |
1695 | wxList& list = GetList(); | |
1696 | ||
1697 | list.Append(new wxVariant(value)); | |
1698 | } | |
1699 | ||
1700 | // Insert at front of list | |
1701 | void wxVariant::Insert(const wxVariant& value) | |
1702 | { | |
1703 | wxList& list = GetList(); | |
1704 | ||
1705 | list.Insert(new wxVariant(value)); | |
1706 | } | |
1707 | ||
1708 | // Returns TRUE if the variant is a member of the list | |
1709 | bool wxVariant::Member(const wxVariant& value) const | |
1710 | { | |
1711 | wxList& list = GetList(); | |
1712 | ||
1713 | wxNode* node = list.First(); | |
1714 | while (node) | |
1715 | { | |
1716 | wxVariant* other = (wxVariant*) node->Data(); | |
1717 | if (value == *other) | |
1718 | return TRUE; | |
1719 | node = node->Next(); | |
1720 | } | |
1721 | return FALSE; | |
1722 | } | |
1723 | ||
1724 | // Deletes the nth element of the list | |
1725 | bool wxVariant::Delete(int item) | |
1726 | { | |
1727 | wxList& list = GetList(); | |
1728 | ||
783b6cfd | 1729 | wxASSERT_MSG( (item < list.Number()), _T("Invalid index to Delete") ); |
341287bf JS |
1730 | wxNode* node = list.Nth(item); |
1731 | wxVariant* variant = (wxVariant*) node->Data(); | |
1732 | delete variant; | |
1733 | delete node; | |
1734 | return TRUE; | |
1735 | } | |
1736 | ||
1737 | // Clear list | |
1738 | void wxVariant::ClearList() | |
1739 | { | |
783b6cfd | 1740 | if (!IsNull() && (GetType() == _T("list"))) |
341287bf JS |
1741 | { |
1742 | ((wxVariantDataList*) m_data)->Clear(); | |
1743 | } | |
1744 | else | |
1745 | { | |
783b6cfd | 1746 | if (GetType() != _T("list")) |
341287bf JS |
1747 | { |
1748 | delete m_data; | |
1749 | m_data = NULL; | |
1750 | } | |
1751 | m_data = new wxVariantDataList; | |
1752 | } | |
1753 | } | |
1754 | ||
1755 | // Type conversion | |
1756 | bool wxVariant::Convert(long* value) const | |
1757 | { | |
1758 | wxString type(GetType()); | |
783b6cfd | 1759 | if (type == _T("double")) |
341287bf | 1760 | *value = (long) (((wxVariantDataReal*)GetData())->GetValue()); |
783b6cfd | 1761 | else if (type == _T("long")) |
341287bf | 1762 | *value = ((wxVariantDataLong*)GetData())->GetValue(); |
862416e0 | 1763 | #ifdef HAVE_BOOL |
783b6cfd | 1764 | else if (type == _T("bool")) |
341287bf | 1765 | *value = (long) (((wxVariantDataBool*)GetData())->GetValue()); |
862416e0 | 1766 | #endif |
783b6cfd OK |
1767 | else if (type == _T("string")) |
1768 | *value = wxAtol((const wxChar*) ((wxVariantDataString*)GetData())->GetValue()); | |
341287bf JS |
1769 | else |
1770 | return FALSE; | |
1771 | ||
1772 | return TRUE; | |
1773 | } | |
1774 | ||
1775 | bool wxVariant::Convert(bool* value) const | |
1776 | { | |
1777 | wxString type(GetType()); | |
783b6cfd | 1778 | if (type == _T("double")) |
341287bf | 1779 | *value = ((int) (((wxVariantDataReal*)GetData())->GetValue()) != 0); |
783b6cfd | 1780 | else if (type == _T("long")) |
341287bf | 1781 | *value = (((wxVariantDataLong*)GetData())->GetValue() != 0); |
862416e0 | 1782 | #ifdef HAVE_BOOL |
783b6cfd | 1783 | else if (type == _T("bool")) |
341287bf | 1784 | *value = ((wxVariantDataBool*)GetData())->GetValue(); |
862416e0 | 1785 | #endif |
783b6cfd | 1786 | else if (type == _T("string")) |
341287bf JS |
1787 | { |
1788 | wxString val(((wxVariantDataString*)GetData())->GetValue()); | |
1789 | val.MakeLower(); | |
783b6cfd | 1790 | if (val == _T("true") || val == _T("yes")) |
341287bf | 1791 | *value = TRUE; |
783b6cfd | 1792 | else if (val == _T("false") || val == _T("no")) |
341287bf JS |
1793 | *value = FALSE; |
1794 | else | |
1795 | return FALSE; | |
1796 | } | |
1797 | else | |
1798 | return FALSE; | |
1799 | ||
1800 | return TRUE; | |
1801 | } | |
1802 | ||
1803 | bool wxVariant::Convert(double* value) const | |
1804 | { | |
1805 | wxString type(GetType()); | |
783b6cfd | 1806 | if (type == _T("double")) |
341287bf | 1807 | *value = ((wxVariantDataReal*)GetData())->GetValue(); |
783b6cfd | 1808 | else if (type == _T("long")) |
341287bf | 1809 | *value = (double) (((wxVariantDataLong*)GetData())->GetValue()); |
862416e0 | 1810 | #ifdef HAVE_BOOL |
783b6cfd | 1811 | else if (type == _T("bool")) |
341287bf | 1812 | *value = (double) (((wxVariantDataBool*)GetData())->GetValue()); |
862416e0 | 1813 | #endif |
783b6cfd OK |
1814 | else if (type == _T("string")) |
1815 | *value = (double) wxAtof((const wxChar*) ((wxVariantDataString*)GetData())->GetValue()); | |
341287bf JS |
1816 | else |
1817 | return FALSE; | |
1818 | ||
1819 | return TRUE; | |
1820 | } | |
1821 | ||
1822 | bool wxVariant::Convert(char* value) const | |
1823 | { | |
1824 | wxString type(GetType()); | |
783b6cfd | 1825 | if (type == _T("char")) |
341287bf | 1826 | *value = ((wxVariantDataChar*)GetData())->GetValue(); |
783b6cfd | 1827 | else if (type == _T("long")) |
341287bf | 1828 | *value = (char) (((wxVariantDataLong*)GetData())->GetValue()); |
862416e0 | 1829 | #ifdef HAVE_BOOL |
783b6cfd | 1830 | else if (type == _T("bool")) |
341287bf | 1831 | *value = (char) (((wxVariantDataBool*)GetData())->GetValue()); |
862416e0 | 1832 | #endif |
341287bf JS |
1833 | else |
1834 | return FALSE; | |
1835 | ||
1836 | return TRUE; | |
1837 | } | |
1838 | ||
1839 | bool wxVariant::Convert(wxString* value) const | |
1840 | { | |
1841 | *value = MakeString(); | |
1842 | return TRUE; | |
1843 | } | |
1844 | ||
a0a302dc JS |
1845 | #if wxUSE_TIMEDATE |
1846 | bool wxVariant::Convert(wxTime* value) const | |
1847 | { | |
1848 | wxString type(GetType()); | |
783b6cfd | 1849 | if (type == _T("time")) |
a0a302dc | 1850 | *value = ((wxVariantDataTime*)GetData())->GetValue(); |
783b6cfd | 1851 | else if (type == _T("date")) |
a0a302dc JS |
1852 | *value = wxTime(((wxVariantDataDate*)GetData())->GetValue()); |
1853 | else | |
1854 | return FALSE; | |
1855 | ||
1856 | return TRUE; | |
1857 | } | |
1858 | ||
1859 | bool wxVariant::Convert(wxDate* value) const | |
1860 | { | |
1861 | wxString type(GetType()); | |
783b6cfd | 1862 | if (type == _T("date")) |
a0a302dc JS |
1863 | *value = ((wxVariantDataDate*)GetData())->GetValue(); |
1864 | else | |
1865 | return FALSE; | |
1866 | ||
1867 | return TRUE; | |
1868 | } | |
1869 | #endif | |
1870 | // wxUSE_TIMEDATE |