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
)
833 class wxVariantDataTime
: public wxVariantData
835 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
837 wxVariantDataTime() { }
838 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
840 inline wxTime
GetValue() const { return m_value
; }
841 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
843 virtual void Copy(wxVariantData
& data
);
844 virtual bool Eq(wxVariantData
& data
) const;
845 #if wxUSE_STD_IOSTREAM
846 virtual bool Write(ostream
& str
) const;
848 virtual bool Write(wxString
& str
) const;
849 #if wxUSE_STD_IOSTREAM
850 virtual bool Read(istream
& str
);
852 virtual bool Read(wxString
& str
);
853 virtual wxString
GetType() const { return wxT("time"); };
854 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
860 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
862 void wxVariantDataTime::Copy(wxVariantData
& data
)
864 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Copy: Can't copy to this type of data") );
866 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
868 otherData
.m_value
= m_value
;
871 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
873 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Eq: argument mismatch") );
875 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
877 return (otherData
.m_value
== m_value
);
880 #if wxUSE_STD_IOSTREAM
881 bool wxVariantDataTime::Write(ostream
& str
) const
885 str
<< (const char*) s
.mb_str();
890 bool wxVariantDataTime::Write(wxString
& str
) const
892 wxChar
*s
= m_value
.FormatTime();
897 #if wxUSE_STD_IOSTREAM
898 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
905 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
915 class wxVariantDataDate
: public wxVariantData
917 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
919 wxVariantDataDate() { }
920 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
922 inline wxDate
GetValue() const { return m_value
; }
923 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
925 virtual void Copy(wxVariantData
& data
);
926 virtual bool Eq(wxVariantData
& data
) const;
927 #if wxUSE_STD_IOSTREAM
928 virtual bool Write(ostream
& str
) const;
930 virtual bool Write(wxString
& str
) const;
931 #if wxUSE_STD_IOSTREAM
932 virtual bool Read(istream
& str
);
934 virtual bool Read(wxString
& str
);
935 virtual wxString
GetType() const { return wxT("date"); };
936 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
942 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
944 void wxVariantDataDate::Copy(wxVariantData
& data
)
946 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Copy: Can't copy to this type of data") );
948 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
950 otherData
.m_value
= m_value
;
953 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
955 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Eq: argument mismatch") );
957 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
959 return (otherData
.m_value
== m_value
);
962 #if wxUSE_STD_IOSTREAM
963 bool wxVariantDataDate::Write(ostream
& str
) const
967 str
<< (const char*) s
.mb_str();
972 bool wxVariantDataDate::Write(wxString
& str
) const
974 str
= m_value
.FormatDate();
978 #if wxUSE_STD_IOSTREAM
979 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
986 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
995 * wxVariantDataVoidPtr
998 class wxVariantDataVoidPtr
: public wxVariantData
1000 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
1002 wxVariantDataVoidPtr() { }
1003 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1005 inline void* GetValue() const { return m_value
; }
1006 inline void SetValue(void* value
) { m_value
= value
; }
1008 virtual void Copy(wxVariantData
& data
);
1009 virtual bool Eq(wxVariantData
& data
) const;
1010 #if wxUSE_STD_IOSTREAM
1011 virtual bool Write(ostream
& str
) const;
1013 virtual bool Write(wxString
& str
) const;
1014 #if wxUSE_STD_IOSTREAM
1015 virtual bool Read(istream
& str
);
1017 virtual bool Read(wxString
& str
);
1018 virtual wxString
GetType() const { return wxT("void*"); };
1019 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1025 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1027 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1029 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1031 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1033 otherData
.m_value
= m_value
;
1036 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1038 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1040 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1042 return (otherData
.m_value
== m_value
);
1045 #if wxUSE_STD_IOSTREAM
1046 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1050 str
<< (const char*) s
.mb_str();
1055 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1057 str
.Printf(wxT("%ld"), (long) m_value
);
1061 #if wxUSE_STD_IOSTREAM
1062 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1069 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1080 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1082 // Construction & destruction
1083 wxVariant::wxVariant()
1085 m_data
= (wxVariantData
*) NULL
;
1088 wxVariant::wxVariant(double val
, const wxString
& name
)
1090 m_data
= new wxVariantDataReal(val
);
1094 wxVariant::wxVariant(long val
, const wxString
& name
)
1096 m_data
= new wxVariantDataLong(val
);
1101 wxVariant::wxVariant(bool val
, const wxString
& name
)
1103 m_data
= new wxVariantDataBool(val
);
1108 wxVariant::wxVariant(char val
, const wxString
& name
)
1110 m_data
= new wxVariantDataChar(val
);
1114 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1116 m_data
= new wxVariantDataString(val
);
1120 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1122 m_data
= new wxVariantDataString(wxString(val
));
1126 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1128 m_data
= new wxVariantDataStringList(val
);
1132 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1134 m_data
= new wxVariantDataList(val
);
1139 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1141 m_data
= new wxVariantDataTime(val
);
1145 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1147 m_data
= new wxVariantDataDate(val
);
1152 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1154 m_data
= new wxVariantDataVoidPtr(val
);
1158 wxVariant::wxVariant(const wxVariant
& variant
)
1160 if (!variant
.IsNull())
1162 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1163 variant
.m_data
->Copy(*m_data
);
1166 m_data
= (wxVariantData
*) NULL
;
1167 m_name
= variant
.m_name
;
1170 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1176 wxVariant::~wxVariant()
1182 // Make NULL (i.e. delete the data)
1183 void wxVariant::MakeNull()
1189 // Generic operators
1191 void wxVariant::operator= (const wxVariant
& variant
)
1193 if (variant
.IsNull())
1199 if (IsNull() || (GetType() != variant
.GetType()))
1203 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1205 // GetData()->Copy(* variant.GetData());
1206 variant
.GetData()->Copy(* GetData());
1209 // Assignment using data, e.g.
1210 // myVariant = new wxStringVariantData("hello")
1211 void wxVariant::operator= (wxVariantData
* variantData
)
1214 m_data
= variantData
;
1217 bool wxVariant::operator== (const wxVariant
& variant
) const
1219 if (IsNull() || variant
.IsNull())
1220 return (IsNull() == variant
.IsNull());
1222 return (GetData()->Eq(* variant
.GetData()));
1225 bool wxVariant::operator!= (const wxVariant
& variant
) const
1227 return (!(*this == variant
));
1231 // Specific operators
1232 bool wxVariant::operator== (double value
) const
1235 if (!Convert(&thisValue
))
1238 return (value
== thisValue
);
1241 bool wxVariant::operator!= (double value
) const
1243 return (!((*this) == value
));
1246 void wxVariant::operator= (double value
)
1248 if (GetType() == wxT("double"))
1250 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1256 m_data
= new wxVariantDataReal(value
);
1260 bool wxVariant::operator== (long value
) const
1263 if (!Convert(&thisValue
))
1266 return (value
== thisValue
);
1269 bool wxVariant::operator!= (long value
) const
1271 return (!((*this) == value
));
1274 void wxVariant::operator= (long value
)
1276 if (GetType() == wxT("long"))
1278 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1284 m_data
= new wxVariantDataLong(value
);
1288 bool wxVariant::operator== (char value
) const
1291 if (!Convert(&thisValue
))
1294 return (value
== thisValue
);
1297 bool wxVariant::operator!= (char value
) const
1299 return (!((*this) == value
));
1302 void wxVariant::operator= (char value
)
1304 if (GetType() == wxT("char"))
1306 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1312 m_data
= new wxVariantDataChar(value
);
1317 bool wxVariant::operator== (bool value
) const
1320 if (!Convert(&thisValue
))
1323 return (value
== thisValue
);
1326 bool wxVariant::operator!= (bool value
) const
1328 return (!((*this) == value
));
1331 void wxVariant::operator= (bool value
)
1333 if (GetType() == wxT("bool"))
1335 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1341 m_data
= new wxVariantDataBool(value
);
1346 bool wxVariant::operator== (const wxString
& value
) const
1349 if (!Convert(&thisValue
))
1352 return (value
== thisValue
);
1355 bool wxVariant::operator!= (const wxString
& value
) const
1357 return (!((*this) == value
));
1360 void wxVariant::operator= (const wxString
& value
)
1362 if (GetType() == wxT("string"))
1364 ((wxVariantDataString
*)GetData())->SetValue(value
);
1370 m_data
= new wxVariantDataString(value
);
1374 void wxVariant::operator= (const wxChar
* value
)
1376 if (GetType() == wxT("string"))
1378 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1384 m_data
= new wxVariantDataString(wxString(value
));
1388 bool wxVariant::operator== (const wxStringList
& value
) const
1390 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1392 wxVariantDataStringList
other(value
);
1393 return (m_data
->Eq(other
));
1396 bool wxVariant::operator!= (const wxStringList
& value
) const
1398 return (!((*this) == value
));
1401 void wxVariant::operator= (const wxStringList
& value
)
1403 if (GetType() == wxT("stringlist"))
1405 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1411 m_data
= new wxVariantDataStringList(value
);
1415 bool wxVariant::operator== (const wxList
& value
) const
1417 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1419 wxVariantDataList
other(value
);
1420 return (m_data
->Eq(other
));
1423 bool wxVariant::operator!= (const wxList
& value
) const
1425 return (!((*this) == value
));
1428 void wxVariant::operator= (const wxList
& value
)
1430 if (GetType() == wxT("list"))
1432 ((wxVariantDataList
*)GetData())->SetValue(value
);
1438 m_data
= new wxVariantDataList(value
);
1443 bool wxVariant::operator== (const wxTime
& value
) const
1446 if (!Convert(&thisValue
))
1449 return (value
== thisValue
);
1452 bool wxVariant::operator!= (const wxTime
& value
) const
1454 return (!((*this) == value
));
1457 void wxVariant::operator= (const wxTime
& value
)
1459 if (GetType() == wxT("time"))
1461 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1467 m_data
= new wxVariantDataTime(value
);
1471 bool wxVariant::operator== (const wxDate
& value
) const
1474 if (!Convert(&thisValue
))
1477 return (value
== thisValue
);
1480 bool wxVariant::operator!= (const wxDate
& value
) const
1482 return (!((*this) == value
));
1485 void wxVariant::operator= (const wxDate
& value
)
1487 if (GetType() == wxT("date"))
1489 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1495 m_data
= new wxVariantDataDate(value
);
1500 bool wxVariant::operator== (void* value
) const
1502 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1505 bool wxVariant::operator!= (void* value
) const
1507 return (!((*this) == value
));
1510 void wxVariant::operator= (void* value
)
1512 if (GetType() == wxT("void*"))
1514 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1520 m_data
= new wxVariantDataVoidPtr(value
);
1524 // Treat a list variant as an array
1525 wxVariant
wxVariant::operator[] (size_t idx
) const
1527 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1529 if (GetType() == wxT("list"))
1531 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1532 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1533 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1535 else if (GetType() == wxT("stringlist"))
1537 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1538 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1540 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1543 return wxNullVariant
;
1546 wxVariant
& wxVariant::operator[] (size_t idx
)
1548 // We can't return a reference to a variant for a string list, since the string
1549 // is actually stored as a char*, not a variant.
1551 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1553 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1554 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1556 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1559 // Return the number of elements in a list
1560 int wxVariant::GetCount() const
1562 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1564 if (GetType() == wxT("list"))
1566 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1567 return data
->GetValue().Number();
1569 else if (GetType() == wxT("stringlist"))
1571 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1572 return data
->GetValue().Number();
1577 wxString
wxVariant::MakeString() const
1582 if (GetData()->Write(str
))
1585 return wxString(wxT(""));
1590 void wxVariant::SetData(wxVariantData
* data
)
1592 if (m_data
) delete m_data
;
1597 // Returns a string representing the type of the variant,
1598 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1599 wxString
wxVariant::GetType() const
1602 return wxString(wxT("null"));
1604 return m_data
->GetType();
1608 bool wxVariant::IsType(const wxString
& type
) const
1610 return (GetType() == type
);
1615 double wxVariant::GetReal() const
1618 if (Convert(& value
))
1622 wxFAIL_MSG(wxT("Could not convert to a real number"));
1627 long wxVariant::GetInteger() const
1630 if (Convert(& value
))
1634 wxFAIL_MSG(wxT("Could not convert to an integer"));
1639 char wxVariant::GetChar() const
1642 if (Convert(& value
))
1646 wxFAIL_MSG(wxT("Could not convert to a char"));
1651 bool wxVariant::GetBool() const
1654 if (Convert(& value
))
1658 wxFAIL_MSG(wxT("Could not convert to a bool"));
1663 wxString
wxVariant::GetString() const
1666 if (Convert(& value
))
1670 wxFAIL_MSG(wxT("Could not convert to a string"));
1671 return wxString("");
1676 wxTime
wxVariant::GetTime() const
1679 if (Convert(& value
))
1683 wxFAIL_MSG(wxT("Could not convert to a time"));
1688 wxDate
wxVariant::GetDate() const
1691 if (Convert(& value
))
1695 wxFAIL_MSG(wxT("Could not convert to a date"));
1701 void* wxVariant::GetVoidPtr() const
1703 wxASSERT( (GetType() == wxT("void*")) );
1705 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1708 wxList
& wxVariant::GetList() const
1710 wxASSERT( (GetType() == wxT("list")) );
1712 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1715 wxStringList
& wxVariant::GetStringList() const
1717 wxASSERT( (GetType() == wxT("stringlist")) );
1719 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1723 void wxVariant::Append(const wxVariant
& value
)
1725 wxList
& list
= GetList();
1727 list
.Append(new wxVariant(value
));
1730 // Insert at front of list
1731 void wxVariant::Insert(const wxVariant
& value
)
1733 wxList
& list
= GetList();
1735 list
.Insert(new wxVariant(value
));
1738 // Returns TRUE if the variant is a member of the list
1739 bool wxVariant::Member(const wxVariant
& value
) const
1741 wxList
& list
= GetList();
1743 wxNode
* node
= list
.First();
1746 wxVariant
* other
= (wxVariant
*) node
->Data();
1747 if (value
== *other
)
1749 node
= node
->Next();
1754 // Deletes the nth element of the list
1755 bool wxVariant::Delete(int item
)
1757 wxList
& list
= GetList();
1759 wxASSERT_MSG( (item
< list
.Number()), wxT("Invalid index to Delete") );
1760 wxNode
* node
= list
.Nth(item
);
1761 wxVariant
* variant
= (wxVariant
*) node
->Data();
1768 void wxVariant::ClearList()
1770 if (!IsNull() && (GetType() == wxT("list")))
1772 ((wxVariantDataList
*) m_data
)->Clear();
1776 if (GetType() != wxT("list"))
1781 m_data
= new wxVariantDataList
;
1786 bool wxVariant::Convert(long* value
) const
1788 wxString
type(GetType());
1789 if (type
== wxT("double"))
1790 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1791 else if (type
== wxT("long"))
1792 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1794 else if (type
== wxT("bool"))
1795 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1797 else if (type
== wxT("string"))
1798 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1805 bool wxVariant::Convert(bool* value
) const
1807 wxString
type(GetType());
1808 if (type
== wxT("double"))
1809 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1810 else if (type
== wxT("long"))
1811 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1813 else if (type
== wxT("bool"))
1814 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1816 else if (type
== wxT("string"))
1818 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1820 if (val
== wxT("true") || val
== wxT("yes"))
1822 else if (val
== wxT("false") || val
== wxT("no"))
1833 bool wxVariant::Convert(double* value
) const
1835 wxString
type(GetType());
1836 if (type
== wxT("double"))
1837 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1838 else if (type
== wxT("long"))
1839 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1841 else if (type
== wxT("bool"))
1842 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1844 else if (type
== wxT("string"))
1845 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1852 bool wxVariant::Convert(char* value
) const
1854 wxString
type(GetType());
1855 if (type
== wxT("char"))
1856 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1857 else if (type
== wxT("long"))
1858 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1860 else if (type
== wxT("bool"))
1861 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1869 bool wxVariant::Convert(wxString
* value
) const
1871 *value
= MakeString();
1876 bool wxVariant::Convert(wxTime
* value
) const
1878 wxString
type(GetType());
1879 if (type
== wxT("time"))
1880 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1881 else if (type
== wxT("date"))
1882 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1889 bool wxVariant::Convert(wxDate
* value
) const
1891 wxString
type(GetType());
1892 if (type
== wxT("date"))
1893 *value
= ((wxVariantDataDate
*)GetData())->GetValue();