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_IOSTREAMH && wxUSE_STD_IOSTREAM
30 #include "wx/stream.h"
31 #include "wx/txtstrm.h"
34 #include "wx/string.h"
35 #include "wx/variant.h"
37 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
39 wxVariant WXDLLEXPORT wxNullVariant
;
45 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
47 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
49 wxVariantDataList() {}
50 wxVariantDataList(const wxList
& list
);
53 wxList
& GetValue() const { return (wxList
&) m_value
; }
54 void SetValue(const wxList
& value
) ;
56 virtual void Copy(wxVariantData
& data
);
57 virtual bool Eq(wxVariantData
& data
) const;
58 #if wxUSE_STD_IOSTREAM
59 virtual bool Write(ostream
& str
) const;
61 virtual bool Write(wxString
& str
) const;
62 #if wxUSE_STD_IOSTREAM
63 virtual bool Read(istream
& str
);
65 virtual bool Read(wxString
& str
);
66 virtual wxString
GetType() const { return T("list"); };
74 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
76 wxVariantDataList::wxVariantDataList(const wxList
& list
)
81 wxVariantDataList::~wxVariantDataList()
86 void wxVariantDataList::SetValue(const wxList
& value
)
89 wxNode
* node
= value
.First();
92 wxVariant
* var
= (wxVariant
*) node
->Data();
93 m_value
.Append(new wxVariant(*var
));
98 void wxVariantDataList::Clear()
100 wxNode
* node
= m_value
.First();
103 wxVariant
* var
= (wxVariant
*) node
->Data();
110 void wxVariantDataList::Copy(wxVariantData
& data
)
112 wxASSERT_MSG( (data
.GetType() == T("list")), T("wxVariantDataList::Copy: Can't copy to this type of data") );
114 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
117 wxNode
* node
= m_value
.First();
120 wxVariant
* var
= (wxVariant
*) node
->Data();
121 listData
.m_value
.Append(new wxVariant(*var
));
126 bool wxVariantDataList::Eq(wxVariantData
& data
) const
128 wxASSERT_MSG( (data
.GetType() == T("list")), T("wxVariantDataList::Eq: argument mismatch") );
130 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
131 wxNode
* node1
= m_value
.First();
132 wxNode
* node2
= listData
.GetValue().First();
133 while (node1
&& node2
)
135 wxVariant
* var1
= (wxVariant
*) node1
->Data();
136 wxVariant
* var2
= (wxVariant
*) node2
->Data();
137 if ((*var1
) != (*var2
))
139 node1
= node1
->Next();
140 node2
= node2
->Next();
142 if (node1
|| node2
) return FALSE
;
146 #if wxUSE_STD_IOSTREAM
147 bool wxVariantDataList::Write(ostream
& str
) const
151 str
<< (const char*) s
.mb_str();
156 bool wxVariantDataList::Write(wxString
& str
) const
159 wxNode
* node
= m_value
.First();
162 wxVariant
* var
= (wxVariant
*) node
->Data();
163 if (node
!= m_value
.First())
166 str
+= var
->MakeString();
173 #if wxUSE_STD_IOSTREAM
174 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
176 wxFAIL_MSG(T("Unimplemented"));
182 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
184 wxFAIL_MSG(T("Unimplemented"));
190 * wxVariantDataStringList
193 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
195 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
197 wxVariantDataStringList() {}
198 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
200 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
201 void SetValue(const wxStringList
& value
);
203 virtual void Copy(wxVariantData
& data
);
204 virtual bool Eq(wxVariantData
& data
) const;
205 #if wxUSE_STD_IOSTREAM
206 virtual bool Write(ostream
& str
) const;
208 virtual bool Write(wxString
& str
) const;
209 #if wxUSE_STD_IOSTREAM
210 virtual bool Read(istream
& str
);
212 virtual bool Read(wxString
& str
);
213 virtual wxString
GetType() const { return T("stringlist"); };
216 wxStringList m_value
;
219 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
221 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
226 void wxVariantDataStringList::Copy(wxVariantData
& data
)
228 wxASSERT_MSG( (data
.GetType() == T("stringlist")), T("wxVariantDataStringList::Copy: Can't copy to this type of data") );
230 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
232 listData
.m_value
= m_value
;
235 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
237 wxASSERT_MSG( (data
.GetType() == T("stringlist")), T("wxVariantDataStringList::Eq: argument mismatch") );
239 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
240 wxNode
* node1
= m_value
.First();
241 wxNode
* node2
= listData
.GetValue().First();
242 while (node1
&& node2
)
244 wxString
str1 ((wxChar
*) node1
->Data());
245 wxString
str2 ((wxChar
*) node2
->Data());
248 node1
= node1
->Next();
249 node2
= node2
->Next();
251 if (node1
|| node2
) return FALSE
;
255 #if wxUSE_STD_IOSTREAM
256 bool wxVariantDataStringList::Write(ostream
& str
) const
260 str
<< (const char*) s
.mb_str();
265 bool wxVariantDataStringList::Write(wxString
& str
) const
268 wxNode
* node
= m_value
.First();
271 wxChar
* s
= (wxChar
*) node
->Data();
272 if (node
!= m_value
.First())
281 #if wxUSE_STD_IOSTREAM
282 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
284 wxFAIL_MSG(T("Unimplemented"));
290 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
292 wxFAIL_MSG(T("Unimplemented"));
301 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
303 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
305 wxVariantDataLong() { m_value
= 0; }
306 wxVariantDataLong(long value
) { m_value
= value
; }
308 inline long GetValue() const { return m_value
; }
309 inline void SetValue(long value
) { m_value
= value
; }
311 virtual void Copy(wxVariantData
& data
);
312 virtual bool Eq(wxVariantData
& data
) const;
314 virtual bool Read(wxString
& str
);
315 virtual bool Write(wxString
& str
) const;
316 #if wxUSE_STD_IOSTREAM
317 virtual bool Read(istream
& str
);
318 virtual bool Write(ostream
& str
) const;
321 virtual bool Read(wxInputStream
& str
);
322 virtual bool Write(wxOutputStream
&str
) const;
323 #endif // wxUSE_STREAMS
325 virtual wxString
GetType() const { return T("long"); };
331 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
333 void wxVariantDataLong::Copy(wxVariantData
& data
)
335 wxASSERT_MSG( (data
.GetType() == T("long")), T("wxVariantDataLong::Copy: Can't copy to this type of data") );
337 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
339 otherData
.m_value
= m_value
;
342 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
344 wxASSERT_MSG( (data
.GetType() == T("long")), T("wxVariantDataLong::Eq: argument mismatch") );
346 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
348 return (otherData
.m_value
== m_value
);
351 #if wxUSE_STD_IOSTREAM
352 bool wxVariantDataLong::Write(ostream
& str
) const
356 str
<< (const char*) s
.mb_str();
361 bool wxVariantDataLong::Write(wxString
& str
) const
363 str
.Printf(T("%ld"), m_value
);
367 #if wxUSE_STD_IOSTREAM
368 bool wxVariantDataLong::Read(istream
& str
)
376 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
378 wxTextOutputStream
s(str
);
384 bool wxVariantDataLong::Read(wxInputStream
& str
)
386 wxTextInputStream
s(str
);
387 m_value
= s
.Read32();
390 #endif // wxUSE_STREAMS
392 bool wxVariantDataLong::Read(wxString
& str
)
394 m_value
= wxAtol((const wxChar
*) str
);
402 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
404 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
406 wxVariantDataReal() { m_value
= 0.0; }
407 wxVariantDataReal(double value
) { m_value
= value
; }
409 inline double GetValue() const { return m_value
; }
410 inline void SetValue(double value
) { m_value
= value
; }
412 virtual void Copy(wxVariantData
& data
);
413 virtual bool Eq(wxVariantData
& data
) const;
414 virtual bool Read(wxString
& str
);
415 #if wxUSE_STD_IOSTREAM
416 virtual bool Write(ostream
& str
) const;
418 virtual bool Write(wxString
& str
) const;
419 #if wxUSE_STD_IOSTREAM
420 virtual bool Read(istream
& str
);
423 virtual bool Read(wxInputStream
& str
);
424 virtual bool Write(wxOutputStream
&str
) const;
425 #endif // wxUSE_STREAMS
426 virtual wxString
GetType() const { return T("double"); };
432 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
434 void wxVariantDataReal::Copy(wxVariantData
& data
)
436 wxASSERT_MSG( (data
.GetType() == T("double")), T("wxVariantDataReal::Copy: Can't copy to this type of data") );
438 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
440 otherData
.m_value
= m_value
;
443 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
445 wxASSERT_MSG( (data
.GetType() == T("double")), T("wxVariantDataReal::Eq: argument mismatch") );
447 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
449 return (otherData
.m_value
== m_value
);
452 #if wxUSE_STD_IOSTREAM
453 bool wxVariantDataReal::Write(ostream
& str
) const
457 str
<< (const char*) s
.mb_str();
462 bool wxVariantDataReal::Write(wxString
& str
) const
464 str
.Printf(T("%.4f"), m_value
);
468 #if wxUSE_STD_IOSTREAM
469 bool wxVariantDataReal::Read(istream
& str
)
477 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
479 wxTextOutputStream
s(str
);
480 s
.WriteDouble((double)m_value
);
484 bool wxVariantDataReal::Read(wxInputStream
& str
)
486 wxTextInputStream
s(str
);
487 m_value
= (float)s
.ReadDouble();
490 #endif // wxUSE_STREAMS
492 bool wxVariantDataReal::Read(wxString
& str
)
494 m_value
= wxAtof((const wxChar
*) str
);
503 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
505 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
507 wxVariantDataBool() { m_value
= 0; }
508 wxVariantDataBool(bool value
) { m_value
= value
; }
510 inline bool GetValue() const { return m_value
; }
511 inline void SetValue(bool value
) { m_value
= value
; }
513 virtual void Copy(wxVariantData
& data
);
514 virtual bool Eq(wxVariantData
& data
) const;
515 #if wxUSE_STD_IOSTREAM
516 virtual bool Write(ostream
& str
) const;
518 virtual bool Write(wxString
& str
) const;
519 virtual bool Read(wxString
& str
);
520 #if wxUSE_STD_IOSTREAM
521 virtual bool Read(istream
& str
);
524 virtual bool Read(wxInputStream
& str
);
525 virtual bool Write(wxOutputStream
& str
) const;
526 #endif // wxUSE_STREAMS
527 virtual wxString
GetType() const { return T("bool"); };
533 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
535 void wxVariantDataBool::Copy(wxVariantData
& data
)
537 wxASSERT_MSG( (data
.GetType() == T("bool")), T("wxVariantDataBool::Copy: Can't copy to this type of data") );
539 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
541 otherData
.m_value
= m_value
;
544 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
546 wxASSERT_MSG( (data
.GetType() == T("bool")), T("wxVariantDataBool::Eq: argument mismatch") );
548 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
550 return (otherData
.m_value
== m_value
);
553 #if wxUSE_STD_IOSTREAM
554 bool wxVariantDataBool::Write(ostream
& str
) const
558 str
<< (const char*) s
.mb_str();
563 bool wxVariantDataBool::Write(wxString
& str
) const
565 str
.Printf(T("%d"), (int) m_value
);
569 #if wxUSE_STD_IOSTREAM
570 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
572 wxFAIL_MSG(T("Unimplemented"));
573 // str >> (long) m_value;
579 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
581 wxTextOutputStream
s(str
);
587 bool wxVariantDataBool::Read(wxInputStream
& str
)
589 wxTextInputStream
s(str
);
591 m_value
= s
.Read8() != 0;
594 #endif // wxUSE_STREAMS
596 bool wxVariantDataBool::Read(wxString
& str
)
598 m_value
= (wxAtol((const wxChar
*) str
) != 0);
607 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
609 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
611 wxVariantDataChar() { m_value
= 0; }
612 wxVariantDataChar(char value
) { m_value
= value
; }
614 inline char GetValue() const { return m_value
; }
615 inline void SetValue(char value
) { m_value
= value
; }
617 virtual void Copy(wxVariantData
& data
);
618 virtual bool Eq(wxVariantData
& data
) const;
619 #if wxUSE_STD_IOSTREAM
620 virtual bool Read(istream
& str
);
621 virtual bool Write(ostream
& str
) const;
623 virtual bool Read(wxString
& str
);
624 virtual bool Write(wxString
& str
) const;
626 virtual bool Read(wxInputStream
& str
);
627 virtual bool Write(wxOutputStream
& str
) const;
628 #endif // wxUSE_STREAMS
629 virtual wxString
GetType() const { return T("char"); };
635 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
637 void wxVariantDataChar::Copy(wxVariantData
& data
)
639 wxASSERT_MSG( (data
.GetType() == T("char")), T("wxVariantDataChar::Copy: Can't copy to this type of data") );
641 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
643 otherData
.m_value
= m_value
;
646 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
648 wxASSERT_MSG( (data
.GetType() == T("char")), T("wxVariantDataChar::Eq: argument mismatch") );
650 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
652 return (otherData
.m_value
== m_value
);
655 #if wxUSE_STD_IOSTREAM
656 bool wxVariantDataChar::Write(ostream
& str
) const
660 str
<< (const char*) s
.mb_str();
665 bool wxVariantDataChar::Write(wxString
& str
) const
667 str
.Printf(T("%c"), m_value
);
671 #if wxUSE_STD_IOSTREAM
672 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
674 wxFAIL_MSG(T("Unimplemented"));
681 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
683 wxTextOutputStream
s(str
);
689 bool wxVariantDataChar::Read(wxInputStream
& str
)
691 wxTextInputStream
s(str
);
696 #endif // wxUSE_STREAMS
698 bool wxVariantDataChar::Read(wxString
& str
)
700 m_value
= str
[(size_t)0];
705 * wxVariantDataString
708 #if defined(__BORLANDC__) && defined(__WIN16__)
709 // Change name because of truncation
710 #define wxVariantDataString wxVariantStringData
713 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
715 #if defined(__BORLANDC__) && defined(__WIN16__)
716 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
718 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
721 wxVariantDataString() { }
722 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
724 inline wxString
GetValue() const { return m_value
; }
725 inline void SetValue(const wxString
& value
) { m_value
= value
; }
727 virtual void Copy(wxVariantData
& data
);
728 virtual bool Eq(wxVariantData
& data
) const;
729 #if wxUSE_STD_IOSTREAM
730 virtual bool Write(ostream
& str
) const;
732 virtual bool Read(wxString
& str
);
733 virtual bool Write(wxString
& str
) const;
734 #if wxUSE_STD_IOSTREAM
735 virtual bool Read(istream
& str
);
738 virtual bool Read(wxInputStream
& str
);
739 virtual bool Write(wxOutputStream
& str
) const;
740 #endif // wxUSE_STREAMS
741 virtual wxString
GetType() const { return T("string"); };
747 void wxVariantDataString::Copy(wxVariantData
& data
)
749 wxASSERT_MSG( (data
.GetType() == T("string")), T("wxVariantDataString::Copy: Can't copy to this type of data") );
751 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
753 otherData
.m_value
= m_value
;
756 bool wxVariantDataString::Eq(wxVariantData
& data
) const
758 wxASSERT_MSG( (data
.GetType() == T("string")), T("wxVariantDataString::Eq: argument mismatch") );
760 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
762 return (otherData
.m_value
== m_value
);
765 #if wxUSE_STD_IOSTREAM
766 bool wxVariantDataString::Write(ostream
& str
) const
768 str
<< (const char*) m_value
.mb_str();
773 bool wxVariantDataString::Write(wxString
& str
) const
779 #if wxUSE_STD_IOSTREAM
780 bool wxVariantDataString::Read(istream
& str
)
788 bool wxVariantDataString::Write(wxOutputStream
& str
) const
790 // why doesn't wxOutputStream::operator<< take "const wxString&"
791 wxTextOutputStream
s(str
);
792 s
.WriteString(m_value
);
796 bool wxVariantDataString::Read(wxInputStream
& str
)
798 wxTextInputStream
s(str
);
800 m_value
= s
.ReadString();
803 #endif // wxUSE_STREAMS
805 bool wxVariantDataString::Read(wxString
& str
)
811 #if defined(__BORLANDC__) && defined(__WIN16__)
812 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
814 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
823 class wxVariantDataTime
: public wxVariantData
825 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
827 wxVariantDataTime() { }
828 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
830 inline wxTime
GetValue() const { return m_value
; }
831 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
833 virtual void Copy(wxVariantData
& data
);
834 virtual bool Eq(wxVariantData
& data
) const;
835 #if wxUSE_STD_IOSTREAM
836 virtual bool Write(ostream
& str
) const;
838 virtual bool Write(wxString
& str
) const;
839 #if wxUSE_STD_IOSTREAM
840 virtual bool Read(istream
& str
);
842 virtual bool Read(wxString
& str
);
843 virtual wxString
GetType() const { return T("time"); };
844 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
850 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
852 void wxVariantDataTime::Copy(wxVariantData
& data
)
854 wxASSERT_MSG( (data
.GetType() == T("time")), T("wxVariantDataTime::Copy: Can't copy to this type of data") );
856 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
858 otherData
.m_value
= m_value
;
861 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
863 wxASSERT_MSG( (data
.GetType() == T("time")), T("wxVariantDataTime::Eq: argument mismatch") );
865 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
867 return (otherData
.m_value
== m_value
);
870 #if wxUSE_STD_IOSTREAM
871 bool wxVariantDataTime::Write(ostream
& str
) const
875 str
<< (const char*) s
.mb_str();
880 bool wxVariantDataTime::Write(wxString
& str
) const
882 wxChar
*s
= m_value
.FormatTime();
887 #if wxUSE_STD_IOSTREAM
888 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
895 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
905 class wxVariantDataDate
: public wxVariantData
907 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
909 wxVariantDataDate() { }
910 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
912 inline wxDate
GetValue() const { return m_value
; }
913 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
915 virtual void Copy(wxVariantData
& data
);
916 virtual bool Eq(wxVariantData
& data
) const;
917 #if wxUSE_STD_IOSTREAM
918 virtual bool Write(ostream
& str
) const;
920 virtual bool Write(wxString
& str
) const;
921 #if wxUSE_STD_IOSTREAM
922 virtual bool Read(istream
& str
);
924 virtual bool Read(wxString
& str
);
925 virtual wxString
GetType() const { return T("date"); };
926 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
932 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
934 void wxVariantDataDate::Copy(wxVariantData
& data
)
936 wxASSERT_MSG( (data
.GetType() == T("date")), T("wxVariantDataDate::Copy: Can't copy to this type of data") );
938 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
940 otherData
.m_value
= m_value
;
943 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
945 wxASSERT_MSG( (data
.GetType() == T("date")), T("wxVariantDataDate::Eq: argument mismatch") );
947 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
949 return (otherData
.m_value
== m_value
);
952 #if wxUSE_STD_IOSTREAM
953 bool wxVariantDataDate::Write(ostream
& str
) const
957 str
<< (const char*) s
.mb_str();
962 bool wxVariantDataDate::Write(wxString
& str
) const
964 str
= m_value
.FormatDate();
968 #if wxUSE_STD_IOSTREAM
969 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
976 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
985 * wxVariantDataVoidPtr
988 class wxVariantDataVoidPtr
: public wxVariantData
990 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
992 wxVariantDataVoidPtr() { }
993 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
995 inline void* GetValue() const { return m_value
; }
996 inline void SetValue(void* value
) { m_value
= value
; }
998 virtual void Copy(wxVariantData
& data
);
999 virtual bool Eq(wxVariantData
& data
) const;
1000 #if wxUSE_STD_IOSTREAM
1001 virtual bool Write(ostream
& str
) const;
1003 virtual bool Write(wxString
& str
) const;
1004 #if wxUSE_STD_IOSTREAM
1005 virtual bool Read(istream
& str
);
1007 virtual bool Read(wxString
& str
);
1008 virtual wxString
GetType() const { return T("void*"); };
1009 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1015 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1017 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1019 wxASSERT_MSG( (data
.GetType() == T("void*")), T("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1021 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1023 otherData
.m_value
= m_value
;
1026 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1028 wxASSERT_MSG( (data
.GetType() == T("void*")), T("wxVariantDataVoidPtr::Eq: argument mismatch") );
1030 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1032 return (otherData
.m_value
== m_value
);
1035 #if wxUSE_STD_IOSTREAM
1036 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1040 str
<< (const char*) s
.mb_str();
1045 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1047 str
.Printf(T("%ld"), (long) m_value
);
1051 #if wxUSE_STD_IOSTREAM
1052 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1059 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1070 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1072 // Construction & destruction
1073 wxVariant::wxVariant()
1075 m_data
= (wxVariantData
*) NULL
;
1078 wxVariant::wxVariant(double val
, const wxString
& name
)
1080 m_data
= new wxVariantDataReal(val
);
1084 wxVariant::wxVariant(long val
, const wxString
& name
)
1086 m_data
= new wxVariantDataLong(val
);
1091 wxVariant::wxVariant(bool val
, const wxString
& name
)
1093 m_data
= new wxVariantDataBool(val
);
1098 wxVariant::wxVariant(char val
, const wxString
& name
)
1100 m_data
= new wxVariantDataChar(val
);
1104 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1106 m_data
= new wxVariantDataString(val
);
1110 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1112 m_data
= new wxVariantDataString(wxString(val
));
1116 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1118 m_data
= new wxVariantDataStringList(val
);
1122 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1124 m_data
= new wxVariantDataList(val
);
1129 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1131 m_data
= new wxVariantDataTime(val
);
1135 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1137 m_data
= new wxVariantDataDate(val
);
1142 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1144 m_data
= new wxVariantDataVoidPtr(val
);
1148 wxVariant::wxVariant(const wxVariant
& variant
)
1150 if (!variant
.IsNull())
1152 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1153 variant
.m_data
->Copy(*m_data
);
1156 m_data
= (wxVariantData
*) NULL
;
1157 m_name
= variant
.m_name
;
1160 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1166 wxVariant::~wxVariant()
1172 // Make NULL (i.e. delete the data)
1173 void wxVariant::MakeNull()
1179 // Generic operators
1181 void wxVariant::operator= (const wxVariant
& variant
)
1183 if (variant
.IsNull())
1189 if (IsNull() || (GetType() != variant
.GetType()))
1193 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1195 // GetData()->Copy(* variant.GetData());
1196 variant
.GetData()->Copy(* GetData());
1199 // Assignment using data, e.g.
1200 // myVariant = new wxStringVariantData("hello")
1201 void wxVariant::operator= (wxVariantData
* variantData
)
1204 m_data
= variantData
;
1207 bool wxVariant::operator== (const wxVariant
& variant
) const
1209 if (IsNull() || variant
.IsNull())
1210 return (IsNull() == variant
.IsNull());
1212 return (GetData()->Eq(* variant
.GetData()));
1215 bool wxVariant::operator!= (const wxVariant
& variant
) const
1217 return (!(*this == variant
));
1221 // Specific operators
1222 bool wxVariant::operator== (double value
) const
1225 if (!Convert(&thisValue
))
1228 return (value
== thisValue
);
1231 bool wxVariant::operator!= (double value
) const
1233 return (!((*this) == value
));
1236 void wxVariant::operator= (double value
)
1238 if (GetType() == T("double"))
1240 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1246 m_data
= new wxVariantDataReal(value
);
1250 bool wxVariant::operator== (long value
) const
1253 if (!Convert(&thisValue
))
1256 return (value
== thisValue
);
1259 bool wxVariant::operator!= (long value
) const
1261 return (!((*this) == value
));
1264 void wxVariant::operator= (long value
)
1266 if (GetType() == T("long"))
1268 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1274 m_data
= new wxVariantDataLong(value
);
1278 bool wxVariant::operator== (char value
) const
1281 if (!Convert(&thisValue
))
1284 return (value
== thisValue
);
1287 bool wxVariant::operator!= (char value
) const
1289 return (!((*this) == value
));
1292 void wxVariant::operator= (char value
)
1294 if (GetType() == T("char"))
1296 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1302 m_data
= new wxVariantDataChar(value
);
1307 bool wxVariant::operator== (bool value
) const
1310 if (!Convert(&thisValue
))
1313 return (value
== thisValue
);
1316 bool wxVariant::operator!= (bool value
) const
1318 return (!((*this) == value
));
1321 void wxVariant::operator= (bool value
)
1323 if (GetType() == T("bool"))
1325 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1331 m_data
= new wxVariantDataBool(value
);
1336 bool wxVariant::operator== (const wxString
& value
) const
1339 if (!Convert(&thisValue
))
1342 return (value
== thisValue
);
1345 bool wxVariant::operator!= (const wxString
& value
) const
1347 return (!((*this) == value
));
1350 void wxVariant::operator= (const wxString
& value
)
1352 if (GetType() == T("string"))
1354 ((wxVariantDataString
*)GetData())->SetValue(value
);
1360 m_data
= new wxVariantDataString(value
);
1364 void wxVariant::operator= (const wxChar
* value
)
1366 if (GetType() == T("string"))
1368 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1374 m_data
= new wxVariantDataString(wxString(value
));
1378 bool wxVariant::operator== (const wxStringList
& value
) const
1380 wxASSERT_MSG( (GetType() == T("stringlist")), T("Invalid type for == operator") );
1382 wxVariantDataStringList
other(value
);
1383 return (m_data
->Eq(other
));
1386 bool wxVariant::operator!= (const wxStringList
& value
) const
1388 return (!((*this) == value
));
1391 void wxVariant::operator= (const wxStringList
& value
)
1393 if (GetType() == T("stringlist"))
1395 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1401 m_data
= new wxVariantDataStringList(value
);
1405 bool wxVariant::operator== (const wxList
& value
) const
1407 wxASSERT_MSG( (GetType() == T("list")), T("Invalid type for == operator") );
1409 wxVariantDataList
other(value
);
1410 return (m_data
->Eq(other
));
1413 bool wxVariant::operator!= (const wxList
& value
) const
1415 return (!((*this) == value
));
1418 void wxVariant::operator= (const wxList
& value
)
1420 if (GetType() == T("list"))
1422 ((wxVariantDataList
*)GetData())->SetValue(value
);
1428 m_data
= new wxVariantDataList(value
);
1433 bool wxVariant::operator== (const wxTime
& value
) const
1436 if (!Convert(&thisValue
))
1439 return (value
== thisValue
);
1442 bool wxVariant::operator!= (const wxTime
& value
) const
1444 return (!((*this) == value
));
1447 void wxVariant::operator= (const wxTime
& value
)
1449 if (GetType() == T("time"))
1451 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1457 m_data
= new wxVariantDataTime(value
);
1461 bool wxVariant::operator== (const wxDate
& value
) const
1464 if (!Convert(&thisValue
))
1467 return (value
== thisValue
);
1470 bool wxVariant::operator!= (const wxDate
& value
) const
1472 return (!((*this) == value
));
1475 void wxVariant::operator= (const wxDate
& value
)
1477 if (GetType() == T("date"))
1479 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1485 m_data
= new wxVariantDataDate(value
);
1490 bool wxVariant::operator== (void* value
) const
1492 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1495 bool wxVariant::operator!= (void* value
) const
1497 return (!((*this) == value
));
1500 void wxVariant::operator= (void* value
)
1502 if (GetType() == T("void*"))
1504 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1510 m_data
= new wxVariantDataVoidPtr(value
);
1514 // Treat a list variant as an array
1515 wxVariant
wxVariant::operator[] (size_t idx
) const
1517 wxASSERT_MSG( (GetType() == T("list") || GetType() == T("stringlist")), T("Invalid type for array operator") );
1519 if (GetType() == T("list"))
1521 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1522 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), T("Invalid index for array") );
1523 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1525 else if (GetType() == T("stringlist"))
1527 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1528 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), T("Invalid index for array") );
1530 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1533 return wxNullVariant
;
1536 wxVariant
& wxVariant::operator[] (size_t idx
)
1538 // We can't return a reference to a variant for a string list, since the string
1539 // is actually stored as a char*, not a variant.
1541 wxASSERT_MSG( (GetType() == T("list")), T("Invalid type for array operator") );
1543 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1544 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), T("Invalid index for array") );
1546 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1549 // Return the number of elements in a list
1550 int wxVariant::GetCount() const
1552 wxASSERT_MSG( (GetType() == T("list") || GetType() == T("stringlist")), T("Invalid type for GetCount()") );
1554 if (GetType() == T("list"))
1556 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1557 return data
->GetValue().Number();
1559 else if (GetType() == T("stringlist"))
1561 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1562 return data
->GetValue().Number();
1567 wxString
wxVariant::MakeString() const
1572 if (GetData()->Write(str
))
1575 return wxString(T(""));
1580 void wxVariant::SetData(wxVariantData
* data
)
1582 if (m_data
) delete m_data
;
1587 // Returns a string representing the type of the variant,
1588 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1589 wxString
wxVariant::GetType() const
1592 return wxString(T("null"));
1594 return m_data
->GetType();
1598 bool wxVariant::IsType(const wxString
& type
) const
1600 return (GetType() == type
);
1605 double wxVariant::GetReal() const
1608 if (Convert(& value
))
1612 wxFAIL_MSG(T("Could not convert to a real number"));
1617 long wxVariant::GetInteger() const
1620 if (Convert(& value
))
1624 wxFAIL_MSG(T("Could not convert to an integer"));
1629 char wxVariant::GetChar() const
1632 if (Convert(& value
))
1636 wxFAIL_MSG(T("Could not convert to a char"));
1641 bool wxVariant::GetBool() const
1644 if (Convert(& value
))
1648 wxFAIL_MSG(T("Could not convert to a bool"));
1653 wxString
wxVariant::GetString() const
1656 if (Convert(& value
))
1660 wxFAIL_MSG(T("Could not convert to a string"));
1661 return wxString("");
1666 wxTime
wxVariant::GetTime() const
1669 if (Convert(& value
))
1673 wxFAIL_MSG(T("Could not convert to a time"));
1678 wxDate
wxVariant::GetDate() const
1681 if (Convert(& value
))
1685 wxFAIL_MSG(T("Could not convert to a date"));
1691 void* wxVariant::GetVoidPtr() const
1693 wxASSERT( (GetType() == T("void*")) );
1695 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1698 wxList
& wxVariant::GetList() const
1700 wxASSERT( (GetType() == T("list")) );
1702 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1705 wxStringList
& wxVariant::GetStringList() const
1707 wxASSERT( (GetType() == T("stringlist")) );
1709 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1713 void wxVariant::Append(const wxVariant
& value
)
1715 wxList
& list
= GetList();
1717 list
.Append(new wxVariant(value
));
1720 // Insert at front of list
1721 void wxVariant::Insert(const wxVariant
& value
)
1723 wxList
& list
= GetList();
1725 list
.Insert(new wxVariant(value
));
1728 // Returns TRUE if the variant is a member of the list
1729 bool wxVariant::Member(const wxVariant
& value
) const
1731 wxList
& list
= GetList();
1733 wxNode
* node
= list
.First();
1736 wxVariant
* other
= (wxVariant
*) node
->Data();
1737 if (value
== *other
)
1739 node
= node
->Next();
1744 // Deletes the nth element of the list
1745 bool wxVariant::Delete(int item
)
1747 wxList
& list
= GetList();
1749 wxASSERT_MSG( (item
< list
.Number()), T("Invalid index to Delete") );
1750 wxNode
* node
= list
.Nth(item
);
1751 wxVariant
* variant
= (wxVariant
*) node
->Data();
1758 void wxVariant::ClearList()
1760 if (!IsNull() && (GetType() == T("list")))
1762 ((wxVariantDataList
*) m_data
)->Clear();
1766 if (GetType() != T("list"))
1771 m_data
= new wxVariantDataList
;
1776 bool wxVariant::Convert(long* value
) const
1778 wxString
type(GetType());
1779 if (type
== T("double"))
1780 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1781 else if (type
== T("long"))
1782 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1784 else if (type
== T("bool"))
1785 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1787 else if (type
== T("string"))
1788 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1795 bool wxVariant::Convert(bool* value
) const
1797 wxString
type(GetType());
1798 if (type
== T("double"))
1799 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1800 else if (type
== T("long"))
1801 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1803 else if (type
== T("bool"))
1804 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1806 else if (type
== T("string"))
1808 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1810 if (val
== T("true") || val
== T("yes"))
1812 else if (val
== T("false") || val
== T("no"))
1823 bool wxVariant::Convert(double* value
) const
1825 wxString
type(GetType());
1826 if (type
== T("double"))
1827 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1828 else if (type
== T("long"))
1829 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1831 else if (type
== T("bool"))
1832 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1834 else if (type
== T("string"))
1835 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1842 bool wxVariant::Convert(char* value
) const
1844 wxString
type(GetType());
1845 if (type
== T("char"))
1846 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1847 else if (type
== T("long"))
1848 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1850 else if (type
== T("bool"))
1851 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1859 bool wxVariant::Convert(wxString
* value
) const
1861 *value
= MakeString();
1866 bool wxVariant::Convert(wxTime
* value
) const
1868 wxString
type(GetType());
1869 if (type
== T("time"))
1870 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1871 else if (type
== T("date"))
1872 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1879 bool wxVariant::Convert(wxDate
* value
) const
1881 wxString
type(GetType());
1882 if (type
== T("date"))
1883 *value
= ((wxVariantDataDate
*)GetData())->GetValue();