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