1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "variant.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
32 #include "wx/stream.h"
33 #include "wx/string.h"
34 #include "wx/variant.h"
36 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
38 wxVariant WXDLLEXPORT wxNullVariant
;
44 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
46 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
48 wxVariantDataList() {}
49 wxVariantDataList(const wxList
& list
);
52 wxList
& GetValue() const { return (wxList
&) m_value
; }
53 void SetValue(const wxList
& value
) ;
55 virtual void Copy(wxVariantData
& data
);
56 virtual bool Eq(wxVariantData
& data
) const;
57 virtual bool Write(ostream
& str
) const;
58 virtual bool Write(wxString
& str
) const;
59 virtual bool Read(istream
& str
);
60 virtual bool Read(wxString
& str
);
61 virtual wxString
GetType() const { return "list"; };
69 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
71 wxVariantDataList::wxVariantDataList(const wxList
& list
)
76 wxVariantDataList::~wxVariantDataList()
81 void wxVariantDataList::SetValue(const wxList
& value
)
84 wxNode
* node
= value
.First();
87 wxVariant
* var
= (wxVariant
*) node
->Data();
88 m_value
.Append(new wxVariant(*var
));
93 void wxVariantDataList::Clear()
95 wxNode
* node
= m_value
.First();
98 wxVariant
* var
= (wxVariant
*) node
->Data();
105 void wxVariantDataList::Copy(wxVariantData
& data
)
107 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Copy: Can't copy to this type of data" );
109 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
112 wxNode
* node
= m_value
.First();
115 wxVariant
* var
= (wxVariant
*) node
->Data();
116 listData
.m_value
.Append(new wxVariant(*var
));
121 bool wxVariantDataList::Eq(wxVariantData
& data
) const
123 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Eq: argument mismatch" );
125 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
126 wxNode
* node1
= m_value
.First();
127 wxNode
* node2
= listData
.GetValue().First();
128 while (node1
&& node2
)
130 wxVariant
* var1
= (wxVariant
*) node1
->Data();
131 wxVariant
* var2
= (wxVariant
*) node2
->Data();
132 if ((*var1
) != (*var2
))
134 node1
= node1
->Next();
135 node2
= node2
->Next();
137 if (node1
|| node2
) return FALSE
;
141 bool wxVariantDataList::Write(ostream
& str
) const
145 str
<< (const char*) s
;
149 bool wxVariantDataList::Write(wxString
& str
) const
152 wxNode
* node
= m_value
.First();
155 wxVariant
* var
= (wxVariant
*) node
->Data();
156 if (node
!= m_value
.First())
159 str
+= var
->MakeString();
166 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
168 wxFAIL_MSG("Unimplemented");
173 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
175 wxFAIL_MSG("Unimplemented");
181 * wxVariantDataStringList
184 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
186 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
188 wxVariantDataStringList() {}
189 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
191 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
192 void SetValue(const wxStringList
& value
);
194 virtual void Copy(wxVariantData
& data
);
195 virtual bool Eq(wxVariantData
& data
) const;
196 virtual bool Write(ostream
& str
) const;
197 virtual bool Write(wxString
& str
) const;
198 virtual bool Read(istream
& str
);
199 virtual bool Read(wxString
& str
);
200 virtual wxString
GetType() const { return "stringlist"; };
203 wxStringList m_value
;
206 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
208 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
213 void wxVariantDataStringList::Copy(wxVariantData
& data
)
215 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Copy: Can't copy to this type of data" );
217 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
219 listData
.m_value
= m_value
;
222 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
224 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Eq: argument mismatch" );
226 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
227 wxNode
* node1
= m_value
.First();
228 wxNode
* node2
= listData
.GetValue().First();
229 while (node1
&& node2
)
231 wxString
str1 ((char*) node1
->Data());
232 wxString
str2 ((char*) node2
->Data());
235 node1
= node1
->Next();
236 node2
= node2
->Next();
238 if (node1
|| node2
) return FALSE
;
242 bool wxVariantDataStringList::Write(ostream
& str
) const
246 str
<< (const char*) s
;
250 bool wxVariantDataStringList::Write(wxString
& str
) const
253 wxNode
* node
= m_value
.First();
256 char* s
= (char*) node
->Data();
257 if (node
!= m_value
.First())
266 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
268 wxFAIL_MSG("Unimplemented");
273 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
275 wxFAIL_MSG("Unimplemented");
284 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
286 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
288 wxVariantDataLong() { m_value
= 0; }
289 wxVariantDataLong(long value
) { m_value
= value
; }
291 inline long GetValue() const { return m_value
; }
292 inline void SetValue(long value
) { m_value
= value
; }
294 virtual void Copy(wxVariantData
& data
);
295 virtual bool Eq(wxVariantData
& data
) const;
297 virtual bool Read(wxString
& str
);
298 virtual bool Write(wxString
& str
) const;
299 virtual bool Read(istream
& str
);
300 virtual bool Write(ostream
& str
) const;
303 virtual bool Read(wxInputStream
& str
);
304 virtual bool Write(wxOutputStream
&str
) const;
305 #endif // wxUSE_STREAMS
307 virtual wxString
GetType() const { return "long"; };
313 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
315 void wxVariantDataLong::Copy(wxVariantData
& data
)
317 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Copy: Can't copy to this type of data" );
319 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
321 otherData
.m_value
= m_value
;
324 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
326 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Eq: argument mismatch" );
328 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
330 return (otherData
.m_value
== m_value
);
333 bool wxVariantDataLong::Write(ostream
& str
) const
337 str
<< (const char*) s
;
341 bool wxVariantDataLong::Write(wxString
& str
) const
343 str
.Printf("%ld", m_value
);
347 bool wxVariantDataLong::Read(istream
& str
)
354 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
360 bool wxVariantDataLong::Read(wxInputStream
& str
)
365 #endif // wxUSE_STREAMS
367 bool wxVariantDataLong::Read(wxString
& str
)
369 m_value
= atol((const char*) str
);
377 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
379 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
381 wxVariantDataReal() { m_value
= 0.0; }
382 wxVariantDataReal(double value
) { m_value
= value
; }
384 inline double GetValue() const { return m_value
; }
385 inline void SetValue(double value
) { m_value
= value
; }
387 virtual void Copy(wxVariantData
& data
);
388 virtual bool Eq(wxVariantData
& data
) const;
389 virtual bool Read(wxString
& str
);
390 virtual bool Write(ostream
& str
) const;
391 virtual bool Write(wxString
& str
) const;
392 virtual bool Read(istream
& str
);
394 virtual bool Read(wxInputStream
& str
);
395 virtual bool Write(wxOutputStream
&str
) const;
396 #endif // wxUSE_STREAMS
397 virtual wxString
GetType() const { return "double"; };
403 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
405 void wxVariantDataReal::Copy(wxVariantData
& data
)
407 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Copy: Can't copy to this type of data" );
409 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
411 otherData
.m_value
= m_value
;
414 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
416 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Eq: argument mismatch" );
418 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
420 return (otherData
.m_value
== m_value
);
423 bool wxVariantDataReal::Write(ostream
& str
) const
427 str
<< (const char*) s
;
431 bool wxVariantDataReal::Write(wxString
& str
) const
433 str
.Printf("%.4f", m_value
);
437 bool wxVariantDataReal::Read(istream
& str
)
444 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
450 bool wxVariantDataReal::Read(wxInputStream
& str
)
452 str
>> (float&)m_value
;
455 #endif // wxUSE_STREAMS
457 bool wxVariantDataReal::Read(wxString
& str
)
459 m_value
= atof((const char*) str
);
467 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
469 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
471 wxVariantDataBool() { m_value
= 0; }
472 wxVariantDataBool(bool value
) { m_value
= value
; }
474 inline bool GetValue() const { return m_value
; }
475 inline void SetValue(bool value
) { m_value
= value
; }
477 virtual void Copy(wxVariantData
& data
);
478 virtual bool Eq(wxVariantData
& data
) const;
479 virtual bool Write(ostream
& str
) const;
480 virtual bool Write(wxString
& str
) const;
481 virtual bool Read(wxString
& str
);
482 virtual bool Read(istream
& str
);
484 virtual bool Read(wxInputStream
& str
);
485 virtual bool Write(wxOutputStream
& str
) const;
486 #endif // wxUSE_STREAMS
487 virtual wxString
GetType() const { return "bool"; };
493 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
495 void wxVariantDataBool::Copy(wxVariantData
& data
)
497 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Copy: Can't copy to this type of data" );
499 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
501 otherData
.m_value
= m_value
;
504 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
506 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Eq: argument mismatch" );
508 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
510 return (otherData
.m_value
== m_value
);
513 bool wxVariantDataBool::Write(ostream
& str
) const
517 str
<< (const char*) s
;
521 bool wxVariantDataBool::Write(wxString
& str
) const
523 str
.Printf("%d", (int) m_value
);
527 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
529 wxFAIL_MSG("Unimplemented");
530 // str >> (long) m_value;
535 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
537 str
<< (char)m_value
;
541 bool wxVariantDataBool::Read(wxInputStream
& str
)
543 str
>> (char&)m_value
;
546 #endif // wxUSE_STREAMS
548 bool wxVariantDataBool::Read(wxString
& str
)
550 m_value
= (atol((const char*) str
) != 0);
558 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
560 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
562 wxVariantDataChar() { m_value
= 0; }
563 wxVariantDataChar(char value
) { m_value
= value
; }
565 inline char GetValue() const { return m_value
; }
566 inline void SetValue(char value
) { m_value
= value
; }
568 virtual void Copy(wxVariantData
& data
);
569 virtual bool Eq(wxVariantData
& data
) const;
570 virtual bool Read(istream
& str
);
571 virtual bool Write(ostream
& str
) const;
572 virtual bool Read(wxString
& str
);
573 virtual bool Write(wxString
& str
) const;
575 virtual bool Read(wxInputStream
& str
);
576 virtual bool Write(wxOutputStream
& str
) const;
577 #endif // wxUSE_STREAMS
578 virtual wxString
GetType() const { return "char"; };
584 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
586 void wxVariantDataChar::Copy(wxVariantData
& data
)
588 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Copy: Can't copy to this type of data" );
590 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
592 otherData
.m_value
= m_value
;
595 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
597 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Eq: argument mismatch" );
599 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
601 return (otherData
.m_value
== m_value
);
604 bool wxVariantDataChar::Write(ostream
& str
) const
608 str
<< (const char*) s
;
612 bool wxVariantDataChar::Write(wxString
& str
) const
614 str
.Printf("%c", m_value
);
618 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
620 wxFAIL_MSG("Unimplemented");
626 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
632 bool wxVariantDataChar::Read(wxInputStream
& str
)
637 #endif // wxUSE_STREAMS
639 bool wxVariantDataChar::Read(wxString
& str
)
641 m_value
= str
[(size_t)0];
646 * wxVariantDataString
649 #if defined(__BORLANDC__) && defined(__WIN16__)
650 // Change name because of truncation
651 #define wxVariantDataString wxVariantStringData
654 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
656 #if defined(__BORLANDC__) && defined(__WIN16__)
657 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
659 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
662 wxVariantDataString() { }
663 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
665 inline wxString
GetValue() const { return m_value
; }
666 inline void SetValue(const wxString
& value
) { m_value
= value
; }
668 virtual void Copy(wxVariantData
& data
);
669 virtual bool Eq(wxVariantData
& data
) const;
670 virtual bool Write(ostream
& str
) const;
671 virtual bool Read(wxString
& str
);
672 virtual bool Write(wxString
& str
) const;
673 virtual bool Read(istream
& str
);
675 virtual bool Read(wxInputStream
& str
);
676 virtual bool Write(wxOutputStream
& str
) const;
677 #endif // wxUSE_STREAMS
678 virtual wxString
GetType() const { return "string"; };
684 void wxVariantDataString::Copy(wxVariantData
& data
)
686 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Copy: Can't copy to this type of data" );
688 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
690 otherData
.m_value
= m_value
;
693 bool wxVariantDataString::Eq(wxVariantData
& data
) const
695 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Eq: argument mismatch" );
697 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
699 return (otherData
.m_value
== m_value
);
702 bool wxVariantDataString::Write(ostream
& str
) const
704 str
<< (const char*) m_value
;
708 bool wxVariantDataString::Write(wxString
& str
) const
714 bool wxVariantDataString::Read(istream
& str
)
721 bool wxVariantDataString::Write(wxOutputStream
& str
) const
723 str
<< (const char*) m_value
;
727 bool wxVariantDataString::Read(wxInputStream
& str
)
732 #endif // wxUSE_STREAMS
734 bool wxVariantDataString::Read(wxString
& str
)
740 #if defined(__BORLANDC__) && defined(__WIN16__)
741 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
743 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
752 class wxVariantDataTime
: public wxVariantData
754 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
756 wxVariantDataTime() { }
757 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
759 inline wxTime
GetValue() const { return m_value
; }
760 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
762 virtual void Copy(wxVariantData
& data
);
763 virtual bool Eq(wxVariantData
& data
) const;
764 virtual bool Write(ostream
& str
) const;
765 virtual bool Write(wxString
& str
) const;
766 virtual bool Read(istream
& str
);
767 virtual bool Read(wxString
& str
);
768 virtual wxString
GetType() const { return "time"; };
769 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
775 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
777 void wxVariantDataTime::Copy(wxVariantData
& data
)
779 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Copy: Can't copy to this type of data" );
781 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
783 otherData
.m_value
= m_value
;
786 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
788 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Eq: argument mismatch" );
790 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
792 return (otherData
.m_value
== m_value
);
795 bool wxVariantDataTime::Write(ostream
& str
) const
799 str
<< (const char*) s
;
803 bool wxVariantDataTime::Write(wxString
& str
) const
805 char*s
= m_value
.FormatTime();
810 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
816 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
826 class wxVariantDataDate
: public wxVariantData
828 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
830 wxVariantDataDate() { }
831 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
833 inline wxDate
GetValue() const { return m_value
; }
834 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
836 virtual void Copy(wxVariantData
& data
);
837 virtual bool Eq(wxVariantData
& data
) const;
838 virtual bool Write(ostream
& str
) const;
839 virtual bool Write(wxString
& str
) const;
840 virtual bool Read(istream
& str
);
841 virtual bool Read(wxString
& str
);
842 virtual wxString
GetType() const { return "date"; };
843 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
849 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
851 void wxVariantDataDate::Copy(wxVariantData
& data
)
853 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Copy: Can't copy to this type of data" );
855 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
857 otherData
.m_value
= m_value
;
860 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
862 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Eq: argument mismatch" );
864 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
866 return (otherData
.m_value
== m_value
);
869 bool wxVariantDataDate::Write(ostream
& str
) const
873 str
<< (const char*) s
;
877 bool wxVariantDataDate::Write(wxString
& str
) const
879 str
= m_value
.FormatDate();
883 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
889 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
898 * wxVariantDataVoidPtr
901 class wxVariantDataVoidPtr
: public wxVariantData
903 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
905 wxVariantDataVoidPtr() { }
906 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
908 inline void* GetValue() const { return m_value
; }
909 inline void SetValue(void* value
) { m_value
= value
; }
911 virtual void Copy(wxVariantData
& data
);
912 virtual bool Eq(wxVariantData
& data
) const;
913 virtual bool Write(ostream
& str
) const;
914 virtual bool Write(wxString
& str
) const;
915 virtual bool Read(istream
& str
);
916 virtual bool Read(wxString
& str
);
917 virtual wxString
GetType() const { return "void*"; };
918 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
924 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
926 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
928 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Copy: Can't copy to this type of data" );
930 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
932 otherData
.m_value
= m_value
;
935 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
937 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Eq: argument mismatch" );
939 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
941 return (otherData
.m_value
== m_value
);
944 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
948 str
<< (const char*) s
;
952 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
955 sprintf(buf
, "%ld", (long) m_value
);
960 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
966 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
977 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
979 // Construction & destruction
980 wxVariant::wxVariant()
982 m_data
= (wxVariantData
*) NULL
;
985 wxVariant::wxVariant(double val
, const wxString
& name
)
987 m_data
= new wxVariantDataReal(val
);
991 wxVariant::wxVariant(long val
, const wxString
& name
)
993 m_data
= new wxVariantDataLong(val
);
997 wxVariant::wxVariant(bool val
, const wxString
& name
)
999 m_data
= new wxVariantDataBool(val
);
1003 wxVariant::wxVariant(char val
, const wxString
& name
)
1005 m_data
= new wxVariantDataChar(val
);
1009 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1011 m_data
= new wxVariantDataString(val
);
1015 wxVariant::wxVariant(const char* val
, const wxString
& name
)
1017 m_data
= new wxVariantDataString(wxString(val
));
1021 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1023 m_data
= new wxVariantDataStringList(val
);
1027 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1029 m_data
= new wxVariantDataList(val
);
1034 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1036 m_data
= new wxVariantDataTime(val
);
1040 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1042 m_data
= new wxVariantDataDate(val
);
1047 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1049 m_data
= new wxVariantDataVoidPtr(val
);
1053 wxVariant::wxVariant(const wxVariant
& variant
)
1055 if (!variant
.IsNull())
1057 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1058 variant
.m_data
->Copy(*m_data
);
1061 m_data
= (wxVariantData
*) NULL
;
1062 m_name
= variant
.m_name
;
1065 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1071 wxVariant::~wxVariant()
1077 // Make NULL (i.e. delete the data)
1078 void wxVariant::MakeNull()
1084 // Generic operators
1086 void wxVariant::operator= (const wxVariant
& variant
)
1088 if (variant
.IsNull())
1094 if (IsNull() || (GetType() != variant
.GetType()))
1098 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1100 // GetData()->Copy(* variant.GetData());
1101 variant
.GetData()->Copy(* GetData());
1104 // Assignment using data, e.g.
1105 // myVariant = new wxStringVariantData("hello")
1106 void wxVariant::operator= (wxVariantData
* variantData
)
1109 m_data
= variantData
;
1112 bool wxVariant::operator== (const wxVariant
& variant
) const
1114 if (IsNull() || variant
.IsNull())
1115 return (IsNull() == variant
.IsNull());
1117 return (GetData()->Eq(* variant
.GetData()));
1120 bool wxVariant::operator!= (const wxVariant
& variant
) const
1122 return (!(*this == variant
));
1126 // Specific operators
1127 bool wxVariant::operator== (double value
) const
1130 if (!Convert(&thisValue
))
1133 return (value
== thisValue
);
1136 bool wxVariant::operator!= (double value
) const
1138 return (!((*this) == value
));
1141 void wxVariant::operator= (double value
)
1143 if (GetType() == "double")
1145 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1151 m_data
= new wxVariantDataReal(value
);
1155 bool wxVariant::operator== (long value
) const
1158 if (!Convert(&thisValue
))
1161 return (value
== thisValue
);
1164 bool wxVariant::operator!= (long value
) const
1166 return (!((*this) == value
));
1169 void wxVariant::operator= (long value
)
1171 if (GetType() == "long")
1173 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1179 m_data
= new wxVariantDataLong(value
);
1183 bool wxVariant::operator== (char value
) const
1186 if (!Convert(&thisValue
))
1189 return (value
== thisValue
);
1192 bool wxVariant::operator!= (char value
) const
1194 return (!((*this) == value
));
1197 void wxVariant::operator= (char value
)
1199 if (GetType() == "char")
1201 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1207 m_data
= new wxVariantDataChar(value
);
1211 bool wxVariant::operator== (bool value
) const
1214 if (!Convert(&thisValue
))
1217 return (value
== thisValue
);
1220 bool wxVariant::operator!= (bool value
) const
1222 return (!((*this) == value
));
1225 void wxVariant::operator= (bool value
)
1227 if (GetType() == "bool")
1229 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1235 m_data
= new wxVariantDataBool(value
);
1239 bool wxVariant::operator== (const wxString
& value
) const
1242 if (!Convert(&thisValue
))
1245 return (value
== thisValue
);
1248 bool wxVariant::operator!= (const wxString
& value
) const
1250 return (!((*this) == value
));
1253 void wxVariant::operator= (const wxString
& value
)
1255 if (GetType() == "string")
1257 ((wxVariantDataString
*)GetData())->SetValue(value
);
1263 m_data
= new wxVariantDataString(value
);
1267 void wxVariant::operator= (const char* value
)
1269 if (GetType() == "string")
1271 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1277 m_data
= new wxVariantDataString(wxString(value
));
1281 bool wxVariant::operator== (const wxStringList
& value
) const
1283 wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" );
1285 wxVariantDataStringList
other(value
);
1286 return (m_data
->Eq(other
));
1289 bool wxVariant::operator!= (const wxStringList
& value
) const
1291 return (!((*this) == value
));
1294 void wxVariant::operator= (const wxStringList
& value
)
1296 if (GetType() == "stringlist")
1298 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1304 m_data
= new wxVariantDataStringList(value
);
1308 bool wxVariant::operator== (const wxList
& value
) const
1310 wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" );
1312 wxVariantDataList
other(value
);
1313 return (m_data
->Eq(other
));
1316 bool wxVariant::operator!= (const wxList
& value
) const
1318 return (!((*this) == value
));
1321 void wxVariant::operator= (const wxList
& value
)
1323 if (GetType() == "list")
1325 ((wxVariantDataList
*)GetData())->SetValue(value
);
1331 m_data
= new wxVariantDataList(value
);
1336 bool wxVariant::operator== (const wxTime
& value
) const
1339 if (!Convert(&thisValue
))
1342 return (value
== thisValue
);
1345 bool wxVariant::operator!= (const wxTime
& value
) const
1347 return (!((*this) == value
));
1350 void wxVariant::operator= (const wxTime
& value
)
1352 if (GetType() == "time")
1354 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1360 m_data
= new wxVariantDataTime(value
);
1364 bool wxVariant::operator== (const wxDate
& value
) const
1367 if (!Convert(&thisValue
))
1370 return (value
== thisValue
);
1373 bool wxVariant::operator!= (const wxDate
& value
) const
1375 return (!((*this) == value
));
1378 void wxVariant::operator= (const wxDate
& value
)
1380 if (GetType() == "date")
1382 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1388 m_data
= new wxVariantDataDate(value
);
1393 bool wxVariant::operator== (void* value
) const
1395 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1398 bool wxVariant::operator!= (void* value
) const
1400 return (!((*this) == value
));
1403 void wxVariant::operator= (void* value
)
1405 if (GetType() == "void*")
1407 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1413 m_data
= new wxVariantDataVoidPtr(value
);
1417 // Treat a list variant as an array
1418 wxVariant
wxVariant::operator[] (size_t idx
) const
1420 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" );
1422 if (GetType() == "list")
1424 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1425 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1426 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1428 else if (GetType() == "stringlist")
1430 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1431 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1433 wxVariant
variant( wxString( (char*) (data
->GetValue().Nth(idx
)->Data()) ));
1436 return wxNullVariant
;
1439 wxVariant
& wxVariant::operator[] (size_t idx
)
1441 // We can't return a reference to a variant for a string list, since the string
1442 // is actually stored as a char*, not a variant.
1444 wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" );
1446 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1447 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1449 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1452 // Return the number of elements in a list
1453 int wxVariant::GetCount() const
1455 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" );
1457 if (GetType() == "list")
1459 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1460 return data
->GetValue().Number();
1462 else if (GetType() == "stringlist")
1464 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1465 return data
->GetValue().Number();
1470 wxString
wxVariant::MakeString() const
1475 if (GetData()->Write(str
))
1478 return wxString("");
1483 void wxVariant::SetData(wxVariantData
* data
)
1485 if (m_data
) delete m_data
;
1490 // Returns a string representing the type of the variant,
1491 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1492 wxString
wxVariant::GetType() const
1495 return wxString("null");
1497 return m_data
->GetType();
1501 bool wxVariant::IsType(const wxString
& type
) const
1503 return (GetType() == type
);
1508 double wxVariant::GetReal() const
1511 if (Convert(& value
))
1515 wxFAIL_MSG("Could not convert to a real number");
1520 long wxVariant::GetInteger() const
1523 if (Convert(& value
))
1527 wxFAIL_MSG("Could not convert to an integer");
1532 char wxVariant::GetChar() const
1535 if (Convert(& value
))
1539 wxFAIL_MSG("Could not convert to a char");
1544 bool wxVariant::GetBool() const
1547 if (Convert(& value
))
1551 wxFAIL_MSG("Could not convert to a bool");
1556 wxString
wxVariant::GetString() const
1559 if (Convert(& value
))
1563 wxFAIL_MSG("Could not convert to a string");
1564 return wxString("");
1569 wxTime
wxVariant::GetTime() const
1572 if (Convert(& value
))
1576 wxFAIL_MSG("Could not convert to a time");
1581 wxDate
wxVariant::GetDate() const
1584 if (Convert(& value
))
1588 wxFAIL_MSG("Could not convert to a date");
1594 void* wxVariant::GetVoidPtr() const
1596 wxASSERT( (GetType() == "void*") );
1598 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1601 wxList
& wxVariant::GetList() const
1603 wxASSERT( (GetType() == "list") );
1605 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1608 wxStringList
& wxVariant::GetStringList() const
1610 wxASSERT( (GetType() == "stringlist") );
1612 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1616 void wxVariant::Append(const wxVariant
& value
)
1618 wxList
& list
= GetList();
1620 list
.Append(new wxVariant(value
));
1623 // Insert at front of list
1624 void wxVariant::Insert(const wxVariant
& value
)
1626 wxList
& list
= GetList();
1628 list
.Insert(new wxVariant(value
));
1631 // Returns TRUE if the variant is a member of the list
1632 bool wxVariant::Member(const wxVariant
& value
) const
1634 wxList
& list
= GetList();
1636 wxNode
* node
= list
.First();
1639 wxVariant
* other
= (wxVariant
*) node
->Data();
1640 if (value
== *other
)
1642 node
= node
->Next();
1647 // Deletes the nth element of the list
1648 bool wxVariant::Delete(int item
)
1650 wxList
& list
= GetList();
1652 wxASSERT_MSG( (item
< list
.Number()), "Invalid index to Delete" );
1653 wxNode
* node
= list
.Nth(item
);
1654 wxVariant
* variant
= (wxVariant
*) node
->Data();
1661 void wxVariant::ClearList()
1663 if (!IsNull() && (GetType() == "list"))
1665 ((wxVariantDataList
*) m_data
)->Clear();
1669 if (GetType() != "list")
1674 m_data
= new wxVariantDataList
;
1679 bool wxVariant::Convert(long* value
) const
1681 wxString
type(GetType());
1682 if (type
== "double")
1683 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1684 else if (type
== "long")
1685 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1686 else if (type
== "bool")
1687 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1688 else if (type
== "string")
1689 *value
= atol((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1696 bool wxVariant::Convert(bool* value
) const
1698 wxString
type(GetType());
1699 if (type
== "double")
1700 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1701 else if (type
== "long")
1702 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1703 else if (type
== "bool")
1704 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1705 else if (type
== "string")
1707 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1709 if (val
== "true" || val
== "yes")
1711 else if (val
== "false" || val
== "no")
1722 bool wxVariant::Convert(double* value
) const
1724 wxString
type(GetType());
1725 if (type
== "double")
1726 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1727 else if (type
== "long")
1728 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1729 else if (type
== "bool")
1730 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1731 else if (type
== "string")
1732 *value
= (double) atof((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1739 bool wxVariant::Convert(char* value
) const
1741 wxString
type(GetType());
1743 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1744 else if (type
== "long")
1745 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1746 else if (type
== "bool")
1747 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1754 bool wxVariant::Convert(wxString
* value
) const
1756 *value
= MakeString();
1761 bool wxVariant::Convert(wxTime
* value
) const
1763 wxString
type(GetType());
1765 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1766 else if (type
== "date")
1767 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1774 bool wxVariant::Convert(wxDate
* value
) const
1776 wxString
type(GetType());
1778 *value
= ((wxVariantDataDate
*)GetData())->GetValue();