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
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
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
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
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
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
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 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
632 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
634 wxVariantDataString() { }
635 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
637 inline wxString
GetValue() const { return m_value
; }
638 inline void SetValue(const wxString
& value
) { m_value
= value
; }
640 virtual void Copy(wxVariantData
& data
);
641 virtual bool Eq(wxVariantData
& data
) const;
642 virtual bool Write(ostream
& str
) const;
643 virtual bool Write(wxOutputStream
& str
) const;
644 virtual bool Write(wxString
& str
) const;
645 virtual bool Read(istream
& str
);
646 virtual bool Read(wxInputStream
& str
);
647 virtual bool Read(wxString
& str
);
648 virtual wxString
GetType() const { return "string"; };
654 void wxVariantDataString::Copy(wxVariantData
& data
)
656 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Copy: Can't copy to this type of data" );
658 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
660 otherData
.m_value
= m_value
;
663 bool wxVariantDataString::Eq(wxVariantData
& data
) const
665 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Eq: argument mismatch" );
667 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
669 return (otherData
.m_value
== m_value
);
672 bool wxVariantDataString::Write(ostream
& str
) const
678 bool wxVariantDataString::Write(wxOutputStream
& str
) const
680 str
<< (const char*) m_value
;
684 bool wxVariantDataString::Write(wxString
& str
) const
690 bool wxVariantDataString::Read(istream
& str
)
696 bool wxVariantDataString::Read(wxInputStream
& str
)
702 bool wxVariantDataString::Read(wxString
& str
)
708 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
714 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
716 // Construction & destruction
717 wxVariant::wxVariant()
719 m_data
= (wxVariantData
*) NULL
;
722 wxVariant::wxVariant(double val
)
724 m_data
= new wxVariantDataReal(val
);
727 wxVariant::wxVariant(long val
)
729 m_data
= new wxVariantDataLong(val
);
732 wxVariant::wxVariant(bool val
)
734 m_data
= new wxVariantDataBool(val
);
737 wxVariant::wxVariant(char val
)
739 m_data
= new wxVariantDataChar(val
);
742 wxVariant::wxVariant(const wxString
& val
)
744 m_data
= new wxVariantDataString(val
);
747 wxVariant::wxVariant(const char* val
)
749 m_data
= new wxVariantDataString(wxString(val
));
753 wxVariant::wxVariant(const wxStringList& val)
755 m_data = new wxVariantDataStringList(val);
759 wxVariant::wxVariant(const wxList
& val
) // List of variants
761 m_data
= new wxVariantDataList(val
);
764 wxVariant::wxVariant(const wxVariant
& variant
)
766 if (!variant
.IsNull())
768 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
769 variant
.m_data
->Copy(*m_data
);
772 m_data
= (wxVariantData
*) NULL
;
775 wxVariant::wxVariant(wxVariantData
* data
) // User-defined data
780 wxVariant::~wxVariant()
786 // Make NULL (i.e. delete the data)
787 void wxVariant::MakeNull()
795 void wxVariant::operator= (const wxVariant
& variant
)
797 if (variant
.IsNull())
803 if (IsNull() || (GetType() != variant
.GetType()))
807 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
809 // GetData()->Copy(* variant.GetData());
810 variant
.GetData()->Copy(* GetData());
813 // Assignment using data, e.g.
814 // myVariant = new wxStringVariantData("hello")
815 void wxVariant::operator= (wxVariantData
* variantData
)
818 m_data
= variantData
;
821 bool wxVariant::operator== (const wxVariant
& variant
) const
823 if (IsNull() || variant
.IsNull())
824 return (IsNull() == variant
.IsNull());
826 return (GetData()->Eq(* variant
.GetData()));
829 bool wxVariant::operator!= (const wxVariant
& variant
) const
831 return (!(*this == variant
));
835 // Specific operators
836 bool wxVariant::operator== (double value
) const
839 if (!Convert(&thisValue
))
842 return (value
== thisValue
);
845 bool wxVariant::operator!= (double value
) const
847 return (!((*this) == value
));
850 void wxVariant::operator= (double value
)
852 if (GetType() == "double")
854 ((wxVariantDataReal
*)GetData())->SetValue(value
);
860 m_data
= new wxVariantDataReal(value
);
864 bool wxVariant::operator== (long value
) const
867 if (!Convert(&thisValue
))
870 return (value
== thisValue
);
873 bool wxVariant::operator!= (long value
) const
875 return (!((*this) == value
));
878 void wxVariant::operator= (long value
)
880 if (GetType() == "long")
882 ((wxVariantDataLong
*)GetData())->SetValue(value
);
888 m_data
= new wxVariantDataLong(value
);
892 bool wxVariant::operator== (char value
) const
895 if (!Convert(&thisValue
))
898 return (value
== thisValue
);
901 bool wxVariant::operator!= (char value
) const
903 return (!((*this) == value
));
906 void wxVariant::operator= (char value
)
908 if (GetType() == "char")
910 ((wxVariantDataChar
*)GetData())->SetValue(value
);
916 m_data
= new wxVariantDataChar(value
);
920 bool wxVariant::operator== (bool value
) const
923 if (!Convert(&thisValue
))
926 return (value
== thisValue
);
929 bool wxVariant::operator!= (bool value
) const
931 return (!((*this) == value
));
934 void wxVariant::operator= (bool value
)
936 if (GetType() == "bool")
938 ((wxVariantDataBool
*)GetData())->SetValue(value
);
944 m_data
= new wxVariantDataBool(value
);
948 bool wxVariant::operator== (const wxString
& value
) const
951 if (!Convert(&thisValue
))
954 return (value
== thisValue
);
957 bool wxVariant::operator!= (const wxString
& value
) const
959 return (!((*this) == value
));
962 void wxVariant::operator= (const wxString
& value
)
964 if (GetType() == "string")
966 ((wxVariantDataString
*)GetData())->SetValue(value
);
972 m_data
= new wxVariantDataString(value
);
976 void wxVariant::operator= (const char* value
)
978 if (GetType() == "string")
980 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
986 m_data
= new wxVariantDataString(wxString(value
));
990 bool wxVariant::operator== (const wxStringList
& value
) const
992 wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" );
994 wxVariantDataStringList
other(value
);
995 return (m_data
->Eq(other
));
998 bool wxVariant::operator!= (const wxStringList
& value
) const
1000 return (!((*this) == value
));
1003 void wxVariant::operator= (const wxStringList
& value
)
1005 if (GetType() == "stringlist")
1007 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1013 m_data
= new wxVariantDataStringList(value
);
1017 bool wxVariant::operator== (const wxList
& value
) const
1019 wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" );
1021 wxVariantDataList
other(value
);
1022 return (m_data
->Eq(other
));
1025 bool wxVariant::operator!= (const wxList
& value
) const
1027 return (!((*this) == value
));
1030 void wxVariant::operator= (const wxList
& value
)
1032 if (GetType() == "list")
1034 ((wxVariantDataList
*)GetData())->SetValue(value
);
1040 m_data
= new wxVariantDataList(value
);
1045 // Treat a list variant as an array
1046 wxVariant
wxVariant::operator[] (size_t idx
) const
1048 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" );
1050 if (GetType() == "list")
1052 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1053 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1054 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1056 else if (GetType() == "stringlist")
1058 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1059 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1061 wxVariant
variant( wxString( (char*) (data
->GetValue().Nth(idx
)->Data()) ));
1064 return wxNullVariant
;
1067 wxVariant
& wxVariant::operator[] (size_t idx
)
1069 // We can't return a reference to a variant for a string list, since the string
1070 // is actually stored as a char*, not a variant.
1072 wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" );
1074 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1075 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1077 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1080 // Return the number of elements in a list
1081 int wxVariant::GetCount() const
1083 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" );
1085 if (GetType() == "list")
1087 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1088 return data
->GetValue().Number();
1090 else if (GetType() == "stringlist")
1092 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1093 return data
->GetValue().Number();
1098 wxString
wxVariant::MakeString() const
1103 if (GetData()->Write(str
))
1106 return wxString("");
1111 void wxVariant::SetData(wxVariantData
* data
)
1113 if (m_data
) delete m_data
;
1118 // Returns a string representing the type of the variant,
1119 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1120 wxString
wxVariant::GetType() const
1123 return wxString("null");
1125 return m_data
->GetType();
1129 bool wxVariant::IsType(const wxString
& type
) const
1131 return (GetType() == type
);
1136 double wxVariant::GetReal() const
1139 if (Convert(& value
))
1143 wxFAIL_MSG("Could not convert to a real number");
1148 long wxVariant::GetInteger() const
1151 if (Convert(& value
))
1155 wxFAIL_MSG("Could not convert to an integer");
1160 char wxVariant::GetChar() const
1163 if (Convert(& value
))
1167 wxFAIL_MSG("Could not convert to a char");
1172 bool wxVariant::GetBool() const
1175 if (Convert(& value
))
1179 wxFAIL_MSG("Could not convert to a bool");
1184 wxString
wxVariant::GetString() const
1187 if (Convert(& value
))
1191 wxFAIL_MSG("Could not convert to a string");
1192 return wxString("");
1196 wxList
& wxVariant::GetList() const
1198 wxASSERT( (GetType() == "list") );
1200 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1203 wxStringList
& wxVariant::GetStringList() const
1205 wxASSERT( (GetType() == "stringlist") );
1207 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1211 void wxVariant::Append(const wxVariant
& value
)
1213 wxList
& list
= GetList();
1215 list
.Append(new wxVariant(value
));
1218 // Insert at front of list
1219 void wxVariant::Insert(const wxVariant
& value
)
1221 wxList
& list
= GetList();
1223 list
.Insert(new wxVariant(value
));
1226 // Returns TRUE if the variant is a member of the list
1227 bool wxVariant::Member(const wxVariant
& value
) const
1229 wxList
& list
= GetList();
1231 wxNode
* node
= list
.First();
1234 wxVariant
* other
= (wxVariant
*) node
->Data();
1235 if (value
== *other
)
1237 node
= node
->Next();
1242 // Deletes the nth element of the list
1243 bool wxVariant::Delete(int item
)
1245 wxList
& list
= GetList();
1247 wxASSERT_MSG( (item
< list
.Number()), "Invalid index to Delete" );
1248 wxNode
* node
= list
.Nth(item
);
1249 wxVariant
* variant
= (wxVariant
*) node
->Data();
1256 void wxVariant::ClearList()
1258 if (!IsNull() && (GetType() == "list"))
1260 ((wxVariantDataList
*) m_data
)->Clear();
1264 if (GetType() != "list")
1269 m_data
= new wxVariantDataList
;
1274 bool wxVariant::Convert(long* value
) const
1276 wxString
type(GetType());
1277 if (type
== "double")
1278 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1279 else if (type
== "long")
1280 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1281 else if (type
== "bool")
1282 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1283 else if (type
== "string")
1284 *value
= atol((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1291 bool wxVariant::Convert(bool* value
) const
1293 wxString
type(GetType());
1294 if (type
== "double")
1295 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1296 else if (type
== "long")
1297 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1298 else if (type
== "bool")
1299 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1300 else if (type
== "string")
1302 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1304 if (val
== "true" || val
== "yes")
1306 else if (val
== "false" || val
== "no")
1317 bool wxVariant::Convert(double* value
) const
1319 wxString
type(GetType());
1320 if (type
== "double")
1321 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1322 else if (type
== "long")
1323 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1324 else if (type
== "bool")
1325 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1326 else if (type
== "string")
1327 *value
= (double) atof((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1334 bool wxVariant::Convert(char* value
) const
1336 wxString
type(GetType());
1338 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1339 else if (type
== "long")
1340 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1341 else if (type
== "bool")
1342 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1349 bool wxVariant::Convert(wxString
* value
) const
1351 *value
= MakeString();