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