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