1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "variant.h"
14 #pragma implementation "time.h"
15 #pragma implementation "date.h"
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
25 #if wxUSE_STD_IOSTREAM
34 #include "wx/stream.h"
35 #include "wx/txtstrm.h"
38 #include "wx/string.h"
39 #include "wx/tokenzr.h"
41 #include "wx/variant.h"
44 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
45 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
47 wxTime::tFormat
wxTime::ms_Format
= wxTime::wx12h
;
48 wxTime::tPrecision
wxTime::ms_Precision
= wxTime::wxStdMinSec
;
49 wxChar
wxTime::ms_bufTime
[128];
52 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
54 wxVariant WXDLLEXPORT wxNullVariant
;
60 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
62 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
64 wxVariantDataList() {}
65 wxVariantDataList(const wxList
& list
);
68 wxList
& GetValue() const { return (wxList
&) m_value
; }
69 void SetValue(const wxList
& value
) ;
71 virtual void Copy(wxVariantData
& data
);
72 virtual bool Eq(wxVariantData
& data
) const;
73 #if wxUSE_STD_IOSTREAM
74 virtual bool Write(wxSTD ostream
& str
) const;
76 virtual bool Write(wxString
& str
) const;
77 #if wxUSE_STD_IOSTREAM
78 virtual bool Read(wxSTD istream
& str
);
80 virtual bool Read(wxString
& str
);
81 virtual wxString
GetType() const { return wxT("list"); };
89 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
91 wxVariantDataList::wxVariantDataList(const wxList
& list
)
96 wxVariantDataList::~wxVariantDataList()
101 void wxVariantDataList::SetValue(const wxList
& value
)
104 wxNode
* node
= value
.GetFirst();
107 wxVariant
* var
= (wxVariant
*) node
->GetData();
108 m_value
.Append(new wxVariant(*var
));
109 node
= node
->GetNext();
113 void wxVariantDataList::Clear()
115 wxNode
* node
= m_value
.GetFirst();
118 wxVariant
* var
= (wxVariant
*) node
->GetData();
120 node
= node
->GetNext();
125 void wxVariantDataList::Copy(wxVariantData
& data
)
127 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
129 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
132 wxNode
* node
= m_value
.GetFirst();
135 wxVariant
* var
= (wxVariant
*) node
->GetData();
136 listData
.m_value
.Append(new wxVariant(*var
));
137 node
= node
->GetNext();
141 bool wxVariantDataList::Eq(wxVariantData
& data
) const
143 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
145 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
146 wxNode
* node1
= m_value
.GetFirst();
147 wxNode
* node2
= listData
.GetValue().GetFirst();
148 while (node1
&& node2
)
150 wxVariant
* var1
= (wxVariant
*) node1
->GetData();
151 wxVariant
* var2
= (wxVariant
*) node2
->GetData();
152 if ((*var1
) != (*var2
))
154 node1
= node1
->GetNext();
155 node2
= node2
->GetNext();
157 if (node1
|| node2
) return FALSE
;
161 #if wxUSE_STD_IOSTREAM
162 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
166 str
<< (const char*) s
.mb_str();
171 bool wxVariantDataList::Write(wxString
& str
) const
174 wxNode
* node
= m_value
.GetFirst();
177 wxVariant
* var
= (wxVariant
*) node
->GetData();
178 if (node
!= m_value
.GetFirst())
181 str
+= var
->MakeString();
182 node
= node
->GetNext();
188 #if wxUSE_STD_IOSTREAM
189 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
191 wxFAIL_MSG(wxT("Unimplemented"));
197 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
199 wxFAIL_MSG(wxT("Unimplemented"));
205 * wxVariantDataStringList
208 class WXDLLEXPORT 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::Node
*node1
= m_value
.GetFirst();
256 wxStringList::Node
*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::Node
*node
= m_value
.GetFirst();
286 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"));
316 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
318 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
320 wxVariantDataLong() { m_value
= 0; }
321 wxVariantDataLong(long value
) { m_value
= value
; }
323 inline long GetValue() const { return m_value
; }
324 inline void SetValue(long value
) { m_value
= value
; }
326 virtual void Copy(wxVariantData
& data
);
327 virtual bool Eq(wxVariantData
& data
) const;
329 virtual bool Read(wxString
& str
);
330 virtual bool Write(wxString
& str
) const;
331 #if wxUSE_STD_IOSTREAM
332 virtual bool Read(wxSTD istream
& str
);
333 virtual bool Write(wxSTD ostream
& str
) const;
336 virtual bool Read(wxInputStream
& str
);
337 virtual bool Write(wxOutputStream
&str
) const;
338 #endif // wxUSE_STREAMS
340 virtual wxString
GetType() const { return wxT("long"); };
346 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
348 void wxVariantDataLong::Copy(wxVariantData
& data
)
350 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
352 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
354 otherData
.m_value
= m_value
;
357 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
359 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
361 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
363 return (otherData
.m_value
== m_value
);
366 #if wxUSE_STD_IOSTREAM
367 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
371 str
<< (const char*) s
.mb_str();
376 bool wxVariantDataLong::Write(wxString
& str
) const
378 str
.Printf(wxT("%ld"), m_value
);
382 #if wxUSE_STD_IOSTREAM
383 bool wxVariantDataLong::Read(wxSTD istream
& str
)
391 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
393 wxTextOutputStream
s(str
);
395 s
.Write32((size_t)m_value
);
399 bool wxVariantDataLong::Read(wxInputStream
& str
)
401 wxTextInputStream
s(str
);
402 m_value
= s
.Read32();
405 #endif // wxUSE_STREAMS
407 bool wxVariantDataLong::Read(wxString
& str
)
409 m_value
= wxAtol((const wxChar
*) str
);
417 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
419 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
421 wxVariantDataReal() { m_value
= 0.0; }
422 wxVariantDataReal(double value
) { m_value
= value
; }
424 inline double GetValue() const { return m_value
; }
425 inline void SetValue(double value
) { m_value
= value
; }
427 virtual void Copy(wxVariantData
& data
);
428 virtual bool Eq(wxVariantData
& data
) const;
429 virtual bool Read(wxString
& str
);
430 #if wxUSE_STD_IOSTREAM
431 virtual bool Write(wxSTD ostream
& str
) const;
433 virtual bool Write(wxString
& str
) const;
434 #if wxUSE_STD_IOSTREAM
435 virtual bool Read(wxSTD istream
& str
);
438 virtual bool Read(wxInputStream
& str
);
439 virtual bool Write(wxOutputStream
&str
) const;
440 #endif // wxUSE_STREAMS
441 virtual wxString
GetType() const { return wxT("double"); };
447 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
449 void wxVariantDataReal::Copy(wxVariantData
& data
)
451 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
453 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
455 otherData
.m_value
= m_value
;
458 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
460 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
462 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
464 return (otherData
.m_value
== m_value
);
467 #if wxUSE_STD_IOSTREAM
468 bool wxVariantDataReal::Write(wxSTD ostream
& str
) const
472 str
<< (const char*) s
.mb_str();
477 bool wxVariantDataReal::Write(wxString
& str
) const
479 str
.Printf(wxT("%.4f"), m_value
);
483 #if wxUSE_STD_IOSTREAM
484 bool wxVariantDataReal::Read(wxSTD istream
& str
)
492 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
494 wxTextOutputStream
s(str
);
495 s
.WriteDouble((double)m_value
);
499 bool wxVariantDataReal::Read(wxInputStream
& str
)
501 wxTextInputStream
s(str
);
502 m_value
= (float)s
.ReadDouble();
505 #endif // wxUSE_STREAMS
507 bool wxVariantDataReal::Read(wxString
& str
)
509 m_value
= wxAtof((const wxChar
*) str
);
518 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
520 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
522 wxVariantDataBool() { m_value
= 0; }
523 wxVariantDataBool(bool value
) { m_value
= value
; }
525 inline bool GetValue() const { return m_value
; }
526 inline void SetValue(bool value
) { m_value
= value
; }
528 virtual void Copy(wxVariantData
& data
);
529 virtual bool Eq(wxVariantData
& data
) const;
530 #if wxUSE_STD_IOSTREAM
531 virtual bool Write(wxSTD ostream
& str
) const;
533 virtual bool Write(wxString
& str
) const;
534 virtual bool Read(wxString
& str
);
535 #if wxUSE_STD_IOSTREAM
536 virtual bool Read(wxSTD istream
& str
);
539 virtual bool Read(wxInputStream
& str
);
540 virtual bool Write(wxOutputStream
& str
) const;
541 #endif // wxUSE_STREAMS
542 virtual wxString
GetType() const { return wxT("bool"); };
548 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
550 void wxVariantDataBool::Copy(wxVariantData
& data
)
552 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
554 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
556 otherData
.m_value
= m_value
;
559 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
561 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
563 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
565 return (otherData
.m_value
== m_value
);
568 #if wxUSE_STD_IOSTREAM
569 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
573 str
<< (const char*) s
.mb_str();
578 bool wxVariantDataBool::Write(wxString
& str
) const
580 str
.Printf(wxT("%d"), (int) m_value
);
584 #if wxUSE_STD_IOSTREAM
585 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
587 wxFAIL_MSG(wxT("Unimplemented"));
588 // str >> (long) m_value;
594 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
596 wxTextOutputStream
s(str
);
602 bool wxVariantDataBool::Read(wxInputStream
& str
)
604 wxTextInputStream
s(str
);
606 m_value
= s
.Read8() != 0;
609 #endif // wxUSE_STREAMS
611 bool wxVariantDataBool::Read(wxString
& str
)
613 m_value
= (wxAtol((const wxChar
*) str
) != 0);
622 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
624 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
626 wxVariantDataChar() { m_value
= 0; }
627 wxVariantDataChar(char value
) { m_value
= value
; }
629 inline char GetValue() const { return m_value
; }
630 inline void SetValue(char value
) { m_value
= value
; }
632 virtual void Copy(wxVariantData
& data
);
633 virtual bool Eq(wxVariantData
& data
) const;
634 #if wxUSE_STD_IOSTREAM
635 virtual bool Read(wxSTD istream
& str
);
636 virtual bool Write(wxSTD ostream
& str
) const;
638 virtual bool Read(wxString
& str
);
639 virtual bool Write(wxString
& str
) const;
641 virtual bool Read(wxInputStream
& str
);
642 virtual bool Write(wxOutputStream
& str
) const;
643 #endif // wxUSE_STREAMS
644 virtual wxString
GetType() const { return wxT("char"); };
650 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
652 void wxVariantDataChar::Copy(wxVariantData
& data
)
654 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
656 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
658 otherData
.m_value
= m_value
;
661 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
663 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
665 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
667 return (otherData
.m_value
== m_value
);
670 #if wxUSE_STD_IOSTREAM
671 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
675 str
<< (const char*) s
.mb_str();
680 bool wxVariantDataChar::Write(wxString
& str
) const
682 str
.Printf(wxT("%c"), m_value
);
686 #if wxUSE_STD_IOSTREAM
687 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
689 wxFAIL_MSG(wxT("Unimplemented"));
696 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
698 wxTextOutputStream
s(str
);
704 bool wxVariantDataChar::Read(wxInputStream
& str
)
706 wxTextInputStream
s(str
);
711 #endif // wxUSE_STREAMS
713 bool wxVariantDataChar::Read(wxString
& str
)
715 m_value
= str
[(size_t)0];
720 * wxVariantDataString
723 #if defined(__BORLANDC__) && defined(__WIN16__)
724 // Change name because of truncation
725 #define wxVariantDataString wxVariantStringData
728 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
730 #if defined(__BORLANDC__) && defined(__WIN16__)
731 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
733 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
736 wxVariantDataString() { }
737 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
739 inline wxString
GetValue() const { return m_value
; }
740 inline void SetValue(const wxString
& value
) { m_value
= value
; }
742 virtual void Copy(wxVariantData
& data
);
743 virtual bool Eq(wxVariantData
& data
) const;
744 #if wxUSE_STD_IOSTREAM
745 virtual bool Write(wxSTD ostream
& str
) const;
747 virtual bool Read(wxString
& str
);
748 virtual bool Write(wxString
& str
) const;
749 #if wxUSE_STD_IOSTREAM
750 virtual bool Read(wxSTD istream
& str
);
753 virtual bool Read(wxInputStream
& str
);
754 virtual bool Write(wxOutputStream
& str
) const;
755 #endif // wxUSE_STREAMS
756 virtual wxString
GetType() const { return wxT("string"); };
762 void wxVariantDataString::Copy(wxVariantData
& data
)
764 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
766 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
768 otherData
.m_value
= m_value
;
771 bool wxVariantDataString::Eq(wxVariantData
& data
) const
773 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
775 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
777 return (otherData
.m_value
== m_value
);
780 #if wxUSE_STD_IOSTREAM
781 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
783 str
<< (const char*) m_value
.mb_str();
788 bool wxVariantDataString::Write(wxString
& str
) const
794 #if wxUSE_STD_IOSTREAM
795 bool wxVariantDataString::Read(wxSTD istream
& str
)
803 bool wxVariantDataString::Write(wxOutputStream
& str
) const
805 // why doesn't wxOutputStream::operator<< take "const wxString&"
806 wxTextOutputStream
s(str
);
807 s
.WriteString(m_value
);
811 bool wxVariantDataString::Read(wxInputStream
& str
)
813 wxTextInputStream
s(str
);
815 m_value
= s
.ReadString();
818 #endif // wxUSE_STREAMS
820 bool wxVariantDataString::Read(wxString
& str
)
826 #if defined(__BORLANDC__) && defined(__WIN16__)
827 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
829 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
836 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
837 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
839 class wxVariantDataTime
: public wxVariantData
841 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
843 wxVariantDataTime() { }
844 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
846 inline wxTime
GetValue() const { return m_value
; }
847 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
849 virtual void Copy(wxVariantData
& data
);
850 virtual bool Eq(wxVariantData
& data
) const;
851 #if wxUSE_STD_IOSTREAM
852 virtual bool Write(wxSTD ostream
& str
) const;
854 virtual bool Write(wxString
& str
) const;
855 #if wxUSE_STD_IOSTREAM
856 virtual bool Read(wxSTD istream
& str
);
858 virtual bool Read(wxString
& str
);
859 virtual wxString
GetType() const { return wxT("time"); };
860 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
866 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
868 void wxVariantDataTime::Copy(wxVariantData
& data
)
870 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Copy: Can't copy to this type of data") );
872 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
874 otherData
.m_value
= m_value
;
877 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
879 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Eq: argument mismatch") );
881 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
883 return (otherData
.m_value
== m_value
);
886 #if wxUSE_STD_IOSTREAM
887 bool wxVariantDataTime::Write(wxSTD ostream
& str
) const
891 str
<< (const char*) s
.mb_str();
896 bool wxVariantDataTime::Write(wxString
& str
) const
898 wxChar
*s
= m_value
.FormatTime();
903 #if wxUSE_STD_IOSTREAM
904 bool wxVariantDataTime::Read(wxSTD istream
& WXUNUSED(str
))
911 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
921 class wxVariantDataDate
: public wxVariantData
923 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
925 wxVariantDataDate() { }
926 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
928 inline wxDate
GetValue() const { return m_value
; }
929 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
931 virtual void Copy(wxVariantData
& data
);
932 virtual bool Eq(wxVariantData
& data
) const;
933 #if wxUSE_STD_IOSTREAM
934 virtual bool Write(wxSTD ostream
& str
) const;
936 virtual bool Write(wxString
& str
) const;
937 #if wxUSE_STD_IOSTREAM
938 virtual bool Read(wxSTD istream
& str
);
940 virtual bool Read(wxString
& str
);
941 virtual wxString
GetType() const { return wxT("date"); };
942 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
948 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
950 void wxVariantDataDate::Copy(wxVariantData
& data
)
952 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Copy: Can't copy to this type of data") );
954 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
956 otherData
.m_value
= m_value
;
959 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
961 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Eq: argument mismatch") );
963 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
965 return (otherData
.m_value
== m_value
);
968 #if wxUSE_STD_IOSTREAM
969 bool wxVariantDataDate::Write(wxSTD ostream
& str
) const
973 str
<< (const char*) s
.mb_str();
978 bool wxVariantDataDate::Write(wxString
& str
) const
980 str
= m_value
.FormatDate();
984 #if wxUSE_STD_IOSTREAM
985 bool wxVariantDataDate::Read(wxSTD istream
& WXUNUSED(str
))
992 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
1001 * wxVariantDataVoidPtr
1004 class wxVariantDataVoidPtr
: public wxVariantData
1006 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
1008 wxVariantDataVoidPtr() { }
1009 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1011 inline void* GetValue() const { return m_value
; }
1012 inline void SetValue(void* value
) { m_value
= value
; }
1014 virtual void Copy(wxVariantData
& data
);
1015 virtual bool Eq(wxVariantData
& data
) const;
1016 #if wxUSE_STD_IOSTREAM
1017 virtual bool Write(wxSTD ostream
& str
) const;
1019 virtual bool Write(wxString
& str
) const;
1020 #if wxUSE_STD_IOSTREAM
1021 virtual bool Read(wxSTD istream
& str
);
1023 virtual bool Read(wxString
& str
);
1024 virtual wxString
GetType() const { return wxT("void*"); };
1025 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1030 DECLARE_NO_COPY_CLASS(wxVariantDataVoidPtr
)
1033 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1035 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1037 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1039 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1041 otherData
.m_value
= m_value
;
1044 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1046 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1048 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1050 return (otherData
.m_value
== m_value
);
1053 #if wxUSE_STD_IOSTREAM
1054 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1058 str
<< (const char*) s
.mb_str();
1063 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1065 str
.Printf(wxT("%ld"), (long) m_value
);
1069 #if wxUSE_STD_IOSTREAM
1070 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1077 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1084 * wxVariantDataDateTime
1089 class wxVariantDataDateTime
: public wxVariantData
1091 DECLARE_DYNAMIC_CLASS(wxVariantDataDateTime
)
1094 wxVariantDataDateTime() { }
1095 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1097 wxVariantDataDateTime(const TIME_STRUCT
* valptr
)
1098 { m_value
= wxDateTime(valptr
->hour
, valptr
->minute
, valptr
->second
); }
1099 wxVariantDataDateTime(const DATE_STRUCT
* valptr
)
1100 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1),valptr
->year
); }
1101 wxVariantDataDateTime(const TIMESTAMP_STRUCT
* valptr
)
1102 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1), valptr
->year
,
1103 valptr
->hour
, valptr
->minute
, valptr
->second
, valptr
->fraction
); }
1106 inline wxDateTime
GetValue() const { return m_value
; }
1107 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1109 virtual void Copy(wxVariantData
& data
);
1110 virtual bool Eq(wxVariantData
& data
) const;
1111 #if wxUSE_STD_IOSTREAM
1112 virtual bool Write(wxSTD ostream
& str
) const;
1114 virtual bool Write(wxString
& str
) const;
1115 #if wxUSE_STD_IOSTREAM
1116 virtual bool Read(wxSTD istream
& str
);
1118 virtual bool Read(wxString
& str
);
1119 virtual wxString
GetType() const { return wxT("datetime"); };
1120 virtual wxVariantData
* Clone() { return new wxVariantDataDateTime
; }
1127 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDateTime
, wxVariantData
)
1129 void wxVariantDataDateTime::Copy(wxVariantData
& data
)
1131 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Copy: Can't copy to this type of data") );
1133 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1135 otherData
.m_value
= m_value
;
1139 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1141 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1143 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1145 return (otherData
.m_value
== m_value
);
1149 #if wxUSE_STD_IOSTREAM
1150 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1158 bool wxVariantDataDateTime::Write(wxString
& str
) const
1160 str
= m_value
.Format();
1165 #if wxUSE_STD_IOSTREAM
1166 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1174 bool wxVariantDataDateTime::Read(wxString
& str
)
1176 if(! m_value
.ParseDateTime(str
))
1181 #endif // wxUSE_DATETIME
1183 // ----------------------------------------------------------------------------
1184 // wxVariantDataArrayString
1185 // ----------------------------------------------------------------------------
1187 class wxVariantDataArrayString
: public wxVariantData
1190 wxVariantDataArrayString() { }
1191 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1193 wxArrayString
GetValue() const { return m_value
; }
1194 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1196 virtual void Copy(wxVariantData
& data
);
1197 virtual bool Eq(wxVariantData
& data
) const;
1198 #if wxUSE_STD_IOSTREAM
1199 virtual bool Write(wxSTD ostream
& str
) const;
1201 virtual bool Write(wxString
& str
) const;
1202 #if wxUSE_STD_IOSTREAM
1203 virtual bool Read(wxSTD istream
& str
);
1205 virtual bool Read(wxString
& str
);
1206 virtual wxString
GetType() const { return wxT("arrstring"); };
1207 virtual wxVariantData
* Clone() { return new wxVariantDataArrayString
; }
1210 wxArrayString m_value
;
1212 DECLARE_DYNAMIC_CLASS(wxVariantDataArrayString
)
1215 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataArrayString
, wxVariantData
)
1217 void wxVariantDataArrayString::Copy(wxVariantData
& data
)
1219 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Copy: Can't copy to this type of data") );
1221 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1223 otherData
.m_value
= m_value
;
1227 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1229 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1231 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1233 return otherData
.m_value
== m_value
;
1237 #if wxUSE_STD_IOSTREAM
1238 bool wxVariantDataArrayString::Write(wxSTD ostream
& str
) const
1246 bool wxVariantDataArrayString::Write(wxString
& str
) const
1248 size_t count
= m_value
.GetCount();
1249 for ( size_t n
= 0; n
< count
; n
++ )
1261 #if wxUSE_STD_IOSTREAM
1262 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1270 bool wxVariantDataArrayString::Read(wxString
& str
)
1272 wxStringTokenizer
tk(str
, _T(";"));
1273 while ( tk
.HasMoreTokens() )
1275 m_value
.Add(tk
.GetNextToken());
1287 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1289 // Construction & destruction
1290 wxVariant::wxVariant()
1292 m_data
= (wxVariantData
*) NULL
;
1295 wxVariant::wxVariant(double val
, const wxString
& name
)
1297 m_data
= new wxVariantDataReal(val
);
1301 wxVariant::wxVariant(long val
, const wxString
& name
)
1303 m_data
= new wxVariantDataLong(val
);
1308 wxVariant::wxVariant(bool val
, const wxString
& name
)
1310 m_data
= new wxVariantDataBool(val
);
1315 wxVariant::wxVariant(char val
, const wxString
& name
)
1317 m_data
= new wxVariantDataChar(val
);
1321 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1323 m_data
= new wxVariantDataString(val
);
1327 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1329 m_data
= new wxVariantDataString(wxString(val
));
1333 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1335 m_data
= new wxVariantDataStringList(val
);
1339 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1341 m_data
= new wxVariantDataList(val
);
1345 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1346 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1347 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1349 m_data
= new wxVariantDataTime(val
);
1353 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1355 m_data
= new wxVariantDataDate(val
);
1360 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1362 m_data
= new wxVariantDataVoidPtr(val
);
1367 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1369 m_data
= new wxVariantDataDateTime(val
);
1372 #endif // wxUSE_DATETIME
1375 wxVariant::wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
) // Date
1377 m_data
= new wxVariantDataDateTime(valptr
);
1381 wxVariant::wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
) // Date
1383 m_data
= new wxVariantDataDateTime(valptr
);
1387 wxVariant::wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
) // Date
1389 m_data
= new wxVariantDataDateTime(valptr
);
1392 #endif // wxUSE_ODBC
1394 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1396 m_data
= new wxVariantDataArrayString(val
);
1400 wxVariant::wxVariant(const wxVariant
& variant
)
1403 if (!variant
.IsNull())
1405 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1406 variant
.m_data
->Copy(*m_data
);
1409 m_data
= (wxVariantData
*) NULL
;
1410 m_name
= variant
.m_name
;
1413 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1419 wxVariant::~wxVariant()
1425 // Make NULL (i.e. delete the data)
1426 void wxVariant::MakeNull()
1432 // Generic operators
1434 void wxVariant::operator= (const wxVariant
& variant
)
1436 if (variant
.IsNull())
1442 if (IsNull() || (GetType() != variant
.GetType()))
1446 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1449 variant
.GetData()->Copy(* GetData());
1451 m_name
= variant
.m_name
;
1454 // Assignment using data, e.g.
1455 // myVariant = new wxStringVariantData("hello")
1456 void wxVariant::operator= (wxVariantData
* variantData
)
1459 m_data
= variantData
;
1462 bool wxVariant::operator== (const wxVariant
& variant
) const
1464 if (IsNull() || variant
.IsNull())
1465 return (IsNull() == variant
.IsNull());
1467 return (GetData()->Eq(* variant
.GetData()));
1470 bool wxVariant::operator!= (const wxVariant
& variant
) const
1472 return (!(*this == variant
));
1476 // Specific operators
1477 bool wxVariant::operator== (double value
) const
1480 if (!Convert(&thisValue
))
1483 return (value
== thisValue
);
1486 bool wxVariant::operator!= (double value
) const
1488 return (!((*this) == value
));
1491 void wxVariant::operator= (double value
)
1493 if (GetType() == wxT("double"))
1495 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1501 m_data
= new wxVariantDataReal(value
);
1505 bool wxVariant::operator== (long value
) const
1508 if (!Convert(&thisValue
))
1511 return (value
== thisValue
);
1514 bool wxVariant::operator!= (long value
) const
1516 return (!((*this) == value
));
1519 void wxVariant::operator= (long value
)
1521 if (GetType() == wxT("long"))
1523 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1529 m_data
= new wxVariantDataLong(value
);
1533 bool wxVariant::operator== (char value
) const
1536 if (!Convert(&thisValue
))
1539 return (value
== thisValue
);
1542 bool wxVariant::operator!= (char value
) const
1544 return (!((*this) == value
));
1547 void wxVariant::operator= (char value
)
1549 if (GetType() == wxT("char"))
1551 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1557 m_data
= new wxVariantDataChar(value
);
1562 bool wxVariant::operator== (bool value
) const
1565 if (!Convert(&thisValue
))
1568 return (value
== thisValue
);
1571 bool wxVariant::operator!= (bool value
) const
1573 return (!((*this) == value
));
1576 void wxVariant::operator= (bool value
)
1578 if (GetType() == wxT("bool"))
1580 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1586 m_data
= new wxVariantDataBool(value
);
1591 bool wxVariant::operator== (const wxString
& value
) const
1594 if (!Convert(&thisValue
))
1597 return value
== thisValue
;
1600 bool wxVariant::operator!= (const wxString
& value
) const
1602 return (!((*this) == value
));
1605 void wxVariant::operator= (const wxString
& value
)
1607 if (GetType() == wxT("string"))
1609 ((wxVariantDataString
*)GetData())->SetValue(value
);
1615 m_data
= new wxVariantDataString(value
);
1619 void wxVariant::operator= (const wxChar
* value
)
1621 if (GetType() == wxT("string"))
1623 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1629 m_data
= new wxVariantDataString(wxString(value
));
1633 bool wxVariant::operator== (const wxStringList
& value
) const
1635 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1637 wxVariantDataStringList
other(value
);
1638 return (m_data
->Eq(other
));
1641 bool wxVariant::operator!= (const wxStringList
& value
) const
1643 return (!((*this) == value
));
1646 void wxVariant::operator= (const wxStringList
& value
)
1648 if (GetType() == wxT("stringlist"))
1650 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1656 m_data
= new wxVariantDataStringList(value
);
1660 bool wxVariant::operator== (const wxList
& value
) const
1662 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1664 wxVariantDataList
other(value
);
1665 return (m_data
->Eq(other
));
1668 bool wxVariant::operator!= (const wxList
& value
) const
1670 return (!((*this) == value
));
1673 void wxVariant::operator= (const wxList
& value
)
1675 if (GetType() == wxT("list"))
1677 ((wxVariantDataList
*)GetData())->SetValue(value
);
1683 m_data
= new wxVariantDataList(value
);
1687 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1688 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1689 bool wxVariant::operator== (const wxTime
& value
) const
1692 if (!Convert(&thisValue
))
1695 return value
== thisValue
;
1698 bool wxVariant::operator!= (const wxTime
& value
) const
1700 return (!((*this) == value
));
1703 void wxVariant::operator= (const wxTime
& value
)
1705 if (GetType() == wxT("time"))
1707 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1713 m_data
= new wxVariantDataTime(value
);
1717 bool wxVariant::operator== (const wxDate
& value
) const
1720 if (!Convert(&thisValue
))
1723 return (value
== thisValue
);
1726 bool wxVariant::operator!= (const wxDate
& value
) const
1728 return (!((*this) == value
));
1731 void wxVariant::operator= (const wxDate
& value
)
1733 if (GetType() == wxT("date"))
1735 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1741 m_data
= new wxVariantDataDate(value
);
1746 bool wxVariant::operator== (void* value
) const
1748 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1751 bool wxVariant::operator!= (void* value
) const
1753 return (!((*this) == (void*) value
));
1756 void wxVariant::operator= (void* value
)
1758 if (GetType() == wxT("void*"))
1760 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1766 m_data
= new wxVariantDataVoidPtr(value
);
1771 bool wxVariant::operator== (const wxDateTime
& value
) const
1773 wxDateTime thisValue
;
1774 if (!Convert(&thisValue
))
1777 return value
.IsEqualTo(thisValue
);
1780 bool wxVariant::operator!= (const wxDateTime
& value
) const
1782 return (!((*this) == value
));
1785 void wxVariant::operator= (const wxDateTime
& value
)
1787 if (GetType() == wxT("datetime"))
1789 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1795 m_data
= new wxVariantDataDateTime(value
);
1798 #endif // wxUSE_DATETIME
1801 void wxVariant::operator= (const DATE_STRUCT
* value
)
1805 m_data
= new wxVariantDataDateTime(value
);
1809 void wxVariant::operator= (const TIME_STRUCT
* value
)
1813 m_data
= new wxVariantDataDateTime(value
);
1817 void wxVariant::operator= (const TIMESTAMP_STRUCT
* value
)
1821 m_data
= new wxVariantDataDateTime(value
);
1824 #endif // wxUSE_ODBC
1826 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1828 wxFAIL_MSG( _T("TODO") );
1833 bool wxVariant::operator!=(const wxArrayString
& value
) const
1835 return !(*this == value
);
1838 void wxVariant::operator=(const wxArrayString
& value
)
1840 if (GetType() == wxT("arrstring"))
1842 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1847 m_data
= new wxVariantDataArrayString(value
);
1851 wxArrayString
wxVariant::GetArrayString() const
1853 if ( GetType() == wxT("arrstring") )
1854 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1856 return wxArrayString();
1860 // Treat a list variant as an array
1861 wxVariant
wxVariant::operator[] (size_t idx
) const
1863 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1865 if (GetType() == wxT("list"))
1867 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1868 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1869 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1871 else if (GetType() == wxT("stringlist"))
1873 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1874 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1876 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Item(idx
)->GetData()) ));
1879 return wxNullVariant
;
1882 wxVariant
& wxVariant::operator[] (size_t idx
)
1884 // We can't return a reference to a variant for a string list, since the string
1885 // is actually stored as a char*, not a variant.
1887 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1889 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1890 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1892 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1895 // Return the number of elements in a list
1896 int wxVariant::GetCount() const
1898 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1900 if (GetType() == wxT("list"))
1902 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1903 return data
->GetValue().GetCount();
1905 else if (GetType() == wxT("stringlist"))
1907 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1908 return data
->GetValue().GetCount();
1913 wxString
wxVariant::MakeString() const
1918 if (GetData()->Write(str
))
1921 return wxString(wxT(""));
1926 void wxVariant::SetData(wxVariantData
* data
)
1928 if (m_data
) delete m_data
;
1933 // Returns a string representing the type of the variant,
1934 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1935 wxString
wxVariant::GetType() const
1938 return wxString(wxT("null"));
1940 return m_data
->GetType();
1944 bool wxVariant::IsType(const wxString
& type
) const
1946 return (GetType() == type
);
1951 double wxVariant::GetReal() const
1954 if (Convert(& value
))
1958 wxFAIL_MSG(wxT("Could not convert to a real number"));
1963 long wxVariant::GetInteger() const
1966 if (Convert(& value
))
1970 wxFAIL_MSG(wxT("Could not convert to an integer"));
1975 char wxVariant::GetChar() const
1978 if (Convert(& value
))
1982 wxFAIL_MSG(wxT("Could not convert to a char"));
1987 bool wxVariant::GetBool() const
1990 if (Convert(& value
))
1994 wxFAIL_MSG(wxT("Could not convert to a bool"));
1999 wxString
wxVariant::GetString() const
2002 if (!Convert(& value
))
2004 wxFAIL_MSG(wxT("Could not convert to a string"));
2010 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
2011 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
2012 wxTime
wxVariant::GetTime() const
2015 if (!Convert(& value
))
2017 wxFAIL_MSG(wxT("Could not convert to a time"));
2023 wxDate
wxVariant::GetDate() const
2026 if (!Convert(& value
))
2028 wxFAIL_MSG(wxT("Could not convert to a date"));
2033 #endif // wxUSE_TIMEDATE
2035 void* wxVariant::GetVoidPtr() const
2037 wxASSERT( (GetType() == wxT("void*")) );
2039 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
2043 wxDateTime
wxVariant::GetDateTime() const
2046 if (!Convert(& value
))
2048 wxFAIL_MSG(wxT("Could not convert to a datetime"));
2053 #endif // wxUSE_DATETIME
2055 wxList
& wxVariant::GetList() const
2057 wxASSERT( (GetType() == wxT("list")) );
2059 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
2062 wxStringList
& wxVariant::GetStringList() const
2064 wxASSERT( (GetType() == wxT("stringlist")) );
2066 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
2070 void wxVariant::NullList()
2072 SetData(new wxVariantDataList());
2076 void wxVariant::Append(const wxVariant
& value
)
2078 wxList
& list
= GetList();
2080 list
.Append(new wxVariant(value
));
2083 // Insert at front of list
2084 void wxVariant::Insert(const wxVariant
& value
)
2086 wxList
& list
= GetList();
2088 list
.Insert(new wxVariant(value
));
2091 // Returns TRUE if the variant is a member of the list
2092 bool wxVariant::Member(const wxVariant
& value
) const
2094 wxList
& list
= GetList();
2096 wxNode
* node
= list
.GetFirst();
2099 wxVariant
* other
= (wxVariant
*) node
->GetData();
2100 if (value
== *other
)
2102 node
= node
->GetNext();
2107 // Deletes the nth element of the list
2108 bool wxVariant::Delete(int item
)
2110 wxList
& list
= GetList();
2112 wxASSERT_MSG( (item
< (int) list
.GetCount()), wxT("Invalid index to Delete") );
2113 wxNode
* node
= list
.Item(item
);
2114 wxVariant
* variant
= (wxVariant
*) node
->GetData();
2121 void wxVariant::ClearList()
2123 if (!IsNull() && (GetType() == wxT("list")))
2125 ((wxVariantDataList
*) m_data
)->Clear();
2129 if (GetType() != wxT("list"))
2134 m_data
= new wxVariantDataList
;
2139 bool wxVariant::Convert(long* value
) const
2141 wxString
type(GetType());
2142 if (type
== wxT("double"))
2143 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
2144 else if (type
== wxT("long"))
2145 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2147 else if (type
== wxT("bool"))
2148 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2150 else if (type
== wxT("string"))
2151 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
2158 bool wxVariant::Convert(bool* value
) const
2160 wxString
type(GetType());
2161 if (type
== wxT("double"))
2162 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
2163 else if (type
== wxT("long"))
2164 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2166 else if (type
== wxT("bool"))
2167 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2169 else if (type
== wxT("string"))
2171 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2173 if (val
== wxT("TRUE") || val
== wxT("yes"))
2175 else if (val
== wxT("FALSE") || val
== wxT("no"))
2186 bool wxVariant::Convert(double* value
) const
2188 wxString
type(GetType());
2189 if (type
== wxT("double"))
2190 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
2191 else if (type
== wxT("long"))
2192 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2194 else if (type
== wxT("bool"))
2195 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2197 else if (type
== wxT("string"))
2198 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
2205 bool wxVariant::Convert(char* value
) const
2207 wxString
type(GetType());
2208 if (type
== wxT("char"))
2209 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2210 else if (type
== wxT("long"))
2211 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2213 else if (type
== wxT("bool"))
2214 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2222 bool wxVariant::Convert(wxString
* value
) const
2224 *value
= MakeString();
2228 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
2229 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
2230 bool wxVariant::Convert(wxTime
* value
) const
2232 wxString
type(GetType());
2233 if (type
== wxT("time"))
2234 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
2235 else if (type
== wxT("date"))
2236 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
2243 bool wxVariant::Convert(wxDate
* value
) const
2245 wxString
type(GetType());
2246 if (type
== wxT("date"))
2247 *value
= ((wxVariantDataDate
*)GetData())->GetValue();
2253 #endif // wxUSE_TIMEDATE
2256 bool wxVariant::Convert(wxDateTime
* value
) const
2258 wxString
type(GetType());
2259 if (type
== wxT("datetime"))
2261 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2264 // Fallback to string conversion
2266 return Convert(&val
) && (value
->ParseDate(val
));
2268 #endif // wxUSE_DATETIME