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/variant.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
43 IMPLEMENT_DYNAMIC_CLASS(wxTime
, wxObject
)
45 wxTime::tFormat
wxTime::ms_Format
= wxTime::wx12h
;
46 wxTime::tPrecision
wxTime::ms_Precision
= wxTime::wxStdMinSec
;
47 wxChar
wxTime::ms_bufTime
[128];
50 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
52 wxVariant WXDLLEXPORT wxNullVariant
;
58 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
60 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
62 wxVariantDataList() {}
63 wxVariantDataList(const wxList
& list
);
66 wxList
& GetValue() const { return (wxList
&) m_value
; }
67 void SetValue(const wxList
& value
) ;
69 virtual void Copy(wxVariantData
& data
);
70 virtual bool Eq(wxVariantData
& data
) const;
71 #if wxUSE_STD_IOSTREAM
72 virtual bool Write(ostream
& str
) const;
74 virtual bool Write(wxString
& str
) const;
75 #if wxUSE_STD_IOSTREAM
76 virtual bool Read(istream
& str
);
78 virtual bool Read(wxString
& str
);
79 virtual wxString
GetType() const { return wxT("list"); };
87 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
89 wxVariantDataList::wxVariantDataList(const wxList
& list
)
94 wxVariantDataList::~wxVariantDataList()
99 void wxVariantDataList::SetValue(const wxList
& value
)
102 wxNode
* node
= value
.First();
105 wxVariant
* var
= (wxVariant
*) node
->Data();
106 m_value
.Append(new wxVariant(*var
));
111 void wxVariantDataList::Clear()
113 wxNode
* node
= m_value
.First();
116 wxVariant
* var
= (wxVariant
*) node
->Data();
123 void wxVariantDataList::Copy(wxVariantData
& data
)
125 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
127 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
130 wxNode
* node
= m_value
.First();
133 wxVariant
* var
= (wxVariant
*) node
->Data();
134 listData
.m_value
.Append(new wxVariant(*var
));
139 bool wxVariantDataList::Eq(wxVariantData
& data
) const
141 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
143 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
144 wxNode
* node1
= m_value
.First();
145 wxNode
* node2
= listData
.GetValue().First();
146 while (node1
&& node2
)
148 wxVariant
* var1
= (wxVariant
*) node1
->Data();
149 wxVariant
* var2
= (wxVariant
*) node2
->Data();
150 if ((*var1
) != (*var2
))
152 node1
= node1
->Next();
153 node2
= node2
->Next();
155 if (node1
|| node2
) return FALSE
;
159 #if wxUSE_STD_IOSTREAM
160 bool wxVariantDataList::Write(ostream
& str
) const
164 str
<< (const char*) s
.mb_str();
169 bool wxVariantDataList::Write(wxString
& str
) const
172 wxNode
* node
= m_value
.First();
175 wxVariant
* var
= (wxVariant
*) node
->Data();
176 if (node
!= m_value
.First())
179 str
+= var
->MakeString();
186 #if wxUSE_STD_IOSTREAM
187 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
189 wxFAIL_MSG(wxT("Unimplemented"));
195 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
197 wxFAIL_MSG(wxT("Unimplemented"));
203 * wxVariantDataStringList
206 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
208 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
210 wxVariantDataStringList() {}
211 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
213 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
214 void SetValue(const wxStringList
& value
);
216 virtual void Copy(wxVariantData
& data
);
217 virtual bool Eq(wxVariantData
& data
) const;
218 #if wxUSE_STD_IOSTREAM
219 virtual bool Write(ostream
& str
) const;
221 virtual bool Write(wxString
& str
) const;
222 #if wxUSE_STD_IOSTREAM
223 virtual bool Read(istream
& str
);
225 virtual bool Read(wxString
& str
);
226 virtual wxString
GetType() const { return wxT("stringlist"); };
229 wxStringList m_value
;
232 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
234 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
239 void wxVariantDataStringList::Copy(wxVariantData
& data
)
241 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
243 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
245 listData
.m_value
= m_value
;
248 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
250 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
252 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
253 wxNode
* node1
= m_value
.First();
254 wxNode
* node2
= listData
.GetValue().First();
255 while (node1
&& node2
)
257 wxString
str1 ((wxChar
*) node1
->Data());
258 wxString
str2 ((wxChar
*) node2
->Data());
261 node1
= node1
->Next();
262 node2
= node2
->Next();
264 if (node1
|| node2
) return FALSE
;
268 #if wxUSE_STD_IOSTREAM
269 bool wxVariantDataStringList::Write(ostream
& str
) const
273 str
<< (const char*) s
.mb_str();
278 bool wxVariantDataStringList::Write(wxString
& str
) const
281 wxNode
* node
= m_value
.First();
284 wxChar
* s
= (wxChar
*) node
->Data();
285 if (node
!= m_value
.First())
294 #if wxUSE_STD_IOSTREAM
295 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
297 wxFAIL_MSG(wxT("Unimplemented"));
303 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
305 wxFAIL_MSG(wxT("Unimplemented"));
314 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
316 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
318 wxVariantDataLong() { m_value
= 0; }
319 wxVariantDataLong(long value
) { m_value
= value
; }
321 inline long GetValue() const { return m_value
; }
322 inline void SetValue(long value
) { m_value
= value
; }
324 virtual void Copy(wxVariantData
& data
);
325 virtual bool Eq(wxVariantData
& data
) const;
327 virtual bool Read(wxString
& str
);
328 virtual bool Write(wxString
& str
) const;
329 #if wxUSE_STD_IOSTREAM
330 virtual bool Read(istream
& str
);
331 virtual bool Write(ostream
& str
) const;
334 virtual bool Read(wxInputStream
& str
);
335 virtual bool Write(wxOutputStream
&str
) const;
336 #endif // wxUSE_STREAMS
338 virtual wxString
GetType() const { return wxT("long"); };
344 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
346 void wxVariantDataLong::Copy(wxVariantData
& data
)
348 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
350 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
352 otherData
.m_value
= m_value
;
355 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
357 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
359 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
361 return (otherData
.m_value
== m_value
);
364 #if wxUSE_STD_IOSTREAM
365 bool wxVariantDataLong::Write(ostream
& str
) const
369 str
<< (const char*) s
.mb_str();
374 bool wxVariantDataLong::Write(wxString
& str
) const
376 str
.Printf(wxT("%ld"), m_value
);
380 #if wxUSE_STD_IOSTREAM
381 bool wxVariantDataLong::Read(istream
& str
)
389 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
391 wxTextOutputStream
s(str
);
393 s
.Write32((size_t)m_value
);
397 bool wxVariantDataLong::Read(wxInputStream
& str
)
399 wxTextInputStream
s(str
);
400 m_value
= s
.Read32();
403 #endif // wxUSE_STREAMS
405 bool wxVariantDataLong::Read(wxString
& str
)
407 m_value
= wxAtol((const wxChar
*) str
);
415 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
417 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
419 wxVariantDataReal() { m_value
= 0.0; }
420 wxVariantDataReal(double value
) { m_value
= value
; }
422 inline double GetValue() const { return m_value
; }
423 inline void SetValue(double value
) { m_value
= value
; }
425 virtual void Copy(wxVariantData
& data
);
426 virtual bool Eq(wxVariantData
& data
) const;
427 virtual bool Read(wxString
& str
);
428 #if wxUSE_STD_IOSTREAM
429 virtual bool Write(ostream
& str
) const;
431 virtual bool Write(wxString
& str
) const;
432 #if wxUSE_STD_IOSTREAM
433 virtual bool Read(istream
& str
);
436 virtual bool Read(wxInputStream
& str
);
437 virtual bool Write(wxOutputStream
&str
) const;
438 #endif // wxUSE_STREAMS
439 virtual wxString
GetType() const { return wxT("double"); };
445 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
447 void wxVariantDataReal::Copy(wxVariantData
& data
)
449 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
451 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
453 otherData
.m_value
= m_value
;
456 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
458 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
460 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
462 return (otherData
.m_value
== m_value
);
465 #if wxUSE_STD_IOSTREAM
466 bool wxVariantDataReal::Write(ostream
& str
) const
470 str
<< (const char*) s
.mb_str();
475 bool wxVariantDataReal::Write(wxString
& str
) const
477 str
.Printf(wxT("%.4f"), m_value
);
481 #if wxUSE_STD_IOSTREAM
482 bool wxVariantDataReal::Read(istream
& str
)
490 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
492 wxTextOutputStream
s(str
);
493 s
.WriteDouble((double)m_value
);
497 bool wxVariantDataReal::Read(wxInputStream
& str
)
499 wxTextInputStream
s(str
);
500 m_value
= (float)s
.ReadDouble();
503 #endif // wxUSE_STREAMS
505 bool wxVariantDataReal::Read(wxString
& str
)
507 m_value
= wxAtof((const wxChar
*) str
);
516 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
518 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
520 wxVariantDataBool() { m_value
= 0; }
521 wxVariantDataBool(bool value
) { m_value
= value
; }
523 inline bool GetValue() const { return m_value
; }
524 inline void SetValue(bool value
) { m_value
= value
; }
526 virtual void Copy(wxVariantData
& data
);
527 virtual bool Eq(wxVariantData
& data
) const;
528 #if wxUSE_STD_IOSTREAM
529 virtual bool Write(ostream
& str
) const;
531 virtual bool Write(wxString
& str
) const;
532 virtual bool Read(wxString
& str
);
533 #if wxUSE_STD_IOSTREAM
534 virtual bool Read(istream
& str
);
537 virtual bool Read(wxInputStream
& str
);
538 virtual bool Write(wxOutputStream
& str
) const;
539 #endif // wxUSE_STREAMS
540 virtual wxString
GetType() const { return wxT("bool"); };
546 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
548 void wxVariantDataBool::Copy(wxVariantData
& data
)
550 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
552 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
554 otherData
.m_value
= m_value
;
557 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
559 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
561 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
563 return (otherData
.m_value
== m_value
);
566 #if wxUSE_STD_IOSTREAM
567 bool wxVariantDataBool::Write(ostream
& str
) const
571 str
<< (const char*) s
.mb_str();
576 bool wxVariantDataBool::Write(wxString
& str
) const
578 str
.Printf(wxT("%d"), (int) m_value
);
582 #if wxUSE_STD_IOSTREAM
583 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
585 wxFAIL_MSG(wxT("Unimplemented"));
586 // str >> (long) m_value;
592 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
594 wxTextOutputStream
s(str
);
600 bool wxVariantDataBool::Read(wxInputStream
& str
)
602 wxTextInputStream
s(str
);
604 m_value
= s
.Read8() != 0;
607 #endif // wxUSE_STREAMS
609 bool wxVariantDataBool::Read(wxString
& str
)
611 m_value
= (wxAtol((const wxChar
*) str
) != 0);
620 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
622 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
624 wxVariantDataChar() { m_value
= 0; }
625 wxVariantDataChar(char value
) { m_value
= value
; }
627 inline char GetValue() const { return m_value
; }
628 inline void SetValue(char value
) { m_value
= value
; }
630 virtual void Copy(wxVariantData
& data
);
631 virtual bool Eq(wxVariantData
& data
) const;
632 #if wxUSE_STD_IOSTREAM
633 virtual bool Read(istream
& str
);
634 virtual bool Write(ostream
& str
) const;
636 virtual bool Read(wxString
& str
);
637 virtual bool Write(wxString
& str
) const;
639 virtual bool Read(wxInputStream
& str
);
640 virtual bool Write(wxOutputStream
& str
) const;
641 #endif // wxUSE_STREAMS
642 virtual wxString
GetType() const { return wxT("char"); };
648 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
650 void wxVariantDataChar::Copy(wxVariantData
& data
)
652 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
654 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
656 otherData
.m_value
= m_value
;
659 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
661 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
663 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
665 return (otherData
.m_value
== m_value
);
668 #if wxUSE_STD_IOSTREAM
669 bool wxVariantDataChar::Write(ostream
& str
) const
673 str
<< (const char*) s
.mb_str();
678 bool wxVariantDataChar::Write(wxString
& str
) const
680 str
.Printf(wxT("%c"), m_value
);
684 #if wxUSE_STD_IOSTREAM
685 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
687 wxFAIL_MSG(wxT("Unimplemented"));
694 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
696 wxTextOutputStream
s(str
);
702 bool wxVariantDataChar::Read(wxInputStream
& str
)
704 wxTextInputStream
s(str
);
709 #endif // wxUSE_STREAMS
711 bool wxVariantDataChar::Read(wxString
& str
)
713 m_value
= str
[(size_t)0];
718 * wxVariantDataString
721 #if defined(__BORLANDC__) && defined(__WIN16__)
722 // Change name because of truncation
723 #define wxVariantDataString wxVariantStringData
726 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
728 #if defined(__BORLANDC__) && defined(__WIN16__)
729 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
731 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
734 wxVariantDataString() { }
735 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
737 inline wxString
GetValue() const { return m_value
; }
738 inline void SetValue(const wxString
& value
) { m_value
= value
; }
740 virtual void Copy(wxVariantData
& data
);
741 virtual bool Eq(wxVariantData
& data
) const;
742 #if wxUSE_STD_IOSTREAM
743 virtual bool Write(ostream
& str
) const;
745 virtual bool Read(wxString
& str
);
746 virtual bool Write(wxString
& str
) const;
747 #if wxUSE_STD_IOSTREAM
748 virtual bool Read(istream
& str
);
751 virtual bool Read(wxInputStream
& str
);
752 virtual bool Write(wxOutputStream
& str
) const;
753 #endif // wxUSE_STREAMS
754 virtual wxString
GetType() const { return wxT("string"); };
760 void wxVariantDataString::Copy(wxVariantData
& data
)
762 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
764 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
766 otherData
.m_value
= m_value
;
769 bool wxVariantDataString::Eq(wxVariantData
& data
) const
771 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
773 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
775 return (otherData
.m_value
== m_value
);
778 #if wxUSE_STD_IOSTREAM
779 bool wxVariantDataString::Write(ostream
& str
) const
781 str
<< (const char*) m_value
.mb_str();
786 bool wxVariantDataString::Write(wxString
& str
) const
792 #if wxUSE_STD_IOSTREAM
793 bool wxVariantDataString::Read(istream
& str
)
801 bool wxVariantDataString::Write(wxOutputStream
& str
) const
803 // why doesn't wxOutputStream::operator<< take "const wxString&"
804 wxTextOutputStream
s(str
);
805 s
.WriteString(m_value
);
809 bool wxVariantDataString::Read(wxInputStream
& str
)
811 wxTextInputStream
s(str
);
813 m_value
= s
.ReadString();
816 #endif // wxUSE_STREAMS
818 bool wxVariantDataString::Read(wxString
& str
)
824 #if defined(__BORLANDC__) && defined(__WIN16__)
825 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
827 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
834 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
835 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
837 class wxVariantDataTime
: public wxVariantData
839 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
841 wxVariantDataTime() { }
842 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
844 inline wxTime
GetValue() const { return m_value
; }
845 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
847 virtual void Copy(wxVariantData
& data
);
848 virtual bool Eq(wxVariantData
& data
) const;
849 #if wxUSE_STD_IOSTREAM
850 virtual bool Write(ostream
& str
) const;
852 virtual bool Write(wxString
& str
) const;
853 #if wxUSE_STD_IOSTREAM
854 virtual bool Read(istream
& str
);
856 virtual bool Read(wxString
& str
);
857 virtual wxString
GetType() const { return wxT("time"); };
858 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
864 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
866 void wxVariantDataTime::Copy(wxVariantData
& data
)
868 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Copy: Can't copy to this type of data") );
870 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
872 otherData
.m_value
= m_value
;
875 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
877 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Eq: argument mismatch") );
879 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
881 return (otherData
.m_value
== m_value
);
884 #if wxUSE_STD_IOSTREAM
885 bool wxVariantDataTime::Write(ostream
& str
) const
889 str
<< (const char*) s
.mb_str();
894 bool wxVariantDataTime::Write(wxString
& str
) const
896 wxChar
*s
= m_value
.FormatTime();
901 #if wxUSE_STD_IOSTREAM
902 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
909 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
919 class wxVariantDataDate
: public wxVariantData
921 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
923 wxVariantDataDate() { }
924 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
926 inline wxDate
GetValue() const { return m_value
; }
927 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
929 virtual void Copy(wxVariantData
& data
);
930 virtual bool Eq(wxVariantData
& data
) const;
931 #if wxUSE_STD_IOSTREAM
932 virtual bool Write(ostream
& str
) const;
934 virtual bool Write(wxString
& str
) const;
935 #if wxUSE_STD_IOSTREAM
936 virtual bool Read(istream
& str
);
938 virtual bool Read(wxString
& str
);
939 virtual wxString
GetType() const { return wxT("date"); };
940 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
946 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
948 void wxVariantDataDate::Copy(wxVariantData
& data
)
950 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Copy: Can't copy to this type of data") );
952 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
954 otherData
.m_value
= m_value
;
957 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
959 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Eq: argument mismatch") );
961 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
963 return (otherData
.m_value
== m_value
);
966 #if wxUSE_STD_IOSTREAM
967 bool wxVariantDataDate::Write(ostream
& str
) const
971 str
<< (const char*) s
.mb_str();
976 bool wxVariantDataDate::Write(wxString
& str
) const
978 str
= m_value
.FormatDate();
982 #if wxUSE_STD_IOSTREAM
983 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
990 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
999 * wxVariantDataVoidPtr
1002 class wxVariantDataVoidPtr
: public wxVariantData
1004 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
1006 wxVariantDataVoidPtr() { }
1007 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1009 inline void* GetValue() const { return m_value
; }
1010 inline void SetValue(void* value
) { m_value
= value
; }
1012 virtual void Copy(wxVariantData
& data
);
1013 virtual bool Eq(wxVariantData
& data
) const;
1014 #if wxUSE_STD_IOSTREAM
1015 virtual bool Write(ostream
& str
) const;
1017 virtual bool Write(wxString
& str
) const;
1018 #if wxUSE_STD_IOSTREAM
1019 virtual bool Read(istream
& str
);
1021 virtual bool Read(wxString
& str
);
1022 virtual wxString
GetType() const { return wxT("void*"); };
1023 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1029 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1031 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1033 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1035 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1037 otherData
.m_value
= m_value
;
1040 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1042 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1044 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1046 return (otherData
.m_value
== m_value
);
1049 #if wxUSE_STD_IOSTREAM
1050 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1054 str
<< (const char*) s
.mb_str();
1059 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1061 str
.Printf(wxT("%ld"), (long) m_value
);
1065 #if wxUSE_STD_IOSTREAM
1066 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1073 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1084 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1086 // Construction & destruction
1087 wxVariant::wxVariant()
1089 m_data
= (wxVariantData
*) NULL
;
1092 wxVariant::wxVariant(double val
, const wxString
& name
)
1094 m_data
= new wxVariantDataReal(val
);
1098 wxVariant::wxVariant(long val
, const wxString
& name
)
1100 m_data
= new wxVariantDataLong(val
);
1105 wxVariant::wxVariant(bool val
, const wxString
& name
)
1107 m_data
= new wxVariantDataBool(val
);
1112 wxVariant::wxVariant(char val
, const wxString
& name
)
1114 m_data
= new wxVariantDataChar(val
);
1118 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1120 m_data
= new wxVariantDataString(val
);
1124 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1126 m_data
= new wxVariantDataString(wxString(val
));
1130 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1132 m_data
= new wxVariantDataStringList(val
);
1136 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1138 m_data
= new wxVariantDataList(val
);
1142 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1143 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1144 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1146 m_data
= new wxVariantDataTime(val
);
1150 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1152 m_data
= new wxVariantDataDate(val
);
1157 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1159 m_data
= new wxVariantDataVoidPtr(val
);
1163 wxVariant::wxVariant(const wxVariant
& variant
)
1165 if (!variant
.IsNull())
1167 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1168 variant
.m_data
->Copy(*m_data
);
1171 m_data
= (wxVariantData
*) NULL
;
1172 m_name
= variant
.m_name
;
1175 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1181 wxVariant::~wxVariant()
1187 // Make NULL (i.e. delete the data)
1188 void wxVariant::MakeNull()
1194 // Generic operators
1196 void wxVariant::operator= (const wxVariant
& variant
)
1198 if (variant
.IsNull())
1204 if (IsNull() || (GetType() != variant
.GetType()))
1208 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1210 // GetData()->Copy(* variant.GetData());
1211 variant
.GetData()->Copy(* GetData());
1214 // Assignment using data, e.g.
1215 // myVariant = new wxStringVariantData("hello")
1216 void wxVariant::operator= (wxVariantData
* variantData
)
1219 m_data
= variantData
;
1222 bool wxVariant::operator== (const wxVariant
& variant
) const
1224 if (IsNull() || variant
.IsNull())
1225 return (IsNull() == variant
.IsNull());
1227 return (GetData()->Eq(* variant
.GetData()));
1230 bool wxVariant::operator!= (const wxVariant
& variant
) const
1232 return (!(*this == variant
));
1236 // Specific operators
1237 bool wxVariant::operator== (double value
) const
1240 if (!Convert(&thisValue
))
1243 return (value
== thisValue
);
1246 bool wxVariant::operator!= (double value
) const
1248 return (!((*this) == value
));
1251 void wxVariant::operator= (double value
)
1253 if (GetType() == wxT("double"))
1255 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1261 m_data
= new wxVariantDataReal(value
);
1265 bool wxVariant::operator== (long value
) const
1268 if (!Convert(&thisValue
))
1271 return (value
== thisValue
);
1274 bool wxVariant::operator!= (long value
) const
1276 return (!((*this) == value
));
1279 void wxVariant::operator= (long value
)
1281 if (GetType() == wxT("long"))
1283 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1289 m_data
= new wxVariantDataLong(value
);
1293 bool wxVariant::operator== (char value
) const
1296 if (!Convert(&thisValue
))
1299 return (value
== thisValue
);
1302 bool wxVariant::operator!= (char value
) const
1304 return (!((*this) == value
));
1307 void wxVariant::operator= (char value
)
1309 if (GetType() == wxT("char"))
1311 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1317 m_data
= new wxVariantDataChar(value
);
1322 bool wxVariant::operator== (bool value
) const
1325 if (!Convert(&thisValue
))
1328 return (value
== thisValue
);
1331 bool wxVariant::operator!= (bool value
) const
1333 return (!((*this) == value
));
1336 void wxVariant::operator= (bool value
)
1338 if (GetType() == wxT("bool"))
1340 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1346 m_data
= new wxVariantDataBool(value
);
1351 bool wxVariant::operator== (const wxString
& value
) const
1354 if (!Convert(&thisValue
))
1357 return value
== thisValue
;
1360 bool wxVariant::operator!= (const wxString
& value
) const
1362 return (!((*this) == value
));
1365 void wxVariant::operator= (const wxString
& value
)
1367 if (GetType() == wxT("string"))
1369 ((wxVariantDataString
*)GetData())->SetValue(value
);
1375 m_data
= new wxVariantDataString(value
);
1379 void wxVariant::operator= (const wxChar
* value
)
1381 if (GetType() == wxT("string"))
1383 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1389 m_data
= new wxVariantDataString(wxString(value
));
1393 bool wxVariant::operator== (const wxStringList
& value
) const
1395 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1397 wxVariantDataStringList
other(value
);
1398 return (m_data
->Eq(other
));
1401 bool wxVariant::operator!= (const wxStringList
& value
) const
1403 return (!((*this) == value
));
1406 void wxVariant::operator= (const wxStringList
& value
)
1408 if (GetType() == wxT("stringlist"))
1410 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1416 m_data
= new wxVariantDataStringList(value
);
1420 bool wxVariant::operator== (const wxList
& value
) const
1422 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1424 wxVariantDataList
other(value
);
1425 return (m_data
->Eq(other
));
1428 bool wxVariant::operator!= (const wxList
& value
) const
1430 return (!((*this) == value
));
1433 void wxVariant::operator= (const wxList
& value
)
1435 if (GetType() == wxT("list"))
1437 ((wxVariantDataList
*)GetData())->SetValue(value
);
1443 m_data
= new wxVariantDataList(value
);
1447 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1448 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1449 bool wxVariant::operator== (const wxTime
& value
) const
1452 if (!Convert(&thisValue
))
1455 return value
== thisValue
;
1458 bool wxVariant::operator!= (const wxTime
& value
) const
1460 return (!((*this) == value
));
1463 void wxVariant::operator= (const wxTime
& value
)
1465 if (GetType() == wxT("time"))
1467 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1473 m_data
= new wxVariantDataTime(value
);
1477 bool wxVariant::operator== (const wxDate
& value
) const
1480 if (!Convert(&thisValue
))
1483 return (value
== thisValue
);
1486 bool wxVariant::operator!= (const wxDate
& value
) const
1488 return (!((*this) == value
));
1491 void wxVariant::operator= (const wxDate
& value
)
1493 if (GetType() == wxT("date"))
1495 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1501 m_data
= new wxVariantDataDate(value
);
1506 bool wxVariant::operator== (void* value
) const
1508 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1511 bool wxVariant::operator!= (void* value
) const
1513 return (!((*this) == (void*) value
));
1516 void wxVariant::operator= (void* value
)
1518 if (GetType() == wxT("void*"))
1520 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1526 m_data
= new wxVariantDataVoidPtr(value
);
1530 // Treat a list variant as an array
1531 wxVariant
wxVariant::operator[] (size_t idx
) const
1533 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1535 if (GetType() == wxT("list"))
1537 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1538 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1539 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1541 else if (GetType() == wxT("stringlist"))
1543 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1544 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1546 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1549 return wxNullVariant
;
1552 wxVariant
& wxVariant::operator[] (size_t idx
)
1554 // We can't return a reference to a variant for a string list, since the string
1555 // is actually stored as a char*, not a variant.
1557 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1559 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1560 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1562 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1565 // Return the number of elements in a list
1566 int wxVariant::GetCount() const
1568 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1570 if (GetType() == wxT("list"))
1572 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1573 return data
->GetValue().Number();
1575 else if (GetType() == wxT("stringlist"))
1577 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1578 return data
->GetValue().Number();
1583 wxString
wxVariant::MakeString() const
1588 if (GetData()->Write(str
))
1591 return wxString(wxT(""));
1596 void wxVariant::SetData(wxVariantData
* data
)
1598 if (m_data
) delete m_data
;
1603 // Returns a string representing the type of the variant,
1604 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1605 wxString
wxVariant::GetType() const
1608 return wxString(wxT("null"));
1610 return m_data
->GetType();
1614 bool wxVariant::IsType(const wxString
& type
) const
1616 return (GetType() == type
);
1621 double wxVariant::GetReal() const
1624 if (Convert(& value
))
1628 wxFAIL_MSG(wxT("Could not convert to a real number"));
1633 long wxVariant::GetInteger() const
1636 if (Convert(& value
))
1640 wxFAIL_MSG(wxT("Could not convert to an integer"));
1645 char wxVariant::GetChar() const
1648 if (Convert(& value
))
1652 wxFAIL_MSG(wxT("Could not convert to a char"));
1657 bool wxVariant::GetBool() const
1660 if (Convert(& value
))
1664 wxFAIL_MSG(wxT("Could not convert to a bool"));
1669 wxString
wxVariant::GetString() const
1672 if (!Convert(& value
))
1674 wxFAIL_MSG(wxT("Could not convert to a string"));
1680 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1681 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1682 wxTime
wxVariant::GetTime() const
1685 if (!Convert(& value
))
1687 wxFAIL_MSG(wxT("Could not convert to a time"));
1693 wxDate
wxVariant::GetDate() const
1696 if (!Convert(& value
))
1698 wxFAIL_MSG(wxT("Could not convert to a date"));
1703 #endif // wxUSE_TIMEDATE
1705 void* wxVariant::GetVoidPtr() const
1707 wxASSERT( (GetType() == wxT("void*")) );
1709 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1712 wxList
& wxVariant::GetList() const
1714 wxASSERT( (GetType() == wxT("list")) );
1716 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1719 wxStringList
& wxVariant::GetStringList() const
1721 wxASSERT( (GetType() == wxT("stringlist")) );
1723 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1727 void wxVariant::Append(const wxVariant
& value
)
1729 wxList
& list
= GetList();
1731 list
.Append(new wxVariant(value
));
1734 // Insert at front of list
1735 void wxVariant::Insert(const wxVariant
& value
)
1737 wxList
& list
= GetList();
1739 list
.Insert(new wxVariant(value
));
1742 // Returns TRUE if the variant is a member of the list
1743 bool wxVariant::Member(const wxVariant
& value
) const
1745 wxList
& list
= GetList();
1747 wxNode
* node
= list
.First();
1750 wxVariant
* other
= (wxVariant
*) node
->Data();
1751 if (value
== *other
)
1753 node
= node
->Next();
1758 // Deletes the nth element of the list
1759 bool wxVariant::Delete(int item
)
1761 wxList
& list
= GetList();
1763 wxASSERT_MSG( (item
< list
.Number()), wxT("Invalid index to Delete") );
1764 wxNode
* node
= list
.Nth(item
);
1765 wxVariant
* variant
= (wxVariant
*) node
->Data();
1772 void wxVariant::ClearList()
1774 if (!IsNull() && (GetType() == wxT("list")))
1776 ((wxVariantDataList
*) m_data
)->Clear();
1780 if (GetType() != wxT("list"))
1785 m_data
= new wxVariantDataList
;
1790 bool wxVariant::Convert(long* value
) const
1792 wxString
type(GetType());
1793 if (type
== wxT("double"))
1794 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1795 else if (type
== wxT("long"))
1796 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1798 else if (type
== wxT("bool"))
1799 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1801 else if (type
== wxT("string"))
1802 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1809 bool wxVariant::Convert(bool* value
) const
1811 wxString
type(GetType());
1812 if (type
== wxT("double"))
1813 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1814 else if (type
== wxT("long"))
1815 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1817 else if (type
== wxT("bool"))
1818 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1820 else if (type
== wxT("string"))
1822 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1824 if (val
== wxT("true") || val
== wxT("yes"))
1826 else if (val
== wxT("false") || val
== wxT("no"))
1837 bool wxVariant::Convert(double* value
) const
1839 wxString
type(GetType());
1840 if (type
== wxT("double"))
1841 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1842 else if (type
== wxT("long"))
1843 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1845 else if (type
== wxT("bool"))
1846 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1848 else if (type
== wxT("string"))
1849 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1856 bool wxVariant::Convert(char* value
) const
1858 wxString
type(GetType());
1859 if (type
== wxT("char"))
1860 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1861 else if (type
== wxT("long"))
1862 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1864 else if (type
== wxT("bool"))
1865 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1873 bool wxVariant::Convert(wxString
* value
) const
1875 *value
= MakeString();
1879 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1880 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1881 bool wxVariant::Convert(wxTime
* value
) const
1883 wxString
type(GetType());
1884 if (type
== wxT("time"))
1885 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1886 else if (type
== wxT("date"))
1887 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1894 bool wxVariant::Convert(wxDate
* value
) const
1896 wxString
type(GetType());
1897 if (type
== wxT("date"))
1898 *value
= ((wxVariantDataDate
*)GetData())->GetValue();