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
;
49 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
51 wxVariant WXDLLEXPORT wxNullVariant
;
57 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
59 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
61 wxVariantDataList() {}
62 wxVariantDataList(const wxList
& list
);
65 wxList
& GetValue() const { return (wxList
&) m_value
; }
66 void SetValue(const wxList
& value
) ;
68 virtual void Copy(wxVariantData
& data
);
69 virtual bool Eq(wxVariantData
& data
) const;
70 #if wxUSE_STD_IOSTREAM
71 virtual bool Write(ostream
& str
) const;
73 virtual bool Write(wxString
& str
) const;
74 #if wxUSE_STD_IOSTREAM
75 virtual bool Read(istream
& str
);
77 virtual bool Read(wxString
& str
);
78 virtual wxString
GetType() const { return wxT("list"); };
86 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
88 wxVariantDataList::wxVariantDataList(const wxList
& list
)
93 wxVariantDataList::~wxVariantDataList()
98 void wxVariantDataList::SetValue(const wxList
& value
)
101 wxNode
* node
= value
.First();
104 wxVariant
* var
= (wxVariant
*) node
->Data();
105 m_value
.Append(new wxVariant(*var
));
110 void wxVariantDataList::Clear()
112 wxNode
* node
= m_value
.First();
115 wxVariant
* var
= (wxVariant
*) node
->Data();
122 void wxVariantDataList::Copy(wxVariantData
& data
)
124 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
126 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
129 wxNode
* node
= m_value
.First();
132 wxVariant
* var
= (wxVariant
*) node
->Data();
133 listData
.m_value
.Append(new wxVariant(*var
));
138 bool wxVariantDataList::Eq(wxVariantData
& data
) const
140 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
142 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
143 wxNode
* node1
= m_value
.First();
144 wxNode
* node2
= listData
.GetValue().First();
145 while (node1
&& node2
)
147 wxVariant
* var1
= (wxVariant
*) node1
->Data();
148 wxVariant
* var2
= (wxVariant
*) node2
->Data();
149 if ((*var1
) != (*var2
))
151 node1
= node1
->Next();
152 node2
= node2
->Next();
154 if (node1
|| node2
) return FALSE
;
158 #if wxUSE_STD_IOSTREAM
159 bool wxVariantDataList::Write(ostream
& str
) const
163 str
<< (const char*) s
.mb_str();
168 bool wxVariantDataList::Write(wxString
& str
) const
171 wxNode
* node
= m_value
.First();
174 wxVariant
* var
= (wxVariant
*) node
->Data();
175 if (node
!= m_value
.First())
178 str
+= var
->MakeString();
185 #if wxUSE_STD_IOSTREAM
186 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
188 wxFAIL_MSG(wxT("Unimplemented"));
194 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
196 wxFAIL_MSG(wxT("Unimplemented"));
202 * wxVariantDataStringList
205 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
207 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
209 wxVariantDataStringList() {}
210 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
212 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
213 void SetValue(const wxStringList
& value
);
215 virtual void Copy(wxVariantData
& data
);
216 virtual bool Eq(wxVariantData
& data
) const;
217 #if wxUSE_STD_IOSTREAM
218 virtual bool Write(ostream
& str
) const;
220 virtual bool Write(wxString
& str
) const;
221 #if wxUSE_STD_IOSTREAM
222 virtual bool Read(istream
& str
);
224 virtual bool Read(wxString
& str
);
225 virtual wxString
GetType() const { return wxT("stringlist"); };
228 wxStringList m_value
;
231 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
233 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
238 void wxVariantDataStringList::Copy(wxVariantData
& data
)
240 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
242 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
244 listData
.m_value
= m_value
;
247 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
249 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
251 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
252 wxNode
* node1
= m_value
.First();
253 wxNode
* node2
= listData
.GetValue().First();
254 while (node1
&& node2
)
256 wxString
str1 ((wxChar
*) node1
->Data());
257 wxString
str2 ((wxChar
*) node2
->Data());
260 node1
= node1
->Next();
261 node2
= node2
->Next();
263 if (node1
|| node2
) return FALSE
;
267 #if wxUSE_STD_IOSTREAM
268 bool wxVariantDataStringList::Write(ostream
& str
) const
272 str
<< (const char*) s
.mb_str();
277 bool wxVariantDataStringList::Write(wxString
& str
) const
280 wxNode
* node
= m_value
.First();
283 wxChar
* s
= (wxChar
*) node
->Data();
284 if (node
!= m_value
.First())
293 #if wxUSE_STD_IOSTREAM
294 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
296 wxFAIL_MSG(wxT("Unimplemented"));
302 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
304 wxFAIL_MSG(wxT("Unimplemented"));
313 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
315 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
317 wxVariantDataLong() { m_value
= 0; }
318 wxVariantDataLong(long value
) { m_value
= value
; }
320 inline long GetValue() const { return m_value
; }
321 inline void SetValue(long value
) { m_value
= value
; }
323 virtual void Copy(wxVariantData
& data
);
324 virtual bool Eq(wxVariantData
& data
) const;
326 virtual bool Read(wxString
& str
);
327 virtual bool Write(wxString
& str
) const;
328 #if wxUSE_STD_IOSTREAM
329 virtual bool Read(istream
& str
);
330 virtual bool Write(ostream
& str
) const;
333 virtual bool Read(wxInputStream
& str
);
334 virtual bool Write(wxOutputStream
&str
) const;
335 #endif // wxUSE_STREAMS
337 virtual wxString
GetType() const { return wxT("long"); };
343 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
345 void wxVariantDataLong::Copy(wxVariantData
& data
)
347 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
349 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
351 otherData
.m_value
= m_value
;
354 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
356 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
358 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
360 return (otherData
.m_value
== m_value
);
363 #if wxUSE_STD_IOSTREAM
364 bool wxVariantDataLong::Write(ostream
& str
) const
368 str
<< (const char*) s
.mb_str();
373 bool wxVariantDataLong::Write(wxString
& str
) const
375 str
.Printf(wxT("%ld"), m_value
);
379 #if wxUSE_STD_IOSTREAM
380 bool wxVariantDataLong::Read(istream
& str
)
388 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
390 wxTextOutputStream
s(str
);
392 s
.Write32((size_t)m_value
);
396 bool wxVariantDataLong::Read(wxInputStream
& str
)
398 wxTextInputStream
s(str
);
399 m_value
= s
.Read32();
402 #endif // wxUSE_STREAMS
404 bool wxVariantDataLong::Read(wxString
& str
)
406 m_value
= wxAtol((const wxChar
*) str
);
414 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
416 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
418 wxVariantDataReal() { m_value
= 0.0; }
419 wxVariantDataReal(double value
) { m_value
= value
; }
421 inline double GetValue() const { return m_value
; }
422 inline void SetValue(double value
) { m_value
= value
; }
424 virtual void Copy(wxVariantData
& data
);
425 virtual bool Eq(wxVariantData
& data
) const;
426 virtual bool Read(wxString
& str
);
427 #if wxUSE_STD_IOSTREAM
428 virtual bool Write(ostream
& str
) const;
430 virtual bool Write(wxString
& str
) const;
431 #if wxUSE_STD_IOSTREAM
432 virtual bool Read(istream
& str
);
435 virtual bool Read(wxInputStream
& str
);
436 virtual bool Write(wxOutputStream
&str
) const;
437 #endif // wxUSE_STREAMS
438 virtual wxString
GetType() const { return wxT("double"); };
444 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
446 void wxVariantDataReal::Copy(wxVariantData
& data
)
448 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
450 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
452 otherData
.m_value
= m_value
;
455 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
457 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
459 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
461 return (otherData
.m_value
== m_value
);
464 #if wxUSE_STD_IOSTREAM
465 bool wxVariantDataReal::Write(ostream
& str
) const
469 str
<< (const char*) s
.mb_str();
474 bool wxVariantDataReal::Write(wxString
& str
) const
476 str
.Printf(wxT("%.4f"), m_value
);
480 #if wxUSE_STD_IOSTREAM
481 bool wxVariantDataReal::Read(istream
& str
)
489 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
491 wxTextOutputStream
s(str
);
492 s
.WriteDouble((double)m_value
);
496 bool wxVariantDataReal::Read(wxInputStream
& str
)
498 wxTextInputStream
s(str
);
499 m_value
= (float)s
.ReadDouble();
502 #endif // wxUSE_STREAMS
504 bool wxVariantDataReal::Read(wxString
& str
)
506 m_value
= wxAtof((const wxChar
*) str
);
515 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
517 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
519 wxVariantDataBool() { m_value
= 0; }
520 wxVariantDataBool(bool value
) { m_value
= value
; }
522 inline bool GetValue() const { return m_value
; }
523 inline void SetValue(bool value
) { m_value
= value
; }
525 virtual void Copy(wxVariantData
& data
);
526 virtual bool Eq(wxVariantData
& data
) const;
527 #if wxUSE_STD_IOSTREAM
528 virtual bool Write(ostream
& str
) const;
530 virtual bool Write(wxString
& str
) const;
531 virtual bool Read(wxString
& str
);
532 #if wxUSE_STD_IOSTREAM
533 virtual bool Read(istream
& str
);
536 virtual bool Read(wxInputStream
& str
);
537 virtual bool Write(wxOutputStream
& str
) const;
538 #endif // wxUSE_STREAMS
539 virtual wxString
GetType() const { return wxT("bool"); };
545 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
547 void wxVariantDataBool::Copy(wxVariantData
& data
)
549 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
551 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
553 otherData
.m_value
= m_value
;
556 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
558 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
560 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
562 return (otherData
.m_value
== m_value
);
565 #if wxUSE_STD_IOSTREAM
566 bool wxVariantDataBool::Write(ostream
& str
) const
570 str
<< (const char*) s
.mb_str();
575 bool wxVariantDataBool::Write(wxString
& str
) const
577 str
.Printf(wxT("%d"), (int) m_value
);
581 #if wxUSE_STD_IOSTREAM
582 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
584 wxFAIL_MSG(wxT("Unimplemented"));
585 // str >> (long) m_value;
591 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
593 wxTextOutputStream
s(str
);
599 bool wxVariantDataBool::Read(wxInputStream
& str
)
601 wxTextInputStream
s(str
);
603 m_value
= s
.Read8() != 0;
606 #endif // wxUSE_STREAMS
608 bool wxVariantDataBool::Read(wxString
& str
)
610 m_value
= (wxAtol((const wxChar
*) str
) != 0);
619 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
621 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
623 wxVariantDataChar() { m_value
= 0; }
624 wxVariantDataChar(char value
) { m_value
= value
; }
626 inline char GetValue() const { return m_value
; }
627 inline void SetValue(char value
) { m_value
= value
; }
629 virtual void Copy(wxVariantData
& data
);
630 virtual bool Eq(wxVariantData
& data
) const;
631 #if wxUSE_STD_IOSTREAM
632 virtual bool Read(istream
& str
);
633 virtual bool Write(ostream
& str
) const;
635 virtual bool Read(wxString
& str
);
636 virtual bool Write(wxString
& str
) const;
638 virtual bool Read(wxInputStream
& str
);
639 virtual bool Write(wxOutputStream
& str
) const;
640 #endif // wxUSE_STREAMS
641 virtual wxString
GetType() const { return wxT("char"); };
647 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
649 void wxVariantDataChar::Copy(wxVariantData
& data
)
651 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
653 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
655 otherData
.m_value
= m_value
;
658 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
660 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
662 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
664 return (otherData
.m_value
== m_value
);
667 #if wxUSE_STD_IOSTREAM
668 bool wxVariantDataChar::Write(ostream
& str
) const
672 str
<< (const char*) s
.mb_str();
677 bool wxVariantDataChar::Write(wxString
& str
) const
679 str
.Printf(wxT("%c"), m_value
);
683 #if wxUSE_STD_IOSTREAM
684 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
686 wxFAIL_MSG(wxT("Unimplemented"));
693 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
695 wxTextOutputStream
s(str
);
701 bool wxVariantDataChar::Read(wxInputStream
& str
)
703 wxTextInputStream
s(str
);
708 #endif // wxUSE_STREAMS
710 bool wxVariantDataChar::Read(wxString
& str
)
712 m_value
= str
[(size_t)0];
717 * wxVariantDataString
720 #if defined(__BORLANDC__) && defined(__WIN16__)
721 // Change name because of truncation
722 #define wxVariantDataString wxVariantStringData
725 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
727 #if defined(__BORLANDC__) && defined(__WIN16__)
728 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
730 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
733 wxVariantDataString() { }
734 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
736 inline wxString
GetValue() const { return m_value
; }
737 inline void SetValue(const wxString
& value
) { m_value
= value
; }
739 virtual void Copy(wxVariantData
& data
);
740 virtual bool Eq(wxVariantData
& data
) const;
741 #if wxUSE_STD_IOSTREAM
742 virtual bool Write(ostream
& str
) const;
744 virtual bool Read(wxString
& str
);
745 virtual bool Write(wxString
& str
) const;
746 #if wxUSE_STD_IOSTREAM
747 virtual bool Read(istream
& str
);
750 virtual bool Read(wxInputStream
& str
);
751 virtual bool Write(wxOutputStream
& str
) const;
752 #endif // wxUSE_STREAMS
753 virtual wxString
GetType() const { return wxT("string"); };
759 void wxVariantDataString::Copy(wxVariantData
& data
)
761 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
763 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
765 otherData
.m_value
= m_value
;
768 bool wxVariantDataString::Eq(wxVariantData
& data
) const
770 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
772 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
774 return (otherData
.m_value
== m_value
);
777 #if wxUSE_STD_IOSTREAM
778 bool wxVariantDataString::Write(ostream
& str
) const
780 str
<< (const char*) m_value
.mb_str();
785 bool wxVariantDataString::Write(wxString
& str
) const
791 #if wxUSE_STD_IOSTREAM
792 bool wxVariantDataString::Read(istream
& str
)
800 bool wxVariantDataString::Write(wxOutputStream
& str
) const
802 // why doesn't wxOutputStream::operator<< take "const wxString&"
803 wxTextOutputStream
s(str
);
804 s
.WriteString(m_value
);
808 bool wxVariantDataString::Read(wxInputStream
& str
)
810 wxTextInputStream
s(str
);
812 m_value
= s
.ReadString();
815 #endif // wxUSE_STREAMS
817 bool wxVariantDataString::Read(wxString
& str
)
823 #if defined(__BORLANDC__) && defined(__WIN16__)
824 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
826 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
833 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
834 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
836 class wxVariantDataTime
: public wxVariantData
838 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
840 wxVariantDataTime() { }
841 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
843 inline wxTime
GetValue() const { return m_value
; }
844 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
846 virtual void Copy(wxVariantData
& data
);
847 virtual bool Eq(wxVariantData
& data
) const;
848 #if wxUSE_STD_IOSTREAM
849 virtual bool Write(ostream
& str
) const;
851 virtual bool Write(wxString
& str
) const;
852 #if wxUSE_STD_IOSTREAM
853 virtual bool Read(istream
& str
);
855 virtual bool Read(wxString
& str
);
856 virtual wxString
GetType() const { return wxT("time"); };
857 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
863 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
865 void wxVariantDataTime::Copy(wxVariantData
& data
)
867 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Copy: Can't copy to this type of data") );
869 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
871 otherData
.m_value
= m_value
;
874 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
876 wxASSERT_MSG( (data
.GetType() == wxT("time")), wxT("wxVariantDataTime::Eq: argument mismatch") );
878 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
880 return (otherData
.m_value
== m_value
);
883 #if wxUSE_STD_IOSTREAM
884 bool wxVariantDataTime::Write(ostream
& str
) const
888 str
<< (const char*) s
.mb_str();
893 bool wxVariantDataTime::Write(wxString
& str
) const
895 wxChar
*s
= m_value
.FormatTime();
900 #if wxUSE_STD_IOSTREAM
901 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
908 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
918 class wxVariantDataDate
: public wxVariantData
920 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
922 wxVariantDataDate() { }
923 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
925 inline wxDate
GetValue() const { return m_value
; }
926 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
928 virtual void Copy(wxVariantData
& data
);
929 virtual bool Eq(wxVariantData
& data
) const;
930 #if wxUSE_STD_IOSTREAM
931 virtual bool Write(ostream
& str
) const;
933 virtual bool Write(wxString
& str
) const;
934 #if wxUSE_STD_IOSTREAM
935 virtual bool Read(istream
& str
);
937 virtual bool Read(wxString
& str
);
938 virtual wxString
GetType() const { return wxT("date"); };
939 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
945 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
947 void wxVariantDataDate::Copy(wxVariantData
& data
)
949 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Copy: Can't copy to this type of data") );
951 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
953 otherData
.m_value
= m_value
;
956 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
958 wxASSERT_MSG( (data
.GetType() == wxT("date")), wxT("wxVariantDataDate::Eq: argument mismatch") );
960 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
962 return (otherData
.m_value
== m_value
);
965 #if wxUSE_STD_IOSTREAM
966 bool wxVariantDataDate::Write(ostream
& str
) const
970 str
<< (const char*) s
.mb_str();
975 bool wxVariantDataDate::Write(wxString
& str
) const
977 str
= m_value
.FormatDate();
981 #if wxUSE_STD_IOSTREAM
982 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
989 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
998 * wxVariantDataVoidPtr
1001 class wxVariantDataVoidPtr
: public wxVariantData
1003 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
1005 wxVariantDataVoidPtr() { }
1006 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1008 inline void* GetValue() const { return m_value
; }
1009 inline void SetValue(void* value
) { m_value
= value
; }
1011 virtual void Copy(wxVariantData
& data
);
1012 virtual bool Eq(wxVariantData
& data
) const;
1013 #if wxUSE_STD_IOSTREAM
1014 virtual bool Write(ostream
& str
) const;
1016 virtual bool Write(wxString
& str
) const;
1017 #if wxUSE_STD_IOSTREAM
1018 virtual bool Read(istream
& str
);
1020 virtual bool Read(wxString
& str
);
1021 virtual wxString
GetType() const { return wxT("void*"); };
1022 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1028 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1030 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
1032 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
1034 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1036 otherData
.m_value
= m_value
;
1039 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1041 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1043 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1045 return (otherData
.m_value
== m_value
);
1048 #if wxUSE_STD_IOSTREAM
1049 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
1053 str
<< (const char*) s
.mb_str();
1058 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1060 str
.Printf(wxT("%ld"), (long) m_value
);
1064 #if wxUSE_STD_IOSTREAM
1065 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
1072 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1083 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1085 // Construction & destruction
1086 wxVariant::wxVariant()
1088 m_data
= (wxVariantData
*) NULL
;
1091 wxVariant::wxVariant(double val
, const wxString
& name
)
1093 m_data
= new wxVariantDataReal(val
);
1097 wxVariant::wxVariant(long val
, const wxString
& name
)
1099 m_data
= new wxVariantDataLong(val
);
1104 wxVariant::wxVariant(bool val
, const wxString
& name
)
1106 m_data
= new wxVariantDataBool(val
);
1111 wxVariant::wxVariant(char val
, const wxString
& name
)
1113 m_data
= new wxVariantDataChar(val
);
1117 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1119 m_data
= new wxVariantDataString(val
);
1123 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1125 m_data
= new wxVariantDataString(wxString(val
));
1129 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1131 m_data
= new wxVariantDataStringList(val
);
1135 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1137 m_data
= new wxVariantDataList(val
);
1141 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1142 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1143 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1145 m_data
= new wxVariantDataTime(val
);
1149 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1151 m_data
= new wxVariantDataDate(val
);
1156 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1158 m_data
= new wxVariantDataVoidPtr(val
);
1162 wxVariant::wxVariant(const wxVariant
& variant
)
1164 if (!variant
.IsNull())
1166 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1167 variant
.m_data
->Copy(*m_data
);
1170 m_data
= (wxVariantData
*) NULL
;
1171 m_name
= variant
.m_name
;
1174 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1180 wxVariant::~wxVariant()
1186 // Make NULL (i.e. delete the data)
1187 void wxVariant::MakeNull()
1193 // Generic operators
1195 void wxVariant::operator= (const wxVariant
& variant
)
1197 if (variant
.IsNull())
1203 if (IsNull() || (GetType() != variant
.GetType()))
1207 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1209 // GetData()->Copy(* variant.GetData());
1210 variant
.GetData()->Copy(* GetData());
1213 // Assignment using data, e.g.
1214 // myVariant = new wxStringVariantData("hello")
1215 void wxVariant::operator= (wxVariantData
* variantData
)
1218 m_data
= variantData
;
1221 bool wxVariant::operator== (const wxVariant
& variant
) const
1223 if (IsNull() || variant
.IsNull())
1224 return (IsNull() == variant
.IsNull());
1226 return (GetData()->Eq(* variant
.GetData()));
1229 bool wxVariant::operator!= (const wxVariant
& variant
) const
1231 return (!(*this == variant
));
1235 // Specific operators
1236 bool wxVariant::operator== (double value
) const
1239 if (!Convert(&thisValue
))
1242 return (value
== thisValue
);
1245 bool wxVariant::operator!= (double value
) const
1247 return (!((*this) == value
));
1250 void wxVariant::operator= (double value
)
1252 if (GetType() == wxT("double"))
1254 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1260 m_data
= new wxVariantDataReal(value
);
1264 bool wxVariant::operator== (long value
) const
1267 if (!Convert(&thisValue
))
1270 return (value
== thisValue
);
1273 bool wxVariant::operator!= (long value
) const
1275 return (!((*this) == value
));
1278 void wxVariant::operator= (long value
)
1280 if (GetType() == wxT("long"))
1282 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1288 m_data
= new wxVariantDataLong(value
);
1292 bool wxVariant::operator== (char value
) const
1295 if (!Convert(&thisValue
))
1298 return (value
== thisValue
);
1301 bool wxVariant::operator!= (char value
) const
1303 return (!((*this) == value
));
1306 void wxVariant::operator= (char value
)
1308 if (GetType() == wxT("char"))
1310 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1316 m_data
= new wxVariantDataChar(value
);
1321 bool wxVariant::operator== (bool value
) const
1324 if (!Convert(&thisValue
))
1327 return (value
== thisValue
);
1330 bool wxVariant::operator!= (bool value
) const
1332 return (!((*this) == value
));
1335 void wxVariant::operator= (bool value
)
1337 if (GetType() == wxT("bool"))
1339 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1345 m_data
= new wxVariantDataBool(value
);
1350 bool wxVariant::operator== (const wxString
& value
) const
1353 if (!Convert(&thisValue
))
1356 return value
== thisValue
;
1359 bool wxVariant::operator!= (const wxString
& value
) const
1361 return (!((*this) == value
));
1364 void wxVariant::operator= (const wxString
& value
)
1366 if (GetType() == wxT("string"))
1368 ((wxVariantDataString
*)GetData())->SetValue(value
);
1374 m_data
= new wxVariantDataString(value
);
1378 void wxVariant::operator= (const wxChar
* value
)
1380 if (GetType() == wxT("string"))
1382 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1388 m_data
= new wxVariantDataString(wxString(value
));
1392 bool wxVariant::operator== (const wxStringList
& value
) const
1394 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1396 wxVariantDataStringList
other(value
);
1397 return (m_data
->Eq(other
));
1400 bool wxVariant::operator!= (const wxStringList
& value
) const
1402 return (!((*this) == value
));
1405 void wxVariant::operator= (const wxStringList
& value
)
1407 if (GetType() == wxT("stringlist"))
1409 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1415 m_data
= new wxVariantDataStringList(value
);
1419 bool wxVariant::operator== (const wxList
& value
) const
1421 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1423 wxVariantDataList
other(value
);
1424 return (m_data
->Eq(other
));
1427 bool wxVariant::operator!= (const wxList
& value
) const
1429 return (!((*this) == value
));
1432 void wxVariant::operator= (const wxList
& value
)
1434 if (GetType() == wxT("list"))
1436 ((wxVariantDataList
*)GetData())->SetValue(value
);
1442 m_data
= new wxVariantDataList(value
);
1446 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1447 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1448 bool wxVariant::operator== (const wxTime
& value
) const
1451 if (!Convert(&thisValue
))
1454 return value
== thisValue
;
1457 bool wxVariant::operator!= (const wxTime
& value
) const
1459 return (!((*this) == value
));
1462 void wxVariant::operator= (const wxTime
& value
)
1464 if (GetType() == wxT("time"))
1466 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1472 m_data
= new wxVariantDataTime(value
);
1476 bool wxVariant::operator== (const wxDate
& value
) const
1479 if (!Convert(&thisValue
))
1482 return (value
== thisValue
);
1485 bool wxVariant::operator!= (const wxDate
& value
) const
1487 return (!((*this) == value
));
1490 void wxVariant::operator= (const wxDate
& value
)
1492 if (GetType() == wxT("date"))
1494 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1500 m_data
= new wxVariantDataDate(value
);
1505 bool wxVariant::operator== (void* value
) const
1507 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1510 bool wxVariant::operator!= (void* value
) const
1512 return (!((*this) == (void*) value
));
1515 void wxVariant::operator= (void* value
)
1517 if (GetType() == wxT("void*"))
1519 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1525 m_data
= new wxVariantDataVoidPtr(value
);
1529 // Treat a list variant as an array
1530 wxVariant
wxVariant::operator[] (size_t idx
) const
1532 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1534 if (GetType() == wxT("list"))
1536 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1537 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1538 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1540 else if (GetType() == wxT("stringlist"))
1542 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1543 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1545 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Nth(idx
)->Data()) ));
1548 return wxNullVariant
;
1551 wxVariant
& wxVariant::operator[] (size_t idx
)
1553 // We can't return a reference to a variant for a string list, since the string
1554 // is actually stored as a char*, not a variant.
1556 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1558 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1559 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), wxT("Invalid index for array") );
1561 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1564 // Return the number of elements in a list
1565 int wxVariant::GetCount() const
1567 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1569 if (GetType() == wxT("list"))
1571 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1572 return data
->GetValue().Number();
1574 else if (GetType() == wxT("stringlist"))
1576 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1577 return data
->GetValue().Number();
1582 wxString
wxVariant::MakeString() const
1587 if (GetData()->Write(str
))
1590 return wxString(wxT(""));
1595 void wxVariant::SetData(wxVariantData
* data
)
1597 if (m_data
) delete m_data
;
1602 // Returns a string representing the type of the variant,
1603 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1604 wxString
wxVariant::GetType() const
1607 return wxString(wxT("null"));
1609 return m_data
->GetType();
1613 bool wxVariant::IsType(const wxString
& type
) const
1615 return (GetType() == type
);
1620 double wxVariant::GetReal() const
1623 if (Convert(& value
))
1627 wxFAIL_MSG(wxT("Could not convert to a real number"));
1632 long wxVariant::GetInteger() const
1635 if (Convert(& value
))
1639 wxFAIL_MSG(wxT("Could not convert to an integer"));
1644 char wxVariant::GetChar() const
1647 if (Convert(& value
))
1651 wxFAIL_MSG(wxT("Could not convert to a char"));
1656 bool wxVariant::GetBool() const
1659 if (Convert(& value
))
1663 wxFAIL_MSG(wxT("Could not convert to a bool"));
1668 wxString
wxVariant::GetString() const
1671 if (!Convert(& value
))
1673 wxFAIL_MSG(wxT("Could not convert to a string"));
1679 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1680 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1681 wxTime
wxVariant::GetTime() const
1684 if (!Convert(& value
))
1686 wxFAIL_MSG(wxT("Could not convert to a time"));
1692 wxDate
wxVariant::GetDate() const
1695 if (!Convert(& value
))
1697 wxFAIL_MSG(wxT("Could not convert to a date"));
1702 #endif // wxUSE_TIMEDATE
1704 void* wxVariant::GetVoidPtr() const
1706 wxASSERT( (GetType() == wxT("void*")) );
1708 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1711 wxList
& wxVariant::GetList() const
1713 wxASSERT( (GetType() == wxT("list")) );
1715 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1718 wxStringList
& wxVariant::GetStringList() const
1720 wxASSERT( (GetType() == wxT("stringlist")) );
1722 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1726 void wxVariant::Append(const wxVariant
& value
)
1728 wxList
& list
= GetList();
1730 list
.Append(new wxVariant(value
));
1733 // Insert at front of list
1734 void wxVariant::Insert(const wxVariant
& value
)
1736 wxList
& list
= GetList();
1738 list
.Insert(new wxVariant(value
));
1741 // Returns TRUE if the variant is a member of the list
1742 bool wxVariant::Member(const wxVariant
& value
) const
1744 wxList
& list
= GetList();
1746 wxNode
* node
= list
.First();
1749 wxVariant
* other
= (wxVariant
*) node
->Data();
1750 if (value
== *other
)
1752 node
= node
->Next();
1757 // Deletes the nth element of the list
1758 bool wxVariant::Delete(int item
)
1760 wxList
& list
= GetList();
1762 wxASSERT_MSG( (item
< list
.Number()), wxT("Invalid index to Delete") );
1763 wxNode
* node
= list
.Nth(item
);
1764 wxVariant
* variant
= (wxVariant
*) node
->Data();
1771 void wxVariant::ClearList()
1773 if (!IsNull() && (GetType() == wxT("list")))
1775 ((wxVariantDataList
*) m_data
)->Clear();
1779 if (GetType() != wxT("list"))
1784 m_data
= new wxVariantDataList
;
1789 bool wxVariant::Convert(long* value
) const
1791 wxString
type(GetType());
1792 if (type
== wxT("double"))
1793 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1794 else if (type
== wxT("long"))
1795 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1797 else if (type
== wxT("bool"))
1798 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1800 else if (type
== wxT("string"))
1801 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1808 bool wxVariant::Convert(bool* value
) const
1810 wxString
type(GetType());
1811 if (type
== wxT("double"))
1812 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1813 else if (type
== wxT("long"))
1814 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1816 else if (type
== wxT("bool"))
1817 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1819 else if (type
== wxT("string"))
1821 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1823 if (val
== wxT("true") || val
== wxT("yes"))
1825 else if (val
== wxT("false") || val
== wxT("no"))
1836 bool wxVariant::Convert(double* value
) const
1838 wxString
type(GetType());
1839 if (type
== wxT("double"))
1840 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1841 else if (type
== wxT("long"))
1842 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1844 else if (type
== wxT("bool"))
1845 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1847 else if (type
== wxT("string"))
1848 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1855 bool wxVariant::Convert(char* value
) const
1857 wxString
type(GetType());
1858 if (type
== wxT("char"))
1859 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1860 else if (type
== wxT("long"))
1861 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1863 else if (type
== wxT("bool"))
1864 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1872 bool wxVariant::Convert(wxString
* value
) const
1874 *value
= MakeString();
1878 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
1879 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
1880 bool wxVariant::Convert(wxTime
* value
) const
1882 wxString
type(GetType());
1883 if (type
== wxT("time"))
1884 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1885 else if (type
== wxT("date"))
1886 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1893 bool wxVariant::Convert(wxDate
* value
) const
1895 wxString
type(GetType());
1896 if (type
== wxT("date"))
1897 *value
= ((wxVariantDataDate
*)GetData())->GetValue();