1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "variant.h"
14 #pragma implementation "time.h"
15 #pragma implementation "date.h"
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
25 #if wxUSE_STD_IOSTREAM
34 #include "wx/stream.h"
35 #include "wx/txtstrm.h"
38 #include "wx/string.h"
39 #include "wx/variant.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
42 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
44 wxTime::tFormat
wxTime::ms_Format
= wxTime::wx12h
;
45 wxTime::tPrecision
wxTime::ms_Precision
= wxTime::wxStdMinSec
;
47 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
49 wxVariant WXDLLEXPORT wxNullVariant
;
55 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
57 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
59 wxVariantDataList() {}
60 wxVariantDataList(const wxList
& list
);
63 wxList
& GetValue() const { return (wxList
&) m_value
; }
64 void SetValue(const wxList
& value
) ;
66 virtual void Copy(wxVariantData
& data
);
67 virtual bool Eq(wxVariantData
& data
) const;
68 #if wxUSE_STD_IOSTREAM
69 virtual bool Write(ostream
& str
) const;
71 virtual bool Write(wxString
& str
) const;
72 #if wxUSE_STD_IOSTREAM
73 virtual bool Read(istream
& str
);
75 virtual bool Read(wxString
& str
);
76 virtual wxString
GetType() const { return wxT("list"); };
84 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
86 wxVariantDataList::wxVariantDataList(const wxList
& list
)
91 wxVariantDataList::~wxVariantDataList()
96 void wxVariantDataList::SetValue(const wxList
& value
)
99 wxNode
* node
= value
.First();
102 wxVariant
* var
= (wxVariant
*) node
->Data();
103 m_value
.Append(new wxVariant(*var
));
108 void wxVariantDataList::Clear()
110 wxNode
* node
= m_value
.First();
113 wxVariant
* var
= (wxVariant
*) node
->Data();
120 void wxVariantDataList::Copy(wxVariantData
& data
)
122 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
124 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
127 wxNode
* node
= m_value
.First();
130 wxVariant
* var
= (wxVariant
*) node
->Data();
131 listData
.m_value
.Append(new wxVariant(*var
));
136 bool wxVariantDataList::Eq(wxVariantData
& data
) const
138 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
140 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
141 wxNode
* node1
= m_value
.First();
142 wxNode
* node2
= listData
.GetValue().First();
143 while (node1
&& node2
)
145 wxVariant
* var1
= (wxVariant
*) node1
->Data();
146 wxVariant
* var2
= (wxVariant
*) node2
->Data();
147 if ((*var1
) != (*var2
))
149 node1
= node1
->Next();
150 node2
= node2
->Next();
152 if (node1
|| node2
) return FALSE
;
156 #if wxUSE_STD_IOSTREAM
157 bool wxVariantDataList::Write(ostream
& str
) const
161 str
<< (const char*) s
.mb_str();
166 bool wxVariantDataList::Write(wxString
& str
) const
169 wxNode
* node
= m_value
.First();
172 wxVariant
* var
= (wxVariant
*) node
->Data();
173 if (node
!= m_value
.First())
176 str
+= var
->MakeString();
183 #if wxUSE_STD_IOSTREAM
184 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
186 wxFAIL_MSG(wxT("Unimplemented"));
192 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
194 wxFAIL_MSG(wxT("Unimplemented"));
200 * wxVariantDataStringList
203 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
205 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
207 wxVariantDataStringList() {}
208 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
210 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
211 void SetValue(const wxStringList
& value
);
213 virtual void Copy(wxVariantData
& data
);
214 virtual bool Eq(wxVariantData
& data
) const;
215 #if wxUSE_STD_IOSTREAM
216 virtual bool Write(ostream
& str
) const;
218 virtual bool Write(wxString
& str
) const;
219 #if wxUSE_STD_IOSTREAM
220 virtual bool Read(istream
& str
);
222 virtual bool Read(wxString
& str
);
223 virtual wxString
GetType() const { return wxT("stringlist"); };
226 wxStringList m_value
;
229 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
231 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
236 void wxVariantDataStringList::Copy(wxVariantData
& data
)
238 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
240 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
242 listData
.m_value
= m_value
;
245 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
247 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
249 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
250 wxNode
* node1
= m_value
.First();
251 wxNode
* node2
= listData
.GetValue().First();
252 while (node1
&& node2
)
254 wxString
str1 ((wxChar
*) node1
->Data());
255 wxString
str2 ((wxChar
*) node2
->Data());
258 node1
= node1
->Next();
259 node2
= node2
->Next();
261 if (node1
|| node2
) return FALSE
;
265 #if wxUSE_STD_IOSTREAM
266 bool wxVariantDataStringList::Write(ostream
& str
) const
270 str
<< (const char*) s
.mb_str();
275 bool wxVariantDataStringList::Write(wxString
& str
) const
278 wxNode
* node
= m_value
.First();
281 wxChar
* s
= (wxChar
*) node
->Data();
282 if (node
!= m_value
.First())
291 #if wxUSE_STD_IOSTREAM
292 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
294 wxFAIL_MSG(wxT("Unimplemented"));
300 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
302 wxFAIL_MSG(wxT("Unimplemented"));
311 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
313 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
315 wxVariantDataLong() { m_value
= 0; }
316 wxVariantDataLong(long value
) { m_value
= value
; }
318 inline long GetValue() const { return m_value
; }
319 inline void SetValue(long value
) { m_value
= value
; }
321 virtual void Copy(wxVariantData
& data
);
322 virtual bool Eq(wxVariantData
& data
) const;
324 virtual bool Read(wxString
& str
);
325 virtual bool Write(wxString
& str
) const;
326 #if wxUSE_STD_IOSTREAM
327 virtual bool Read(istream
& str
);
328 virtual bool Write(ostream
& str
) const;
331 virtual bool Read(wxInputStream
& str
);
332 virtual bool Write(wxOutputStream
&str
) const;
333 #endif // wxUSE_STREAMS
335 virtual wxString
GetType() const { return wxT("long"); };
341 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
343 void wxVariantDataLong::Copy(wxVariantData
& data
)
345 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
347 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
349 otherData
.m_value
= m_value
;
352 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
354 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
356 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
358 return (otherData
.m_value
== m_value
);
361 #if wxUSE_STD_IOSTREAM
362 bool wxVariantDataLong::Write(ostream
& str
) const
366 str
<< (const char*) s
.mb_str();
371 bool wxVariantDataLong::Write(wxString
& str
) const
373 str
.Printf(wxT("%ld"), m_value
);
377 #if wxUSE_STD_IOSTREAM
378 bool wxVariantDataLong::Read(istream
& str
)
386 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
388 wxTextOutputStream
s(str
);
390 s
.Write32((size_t)m_value
);
394 bool wxVariantDataLong::Read(wxInputStream
& str
)
396 wxTextInputStream
s(str
);
397 m_value
= s
.Read32();
400 #endif // wxUSE_STREAMS
402 bool wxVariantDataLong::Read(wxString
& str
)
404 m_value
= wxAtol((const wxChar
*) str
);
412 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
414 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
416 wxVariantDataReal() { m_value
= 0.0; }
417 wxVariantDataReal(double value
) { m_value
= value
; }
419 inline double GetValue() const { return m_value
; }
420 inline void SetValue(double value
) { m_value
= value
; }
422 virtual void Copy(wxVariantData
& data
);
423 virtual bool Eq(wxVariantData
& data
) const;
424 virtual bool Read(wxString
& str
);
425 #if wxUSE_STD_IOSTREAM
426 virtual bool Write(ostream
& str
) const;
428 virtual bool Write(wxString
& str
) const;
429 #if wxUSE_STD_IOSTREAM
430 virtual bool Read(istream
& str
);
433 virtual bool Read(wxInputStream
& str
);
434 virtual bool Write(wxOutputStream
&str
) const;
435 #endif // wxUSE_STREAMS
436 virtual wxString
GetType() const { return wxT("double"); };
442 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
444 void wxVariantDataReal::Copy(wxVariantData
& data
)
446 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
448 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
450 otherData
.m_value
= m_value
;
453 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
455 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
457 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
459 return (otherData
.m_value
== m_value
);
462 #if wxUSE_STD_IOSTREAM
463 bool wxVariantDataReal::Write(ostream
& str
) const
467 str
<< (const char*) s
.mb_str();
472 bool wxVariantDataReal::Write(wxString
& str
) const
474 str
.Printf(wxT("%.4f"), m_value
);
478 #if wxUSE_STD_IOSTREAM
479 bool wxVariantDataReal::Read(istream
& str
)
487 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
489 wxTextOutputStream
s(str
);
490 s
.WriteDouble((double)m_value
);
494 bool wxVariantDataReal::Read(wxInputStream
& str
)
496 wxTextInputStream
s(str
);
497 m_value
= (float)s
.ReadDouble();
500 #endif // wxUSE_STREAMS
502 bool wxVariantDataReal::Read(wxString
& str
)
504 m_value
= wxAtof((const wxChar
*) str
);
513 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
515 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
517 wxVariantDataBool() { m_value
= 0; }
518 wxVariantDataBool(bool value
) { m_value
= value
; }
520 inline bool GetValue() const { return m_value
; }
521 inline void SetValue(bool value
) { m_value
= value
; }
523 virtual void Copy(wxVariantData
& data
);
524 virtual bool Eq(wxVariantData
& data
) const;
525 #if wxUSE_STD_IOSTREAM
526 virtual bool Write(ostream
& str
) const;
528 virtual bool Write(wxString
& str
) const;
529 virtual bool Read(wxString
& str
);
530 #if wxUSE_STD_IOSTREAM
531 virtual bool Read(istream
& str
);
534 virtual bool Read(wxInputStream
& str
);
535 virtual bool Write(wxOutputStream
& str
) const;
536 #endif // wxUSE_STREAMS
537 virtual wxString
GetType() const { return wxT("bool"); };
543 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
545 void wxVariantDataBool::Copy(wxVariantData
& data
)
547 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
549 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
551 otherData
.m_value
= m_value
;
554 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
556 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
558 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
560 return (otherData
.m_value
== m_value
);
563 #if wxUSE_STD_IOSTREAM
564 bool wxVariantDataBool::Write(ostream
& str
) const
568 str
<< (const char*) s
.mb_str();
573 bool wxVariantDataBool::Write(wxString
& str
) const
575 str
.Printf(wxT("%d"), (int) m_value
);
579 #if wxUSE_STD_IOSTREAM
580 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
582 wxFAIL_MSG(wxT("Unimplemented"));
583 // str >> (long) m_value;
589 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
591 wxTextOutputStream
s(str
);
597 bool wxVariantDataBool::Read(wxInputStream
& str
)
599 wxTextInputStream
s(str
);
601 m_value
= s
.Read8() != 0;
604 #endif // wxUSE_STREAMS
606 bool wxVariantDataBool::Read(wxString
& str
)
608 m_value
= (wxAtol((const wxChar
*) str
) != 0);
617 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
619 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
621 wxVariantDataChar() { m_value
= 0; }
622 wxVariantDataChar(char value
) { m_value
= value
; }
624 inline char GetValue() const { return m_value
; }
625 inline void SetValue(char value
) { m_value
= value
; }
627 virtual void Copy(wxVariantData
& data
);
628 virtual bool Eq(wxVariantData
& data
) const;
629 #if wxUSE_STD_IOSTREAM
630 virtual bool Read(istream
& str
);
631 virtual bool Write(ostream
& str
) const;
633 virtual bool Read(wxString
& str
);
634 virtual bool Write(wxString
& str
) const;
636 virtual bool Read(wxInputStream
& str
);
637 virtual bool Write(wxOutputStream
& str
) const;
638 #endif // wxUSE_STREAMS
639 virtual wxString
GetType() const { return wxT("char"); };
645 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
647 void wxVariantDataChar::Copy(wxVariantData
& data
)
649 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
651 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
653 otherData
.m_value
= m_value
;
656 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
658 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
660 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
662 return (otherData
.m_value
== m_value
);
665 #if wxUSE_STD_IOSTREAM
666 bool wxVariantDataChar::Write(ostream
& str
) const
670 str
<< (const char*) s
.mb_str();
675 bool wxVariantDataChar::Write(wxString
& str
) const
677 str
.Printf(wxT("%c"), m_value
);
681 #if wxUSE_STD_IOSTREAM
682 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
684 wxFAIL_MSG(wxT("Unimplemented"));
691 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
693 wxTextOutputStream
s(str
);
699 bool wxVariantDataChar::Read(wxInputStream
& str
)
701 wxTextInputStream
s(str
);
706 #endif // wxUSE_STREAMS
708 bool wxVariantDataChar::Read(wxString
& str
)
710 m_value
= str
[(size_t)0];
715 * wxVariantDataString
718 #if defined(__BORLANDC__) && defined(__WIN16__)
719 // Change name because of truncation
720 #define wxVariantDataString wxVariantStringData
723 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
725 #if defined(__BORLANDC__) && defined(__WIN16__)
726 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
728 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
731 wxVariantDataString() { }
732 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
734 inline wxString
GetValue() const { return m_value
; }
735 inline void SetValue(const wxString
& value
) { m_value
= value
; }
737 virtual void Copy(wxVariantData
& data
);
738 virtual bool Eq(wxVariantData
& data
) const;
739 #if wxUSE_STD_IOSTREAM
740 virtual bool Write(ostream
& str
) const;
742 virtual bool Read(wxString
& str
);
743 virtual bool Write(wxString
& str
) const;
744 #if wxUSE_STD_IOSTREAM
745 virtual bool Read(istream
& str
);
748 virtual bool Read(wxInputStream
& str
);
749 virtual bool Write(wxOutputStream
& str
) const;
750 #endif // wxUSE_STREAMS
751 virtual wxString
GetType() const { return wxT("string"); };
757 void wxVariantDataString::Copy(wxVariantData
& data
)
759 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
761 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
763 otherData
.m_value
= m_value
;
766 bool wxVariantDataString::Eq(wxVariantData
& data
) const
768 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
770 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
772 return (otherData
.m_value
== m_value
);
775 #if wxUSE_STD_IOSTREAM
776 bool wxVariantDataString::Write(ostream
& str
) const
778 str
<< (const char*) m_value
.mb_str();
783 bool wxVariantDataString::Write(wxString
& str
) const
789 #if wxUSE_STD_IOSTREAM
790 bool wxVariantDataString::Read(istream
& str
)
798 bool wxVariantDataString::Write(wxOutputStream
& str
) const
800 // why doesn't wxOutputStream::operator<< take "const wxString&"
801 wxTextOutputStream
s(str
);
802 s
.WriteString(m_value
);
806 bool wxVariantDataString::Read(wxInputStream
& str
)
808 wxTextInputStream
s(str
);
810 m_value
= s
.ReadString();
813 #endif // wxUSE_STREAMS
815 bool wxVariantDataString::Read(wxString
& str
)
821 #if defined(__BORLANDC__) && defined(__WIN16__)
822 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
824 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
831 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
832 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
834 class wxVariantDataTime
: public wxVariantData
836 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
838 wxVariantDataTime() { }
839 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
841 inline wxTime
GetValue() const { return m_value
; }
842 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
844 virtual void Copy(wxVariantData
& data
);
845 virtual bool Eq(wxVariantData
& data
) const;
846 #if wxUSE_STD_IOSTREAM
847 virtual bool Write(ostream
& str
) const;
849 virtual bool Write(wxString
& str
) const;
850 #if wxUSE_STD_IOSTREAM
851 virtual bool Read(istream
& str
);
853 virtual bool Read(wxString
& str
);
854 virtual wxString
GetType() const { return wxT("time"); };
855 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
861 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
863 void wxVariantDataTime::Copy(wxVariantData
& data
)
865 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Copy: Can't copy to this type of data") );
867 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
869 otherData
.m_value
= m_value
;
872 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
874 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Eq: argument mismatch") );
876 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
878 return (otherData
.m_value
== m_value
);
881 #if wxUSE_STD_IOSTREAM
882 bool wxVariantDataTime::Write(ostream
& str
) const
886 str
<< (const char*) s
.mb_str();
891 bool wxVariantDataTime::Write(wxString
& str
) const
893 wxChar
*s
= m_value
.FormatTime();
898 #if wxUSE_STD_IOSTREAM
899 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
906 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
916 class wxVariantDataDate
: public wxVariantData
918 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
920 wxVariantDataDate() { }
921 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
923 inline wxDate
GetValue() const { return m_value
; }
924 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
926 virtual void Copy(wxVariantData
& data
);
927 virtual bool Eq(wxVariantData
& data
) const;
928 #if wxUSE_STD_IOSTREAM
929 virtual bool Write(ostream
& str
) const;
931 virtual bool Write(wxString
& str
) const;
932 #if wxUSE_STD_IOSTREAM
933 virtual bool Read(istream
& str
);
935 virtual bool Read(wxString
& str
);
936 virtual wxString
GetType() const { return wxT("date"); };
937 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
943 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
945 void wxVariantDataDate::Copy(wxVariantData
& data
)
947 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Copy: Can't copy to this type of data") );
949 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
951 otherData
.m_value
= m_value
;
954 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
956 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Eq: argument mismatch") );
958 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
960 return (otherData
.m_value
== m_value
);
963 #if wxUSE_STD_IOSTREAM
964 bool wxVariantDataDate::Write(ostream
& str
) const
968 str
<< (const char*) s
.mb_str();
973 bool wxVariantDataDate::Write(wxString
& str
) const
975 str
= m_value
.FormatDate();
979 #if wxUSE_STD_IOSTREAM
980 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
987 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
996 * wxVariantDataVoidPtr
999 class wxVariantDataVoidPtr
: public wxVariantData
1001 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
1003 wxVariantDataVoidPtr() { }
1004 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1006 inline void* GetValue() const { return m_value
; }
1007 inline void SetValue(void* value
) { m_value
= value
; }
1009 virtual void Copy(wxVariantData
& data
);
1010 virtual bool Eq(wxVariantData
& data
) const;
1011 #if wxUSE_STD_IOSTREAM
1012 virtual bool Write(ostream
& str
) const;
1014 virtual bool Write(wxString
& str
) const;
1015 #if wxUSE_STD_IOSTREAM
1016 virtual bool Read(istream
& str
);
1018 virtual bool Read(wxString
& str
);
1019 virtual wxString
GetType() const { return wxT("void*"); };
1020 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1026 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1028 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1030 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1032 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1034 otherData
.m_value
= m_value
;
1037 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1039 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1041 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1043 return (otherData
.m_value
== m_value
);
1046 #if wxUSE_STD_IOSTREAM
1047 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1051 str
<< (const char*) s
.mb_str();
1056 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1058 str
.Printf(wxT("%ld"), (long) m_value
);
1062 #if wxUSE_STD_IOSTREAM
1063 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1070 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1081 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1083 // Construction & destruction
1084 wxVariant::wxVariant()
1086 m_data
= (wxVariantData
*) NULL
;
1089 wxVariant::wxVariant(double val
, const wxString
& name
)
1091 m_data
= new wxVariantDataReal(val
);
1095 wxVariant::wxVariant(long val
, const wxString
& name
)
1097 m_data
= new wxVariantDataLong(val
);
1102 wxVariant::wxVariant(bool val
, const wxString
& name
)
1104 m_data
= new wxVariantDataBool(val
);
1109 wxVariant::wxVariant(char val
, const wxString
& name
)
1111 m_data
= new wxVariantDataChar(val
);
1115 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1117 m_data
= new wxVariantDataString(val
);
1121 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1123 m_data
= new wxVariantDataString(wxString(val
));
1127 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1129 m_data
= new wxVariantDataStringList(val
);
1133 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1135 m_data
= new wxVariantDataList(val
);
1139 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1140 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1141 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1143 m_data
= new wxVariantDataTime(val
);
1147 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1149 m_data
= new wxVariantDataDate(val
);
1154 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1156 m_data
= new wxVariantDataVoidPtr(val
);
1160 wxVariant::wxVariant(const wxVariant
& variant
)
1162 if (!variant
.IsNull())
1164 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1165 variant
.m_data
->Copy(*m_data
);
1168 m_data
= (wxVariantData
*) NULL
;
1169 m_name
= variant
.m_name
;
1172 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1178 wxVariant::~wxVariant()
1184 // Make NULL (i.e. delete the data)
1185 void wxVariant::MakeNull()
1191 // Generic operators
1193 void wxVariant::operator= (const wxVariant
& variant
)
1195 if (variant
.IsNull())
1201 if (IsNull() || (GetType() != variant
.GetType()))
1205 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1207 // GetData()->Copy(* variant.GetData());
1208 variant
.GetData()->Copy(* GetData());
1211 // Assignment using data, e.g.
1212 // myVariant = new wxStringVariantData("hello")
1213 void wxVariant::operator= (wxVariantData
* variantData
)
1216 m_data
= variantData
;
1219 bool wxVariant::operator== (const wxVariant
& variant
) const
1221 if (IsNull() || variant
.IsNull())
1222 return (IsNull() == variant
.IsNull());
1224 return (GetData()->Eq(* variant
.GetData()));
1227 bool wxVariant::operator!= (const wxVariant
& variant
) const
1229 return (!(*this == variant
));
1233 // Specific operators
1234 bool wxVariant::operator== (double value
) const
1237 if (!Convert(&thisValue
))
1240 return (value
== thisValue
);
1243 bool wxVariant::operator!= (double value
) const
1245 return (!((*this) == value
));
1248 void wxVariant::operator= (double value
)
1250 if (GetType() == wxT("double"))
1252 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1258 m_data
= new wxVariantDataReal(value
);
1262 bool wxVariant::operator== (long value
) const
1265 if (!Convert(&thisValue
))
1268 return (value
== thisValue
);
1271 bool wxVariant::operator!= (long value
) const
1273 return (!((*this) == value
));
1276 void wxVariant::operator= (long value
)
1278 if (GetType() == wxT("long"))
1280 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1286 m_data
= new wxVariantDataLong(value
);
1290 bool wxVariant::operator== (char value
) const
1293 if (!Convert(&thisValue
))
1296 return (value
== thisValue
);
1299 bool wxVariant::operator!= (char value
) const
1301 return (!((*this) == value
));
1304 void wxVariant::operator= (char value
)
1306 if (GetType() == wxT("char"))
1308 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1314 m_data
= new wxVariantDataChar(value
);
1319 bool wxVariant::operator== (bool value
) const
1322 if (!Convert(&thisValue
))
1325 return (value
== thisValue
);
1328 bool wxVariant::operator!= (bool value
) const
1330 return (!((*this) == value
));
1333 void wxVariant::operator= (bool value
)
1335 if (GetType() == wxT("bool"))
1337 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1343 m_data
= new wxVariantDataBool(value
);
1348 bool wxVariant::operator== (const wxString
& value
) const
1351 if (!Convert(&thisValue
))
1354 return (value
== thisValue
);
1357 bool wxVariant::operator!= (const wxString
& value
) const
1359 return (!((*this) == value
));
1362 void wxVariant::operator= (const wxString
& value
)
1364 if (GetType() == wxT("string"))
1366 ((wxVariantDataString
*)GetData())->SetValue(value
);
1372 m_data
= new wxVariantDataString(value
);
1376 void wxVariant::operator= (const wxChar
* value
)
1378 if (GetType() == wxT("string"))
1380 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1386 m_data
= new wxVariantDataString(wxString(value
));
1390 bool wxVariant::operator== (const wxStringList
& value
) const
1392 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1394 wxVariantDataStringList
other(value
);
1395 return (m_data
->Eq(other
));
1398 bool wxVariant::operator!= (const wxStringList
& value
) const
1400 return (!((*this) == value
));
1403 void wxVariant::operator= (const wxStringList
& value
)
1405 if (GetType() == wxT("stringlist"))
1407 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1413 m_data
= new wxVariantDataStringList(value
);
1417 bool wxVariant::operator== (const wxList
& value
) const
1419 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1421 wxVariantDataList
other(value
);
1422 return (m_data
->Eq(other
));
1425 bool wxVariant::operator!= (const wxList
& value
) const
1427 return (!((*this) == value
));
1430 void wxVariant::operator= (const wxList
& value
)
1432 if (GetType() == wxT("list"))
1434 ((wxVariantDataList
*)GetData())->SetValue(value
);
1440 m_data
= new wxVariantDataList(value
);
1444 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1445 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1446 bool wxVariant::operator== (const wxTime
& value
) const
1449 if (!Convert(&thisValue
))
1452 return (value
== thisValue
);
1455 bool wxVariant::operator!= (const wxTime
& value
) const
1457 return (!((*this) == value
));
1460 void wxVariant::operator= (const wxTime
& value
)
1462 if (GetType() == wxT("time"))
1464 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1470 m_data
= new wxVariantDataTime(value
);
1474 bool wxVariant::operator== (const wxDate
& value
) const
1477 if (!Convert(&thisValue
))
1480 return (value
== thisValue
);
1483 bool wxVariant::operator!= (const wxDate
& value
) const
1485 return (!((*this) == value
));
1488 void wxVariant::operator= (const wxDate
& value
)
1490 if (GetType() == wxT("date"))
1492 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1498 m_data
= new wxVariantDataDate(value
);
1503 bool wxVariant::operator== (void* value
) const
1505 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1508 bool wxVariant::operator!= (void* value
) const
1510 return (!((*this) == value
));
1513 void wxVariant::operator= (void* value
)
1515 if (GetType() == wxT("void*"))
1517 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1523 m_data
= new wxVariantDataVoidPtr(value
);
1527 // Treat a list variant as an array
1528 wxVariant
wxVariant::operator[] (size_t idx
) const
1530 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1532 if (GetType() == wxT("list"))
1534 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1535 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1536 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1538 else if (GetType() == wxT("stringlist"))
1540 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1541 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1543 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1546 return wxNullVariant
;
1549 wxVariant
& wxVariant::operator[] (size_t idx
)
1551 // We can't return a reference to a variant for a string list, since the string
1552 // is actually stored as a char*, not a variant.
1554 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1556 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1557 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1559 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1562 // Return the number of elements in a list
1563 int wxVariant::GetCount() const
1565 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1567 if (GetType() == wxT("list"))
1569 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1570 return data
->GetValue().Number();
1572 else if (GetType() == wxT("stringlist"))
1574 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1575 return data
->GetValue().Number();
1580 wxString
wxVariant::MakeString() const
1585 if (GetData()->Write(str
))
1588 return wxString(wxT(""));
1593 void wxVariant::SetData(wxVariantData
* data
)
1595 if (m_data
) delete m_data
;
1600 // Returns a string representing the type of the variant,
1601 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1602 wxString
wxVariant::GetType() const
1605 return wxString(wxT("null"));
1607 return m_data
->GetType();
1611 bool wxVariant::IsType(const wxString
& type
) const
1613 return (GetType() == type
);
1618 double wxVariant::GetReal() const
1621 if (Convert(& value
))
1625 wxFAIL_MSG(wxT("Could not convert to a real number"));
1630 long wxVariant::GetInteger() const
1633 if (Convert(& value
))
1637 wxFAIL_MSG(wxT("Could not convert to an integer"));
1642 char wxVariant::GetChar() const
1645 if (Convert(& value
))
1649 wxFAIL_MSG(wxT("Could not convert to a char"));
1654 bool wxVariant::GetBool() const
1657 if (Convert(& value
))
1661 wxFAIL_MSG(wxT("Could not convert to a bool"));
1666 wxString
wxVariant::GetString() const
1669 if (Convert(& value
))
1673 wxFAIL_MSG(wxT("Could not convert to a string"));
1674 return wxString("");
1678 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1679 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1680 wxTime
wxVariant::GetTime() const
1683 if (Convert(& value
))
1687 wxFAIL_MSG(wxT("Could not convert to a time"));
1692 wxDate
wxVariant::GetDate() const
1695 if (Convert(& value
))
1699 wxFAIL_MSG(wxT("Could not convert to a date"));
1705 void* wxVariant::GetVoidPtr() const
1707 wxASSERT( (GetType() == wxT("void*")) );
1709 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1712 wxList
& wxVariant::GetList() const
1714 wxASSERT( (GetType() == wxT("list")) );
1716 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1719 wxStringList
& wxVariant::GetStringList() const
1721 wxASSERT( (GetType() == wxT("stringlist")) );
1723 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1727 void wxVariant::Append(const wxVariant
& value
)
1729 wxList
& list
= GetList();
1731 list
.Append(new wxVariant(value
));
1734 // Insert at front of list
1735 void wxVariant::Insert(const wxVariant
& value
)
1737 wxList
& list
= GetList();
1739 list
.Insert(new wxVariant(value
));
1742 // Returns TRUE if the variant is a member of the list
1743 bool wxVariant::Member(const wxVariant
& value
) const
1745 wxList
& list
= GetList();
1747 wxNode
* node
= list
.First();
1750 wxVariant
* other
= (wxVariant
*) node
->Data();
1751 if (value
== *other
)
1753 node
= node
->Next();
1758 // Deletes the nth element of the list
1759 bool wxVariant::Delete(int item
)
1761 wxList
& list
= GetList();
1763 wxASSERT_MSG( (item
< list
.Number()), wxT("Invalid index to Delete") );
1764 wxNode
* node
= list
.Nth(item
);
1765 wxVariant
* variant
= (wxVariant
*) node
->Data();
1772 void wxVariant::ClearList()
1774 if (!IsNull() && (GetType() == wxT("list")))
1776 ((wxVariantDataList
*) m_data
)->Clear();
1780 if (GetType() != wxT("list"))
1785 m_data
= new wxVariantDataList
;
1790 bool wxVariant::Convert(long* value
) const
1792 wxString
type(GetType());
1793 if (type
== wxT("double"))
1794 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1795 else if (type
== wxT("long"))
1796 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1798 else if (type
== wxT("bool"))
1799 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1801 else if (type
== wxT("string"))
1802 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1809 bool wxVariant::Convert(bool* value
) const
1811 wxString
type(GetType());
1812 if (type
== wxT("double"))
1813 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1814 else if (type
== wxT("long"))
1815 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1817 else if (type
== wxT("bool"))
1818 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1820 else if (type
== wxT("string"))
1822 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1824 if (val
== wxT("true") || val
== wxT("yes"))
1826 else if (val
== wxT("false") || val
== wxT("no"))
1837 bool wxVariant::Convert(double* value
) const
1839 wxString
type(GetType());
1840 if (type
== wxT("double"))
1841 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1842 else if (type
== wxT("long"))
1843 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1845 else if (type
== wxT("bool"))
1846 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1848 else if (type
== wxT("string"))
1849 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1856 bool wxVariant::Convert(char* value
) const
1858 wxString
type(GetType());
1859 if (type
== wxT("char"))
1860 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1861 else if (type
== wxT("long"))
1862 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1864 else if (type
== wxT("bool"))
1865 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1873 bool wxVariant::Convert(wxString
* value
) const
1875 *value
= MakeString();
1879 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1880 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1881 bool wxVariant::Convert(wxTime
* value
) const
1883 wxString
type(GetType());
1884 if (type
== wxT("time"))
1885 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1886 else if (type
== wxT("date"))
1887 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1894 bool wxVariant::Convert(wxDate
* value
) const
1896 wxString
type(GetType());
1897 if (type
== wxT("date"))
1898 *value
= ((wxVariantDataDate
*)GetData())->GetValue();