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"
25 #include "wx/string.h"
26 #include "wx/variant.h"
28 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
30 wxVariant wxNullVariant
;
36 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
38 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
40 wxVariantDataList() {}
41 wxVariantDataList(const wxList
& list
);
44 wxList
& GetValue() const { return (wxList
&) m_value
; }
45 void SetValue(const wxList
& value
) ;
47 virtual void Copy(wxVariantData
& data
);
48 virtual bool Eq(wxVariantData
& data
) const;
49 virtual bool Write(ostream
& str
) const;
50 virtual bool Write(wxString
& str
) const;
51 virtual bool Read(istream
& str
);
52 virtual bool Read(wxString
& str
);
53 virtual wxString
GetType() const { return "list"; };
61 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
63 wxVariantDataList::wxVariantDataList(const wxList
& list
)
68 wxVariantDataList::~wxVariantDataList()
73 void wxVariantDataList::SetValue(const wxList
& value
)
76 wxNode
* node
= value
.First();
79 wxVariant
* var
= (wxVariant
*) node
->Data();
80 m_value
.Append(new wxVariant(*var
));
85 void wxVariantDataList::Clear()
87 wxNode
* node
= m_value
.First();
90 wxVariant
* var
= (wxVariant
*) node
->Data();
97 void wxVariantDataList::Copy(wxVariantData
& data
)
99 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Copy: Can't copy to this type of data" );
101 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
104 wxNode
* node
= m_value
.First();
107 wxVariant
* var
= (wxVariant
*) node
->Data();
108 listData
.m_value
.Append(new wxVariant(*var
));
113 bool wxVariantDataList::Eq(wxVariantData
& data
) const
115 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Eq: argument mismatch" );
117 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
118 wxNode
* node1
= m_value
.First();
119 wxNode
* node2
= listData
.GetValue().First();
120 while (node1
&& node2
)
122 wxVariant
* var1
= (wxVariant
*) node1
->Data();
123 wxVariant
* var2
= (wxVariant
*) node2
->Data();
124 if ((*var1
) != (*var2
))
126 node1
= node1
->Next();
127 node2
= node2
->Next();
129 if (node1
|| node2
) return FALSE
;
133 bool wxVariantDataList::Write(ostream
& str
) const
141 bool wxVariantDataList::Write(wxString
& str
) const
144 wxNode
* node
= m_value
.First();
147 wxVariant
* var
= (wxVariant
*) node
->Data();
148 if (node
!= m_value
.First())
151 str
+= var
->MakeString();
158 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
160 wxFAIL_MSG("Unimplemented");
165 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
167 wxFAIL_MSG("Unimplemented");
173 * wxVariantDataStringList
176 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
178 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
180 wxVariantDataStringList() {}
181 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
183 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
184 void SetValue(const wxStringList
& value
);
186 virtual void Copy(wxVariantData
& data
);
187 virtual bool Eq(wxVariantData
& data
) const;
188 virtual bool Write(ostream
& str
) const;
189 virtual bool Write(wxString
& str
) const;
190 virtual bool Read(istream
& str
);
191 virtual bool Read(wxString
& str
);
192 virtual wxString
GetType() const { return "stringlist"; };
195 wxStringList m_value
;
198 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
200 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
205 void wxVariantDataStringList::Copy(wxVariantData
& data
)
207 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Copy: Can't copy to this type of data" );
209 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
211 listData
.m_value
= m_value
;
214 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
216 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Eq: argument mismatch" );
218 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
219 wxNode
* node1
= m_value
.First();
220 wxNode
* node2
= listData
.GetValue().First();
221 while (node1
&& node2
)
223 wxString
str1 ((char*) node1
->Data());
224 wxString
str2 ((char*) node2
->Data());
227 node1
= node1
->Next();
228 node2
= node2
->Next();
230 if (node1
|| node2
) return FALSE
;
234 bool wxVariantDataStringList::Write(ostream
& str
) const
242 bool wxVariantDataStringList::Write(wxString
& str
) const
245 wxNode
* node
= m_value
.First();
248 char* s
= (char*) node
->Data();
249 if (node
!= m_value
.First())
258 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
260 wxFAIL_MSG("Unimplemented");
265 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
267 wxFAIL_MSG("Unimplemented");
276 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
278 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
280 wxVariantDataLong() { m_value
= 0; }
281 wxVariantDataLong(long value
) { m_value
= value
; }
283 inline long GetValue() const { return m_value
; }
284 inline void SetValue(long value
) { m_value
= value
; }
286 virtual void Copy(wxVariantData
& data
);
287 virtual bool Eq(wxVariantData
& data
) const;
288 virtual bool Write(ostream
& str
) const;
289 virtual bool Write(wxString
& str
) const;
290 virtual bool Read(istream
& str
);
291 virtual bool Read(wxString
& str
);
292 virtual wxString
GetType() const { return "long"; };
298 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
300 void wxVariantDataLong::Copy(wxVariantData
& data
)
302 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Copy: Can't copy to this type of data" );
304 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
306 otherData
.m_value
= m_value
;
309 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
311 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Eq: argument mismatch" );
313 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
315 return (otherData
.m_value
== m_value
);
318 bool wxVariantDataLong::Write(ostream
& str
) const
326 bool wxVariantDataLong::Write(wxString
& str
) const
328 str
.Printf("%ld", m_value
);
332 bool wxVariantDataLong::Read(istream
& str
)
338 bool wxVariantDataLong::Read(wxString
& str
)
340 m_value
= atol((const char*) str
);
348 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
350 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
352 wxVariantDataReal() { m_value
= 0.0; }
353 wxVariantDataReal(double value
) { m_value
= value
; }
355 inline double GetValue() const { return m_value
; }
356 inline void SetValue(double value
) { m_value
= value
; }
358 virtual void Copy(wxVariantData
& data
);
359 virtual bool Eq(wxVariantData
& data
) const;
360 virtual bool Write(ostream
& str
) const;
361 virtual bool Write(wxString
& str
) const;
362 virtual bool Read(istream
& str
);
363 virtual bool Read(wxString
& str
);
364 virtual wxString
GetType() const { return "double"; };
370 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
372 void wxVariantDataReal::Copy(wxVariantData
& data
)
374 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Copy: Can't copy to this type of data" );
376 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
378 otherData
.m_value
= m_value
;
381 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
383 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Eq: argument mismatch" );
385 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
387 return (otherData
.m_value
== m_value
);
390 bool wxVariantDataReal::Write(ostream
& str
) const
398 bool wxVariantDataReal::Write(wxString
& str
) const
400 str
.Printf("%.4f", m_value
);
404 bool wxVariantDataReal::Read(istream
& str
)
410 bool wxVariantDataReal::Read(wxString
& str
)
412 m_value
= atof((const char*) str
);
420 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
422 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
424 wxVariantDataBool() { m_value
= 0; }
425 wxVariantDataBool(bool value
) { m_value
= value
; }
427 inline bool GetValue() const { return m_value
; }
428 inline void SetValue(bool value
) { m_value
= value
; }
430 virtual void Copy(wxVariantData
& data
);
431 virtual bool Eq(wxVariantData
& data
) const;
432 virtual bool Write(ostream
& str
) const;
433 virtual bool Write(wxString
& str
) const;
434 virtual bool Read(istream
& str
);
435 virtual bool Read(wxString
& str
);
436 virtual wxString
GetType() const { return "bool"; };
442 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
444 void wxVariantDataBool::Copy(wxVariantData
& data
)
446 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Copy: Can't copy to this type of data" );
448 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
450 otherData
.m_value
= m_value
;
453 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
455 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Eq: argument mismatch" );
457 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
459 return (otherData
.m_value
== m_value
);
462 bool wxVariantDataBool::Write(ostream
& str
) const
470 bool wxVariantDataBool::Write(wxString
& str
) const
472 str
.Printf("%d", (int) m_value
);
476 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
478 wxFAIL_MSG("Unimplemented");
479 // str >> (long) m_value;
483 bool wxVariantDataBool::Read(wxString
& str
)
485 m_value
= (atol((const char*) str
) != 0);
493 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
495 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
497 wxVariantDataChar() { m_value
= 0; }
498 wxVariantDataChar(char value
) { m_value
= value
; }
500 inline char GetValue() const { return m_value
; }
501 inline void SetValue(char value
) { m_value
= value
; }
503 virtual void Copy(wxVariantData
& data
);
504 virtual bool Eq(wxVariantData
& data
) const;
505 virtual bool Write(ostream
& str
) const;
506 virtual bool Write(wxString
& str
) const;
507 virtual bool Read(istream
& str
);
508 virtual bool Read(wxString
& str
);
509 virtual wxString
GetType() const { return "char"; };
515 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
517 void wxVariantDataChar::Copy(wxVariantData
& data
)
519 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Copy: Can't copy to this type of data" );
521 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
523 otherData
.m_value
= m_value
;
526 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
528 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Eq: argument mismatch" );
530 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
532 return (otherData
.m_value
== m_value
);
535 bool wxVariantDataChar::Write(ostream
& str
) const
543 bool wxVariantDataChar::Write(wxString
& str
) const
545 str
.Printf("%c", m_value
);
549 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
551 wxFAIL_MSG("Unimplemented");
556 bool wxVariantDataChar::Read(wxString
& str
)
558 m_value
= str
[(size_t)0];
563 * wxVariantDataString
566 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
568 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
570 wxVariantDataString() { }
571 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
573 inline wxString
GetValue() const { return m_value
; }
574 inline void SetValue(const wxString
& value
) { m_value
= value
; }
576 virtual void Copy(wxVariantData
& data
);
577 virtual bool Eq(wxVariantData
& data
) const;
578 virtual bool Write(ostream
& str
) const;
579 virtual bool Write(wxString
& str
) const;
580 virtual bool Read(istream
& str
);
581 virtual bool Read(wxString
& str
);
582 virtual wxString
GetType() const { return "string"; };
588 void wxVariantDataString::Copy(wxVariantData
& data
)
590 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Copy: Can't copy to this type of data" );
592 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
594 otherData
.m_value
= m_value
;
597 bool wxVariantDataString::Eq(wxVariantData
& data
) const
599 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Eq: argument mismatch" );
601 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
603 return (otherData
.m_value
== m_value
);
606 bool wxVariantDataString::Write(ostream
& str
) const
612 bool wxVariantDataString::Write(wxString
& str
) const
618 bool wxVariantDataString::Read(istream
& str
)
624 bool wxVariantDataString::Read(wxString
& str
)
630 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
636 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
638 // Construction & destruction
639 wxVariant::wxVariant()
641 m_data
= (wxVariantData
*) NULL
;
644 wxVariant::wxVariant(double val
)
646 m_data
= new wxVariantDataReal(val
);
649 wxVariant::wxVariant(long val
)
651 m_data
= new wxVariantDataLong(val
);
654 wxVariant::wxVariant(bool val
)
656 m_data
= new wxVariantDataBool(val
);
659 wxVariant::wxVariant(char val
)
661 m_data
= new wxVariantDataChar(val
);
664 wxVariant::wxVariant(const wxString
& val
)
666 m_data
= new wxVariantDataString(val
);
669 wxVariant::wxVariant(const char* val
)
671 m_data
= new wxVariantDataString(wxString(val
));
675 wxVariant::wxVariant(const wxStringList& val)
677 m_data = new wxVariantDataStringList(val);
681 wxVariant::wxVariant(const wxList
& val
) // List of variants
683 m_data
= new wxVariantDataList(val
);
686 wxVariant::wxVariant(const wxVariant
& variant
)
688 if (!variant
.IsNull())
690 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
691 variant
.m_data
->Copy(*m_data
);
695 wxVariant::wxVariant(wxVariantData
* data
) // User-defined data
700 wxVariant::~wxVariant()
706 // Make NULL (i.e. delete the data)
707 void wxVariant::MakeNull()
715 void wxVariant::operator= (const wxVariant
& variant
)
717 if (variant
.IsNull())
723 if (IsNull() || (GetType() != variant
.GetType()))
727 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
729 GetData()->Copy(* variant
.GetData());
732 // Assignment using data, e.g.
733 // myVariant = new wxStringVariantData("hello")
734 void wxVariant::operator= (wxVariantData
* variantData
)
737 m_data
= variantData
;
740 bool wxVariant::operator== (const wxVariant
& variant
) const
742 if (IsNull() || variant
.IsNull())
743 return (IsNull() == variant
.IsNull());
745 return (GetData()->Eq(* variant
.GetData()));
748 bool wxVariant::operator!= (const wxVariant
& variant
) const
750 return (!(*this == variant
));
754 // Specific operators
755 bool wxVariant::operator== (double value
) const
758 if (!Convert(&thisValue
))
761 return (value
== thisValue
);
764 bool wxVariant::operator!= (double value
) const
766 return (!((*this) == value
));
769 void wxVariant::operator= (double value
)
771 if (GetType() == "double")
773 ((wxVariantDataReal
*)GetData())->SetValue(value
);
779 m_data
= new wxVariantDataReal(value
);
783 bool wxVariant::operator== (long value
) const
786 if (!Convert(&thisValue
))
789 return (value
== thisValue
);
792 bool wxVariant::operator!= (long value
) const
794 return (!((*this) == value
));
797 void wxVariant::operator= (long value
)
799 if (GetType() == "long")
801 ((wxVariantDataLong
*)GetData())->SetValue(value
);
807 m_data
= new wxVariantDataLong(value
);
811 bool wxVariant::operator== (char value
) const
814 if (!Convert(&thisValue
))
817 return (value
== thisValue
);
820 bool wxVariant::operator!= (char value
) const
822 return (!((*this) == value
));
825 void wxVariant::operator= (char value
)
827 if (GetType() == "char")
829 ((wxVariantDataChar
*)GetData())->SetValue(value
);
835 m_data
= new wxVariantDataChar(value
);
839 bool wxVariant::operator== (bool value
) const
842 if (!Convert(&thisValue
))
845 return (value
== thisValue
);
848 bool wxVariant::operator!= (bool value
) const
850 return (!((*this) == value
));
853 void wxVariant::operator= (bool value
)
855 if (GetType() == "bool")
857 ((wxVariantDataBool
*)GetData())->SetValue(value
);
863 m_data
= new wxVariantDataBool(value
);
867 bool wxVariant::operator== (const wxString
& value
) const
870 if (!Convert(&thisValue
))
873 return (value
== thisValue
);
876 bool wxVariant::operator!= (const wxString
& value
) const
878 return (!((*this) == value
));
881 void wxVariant::operator= (const wxString
& value
)
883 if (GetType() == "string")
885 ((wxVariantDataString
*)GetData())->SetValue(value
);
891 m_data
= new wxVariantDataString(value
);
895 void wxVariant::operator= (const char* value
)
897 if (GetType() == "string")
899 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
905 m_data
= new wxVariantDataString(wxString(value
));
909 bool wxVariant::operator== (const wxStringList
& value
) const
911 wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" );
913 wxVariantDataStringList
other(value
);
914 return (m_data
->Eq(other
));
917 bool wxVariant::operator!= (const wxStringList
& value
) const
919 return (!((*this) == value
));
922 void wxVariant::operator= (const wxStringList
& value
)
924 if (GetType() == "stringlist")
926 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
932 m_data
= new wxVariantDataStringList(value
);
936 bool wxVariant::operator== (const wxList
& value
) const
938 wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" );
940 wxVariantDataList
other(value
);
941 return (m_data
->Eq(other
));
944 bool wxVariant::operator!= (const wxList
& value
) const
946 return (!((*this) == value
));
949 void wxVariant::operator= (const wxList
& value
)
951 if (GetType() == "list")
953 ((wxVariantDataList
*)GetData())->SetValue(value
);
959 m_data
= new wxVariantDataList(value
);
964 // Treat a list variant as an array
965 wxVariant
wxVariant::operator[] (size_t idx
) const
967 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" );
969 if (GetType() == "list")
971 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
972 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
973 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
975 else if (GetType() == "stringlist")
977 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
978 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
980 wxVariant
variant( wxString( (char*) (data
->GetValue().Nth(idx
)->Data()) ));
983 return wxNullVariant
;
986 wxVariant
& wxVariant::operator[] (size_t idx
)
988 // We can't return a reference to a variant for a string list, since the string
989 // is actually stored as a char*, not a variant.
991 wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" );
993 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
994 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
996 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
999 // Return the number of elements in a list
1000 int wxVariant::GetCount() const
1002 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" );
1004 if (GetType() == "list")
1006 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1007 return data
->GetValue().Number();
1009 else if (GetType() == "stringlist")
1011 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1012 return data
->GetValue().Number();
1017 wxString
wxVariant::MakeString() const
1022 if (GetData()->Write(str
))
1025 return wxString("");
1030 void wxVariant::SetData(wxVariantData
* data
)
1032 if (m_data
) delete m_data
;
1037 // Returns a string representing the type of the variant,
1038 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1039 wxString
wxVariant::GetType() const
1042 return wxString("null");
1044 return m_data
->GetType();
1048 bool wxVariant::IsType(const wxString
& type
) const
1050 return (GetType() == type
);
1055 double wxVariant::GetReal() const
1058 if (Convert(& value
))
1062 wxFAIL_MSG("Could not convert to a real number");
1067 long wxVariant::GetInteger() const
1070 if (Convert(& value
))
1074 wxFAIL_MSG("Could not convert to an integer");
1079 char wxVariant::GetChar() const
1082 if (Convert(& value
))
1086 wxFAIL_MSG("Could not convert to a char");
1091 bool wxVariant::GetBool() const
1094 if (Convert(& value
))
1098 wxFAIL_MSG("Could not convert to a bool");
1103 wxString
wxVariant::GetString() const
1106 if (Convert(& value
))
1110 wxFAIL_MSG("Could not convert to a string");
1111 return wxString("");
1115 wxList
& wxVariant::GetList() const
1117 wxASSERT( (GetType() == "list") );
1119 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1122 wxStringList
& wxVariant::GetStringList() const
1124 wxASSERT( (GetType() == "stringlist") );
1126 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1130 void wxVariant::Append(const wxVariant
& value
)
1132 wxList
& list
= GetList();
1134 list
.Append(new wxVariant(value
));
1137 // Insert at front of list
1138 void wxVariant::Insert(const wxVariant
& value
)
1140 wxList
& list
= GetList();
1142 list
.Insert(new wxVariant(value
));
1145 // Returns TRUE if the variant is a member of the list
1146 bool wxVariant::Member(const wxVariant
& value
) const
1148 wxList
& list
= GetList();
1150 wxNode
* node
= list
.First();
1153 wxVariant
* other
= (wxVariant
*) node
->Data();
1154 if (value
== *other
)
1156 node
= node
->Next();
1161 // Deletes the nth element of the list
1162 bool wxVariant::Delete(int item
)
1164 wxList
& list
= GetList();
1166 wxASSERT_MSG( (item
< list
.Number()), "Invalid index to Delete" );
1167 wxNode
* node
= list
.Nth(item
);
1168 wxVariant
* variant
= (wxVariant
*) node
->Data();
1175 void wxVariant::ClearList()
1177 if (!IsNull() && (GetType() == "list"))
1179 ((wxVariantDataList
*) m_data
)->Clear();
1183 if (GetType() != "list")
1188 m_data
= new wxVariantDataList
;
1193 bool wxVariant::Convert(long* value
) const
1195 wxString
type(GetType());
1196 if (type
== "double")
1197 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1198 else if (type
== "long")
1199 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1200 else if (type
== "bool")
1201 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1202 else if (type
== "string")
1203 *value
= atol((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1210 bool wxVariant::Convert(bool* value
) const
1212 wxString
type(GetType());
1213 if (type
== "double")
1214 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1215 else if (type
== "long")
1216 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1217 else if (type
== "bool")
1218 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1219 else if (type
== "string")
1221 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1223 if (val
== "true" || val
== "yes")
1225 else if (val
== "false" || val
== "no")
1236 bool wxVariant::Convert(double* value
) const
1238 wxString
type(GetType());
1239 if (type
== "double")
1240 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1241 else if (type
== "long")
1242 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1243 else if (type
== "bool")
1244 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1245 else if (type
== "string")
1246 *value
= (double) atof((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1253 bool wxVariant::Convert(char* value
) const
1255 wxString
type(GetType());
1257 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1258 else if (type
== "long")
1259 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1260 else if (type
== "bool")
1261 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1268 bool wxVariant::Convert(wxString
* value
) const
1270 *value
= MakeString();