1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "variant.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/stream.h"
30 #include "wx/string.h"
31 #include "wx/variant.h"
33 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
35 wxVariant WXDLLEXPORT wxNullVariant
;
41 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
43 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
45 wxVariantDataList() {}
46 wxVariantDataList(const wxList
& list
);
49 wxList
& GetValue() const { return (wxList
&) m_value
; }
50 void SetValue(const wxList
& value
) ;
52 virtual void Copy(wxVariantData
& data
);
53 virtual bool Eq(wxVariantData
& data
) const;
54 virtual bool Write(ostream
& str
) const;
55 virtual bool Write(wxString
& str
) const;
56 virtual bool Read(istream
& str
);
57 virtual bool Read(wxString
& str
);
58 virtual wxString
GetType() const { return "list"; };
66 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
68 wxVariantDataList::wxVariantDataList(const wxList
& list
)
73 wxVariantDataList::~wxVariantDataList()
78 void wxVariantDataList::SetValue(const wxList
& value
)
81 wxNode
* node
= value
.First();
84 wxVariant
* var
= (wxVariant
*) node
->Data();
85 m_value
.Append(new wxVariant(*var
));
90 void wxVariantDataList::Clear()
92 wxNode
* node
= m_value
.First();
95 wxVariant
* var
= (wxVariant
*) node
->Data();
102 void wxVariantDataList::Copy(wxVariantData
& data
)
104 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Copy: Can't copy to this type of data" );
106 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
109 wxNode
* node
= m_value
.First();
112 wxVariant
* var
= (wxVariant
*) node
->Data();
113 listData
.m_value
.Append(new wxVariant(*var
));
118 bool wxVariantDataList::Eq(wxVariantData
& data
) const
120 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Eq: argument mismatch" );
122 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
123 wxNode
* node1
= m_value
.First();
124 wxNode
* node2
= listData
.GetValue().First();
125 while (node1
&& node2
)
127 wxVariant
* var1
= (wxVariant
*) node1
->Data();
128 wxVariant
* var2
= (wxVariant
*) node2
->Data();
129 if ((*var1
) != (*var2
))
131 node1
= node1
->Next();
132 node2
= node2
->Next();
134 if (node1
|| node2
) return FALSE
;
138 bool wxVariantDataList::Write(ostream
& str
) const
142 str
<< (const char*) s
;
146 bool wxVariantDataList::Write(wxString
& str
) const
149 wxNode
* node
= m_value
.First();
152 wxVariant
* var
= (wxVariant
*) node
->Data();
153 if (node
!= m_value
.First())
156 str
+= var
->MakeString();
163 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
165 wxFAIL_MSG("Unimplemented");
170 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
172 wxFAIL_MSG("Unimplemented");
178 * wxVariantDataStringList
181 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
183 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
185 wxVariantDataStringList() {}
186 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
188 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
189 void SetValue(const wxStringList
& value
);
191 virtual void Copy(wxVariantData
& data
);
192 virtual bool Eq(wxVariantData
& data
) const;
193 virtual bool Write(ostream
& str
) const;
194 virtual bool Write(wxString
& str
) const;
195 virtual bool Read(istream
& str
);
196 virtual bool Read(wxString
& str
);
197 virtual wxString
GetType() const { return "stringlist"; };
200 wxStringList m_value
;
203 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
205 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
210 void wxVariantDataStringList::Copy(wxVariantData
& data
)
212 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Copy: Can't copy to this type of data" );
214 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
216 listData
.m_value
= m_value
;
219 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
221 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Eq: argument mismatch" );
223 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
224 wxNode
* node1
= m_value
.First();
225 wxNode
* node2
= listData
.GetValue().First();
226 while (node1
&& node2
)
228 wxString
str1 ((char*) node1
->Data());
229 wxString
str2 ((char*) node2
->Data());
232 node1
= node1
->Next();
233 node2
= node2
->Next();
235 if (node1
|| node2
) return FALSE
;
239 bool wxVariantDataStringList::Write(ostream
& str
) const
243 str
<< (const char*) s
;
247 bool wxVariantDataStringList::Write(wxString
& str
) const
250 wxNode
* node
= m_value
.First();
253 char* s
= (char*) node
->Data();
254 if (node
!= m_value
.First())
263 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
265 wxFAIL_MSG("Unimplemented");
270 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
272 wxFAIL_MSG("Unimplemented");
281 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
283 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
285 wxVariantDataLong() { m_value
= 0; }
286 wxVariantDataLong(long value
) { m_value
= value
; }
288 inline long GetValue() const { return m_value
; }
289 inline void SetValue(long value
) { m_value
= value
; }
291 virtual void Copy(wxVariantData
& data
);
292 virtual bool Eq(wxVariantData
& data
) const;
294 virtual bool Read(wxString
& str
);
295 virtual bool Write(wxString
& str
) const;
296 virtual bool Read(istream
& str
);
297 virtual bool Write(ostream
& str
) const;
300 virtual bool Read(wxInputStream
& str
);
301 virtual bool Write(wxOutputStream
&str
) const;
302 #endif // wxUSE_STREAMS
304 virtual wxString
GetType() const { return "long"; };
310 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
312 void wxVariantDataLong::Copy(wxVariantData
& data
)
314 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Copy: Can't copy to this type of data" );
316 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
318 otherData
.m_value
= m_value
;
321 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
323 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Eq: argument mismatch" );
325 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
327 return (otherData
.m_value
== m_value
);
330 bool wxVariantDataLong::Write(ostream
& str
) const
334 str
<< (const char*) s
;
338 bool wxVariantDataLong::Write(wxString
& str
) const
340 str
.Printf("%ld", m_value
);
344 bool wxVariantDataLong::Read(istream
& str
)
351 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
357 bool wxVariantDataLong::Read(wxInputStream
& str
)
362 #endif // wxUSE_STREAMS
364 bool wxVariantDataLong::Read(wxString
& str
)
366 m_value
= atol((const char*) str
);
374 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
376 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
378 wxVariantDataReal() { m_value
= 0.0; }
379 wxVariantDataReal(double value
) { m_value
= value
; }
381 inline double GetValue() const { return m_value
; }
382 inline void SetValue(double value
) { m_value
= value
; }
384 virtual void Copy(wxVariantData
& data
);
385 virtual bool Eq(wxVariantData
& data
) const;
386 virtual bool Read(wxString
& str
);
387 virtual bool Write(ostream
& str
) const;
388 virtual bool Write(wxString
& str
) const;
389 virtual bool Read(istream
& str
);
391 virtual bool Read(wxInputStream
& str
);
392 virtual bool Write(wxOutputStream
&str
) const;
393 #endif // wxUSE_STREAMS
394 virtual wxString
GetType() const { return "double"; };
400 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
402 void wxVariantDataReal::Copy(wxVariantData
& data
)
404 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Copy: Can't copy to this type of data" );
406 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
408 otherData
.m_value
= m_value
;
411 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
413 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Eq: argument mismatch" );
415 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
417 return (otherData
.m_value
== m_value
);
420 bool wxVariantDataReal::Write(ostream
& str
) const
424 str
<< (const char*) s
;
428 bool wxVariantDataReal::Write(wxString
& str
) const
430 str
.Printf("%.4f", m_value
);
434 bool wxVariantDataReal::Read(istream
& str
)
441 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
447 bool wxVariantDataReal::Read(wxInputStream
& str
)
449 str
>> (float&)m_value
;
452 #endif // wxUSE_STREAMS
454 bool wxVariantDataReal::Read(wxString
& str
)
456 m_value
= atof((const char*) str
);
465 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
467 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
469 wxVariantDataBool() { m_value
= 0; }
470 wxVariantDataBool(bool value
) { m_value
= value
; }
472 inline bool GetValue() const { return m_value
; }
473 inline void SetValue(bool value
) { m_value
= value
; }
475 virtual void Copy(wxVariantData
& data
);
476 virtual bool Eq(wxVariantData
& data
) const;
477 virtual bool Write(ostream
& str
) const;
478 virtual bool Write(wxString
& str
) const;
479 virtual bool Read(wxString
& str
);
480 virtual bool Read(istream
& str
);
482 virtual bool Read(wxInputStream
& str
);
483 virtual bool Write(wxOutputStream
& str
) const;
484 #endif // wxUSE_STREAMS
485 virtual wxString
GetType() const { return "bool"; };
491 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
493 void wxVariantDataBool::Copy(wxVariantData
& data
)
495 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Copy: Can't copy to this type of data" );
497 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
499 otherData
.m_value
= m_value
;
502 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
504 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Eq: argument mismatch" );
506 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
508 return (otherData
.m_value
== m_value
);
511 bool wxVariantDataBool::Write(ostream
& str
) const
515 str
<< (const char*) s
;
519 bool wxVariantDataBool::Write(wxString
& str
) const
521 str
.Printf("%d", (int) m_value
);
525 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
527 wxFAIL_MSG("Unimplemented");
528 // str >> (long) m_value;
533 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
535 str
<< (char)m_value
;
539 bool wxVariantDataBool::Read(wxInputStream
& str
)
541 str
>> (char&)m_value
;
544 #endif // wxUSE_STREAMS
546 bool wxVariantDataBool::Read(wxString
& str
)
548 m_value
= (atol((const char*) str
) != 0);
557 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
559 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
561 wxVariantDataChar() { m_value
= 0; }
562 wxVariantDataChar(char value
) { m_value
= value
; }
564 inline char GetValue() const { return m_value
; }
565 inline void SetValue(char value
) { m_value
= value
; }
567 virtual void Copy(wxVariantData
& data
);
568 virtual bool Eq(wxVariantData
& data
) const;
569 virtual bool Read(istream
& str
);
570 virtual bool Write(ostream
& str
) const;
571 virtual bool Read(wxString
& str
);
572 virtual bool Write(wxString
& str
) const;
574 virtual bool Read(wxInputStream
& str
);
575 virtual bool Write(wxOutputStream
& str
) const;
576 #endif // wxUSE_STREAMS
577 virtual wxString
GetType() const { return "char"; };
583 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
585 void wxVariantDataChar::Copy(wxVariantData
& data
)
587 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Copy: Can't copy to this type of data" );
589 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
591 otherData
.m_value
= m_value
;
594 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
596 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Eq: argument mismatch" );
598 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
600 return (otherData
.m_value
== m_value
);
603 bool wxVariantDataChar::Write(ostream
& str
) const
607 str
<< (const char*) s
;
611 bool wxVariantDataChar::Write(wxString
& str
) const
613 str
.Printf("%c", m_value
);
617 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
619 wxFAIL_MSG("Unimplemented");
625 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
631 bool wxVariantDataChar::Read(wxInputStream
& str
)
636 #endif // wxUSE_STREAMS
638 bool wxVariantDataChar::Read(wxString
& str
)
640 m_value
= str
[(size_t)0];
645 * wxVariantDataString
648 #if defined(__BORLANDC__) && defined(__WIN16__)
649 // Change name because of truncation
650 #define wxVariantDataString wxVariantStringData
653 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
655 #if defined(__BORLANDC__) && defined(__WIN16__)
656 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
658 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
661 wxVariantDataString() { }
662 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
664 inline wxString
GetValue() const { return m_value
; }
665 inline void SetValue(const wxString
& value
) { m_value
= value
; }
667 virtual void Copy(wxVariantData
& data
);
668 virtual bool Eq(wxVariantData
& data
) const;
669 virtual bool Write(ostream
& str
) const;
670 virtual bool Read(wxString
& str
);
671 virtual bool Write(wxString
& str
) const;
672 virtual bool Read(istream
& str
);
674 virtual bool Read(wxInputStream
& str
);
675 virtual bool Write(wxOutputStream
& str
) const;
676 #endif // wxUSE_STREAMS
677 virtual wxString
GetType() const { return "string"; };
683 void wxVariantDataString::Copy(wxVariantData
& data
)
685 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Copy: Can't copy to this type of data" );
687 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
689 otherData
.m_value
= m_value
;
692 bool wxVariantDataString::Eq(wxVariantData
& data
) const
694 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Eq: argument mismatch" );
696 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
698 return (otherData
.m_value
== m_value
);
701 bool wxVariantDataString::Write(ostream
& str
) const
703 str
<< (const char*) m_value
;
707 bool wxVariantDataString::Write(wxString
& str
) const
713 bool wxVariantDataString::Read(istream
& str
)
720 bool wxVariantDataString::Write(wxOutputStream
& str
) const
722 str
<< (const char*) m_value
;
726 bool wxVariantDataString::Read(wxInputStream
& str
)
731 #endif // wxUSE_STREAMS
733 bool wxVariantDataString::Read(wxString
& str
)
739 #if defined(__BORLANDC__) && defined(__WIN16__)
740 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
742 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
751 class wxVariantDataTime
: public wxVariantData
753 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
755 wxVariantDataTime() { }
756 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
758 inline wxTime
GetValue() const { return m_value
; }
759 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
761 virtual void Copy(wxVariantData
& data
);
762 virtual bool Eq(wxVariantData
& data
) const;
763 virtual bool Write(ostream
& str
) const;
764 virtual bool Write(wxString
& str
) const;
765 virtual bool Read(istream
& str
);
766 virtual bool Read(wxString
& str
);
767 virtual wxString
GetType() const { return "time"; };
768 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
774 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
776 void wxVariantDataTime::Copy(wxVariantData
& data
)
778 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Copy: Can't copy to this type of data" );
780 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
782 otherData
.m_value
= m_value
;
785 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
787 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Eq: argument mismatch" );
789 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
791 return (otherData
.m_value
== m_value
);
794 bool wxVariantDataTime::Write(ostream
& str
) const
798 str
<< (const char*) s
;
802 bool wxVariantDataTime::Write(wxString
& str
) const
804 char*s
= m_value
.FormatTime();
809 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
815 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
825 class wxVariantDataDate
: public wxVariantData
827 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
829 wxVariantDataDate() { }
830 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
832 inline wxDate
GetValue() const { return m_value
; }
833 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
835 virtual void Copy(wxVariantData
& data
);
836 virtual bool Eq(wxVariantData
& data
) const;
837 virtual bool Write(ostream
& str
) const;
838 virtual bool Write(wxString
& str
) const;
839 virtual bool Read(istream
& str
);
840 virtual bool Read(wxString
& str
);
841 virtual wxString
GetType() const { return "date"; };
842 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
848 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
850 void wxVariantDataDate::Copy(wxVariantData
& data
)
852 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Copy: Can't copy to this type of data" );
854 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
856 otherData
.m_value
= m_value
;
859 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
861 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Eq: argument mismatch" );
863 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
865 return (otherData
.m_value
== m_value
);
868 bool wxVariantDataDate::Write(ostream
& str
) const
872 str
<< (const char*) s
;
876 bool wxVariantDataDate::Write(wxString
& str
) const
878 str
= m_value
.FormatDate();
882 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
888 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
897 * wxVariantDataVoidPtr
900 class wxVariantDataVoidPtr
: public wxVariantData
902 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
904 wxVariantDataVoidPtr() { }
905 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
907 inline void* GetValue() const { return m_value
; }
908 inline void SetValue(void* value
) { m_value
= value
; }
910 virtual void Copy(wxVariantData
& data
);
911 virtual bool Eq(wxVariantData
& data
) const;
912 virtual bool Write(ostream
& str
) const;
913 virtual bool Write(wxString
& str
) const;
914 virtual bool Read(istream
& str
);
915 virtual bool Read(wxString
& str
);
916 virtual wxString
GetType() const { return "void*"; };
917 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
923 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
925 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
927 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Copy: Can't copy to this type of data" );
929 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
931 otherData
.m_value
= m_value
;
934 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
936 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Eq: argument mismatch" );
938 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
940 return (otherData
.m_value
== m_value
);
943 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
947 str
<< (const char*) s
;
951 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
954 sprintf(buf
, "%ld", (long) m_value
);
959 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
965 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
976 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
978 // Construction & destruction
979 wxVariant::wxVariant()
981 m_data
= (wxVariantData
*) NULL
;
984 wxVariant::wxVariant(double val
, const wxString
& name
)
986 m_data
= new wxVariantDataReal(val
);
990 wxVariant::wxVariant(long val
, const wxString
& name
)
992 m_data
= new wxVariantDataLong(val
);
997 wxVariant::wxVariant(bool val
, const wxString
& name
)
999 m_data
= new wxVariantDataBool(val
);
1004 wxVariant::wxVariant(char val
, const wxString
& name
)
1006 m_data
= new wxVariantDataChar(val
);
1010 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1012 m_data
= new wxVariantDataString(val
);
1016 wxVariant::wxVariant(const char* val
, const wxString
& name
)
1018 m_data
= new wxVariantDataString(wxString(val
));
1022 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1024 m_data
= new wxVariantDataStringList(val
);
1028 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1030 m_data
= new wxVariantDataList(val
);
1035 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1037 m_data
= new wxVariantDataTime(val
);
1041 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1043 m_data
= new wxVariantDataDate(val
);
1048 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1050 m_data
= new wxVariantDataVoidPtr(val
);
1054 wxVariant::wxVariant(const wxVariant
& variant
)
1056 if (!variant
.IsNull())
1058 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1059 variant
.m_data
->Copy(*m_data
);
1062 m_data
= (wxVariantData
*) NULL
;
1063 m_name
= variant
.m_name
;
1066 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1072 wxVariant::~wxVariant()
1078 // Make NULL (i.e. delete the data)
1079 void wxVariant::MakeNull()
1085 // Generic operators
1087 void wxVariant::operator= (const wxVariant
& variant
)
1089 if (variant
.IsNull())
1095 if (IsNull() || (GetType() != variant
.GetType()))
1099 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1101 // GetData()->Copy(* variant.GetData());
1102 variant
.GetData()->Copy(* GetData());
1105 // Assignment using data, e.g.
1106 // myVariant = new wxStringVariantData("hello")
1107 void wxVariant::operator= (wxVariantData
* variantData
)
1110 m_data
= variantData
;
1113 bool wxVariant::operator== (const wxVariant
& variant
) const
1115 if (IsNull() || variant
.IsNull())
1116 return (IsNull() == variant
.IsNull());
1118 return (GetData()->Eq(* variant
.GetData()));
1121 bool wxVariant::operator!= (const wxVariant
& variant
) const
1123 return (!(*this == variant
));
1127 // Specific operators
1128 bool wxVariant::operator== (double value
) const
1131 if (!Convert(&thisValue
))
1134 return (value
== thisValue
);
1137 bool wxVariant::operator!= (double value
) const
1139 return (!((*this) == value
));
1142 void wxVariant::operator= (double value
)
1144 if (GetType() == "double")
1146 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1152 m_data
= new wxVariantDataReal(value
);
1156 bool wxVariant::operator== (long value
) const
1159 if (!Convert(&thisValue
))
1162 return (value
== thisValue
);
1165 bool wxVariant::operator!= (long value
) const
1167 return (!((*this) == value
));
1170 void wxVariant::operator= (long value
)
1172 if (GetType() == "long")
1174 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1180 m_data
= new wxVariantDataLong(value
);
1184 bool wxVariant::operator== (char value
) const
1187 if (!Convert(&thisValue
))
1190 return (value
== thisValue
);
1193 bool wxVariant::operator!= (char value
) const
1195 return (!((*this) == value
));
1198 void wxVariant::operator= (char value
)
1200 if (GetType() == "char")
1202 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1208 m_data
= new wxVariantDataChar(value
);
1213 bool wxVariant::operator== (bool value
) const
1216 if (!Convert(&thisValue
))
1219 return (value
== thisValue
);
1222 bool wxVariant::operator!= (bool value
) const
1224 return (!((*this) == value
));
1227 void wxVariant::operator= (bool value
)
1229 if (GetType() == "bool")
1231 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1237 m_data
= new wxVariantDataBool(value
);
1242 bool wxVariant::operator== (const wxString
& value
) const
1245 if (!Convert(&thisValue
))
1248 return (value
== thisValue
);
1251 bool wxVariant::operator!= (const wxString
& value
) const
1253 return (!((*this) == value
));
1256 void wxVariant::operator= (const wxString
& value
)
1258 if (GetType() == "string")
1260 ((wxVariantDataString
*)GetData())->SetValue(value
);
1266 m_data
= new wxVariantDataString(value
);
1270 void wxVariant::operator= (const char* value
)
1272 if (GetType() == "string")
1274 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1280 m_data
= new wxVariantDataString(wxString(value
));
1284 bool wxVariant::operator== (const wxStringList
& value
) const
1286 wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" );
1288 wxVariantDataStringList
other(value
);
1289 return (m_data
->Eq(other
));
1292 bool wxVariant::operator!= (const wxStringList
& value
) const
1294 return (!((*this) == value
));
1297 void wxVariant::operator= (const wxStringList
& value
)
1299 if (GetType() == "stringlist")
1301 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1307 m_data
= new wxVariantDataStringList(value
);
1311 bool wxVariant::operator== (const wxList
& value
) const
1313 wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" );
1315 wxVariantDataList
other(value
);
1316 return (m_data
->Eq(other
));
1319 bool wxVariant::operator!= (const wxList
& value
) const
1321 return (!((*this) == value
));
1324 void wxVariant::operator= (const wxList
& value
)
1326 if (GetType() == "list")
1328 ((wxVariantDataList
*)GetData())->SetValue(value
);
1334 m_data
= new wxVariantDataList(value
);
1339 bool wxVariant::operator== (const wxTime
& value
) const
1342 if (!Convert(&thisValue
))
1345 return (value
== thisValue
);
1348 bool wxVariant::operator!= (const wxTime
& value
) const
1350 return (!((*this) == value
));
1353 void wxVariant::operator= (const wxTime
& value
)
1355 if (GetType() == "time")
1357 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1363 m_data
= new wxVariantDataTime(value
);
1367 bool wxVariant::operator== (const wxDate
& value
) const
1370 if (!Convert(&thisValue
))
1373 return (value
== thisValue
);
1376 bool wxVariant::operator!= (const wxDate
& value
) const
1378 return (!((*this) == value
));
1381 void wxVariant::operator= (const wxDate
& value
)
1383 if (GetType() == "date")
1385 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1391 m_data
= new wxVariantDataDate(value
);
1396 bool wxVariant::operator== (void* value
) const
1398 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1401 bool wxVariant::operator!= (void* value
) const
1403 return (!((*this) == value
));
1406 void wxVariant::operator= (void* value
)
1408 if (GetType() == "void*")
1410 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1416 m_data
= new wxVariantDataVoidPtr(value
);
1420 // Treat a list variant as an array
1421 wxVariant
wxVariant::operator[] (size_t idx
) const
1423 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" );
1425 if (GetType() == "list")
1427 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1428 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1429 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1431 else if (GetType() == "stringlist")
1433 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1434 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1436 wxVariant
variant( wxString( (char*) (data
->GetValue().Nth(idx
)->Data()) ));
1439 return wxNullVariant
;
1442 wxVariant
& wxVariant::operator[] (size_t idx
)
1444 // We can't return a reference to a variant for a string list, since the string
1445 // is actually stored as a char*, not a variant.
1447 wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" );
1449 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1450 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1452 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1455 // Return the number of elements in a list
1456 int wxVariant::GetCount() const
1458 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" );
1460 if (GetType() == "list")
1462 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1463 return data
->GetValue().Number();
1465 else if (GetType() == "stringlist")
1467 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1468 return data
->GetValue().Number();
1473 wxString
wxVariant::MakeString() const
1478 if (GetData()->Write(str
))
1481 return wxString("");
1486 void wxVariant::SetData(wxVariantData
* data
)
1488 if (m_data
) delete m_data
;
1493 // Returns a string representing the type of the variant,
1494 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1495 wxString
wxVariant::GetType() const
1498 return wxString("null");
1500 return m_data
->GetType();
1504 bool wxVariant::IsType(const wxString
& type
) const
1506 return (GetType() == type
);
1511 double wxVariant::GetReal() const
1514 if (Convert(& value
))
1518 wxFAIL_MSG("Could not convert to a real number");
1523 long wxVariant::GetInteger() const
1526 if (Convert(& value
))
1530 wxFAIL_MSG("Could not convert to an integer");
1535 char wxVariant::GetChar() const
1538 if (Convert(& value
))
1542 wxFAIL_MSG("Could not convert to a char");
1547 bool wxVariant::GetBool() const
1550 if (Convert(& value
))
1554 wxFAIL_MSG("Could not convert to a bool");
1559 wxString
wxVariant::GetString() const
1562 if (Convert(& value
))
1566 wxFAIL_MSG("Could not convert to a string");
1567 return wxString("");
1572 wxTime
wxVariant::GetTime() const
1575 if (Convert(& value
))
1579 wxFAIL_MSG("Could not convert to a time");
1584 wxDate
wxVariant::GetDate() const
1587 if (Convert(& value
))
1591 wxFAIL_MSG("Could not convert to a date");
1597 void* wxVariant::GetVoidPtr() const
1599 wxASSERT( (GetType() == "void*") );
1601 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1604 wxList
& wxVariant::GetList() const
1606 wxASSERT( (GetType() == "list") );
1608 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1611 wxStringList
& wxVariant::GetStringList() const
1613 wxASSERT( (GetType() == "stringlist") );
1615 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1619 void wxVariant::Append(const wxVariant
& value
)
1621 wxList
& list
= GetList();
1623 list
.Append(new wxVariant(value
));
1626 // Insert at front of list
1627 void wxVariant::Insert(const wxVariant
& value
)
1629 wxList
& list
= GetList();
1631 list
.Insert(new wxVariant(value
));
1634 // Returns TRUE if the variant is a member of the list
1635 bool wxVariant::Member(const wxVariant
& value
) const
1637 wxList
& list
= GetList();
1639 wxNode
* node
= list
.First();
1642 wxVariant
* other
= (wxVariant
*) node
->Data();
1643 if (value
== *other
)
1645 node
= node
->Next();
1650 // Deletes the nth element of the list
1651 bool wxVariant::Delete(int item
)
1653 wxList
& list
= GetList();
1655 wxASSERT_MSG( (item
< list
.Number()), "Invalid index to Delete" );
1656 wxNode
* node
= list
.Nth(item
);
1657 wxVariant
* variant
= (wxVariant
*) node
->Data();
1664 void wxVariant::ClearList()
1666 if (!IsNull() && (GetType() == "list"))
1668 ((wxVariantDataList
*) m_data
)->Clear();
1672 if (GetType() != "list")
1677 m_data
= new wxVariantDataList
;
1682 bool wxVariant::Convert(long* value
) const
1684 wxString
type(GetType());
1685 if (type
== "double")
1686 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1687 else if (type
== "long")
1688 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1690 else if (type
== "bool")
1691 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1693 else if (type
== "string")
1694 *value
= atol((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1701 bool wxVariant::Convert(bool* value
) const
1703 wxString
type(GetType());
1704 if (type
== "double")
1705 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1706 else if (type
== "long")
1707 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1709 else if (type
== "bool")
1710 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1712 else if (type
== "string")
1714 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1716 if (val
== "true" || val
== "yes")
1718 else if (val
== "false" || val
== "no")
1729 bool wxVariant::Convert(double* value
) const
1731 wxString
type(GetType());
1732 if (type
== "double")
1733 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1734 else if (type
== "long")
1735 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1737 else if (type
== "bool")
1738 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1740 else if (type
== "string")
1741 *value
= (double) atof((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1748 bool wxVariant::Convert(char* value
) const
1750 wxString
type(GetType());
1752 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1753 else if (type
== "long")
1754 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1756 else if (type
== "bool")
1757 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1765 bool wxVariant::Convert(wxString
* value
) const
1767 *value
= MakeString();
1772 bool wxVariant::Convert(wxTime
* value
) const
1774 wxString
type(GetType());
1776 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1777 else if (type
== "date")
1778 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1785 bool wxVariant::Convert(wxDate
* value
) const
1787 wxString
type(GetType());
1789 *value
= ((wxVariantDataDate
*)GetData())->GetValue();