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