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 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;
296 virtual bool Write(ostream
& str
) const;
297 virtual bool Write(wxString
& str
) const;
298 virtual bool Write(wxOutputStream
&str
) const;
299 virtual bool Read(istream
& str
);
300 virtual bool Read(wxInputStream
& str
);
301 virtual bool Read(wxString
& str
);
302 virtual wxString
GetType() const { return "long"; };
308 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
310 void wxVariantDataLong::Copy(wxVariantData
& data
)
312 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Copy: Can't copy to this type of data" );
314 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
316 otherData
.m_value
= m_value
;
319 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
321 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Eq: argument mismatch" );
323 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
325 return (otherData
.m_value
== m_value
);
328 bool wxVariantDataLong::Write(ostream
& str
) const
332 str
<< (const char*) s
;
336 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
342 bool wxVariantDataLong::Write(wxString
& str
) const
344 str
.Printf("%ld", m_value
);
348 bool wxVariantDataLong::Read(istream
& str
)
354 bool wxVariantDataLong::Read(wxInputStream
& str
)
360 bool wxVariantDataLong::Read(wxString
& str
)
362 m_value
= atol((const char*) str
);
370 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
372 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
374 wxVariantDataReal() { m_value
= 0.0; }
375 wxVariantDataReal(double value
) { m_value
= value
; }
377 inline double GetValue() const { return m_value
; }
378 inline void SetValue(double value
) { m_value
= value
; }
380 virtual void Copy(wxVariantData
& data
);
381 virtual bool Eq(wxVariantData
& data
) const;
382 virtual bool Write(ostream
& str
) const;
383 virtual bool Write(wxString
& str
) const;
384 virtual bool Write(wxOutputStream
&str
) const;
385 virtual bool Read(istream
& str
);
386 virtual bool Read(wxInputStream
& str
);
387 virtual bool Read(wxString
& str
);
388 virtual wxString
GetType() const { return "double"; };
394 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
396 void wxVariantDataReal::Copy(wxVariantData
& data
)
398 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Copy: Can't copy to this type of data" );
400 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
402 otherData
.m_value
= m_value
;
405 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
407 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Eq: argument mismatch" );
409 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
411 return (otherData
.m_value
== m_value
);
414 bool wxVariantDataReal::Write(ostream
& str
) const
418 str
<< (const char*) s
;
422 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
428 bool wxVariantDataReal::Write(wxString
& str
) const
430 str
.Printf("%.4f", m_value
);
434 bool wxVariantDataReal::Read(istream
& str
)
440 bool wxVariantDataReal::Read(wxInputStream
& str
)
442 str
>> (float&)m_value
;
446 bool wxVariantDataReal::Read(wxString
& str
)
448 m_value
= atof((const char*) str
);
456 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
458 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
460 wxVariantDataBool() { m_value
= 0; }
461 wxVariantDataBool(bool value
) { m_value
= value
; }
463 inline bool GetValue() const { return m_value
; }
464 inline void SetValue(bool value
) { m_value
= value
; }
466 virtual void Copy(wxVariantData
& data
);
467 virtual bool Eq(wxVariantData
& data
) const;
468 virtual bool Write(ostream
& str
) const;
469 virtual bool Write(wxOutputStream
& str
) const;
470 virtual bool Write(wxString
& str
) const;
471 virtual bool Read(istream
& str
);
472 virtual bool Read(wxInputStream
& str
);
473 virtual bool Read(wxString
& str
);
474 virtual wxString
GetType() const { return "bool"; };
480 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
482 void wxVariantDataBool::Copy(wxVariantData
& data
)
484 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Copy: Can't copy to this type of data" );
486 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
488 otherData
.m_value
= m_value
;
491 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
493 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Eq: argument mismatch" );
495 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
497 return (otherData
.m_value
== m_value
);
500 bool wxVariantDataBool::Write(ostream
& str
) const
504 str
<< (const char*) s
;
508 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
510 str
<< (char)m_value
;
514 bool wxVariantDataBool::Write(wxString
& str
) const
516 str
.Printf("%d", (int) m_value
);
520 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
522 wxFAIL_MSG("Unimplemented");
523 // str >> (long) m_value;
527 bool wxVariantDataBool::Read(wxInputStream
& str
)
529 str
>> (char&)m_value
;
533 bool wxVariantDataBool::Read(wxString
& str
)
535 m_value
= (atol((const char*) str
) != 0);
543 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
545 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
547 wxVariantDataChar() { m_value
= 0; }
548 wxVariantDataChar(char value
) { m_value
= value
; }
550 inline char GetValue() const { return m_value
; }
551 inline void SetValue(char value
) { m_value
= value
; }
553 virtual void Copy(wxVariantData
& data
);
554 virtual bool Eq(wxVariantData
& data
) const;
555 virtual bool Write(ostream
& str
) const;
556 virtual bool Write(wxOutputStream
& str
) const;
557 virtual bool Write(wxString
& str
) const;
558 virtual bool Read(istream
& str
);
559 virtual bool Read(wxInputStream
& str
);
560 virtual bool Read(wxString
& str
);
561 virtual wxString
GetType() const { return "char"; };
567 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
569 void wxVariantDataChar::Copy(wxVariantData
& data
)
571 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Copy: Can't copy to this type of data" );
573 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
575 otherData
.m_value
= m_value
;
578 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
580 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Eq: argument mismatch" );
582 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
584 return (otherData
.m_value
== m_value
);
587 bool wxVariantDataChar::Write(ostream
& str
) const
591 str
<< (const char*) s
;
595 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
601 bool wxVariantDataChar::Write(wxString
& str
) const
603 str
.Printf("%c", m_value
);
607 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
609 wxFAIL_MSG("Unimplemented");
614 bool wxVariantDataChar::Read(wxInputStream
& str
)
620 bool wxVariantDataChar::Read(wxString
& str
)
622 m_value
= str
[(size_t)0];
627 * wxVariantDataString
630 #if defined(__BORLANDC__) && defined(__WIN16__)
631 // Change name because of truncation
632 #define wxVariantDataString wxVariantStringData
635 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
637 #if defined(__BORLANDC__) && defined(__WIN16__)
638 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
640 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
643 wxVariantDataString() { }
644 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
646 inline wxString
GetValue() const { return m_value
; }
647 inline void SetValue(const wxString
& value
) { m_value
= value
; }
649 virtual void Copy(wxVariantData
& data
);
650 virtual bool Eq(wxVariantData
& data
) const;
651 virtual bool Write(ostream
& str
) const;
652 virtual bool Write(wxOutputStream
& str
) const;
653 virtual bool Write(wxString
& str
) const;
654 virtual bool Read(istream
& str
);
655 virtual bool Read(wxInputStream
& str
);
656 virtual bool Read(wxString
& str
);
657 virtual wxString
GetType() const { return "string"; };
663 void wxVariantDataString::Copy(wxVariantData
& data
)
665 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Copy: Can't copy to this type of data" );
667 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
669 otherData
.m_value
= m_value
;
672 bool wxVariantDataString::Eq(wxVariantData
& data
) const
674 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Eq: argument mismatch" );
676 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
678 return (otherData
.m_value
== m_value
);
681 bool wxVariantDataString::Write(ostream
& str
) const
683 str
<< (const char*) m_value
;
687 bool wxVariantDataString::Write(wxOutputStream
& str
) const
689 str
<< (const char*) m_value
;
693 bool wxVariantDataString::Write(wxString
& str
) const
699 bool wxVariantDataString::Read(istream
& str
)
705 bool wxVariantDataString::Read(wxInputStream
& str
)
711 bool wxVariantDataString::Read(wxString
& str
)
717 #if defined(__BORLANDC__) && defined(__WIN16__)
718 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
720 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
729 class wxVariantDataTime
: public wxVariantData
731 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
733 wxVariantDataTime() { }
734 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
736 inline wxTime
GetValue() const { return m_value
; }
737 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
739 virtual void Copy(wxVariantData
& data
);
740 virtual bool Eq(wxVariantData
& data
) const;
741 virtual bool Write(ostream
& str
) const;
742 virtual bool Write(wxString
& str
) const;
743 virtual bool Read(istream
& str
);
744 virtual bool Read(wxString
& str
);
745 virtual wxString
GetType() const { return "time"; };
746 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
752 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
754 void wxVariantDataTime::Copy(wxVariantData
& data
)
756 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Copy: Can't copy to this type of data" );
758 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
760 otherData
.m_value
= m_value
;
763 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
765 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Eq: argument mismatch" );
767 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
769 return (otherData
.m_value
== m_value
);
772 bool wxVariantDataTime::Write(ostream
& str
) const
776 str
<< (const char*) s
;
780 bool wxVariantDataTime::Write(wxString
& str
) const
782 char*s
= m_value
.FormatTime();
787 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
793 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
803 class wxVariantDataDate
: public wxVariantData
805 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
807 wxVariantDataDate() { }
808 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
810 inline wxDate
GetValue() const { return m_value
; }
811 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
813 virtual void Copy(wxVariantData
& data
);
814 virtual bool Eq(wxVariantData
& data
) const;
815 virtual bool Write(ostream
& str
) const;
816 virtual bool Write(wxString
& str
) const;
817 virtual bool Read(istream
& str
);
818 virtual bool Read(wxString
& str
);
819 virtual wxString
GetType() const { return "date"; };
820 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
826 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
828 void wxVariantDataDate::Copy(wxVariantData
& data
)
830 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Copy: Can't copy to this type of data" );
832 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
834 otherData
.m_value
= m_value
;
837 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
839 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Eq: argument mismatch" );
841 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
843 return (otherData
.m_value
== m_value
);
846 bool wxVariantDataDate::Write(ostream
& str
) const
850 str
<< (const char*) s
;
854 bool wxVariantDataDate::Write(wxString
& str
) const
856 str
= m_value
.FormatDate();
860 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
866 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
875 * wxVariantDataVoidPtr
878 class wxVariantDataVoidPtr
: public wxVariantData
880 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
882 wxVariantDataVoidPtr() { }
883 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
885 inline void* GetValue() const { return m_value
; }
886 inline void SetValue(void* value
) { m_value
= value
; }
888 virtual void Copy(wxVariantData
& data
);
889 virtual bool Eq(wxVariantData
& data
) const;
890 virtual bool Write(ostream
& str
) const;
891 virtual bool Write(wxString
& str
) const;
892 virtual bool Read(istream
& str
);
893 virtual bool Read(wxString
& str
);
894 virtual wxString
GetType() const { return "void*"; };
895 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
901 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
903 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
905 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Copy: Can't copy to this type of data" );
907 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
909 otherData
.m_value
= m_value
;
912 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
914 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Eq: argument mismatch" );
916 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
918 return (otherData
.m_value
== m_value
);
921 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
925 str
<< (const char*) s
;
929 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
932 sprintf(buf
, "%ld", (long) m_value
);
937 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
943 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
954 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
956 // Construction & destruction
957 wxVariant::wxVariant()
959 m_data
= (wxVariantData
*) NULL
;
962 wxVariant::wxVariant(double val
, const wxString
& name
)
964 m_data
= new wxVariantDataReal(val
);
968 wxVariant::wxVariant(long val
, const wxString
& name
)
970 m_data
= new wxVariantDataLong(val
);
974 wxVariant::wxVariant(bool val
, const wxString
& name
)
976 m_data
= new wxVariantDataBool(val
);
980 wxVariant::wxVariant(char val
, const wxString
& name
)
982 m_data
= new wxVariantDataChar(val
);
986 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
988 m_data
= new wxVariantDataString(val
);
992 wxVariant::wxVariant(const char* val
, const wxString
& name
)
994 m_data
= new wxVariantDataString(wxString(val
));
998 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1000 m_data
= new wxVariantDataStringList(val
);
1004 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1006 m_data
= new wxVariantDataList(val
);
1011 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1013 m_data
= new wxVariantDataTime(val
);
1017 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1019 m_data
= new wxVariantDataDate(val
);
1024 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1026 m_data
= new wxVariantDataVoidPtr(val
);
1030 wxVariant::wxVariant(const wxVariant
& variant
)
1032 if (!variant
.IsNull())
1034 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1035 variant
.m_data
->Copy(*m_data
);
1038 m_data
= (wxVariantData
*) NULL
;
1039 m_name
= variant
.m_name
;
1042 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1048 wxVariant::~wxVariant()
1054 // Make NULL (i.e. delete the data)
1055 void wxVariant::MakeNull()
1061 // Generic operators
1063 void wxVariant::operator= (const wxVariant
& variant
)
1065 if (variant
.IsNull())
1071 if (IsNull() || (GetType() != variant
.GetType()))
1075 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1077 // GetData()->Copy(* variant.GetData());
1078 variant
.GetData()->Copy(* GetData());
1081 // Assignment using data, e.g.
1082 // myVariant = new wxStringVariantData("hello")
1083 void wxVariant::operator= (wxVariantData
* variantData
)
1086 m_data
= variantData
;
1089 bool wxVariant::operator== (const wxVariant
& variant
) const
1091 if (IsNull() || variant
.IsNull())
1092 return (IsNull() == variant
.IsNull());
1094 return (GetData()->Eq(* variant
.GetData()));
1097 bool wxVariant::operator!= (const wxVariant
& variant
) const
1099 return (!(*this == variant
));
1103 // Specific operators
1104 bool wxVariant::operator== (double value
) const
1107 if (!Convert(&thisValue
))
1110 return (value
== thisValue
);
1113 bool wxVariant::operator!= (double value
) const
1115 return (!((*this) == value
));
1118 void wxVariant::operator= (double value
)
1120 if (GetType() == "double")
1122 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1128 m_data
= new wxVariantDataReal(value
);
1132 bool wxVariant::operator== (long value
) const
1135 if (!Convert(&thisValue
))
1138 return (value
== thisValue
);
1141 bool wxVariant::operator!= (long value
) const
1143 return (!((*this) == value
));
1146 void wxVariant::operator= (long value
)
1148 if (GetType() == "long")
1150 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1156 m_data
= new wxVariantDataLong(value
);
1160 bool wxVariant::operator== (char value
) const
1163 if (!Convert(&thisValue
))
1166 return (value
== thisValue
);
1169 bool wxVariant::operator!= (char value
) const
1171 return (!((*this) == value
));
1174 void wxVariant::operator= (char value
)
1176 if (GetType() == "char")
1178 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1184 m_data
= new wxVariantDataChar(value
);
1188 bool wxVariant::operator== (bool value
) const
1191 if (!Convert(&thisValue
))
1194 return (value
== thisValue
);
1197 bool wxVariant::operator!= (bool value
) const
1199 return (!((*this) == value
));
1202 void wxVariant::operator= (bool value
)
1204 if (GetType() == "bool")
1206 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1212 m_data
= new wxVariantDataBool(value
);
1216 bool wxVariant::operator== (const wxString
& value
) const
1219 if (!Convert(&thisValue
))
1222 return (value
== thisValue
);
1225 bool wxVariant::operator!= (const wxString
& value
) const
1227 return (!((*this) == value
));
1230 void wxVariant::operator= (const wxString
& value
)
1232 if (GetType() == "string")
1234 ((wxVariantDataString
*)GetData())->SetValue(value
);
1240 m_data
= new wxVariantDataString(value
);
1244 void wxVariant::operator= (const char* value
)
1246 if (GetType() == "string")
1248 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1254 m_data
= new wxVariantDataString(wxString(value
));
1258 bool wxVariant::operator== (const wxStringList
& value
) const
1260 wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" );
1262 wxVariantDataStringList
other(value
);
1263 return (m_data
->Eq(other
));
1266 bool wxVariant::operator!= (const wxStringList
& value
) const
1268 return (!((*this) == value
));
1271 void wxVariant::operator= (const wxStringList
& value
)
1273 if (GetType() == "stringlist")
1275 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1281 m_data
= new wxVariantDataStringList(value
);
1285 bool wxVariant::operator== (const wxList
& value
) const
1287 wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" );
1289 wxVariantDataList
other(value
);
1290 return (m_data
->Eq(other
));
1293 bool wxVariant::operator!= (const wxList
& value
) const
1295 return (!((*this) == value
));
1298 void wxVariant::operator= (const wxList
& value
)
1300 if (GetType() == "list")
1302 ((wxVariantDataList
*)GetData())->SetValue(value
);
1308 m_data
= new wxVariantDataList(value
);
1313 bool wxVariant::operator== (const wxTime
& value
) const
1316 if (!Convert(&thisValue
))
1319 return (value
== thisValue
);
1322 bool wxVariant::operator!= (const wxTime
& value
) const
1324 return (!((*this) == value
));
1327 void wxVariant::operator= (const wxTime
& value
)
1329 if (GetType() == "time")
1331 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1337 m_data
= new wxVariantDataTime(value
);
1341 bool wxVariant::operator== (const wxDate
& value
) const
1344 if (!Convert(&thisValue
))
1347 return (value
== thisValue
);
1350 bool wxVariant::operator!= (const wxDate
& value
) const
1352 return (!((*this) == value
));
1355 void wxVariant::operator= (const wxDate
& value
)
1357 if (GetType() == "date")
1359 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1365 m_data
= new wxVariantDataDate(value
);
1370 bool wxVariant::operator== (void* value
) const
1372 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1375 bool wxVariant::operator!= (void* value
) const
1377 return (!((*this) == value
));
1380 void wxVariant::operator= (void* value
)
1382 if (GetType() == "void*")
1384 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1390 m_data
= new wxVariantDataVoidPtr(value
);
1394 // Treat a list variant as an array
1395 wxVariant
wxVariant::operator[] (size_t idx
) const
1397 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" );
1399 if (GetType() == "list")
1401 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1402 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1403 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1405 else if (GetType() == "stringlist")
1407 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1408 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1410 wxVariant
variant( wxString( (char*) (data
->GetValue().Nth(idx
)->Data()) ));
1413 return wxNullVariant
;
1416 wxVariant
& wxVariant::operator[] (size_t idx
)
1418 // We can't return a reference to a variant for a string list, since the string
1419 // is actually stored as a char*, not a variant.
1421 wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" );
1423 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1424 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1426 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1429 // Return the number of elements in a list
1430 int wxVariant::GetCount() const
1432 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" );
1434 if (GetType() == "list")
1436 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1437 return data
->GetValue().Number();
1439 else if (GetType() == "stringlist")
1441 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1442 return data
->GetValue().Number();
1447 wxString
wxVariant::MakeString() const
1452 if (GetData()->Write(str
))
1455 return wxString("");
1460 void wxVariant::SetData(wxVariantData
* data
)
1462 if (m_data
) delete m_data
;
1467 // Returns a string representing the type of the variant,
1468 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1469 wxString
wxVariant::GetType() const
1472 return wxString("null");
1474 return m_data
->GetType();
1478 bool wxVariant::IsType(const wxString
& type
) const
1480 return (GetType() == type
);
1485 double wxVariant::GetReal() const
1488 if (Convert(& value
))
1492 wxFAIL_MSG("Could not convert to a real number");
1497 long wxVariant::GetInteger() const
1500 if (Convert(& value
))
1504 wxFAIL_MSG("Could not convert to an integer");
1509 char wxVariant::GetChar() const
1512 if (Convert(& value
))
1516 wxFAIL_MSG("Could not convert to a char");
1521 bool wxVariant::GetBool() const
1524 if (Convert(& value
))
1528 wxFAIL_MSG("Could not convert to a bool");
1533 wxString
wxVariant::GetString() const
1536 if (Convert(& value
))
1540 wxFAIL_MSG("Could not convert to a string");
1541 return wxString("");
1546 wxTime
wxVariant::GetTime() const
1549 if (Convert(& value
))
1553 wxFAIL_MSG("Could not convert to a time");
1558 wxDate
wxVariant::GetDate() const
1561 if (Convert(& value
))
1565 wxFAIL_MSG("Could not convert to a date");
1571 void* wxVariant::GetVoidPtr() const
1573 wxASSERT( (GetType() == "void*") );
1575 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1578 wxList
& wxVariant::GetList() const
1580 wxASSERT( (GetType() == "list") );
1582 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1585 wxStringList
& wxVariant::GetStringList() const
1587 wxASSERT( (GetType() == "stringlist") );
1589 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1593 void wxVariant::Append(const wxVariant
& value
)
1595 wxList
& list
= GetList();
1597 list
.Append(new wxVariant(value
));
1600 // Insert at front of list
1601 void wxVariant::Insert(const wxVariant
& value
)
1603 wxList
& list
= GetList();
1605 list
.Insert(new wxVariant(value
));
1608 // Returns TRUE if the variant is a member of the list
1609 bool wxVariant::Member(const wxVariant
& value
) const
1611 wxList
& list
= GetList();
1613 wxNode
* node
= list
.First();
1616 wxVariant
* other
= (wxVariant
*) node
->Data();
1617 if (value
== *other
)
1619 node
= node
->Next();
1624 // Deletes the nth element of the list
1625 bool wxVariant::Delete(int item
)
1627 wxList
& list
= GetList();
1629 wxASSERT_MSG( (item
< list
.Number()), "Invalid index to Delete" );
1630 wxNode
* node
= list
.Nth(item
);
1631 wxVariant
* variant
= (wxVariant
*) node
->Data();
1638 void wxVariant::ClearList()
1640 if (!IsNull() && (GetType() == "list"))
1642 ((wxVariantDataList
*) m_data
)->Clear();
1646 if (GetType() != "list")
1651 m_data
= new wxVariantDataList
;
1656 bool wxVariant::Convert(long* value
) const
1658 wxString
type(GetType());
1659 if (type
== "double")
1660 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1661 else if (type
== "long")
1662 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1663 else if (type
== "bool")
1664 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1665 else if (type
== "string")
1666 *value
= atol((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1673 bool wxVariant::Convert(bool* value
) const
1675 wxString
type(GetType());
1676 if (type
== "double")
1677 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1678 else if (type
== "long")
1679 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1680 else if (type
== "bool")
1681 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1682 else if (type
== "string")
1684 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1686 if (val
== "true" || val
== "yes")
1688 else if (val
== "false" || val
== "no")
1699 bool wxVariant::Convert(double* value
) const
1701 wxString
type(GetType());
1702 if (type
== "double")
1703 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1704 else if (type
== "long")
1705 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1706 else if (type
== "bool")
1707 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1708 else if (type
== "string")
1709 *value
= (double) atof((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1716 bool wxVariant::Convert(char* value
) const
1718 wxString
type(GetType());
1720 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1721 else if (type
== "long")
1722 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1723 else if (type
== "bool")
1724 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1731 bool wxVariant::Convert(wxString
* value
) const
1733 *value
= MakeString();
1738 bool wxVariant::Convert(wxTime
* value
) const
1740 wxString
type(GetType());
1742 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1743 else if (type
== "date")
1744 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1751 bool wxVariant::Convert(wxDate
* value
) const
1753 wxString
type(GetType());
1755 *value
= ((wxVariantDataDate
*)GetData())->GetValue();