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"
23 #if wxUSE_STD_IOSTREAM
32 #include "wx/stream.h"
33 #include "wx/txtstrm.h"
36 #include "wx/string.h"
37 #include "wx/variant.h"
39 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
41 wxVariant WXDLLEXPORT wxNullVariant
;
47 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
49 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
51 wxVariantDataList() {}
52 wxVariantDataList(const wxList
& list
);
55 wxList
& GetValue() const { return (wxList
&) m_value
; }
56 void SetValue(const wxList
& value
) ;
58 virtual void Copy(wxVariantData
& data
);
59 virtual bool Eq(wxVariantData
& data
) const;
60 #if wxUSE_STD_IOSTREAM
61 virtual bool Write(ostream
& str
) const;
63 virtual bool Write(wxString
& str
) const;
64 #if wxUSE_STD_IOSTREAM
65 virtual bool Read(istream
& str
);
67 virtual bool Read(wxString
& str
);
68 virtual wxString
GetType() const { return wxT("list"); };
76 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
78 wxVariantDataList::wxVariantDataList(const wxList
& list
)
83 wxVariantDataList::~wxVariantDataList()
88 void wxVariantDataList::SetValue(const wxList
& value
)
91 wxNode
* node
= value
.First();
94 wxVariant
* var
= (wxVariant
*) node
->Data();
95 m_value
.Append(new wxVariant(*var
));
100 void wxVariantDataList::Clear()
102 wxNode
* node
= m_value
.First();
105 wxVariant
* var
= (wxVariant
*) node
->Data();
112 void wxVariantDataList::Copy(wxVariantData
& data
)
114 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
116 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
119 wxNode
* node
= m_value
.First();
122 wxVariant
* var
= (wxVariant
*) node
->Data();
123 listData
.m_value
.Append(new wxVariant(*var
));
128 bool wxVariantDataList::Eq(wxVariantData
& data
) const
130 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
132 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
133 wxNode
* node1
= m_value
.First();
134 wxNode
* node2
= listData
.GetValue().First();
135 while (node1
&& node2
)
137 wxVariant
* var1
= (wxVariant
*) node1
->Data();
138 wxVariant
* var2
= (wxVariant
*) node2
->Data();
139 if ((*var1
) != (*var2
))
141 node1
= node1
->Next();
142 node2
= node2
->Next();
144 if (node1
|| node2
) return FALSE
;
148 #if wxUSE_STD_IOSTREAM
149 bool wxVariantDataList::Write(ostream
& str
) const
153 str
<< (const char*) s
.mb_str();
158 bool wxVariantDataList::Write(wxString
& str
) const
161 wxNode
* node
= m_value
.First();
164 wxVariant
* var
= (wxVariant
*) node
->Data();
165 if (node
!= m_value
.First())
168 str
+= var
->MakeString();
175 #if wxUSE_STD_IOSTREAM
176 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
178 wxFAIL_MSG(wxT("Unimplemented"));
184 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
186 wxFAIL_MSG(wxT("Unimplemented"));
192 * wxVariantDataStringList
195 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
197 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
199 wxVariantDataStringList() {}
200 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
202 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
203 void SetValue(const wxStringList
& value
);
205 virtual void Copy(wxVariantData
& data
);
206 virtual bool Eq(wxVariantData
& data
) const;
207 #if wxUSE_STD_IOSTREAM
208 virtual bool Write(ostream
& str
) const;
210 virtual bool Write(wxString
& str
) const;
211 #if wxUSE_STD_IOSTREAM
212 virtual bool Read(istream
& str
);
214 virtual bool Read(wxString
& str
);
215 virtual wxString
GetType() const { return wxT("stringlist"); };
218 wxStringList m_value
;
221 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
223 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
228 void wxVariantDataStringList::Copy(wxVariantData
& data
)
230 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
232 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
234 listData
.m_value
= m_value
;
237 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
239 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
241 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
242 wxNode
* node1
= m_value
.First();
243 wxNode
* node2
= listData
.GetValue().First();
244 while (node1
&& node2
)
246 wxString
str1 ((wxChar
*) node1
->Data());
247 wxString
str2 ((wxChar
*) node2
->Data());
250 node1
= node1
->Next();
251 node2
= node2
->Next();
253 if (node1
|| node2
) return FALSE
;
257 #if wxUSE_STD_IOSTREAM
258 bool wxVariantDataStringList::Write(ostream
& str
) const
262 str
<< (const char*) s
.mb_str();
267 bool wxVariantDataStringList::Write(wxString
& str
) const
270 wxNode
* node
= m_value
.First();
273 wxChar
* s
= (wxChar
*) node
->Data();
274 if (node
!= m_value
.First())
283 #if wxUSE_STD_IOSTREAM
284 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
286 wxFAIL_MSG(wxT("Unimplemented"));
292 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
294 wxFAIL_MSG(wxT("Unimplemented"));
303 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
305 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
307 wxVariantDataLong() { m_value
= 0; }
308 wxVariantDataLong(long value
) { m_value
= value
; }
310 inline long GetValue() const { return m_value
; }
311 inline void SetValue(long value
) { m_value
= value
; }
313 virtual void Copy(wxVariantData
& data
);
314 virtual bool Eq(wxVariantData
& data
) const;
316 virtual bool Read(wxString
& str
);
317 virtual bool Write(wxString
& str
) const;
318 #if wxUSE_STD_IOSTREAM
319 virtual bool Read(istream
& str
);
320 virtual bool Write(ostream
& str
) const;
323 virtual bool Read(wxInputStream
& str
);
324 virtual bool Write(wxOutputStream
&str
) const;
325 #endif // wxUSE_STREAMS
327 virtual wxString
GetType() const { return wxT("long"); };
333 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
335 void wxVariantDataLong::Copy(wxVariantData
& data
)
337 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
339 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
341 otherData
.m_value
= m_value
;
344 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
346 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
348 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
350 return (otherData
.m_value
== m_value
);
353 #if wxUSE_STD_IOSTREAM
354 bool wxVariantDataLong::Write(ostream
& str
) const
358 str
<< (const char*) s
.mb_str();
363 bool wxVariantDataLong::Write(wxString
& str
) const
365 str
.Printf(wxT("%ld"), m_value
);
369 #if wxUSE_STD_IOSTREAM
370 bool wxVariantDataLong::Read(istream
& str
)
378 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
380 wxTextOutputStream
s(str
);
386 bool wxVariantDataLong::Read(wxInputStream
& str
)
388 wxTextInputStream
s(str
);
389 m_value
= s
.Read32();
392 #endif // wxUSE_STREAMS
394 bool wxVariantDataLong::Read(wxString
& str
)
396 m_value
= wxAtol((const wxChar
*) str
);
404 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
406 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
408 wxVariantDataReal() { m_value
= 0.0; }
409 wxVariantDataReal(double value
) { m_value
= value
; }
411 inline double GetValue() const { return m_value
; }
412 inline void SetValue(double value
) { m_value
= value
; }
414 virtual void Copy(wxVariantData
& data
);
415 virtual bool Eq(wxVariantData
& data
) const;
416 virtual bool Read(wxString
& str
);
417 #if wxUSE_STD_IOSTREAM
418 virtual bool Write(ostream
& str
) const;
420 virtual bool Write(wxString
& str
) const;
421 #if wxUSE_STD_IOSTREAM
422 virtual bool Read(istream
& str
);
425 virtual bool Read(wxInputStream
& str
);
426 virtual bool Write(wxOutputStream
&str
) const;
427 #endif // wxUSE_STREAMS
428 virtual wxString
GetType() const { return wxT("double"); };
434 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
436 void wxVariantDataReal::Copy(wxVariantData
& data
)
438 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
440 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
442 otherData
.m_value
= m_value
;
445 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
447 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
449 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
451 return (otherData
.m_value
== m_value
);
454 #if wxUSE_STD_IOSTREAM
455 bool wxVariantDataReal::Write(ostream
& str
) const
459 str
<< (const char*) s
.mb_str();
464 bool wxVariantDataReal::Write(wxString
& str
) const
466 str
.Printf(wxT("%.4f"), m_value
);
470 #if wxUSE_STD_IOSTREAM
471 bool wxVariantDataReal::Read(istream
& str
)
479 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
481 wxTextOutputStream
s(str
);
482 s
.WriteDouble((double)m_value
);
486 bool wxVariantDataReal::Read(wxInputStream
& str
)
488 wxTextInputStream
s(str
);
489 m_value
= (float)s
.ReadDouble();
492 #endif // wxUSE_STREAMS
494 bool wxVariantDataReal::Read(wxString
& str
)
496 m_value
= wxAtof((const wxChar
*) str
);
505 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
507 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
509 wxVariantDataBool() { m_value
= 0; }
510 wxVariantDataBool(bool value
) { m_value
= value
; }
512 inline bool GetValue() const { return m_value
; }
513 inline void SetValue(bool value
) { m_value
= value
; }
515 virtual void Copy(wxVariantData
& data
);
516 virtual bool Eq(wxVariantData
& data
) const;
517 #if wxUSE_STD_IOSTREAM
518 virtual bool Write(ostream
& str
) const;
520 virtual bool Write(wxString
& str
) const;
521 virtual bool Read(wxString
& str
);
522 #if wxUSE_STD_IOSTREAM
523 virtual bool Read(istream
& str
);
526 virtual bool Read(wxInputStream
& str
);
527 virtual bool Write(wxOutputStream
& str
) const;
528 #endif // wxUSE_STREAMS
529 virtual wxString
GetType() const { return wxT("bool"); };
535 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
537 void wxVariantDataBool::Copy(wxVariantData
& data
)
539 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
541 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
543 otherData
.m_value
= m_value
;
546 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
548 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
550 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
552 return (otherData
.m_value
== m_value
);
555 #if wxUSE_STD_IOSTREAM
556 bool wxVariantDataBool::Write(ostream
& str
) const
560 str
<< (const char*) s
.mb_str();
565 bool wxVariantDataBool::Write(wxString
& str
) const
567 str
.Printf(wxT("%d"), (int) m_value
);
571 #if wxUSE_STD_IOSTREAM
572 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
574 wxFAIL_MSG(wxT("Unimplemented"));
575 // str >> (long) m_value;
581 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
583 wxTextOutputStream
s(str
);
589 bool wxVariantDataBool::Read(wxInputStream
& str
)
591 wxTextInputStream
s(str
);
593 m_value
= s
.Read8() != 0;
596 #endif // wxUSE_STREAMS
598 bool wxVariantDataBool::Read(wxString
& str
)
600 m_value
= (wxAtol((const wxChar
*) str
) != 0);
609 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
611 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
613 wxVariantDataChar() { m_value
= 0; }
614 wxVariantDataChar(char value
) { m_value
= value
; }
616 inline char GetValue() const { return m_value
; }
617 inline void SetValue(char value
) { m_value
= value
; }
619 virtual void Copy(wxVariantData
& data
);
620 virtual bool Eq(wxVariantData
& data
) const;
621 #if wxUSE_STD_IOSTREAM
622 virtual bool Read(istream
& str
);
623 virtual bool Write(ostream
& str
) const;
625 virtual bool Read(wxString
& str
);
626 virtual bool Write(wxString
& str
) const;
628 virtual bool Read(wxInputStream
& str
);
629 virtual bool Write(wxOutputStream
& str
) const;
630 #endif // wxUSE_STREAMS
631 virtual wxString
GetType() const { return wxT("char"); };
637 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
639 void wxVariantDataChar::Copy(wxVariantData
& data
)
641 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
643 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
645 otherData
.m_value
= m_value
;
648 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
650 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
652 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
654 return (otherData
.m_value
== m_value
);
657 #if wxUSE_STD_IOSTREAM
658 bool wxVariantDataChar::Write(ostream
& str
) const
662 str
<< (const char*) s
.mb_str();
667 bool wxVariantDataChar::Write(wxString
& str
) const
669 str
.Printf(wxT("%c"), m_value
);
673 #if wxUSE_STD_IOSTREAM
674 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
676 wxFAIL_MSG(wxT("Unimplemented"));
683 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
685 wxTextOutputStream
s(str
);
691 bool wxVariantDataChar::Read(wxInputStream
& str
)
693 wxTextInputStream
s(str
);
698 #endif // wxUSE_STREAMS
700 bool wxVariantDataChar::Read(wxString
& str
)
702 m_value
= str
[(size_t)0];
707 * wxVariantDataString
710 #if defined(__BORLANDC__) && defined(__WIN16__)
711 // Change name because of truncation
712 #define wxVariantDataString wxVariantStringData
715 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
717 #if defined(__BORLANDC__) && defined(__WIN16__)
718 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
720 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
723 wxVariantDataString() { }
724 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
726 inline wxString
GetValue() const { return m_value
; }
727 inline void SetValue(const wxString
& value
) { m_value
= value
; }
729 virtual void Copy(wxVariantData
& data
);
730 virtual bool Eq(wxVariantData
& data
) const;
731 #if wxUSE_STD_IOSTREAM
732 virtual bool Write(ostream
& str
) const;
734 virtual bool Read(wxString
& str
);
735 virtual bool Write(wxString
& str
) const;
736 #if wxUSE_STD_IOSTREAM
737 virtual bool Read(istream
& str
);
740 virtual bool Read(wxInputStream
& str
);
741 virtual bool Write(wxOutputStream
& str
) const;
742 #endif // wxUSE_STREAMS
743 virtual wxString
GetType() const { return wxT("string"); };
749 void wxVariantDataString::Copy(wxVariantData
& data
)
751 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
753 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
755 otherData
.m_value
= m_value
;
758 bool wxVariantDataString::Eq(wxVariantData
& data
) const
760 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
762 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
764 return (otherData
.m_value
== m_value
);
767 #if wxUSE_STD_IOSTREAM
768 bool wxVariantDataString::Write(ostream
& str
) const
770 str
<< (const char*) m_value
.mb_str();
775 bool wxVariantDataString::Write(wxString
& str
) const
781 #if wxUSE_STD_IOSTREAM
782 bool wxVariantDataString::Read(istream
& str
)
790 bool wxVariantDataString::Write(wxOutputStream
& str
) const
792 // why doesn't wxOutputStream::operator<< take "const wxString&"
793 wxTextOutputStream
s(str
);
794 s
.WriteString(m_value
);
798 bool wxVariantDataString::Read(wxInputStream
& str
)
800 wxTextInputStream
s(str
);
802 m_value
= s
.ReadString();
805 #endif // wxUSE_STREAMS
807 bool wxVariantDataString::Read(wxString
& str
)
813 #if defined(__BORLANDC__) && defined(__WIN16__)
814 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
816 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
825 class wxVariantDataTime
: public wxVariantData
827 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
829 wxVariantDataTime() { }
830 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
832 inline wxTime
GetValue() const { return m_value
; }
833 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
835 virtual void Copy(wxVariantData
& data
);
836 virtual bool Eq(wxVariantData
& data
) const;
837 #if wxUSE_STD_IOSTREAM
838 virtual bool Write(ostream
& str
) const;
840 virtual bool Write(wxString
& str
) const;
841 #if wxUSE_STD_IOSTREAM
842 virtual bool Read(istream
& str
);
844 virtual bool Read(wxString
& str
);
845 virtual wxString
GetType() const { return wxT("time"); };
846 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
852 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
854 void wxVariantDataTime::Copy(wxVariantData
& data
)
856 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Copy: Can't copy to this type of data") );
858 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
860 otherData
.m_value
= m_value
;
863 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
865 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Eq: argument mismatch") );
867 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
869 return (otherData
.m_value
== m_value
);
872 #if wxUSE_STD_IOSTREAM
873 bool wxVariantDataTime::Write(ostream
& str
) const
877 str
<< (const char*) s
.mb_str();
882 bool wxVariantDataTime::Write(wxString
& str
) const
884 wxChar
*s
= m_value
.FormatTime();
889 #if wxUSE_STD_IOSTREAM
890 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
897 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
907 class wxVariantDataDate
: public wxVariantData
909 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
911 wxVariantDataDate() { }
912 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
914 inline wxDate
GetValue() const { return m_value
; }
915 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
917 virtual void Copy(wxVariantData
& data
);
918 virtual bool Eq(wxVariantData
& data
) const;
919 #if wxUSE_STD_IOSTREAM
920 virtual bool Write(ostream
& str
) const;
922 virtual bool Write(wxString
& str
) const;
923 #if wxUSE_STD_IOSTREAM
924 virtual bool Read(istream
& str
);
926 virtual bool Read(wxString
& str
);
927 virtual wxString
GetType() const { return wxT("date"); };
928 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
934 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
936 void wxVariantDataDate::Copy(wxVariantData
& data
)
938 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Copy: Can't copy to this type of data") );
940 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
942 otherData
.m_value
= m_value
;
945 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
947 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Eq: argument mismatch") );
949 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
951 return (otherData
.m_value
== m_value
);
954 #if wxUSE_STD_IOSTREAM
955 bool wxVariantDataDate::Write(ostream
& str
) const
959 str
<< (const char*) s
.mb_str();
964 bool wxVariantDataDate::Write(wxString
& str
) const
966 str
= m_value
.FormatDate();
970 #if wxUSE_STD_IOSTREAM
971 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
978 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
987 * wxVariantDataVoidPtr
990 class wxVariantDataVoidPtr
: public wxVariantData
992 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
994 wxVariantDataVoidPtr() { }
995 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
997 inline void* GetValue() const { return m_value
; }
998 inline void SetValue(void* value
) { m_value
= value
; }
1000 virtual void Copy(wxVariantData
& data
);
1001 virtual bool Eq(wxVariantData
& data
) const;
1002 #if wxUSE_STD_IOSTREAM
1003 virtual bool Write(ostream
& str
) const;
1005 virtual bool Write(wxString
& str
) const;
1006 #if wxUSE_STD_IOSTREAM
1007 virtual bool Read(istream
& str
);
1009 virtual bool Read(wxString
& str
);
1010 virtual wxString
GetType() const { return wxT("void*"); };
1011 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1017 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1019 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1021 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1023 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1025 otherData
.m_value
= m_value
;
1028 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1030 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1032 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1034 return (otherData
.m_value
== m_value
);
1037 #if wxUSE_STD_IOSTREAM
1038 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1042 str
<< (const char*) s
.mb_str();
1047 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1049 str
.Printf(wxT("%ld"), (long) m_value
);
1053 #if wxUSE_STD_IOSTREAM
1054 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1061 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1072 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1074 // Construction & destruction
1075 wxVariant::wxVariant()
1077 m_data
= (wxVariantData
*) NULL
;
1080 wxVariant::wxVariant(double val
, const wxString
& name
)
1082 m_data
= new wxVariantDataReal(val
);
1086 wxVariant::wxVariant(long val
, const wxString
& name
)
1088 m_data
= new wxVariantDataLong(val
);
1093 wxVariant::wxVariant(bool val
, const wxString
& name
)
1095 m_data
= new wxVariantDataBool(val
);
1100 wxVariant::wxVariant(char val
, const wxString
& name
)
1102 m_data
= new wxVariantDataChar(val
);
1106 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1108 m_data
= new wxVariantDataString(val
);
1112 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1114 m_data
= new wxVariantDataString(wxString(val
));
1118 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1120 m_data
= new wxVariantDataStringList(val
);
1124 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1126 m_data
= new wxVariantDataList(val
);
1131 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1133 m_data
= new wxVariantDataTime(val
);
1137 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1139 m_data
= new wxVariantDataDate(val
);
1144 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1146 m_data
= new wxVariantDataVoidPtr(val
);
1150 wxVariant::wxVariant(const wxVariant
& variant
)
1152 if (!variant
.IsNull())
1154 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1155 variant
.m_data
->Copy(*m_data
);
1158 m_data
= (wxVariantData
*) NULL
;
1159 m_name
= variant
.m_name
;
1162 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1168 wxVariant::~wxVariant()
1174 // Make NULL (i.e. delete the data)
1175 void wxVariant::MakeNull()
1181 // Generic operators
1183 void wxVariant::operator= (const wxVariant
& variant
)
1185 if (variant
.IsNull())
1191 if (IsNull() || (GetType() != variant
.GetType()))
1195 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1197 // GetData()->Copy(* variant.GetData());
1198 variant
.GetData()->Copy(* GetData());
1201 // Assignment using data, e.g.
1202 // myVariant = new wxStringVariantData("hello")
1203 void wxVariant::operator= (wxVariantData
* variantData
)
1206 m_data
= variantData
;
1209 bool wxVariant::operator== (const wxVariant
& variant
) const
1211 if (IsNull() || variant
.IsNull())
1212 return (IsNull() == variant
.IsNull());
1214 return (GetData()->Eq(* variant
.GetData()));
1217 bool wxVariant::operator!= (const wxVariant
& variant
) const
1219 return (!(*this == variant
));
1223 // Specific operators
1224 bool wxVariant::operator== (double value
) const
1227 if (!Convert(&thisValue
))
1230 return (value
== thisValue
);
1233 bool wxVariant::operator!= (double value
) const
1235 return (!((*this) == value
));
1238 void wxVariant::operator= (double value
)
1240 if (GetType() == wxT("double"))
1242 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1248 m_data
= new wxVariantDataReal(value
);
1252 bool wxVariant::operator== (long value
) const
1255 if (!Convert(&thisValue
))
1258 return (value
== thisValue
);
1261 bool wxVariant::operator!= (long value
) const
1263 return (!((*this) == value
));
1266 void wxVariant::operator= (long value
)
1268 if (GetType() == wxT("long"))
1270 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1276 m_data
= new wxVariantDataLong(value
);
1280 bool wxVariant::operator== (char value
) const
1283 if (!Convert(&thisValue
))
1286 return (value
== thisValue
);
1289 bool wxVariant::operator!= (char value
) const
1291 return (!((*this) == value
));
1294 void wxVariant::operator= (char value
)
1296 if (GetType() == wxT("char"))
1298 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1304 m_data
= new wxVariantDataChar(value
);
1309 bool wxVariant::operator== (bool value
) const
1312 if (!Convert(&thisValue
))
1315 return (value
== thisValue
);
1318 bool wxVariant::operator!= (bool value
) const
1320 return (!((*this) == value
));
1323 void wxVariant::operator= (bool value
)
1325 if (GetType() == wxT("bool"))
1327 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1333 m_data
= new wxVariantDataBool(value
);
1338 bool wxVariant::operator== (const wxString
& value
) const
1341 if (!Convert(&thisValue
))
1344 return (value
== thisValue
);
1347 bool wxVariant::operator!= (const wxString
& value
) const
1349 return (!((*this) == value
));
1352 void wxVariant::operator= (const wxString
& value
)
1354 if (GetType() == wxT("string"))
1356 ((wxVariantDataString
*)GetData())->SetValue(value
);
1362 m_data
= new wxVariantDataString(value
);
1366 void wxVariant::operator= (const wxChar
* value
)
1368 if (GetType() == wxT("string"))
1370 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1376 m_data
= new wxVariantDataString(wxString(value
));
1380 bool wxVariant::operator== (const wxStringList
& value
) const
1382 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1384 wxVariantDataStringList
other(value
);
1385 return (m_data
->Eq(other
));
1388 bool wxVariant::operator!= (const wxStringList
& value
) const
1390 return (!((*this) == value
));
1393 void wxVariant::operator= (const wxStringList
& value
)
1395 if (GetType() == wxT("stringlist"))
1397 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1403 m_data
= new wxVariantDataStringList(value
);
1407 bool wxVariant::operator== (const wxList
& value
) const
1409 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1411 wxVariantDataList
other(value
);
1412 return (m_data
->Eq(other
));
1415 bool wxVariant::operator!= (const wxList
& value
) const
1417 return (!((*this) == value
));
1420 void wxVariant::operator= (const wxList
& value
)
1422 if (GetType() == wxT("list"))
1424 ((wxVariantDataList
*)GetData())->SetValue(value
);
1430 m_data
= new wxVariantDataList(value
);
1435 bool wxVariant::operator== (const wxTime
& value
) const
1438 if (!Convert(&thisValue
))
1441 return (value
== thisValue
);
1444 bool wxVariant::operator!= (const wxTime
& value
) const
1446 return (!((*this) == value
));
1449 void wxVariant::operator= (const wxTime
& value
)
1451 if (GetType() == wxT("time"))
1453 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1459 m_data
= new wxVariantDataTime(value
);
1463 bool wxVariant::operator== (const wxDate
& value
) const
1466 if (!Convert(&thisValue
))
1469 return (value
== thisValue
);
1472 bool wxVariant::operator!= (const wxDate
& value
) const
1474 return (!((*this) == value
));
1477 void wxVariant::operator= (const wxDate
& value
)
1479 if (GetType() == wxT("date"))
1481 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1487 m_data
= new wxVariantDataDate(value
);
1492 bool wxVariant::operator== (void* value
) const
1494 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1497 bool wxVariant::operator!= (void* value
) const
1499 return (!((*this) == value
));
1502 void wxVariant::operator= (void* value
)
1504 if (GetType() == wxT("void*"))
1506 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1512 m_data
= new wxVariantDataVoidPtr(value
);
1516 // Treat a list variant as an array
1517 wxVariant
wxVariant::operator[] (size_t idx
) const
1519 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1521 if (GetType() == wxT("list"))
1523 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1524 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1525 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1527 else if (GetType() == wxT("stringlist"))
1529 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1530 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1532 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1535 return wxNullVariant
;
1538 wxVariant
& wxVariant::operator[] (size_t idx
)
1540 // We can't return a reference to a variant for a string list, since the string
1541 // is actually stored as a char*, not a variant.
1543 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1545 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1546 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1548 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1551 // Return the number of elements in a list
1552 int wxVariant::GetCount() const
1554 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1556 if (GetType() == wxT("list"))
1558 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1559 return data
->GetValue().Number();
1561 else if (GetType() == wxT("stringlist"))
1563 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1564 return data
->GetValue().Number();
1569 wxString
wxVariant::MakeString() const
1574 if (GetData()->Write(str
))
1577 return wxString(wxT(""));
1582 void wxVariant::SetData(wxVariantData
* data
)
1584 if (m_data
) delete m_data
;
1589 // Returns a string representing the type of the variant,
1590 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1591 wxString
wxVariant::GetType() const
1594 return wxString(wxT("null"));
1596 return m_data
->GetType();
1600 bool wxVariant::IsType(const wxString
& type
) const
1602 return (GetType() == type
);
1607 double wxVariant::GetReal() const
1610 if (Convert(& value
))
1614 wxFAIL_MSG(wxT("Could not convert to a real number"));
1619 long wxVariant::GetInteger() const
1622 if (Convert(& value
))
1626 wxFAIL_MSG(wxT("Could not convert to an integer"));
1631 char wxVariant::GetChar() const
1634 if (Convert(& value
))
1638 wxFAIL_MSG(wxT("Could not convert to a char"));
1643 bool wxVariant::GetBool() const
1646 if (Convert(& value
))
1650 wxFAIL_MSG(wxT("Could not convert to a bool"));
1655 wxString
wxVariant::GetString() const
1658 if (Convert(& value
))
1662 wxFAIL_MSG(wxT("Could not convert to a string"));
1663 return wxString("");
1668 wxTime
wxVariant::GetTime() const
1671 if (Convert(& value
))
1675 wxFAIL_MSG(wxT("Could not convert to a time"));
1680 wxDate
wxVariant::GetDate() const
1683 if (Convert(& value
))
1687 wxFAIL_MSG(wxT("Could not convert to a date"));
1693 void* wxVariant::GetVoidPtr() const
1695 wxASSERT( (GetType() == wxT("void*")) );
1697 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1700 wxList
& wxVariant::GetList() const
1702 wxASSERT( (GetType() == wxT("list")) );
1704 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1707 wxStringList
& wxVariant::GetStringList() const
1709 wxASSERT( (GetType() == wxT("stringlist")) );
1711 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1715 void wxVariant::Append(const wxVariant
& value
)
1717 wxList
& list
= GetList();
1719 list
.Append(new wxVariant(value
));
1722 // Insert at front of list
1723 void wxVariant::Insert(const wxVariant
& value
)
1725 wxList
& list
= GetList();
1727 list
.Insert(new wxVariant(value
));
1730 // Returns TRUE if the variant is a member of the list
1731 bool wxVariant::Member(const wxVariant
& value
) const
1733 wxList
& list
= GetList();
1735 wxNode
* node
= list
.First();
1738 wxVariant
* other
= (wxVariant
*) node
->Data();
1739 if (value
== *other
)
1741 node
= node
->Next();
1746 // Deletes the nth element of the list
1747 bool wxVariant::Delete(int item
)
1749 wxList
& list
= GetList();
1751 wxASSERT_MSG( (item
< list
.Number()), wxT("Invalid index to Delete") );
1752 wxNode
* node
= list
.Nth(item
);
1753 wxVariant
* variant
= (wxVariant
*) node
->Data();
1760 void wxVariant::ClearList()
1762 if (!IsNull() && (GetType() == wxT("list")))
1764 ((wxVariantDataList
*) m_data
)->Clear();
1768 if (GetType() != wxT("list"))
1773 m_data
= new wxVariantDataList
;
1778 bool wxVariant::Convert(long* value
) const
1780 wxString
type(GetType());
1781 if (type
== wxT("double"))
1782 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1783 else if (type
== wxT("long"))
1784 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1786 else if (type
== wxT("bool"))
1787 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1789 else if (type
== wxT("string"))
1790 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1797 bool wxVariant::Convert(bool* value
) const
1799 wxString
type(GetType());
1800 if (type
== wxT("double"))
1801 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1802 else if (type
== wxT("long"))
1803 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1805 else if (type
== wxT("bool"))
1806 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1808 else if (type
== wxT("string"))
1810 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1812 if (val
== wxT("true") || val
== wxT("yes"))
1814 else if (val
== wxT("false") || val
== wxT("no"))
1825 bool wxVariant::Convert(double* value
) const
1827 wxString
type(GetType());
1828 if (type
== wxT("double"))
1829 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1830 else if (type
== wxT("long"))
1831 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1833 else if (type
== wxT("bool"))
1834 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1836 else if (type
== wxT("string"))
1837 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1844 bool wxVariant::Convert(char* value
) const
1846 wxString
type(GetType());
1847 if (type
== wxT("char"))
1848 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1849 else if (type
== wxT("long"))
1850 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1852 else if (type
== wxT("bool"))
1853 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1861 bool wxVariant::Convert(wxString
* value
) const
1863 *value
= MakeString();
1868 bool wxVariant::Convert(wxTime
* value
) const
1870 wxString
type(GetType());
1871 if (type
== wxT("time"))
1872 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1873 else if (type
== wxT("date"))
1874 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1881 bool wxVariant::Convert(wxDate
* value
) const
1883 wxString
type(GetType());
1884 if (type
== wxT("date"))
1885 *value
= ((wxVariantDataDate
*)GetData())->GetValue();