1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "variant.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_STD_IOSTREAM
32 #include "wx/stream.h"
33 #include "wx/txtstrm.h"
36 #include "wx/string.h"
37 #include "wx/variant.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
40 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
42 wxTime::tFormat
wxTime::ms_Format
= wxTime::wx12h
;
43 wxTime::tPrecision
wxTime::ms_Precision
= wxTime::wxStdMinSec
;
45 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
47 wxVariant WXDLLEXPORT wxNullVariant
;
53 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
55 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
57 wxVariantDataList() {}
58 wxVariantDataList(const wxList
& list
);
61 wxList
& GetValue() const { return (wxList
&) m_value
; }
62 void SetValue(const wxList
& value
) ;
64 virtual void Copy(wxVariantData
& data
);
65 virtual bool Eq(wxVariantData
& data
) const;
66 #if wxUSE_STD_IOSTREAM
67 virtual bool Write(ostream
& str
) const;
69 virtual bool Write(wxString
& str
) const;
70 #if wxUSE_STD_IOSTREAM
71 virtual bool Read(istream
& str
);
73 virtual bool Read(wxString
& str
);
74 virtual wxString
GetType() const { return wxT("list"); };
82 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
84 wxVariantDataList::wxVariantDataList(const wxList
& list
)
89 wxVariantDataList::~wxVariantDataList()
94 void wxVariantDataList::SetValue(const wxList
& value
)
97 wxNode
* node
= value
.First();
100 wxVariant
* var
= (wxVariant
*) node
->Data();
101 m_value
.Append(new wxVariant(*var
));
106 void wxVariantDataList::Clear()
108 wxNode
* node
= m_value
.First();
111 wxVariant
* var
= (wxVariant
*) node
->Data();
118 void wxVariantDataList::Copy(wxVariantData
& data
)
120 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
122 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
125 wxNode
* node
= m_value
.First();
128 wxVariant
* var
= (wxVariant
*) node
->Data();
129 listData
.m_value
.Append(new wxVariant(*var
));
134 bool wxVariantDataList::Eq(wxVariantData
& data
) const
136 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
138 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
139 wxNode
* node1
= m_value
.First();
140 wxNode
* node2
= listData
.GetValue().First();
141 while (node1
&& node2
)
143 wxVariant
* var1
= (wxVariant
*) node1
->Data();
144 wxVariant
* var2
= (wxVariant
*) node2
->Data();
145 if ((*var1
) != (*var2
))
147 node1
= node1
->Next();
148 node2
= node2
->Next();
150 if (node1
|| node2
) return FALSE
;
154 #if wxUSE_STD_IOSTREAM
155 bool wxVariantDataList::Write(ostream
& str
) const
159 str
<< (const char*) s
.mb_str();
164 bool wxVariantDataList::Write(wxString
& str
) const
167 wxNode
* node
= m_value
.First();
170 wxVariant
* var
= (wxVariant
*) node
->Data();
171 if (node
!= m_value
.First())
174 str
+= var
->MakeString();
181 #if wxUSE_STD_IOSTREAM
182 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
184 wxFAIL_MSG(wxT("Unimplemented"));
190 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
192 wxFAIL_MSG(wxT("Unimplemented"));
198 * wxVariantDataStringList
201 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
203 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
205 wxVariantDataStringList() {}
206 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
208 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
209 void SetValue(const wxStringList
& value
);
211 virtual void Copy(wxVariantData
& data
);
212 virtual bool Eq(wxVariantData
& data
) const;
213 #if wxUSE_STD_IOSTREAM
214 virtual bool Write(ostream
& str
) const;
216 virtual bool Write(wxString
& str
) const;
217 #if wxUSE_STD_IOSTREAM
218 virtual bool Read(istream
& str
);
220 virtual bool Read(wxString
& str
);
221 virtual wxString
GetType() const { return wxT("stringlist"); };
224 wxStringList m_value
;
227 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
229 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
234 void wxVariantDataStringList::Copy(wxVariantData
& data
)
236 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
238 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
240 listData
.m_value
= m_value
;
243 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
245 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
247 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
248 wxNode
* node1
= m_value
.First();
249 wxNode
* node2
= listData
.GetValue().First();
250 while (node1
&& node2
)
252 wxString
str1 ((wxChar
*) node1
->Data());
253 wxString
str2 ((wxChar
*) node2
->Data());
256 node1
= node1
->Next();
257 node2
= node2
->Next();
259 if (node1
|| node2
) return FALSE
;
263 #if wxUSE_STD_IOSTREAM
264 bool wxVariantDataStringList::Write(ostream
& str
) const
268 str
<< (const char*) s
.mb_str();
273 bool wxVariantDataStringList::Write(wxString
& str
) const
276 wxNode
* node
= m_value
.First();
279 wxChar
* s
= (wxChar
*) node
->Data();
280 if (node
!= m_value
.First())
289 #if wxUSE_STD_IOSTREAM
290 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
292 wxFAIL_MSG(wxT("Unimplemented"));
298 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
300 wxFAIL_MSG(wxT("Unimplemented"));
309 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
311 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
313 wxVariantDataLong() { m_value
= 0; }
314 wxVariantDataLong(long value
) { m_value
= value
; }
316 inline long GetValue() const { return m_value
; }
317 inline void SetValue(long value
) { m_value
= value
; }
319 virtual void Copy(wxVariantData
& data
);
320 virtual bool Eq(wxVariantData
& data
) const;
322 virtual bool Read(wxString
& str
);
323 virtual bool Write(wxString
& str
) const;
324 #if wxUSE_STD_IOSTREAM
325 virtual bool Read(istream
& str
);
326 virtual bool Write(ostream
& str
) const;
329 virtual bool Read(wxInputStream
& str
);
330 virtual bool Write(wxOutputStream
&str
) const;
331 #endif // wxUSE_STREAMS
333 virtual wxString
GetType() const { return wxT("long"); };
339 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
341 void wxVariantDataLong::Copy(wxVariantData
& data
)
343 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
345 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
347 otherData
.m_value
= m_value
;
350 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
352 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
354 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
356 return (otherData
.m_value
== m_value
);
359 #if wxUSE_STD_IOSTREAM
360 bool wxVariantDataLong::Write(ostream
& str
) const
364 str
<< (const char*) s
.mb_str();
369 bool wxVariantDataLong::Write(wxString
& str
) const
371 str
.Printf(wxT("%ld"), m_value
);
375 #if wxUSE_STD_IOSTREAM
376 bool wxVariantDataLong::Read(istream
& str
)
384 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
386 wxTextOutputStream
s(str
);
392 bool wxVariantDataLong::Read(wxInputStream
& str
)
394 wxTextInputStream
s(str
);
395 m_value
= s
.Read32();
398 #endif // wxUSE_STREAMS
400 bool wxVariantDataLong::Read(wxString
& str
)
402 m_value
= wxAtol((const wxChar
*) str
);
410 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
412 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
414 wxVariantDataReal() { m_value
= 0.0; }
415 wxVariantDataReal(double value
) { m_value
= value
; }
417 inline double GetValue() const { return m_value
; }
418 inline void SetValue(double value
) { m_value
= value
; }
420 virtual void Copy(wxVariantData
& data
);
421 virtual bool Eq(wxVariantData
& data
) const;
422 virtual bool Read(wxString
& str
);
423 #if wxUSE_STD_IOSTREAM
424 virtual bool Write(ostream
& str
) const;
426 virtual bool Write(wxString
& str
) const;
427 #if wxUSE_STD_IOSTREAM
428 virtual bool Read(istream
& str
);
431 virtual bool Read(wxInputStream
& str
);
432 virtual bool Write(wxOutputStream
&str
) const;
433 #endif // wxUSE_STREAMS
434 virtual wxString
GetType() const { return wxT("double"); };
440 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
442 void wxVariantDataReal::Copy(wxVariantData
& data
)
444 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
446 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
448 otherData
.m_value
= m_value
;
451 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
453 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
455 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
457 return (otherData
.m_value
== m_value
);
460 #if wxUSE_STD_IOSTREAM
461 bool wxVariantDataReal::Write(ostream
& str
) const
465 str
<< (const char*) s
.mb_str();
470 bool wxVariantDataReal::Write(wxString
& str
) const
472 str
.Printf(wxT("%.4f"), m_value
);
476 #if wxUSE_STD_IOSTREAM
477 bool wxVariantDataReal::Read(istream
& str
)
485 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
487 wxTextOutputStream
s(str
);
488 s
.WriteDouble((double)m_value
);
492 bool wxVariantDataReal::Read(wxInputStream
& str
)
494 wxTextInputStream
s(str
);
495 m_value
= (float)s
.ReadDouble();
498 #endif // wxUSE_STREAMS
500 bool wxVariantDataReal::Read(wxString
& str
)
502 m_value
= wxAtof((const wxChar
*) str
);
511 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
513 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
515 wxVariantDataBool() { m_value
= 0; }
516 wxVariantDataBool(bool value
) { m_value
= value
; }
518 inline bool GetValue() const { return m_value
; }
519 inline void SetValue(bool value
) { m_value
= value
; }
521 virtual void Copy(wxVariantData
& data
);
522 virtual bool Eq(wxVariantData
& data
) const;
523 #if wxUSE_STD_IOSTREAM
524 virtual bool Write(ostream
& str
) const;
526 virtual bool Write(wxString
& str
) const;
527 virtual bool Read(wxString
& str
);
528 #if wxUSE_STD_IOSTREAM
529 virtual bool Read(istream
& str
);
532 virtual bool Read(wxInputStream
& str
);
533 virtual bool Write(wxOutputStream
& str
) const;
534 #endif // wxUSE_STREAMS
535 virtual wxString
GetType() const { return wxT("bool"); };
541 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
543 void wxVariantDataBool::Copy(wxVariantData
& data
)
545 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
547 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
549 otherData
.m_value
= m_value
;
552 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
554 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
556 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
558 return (otherData
.m_value
== m_value
);
561 #if wxUSE_STD_IOSTREAM
562 bool wxVariantDataBool::Write(ostream
& str
) const
566 str
<< (const char*) s
.mb_str();
571 bool wxVariantDataBool::Write(wxString
& str
) const
573 str
.Printf(wxT("%d"), (int) m_value
);
577 #if wxUSE_STD_IOSTREAM
578 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
580 wxFAIL_MSG(wxT("Unimplemented"));
581 // str >> (long) m_value;
587 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
589 wxTextOutputStream
s(str
);
595 bool wxVariantDataBool::Read(wxInputStream
& str
)
597 wxTextInputStream
s(str
);
599 m_value
= s
.Read8() != 0;
602 #endif // wxUSE_STREAMS
604 bool wxVariantDataBool::Read(wxString
& str
)
606 m_value
= (wxAtol((const wxChar
*) str
) != 0);
615 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
617 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
619 wxVariantDataChar() { m_value
= 0; }
620 wxVariantDataChar(char value
) { m_value
= value
; }
622 inline char GetValue() const { return m_value
; }
623 inline void SetValue(char value
) { m_value
= value
; }
625 virtual void Copy(wxVariantData
& data
);
626 virtual bool Eq(wxVariantData
& data
) const;
627 #if wxUSE_STD_IOSTREAM
628 virtual bool Read(istream
& str
);
629 virtual bool Write(ostream
& str
) const;
631 virtual bool Read(wxString
& str
);
632 virtual bool Write(wxString
& str
) const;
634 virtual bool Read(wxInputStream
& str
);
635 virtual bool Write(wxOutputStream
& str
) const;
636 #endif // wxUSE_STREAMS
637 virtual wxString
GetType() const { return wxT("char"); };
643 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
645 void wxVariantDataChar::Copy(wxVariantData
& data
)
647 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
649 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
651 otherData
.m_value
= m_value
;
654 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
656 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
658 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
660 return (otherData
.m_value
== m_value
);
663 #if wxUSE_STD_IOSTREAM
664 bool wxVariantDataChar::Write(ostream
& str
) const
668 str
<< (const char*) s
.mb_str();
673 bool wxVariantDataChar::Write(wxString
& str
) const
675 str
.Printf(wxT("%c"), m_value
);
679 #if wxUSE_STD_IOSTREAM
680 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
682 wxFAIL_MSG(wxT("Unimplemented"));
689 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
691 wxTextOutputStream
s(str
);
697 bool wxVariantDataChar::Read(wxInputStream
& str
)
699 wxTextInputStream
s(str
);
704 #endif // wxUSE_STREAMS
706 bool wxVariantDataChar::Read(wxString
& str
)
708 m_value
= str
[(size_t)0];
713 * wxVariantDataString
716 #if defined(__BORLANDC__) && defined(__WIN16__)
717 // Change name because of truncation
718 #define wxVariantDataString wxVariantStringData
721 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
723 #if defined(__BORLANDC__) && defined(__WIN16__)
724 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
726 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
729 wxVariantDataString() { }
730 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
732 inline wxString
GetValue() const { return m_value
; }
733 inline void SetValue(const wxString
& value
) { m_value
= value
; }
735 virtual void Copy(wxVariantData
& data
);
736 virtual bool Eq(wxVariantData
& data
) const;
737 #if wxUSE_STD_IOSTREAM
738 virtual bool Write(ostream
& str
) const;
740 virtual bool Read(wxString
& str
);
741 virtual bool Write(wxString
& str
) const;
742 #if wxUSE_STD_IOSTREAM
743 virtual bool Read(istream
& str
);
746 virtual bool Read(wxInputStream
& str
);
747 virtual bool Write(wxOutputStream
& str
) const;
748 #endif // wxUSE_STREAMS
749 virtual wxString
GetType() const { return wxT("string"); };
755 void wxVariantDataString::Copy(wxVariantData
& data
)
757 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
759 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
761 otherData
.m_value
= m_value
;
764 bool wxVariantDataString::Eq(wxVariantData
& data
) const
766 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
768 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
770 return (otherData
.m_value
== m_value
);
773 #if wxUSE_STD_IOSTREAM
774 bool wxVariantDataString::Write(ostream
& str
) const
776 str
<< (const char*) m_value
.mb_str();
781 bool wxVariantDataString::Write(wxString
& str
) const
787 #if wxUSE_STD_IOSTREAM
788 bool wxVariantDataString::Read(istream
& str
)
796 bool wxVariantDataString::Write(wxOutputStream
& str
) const
798 // why doesn't wxOutputStream::operator<< take "const wxString&"
799 wxTextOutputStream
s(str
);
800 s
.WriteString(m_value
);
804 bool wxVariantDataString::Read(wxInputStream
& str
)
806 wxTextInputStream
s(str
);
808 m_value
= s
.ReadString();
811 #endif // wxUSE_STREAMS
813 bool wxVariantDataString::Read(wxString
& str
)
819 #if defined(__BORLANDC__) && defined(__WIN16__)
820 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
822 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
831 class wxVariantDataTime
: public wxVariantData
833 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
835 wxVariantDataTime() { }
836 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
838 inline wxTime
GetValue() const { return m_value
; }
839 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
841 virtual void Copy(wxVariantData
& data
);
842 virtual bool Eq(wxVariantData
& data
) const;
843 #if wxUSE_STD_IOSTREAM
844 virtual bool Write(ostream
& str
) const;
846 virtual bool Write(wxString
& str
) const;
847 #if wxUSE_STD_IOSTREAM
848 virtual bool Read(istream
& str
);
850 virtual bool Read(wxString
& str
);
851 virtual wxString
GetType() const { return wxT("time"); };
852 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
858 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
860 void wxVariantDataTime::Copy(wxVariantData
& data
)
862 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Copy: Can't copy to this type of data") );
864 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
866 otherData
.m_value
= m_value
;
869 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
871 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Eq: argument mismatch") );
873 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
875 return (otherData
.m_value
== m_value
);
878 #if wxUSE_STD_IOSTREAM
879 bool wxVariantDataTime::Write(ostream
& str
) const
883 str
<< (const char*) s
.mb_str();
888 bool wxVariantDataTime::Write(wxString
& str
) const
890 wxChar
*s
= m_value
.FormatTime();
895 #if wxUSE_STD_IOSTREAM
896 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
903 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
913 class wxVariantDataDate
: public wxVariantData
915 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
917 wxVariantDataDate() { }
918 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
920 inline wxDate
GetValue() const { return m_value
; }
921 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
923 virtual void Copy(wxVariantData
& data
);
924 virtual bool Eq(wxVariantData
& data
) const;
925 #if wxUSE_STD_IOSTREAM
926 virtual bool Write(ostream
& str
) const;
928 virtual bool Write(wxString
& str
) const;
929 #if wxUSE_STD_IOSTREAM
930 virtual bool Read(istream
& str
);
932 virtual bool Read(wxString
& str
);
933 virtual wxString
GetType() const { return wxT("date"); };
934 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
940 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
942 void wxVariantDataDate::Copy(wxVariantData
& data
)
944 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Copy: Can't copy to this type of data") );
946 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
948 otherData
.m_value
= m_value
;
951 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
953 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Eq: argument mismatch") );
955 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
957 return (otherData
.m_value
== m_value
);
960 #if wxUSE_STD_IOSTREAM
961 bool wxVariantDataDate::Write(ostream
& str
) const
965 str
<< (const char*) s
.mb_str();
970 bool wxVariantDataDate::Write(wxString
& str
) const
972 str
= m_value
.FormatDate();
976 #if wxUSE_STD_IOSTREAM
977 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
984 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
993 * wxVariantDataVoidPtr
996 class wxVariantDataVoidPtr
: public wxVariantData
998 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
1000 wxVariantDataVoidPtr() { }
1001 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1003 inline void* GetValue() const { return m_value
; }
1004 inline void SetValue(void* value
) { m_value
= value
; }
1006 virtual void Copy(wxVariantData
& data
);
1007 virtual bool Eq(wxVariantData
& data
) const;
1008 #if wxUSE_STD_IOSTREAM
1009 virtual bool Write(ostream
& str
) const;
1011 virtual bool Write(wxString
& str
) const;
1012 #if wxUSE_STD_IOSTREAM
1013 virtual bool Read(istream
& str
);
1015 virtual bool Read(wxString
& str
);
1016 virtual wxString
GetType() const { return wxT("void*"); };
1017 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1023 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1025 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1027 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1029 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1031 otherData
.m_value
= m_value
;
1034 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1036 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1038 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1040 return (otherData
.m_value
== m_value
);
1043 #if wxUSE_STD_IOSTREAM
1044 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1048 str
<< (const char*) s
.mb_str();
1053 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1055 str
.Printf(wxT("%ld"), (long) m_value
);
1059 #if wxUSE_STD_IOSTREAM
1060 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1067 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1078 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1080 // Construction & destruction
1081 wxVariant::wxVariant()
1083 m_data
= (wxVariantData
*) NULL
;
1086 wxVariant::wxVariant(double val
, const wxString
& name
)
1088 m_data
= new wxVariantDataReal(val
);
1092 wxVariant::wxVariant(long val
, const wxString
& name
)
1094 m_data
= new wxVariantDataLong(val
);
1099 wxVariant::wxVariant(bool val
, const wxString
& name
)
1101 m_data
= new wxVariantDataBool(val
);
1106 wxVariant::wxVariant(char val
, const wxString
& name
)
1108 m_data
= new wxVariantDataChar(val
);
1112 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1114 m_data
= new wxVariantDataString(val
);
1118 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1120 m_data
= new wxVariantDataString(wxString(val
));
1124 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1126 m_data
= new wxVariantDataStringList(val
);
1130 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1132 m_data
= new wxVariantDataList(val
);
1137 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1139 m_data
= new wxVariantDataTime(val
);
1143 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1145 m_data
= new wxVariantDataDate(val
);
1150 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1152 m_data
= new wxVariantDataVoidPtr(val
);
1156 wxVariant::wxVariant(const wxVariant
& variant
)
1158 if (!variant
.IsNull())
1160 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1161 variant
.m_data
->Copy(*m_data
);
1164 m_data
= (wxVariantData
*) NULL
;
1165 m_name
= variant
.m_name
;
1168 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1174 wxVariant::~wxVariant()
1180 // Make NULL (i.e. delete the data)
1181 void wxVariant::MakeNull()
1187 // Generic operators
1189 void wxVariant::operator= (const wxVariant
& variant
)
1191 if (variant
.IsNull())
1197 if (IsNull() || (GetType() != variant
.GetType()))
1201 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1203 // GetData()->Copy(* variant.GetData());
1204 variant
.GetData()->Copy(* GetData());
1207 // Assignment using data, e.g.
1208 // myVariant = new wxStringVariantData("hello")
1209 void wxVariant::operator= (wxVariantData
* variantData
)
1212 m_data
= variantData
;
1215 bool wxVariant::operator== (const wxVariant
& variant
) const
1217 if (IsNull() || variant
.IsNull())
1218 return (IsNull() == variant
.IsNull());
1220 return (GetData()->Eq(* variant
.GetData()));
1223 bool wxVariant::operator!= (const wxVariant
& variant
) const
1225 return (!(*this == variant
));
1229 // Specific operators
1230 bool wxVariant::operator== (double value
) const
1233 if (!Convert(&thisValue
))
1236 return (value
== thisValue
);
1239 bool wxVariant::operator!= (double value
) const
1241 return (!((*this) == value
));
1244 void wxVariant::operator= (double value
)
1246 if (GetType() == wxT("double"))
1248 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1254 m_data
= new wxVariantDataReal(value
);
1258 bool wxVariant::operator== (long value
) const
1261 if (!Convert(&thisValue
))
1264 return (value
== thisValue
);
1267 bool wxVariant::operator!= (long value
) const
1269 return (!((*this) == value
));
1272 void wxVariant::operator= (long value
)
1274 if (GetType() == wxT("long"))
1276 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1282 m_data
= new wxVariantDataLong(value
);
1286 bool wxVariant::operator== (char value
) const
1289 if (!Convert(&thisValue
))
1292 return (value
== thisValue
);
1295 bool wxVariant::operator!= (char value
) const
1297 return (!((*this) == value
));
1300 void wxVariant::operator= (char value
)
1302 if (GetType() == wxT("char"))
1304 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1310 m_data
= new wxVariantDataChar(value
);
1315 bool wxVariant::operator== (bool value
) const
1318 if (!Convert(&thisValue
))
1321 return (value
== thisValue
);
1324 bool wxVariant::operator!= (bool value
) const
1326 return (!((*this) == value
));
1329 void wxVariant::operator= (bool value
)
1331 if (GetType() == wxT("bool"))
1333 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1339 m_data
= new wxVariantDataBool(value
);
1344 bool wxVariant::operator== (const wxString
& value
) const
1347 if (!Convert(&thisValue
))
1350 return (value
== thisValue
);
1353 bool wxVariant::operator!= (const wxString
& value
) const
1355 return (!((*this) == value
));
1358 void wxVariant::operator= (const wxString
& value
)
1360 if (GetType() == wxT("string"))
1362 ((wxVariantDataString
*)GetData())->SetValue(value
);
1368 m_data
= new wxVariantDataString(value
);
1372 void wxVariant::operator= (const wxChar
* value
)
1374 if (GetType() == wxT("string"))
1376 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1382 m_data
= new wxVariantDataString(wxString(value
));
1386 bool wxVariant::operator== (const wxStringList
& value
) const
1388 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1390 wxVariantDataStringList
other(value
);
1391 return (m_data
->Eq(other
));
1394 bool wxVariant::operator!= (const wxStringList
& value
) const
1396 return (!((*this) == value
));
1399 void wxVariant::operator= (const wxStringList
& value
)
1401 if (GetType() == wxT("stringlist"))
1403 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1409 m_data
= new wxVariantDataStringList(value
);
1413 bool wxVariant::operator== (const wxList
& value
) const
1415 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1417 wxVariantDataList
other(value
);
1418 return (m_data
->Eq(other
));
1421 bool wxVariant::operator!= (const wxList
& value
) const
1423 return (!((*this) == value
));
1426 void wxVariant::operator= (const wxList
& value
)
1428 if (GetType() == wxT("list"))
1430 ((wxVariantDataList
*)GetData())->SetValue(value
);
1436 m_data
= new wxVariantDataList(value
);
1441 bool wxVariant::operator== (const wxTime
& value
) const
1444 if (!Convert(&thisValue
))
1447 return (value
== thisValue
);
1450 bool wxVariant::operator!= (const wxTime
& value
) const
1452 return (!((*this) == value
));
1455 void wxVariant::operator= (const wxTime
& value
)
1457 if (GetType() == wxT("time"))
1459 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1465 m_data
= new wxVariantDataTime(value
);
1469 bool wxVariant::operator== (const wxDate
& value
) const
1472 if (!Convert(&thisValue
))
1475 return (value
== thisValue
);
1478 bool wxVariant::operator!= (const wxDate
& value
) const
1480 return (!((*this) == value
));
1483 void wxVariant::operator= (const wxDate
& value
)
1485 if (GetType() == wxT("date"))
1487 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1493 m_data
= new wxVariantDataDate(value
);
1498 bool wxVariant::operator== (void* value
) const
1500 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1503 bool wxVariant::operator!= (void* value
) const
1505 return (!((*this) == value
));
1508 void wxVariant::operator= (void* value
)
1510 if (GetType() == wxT("void*"))
1512 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1518 m_data
= new wxVariantDataVoidPtr(value
);
1522 // Treat a list variant as an array
1523 wxVariant
wxVariant::operator[] (size_t idx
) const
1525 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1527 if (GetType() == wxT("list"))
1529 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1530 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1531 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1533 else if (GetType() == wxT("stringlist"))
1535 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1536 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1538 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1541 return wxNullVariant
;
1544 wxVariant
& wxVariant::operator[] (size_t idx
)
1546 // We can't return a reference to a variant for a string list, since the string
1547 // is actually stored as a char*, not a variant.
1549 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1551 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1552 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1554 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1557 // Return the number of elements in a list
1558 int wxVariant::GetCount() const
1560 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1562 if (GetType() == wxT("list"))
1564 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1565 return data
->GetValue().Number();
1567 else if (GetType() == wxT("stringlist"))
1569 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1570 return data
->GetValue().Number();
1575 wxString
wxVariant::MakeString() const
1580 if (GetData()->Write(str
))
1583 return wxString(wxT(""));
1588 void wxVariant::SetData(wxVariantData
* data
)
1590 if (m_data
) delete m_data
;
1595 // Returns a string representing the type of the variant,
1596 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1597 wxString
wxVariant::GetType() const
1600 return wxString(wxT("null"));
1602 return m_data
->GetType();
1606 bool wxVariant::IsType(const wxString
& type
) const
1608 return (GetType() == type
);
1613 double wxVariant::GetReal() const
1616 if (Convert(& value
))
1620 wxFAIL_MSG(wxT("Could not convert to a real number"));
1625 long wxVariant::GetInteger() const
1628 if (Convert(& value
))
1632 wxFAIL_MSG(wxT("Could not convert to an integer"));
1637 char wxVariant::GetChar() const
1640 if (Convert(& value
))
1644 wxFAIL_MSG(wxT("Could not convert to a char"));
1649 bool wxVariant::GetBool() const
1652 if (Convert(& value
))
1656 wxFAIL_MSG(wxT("Could not convert to a bool"));
1661 wxString
wxVariant::GetString() const
1664 if (Convert(& value
))
1668 wxFAIL_MSG(wxT("Could not convert to a string"));
1669 return wxString("");
1674 wxTime
wxVariant::GetTime() const
1677 if (Convert(& value
))
1681 wxFAIL_MSG(wxT("Could not convert to a time"));
1686 wxDate
wxVariant::GetDate() const
1689 if (Convert(& value
))
1693 wxFAIL_MSG(wxT("Could not convert to a date"));
1699 void* wxVariant::GetVoidPtr() const
1701 wxASSERT( (GetType() == wxT("void*")) );
1703 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1706 wxList
& wxVariant::GetList() const
1708 wxASSERT( (GetType() == wxT("list")) );
1710 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1713 wxStringList
& wxVariant::GetStringList() const
1715 wxASSERT( (GetType() == wxT("stringlist")) );
1717 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1721 void wxVariant::Append(const wxVariant
& value
)
1723 wxList
& list
= GetList();
1725 list
.Append(new wxVariant(value
));
1728 // Insert at front of list
1729 void wxVariant::Insert(const wxVariant
& value
)
1731 wxList
& list
= GetList();
1733 list
.Insert(new wxVariant(value
));
1736 // Returns TRUE if the variant is a member of the list
1737 bool wxVariant::Member(const wxVariant
& value
) const
1739 wxList
& list
= GetList();
1741 wxNode
* node
= list
.First();
1744 wxVariant
* other
= (wxVariant
*) node
->Data();
1745 if (value
== *other
)
1747 node
= node
->Next();
1752 // Deletes the nth element of the list
1753 bool wxVariant::Delete(int item
)
1755 wxList
& list
= GetList();
1757 wxASSERT_MSG( (item
< list
.Number()), wxT("Invalid index to Delete") );
1758 wxNode
* node
= list
.Nth(item
);
1759 wxVariant
* variant
= (wxVariant
*) node
->Data();
1766 void wxVariant::ClearList()
1768 if (!IsNull() && (GetType() == wxT("list")))
1770 ((wxVariantDataList
*) m_data
)->Clear();
1774 if (GetType() != wxT("list"))
1779 m_data
= new wxVariantDataList
;
1784 bool wxVariant::Convert(long* value
) const
1786 wxString
type(GetType());
1787 if (type
== wxT("double"))
1788 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1789 else if (type
== wxT("long"))
1790 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1792 else if (type
== wxT("bool"))
1793 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1795 else if (type
== wxT("string"))
1796 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1803 bool wxVariant::Convert(bool* value
) const
1805 wxString
type(GetType());
1806 if (type
== wxT("double"))
1807 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1808 else if (type
== wxT("long"))
1809 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1811 else if (type
== wxT("bool"))
1812 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1814 else if (type
== wxT("string"))
1816 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1818 if (val
== wxT("true") || val
== wxT("yes"))
1820 else if (val
== wxT("false") || val
== wxT("no"))
1831 bool wxVariant::Convert(double* value
) const
1833 wxString
type(GetType());
1834 if (type
== wxT("double"))
1835 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1836 else if (type
== wxT("long"))
1837 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1839 else if (type
== wxT("bool"))
1840 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1842 else if (type
== wxT("string"))
1843 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1850 bool wxVariant::Convert(char* value
) const
1852 wxString
type(GetType());
1853 if (type
== wxT("char"))
1854 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1855 else if (type
== wxT("long"))
1856 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1858 else if (type
== wxT("bool"))
1859 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1867 bool wxVariant::Convert(wxString
* value
) const
1869 *value
= MakeString();
1874 bool wxVariant::Convert(wxTime
* value
) const
1876 wxString
type(GetType());
1877 if (type
== wxT("time"))
1878 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1879 else if (type
== wxT("date"))
1880 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1887 bool wxVariant::Convert(wxDate
* value
) const
1889 wxString
type(GetType());
1890 if (type
== wxT("date"))
1891 *value
= ((wxVariantDataDate
*)GetData())->GetValue();