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 _T("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() == _T("list")), _T("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() == _T("list")), _T("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
.mb_str();
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(_T("Unimplemented"));
170 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
172 wxFAIL_MSG(_T("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 _T("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() == _T("stringlist")), _T("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() == _T("stringlist")), _T("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 ((wxChar
*) node1
->Data());
229 wxString
str2 ((wxChar
*) 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
.mb_str();
247 bool wxVariantDataStringList::Write(wxString
& str
) const
250 wxNode
* node
= m_value
.First();
253 wxChar
* s
= (wxChar
*) node
->Data();
254 if (node
!= m_value
.First())
263 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
265 wxFAIL_MSG(_T("Unimplemented"));
270 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
272 wxFAIL_MSG(_T("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 _T("long"); };
310 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
312 void wxVariantDataLong::Copy(wxVariantData
& data
)
314 wxASSERT_MSG( (data
.GetType() == _T("long")), _T("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() == _T("long")), _T("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
.mb_str();
338 bool wxVariantDataLong::Write(wxString
& str
) const
340 str
.Printf(_T("%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
= wxAtol((const wxChar
*) 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 _T("double"); };
400 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
402 void wxVariantDataReal::Copy(wxVariantData
& data
)
404 wxASSERT_MSG( (data
.GetType() == _T("double")), _T("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() == _T("double")), _T("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
.mb_str();
428 bool wxVariantDataReal::Write(wxString
& str
) const
430 str
.Printf(_T("%.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
= wxAtof((const wxChar
*) 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 _T("bool"); };
491 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
493 void wxVariantDataBool::Copy(wxVariantData
& data
)
495 wxASSERT_MSG( (data
.GetType() == _T("bool")), _T("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() == _T("bool")), _T("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
.mb_str();
519 bool wxVariantDataBool::Write(wxString
& str
) const
521 str
.Printf(_T("%d"), (int) m_value
);
525 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
527 wxFAIL_MSG(_T("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
= (wxAtol((const wxChar
*) 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 _T("char"); };
583 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
585 void wxVariantDataChar::Copy(wxVariantData
& data
)
587 wxASSERT_MSG( (data
.GetType() == _T("char")), _T("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() == _T("char")), _T("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
.mb_str();
611 bool wxVariantDataChar::Write(wxString
& str
) const
613 str
.Printf(_T("%c"), m_value
);
617 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
619 wxFAIL_MSG(_T("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 _T("string"); };
683 void wxVariantDataString::Copy(wxVariantData
& data
)
685 wxASSERT_MSG( (data
.GetType() == _T("string")), _T("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() == _T("string")), _T("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
.mb_str();
707 bool wxVariantDataString::Write(wxString
& str
) const
713 bool wxVariantDataString::Read(istream
& str
)
720 bool wxVariantDataString::Write(wxOutputStream
& str
) const
722 // why doesn't wxOutputStream::operator<< take "const wxString&"
723 str
<< (const char*) m_value
.mb_str();
727 bool wxVariantDataString::Read(wxInputStream
& str
)
732 #endif // wxUSE_STREAMS
734 bool wxVariantDataString::Read(wxString
& str
)
740 #if defined(__BORLANDC__) && defined(__WIN16__)
741 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
743 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
752 class wxVariantDataTime
: public wxVariantData
754 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
756 wxVariantDataTime() { }
757 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
759 inline wxTime
GetValue() const { return m_value
; }
760 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
762 virtual void Copy(wxVariantData
& data
);
763 virtual bool Eq(wxVariantData
& data
) const;
764 virtual bool Write(ostream
& str
) const;
765 virtual bool Write(wxString
& str
) const;
766 virtual bool Read(istream
& str
);
767 virtual bool Read(wxString
& str
);
768 virtual wxString
GetType() const { return _T("time"); };
769 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
775 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
777 void wxVariantDataTime::Copy(wxVariantData
& data
)
779 wxASSERT_MSG( (data
.GetType() == _T("time")), _T("wxVariantDataTime::Copy: Can't copy to this type of data") );
781 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
783 otherData
.m_value
= m_value
;
786 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
788 wxASSERT_MSG( (data
.GetType() == _T("time")), _T("wxVariantDataTime::Eq: argument mismatch") );
790 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
792 return (otherData
.m_value
== m_value
);
795 bool wxVariantDataTime::Write(ostream
& str
) const
799 str
<< (const char*) s
.mb_str();
803 bool wxVariantDataTime::Write(wxString
& str
) const
805 wxChar
*s
= m_value
.FormatTime();
810 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
816 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
826 class wxVariantDataDate
: public wxVariantData
828 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
830 wxVariantDataDate() { }
831 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
833 inline wxDate
GetValue() const { return m_value
; }
834 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
836 virtual void Copy(wxVariantData
& data
);
837 virtual bool Eq(wxVariantData
& data
) const;
838 virtual bool Write(ostream
& str
) const;
839 virtual bool Write(wxString
& str
) const;
840 virtual bool Read(istream
& str
);
841 virtual bool Read(wxString
& str
);
842 virtual wxString
GetType() const { return _T("date"); };
843 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
849 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
851 void wxVariantDataDate::Copy(wxVariantData
& data
)
853 wxASSERT_MSG( (data
.GetType() == _T("date")), _T("wxVariantDataDate::Copy: Can't copy to this type of data") );
855 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
857 otherData
.m_value
= m_value
;
860 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
862 wxASSERT_MSG( (data
.GetType() == _T("date")), _T("wxVariantDataDate::Eq: argument mismatch") );
864 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
866 return (otherData
.m_value
== m_value
);
869 bool wxVariantDataDate::Write(ostream
& str
) const
873 str
<< (const char*) s
.mb_str();
877 bool wxVariantDataDate::Write(wxString
& str
) const
879 str
= m_value
.FormatDate();
883 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
889 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
898 * wxVariantDataVoidPtr
901 class wxVariantDataVoidPtr
: public wxVariantData
903 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
905 wxVariantDataVoidPtr() { }
906 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
908 inline void* GetValue() const { return m_value
; }
909 inline void SetValue(void* value
) { m_value
= value
; }
911 virtual void Copy(wxVariantData
& data
);
912 virtual bool Eq(wxVariantData
& data
) const;
913 virtual bool Write(ostream
& str
) const;
914 virtual bool Write(wxString
& str
) const;
915 virtual bool Read(istream
& str
);
916 virtual bool Read(wxString
& str
);
917 virtual wxString
GetType() const { return _T("void*"); };
918 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
924 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
926 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
928 wxASSERT_MSG( (data
.GetType() == _T("void*")), _T("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
930 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
932 otherData
.m_value
= m_value
;
935 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
937 wxASSERT_MSG( (data
.GetType() == _T("void*")), _T("wxVariantDataVoidPtr::Eq: argument mismatch") );
939 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
941 return (otherData
.m_value
== m_value
);
944 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
948 str
<< (const char*) s
.mb_str();
952 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
954 str
.Printf(_T("%ld"), (long) m_value
);
958 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
964 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
975 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
977 // Construction & destruction
978 wxVariant::wxVariant()
980 m_data
= (wxVariantData
*) NULL
;
983 wxVariant::wxVariant(double val
, const wxString
& name
)
985 m_data
= new wxVariantDataReal(val
);
989 wxVariant::wxVariant(long val
, const wxString
& name
)
991 m_data
= new wxVariantDataLong(val
);
996 wxVariant::wxVariant(bool val
, const wxString
& name
)
998 m_data
= new wxVariantDataBool(val
);
1003 wxVariant::wxVariant(char val
, const wxString
& name
)
1005 m_data
= new wxVariantDataChar(val
);
1009 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1011 m_data
= new wxVariantDataString(val
);
1015 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1017 m_data
= new wxVariantDataString(wxString(val
));
1021 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1023 m_data
= new wxVariantDataStringList(val
);
1027 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1029 m_data
= new wxVariantDataList(val
);
1034 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1036 m_data
= new wxVariantDataTime(val
);
1040 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1042 m_data
= new wxVariantDataDate(val
);
1047 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1049 m_data
= new wxVariantDataVoidPtr(val
);
1053 wxVariant::wxVariant(const wxVariant
& variant
)
1055 if (!variant
.IsNull())
1057 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1058 variant
.m_data
->Copy(*m_data
);
1061 m_data
= (wxVariantData
*) NULL
;
1062 m_name
= variant
.m_name
;
1065 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1071 wxVariant::~wxVariant()
1077 // Make NULL (i.e. delete the data)
1078 void wxVariant::MakeNull()
1084 // Generic operators
1086 void wxVariant::operator= (const wxVariant
& variant
)
1088 if (variant
.IsNull())
1094 if (IsNull() || (GetType() != variant
.GetType()))
1098 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1100 // GetData()->Copy(* variant.GetData());
1101 variant
.GetData()->Copy(* GetData());
1104 // Assignment using data, e.g.
1105 // myVariant = new wxStringVariantData("hello")
1106 void wxVariant::operator= (wxVariantData
* variantData
)
1109 m_data
= variantData
;
1112 bool wxVariant::operator== (const wxVariant
& variant
) const
1114 if (IsNull() || variant
.IsNull())
1115 return (IsNull() == variant
.IsNull());
1117 return (GetData()->Eq(* variant
.GetData()));
1120 bool wxVariant::operator!= (const wxVariant
& variant
) const
1122 return (!(*this == variant
));
1126 // Specific operators
1127 bool wxVariant::operator== (double value
) const
1130 if (!Convert(&thisValue
))
1133 return (value
== thisValue
);
1136 bool wxVariant::operator!= (double value
) const
1138 return (!((*this) == value
));
1141 void wxVariant::operator= (double value
)
1143 if (GetType() == _T("double"))
1145 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1151 m_data
= new wxVariantDataReal(value
);
1155 bool wxVariant::operator== (long value
) const
1158 if (!Convert(&thisValue
))
1161 return (value
== thisValue
);
1164 bool wxVariant::operator!= (long value
) const
1166 return (!((*this) == value
));
1169 void wxVariant::operator= (long value
)
1171 if (GetType() == _T("long"))
1173 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1179 m_data
= new wxVariantDataLong(value
);
1183 bool wxVariant::operator== (char value
) const
1186 if (!Convert(&thisValue
))
1189 return (value
== thisValue
);
1192 bool wxVariant::operator!= (char value
) const
1194 return (!((*this) == value
));
1197 void wxVariant::operator= (char value
)
1199 if (GetType() == _T("char"))
1201 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1207 m_data
= new wxVariantDataChar(value
);
1212 bool wxVariant::operator== (bool value
) const
1215 if (!Convert(&thisValue
))
1218 return (value
== thisValue
);
1221 bool wxVariant::operator!= (bool value
) const
1223 return (!((*this) == value
));
1226 void wxVariant::operator= (bool value
)
1228 if (GetType() == _T("bool"))
1230 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1236 m_data
= new wxVariantDataBool(value
);
1241 bool wxVariant::operator== (const wxString
& value
) const
1244 if (!Convert(&thisValue
))
1247 return (value
== thisValue
);
1250 bool wxVariant::operator!= (const wxString
& value
) const
1252 return (!((*this) == value
));
1255 void wxVariant::operator= (const wxString
& value
)
1257 if (GetType() == _T("string"))
1259 ((wxVariantDataString
*)GetData())->SetValue(value
);
1265 m_data
= new wxVariantDataString(value
);
1269 void wxVariant::operator= (const wxChar
* value
)
1271 if (GetType() == _T("string"))
1273 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1279 m_data
= new wxVariantDataString(wxString(value
));
1283 bool wxVariant::operator== (const wxStringList
& value
) const
1285 wxASSERT_MSG( (GetType() == _T("stringlist")), _T("Invalid type for == operator") );
1287 wxVariantDataStringList
other(value
);
1288 return (m_data
->Eq(other
));
1291 bool wxVariant::operator!= (const wxStringList
& value
) const
1293 return (!((*this) == value
));
1296 void wxVariant::operator= (const wxStringList
& value
)
1298 if (GetType() == _T("stringlist"))
1300 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1306 m_data
= new wxVariantDataStringList(value
);
1310 bool wxVariant::operator== (const wxList
& value
) const
1312 wxASSERT_MSG( (GetType() == _T("list")), _T("Invalid type for == operator") );
1314 wxVariantDataList
other(value
);
1315 return (m_data
->Eq(other
));
1318 bool wxVariant::operator!= (const wxList
& value
) const
1320 return (!((*this) == value
));
1323 void wxVariant::operator= (const wxList
& value
)
1325 if (GetType() == _T("list"))
1327 ((wxVariantDataList
*)GetData())->SetValue(value
);
1333 m_data
= new wxVariantDataList(value
);
1338 bool wxVariant::operator== (const wxTime
& value
) const
1341 if (!Convert(&thisValue
))
1344 return (value
== thisValue
);
1347 bool wxVariant::operator!= (const wxTime
& value
) const
1349 return (!((*this) == value
));
1352 void wxVariant::operator= (const wxTime
& value
)
1354 if (GetType() == _T("time"))
1356 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1362 m_data
= new wxVariantDataTime(value
);
1366 bool wxVariant::operator== (const wxDate
& value
) const
1369 if (!Convert(&thisValue
))
1372 return (value
== thisValue
);
1375 bool wxVariant::operator!= (const wxDate
& value
) const
1377 return (!((*this) == value
));
1380 void wxVariant::operator= (const wxDate
& value
)
1382 if (GetType() == _T("date"))
1384 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1390 m_data
= new wxVariantDataDate(value
);
1395 bool wxVariant::operator== (void* value
) const
1397 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1400 bool wxVariant::operator!= (void* value
) const
1402 return (!((*this) == value
));
1405 void wxVariant::operator= (void* value
)
1407 if (GetType() == _T("void*"))
1409 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1415 m_data
= new wxVariantDataVoidPtr(value
);
1419 // Treat a list variant as an array
1420 wxVariant
wxVariant::operator[] (size_t idx
) const
1422 wxASSERT_MSG( (GetType() == _T("list") || GetType() == _T("stringlist")), _T("Invalid type for array operator") );
1424 if (GetType() == _T("list"))
1426 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1427 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), _T("Invalid index for array") );
1428 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1430 else if (GetType() == _T("stringlist"))
1432 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1433 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), _T("Invalid index for array") );
1435 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1438 return wxNullVariant
;
1441 wxVariant
& wxVariant::operator[] (size_t idx
)
1443 // We can't return a reference to a variant for a string list, since the string
1444 // is actually stored as a char*, not a variant.
1446 wxASSERT_MSG( (GetType() == _T("list")), _T("Invalid type for array operator") );
1448 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1449 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), _T("Invalid index for array") );
1451 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1454 // Return the number of elements in a list
1455 int wxVariant::GetCount() const
1457 wxASSERT_MSG( (GetType() == _T("list") || GetType() == _T("stringlist")), _T("Invalid type for GetCount()") );
1459 if (GetType() == _T("list"))
1461 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1462 return data
->GetValue().Number();
1464 else if (GetType() == _T("stringlist"))
1466 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1467 return data
->GetValue().Number();
1472 wxString
wxVariant::MakeString() const
1477 if (GetData()->Write(str
))
1480 return wxString(_T(""));
1485 void wxVariant::SetData(wxVariantData
* data
)
1487 if (m_data
) delete m_data
;
1492 // Returns a string representing the type of the variant,
1493 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1494 wxString
wxVariant::GetType() const
1497 return wxString(_T("null"));
1499 return m_data
->GetType();
1503 bool wxVariant::IsType(const wxString
& type
) const
1505 return (GetType() == type
);
1510 double wxVariant::GetReal() const
1513 if (Convert(& value
))
1517 wxFAIL_MSG(_T("Could not convert to a real number"));
1522 long wxVariant::GetInteger() const
1525 if (Convert(& value
))
1529 wxFAIL_MSG(_T("Could not convert to an integer"));
1534 char wxVariant::GetChar() const
1537 if (Convert(& value
))
1541 wxFAIL_MSG(_T("Could not convert to a char"));
1546 bool wxVariant::GetBool() const
1549 if (Convert(& value
))
1553 wxFAIL_MSG(_T("Could not convert to a bool"));
1558 wxString
wxVariant::GetString() const
1561 if (Convert(& value
))
1565 wxFAIL_MSG(_T("Could not convert to a string"));
1566 return wxString("");
1571 wxTime
wxVariant::GetTime() const
1574 if (Convert(& value
))
1578 wxFAIL_MSG(_T("Could not convert to a time"));
1583 wxDate
wxVariant::GetDate() const
1586 if (Convert(& value
))
1590 wxFAIL_MSG(_T("Could not convert to a date"));
1596 void* wxVariant::GetVoidPtr() const
1598 wxASSERT( (GetType() == _T("void*")) );
1600 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1603 wxList
& wxVariant::GetList() const
1605 wxASSERT( (GetType() == _T("list")) );
1607 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1610 wxStringList
& wxVariant::GetStringList() const
1612 wxASSERT( (GetType() == _T("stringlist")) );
1614 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1618 void wxVariant::Append(const wxVariant
& value
)
1620 wxList
& list
= GetList();
1622 list
.Append(new wxVariant(value
));
1625 // Insert at front of list
1626 void wxVariant::Insert(const wxVariant
& value
)
1628 wxList
& list
= GetList();
1630 list
.Insert(new wxVariant(value
));
1633 // Returns TRUE if the variant is a member of the list
1634 bool wxVariant::Member(const wxVariant
& value
) const
1636 wxList
& list
= GetList();
1638 wxNode
* node
= list
.First();
1641 wxVariant
* other
= (wxVariant
*) node
->Data();
1642 if (value
== *other
)
1644 node
= node
->Next();
1649 // Deletes the nth element of the list
1650 bool wxVariant::Delete(int item
)
1652 wxList
& list
= GetList();
1654 wxASSERT_MSG( (item
< list
.Number()), _T("Invalid index to Delete") );
1655 wxNode
* node
= list
.Nth(item
);
1656 wxVariant
* variant
= (wxVariant
*) node
->Data();
1663 void wxVariant::ClearList()
1665 if (!IsNull() && (GetType() == _T("list")))
1667 ((wxVariantDataList
*) m_data
)->Clear();
1671 if (GetType() != _T("list"))
1676 m_data
= new wxVariantDataList
;
1681 bool wxVariant::Convert(long* value
) const
1683 wxString
type(GetType());
1684 if (type
== _T("double"))
1685 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1686 else if (type
== _T("long"))
1687 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1689 else if (type
== _T("bool"))
1690 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1692 else if (type
== _T("string"))
1693 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1700 bool wxVariant::Convert(bool* value
) const
1702 wxString
type(GetType());
1703 if (type
== _T("double"))
1704 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1705 else if (type
== _T("long"))
1706 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1708 else if (type
== _T("bool"))
1709 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1711 else if (type
== _T("string"))
1713 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1715 if (val
== _T("true") || val
== _T("yes"))
1717 else if (val
== _T("false") || val
== _T("no"))
1728 bool wxVariant::Convert(double* value
) const
1730 wxString
type(GetType());
1731 if (type
== _T("double"))
1732 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1733 else if (type
== _T("long"))
1734 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1736 else if (type
== _T("bool"))
1737 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1739 else if (type
== _T("string"))
1740 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1747 bool wxVariant::Convert(char* value
) const
1749 wxString
type(GetType());
1750 if (type
== _T("char"))
1751 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1752 else if (type
== _T("long"))
1753 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1755 else if (type
== _T("bool"))
1756 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1764 bool wxVariant::Convert(wxString
* value
) const
1766 *value
= MakeString();
1771 bool wxVariant::Convert(wxTime
* value
) const
1773 wxString
type(GetType());
1774 if (type
== _T("time"))
1775 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1776 else if (type
== _T("date"))
1777 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1784 bool wxVariant::Convert(wxDate
* value
) const
1786 wxString
type(GetType());
1787 if (type
== _T("date"))
1788 *value
= ((wxVariantDataDate
*)GetData())->GetValue();