1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/variant.cpp
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/variant.h"
24 #include "wx/string.h"
27 #include "wx/stream.h"
31 #if wxUSE_STD_IOSTREAM
39 #if defined(__MWERKS__) && __MSL__ >= 0x6000
45 #include "wx/txtstrm.h"
48 #include "wx/string.h"
49 #include "wx/tokenzr.h"
51 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
53 wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
59 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
61 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
63 wxVariantDataList() {}
64 wxVariantDataList(const wxList
& list
);
65 virtual ~wxVariantDataList();
67 wxList
& GetValue() { return m_value
; }
68 void SetValue(const wxList
& value
) ;
70 virtual void Copy(wxVariantData
& data
);
71 virtual bool Eq(wxVariantData
& data
) const;
72 #if wxUSE_STD_IOSTREAM
73 virtual bool Write(wxSTD ostream
& str
) const;
75 virtual bool Write(wxString
& str
) const;
76 #if wxUSE_STD_IOSTREAM
77 virtual bool Read(wxSTD istream
& str
);
79 virtual bool Read(wxString
& str
);
80 virtual wxString
GetType() const { return wxT("list"); };
88 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
90 wxVariantDataList::wxVariantDataList(const wxList
& list
)
95 wxVariantDataList::~wxVariantDataList()
100 void wxVariantDataList::SetValue(const wxList
& value
)
103 wxList::compatibility_iterator node
= value
.GetFirst();
106 wxVariant
* var
= (wxVariant
*) node
->GetData();
107 m_value
.Append(new wxVariant(*var
));
108 node
= node
->GetNext();
112 void wxVariantDataList::Clear()
114 wxList::compatibility_iterator node
= m_value
.GetFirst();
117 wxVariant
* var
= (wxVariant
*) node
->GetData();
119 node
= node
->GetNext();
124 void wxVariantDataList::Copy(wxVariantData
& data
)
126 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
128 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
131 wxList::compatibility_iterator node
= m_value
.GetFirst();
134 wxVariant
* var
= (wxVariant
*) node
->GetData();
135 listData
.m_value
.Append(new wxVariant(*var
));
136 node
= node
->GetNext();
140 bool wxVariantDataList::Eq(wxVariantData
& data
) const
142 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
144 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
145 wxList::compatibility_iterator node1
= m_value
.GetFirst();
146 wxList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
147 while (node1
&& node2
)
149 wxVariant
* var1
= (wxVariant
*) node1
->GetData();
150 wxVariant
* var2
= (wxVariant
*) node2
->GetData();
151 if ((*var1
) != (*var2
))
153 node1
= node1
->GetNext();
154 node2
= node2
->GetNext();
156 if (node1
|| node2
) return false;
160 #if wxUSE_STD_IOSTREAM
161 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
165 str
<< (const char*) s
.mb_str();
170 bool wxVariantDataList::Write(wxString
& str
) const
173 wxList::compatibility_iterator node
= m_value
.GetFirst();
176 wxVariant
* var
= (wxVariant
*) node
->GetData();
177 if (node
!= m_value
.GetFirst())
180 str
+= var
->MakeString();
181 node
= node
->GetNext();
187 #if wxUSE_STD_IOSTREAM
188 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
190 wxFAIL_MSG(wxT("Unimplemented"));
196 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
198 wxFAIL_MSG(wxT("Unimplemented"));
202 #if WXWIN_COMPATIBILITY_2_4
205 * wxVariantDataStringList
208 class WXDLLIMPEXP_BASE wxVariantDataStringList
: public wxVariantData
210 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
212 wxVariantDataStringList() {}
213 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
215 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
216 void SetValue(const wxStringList
& value
);
218 virtual void Copy(wxVariantData
& data
);
219 virtual bool Eq(wxVariantData
& data
) const;
220 #if wxUSE_STD_IOSTREAM
221 virtual bool Write(wxSTD ostream
& str
) const;
223 virtual bool Write(wxString
& str
) const;
224 #if wxUSE_STD_IOSTREAM
225 virtual bool Read(wxSTD istream
& str
);
227 virtual bool Read(wxString
& str
);
228 virtual wxString
GetType() const { return wxT("stringlist"); };
231 wxStringList m_value
;
234 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
236 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
241 void wxVariantDataStringList::Copy(wxVariantData
& data
)
243 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
245 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
247 listData
.m_value
= m_value
;
250 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
252 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
254 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
255 wxStringList::compatibility_iterator node1
= m_value
.GetFirst();
256 wxStringList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
257 while (node1
&& node2
)
259 wxString
str1 ( node1
->GetData() );
260 wxString
str2 ( node2
->GetData() );
263 node1
= node1
->GetNext();
264 node2
= node2
->GetNext();
266 if (node1
|| node2
) return false;
270 #if wxUSE_STD_IOSTREAM
271 bool wxVariantDataStringList::Write(wxSTD ostream
& str
) const
275 str
<< (const char*) s
.mb_str();
280 bool wxVariantDataStringList::Write(wxString
& str
) const
283 wxStringList::compatibility_iterator node
= m_value
.GetFirst();
286 const wxChar
* s
= node
->GetData();
287 if (node
!= m_value
.GetFirst())
290 node
= node
->GetNext();
296 #if wxUSE_STD_IOSTREAM
297 bool wxVariantDataStringList::Read(wxSTD istream
& WXUNUSED(str
))
299 wxFAIL_MSG(wxT("Unimplemented"));
305 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
307 wxFAIL_MSG(wxT("Unimplemented"));
318 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
320 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
322 wxVariantDataLong() { m_value
= 0; }
323 wxVariantDataLong(long value
) { m_value
= value
; }
325 inline long GetValue() const { return m_value
; }
326 inline void SetValue(long value
) { m_value
= value
; }
328 virtual void Copy(wxVariantData
& data
);
329 virtual bool Eq(wxVariantData
& data
) const;
331 virtual bool Read(wxString
& str
);
332 virtual bool Write(wxString
& str
) const;
333 #if wxUSE_STD_IOSTREAM
334 virtual bool Read(wxSTD istream
& str
);
335 virtual bool Write(wxSTD ostream
& str
) const;
338 virtual bool Read(wxInputStream
& str
);
339 virtual bool Write(wxOutputStream
&str
) const;
340 #endif // wxUSE_STREAMS
342 virtual wxString
GetType() const { return wxT("long"); };
348 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
350 void wxVariantDataLong::Copy(wxVariantData
& data
)
352 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
354 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
356 otherData
.m_value
= m_value
;
359 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
361 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
363 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
365 return (otherData
.m_value
== m_value
);
368 #if wxUSE_STD_IOSTREAM
369 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
373 str
<< (const char*) s
.mb_str();
378 bool wxVariantDataLong::Write(wxString
& str
) const
380 str
.Printf(wxT("%ld"), m_value
);
384 #if wxUSE_STD_IOSTREAM
385 bool wxVariantDataLong::Read(wxSTD istream
& str
)
393 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
395 wxTextOutputStream
s(str
);
397 s
.Write32((size_t)m_value
);
401 bool wxVariantDataLong::Read(wxInputStream
& str
)
403 wxTextInputStream
s(str
);
404 m_value
= s
.Read32();
407 #endif // wxUSE_STREAMS
409 bool wxVariantDataLong::Read(wxString
& str
)
411 m_value
= wxAtol((const wxChar
*) str
);
419 class WXDLLIMPEXP_BASE wxVariantDataReal
: public wxVariantData
421 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
423 wxVariantDataReal() { m_value
= 0.0; }
424 wxVariantDataReal(double value
) { m_value
= value
; }
426 inline double GetValue() const { return m_value
; }
427 inline void SetValue(double value
) { m_value
= value
; }
429 virtual void Copy(wxVariantData
& data
);
430 virtual bool Eq(wxVariantData
& data
) const;
431 virtual bool Read(wxString
& str
);
432 #if wxUSE_STD_IOSTREAM
433 virtual bool Write(wxSTD ostream
& str
) const;
435 virtual bool Write(wxString
& str
) const;
436 #if wxUSE_STD_IOSTREAM
437 virtual bool Read(wxSTD istream
& str
);
440 virtual bool Read(wxInputStream
& str
);
441 virtual bool Write(wxOutputStream
&str
) const;
442 #endif // wxUSE_STREAMS
443 virtual wxString
GetType() const { return wxT("double"); };
449 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
451 void wxVariantDataReal::Copy(wxVariantData
& data
)
453 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
455 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
457 otherData
.m_value
= m_value
;
460 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
462 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
464 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
466 return wxIsSameDouble(otherData
.m_value
, m_value
);
469 #if wxUSE_STD_IOSTREAM
470 bool wxVariantDataReal::Write(wxSTD ostream
& str
) const
474 str
<< (const char*) s
.mb_str();
479 bool wxVariantDataReal::Write(wxString
& str
) const
481 str
.Printf(wxT("%.14g"), m_value
);
485 #if wxUSE_STD_IOSTREAM
486 bool wxVariantDataReal::Read(wxSTD istream
& str
)
494 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
496 wxTextOutputStream
s(str
);
497 s
.WriteDouble((double)m_value
);
501 bool wxVariantDataReal::Read(wxInputStream
& str
)
503 wxTextInputStream
s(str
);
504 m_value
= (float)s
.ReadDouble();
507 #endif // wxUSE_STREAMS
509 bool wxVariantDataReal::Read(wxString
& str
)
511 m_value
= wxAtof((const wxChar
*) str
);
520 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
522 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
524 wxVariantDataBool() { m_value
= 0; }
525 wxVariantDataBool(bool value
) { m_value
= value
; }
527 inline bool GetValue() const { return m_value
; }
528 inline void SetValue(bool value
) { m_value
= value
; }
530 virtual void Copy(wxVariantData
& data
);
531 virtual bool Eq(wxVariantData
& data
) const;
532 #if wxUSE_STD_IOSTREAM
533 virtual bool Write(wxSTD ostream
& str
) const;
535 virtual bool Write(wxString
& str
) const;
536 virtual bool Read(wxString
& str
);
537 #if wxUSE_STD_IOSTREAM
538 virtual bool Read(wxSTD istream
& str
);
541 virtual bool Read(wxInputStream
& str
);
542 virtual bool Write(wxOutputStream
& str
) const;
543 #endif // wxUSE_STREAMS
544 virtual wxString
GetType() const { return wxT("bool"); };
550 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
552 void wxVariantDataBool::Copy(wxVariantData
& data
)
554 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
556 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
558 otherData
.m_value
= m_value
;
561 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
563 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
565 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
567 return (otherData
.m_value
== m_value
);
570 #if wxUSE_STD_IOSTREAM
571 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
575 str
<< (const char*) s
.mb_str();
580 bool wxVariantDataBool::Write(wxString
& str
) const
582 str
.Printf(wxT("%d"), (int) m_value
);
586 #if wxUSE_STD_IOSTREAM
587 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
589 wxFAIL_MSG(wxT("Unimplemented"));
590 // str >> (long) m_value;
596 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
598 wxTextOutputStream
s(str
);
604 bool wxVariantDataBool::Read(wxInputStream
& str
)
606 wxTextInputStream
s(str
);
608 m_value
= s
.Read8() != 0;
611 #endif // wxUSE_STREAMS
613 bool wxVariantDataBool::Read(wxString
& str
)
615 m_value
= (wxAtol((const wxChar
*) str
) != 0);
624 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
626 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
628 wxVariantDataChar() { m_value
= 0; }
629 wxVariantDataChar(char value
) { m_value
= value
; }
631 inline char GetValue() const { return m_value
; }
632 inline void SetValue(char value
) { m_value
= value
; }
634 virtual void Copy(wxVariantData
& data
);
635 virtual bool Eq(wxVariantData
& data
) const;
636 #if wxUSE_STD_IOSTREAM
637 virtual bool Read(wxSTD istream
& str
);
638 virtual bool Write(wxSTD ostream
& str
) const;
640 virtual bool Read(wxString
& str
);
641 virtual bool Write(wxString
& str
) const;
643 virtual bool Read(wxInputStream
& str
);
644 virtual bool Write(wxOutputStream
& str
) const;
645 #endif // wxUSE_STREAMS
646 virtual wxString
GetType() const { return wxT("char"); };
652 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
654 void wxVariantDataChar::Copy(wxVariantData
& data
)
656 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
658 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
660 otherData
.m_value
= m_value
;
663 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
665 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
667 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
669 return (otherData
.m_value
== m_value
);
672 #if wxUSE_STD_IOSTREAM
673 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
677 str
<< (const char*) s
.mb_str();
682 bool wxVariantDataChar::Write(wxString
& str
) const
684 str
.Printf(wxT("%c"), m_value
);
688 #if wxUSE_STD_IOSTREAM
689 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
691 wxFAIL_MSG(wxT("Unimplemented"));
698 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
700 wxTextOutputStream
s(str
);
706 bool wxVariantDataChar::Read(wxInputStream
& str
)
708 wxTextInputStream
s(str
);
713 #endif // wxUSE_STREAMS
715 bool wxVariantDataChar::Read(wxString
& str
)
717 m_value
= str
.ToAscii()[size_t(0)];
722 * wxVariantDataString
725 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
727 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(wxSTD 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(wxSTD istream
& WXUNUSED(str
)) { return false; };
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(wxSTD ostream
& str
) const
776 str
<< (const char*) m_value
.mb_str();
781 bool wxVariantDataString::Write(wxString
& str
) const
788 bool wxVariantDataString::Write(wxOutputStream
& str
) const
790 // why doesn't wxOutputStream::operator<< take "const wxString&"
791 wxTextOutputStream
s(str
);
792 s
.WriteString(m_value
);
796 bool wxVariantDataString::Read(wxInputStream
& str
)
798 wxTextInputStream
s(str
);
800 m_value
= s
.ReadLine();
803 #endif // wxUSE_STREAMS
805 bool wxVariantDataString::Read(wxString
& str
)
811 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
814 * wxVariantDataVoidPtr
817 class wxVariantDataVoidPtr
: public wxVariantData
819 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
821 wxVariantDataVoidPtr() { }
822 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
824 inline void* GetValue() const { return m_value
; }
825 inline void SetValue(void* value
) { m_value
= value
; }
827 virtual void Copy(wxVariantData
& data
);
828 virtual bool Eq(wxVariantData
& data
) const;
829 #if wxUSE_STD_IOSTREAM
830 virtual bool Write(wxSTD ostream
& str
) const;
832 virtual bool Write(wxString
& str
) const;
833 #if wxUSE_STD_IOSTREAM
834 virtual bool Read(wxSTD istream
& str
);
836 virtual bool Read(wxString
& str
);
837 virtual wxString
GetType() const { return wxT("void*"); };
838 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
843 DECLARE_NO_COPY_CLASS(wxVariantDataVoidPtr
)
846 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
848 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
850 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
852 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
854 otherData
.m_value
= m_value
;
857 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
859 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
861 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
863 return (otherData
.m_value
== m_value
);
866 #if wxUSE_STD_IOSTREAM
867 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
871 str
<< (const char*) s
.mb_str();
876 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
878 str
.Printf(wxT("%p"), m_value
);
882 #if wxUSE_STD_IOSTREAM
883 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
890 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
897 * wxVariantDataWxObjectPtr
900 class wxVariantDataWxObjectPtr
: public wxVariantData
902 DECLARE_DYNAMIC_CLASS(wxVariantDataWxObjectPtr
)
904 wxVariantDataWxObjectPtr() { }
905 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
907 inline wxObject
* GetValue() const { return m_value
; }
908 inline void SetValue(wxObject
* value
) { m_value
= value
; }
910 virtual void Copy(wxVariantData
& data
);
911 virtual bool Eq(wxVariantData
& data
) const;
912 #if wxUSE_STD_IOSTREAM
913 virtual bool Write(wxSTD ostream
& str
) const;
915 virtual bool Write(wxString
& str
) const;
916 #if wxUSE_STD_IOSTREAM
917 virtual bool Read(wxSTD istream
& str
);
919 virtual bool Read(wxString
& str
);
920 virtual wxString
GetType() const ;
921 virtual wxVariantData
* Clone() { return new wxVariantDataWxObjectPtr
; }
923 virtual wxClassInfo
* GetValueClassInfo() ;
927 DECLARE_NO_COPY_CLASS(wxVariantDataWxObjectPtr
)
930 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataWxObjectPtr
, wxVariantData
)
932 void wxVariantDataWxObjectPtr::Copy(wxVariantData
& data
)
934 wxASSERT_MSG( wxIsKindOf((&data
), wxVariantDataWxObjectPtr
) ,\
935 wxT("wxVariantDataWxObjectPtr::Copy: Can't copy to this type of data") \
938 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
940 otherData
.m_value
= m_value
;
943 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
945 wxASSERT_MSG( wxIsKindOf((&data
), wxVariantDataWxObjectPtr
), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
947 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
949 return (otherData
.m_value
== m_value
);
952 wxString
wxVariantDataWxObjectPtr::GetType() const
954 wxString
returnVal(wxT("wxObject"));
956 returnVal
= m_value
->GetClassInfo()->GetClassName();
961 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
963 wxClassInfo
* returnVal
=NULL
;
965 if (m_value
) returnVal
= m_value
->GetClassInfo();
970 #if wxUSE_STD_IOSTREAM
971 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
975 str
<< (const char*) s
.mb_str();
980 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
982 str
.Printf(wxT("%s(%p)"), GetType().c_str(), wx_static_cast(void*, m_value
));
986 #if wxUSE_STD_IOSTREAM
987 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
994 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1002 * wxVariantDataDateTime
1007 class wxVariantDataDateTime
: public wxVariantData
1009 DECLARE_DYNAMIC_CLASS(wxVariantDataDateTime
)
1012 wxVariantDataDateTime() { }
1013 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1015 wxVariantDataDateTime(const TIME_STRUCT
* valptr
)
1016 { m_value
= wxDateTime(valptr
->hour
, valptr
->minute
, valptr
->second
); }
1017 wxVariantDataDateTime(const DATE_STRUCT
* valptr
)
1018 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1),valptr
->year
); }
1019 wxVariantDataDateTime(const TIMESTAMP_STRUCT
* valptr
)
1020 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1), valptr
->year
,
1021 valptr
->hour
, valptr
->minute
, valptr
->second
, (wxDateTime::wxDateTime_t
)valptr
->fraction
); }
1024 inline wxDateTime
GetValue() const { return m_value
; }
1025 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1027 virtual void Copy(wxVariantData
& data
);
1028 virtual bool Eq(wxVariantData
& data
) const;
1029 #if wxUSE_STD_IOSTREAM
1030 virtual bool Write(wxSTD ostream
& str
) const;
1032 virtual bool Write(wxString
& str
) const;
1033 #if wxUSE_STD_IOSTREAM
1034 virtual bool Read(wxSTD istream
& str
);
1036 virtual bool Read(wxString
& str
);
1037 virtual wxString
GetType() const { return wxT("datetime"); };
1038 virtual wxVariantData
* Clone() { return new wxVariantDataDateTime
; }
1045 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDateTime
, wxVariantData
)
1047 void wxVariantDataDateTime::Copy(wxVariantData
& data
)
1049 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Copy: Can't copy to this type of data") );
1051 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1053 otherData
.m_value
= m_value
;
1057 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1059 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1061 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1063 return (otherData
.m_value
== m_value
);
1067 #if wxUSE_STD_IOSTREAM
1068 bool wxVariantDataDateTime::Write(wxSTD ostream
& WXUNUSED(str
)) const
1076 bool wxVariantDataDateTime::Write(wxString
& str
) const
1078 str
= m_value
.Format();
1083 #if wxUSE_STD_IOSTREAM
1084 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1092 bool wxVariantDataDateTime::Read(wxString
& str
)
1094 if(! m_value
.ParseDateTime(str
))
1099 #endif // wxUSE_DATETIME
1101 // ----------------------------------------------------------------------------
1102 // wxVariantDataArrayString
1103 // ----------------------------------------------------------------------------
1105 class wxVariantDataArrayString
: public wxVariantData
1108 wxVariantDataArrayString() { }
1109 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1111 wxArrayString
GetValue() const { return m_value
; }
1112 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1114 virtual void Copy(wxVariantData
& data
);
1115 virtual bool Eq(wxVariantData
& data
) const;
1116 #if wxUSE_STD_IOSTREAM
1117 virtual bool Write(wxSTD ostream
& str
) const;
1119 virtual bool Write(wxString
& str
) const;
1120 #if wxUSE_STD_IOSTREAM
1121 virtual bool Read(wxSTD istream
& str
);
1123 virtual bool Read(wxString
& str
);
1124 virtual wxString
GetType() const { return wxT("arrstring"); };
1125 virtual wxVariantData
* Clone() { return new wxVariantDataArrayString
; }
1128 wxArrayString m_value
;
1130 DECLARE_DYNAMIC_CLASS(wxVariantDataArrayString
)
1133 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataArrayString
, wxVariantData
)
1135 void wxVariantDataArrayString::Copy(wxVariantData
& data
)
1137 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Copy: Can't copy to this type of data") );
1139 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1141 otherData
.m_value
= m_value
;
1145 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1147 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1149 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1151 return otherData
.m_value
== m_value
;
1155 #if wxUSE_STD_IOSTREAM
1156 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1164 bool wxVariantDataArrayString::Write(wxString
& str
) const
1166 size_t count
= m_value
.GetCount();
1167 for ( size_t n
= 0; n
< count
; n
++ )
1179 #if wxUSE_STD_IOSTREAM
1180 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1188 bool wxVariantDataArrayString::Read(wxString
& str
)
1190 wxStringTokenizer
tk(str
, _T(";"));
1191 while ( tk
.HasMoreTokens() )
1193 m_value
.Add(tk
.GetNextToken());
1205 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1207 // Construction & destruction
1208 wxVariant::wxVariant()
1210 m_data
= (wxVariantData
*) NULL
;
1213 wxVariant::wxVariant(double val
, const wxString
& name
)
1215 m_data
= new wxVariantDataReal(val
);
1219 wxVariant::wxVariant(long val
, const wxString
& name
)
1221 m_data
= new wxVariantDataLong(val
);
1226 wxVariant::wxVariant(bool val
, const wxString
& name
)
1228 m_data
= new wxVariantDataBool(val
);
1233 wxVariant::wxVariant(char val
, const wxString
& name
)
1235 m_data
= new wxVariantDataChar(val
);
1239 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1241 m_data
= new wxVariantDataString(val
);
1245 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1247 m_data
= new wxVariantDataString(wxString(val
));
1251 #if WXWIN_COMPATIBILITY_2_4
1253 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1255 m_data
= new wxVariantDataStringList(val
);
1261 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1263 m_data
= new wxVariantDataList(val
);
1267 wxVariant::wxVariant( void* val
, const wxString
& name
)
1269 m_data
= new wxVariantDataVoidPtr(val
);
1273 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1275 m_data
= new wxVariantDataWxObjectPtr(val
);
1280 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1282 m_data
= new wxVariantDataDateTime(val
);
1285 #endif // wxUSE_DATETIME
1288 wxVariant::wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
) // Date
1290 m_data
= new wxVariantDataDateTime(valptr
);
1294 wxVariant::wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
) // Date
1296 m_data
= new wxVariantDataDateTime(valptr
);
1300 wxVariant::wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
) // Date
1302 m_data
= new wxVariantDataDateTime(valptr
);
1305 #endif // wxUSE_ODBC
1307 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1309 m_data
= new wxVariantDataArrayString(val
);
1313 wxVariant::wxVariant(const wxVariant
& variant
)
1316 if (!variant
.IsNull())
1318 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1319 variant
.m_data
->Copy(*m_data
);
1322 m_data
= (wxVariantData
*) NULL
;
1323 m_name
= variant
.m_name
;
1326 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1332 wxVariant::~wxVariant()
1338 // Make NULL (i.e. delete the data)
1339 void wxVariant::MakeNull()
1345 // Generic operators
1347 void wxVariant::operator= (const wxVariant
& variant
)
1349 if (variant
.IsNull())
1355 if (IsNull() || (GetType() != variant
.GetType()))
1359 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1362 variant
.GetData()->Copy(* GetData());
1364 m_name
= variant
.m_name
;
1367 // Assignment using data, e.g.
1368 // myVariant = new wxStringVariantData("hello")
1369 void wxVariant::operator= (wxVariantData
* variantData
)
1372 m_data
= variantData
;
1375 bool wxVariant::operator== (const wxVariant
& variant
) const
1377 if (IsNull() || variant
.IsNull())
1378 return (IsNull() == variant
.IsNull());
1380 return (GetData()->Eq(* variant
.GetData()));
1383 bool wxVariant::operator!= (const wxVariant
& variant
) const
1385 return (!(*this == variant
));
1389 // Specific operators
1390 bool wxVariant::operator== (double value
) const
1393 if (!Convert(&thisValue
))
1396 return wxIsSameDouble(value
, thisValue
);
1399 bool wxVariant::operator!= (double value
) const
1401 return (!((*this) == value
));
1404 void wxVariant::operator= (double value
)
1406 if (GetType() == wxT("double"))
1408 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1414 m_data
= new wxVariantDataReal(value
);
1418 bool wxVariant::operator== (long value
) const
1421 if (!Convert(&thisValue
))
1424 return (value
== thisValue
);
1427 bool wxVariant::operator!= (long value
) const
1429 return (!((*this) == value
));
1432 void wxVariant::operator= (long value
)
1434 if (GetType() == wxT("long"))
1436 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1442 m_data
= new wxVariantDataLong(value
);
1446 bool wxVariant::operator== (char value
) const
1449 if (!Convert(&thisValue
))
1452 return (value
== thisValue
);
1455 bool wxVariant::operator!= (char value
) const
1457 return (!((*this) == value
));
1460 void wxVariant::operator= (char value
)
1462 if (GetType() == wxT("char"))
1464 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1470 m_data
= new wxVariantDataChar(value
);
1475 bool wxVariant::operator== (bool value
) const
1478 if (!Convert(&thisValue
))
1481 return (value
== thisValue
);
1484 bool wxVariant::operator!= (bool value
) const
1486 return (!((*this) == value
));
1489 void wxVariant::operator= (bool value
)
1491 if (GetType() == wxT("bool"))
1493 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1499 m_data
= new wxVariantDataBool(value
);
1504 bool wxVariant::operator== (const wxString
& value
) const
1507 if (!Convert(&thisValue
))
1510 return value
== thisValue
;
1513 bool wxVariant::operator!= (const wxString
& value
) const
1515 return (!((*this) == value
));
1518 void wxVariant::operator= (const wxString
& value
)
1520 if (GetType() == wxT("string"))
1522 ((wxVariantDataString
*)GetData())->SetValue(value
);
1528 m_data
= new wxVariantDataString(value
);
1532 void wxVariant::operator= (const wxChar
* value
)
1534 if (GetType() == wxT("string"))
1536 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1542 m_data
= new wxVariantDataString(wxString(value
));
1546 #if WXWIN_COMPATIBILITY_2_4
1548 bool wxVariant::operator== (const wxStringList
& value
) const
1550 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1552 wxVariantDataStringList
other(value
);
1553 return (m_data
->Eq(other
));
1556 bool wxVariant::operator!= (const wxStringList
& value
) const
1558 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1560 wxVariantDataStringList
other(value
);
1561 return !(m_data
->Eq(other
));
1564 void wxVariant::operator= (const wxStringList
& value
)
1566 if (GetType() == wxT("stringlist"))
1568 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1574 m_data
= new wxVariantDataStringList(value
);
1580 bool wxVariant::operator== (const wxList
& value
) const
1582 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1584 wxVariantDataList
other(value
);
1585 return (m_data
->Eq(other
));
1588 bool wxVariant::operator!= (const wxList
& value
) const
1590 return (!((*this) == value
));
1593 void wxVariant::operator= (const wxList
& value
)
1595 if (GetType() == wxT("list"))
1597 ((wxVariantDataList
*)GetData())->SetValue(value
);
1603 m_data
= new wxVariantDataList(value
);
1607 bool wxVariant::operator== (void* value
) const
1609 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1612 bool wxVariant::operator!= (void* value
) const
1614 return (!((*this) == (void*) value
));
1617 void wxVariant::operator= (void* value
)
1619 if (GetType() == wxT("void*"))
1621 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1627 m_data
= new wxVariantDataVoidPtr(value
);
1631 bool wxVariant::operator== (wxObject
* value
) const
1633 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1636 bool wxVariant::operator!= (wxObject
* value
) const
1638 return (!((*this) == (void*) value
));
1641 void wxVariant::operator= (wxObject
* value
)
1645 m_data
= new wxVariantDataWxObjectPtr(value
);
1649 bool wxVariant::operator== (const wxDateTime
& value
) const
1651 wxDateTime thisValue
;
1652 if (!Convert(&thisValue
))
1655 return value
.IsEqualTo(thisValue
);
1658 bool wxVariant::operator!= (const wxDateTime
& value
) const
1660 return (!((*this) == value
));
1663 void wxVariant::operator= (const wxDateTime
& value
)
1665 if (GetType() == wxT("datetime"))
1667 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1673 m_data
= new wxVariantDataDateTime(value
);
1676 #endif // wxUSE_DATETIME
1679 void wxVariant::operator= (const DATE_STRUCT
* value
)
1683 m_data
= new wxVariantDataDateTime(value
);
1687 void wxVariant::operator= (const TIME_STRUCT
* value
)
1691 m_data
= new wxVariantDataDateTime(value
);
1695 void wxVariant::operator= (const TIMESTAMP_STRUCT
* value
)
1699 m_data
= new wxVariantDataDateTime(value
);
1702 #endif // wxUSE_ODBC
1704 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1706 wxFAIL_MSG( _T("TODO") );
1711 bool wxVariant::operator!=(const wxArrayString
& value
) const
1713 return !(*this == value
);
1716 void wxVariant::operator=(const wxArrayString
& value
)
1718 if (GetType() == wxT("arrstring"))
1720 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1725 m_data
= new wxVariantDataArrayString(value
);
1729 wxArrayString
wxVariant::GetArrayString() const
1731 if ( GetType() == wxT("arrstring") )
1732 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1734 return wxArrayString();
1738 // Treat a list variant as an array
1739 wxVariant
wxVariant::operator[] (size_t idx
) const
1741 #if WXWIN_COMPATIBILITY_2_4
1742 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1744 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
1747 if (GetType() == wxT("list"))
1749 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1750 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1751 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1753 #if WXWIN_COMPATIBILITY_2_4
1754 else if (GetType() == wxT("stringlist"))
1756 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1757 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1759 wxString
str( (const wxChar
*) (data
->GetValue().Item(idx
)->GetData()) );
1760 wxVariant
variant( str
);
1764 return wxNullVariant
;
1767 wxVariant
& wxVariant::operator[] (size_t idx
)
1769 // We can't return a reference to a variant for a string list, since the string
1770 // is actually stored as a char*, not a variant.
1772 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1774 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1775 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1777 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1780 // Return the number of elements in a list
1781 size_t wxVariant::GetCount() const
1783 #if WXWIN_COMPATIBILITY_2_4
1784 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1786 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
1789 if (GetType() == wxT("list"))
1791 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1792 return data
->GetValue().GetCount();
1794 #if WXWIN_COMPATIBILITY_2_4
1795 else if (GetType() == wxT("stringlist"))
1797 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1798 return data
->GetValue().GetCount();
1804 wxString
wxVariant::MakeString() const
1809 if (GetData()->Write(str
))
1812 return wxEmptyString
;
1817 void wxVariant::SetData(wxVariantData
* data
)
1819 if (m_data
) delete m_data
;
1824 // Returns a string representing the type of the variant,
1825 // e.g. "string", "bool", "list", "double", "long"
1826 wxString
wxVariant::GetType() const
1829 return wxString(wxT("null"));
1831 return m_data
->GetType();
1835 bool wxVariant::IsType(const wxString
& type
) const
1837 return (GetType() == type
);
1840 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
1842 wxClassInfo
* info
=m_data
->GetValueClassInfo();
1843 return info
? info
->IsKindOf(type
) : false ;
1848 double wxVariant::GetReal() const
1851 if (Convert(& value
))
1855 wxFAIL_MSG(wxT("Could not convert to a real number"));
1860 long wxVariant::GetInteger() const
1863 if (Convert(& value
))
1867 wxFAIL_MSG(wxT("Could not convert to an integer"));
1872 char wxVariant::GetChar() const
1875 if (Convert(& value
))
1879 wxFAIL_MSG(wxT("Could not convert to a char"));
1884 bool wxVariant::GetBool() const
1887 if (Convert(& value
))
1891 wxFAIL_MSG(wxT("Could not convert to a bool"));
1896 wxString
wxVariant::GetString() const
1899 if (!Convert(& value
))
1901 wxFAIL_MSG(wxT("Could not convert to a string"));
1907 void* wxVariant::GetVoidPtr() const
1909 wxASSERT( (GetType() == wxT("void*")) );
1911 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1914 wxObject
* wxVariant::GetWxObjectPtr() const
1916 wxASSERT(wxIsKindOf(m_data
, wxVariantDataWxObjectPtr
));
1917 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_data
)->GetValue();
1921 wxDateTime
wxVariant::GetDateTime() const
1924 if (!Convert(& value
))
1926 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1931 #endif // wxUSE_DATETIME
1933 wxList
& wxVariant::GetList() const
1935 wxASSERT( (GetType() == wxT("list")) );
1937 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1940 #if WXWIN_COMPATIBILITY_2_4
1942 wxStringList
& wxVariant::GetStringList() const
1944 wxASSERT( (GetType() == wxT("stringlist")) );
1946 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1952 void wxVariant::NullList()
1954 SetData(new wxVariantDataList());
1958 void wxVariant::Append(const wxVariant
& value
)
1960 wxList
& list
= GetList();
1962 list
.Append(new wxVariant(value
));
1965 // Insert at front of list
1966 void wxVariant::Insert(const wxVariant
& value
)
1968 wxList
& list
= GetList();
1970 list
.Insert(new wxVariant(value
));
1973 // Returns true if the variant is a member of the list
1974 bool wxVariant::Member(const wxVariant
& value
) const
1976 wxList
& list
= GetList();
1978 wxList::compatibility_iterator node
= list
.GetFirst();
1981 wxVariant
* other
= (wxVariant
*) node
->GetData();
1982 if (value
== *other
)
1984 node
= node
->GetNext();
1989 // Deletes the nth element of the list
1990 bool wxVariant::Delete(size_t item
)
1992 wxList
& list
= GetList();
1994 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
1995 wxList::compatibility_iterator node
= list
.Item(item
);
1996 wxVariant
* variant
= (wxVariant
*) node
->GetData();
2003 void wxVariant::ClearList()
2005 if (!IsNull() && (GetType() == wxT("list")))
2007 ((wxVariantDataList
*) m_data
)->Clear();
2011 if (!GetType().IsSameAs(wxT("list")))
2016 m_data
= new wxVariantDataList
;
2021 bool wxVariant::Convert(long* value
) const
2023 wxString
type(GetType());
2024 if (type
== wxT("double"))
2025 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
2026 else if (type
== wxT("long"))
2027 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2029 else if (type
== wxT("bool"))
2030 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2032 else if (type
== wxT("string"))
2033 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
2040 bool wxVariant::Convert(bool* value
) const
2042 wxString
type(GetType());
2043 if (type
== wxT("double"))
2044 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
2045 else if (type
== wxT("long"))
2046 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2048 else if (type
== wxT("bool"))
2049 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2051 else if (type
== wxT("string"))
2053 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2055 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
2057 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
2068 bool wxVariant::Convert(double* value
) const
2070 wxString
type(GetType());
2071 if (type
== wxT("double"))
2072 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
2073 else if (type
== wxT("long"))
2074 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2076 else if (type
== wxT("bool"))
2077 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2079 else if (type
== wxT("string"))
2080 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
2087 bool wxVariant::Convert(char* value
) const
2089 wxString
type(GetType());
2090 if (type
== wxT("char"))
2091 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2092 else if (type
== wxT("long"))
2093 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2095 else if (type
== wxT("bool"))
2096 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2104 bool wxVariant::Convert(wxString
* value
) const
2106 *value
= MakeString();
2111 bool wxVariant::Convert(wxDateTime
* value
) const
2113 wxString
type(GetType());
2114 if (type
== wxT("datetime"))
2116 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2119 // Fallback to string conversion
2121 return Convert(&val
) &&
2122 (value
->ParseDateTime(val
) || value
->ParseDate(val
) || value
->ParseTime(val
));
2124 #endif // wxUSE_DATETIME
2126 #endif // wxUSE_VARIANT