1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "variant.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_IOSTREAMH && wxUSE_STD_IOSTREAM
29 #include "wx/stream.h"
30 #include "wx/string.h"
31 #include "wx/variant.h"
33 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
35 wxVariant WXDLLEXPORT wxNullVariant
;
41 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
43 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
45 wxVariantDataList() {}
46 wxVariantDataList(const wxList
& list
);
49 wxList
& GetValue() const { return (wxList
&) m_value
; }
50 void SetValue(const wxList
& value
) ;
52 virtual void Copy(wxVariantData
& data
);
53 virtual bool Eq(wxVariantData
& data
) const;
54 #if wxUSE_STD_IOSTREAM
55 virtual bool Write(ostream
& str
) const;
57 virtual bool Write(wxString
& str
) const;
58 #if wxUSE_STD_IOSTREAM
59 virtual bool Read(istream
& str
);
61 virtual bool Read(wxString
& str
);
62 virtual wxString
GetType() const { return _T("list"); };
70 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
72 wxVariantDataList::wxVariantDataList(const wxList
& list
)
77 wxVariantDataList::~wxVariantDataList()
82 void wxVariantDataList::SetValue(const wxList
& value
)
85 wxNode
* node
= value
.First();
88 wxVariant
* var
= (wxVariant
*) node
->Data();
89 m_value
.Append(new wxVariant(*var
));
94 void wxVariantDataList::Clear()
96 wxNode
* node
= m_value
.First();
99 wxVariant
* var
= (wxVariant
*) node
->Data();
106 void wxVariantDataList::Copy(wxVariantData
& data
)
108 wxASSERT_MSG( (data
.GetType() == _T("list")), _T("wxVariantDataList::Copy: Can't copy to this type of data") );
110 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
113 wxNode
* node
= m_value
.First();
116 wxVariant
* var
= (wxVariant
*) node
->Data();
117 listData
.m_value
.Append(new wxVariant(*var
));
122 bool wxVariantDataList::Eq(wxVariantData
& data
) const
124 wxASSERT_MSG( (data
.GetType() == _T("list")), _T("wxVariantDataList::Eq: argument mismatch") );
126 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
127 wxNode
* node1
= m_value
.First();
128 wxNode
* node2
= listData
.GetValue().First();
129 while (node1
&& node2
)
131 wxVariant
* var1
= (wxVariant
*) node1
->Data();
132 wxVariant
* var2
= (wxVariant
*) node2
->Data();
133 if ((*var1
) != (*var2
))
135 node1
= node1
->Next();
136 node2
= node2
->Next();
138 if (node1
|| node2
) return FALSE
;
142 #if wxUSE_STD_IOSTREAM
143 bool wxVariantDataList::Write(ostream
& str
) const
147 str
<< (const char*) s
.mb_str();
152 bool wxVariantDataList::Write(wxString
& str
) const
155 wxNode
* node
= m_value
.First();
158 wxVariant
* var
= (wxVariant
*) node
->Data();
159 if (node
!= m_value
.First())
162 str
+= var
->MakeString();
169 #if wxUSE_STD_IOSTREAM
170 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
172 wxFAIL_MSG(_T("Unimplemented"));
178 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
180 wxFAIL_MSG(_T("Unimplemented"));
186 * wxVariantDataStringList
189 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
191 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
193 wxVariantDataStringList() {}
194 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
196 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
197 void SetValue(const wxStringList
& value
);
199 virtual void Copy(wxVariantData
& data
);
200 virtual bool Eq(wxVariantData
& data
) const;
201 #if wxUSE_STD_IOSTREAM
202 virtual bool Write(ostream
& str
) const;
204 virtual bool Write(wxString
& str
) const;
205 #if wxUSE_STD_IOSTREAM
206 virtual bool Read(istream
& str
);
208 virtual bool Read(wxString
& str
);
209 virtual wxString
GetType() const { return _T("stringlist"); };
212 wxStringList m_value
;
215 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
217 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
222 void wxVariantDataStringList::Copy(wxVariantData
& data
)
224 wxASSERT_MSG( (data
.GetType() == _T("stringlist")), _T("wxVariantDataStringList::Copy: Can't copy to this type of data") );
226 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
228 listData
.m_value
= m_value
;
231 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
233 wxASSERT_MSG( (data
.GetType() == _T("stringlist")), _T("wxVariantDataStringList::Eq: argument mismatch") );
235 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
236 wxNode
* node1
= m_value
.First();
237 wxNode
* node2
= listData
.GetValue().First();
238 while (node1
&& node2
)
240 wxString
str1 ((wxChar
*) node1
->Data());
241 wxString
str2 ((wxChar
*) node2
->Data());
244 node1
= node1
->Next();
245 node2
= node2
->Next();
247 if (node1
|| node2
) return FALSE
;
251 #if wxUSE_STD_IOSTREAM
252 bool wxVariantDataStringList::Write(ostream
& str
) const
256 str
<< (const char*) s
.mb_str();
261 bool wxVariantDataStringList::Write(wxString
& str
) const
264 wxNode
* node
= m_value
.First();
267 wxChar
* s
= (wxChar
*) node
->Data();
268 if (node
!= m_value
.First())
277 #if wxUSE_STD_IOSTREAM
278 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
280 wxFAIL_MSG(_T("Unimplemented"));
286 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
288 wxFAIL_MSG(_T("Unimplemented"));
297 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
299 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
301 wxVariantDataLong() { m_value
= 0; }
302 wxVariantDataLong(long value
) { m_value
= value
; }
304 inline long GetValue() const { return m_value
; }
305 inline void SetValue(long value
) { m_value
= value
; }
307 virtual void Copy(wxVariantData
& data
);
308 virtual bool Eq(wxVariantData
& data
) const;
310 virtual bool Read(wxString
& str
);
311 virtual bool Write(wxString
& str
) const;
312 #if wxUSE_STD_IOSTREAM
313 virtual bool Read(istream
& str
);
314 virtual bool Write(ostream
& str
) const;
317 virtual bool Read(wxInputStream
& str
);
318 virtual bool Write(wxOutputStream
&str
) const;
319 #endif // wxUSE_STREAMS
321 virtual wxString
GetType() const { return _T("long"); };
327 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
329 void wxVariantDataLong::Copy(wxVariantData
& data
)
331 wxASSERT_MSG( (data
.GetType() == _T("long")), _T("wxVariantDataLong::Copy: Can't copy to this type of data") );
333 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
335 otherData
.m_value
= m_value
;
338 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
340 wxASSERT_MSG( (data
.GetType() == _T("long")), _T("wxVariantDataLong::Eq: argument mismatch") );
342 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
344 return (otherData
.m_value
== m_value
);
347 #if wxUSE_STD_IOSTREAM
348 bool wxVariantDataLong::Write(ostream
& str
) const
352 str
<< (const char*) s
.mb_str();
357 bool wxVariantDataLong::Write(wxString
& str
) const
359 str
.Printf(_T("%ld"), m_value
);
363 #if wxUSE_STD_IOSTREAM
364 bool wxVariantDataLong::Read(istream
& str
)
372 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
378 bool wxVariantDataLong::Read(wxInputStream
& str
)
383 #endif // wxUSE_STREAMS
385 bool wxVariantDataLong::Read(wxString
& str
)
387 m_value
= wxAtol((const wxChar
*) str
);
395 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
397 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
399 wxVariantDataReal() { m_value
= 0.0; }
400 wxVariantDataReal(double value
) { m_value
= value
; }
402 inline double GetValue() const { return m_value
; }
403 inline void SetValue(double value
) { m_value
= value
; }
405 virtual void Copy(wxVariantData
& data
);
406 virtual bool Eq(wxVariantData
& data
) const;
407 virtual bool Read(wxString
& str
);
408 #if wxUSE_STD_IOSTREAM
409 virtual bool Write(ostream
& str
) const;
411 virtual bool Write(wxString
& str
) const;
412 #if wxUSE_STD_IOSTREAM
413 virtual bool Read(istream
& str
);
416 virtual bool Read(wxInputStream
& str
);
417 virtual bool Write(wxOutputStream
&str
) const;
418 #endif // wxUSE_STREAMS
419 virtual wxString
GetType() const { return _T("double"); };
425 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
427 void wxVariantDataReal::Copy(wxVariantData
& data
)
429 wxASSERT_MSG( (data
.GetType() == _T("double")), _T("wxVariantDataReal::Copy: Can't copy to this type of data") );
431 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
433 otherData
.m_value
= m_value
;
436 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
438 wxASSERT_MSG( (data
.GetType() == _T("double")), _T("wxVariantDataReal::Eq: argument mismatch") );
440 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
442 return (otherData
.m_value
== m_value
);
445 #if wxUSE_STD_IOSTREAM
446 bool wxVariantDataReal::Write(ostream
& str
) const
450 str
<< (const char*) s
.mb_str();
455 bool wxVariantDataReal::Write(wxString
& str
) const
457 str
.Printf(_T("%.4f"), m_value
);
461 #if wxUSE_STD_IOSTREAM
462 bool wxVariantDataReal::Read(istream
& str
)
470 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
476 bool wxVariantDataReal::Read(wxInputStream
& str
)
478 str
>> (float&)m_value
;
481 #endif // wxUSE_STREAMS
483 bool wxVariantDataReal::Read(wxString
& str
)
485 m_value
= wxAtof((const wxChar
*) str
);
494 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
496 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
498 wxVariantDataBool() { m_value
= 0; }
499 wxVariantDataBool(bool value
) { m_value
= value
; }
501 inline bool GetValue() const { return m_value
; }
502 inline void SetValue(bool value
) { m_value
= value
; }
504 virtual void Copy(wxVariantData
& data
);
505 virtual bool Eq(wxVariantData
& data
) const;
506 #if wxUSE_STD_IOSTREAM
507 virtual bool Write(ostream
& str
) const;
509 virtual bool Write(wxString
& str
) const;
510 virtual bool Read(wxString
& str
);
511 #if wxUSE_STD_IOSTREAM
512 virtual bool Read(istream
& str
);
515 virtual bool Read(wxInputStream
& str
);
516 virtual bool Write(wxOutputStream
& str
) const;
517 #endif // wxUSE_STREAMS
518 virtual wxString
GetType() const { return _T("bool"); };
524 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
526 void wxVariantDataBool::Copy(wxVariantData
& data
)
528 wxASSERT_MSG( (data
.GetType() == _T("bool")), _T("wxVariantDataBool::Copy: Can't copy to this type of data") );
530 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
532 otherData
.m_value
= m_value
;
535 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
537 wxASSERT_MSG( (data
.GetType() == _T("bool")), _T("wxVariantDataBool::Eq: argument mismatch") );
539 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
541 return (otherData
.m_value
== m_value
);
544 #if wxUSE_STD_IOSTREAM
545 bool wxVariantDataBool::Write(ostream
& str
) const
549 str
<< (const char*) s
.mb_str();
554 bool wxVariantDataBool::Write(wxString
& str
) const
556 str
.Printf(_T("%d"), (int) m_value
);
560 #if wxUSE_STD_IOSTREAM
561 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
563 wxFAIL_MSG(_T("Unimplemented"));
564 // str >> (long) m_value;
570 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
572 str
<< (char)m_value
;
576 bool wxVariantDataBool::Read(wxInputStream
& str
)
578 str
>> (char&)m_value
;
581 #endif // wxUSE_STREAMS
583 bool wxVariantDataBool::Read(wxString
& str
)
585 m_value
= (wxAtol((const wxChar
*) str
) != 0);
594 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
596 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
598 wxVariantDataChar() { m_value
= 0; }
599 wxVariantDataChar(char value
) { m_value
= value
; }
601 inline char GetValue() const { return m_value
; }
602 inline void SetValue(char value
) { m_value
= value
; }
604 virtual void Copy(wxVariantData
& data
);
605 virtual bool Eq(wxVariantData
& data
) const;
606 #if wxUSE_STD_IOSTREAM
607 virtual bool Read(istream
& str
);
608 virtual bool Write(ostream
& str
) const;
610 virtual bool Read(wxString
& str
);
611 virtual bool Write(wxString
& str
) const;
613 virtual bool Read(wxInputStream
& str
);
614 virtual bool Write(wxOutputStream
& str
) const;
615 #endif // wxUSE_STREAMS
616 virtual wxString
GetType() const { return _T("char"); };
622 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
624 void wxVariantDataChar::Copy(wxVariantData
& data
)
626 wxASSERT_MSG( (data
.GetType() == _T("char")), _T("wxVariantDataChar::Copy: Can't copy to this type of data") );
628 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
630 otherData
.m_value
= m_value
;
633 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
635 wxASSERT_MSG( (data
.GetType() == _T("char")), _T("wxVariantDataChar::Eq: argument mismatch") );
637 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
639 return (otherData
.m_value
== m_value
);
642 #if wxUSE_STD_IOSTREAM
643 bool wxVariantDataChar::Write(ostream
& str
) const
647 str
<< (const char*) s
.mb_str();
652 bool wxVariantDataChar::Write(wxString
& str
) const
654 str
.Printf(_T("%c"), m_value
);
658 #if wxUSE_STD_IOSTREAM
659 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
661 wxFAIL_MSG(_T("Unimplemented"));
668 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
674 bool wxVariantDataChar::Read(wxInputStream
& str
)
679 #endif // wxUSE_STREAMS
681 bool wxVariantDataChar::Read(wxString
& str
)
683 m_value
= str
[(size_t)0];
688 * wxVariantDataString
691 #if defined(__BORLANDC__) && defined(__WIN16__)
692 // Change name because of truncation
693 #define wxVariantDataString wxVariantStringData
696 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
698 #if defined(__BORLANDC__) && defined(__WIN16__)
699 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
701 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
704 wxVariantDataString() { }
705 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
707 inline wxString
GetValue() const { return m_value
; }
708 inline void SetValue(const wxString
& value
) { m_value
= value
; }
710 virtual void Copy(wxVariantData
& data
);
711 virtual bool Eq(wxVariantData
& data
) const;
712 #if wxUSE_STD_IOSTREAM
713 virtual bool Write(ostream
& str
) const;
715 virtual bool Read(wxString
& str
);
716 virtual bool Write(wxString
& str
) const;
717 #if wxUSE_STD_IOSTREAM
718 virtual bool Read(istream
& str
);
721 virtual bool Read(wxInputStream
& str
);
722 virtual bool Write(wxOutputStream
& str
) const;
723 #endif // wxUSE_STREAMS
724 virtual wxString
GetType() const { return _T("string"); };
730 void wxVariantDataString::Copy(wxVariantData
& data
)
732 wxASSERT_MSG( (data
.GetType() == _T("string")), _T("wxVariantDataString::Copy: Can't copy to this type of data") );
734 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
736 otherData
.m_value
= m_value
;
739 bool wxVariantDataString::Eq(wxVariantData
& data
) const
741 wxASSERT_MSG( (data
.GetType() == _T("string")), _T("wxVariantDataString::Eq: argument mismatch") );
743 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
745 return (otherData
.m_value
== m_value
);
748 #if wxUSE_STD_IOSTREAM
749 bool wxVariantDataString::Write(ostream
& str
) const
751 str
<< (const char*) m_value
.mb_str();
756 bool wxVariantDataString::Write(wxString
& str
) const
762 #if wxUSE_STD_IOSTREAM
763 bool wxVariantDataString::Read(istream
& str
)
771 bool wxVariantDataString::Write(wxOutputStream
& str
) const
773 // why doesn't wxOutputStream::operator<< take "const wxString&"
774 str
<< (const char*) m_value
.mb_str();
778 bool wxVariantDataString::Read(wxInputStream
& str
)
783 #endif // wxUSE_STREAMS
785 bool wxVariantDataString::Read(wxString
& str
)
791 #if defined(__BORLANDC__) && defined(__WIN16__)
792 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
794 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
803 class wxVariantDataTime
: public wxVariantData
805 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
807 wxVariantDataTime() { }
808 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
810 inline wxTime
GetValue() const { return m_value
; }
811 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
813 virtual void Copy(wxVariantData
& data
);
814 virtual bool Eq(wxVariantData
& data
) const;
815 #if wxUSE_STD_IOSTREAM
816 virtual bool Write(ostream
& str
) const;
818 virtual bool Write(wxString
& str
) const;
819 #if wxUSE_STD_IOSTREAM
820 virtual bool Read(istream
& str
);
822 virtual bool Read(wxString
& str
);
823 virtual wxString
GetType() const { return _T("time"); };
824 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
830 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
832 void wxVariantDataTime::Copy(wxVariantData
& data
)
834 wxASSERT_MSG( (data
.GetType() == _T("time")), _T("wxVariantDataTime::Copy: Can't copy to this type of data") );
836 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
838 otherData
.m_value
= m_value
;
841 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
843 wxASSERT_MSG( (data
.GetType() == _T("time")), _T("wxVariantDataTime::Eq: argument mismatch") );
845 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
847 return (otherData
.m_value
== m_value
);
850 #if wxUSE_STD_IOSTREAM
851 bool wxVariantDataTime::Write(ostream
& str
) const
855 str
<< (const char*) s
.mb_str();
860 bool wxVariantDataTime::Write(wxString
& str
) const
862 wxChar
*s
= m_value
.FormatTime();
867 #if wxUSE_STD_IOSTREAM
868 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
875 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
885 class wxVariantDataDate
: public wxVariantData
887 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
889 wxVariantDataDate() { }
890 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
892 inline wxDate
GetValue() const { return m_value
; }
893 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
895 virtual void Copy(wxVariantData
& data
);
896 virtual bool Eq(wxVariantData
& data
) const;
897 #if wxUSE_STD_IOSTREAM
898 virtual bool Write(ostream
& str
) const;
900 virtual bool Write(wxString
& str
) const;
901 #if wxUSE_STD_IOSTREAM
902 virtual bool Read(istream
& str
);
904 virtual bool Read(wxString
& str
);
905 virtual wxString
GetType() const { return _T("date"); };
906 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
912 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
914 void wxVariantDataDate::Copy(wxVariantData
& data
)
916 wxASSERT_MSG( (data
.GetType() == _T("date")), _T("wxVariantDataDate::Copy: Can't copy to this type of data") );
918 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
920 otherData
.m_value
= m_value
;
923 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
925 wxASSERT_MSG( (data
.GetType() == _T("date")), _T("wxVariantDataDate::Eq: argument mismatch") );
927 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
929 return (otherData
.m_value
== m_value
);
932 #if wxUSE_STD_IOSTREAM
933 bool wxVariantDataDate::Write(ostream
& str
) const
937 str
<< (const char*) s
.mb_str();
942 bool wxVariantDataDate::Write(wxString
& str
) const
944 str
= m_value
.FormatDate();
948 #if wxUSE_STD_IOSTREAM
949 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
956 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
965 * wxVariantDataVoidPtr
968 class wxVariantDataVoidPtr
: public wxVariantData
970 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
972 wxVariantDataVoidPtr() { }
973 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
975 inline void* GetValue() const { return m_value
; }
976 inline void SetValue(void* value
) { m_value
= value
; }
978 virtual void Copy(wxVariantData
& data
);
979 virtual bool Eq(wxVariantData
& data
) const;
980 #if wxUSE_STD_IOSTREAM
981 virtual bool Write(ostream
& str
) const;
983 virtual bool Write(wxString
& str
) const;
984 #if wxUSE_STD_IOSTREAM
985 virtual bool Read(istream
& str
);
987 virtual bool Read(wxString
& str
);
988 virtual wxString
GetType() const { return _T("void*"); };
989 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
995 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
997 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
999 wxASSERT_MSG( (data
.GetType() == _T("void*")), _T("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1001 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1003 otherData
.m_value
= m_value
;
1006 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1008 wxASSERT_MSG( (data
.GetType() == _T("void*")), _T("wxVariantDataVoidPtr::Eq: argument mismatch") );
1010 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1012 return (otherData
.m_value
== m_value
);
1015 #if wxUSE_STD_IOSTREAM
1016 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1020 str
<< (const char*) s
.mb_str();
1025 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1027 str
.Printf(_T("%ld"), (long) m_value
);
1031 #if wxUSE_STD_IOSTREAM
1032 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1039 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1050 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1052 // Construction & destruction
1053 wxVariant::wxVariant()
1055 m_data
= (wxVariantData
*) NULL
;
1058 wxVariant::wxVariant(double val
, const wxString
& name
)
1060 m_data
= new wxVariantDataReal(val
);
1064 wxVariant::wxVariant(long val
, const wxString
& name
)
1066 m_data
= new wxVariantDataLong(val
);
1071 wxVariant::wxVariant(bool val
, const wxString
& name
)
1073 m_data
= new wxVariantDataBool(val
);
1078 wxVariant::wxVariant(char val
, const wxString
& name
)
1080 m_data
= new wxVariantDataChar(val
);
1084 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1086 m_data
= new wxVariantDataString(val
);
1090 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1092 m_data
= new wxVariantDataString(wxString(val
));
1096 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1098 m_data
= new wxVariantDataStringList(val
);
1102 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1104 m_data
= new wxVariantDataList(val
);
1109 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1111 m_data
= new wxVariantDataTime(val
);
1115 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1117 m_data
= new wxVariantDataDate(val
);
1122 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1124 m_data
= new wxVariantDataVoidPtr(val
);
1128 wxVariant::wxVariant(const wxVariant
& variant
)
1130 if (!variant
.IsNull())
1132 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1133 variant
.m_data
->Copy(*m_data
);
1136 m_data
= (wxVariantData
*) NULL
;
1137 m_name
= variant
.m_name
;
1140 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1146 wxVariant::~wxVariant()
1152 // Make NULL (i.e. delete the data)
1153 void wxVariant::MakeNull()
1159 // Generic operators
1161 void wxVariant::operator= (const wxVariant
& variant
)
1163 if (variant
.IsNull())
1169 if (IsNull() || (GetType() != variant
.GetType()))
1173 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1175 // GetData()->Copy(* variant.GetData());
1176 variant
.GetData()->Copy(* GetData());
1179 // Assignment using data, e.g.
1180 // myVariant = new wxStringVariantData("hello")
1181 void wxVariant::operator= (wxVariantData
* variantData
)
1184 m_data
= variantData
;
1187 bool wxVariant::operator== (const wxVariant
& variant
) const
1189 if (IsNull() || variant
.IsNull())
1190 return (IsNull() == variant
.IsNull());
1192 return (GetData()->Eq(* variant
.GetData()));
1195 bool wxVariant::operator!= (const wxVariant
& variant
) const
1197 return (!(*this == variant
));
1201 // Specific operators
1202 bool wxVariant::operator== (double value
) const
1205 if (!Convert(&thisValue
))
1208 return (value
== thisValue
);
1211 bool wxVariant::operator!= (double value
) const
1213 return (!((*this) == value
));
1216 void wxVariant::operator= (double value
)
1218 if (GetType() == _T("double"))
1220 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1226 m_data
= new wxVariantDataReal(value
);
1230 bool wxVariant::operator== (long value
) const
1233 if (!Convert(&thisValue
))
1236 return (value
== thisValue
);
1239 bool wxVariant::operator!= (long value
) const
1241 return (!((*this) == value
));
1244 void wxVariant::operator= (long value
)
1246 if (GetType() == _T("long"))
1248 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1254 m_data
= new wxVariantDataLong(value
);
1258 bool wxVariant::operator== (char value
) const
1261 if (!Convert(&thisValue
))
1264 return (value
== thisValue
);
1267 bool wxVariant::operator!= (char value
) const
1269 return (!((*this) == value
));
1272 void wxVariant::operator= (char value
)
1274 if (GetType() == _T("char"))
1276 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1282 m_data
= new wxVariantDataChar(value
);
1287 bool wxVariant::operator== (bool value
) const
1290 if (!Convert(&thisValue
))
1293 return (value
== thisValue
);
1296 bool wxVariant::operator!= (bool value
) const
1298 return (!((*this) == value
));
1301 void wxVariant::operator= (bool value
)
1303 if (GetType() == _T("bool"))
1305 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1311 m_data
= new wxVariantDataBool(value
);
1316 bool wxVariant::operator== (const wxString
& value
) const
1319 if (!Convert(&thisValue
))
1322 return (value
== thisValue
);
1325 bool wxVariant::operator!= (const wxString
& value
) const
1327 return (!((*this) == value
));
1330 void wxVariant::operator= (const wxString
& value
)
1332 if (GetType() == _T("string"))
1334 ((wxVariantDataString
*)GetData())->SetValue(value
);
1340 m_data
= new wxVariantDataString(value
);
1344 void wxVariant::operator= (const wxChar
* value
)
1346 if (GetType() == _T("string"))
1348 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1354 m_data
= new wxVariantDataString(wxString(value
));
1358 bool wxVariant::operator== (const wxStringList
& value
) const
1360 wxASSERT_MSG( (GetType() == _T("stringlist")), _T("Invalid type for == operator") );
1362 wxVariantDataStringList
other(value
);
1363 return (m_data
->Eq(other
));
1366 bool wxVariant::operator!= (const wxStringList
& value
) const
1368 return (!((*this) == value
));
1371 void wxVariant::operator= (const wxStringList
& value
)
1373 if (GetType() == _T("stringlist"))
1375 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1381 m_data
= new wxVariantDataStringList(value
);
1385 bool wxVariant::operator== (const wxList
& value
) const
1387 wxASSERT_MSG( (GetType() == _T("list")), _T("Invalid type for == operator") );
1389 wxVariantDataList
other(value
);
1390 return (m_data
->Eq(other
));
1393 bool wxVariant::operator!= (const wxList
& value
) const
1395 return (!((*this) == value
));
1398 void wxVariant::operator= (const wxList
& value
)
1400 if (GetType() == _T("list"))
1402 ((wxVariantDataList
*)GetData())->SetValue(value
);
1408 m_data
= new wxVariantDataList(value
);
1413 bool wxVariant::operator== (const wxTime
& value
) const
1416 if (!Convert(&thisValue
))
1419 return (value
== thisValue
);
1422 bool wxVariant::operator!= (const wxTime
& value
) const
1424 return (!((*this) == value
));
1427 void wxVariant::operator= (const wxTime
& value
)
1429 if (GetType() == _T("time"))
1431 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1437 m_data
= new wxVariantDataTime(value
);
1441 bool wxVariant::operator== (const wxDate
& value
) const
1444 if (!Convert(&thisValue
))
1447 return (value
== thisValue
);
1450 bool wxVariant::operator!= (const wxDate
& value
) const
1452 return (!((*this) == value
));
1455 void wxVariant::operator= (const wxDate
& value
)
1457 if (GetType() == _T("date"))
1459 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1465 m_data
= new wxVariantDataDate(value
);
1470 bool wxVariant::operator== (void* value
) const
1472 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1475 bool wxVariant::operator!= (void* value
) const
1477 return (!((*this) == value
));
1480 void wxVariant::operator= (void* value
)
1482 if (GetType() == _T("void*"))
1484 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1490 m_data
= new wxVariantDataVoidPtr(value
);
1494 // Treat a list variant as an array
1495 wxVariant
wxVariant::operator[] (size_t idx
) const
1497 wxASSERT_MSG( (GetType() == _T("list") || GetType() == _T("stringlist")), _T("Invalid type for array operator") );
1499 if (GetType() == _T("list"))
1501 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1502 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), _T("Invalid index for array") );
1503 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1505 else if (GetType() == _T("stringlist"))
1507 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1508 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), _T("Invalid index for array") );
1510 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1513 return wxNullVariant
;
1516 wxVariant
& wxVariant::operator[] (size_t idx
)
1518 // We can't return a reference to a variant for a string list, since the string
1519 // is actually stored as a char*, not a variant.
1521 wxASSERT_MSG( (GetType() == _T("list")), _T("Invalid type for array operator") );
1523 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1524 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), _T("Invalid index for array") );
1526 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1529 // Return the number of elements in a list
1530 int wxVariant::GetCount() const
1532 wxASSERT_MSG( (GetType() == _T("list") || GetType() == _T("stringlist")), _T("Invalid type for GetCount()") );
1534 if (GetType() == _T("list"))
1536 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1537 return data
->GetValue().Number();
1539 else if (GetType() == _T("stringlist"))
1541 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1542 return data
->GetValue().Number();
1547 wxString
wxVariant::MakeString() const
1552 if (GetData()->Write(str
))
1555 return wxString(_T(""));
1560 void wxVariant::SetData(wxVariantData
* data
)
1562 if (m_data
) delete m_data
;
1567 // Returns a string representing the type of the variant,
1568 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1569 wxString
wxVariant::GetType() const
1572 return wxString(_T("null"));
1574 return m_data
->GetType();
1578 bool wxVariant::IsType(const wxString
& type
) const
1580 return (GetType() == type
);
1585 double wxVariant::GetReal() const
1588 if (Convert(& value
))
1592 wxFAIL_MSG(_T("Could not convert to a real number"));
1597 long wxVariant::GetInteger() const
1600 if (Convert(& value
))
1604 wxFAIL_MSG(_T("Could not convert to an integer"));
1609 char wxVariant::GetChar() const
1612 if (Convert(& value
))
1616 wxFAIL_MSG(_T("Could not convert to a char"));
1621 bool wxVariant::GetBool() const
1624 if (Convert(& value
))
1628 wxFAIL_MSG(_T("Could not convert to a bool"));
1633 wxString
wxVariant::GetString() const
1636 if (Convert(& value
))
1640 wxFAIL_MSG(_T("Could not convert to a string"));
1641 return wxString("");
1646 wxTime
wxVariant::GetTime() const
1649 if (Convert(& value
))
1653 wxFAIL_MSG(_T("Could not convert to a time"));
1658 wxDate
wxVariant::GetDate() const
1661 if (Convert(& value
))
1665 wxFAIL_MSG(_T("Could not convert to a date"));
1671 void* wxVariant::GetVoidPtr() const
1673 wxASSERT( (GetType() == _T("void*")) );
1675 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1678 wxList
& wxVariant::GetList() const
1680 wxASSERT( (GetType() == _T("list")) );
1682 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1685 wxStringList
& wxVariant::GetStringList() const
1687 wxASSERT( (GetType() == _T("stringlist")) );
1689 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1693 void wxVariant::Append(const wxVariant
& value
)
1695 wxList
& list
= GetList();
1697 list
.Append(new wxVariant(value
));
1700 // Insert at front of list
1701 void wxVariant::Insert(const wxVariant
& value
)
1703 wxList
& list
= GetList();
1705 list
.Insert(new wxVariant(value
));
1708 // Returns TRUE if the variant is a member of the list
1709 bool wxVariant::Member(const wxVariant
& value
) const
1711 wxList
& list
= GetList();
1713 wxNode
* node
= list
.First();
1716 wxVariant
* other
= (wxVariant
*) node
->Data();
1717 if (value
== *other
)
1719 node
= node
->Next();
1724 // Deletes the nth element of the list
1725 bool wxVariant::Delete(int item
)
1727 wxList
& list
= GetList();
1729 wxASSERT_MSG( (item
< list
.Number()), _T("Invalid index to Delete") );
1730 wxNode
* node
= list
.Nth(item
);
1731 wxVariant
* variant
= (wxVariant
*) node
->Data();
1738 void wxVariant::ClearList()
1740 if (!IsNull() && (GetType() == _T("list")))
1742 ((wxVariantDataList
*) m_data
)->Clear();
1746 if (GetType() != _T("list"))
1751 m_data
= new wxVariantDataList
;
1756 bool wxVariant::Convert(long* value
) const
1758 wxString
type(GetType());
1759 if (type
== _T("double"))
1760 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1761 else if (type
== _T("long"))
1762 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1764 else if (type
== _T("bool"))
1765 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1767 else if (type
== _T("string"))
1768 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1775 bool wxVariant::Convert(bool* value
) const
1777 wxString
type(GetType());
1778 if (type
== _T("double"))
1779 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1780 else if (type
== _T("long"))
1781 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1783 else if (type
== _T("bool"))
1784 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1786 else if (type
== _T("string"))
1788 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1790 if (val
== _T("true") || val
== _T("yes"))
1792 else if (val
== _T("false") || val
== _T("no"))
1803 bool wxVariant::Convert(double* value
) const
1805 wxString
type(GetType());
1806 if (type
== _T("double"))
1807 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1808 else if (type
== _T("long"))
1809 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1811 else if (type
== _T("bool"))
1812 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1814 else if (type
== _T("string"))
1815 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1822 bool wxVariant::Convert(char* value
) const
1824 wxString
type(GetType());
1825 if (type
== _T("char"))
1826 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1827 else if (type
== _T("long"))
1828 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1830 else if (type
== _T("bool"))
1831 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1839 bool wxVariant::Convert(wxString
* value
) const
1841 *value
= MakeString();
1846 bool wxVariant::Convert(wxTime
* value
) const
1848 wxString
type(GetType());
1849 if (type
== _T("time"))
1850 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1851 else if (type
== _T("date"))
1852 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1859 bool wxVariant::Convert(wxDate
* value
) const
1861 wxString
type(GetType());
1862 if (type
== _T("date"))
1863 *value
= ((wxVariantDataDate
*)GetData())->GetValue();