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/tokenzr.h"
39 #include "wx/variant.h"
41 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
43 wxVariant WXDLLEXPORT wxNullVariant
;
49 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
51 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
53 wxVariantDataList() {}
54 wxVariantDataList(const wxList
& list
);
57 wxList
& GetValue() const { return (wxList
&) m_value
; }
58 void SetValue(const wxList
& value
) ;
60 virtual void Copy(wxVariantData
& data
);
61 virtual bool Eq(wxVariantData
& data
) const;
62 #if wxUSE_STD_IOSTREAM
63 virtual bool Write(wxSTD ostream
& str
) const;
65 virtual bool Write(wxString
& str
) const;
66 #if wxUSE_STD_IOSTREAM
67 virtual bool Read(wxSTD istream
& str
);
69 virtual bool Read(wxString
& str
);
70 virtual wxString
GetType() const { return wxT("list"); };
78 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
80 wxVariantDataList::wxVariantDataList(const wxList
& list
)
85 wxVariantDataList::~wxVariantDataList()
90 void wxVariantDataList::SetValue(const wxList
& value
)
93 wxNode
* node
= value
.GetFirst();
96 wxVariant
* var
= (wxVariant
*) node
->GetData();
97 m_value
.Append(new wxVariant(*var
));
98 node
= node
->GetNext();
102 void wxVariantDataList::Clear()
104 wxNode
* node
= m_value
.GetFirst();
107 wxVariant
* var
= (wxVariant
*) node
->GetData();
109 node
= node
->GetNext();
114 void wxVariantDataList::Copy(wxVariantData
& data
)
116 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
118 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
121 wxNode
* node
= m_value
.GetFirst();
124 wxVariant
* var
= (wxVariant
*) node
->GetData();
125 listData
.m_value
.Append(new wxVariant(*var
));
126 node
= node
->GetNext();
130 bool wxVariantDataList::Eq(wxVariantData
& data
) const
132 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
134 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
135 wxNode
* node1
= m_value
.GetFirst();
136 wxNode
* node2
= listData
.GetValue().GetFirst();
137 while (node1
&& node2
)
139 wxVariant
* var1
= (wxVariant
*) node1
->GetData();
140 wxVariant
* var2
= (wxVariant
*) node2
->GetData();
141 if ((*var1
) != (*var2
))
143 node1
= node1
->GetNext();
144 node2
= node2
->GetNext();
146 if (node1
|| node2
) return FALSE
;
150 #if wxUSE_STD_IOSTREAM
151 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
155 str
<< (const char*) s
.mb_str();
160 bool wxVariantDataList::Write(wxString
& str
) const
163 wxNode
* node
= m_value
.GetFirst();
166 wxVariant
* var
= (wxVariant
*) node
->GetData();
167 if (node
!= m_value
.GetFirst())
170 str
+= var
->MakeString();
171 node
= node
->GetNext();
177 #if wxUSE_STD_IOSTREAM
178 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
180 wxFAIL_MSG(wxT("Unimplemented"));
186 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
188 wxFAIL_MSG(wxT("Unimplemented"));
194 * wxVariantDataStringList
197 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
199 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
201 wxVariantDataStringList() {}
202 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
204 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
205 void SetValue(const wxStringList
& value
);
207 virtual void Copy(wxVariantData
& data
);
208 virtual bool Eq(wxVariantData
& data
) const;
209 #if wxUSE_STD_IOSTREAM
210 virtual bool Write(wxSTD ostream
& str
) const;
212 virtual bool Write(wxString
& str
) const;
213 #if wxUSE_STD_IOSTREAM
214 virtual bool Read(wxSTD istream
& str
);
216 virtual bool Read(wxString
& str
);
217 virtual wxString
GetType() const { return wxT("stringlist"); };
220 wxStringList m_value
;
223 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
225 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
230 void wxVariantDataStringList::Copy(wxVariantData
& data
)
232 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
234 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
236 listData
.m_value
= m_value
;
239 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
241 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
243 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
244 wxStringList::Node
*node1
= m_value
.GetFirst();
245 wxStringList::Node
*node2
= listData
.GetValue().GetFirst();
246 while (node1
&& node2
)
248 wxString
str1 ( node1
->GetData() );
249 wxString
str2 ( node2
->GetData() );
252 node1
= node1
->GetNext();
253 node2
= node2
->GetNext();
255 if (node1
|| node2
) return FALSE
;
259 #if wxUSE_STD_IOSTREAM
260 bool wxVariantDataStringList::Write(wxSTD ostream
& str
) const
264 str
<< (const char*) s
.mb_str();
269 bool wxVariantDataStringList::Write(wxString
& str
) const
272 wxStringList::Node
*node
= m_value
.GetFirst();
275 wxChar
* s
= node
->GetData();
276 if (node
!= m_value
.GetFirst())
279 node
= node
->GetNext();
285 #if wxUSE_STD_IOSTREAM
286 bool wxVariantDataStringList::Read(wxSTD istream
& WXUNUSED(str
))
288 wxFAIL_MSG(wxT("Unimplemented"));
294 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
296 wxFAIL_MSG(wxT("Unimplemented"));
305 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
307 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
309 wxVariantDataLong() { m_value
= 0; }
310 wxVariantDataLong(long value
) { m_value
= value
; }
312 inline long GetValue() const { return m_value
; }
313 inline void SetValue(long value
) { m_value
= value
; }
315 virtual void Copy(wxVariantData
& data
);
316 virtual bool Eq(wxVariantData
& data
) const;
318 virtual bool Read(wxString
& str
);
319 virtual bool Write(wxString
& str
) const;
320 #if wxUSE_STD_IOSTREAM
321 virtual bool Read(wxSTD istream
& str
);
322 virtual bool Write(wxSTD ostream
& str
) const;
325 virtual bool Read(wxInputStream
& str
);
326 virtual bool Write(wxOutputStream
&str
) const;
327 #endif // wxUSE_STREAMS
329 virtual wxString
GetType() const { return wxT("long"); };
335 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
337 void wxVariantDataLong::Copy(wxVariantData
& data
)
339 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
341 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
343 otherData
.m_value
= m_value
;
346 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
348 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
350 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
352 return (otherData
.m_value
== m_value
);
355 #if wxUSE_STD_IOSTREAM
356 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
360 str
<< (const char*) s
.mb_str();
365 bool wxVariantDataLong::Write(wxString
& str
) const
367 str
.Printf(wxT("%ld"), m_value
);
371 #if wxUSE_STD_IOSTREAM
372 bool wxVariantDataLong::Read(wxSTD istream
& str
)
380 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
382 wxTextOutputStream
s(str
);
384 s
.Write32((size_t)m_value
);
388 bool wxVariantDataLong::Read(wxInputStream
& str
)
390 wxTextInputStream
s(str
);
391 m_value
= s
.Read32();
394 #endif // wxUSE_STREAMS
396 bool wxVariantDataLong::Read(wxString
& str
)
398 m_value
= wxAtol((const wxChar
*) str
);
406 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
408 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
410 wxVariantDataReal() { m_value
= 0.0; }
411 wxVariantDataReal(double value
) { m_value
= value
; }
413 inline double GetValue() const { return m_value
; }
414 inline void SetValue(double value
) { m_value
= value
; }
416 virtual void Copy(wxVariantData
& data
);
417 virtual bool Eq(wxVariantData
& data
) const;
418 virtual bool Read(wxString
& str
);
419 #if wxUSE_STD_IOSTREAM
420 virtual bool Write(wxSTD ostream
& str
) const;
422 virtual bool Write(wxString
& str
) const;
423 #if wxUSE_STD_IOSTREAM
424 virtual bool Read(wxSTD istream
& str
);
427 virtual bool Read(wxInputStream
& str
);
428 virtual bool Write(wxOutputStream
&str
) const;
429 #endif // wxUSE_STREAMS
430 virtual wxString
GetType() const { return wxT("double"); };
436 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
438 void wxVariantDataReal::Copy(wxVariantData
& data
)
440 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
442 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
444 otherData
.m_value
= m_value
;
447 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
449 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
451 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
453 return (otherData
.m_value
== m_value
);
456 #if wxUSE_STD_IOSTREAM
457 bool wxVariantDataReal::Write(wxSTD ostream
& str
) const
461 str
<< (const char*) s
.mb_str();
466 bool wxVariantDataReal::Write(wxString
& str
) const
468 str
.Printf(wxT("%.4f"), m_value
);
472 #if wxUSE_STD_IOSTREAM
473 bool wxVariantDataReal::Read(wxSTD istream
& str
)
481 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
483 wxTextOutputStream
s(str
);
484 s
.WriteDouble((double)m_value
);
488 bool wxVariantDataReal::Read(wxInputStream
& str
)
490 wxTextInputStream
s(str
);
491 m_value
= (float)s
.ReadDouble();
494 #endif // wxUSE_STREAMS
496 bool wxVariantDataReal::Read(wxString
& str
)
498 m_value
= wxAtof((const wxChar
*) str
);
507 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
509 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
511 wxVariantDataBool() { m_value
= 0; }
512 wxVariantDataBool(bool value
) { m_value
= value
; }
514 inline bool GetValue() const { return m_value
; }
515 inline void SetValue(bool value
) { m_value
= value
; }
517 virtual void Copy(wxVariantData
& data
);
518 virtual bool Eq(wxVariantData
& data
) const;
519 #if wxUSE_STD_IOSTREAM
520 virtual bool Write(wxSTD ostream
& str
) const;
522 virtual bool Write(wxString
& str
) const;
523 virtual bool Read(wxString
& str
);
524 #if wxUSE_STD_IOSTREAM
525 virtual bool Read(wxSTD istream
& str
);
528 virtual bool Read(wxInputStream
& str
);
529 virtual bool Write(wxOutputStream
& str
) const;
530 #endif // wxUSE_STREAMS
531 virtual wxString
GetType() const { return wxT("bool"); };
537 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
539 void wxVariantDataBool::Copy(wxVariantData
& data
)
541 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
543 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
545 otherData
.m_value
= m_value
;
548 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
550 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
552 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
554 return (otherData
.m_value
== m_value
);
557 #if wxUSE_STD_IOSTREAM
558 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
562 str
<< (const char*) s
.mb_str();
567 bool wxVariantDataBool::Write(wxString
& str
) const
569 str
.Printf(wxT("%d"), (int) m_value
);
573 #if wxUSE_STD_IOSTREAM
574 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
576 wxFAIL_MSG(wxT("Unimplemented"));
577 // str >> (long) m_value;
583 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
585 wxTextOutputStream
s(str
);
591 bool wxVariantDataBool::Read(wxInputStream
& str
)
593 wxTextInputStream
s(str
);
595 m_value
= s
.Read8() != 0;
598 #endif // wxUSE_STREAMS
600 bool wxVariantDataBool::Read(wxString
& str
)
602 m_value
= (wxAtol((const wxChar
*) str
) != 0);
611 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
613 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
615 wxVariantDataChar() { m_value
= 0; }
616 wxVariantDataChar(char value
) { m_value
= value
; }
618 inline char GetValue() const { return m_value
; }
619 inline void SetValue(char value
) { m_value
= value
; }
621 virtual void Copy(wxVariantData
& data
);
622 virtual bool Eq(wxVariantData
& data
) const;
623 #if wxUSE_STD_IOSTREAM
624 virtual bool Read(wxSTD istream
& str
);
625 virtual bool Write(wxSTD ostream
& str
) const;
627 virtual bool Read(wxString
& str
);
628 virtual bool Write(wxString
& str
) const;
630 virtual bool Read(wxInputStream
& str
);
631 virtual bool Write(wxOutputStream
& str
) const;
632 #endif // wxUSE_STREAMS
633 virtual wxString
GetType() const { return wxT("char"); };
639 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
641 void wxVariantDataChar::Copy(wxVariantData
& data
)
643 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
645 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
647 otherData
.m_value
= m_value
;
650 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
652 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
654 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
656 return (otherData
.m_value
== m_value
);
659 #if wxUSE_STD_IOSTREAM
660 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
664 str
<< (const char*) s
.mb_str();
669 bool wxVariantDataChar::Write(wxString
& str
) const
671 str
.Printf(wxT("%c"), m_value
);
675 #if wxUSE_STD_IOSTREAM
676 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
678 wxFAIL_MSG(wxT("Unimplemented"));
685 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
687 wxTextOutputStream
s(str
);
693 bool wxVariantDataChar::Read(wxInputStream
& str
)
695 wxTextInputStream
s(str
);
700 #endif // wxUSE_STREAMS
702 bool wxVariantDataChar::Read(wxString
& str
)
704 m_value
= str
[(size_t)0];
709 * wxVariantDataString
712 #if defined(__BORLANDC__) && defined(__WIN16__)
713 // Change name because of truncation
714 #define wxVariantDataString wxVariantStringData
717 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
719 #if defined(__BORLANDC__) && defined(__WIN16__)
720 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
722 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
725 wxVariantDataString() { }
726 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
728 inline wxString
GetValue() const { return m_value
; }
729 inline void SetValue(const wxString
& value
) { m_value
= value
; }
731 virtual void Copy(wxVariantData
& data
);
732 virtual bool Eq(wxVariantData
& data
) const;
733 #if wxUSE_STD_IOSTREAM
734 virtual bool Write(wxSTD ostream
& str
) const;
736 virtual bool Read(wxString
& str
);
737 virtual bool Write(wxString
& str
) const;
738 #if wxUSE_STD_IOSTREAM
739 virtual bool Read(wxSTD istream
& str
);
742 virtual bool Read(wxInputStream
& str
);
743 virtual bool Write(wxOutputStream
& str
) const;
744 #endif // wxUSE_STREAMS
745 virtual wxString
GetType() const { return wxT("string"); };
751 void wxVariantDataString::Copy(wxVariantData
& data
)
753 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
755 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
757 otherData
.m_value
= m_value
;
760 bool wxVariantDataString::Eq(wxVariantData
& data
) const
762 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
764 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
766 return (otherData
.m_value
== m_value
);
769 #if wxUSE_STD_IOSTREAM
770 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
772 str
<< (const char*) m_value
.mb_str();
777 bool wxVariantDataString::Write(wxString
& str
) const
783 #if wxUSE_STD_IOSTREAM
784 bool wxVariantDataString::Read(wxSTD istream
& str
)
792 bool wxVariantDataString::Write(wxOutputStream
& str
) const
794 // why doesn't wxOutputStream::operator<< take "const wxString&"
795 wxTextOutputStream
s(str
);
796 s
.WriteString(m_value
);
800 bool wxVariantDataString::Read(wxInputStream
& str
)
802 wxTextInputStream
s(str
);
804 m_value
= s
.ReadString();
807 #endif // wxUSE_STREAMS
809 bool wxVariantDataString::Read(wxString
& str
)
815 #if defined(__BORLANDC__) && defined(__WIN16__)
816 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
818 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
822 * wxVariantDataVoidPtr
825 class wxVariantDataVoidPtr
: public wxVariantData
827 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
829 wxVariantDataVoidPtr() { }
830 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
832 inline void* GetValue() const { return m_value
; }
833 inline void SetValue(void* value
) { m_value
= value
; }
835 virtual void Copy(wxVariantData
& data
);
836 virtual bool Eq(wxVariantData
& data
) const;
837 #if wxUSE_STD_IOSTREAM
838 virtual bool Write(wxSTD ostream
& str
) const;
840 virtual bool Write(wxString
& str
) const;
841 #if wxUSE_STD_IOSTREAM
842 virtual bool Read(wxSTD istream
& str
);
844 virtual bool Read(wxString
& str
);
845 virtual wxString
GetType() const { return wxT("void*"); };
846 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
851 DECLARE_NO_COPY_CLASS(wxVariantDataVoidPtr
)
854 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
856 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
858 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
860 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
862 otherData
.m_value
= m_value
;
865 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
867 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
869 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
871 return (otherData
.m_value
== m_value
);
874 #if wxUSE_STD_IOSTREAM
875 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
879 str
<< (const char*) s
.mb_str();
884 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
886 str
.Printf(wxT("%ld"), (long) m_value
);
890 #if wxUSE_STD_IOSTREAM
891 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
898 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
905 * wxVariantDataDateTime
910 class wxVariantDataDateTime
: public wxVariantData
912 DECLARE_DYNAMIC_CLASS(wxVariantDataDateTime
)
915 wxVariantDataDateTime() { }
916 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
918 wxVariantDataDateTime(const TIME_STRUCT
* valptr
)
919 { m_value
= wxDateTime(valptr
->hour
, valptr
->minute
, valptr
->second
); }
920 wxVariantDataDateTime(const DATE_STRUCT
* valptr
)
921 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1),valptr
->year
); }
922 wxVariantDataDateTime(const TIMESTAMP_STRUCT
* valptr
)
923 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1), valptr
->year
,
924 valptr
->hour
, valptr
->minute
, valptr
->second
, valptr
->fraction
); }
927 inline wxDateTime
GetValue() const { return m_value
; }
928 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
930 virtual void Copy(wxVariantData
& data
);
931 virtual bool Eq(wxVariantData
& data
) const;
932 #if wxUSE_STD_IOSTREAM
933 virtual bool Write(wxSTD ostream
& str
) const;
935 virtual bool Write(wxString
& str
) const;
936 #if wxUSE_STD_IOSTREAM
937 virtual bool Read(wxSTD istream
& str
);
939 virtual bool Read(wxString
& str
);
940 virtual wxString
GetType() const { return wxT("datetime"); };
941 virtual wxVariantData
* Clone() { return new wxVariantDataDateTime
; }
948 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDateTime
, wxVariantData
)
950 void wxVariantDataDateTime::Copy(wxVariantData
& data
)
952 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Copy: Can't copy to this type of data") );
954 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
956 otherData
.m_value
= m_value
;
960 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
962 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
964 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
966 return (otherData
.m_value
== m_value
);
970 #if wxUSE_STD_IOSTREAM
971 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
979 bool wxVariantDataDateTime::Write(wxString
& str
) const
981 str
= m_value
.Format();
986 #if wxUSE_STD_IOSTREAM
987 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
995 bool wxVariantDataDateTime::Read(wxString
& str
)
997 if(! m_value
.ParseDateTime(str
))
1002 #endif // wxUSE_DATETIME
1004 // ----------------------------------------------------------------------------
1005 // wxVariantDataArrayString
1006 // ----------------------------------------------------------------------------
1008 class wxVariantDataArrayString
: public wxVariantData
1011 wxVariantDataArrayString() { }
1012 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1014 wxArrayString
GetValue() const { return m_value
; }
1015 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1017 virtual void Copy(wxVariantData
& data
);
1018 virtual bool Eq(wxVariantData
& data
) const;
1019 #if wxUSE_STD_IOSTREAM
1020 virtual bool Write(wxSTD ostream
& str
) const;
1022 virtual bool Write(wxString
& str
) const;
1023 #if wxUSE_STD_IOSTREAM
1024 virtual bool Read(wxSTD istream
& str
);
1026 virtual bool Read(wxString
& str
);
1027 virtual wxString
GetType() const { return wxT("arrstring"); };
1028 virtual wxVariantData
* Clone() { return new wxVariantDataArrayString
; }
1031 wxArrayString m_value
;
1033 DECLARE_DYNAMIC_CLASS(wxVariantDataArrayString
)
1036 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataArrayString
, wxVariantData
)
1038 void wxVariantDataArrayString::Copy(wxVariantData
& data
)
1040 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Copy: Can't copy to this type of data") );
1042 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1044 otherData
.m_value
= m_value
;
1048 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1050 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1052 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1054 return otherData
.m_value
== m_value
;
1058 #if wxUSE_STD_IOSTREAM
1059 bool wxVariantDataArrayString::Write(wxSTD ostream
& str
) const
1067 bool wxVariantDataArrayString::Write(wxString
& str
) const
1069 size_t count
= m_value
.GetCount();
1070 for ( size_t n
= 0; n
< count
; n
++ )
1082 #if wxUSE_STD_IOSTREAM
1083 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1091 bool wxVariantDataArrayString::Read(wxString
& str
)
1093 wxStringTokenizer
tk(str
, _T(";"));
1094 while ( tk
.HasMoreTokens() )
1096 m_value
.Add(tk
.GetNextToken());
1108 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1110 // Construction & destruction
1111 wxVariant::wxVariant()
1113 m_data
= (wxVariantData
*) NULL
;
1116 wxVariant::wxVariant(double val
, const wxString
& name
)
1118 m_data
= new wxVariantDataReal(val
);
1122 wxVariant::wxVariant(long val
, const wxString
& name
)
1124 m_data
= new wxVariantDataLong(val
);
1129 wxVariant::wxVariant(bool val
, const wxString
& name
)
1131 m_data
= new wxVariantDataBool(val
);
1136 wxVariant::wxVariant(char val
, const wxString
& name
)
1138 m_data
= new wxVariantDataChar(val
);
1142 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1144 m_data
= new wxVariantDataString(val
);
1148 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1150 m_data
= new wxVariantDataString(wxString(val
));
1154 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1156 m_data
= new wxVariantDataStringList(val
);
1160 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1162 m_data
= new wxVariantDataList(val
);
1167 wxVariant::wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
) // Date
1169 m_data
= new wxVariantDataDateTime(valptr
);
1173 wxVariant::wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
) // Date
1175 m_data
= new wxVariantDataDateTime(valptr
);
1179 wxVariant::wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
) // Date
1181 m_data
= new wxVariantDataDateTime(valptr
);
1184 #endif // wxUSE_ODBC
1186 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1188 m_data
= new wxVariantDataArrayString(val
);
1192 wxVariant::wxVariant(const wxVariant
& variant
)
1195 if (!variant
.IsNull())
1197 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1198 variant
.m_data
->Copy(*m_data
);
1201 m_data
= (wxVariantData
*) NULL
;
1202 m_name
= variant
.m_name
;
1205 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1211 wxVariant::~wxVariant()
1217 // Make NULL (i.e. delete the data)
1218 void wxVariant::MakeNull()
1224 // Generic operators
1226 void wxVariant::operator= (const wxVariant
& variant
)
1228 if (variant
.IsNull())
1234 if (IsNull() || (GetType() != variant
.GetType()))
1238 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1241 variant
.GetData()->Copy(* GetData());
1243 m_name
= variant
.m_name
;
1246 // Assignment using data, e.g.
1247 // myVariant = new wxStringVariantData("hello")
1248 void wxVariant::operator= (wxVariantData
* variantData
)
1251 m_data
= variantData
;
1254 bool wxVariant::operator== (const wxVariant
& variant
) const
1256 if (IsNull() || variant
.IsNull())
1257 return (IsNull() == variant
.IsNull());
1259 return (GetData()->Eq(* variant
.GetData()));
1262 bool wxVariant::operator!= (const wxVariant
& variant
) const
1264 return (!(*this == variant
));
1268 // Specific operators
1269 bool wxVariant::operator== (double value
) const
1272 if (!Convert(&thisValue
))
1275 return (value
== thisValue
);
1278 bool wxVariant::operator!= (double value
) const
1280 return (!((*this) == value
));
1283 void wxVariant::operator= (double value
)
1285 if (GetType() == wxT("double"))
1287 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1293 m_data
= new wxVariantDataReal(value
);
1297 bool wxVariant::operator== (long value
) const
1300 if (!Convert(&thisValue
))
1303 return (value
== thisValue
);
1306 bool wxVariant::operator!= (long value
) const
1308 return (!((*this) == value
));
1311 void wxVariant::operator= (long value
)
1313 if (GetType() == wxT("long"))
1315 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1321 m_data
= new wxVariantDataLong(value
);
1325 bool wxVariant::operator== (char value
) const
1328 if (!Convert(&thisValue
))
1331 return (value
== thisValue
);
1334 bool wxVariant::operator!= (char value
) const
1336 return (!((*this) == value
));
1339 void wxVariant::operator= (char value
)
1341 if (GetType() == wxT("char"))
1343 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1349 m_data
= new wxVariantDataChar(value
);
1354 bool wxVariant::operator== (bool value
) const
1357 if (!Convert(&thisValue
))
1360 return (value
== thisValue
);
1363 bool wxVariant::operator!= (bool value
) const
1365 return (!((*this) == value
));
1368 void wxVariant::operator= (bool value
)
1370 if (GetType() == wxT("bool"))
1372 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1378 m_data
= new wxVariantDataBool(value
);
1383 bool wxVariant::operator== (const wxString
& value
) const
1386 if (!Convert(&thisValue
))
1389 return value
== thisValue
;
1392 bool wxVariant::operator!= (const wxString
& value
) const
1394 return (!((*this) == value
));
1397 void wxVariant::operator= (const wxString
& value
)
1399 if (GetType() == wxT("string"))
1401 ((wxVariantDataString
*)GetData())->SetValue(value
);
1407 m_data
= new wxVariantDataString(value
);
1411 void wxVariant::operator= (const wxChar
* value
)
1413 if (GetType() == wxT("string"))
1415 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1421 m_data
= new wxVariantDataString(wxString(value
));
1425 bool wxVariant::operator== (const wxStringList
& value
) const
1427 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1429 wxVariantDataStringList
other(value
);
1430 return (m_data
->Eq(other
));
1433 bool wxVariant::operator!= (const wxStringList
& value
) const
1435 return (!((*this) == value
));
1438 void wxVariant::operator= (const wxStringList
& value
)
1440 if (GetType() == wxT("stringlist"))
1442 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1448 m_data
= new wxVariantDataStringList(value
);
1452 bool wxVariant::operator== (const wxList
& value
) const
1454 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1456 wxVariantDataList
other(value
);
1457 return (m_data
->Eq(other
));
1460 bool wxVariant::operator!= (const wxList
& value
) const
1462 return (!((*this) == value
));
1465 void wxVariant::operator= (const wxList
& value
)
1467 if (GetType() == wxT("list"))
1469 ((wxVariantDataList
*)GetData())->SetValue(value
);
1475 m_data
= new wxVariantDataList(value
);
1479 bool wxVariant::operator== (void* value
) const
1481 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1484 bool wxVariant::operator!= (void* value
) const
1486 return (!((*this) == (void*) value
));
1489 void wxVariant::operator= (void* value
)
1491 if (GetType() == wxT("void*"))
1493 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1499 m_data
= new wxVariantDataVoidPtr(value
);
1504 bool wxVariant::operator== (const wxDateTime
& value
) const
1506 wxDateTime thisValue
;
1507 if (!Convert(&thisValue
))
1510 return value
.IsEqualTo(thisValue
);
1513 bool wxVariant::operator!= (const wxDateTime
& value
) const
1515 return (!((*this) == value
));
1518 void wxVariant::operator= (const wxDateTime
& value
)
1520 if (GetType() == wxT("datetime"))
1522 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1528 m_data
= new wxVariantDataDateTime(value
);
1531 #endif // wxUSE_DATETIME
1534 void wxVariant::operator= (const DATE_STRUCT
* value
)
1538 m_data
= new wxVariantDataDateTime(value
);
1542 void wxVariant::operator= (const TIME_STRUCT
* value
)
1546 m_data
= new wxVariantDataDateTime(value
);
1550 void wxVariant::operator= (const TIMESTAMP_STRUCT
* value
)
1554 m_data
= new wxVariantDataDateTime(value
);
1557 #endif // wxUSE_ODBC
1559 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1561 wxFAIL_MSG( _T("TODO") );
1566 bool wxVariant::operator!=(const wxArrayString
& value
) const
1568 return !(*this == value
);
1571 void wxVariant::operator=(const wxArrayString
& value
)
1573 if (GetType() == wxT("arrstring"))
1575 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1580 m_data
= new wxVariantDataArrayString(value
);
1584 wxArrayString
wxVariant::GetArrayString() const
1586 if ( GetType() == wxT("arrstring") )
1587 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1589 return wxArrayString();
1593 // Treat a list variant as an array
1594 wxVariant
wxVariant::operator[] (size_t idx
) const
1596 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1598 if (GetType() == wxT("list"))
1600 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1601 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1602 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1604 else if (GetType() == wxT("stringlist"))
1606 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1607 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1609 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Item(idx
)->GetData()) ));
1612 return wxNullVariant
;
1615 wxVariant
& wxVariant::operator[] (size_t idx
)
1617 // We can't return a reference to a variant for a string list, since the string
1618 // is actually stored as a char*, not a variant.
1620 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1622 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1623 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1625 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1628 // Return the number of elements in a list
1629 int wxVariant::GetCount() const
1631 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1633 if (GetType() == wxT("list"))
1635 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1636 return data
->GetValue().GetCount();
1638 else if (GetType() == wxT("stringlist"))
1640 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1641 return data
->GetValue().GetCount();
1646 wxString
wxVariant::MakeString() const
1651 if (GetData()->Write(str
))
1654 return wxString(wxT(""));
1659 void wxVariant::SetData(wxVariantData
* data
)
1661 if (m_data
) delete m_data
;
1666 // Returns a string representing the type of the variant,
1667 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1668 wxString
wxVariant::GetType() const
1671 return wxString(wxT("null"));
1673 return m_data
->GetType();
1677 bool wxVariant::IsType(const wxString
& type
) const
1679 return (GetType() == type
);
1684 double wxVariant::GetReal() const
1687 if (Convert(& value
))
1691 wxFAIL_MSG(wxT("Could not convert to a real number"));
1696 long wxVariant::GetInteger() const
1699 if (Convert(& value
))
1703 wxFAIL_MSG(wxT("Could not convert to an integer"));
1708 char wxVariant::GetChar() const
1711 if (Convert(& value
))
1715 wxFAIL_MSG(wxT("Could not convert to a char"));
1720 bool wxVariant::GetBool() const
1723 if (Convert(& value
))
1727 wxFAIL_MSG(wxT("Could not convert to a bool"));
1732 wxString
wxVariant::GetString() const
1735 if (!Convert(& value
))
1737 wxFAIL_MSG(wxT("Could not convert to a string"));
1743 void* wxVariant::GetVoidPtr() const
1745 wxASSERT( (GetType() == wxT("void*")) );
1747 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1751 wxDateTime
wxVariant::GetDateTime() const
1754 if (!Convert(& value
))
1756 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1761 #endif // wxUSE_DATETIME
1763 wxList
& wxVariant::GetList() const
1765 wxASSERT( (GetType() == wxT("list")) );
1767 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1770 wxStringList
& wxVariant::GetStringList() const
1772 wxASSERT( (GetType() == wxT("stringlist")) );
1774 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1778 void wxVariant::NullList()
1780 SetData(new wxVariantDataList());
1784 void wxVariant::Append(const wxVariant
& value
)
1786 wxList
& list
= GetList();
1788 list
.Append(new wxVariant(value
));
1791 // Insert at front of list
1792 void wxVariant::Insert(const wxVariant
& value
)
1794 wxList
& list
= GetList();
1796 list
.Insert(new wxVariant(value
));
1799 // Returns TRUE if the variant is a member of the list
1800 bool wxVariant::Member(const wxVariant
& value
) const
1802 wxList
& list
= GetList();
1804 wxNode
* node
= list
.GetFirst();
1807 wxVariant
* other
= (wxVariant
*) node
->GetData();
1808 if (value
== *other
)
1810 node
= node
->GetNext();
1815 // Deletes the nth element of the list
1816 bool wxVariant::Delete(int item
)
1818 wxList
& list
= GetList();
1820 wxASSERT_MSG( (item
< (int) list
.GetCount()), wxT("Invalid index to Delete") );
1821 wxNode
* node
= list
.Item(item
);
1822 wxVariant
* variant
= (wxVariant
*) node
->GetData();
1829 void wxVariant::ClearList()
1831 if (!IsNull() && (GetType() == wxT("list")))
1833 ((wxVariantDataList
*) m_data
)->Clear();
1837 if (GetType() != wxT("list"))
1842 m_data
= new wxVariantDataList
;
1847 bool wxVariant::Convert(long* value
) const
1849 wxString
type(GetType());
1850 if (type
== wxT("double"))
1851 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1852 else if (type
== wxT("long"))
1853 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1855 else if (type
== wxT("bool"))
1856 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1858 else if (type
== wxT("string"))
1859 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1866 bool wxVariant::Convert(bool* value
) const
1868 wxString
type(GetType());
1869 if (type
== wxT("double"))
1870 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1871 else if (type
== wxT("long"))
1872 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1874 else if (type
== wxT("bool"))
1875 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1877 else if (type
== wxT("string"))
1879 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1881 if (val
== wxT("TRUE") || val
== wxT("yes"))
1883 else if (val
== wxT("FALSE") || val
== wxT("no"))
1894 bool wxVariant::Convert(double* value
) const
1896 wxString
type(GetType());
1897 if (type
== wxT("double"))
1898 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1899 else if (type
== wxT("long"))
1900 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1902 else if (type
== wxT("bool"))
1903 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1905 else if (type
== wxT("string"))
1906 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1913 bool wxVariant::Convert(char* value
) const
1915 wxString
type(GetType());
1916 if (type
== wxT("char"))
1917 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1918 else if (type
== wxT("long"))
1919 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1921 else if (type
== wxT("bool"))
1922 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1930 bool wxVariant::Convert(wxString
* value
) const
1932 *value
= MakeString();
1937 bool wxVariant::Convert(wxDateTime
* value
) const
1939 wxString
type(GetType());
1940 if (type
== wxT("datetime"))
1942 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
1945 // Fallback to string conversion
1947 return Convert(&val
) && (value
->ParseDate(val
));
1949 #endif // wxUSE_DATETIME