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/string.h"
33 #include "wx/variant.h"
35 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
37 wxVariant wxNullVariant
;
43 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
45 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
47 wxVariantDataList() {}
48 wxVariantDataList(const wxList
& list
);
51 wxList
& GetValue() const { return (wxList
&) m_value
; }
52 void SetValue(const wxList
& value
) ;
54 virtual void Copy(wxVariantData
& data
);
55 virtual bool Eq(wxVariantData
& data
) const;
56 virtual bool Write(ostream
& str
) const;
57 virtual bool Write(wxString
& str
) const;
58 virtual bool Read(istream
& str
);
59 virtual bool Read(wxString
& str
);
60 virtual wxString
GetType() const { return "list"; };
68 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
70 wxVariantDataList::wxVariantDataList(const wxList
& list
)
75 wxVariantDataList::~wxVariantDataList()
80 void wxVariantDataList::SetValue(const wxList
& value
)
83 wxNode
* node
= value
.First();
86 wxVariant
* var
= (wxVariant
*) node
->Data();
87 m_value
.Append(new wxVariant(*var
));
92 void wxVariantDataList::Clear()
94 wxNode
* node
= m_value
.First();
97 wxVariant
* var
= (wxVariant
*) node
->Data();
104 void wxVariantDataList::Copy(wxVariantData
& data
)
106 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Copy: Can't copy to this type of data" );
108 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
111 wxNode
* node
= m_value
.First();
114 wxVariant
* var
= (wxVariant
*) node
->Data();
115 listData
.m_value
.Append(new wxVariant(*var
));
120 bool wxVariantDataList::Eq(wxVariantData
& data
) const
122 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Eq: argument mismatch" );
124 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
125 wxNode
* node1
= m_value
.First();
126 wxNode
* node2
= listData
.GetValue().First();
127 while (node1
&& node2
)
129 wxVariant
* var1
= (wxVariant
*) node1
->Data();
130 wxVariant
* var2
= (wxVariant
*) node2
->Data();
131 if ((*var1
) != (*var2
))
133 node1
= node1
->Next();
134 node2
= node2
->Next();
136 if (node1
|| node2
) return FALSE
;
140 bool wxVariantDataList::Write(ostream
& str
) const
148 bool wxVariantDataList::Write(wxString
& str
) const
151 wxNode
* node
= m_value
.First();
154 wxVariant
* var
= (wxVariant
*) node
->Data();
155 if (node
!= m_value
.First())
158 str
+= var
->MakeString();
165 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
167 wxFAIL_MSG("Unimplemented");
172 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
174 wxFAIL_MSG("Unimplemented");
180 * wxVariantDataStringList
183 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
185 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
187 wxVariantDataStringList() {}
188 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
190 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
191 void SetValue(const wxStringList
& value
);
193 virtual void Copy(wxVariantData
& data
);
194 virtual bool Eq(wxVariantData
& data
) const;
195 virtual bool Write(ostream
& str
) const;
196 virtual bool Write(wxString
& str
) const;
197 virtual bool Read(istream
& str
);
198 virtual bool Read(wxString
& str
);
199 virtual wxString
GetType() const { return "stringlist"; };
202 wxStringList m_value
;
205 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
207 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
212 void wxVariantDataStringList::Copy(wxVariantData
& data
)
214 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Copy: Can't copy to this type of data" );
216 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
218 listData
.m_value
= m_value
;
221 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
223 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Eq: argument mismatch" );
225 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
226 wxNode
* node1
= m_value
.First();
227 wxNode
* node2
= listData
.GetValue().First();
228 while (node1
&& node2
)
230 wxString
str1 ((char*) node1
->Data());
231 wxString
str2 ((char*) node2
->Data());
234 node1
= node1
->Next();
235 node2
= node2
->Next();
237 if (node1
|| node2
) return FALSE
;
241 bool wxVariantDataStringList::Write(ostream
& str
) const
249 bool wxVariantDataStringList::Write(wxString
& str
) const
252 wxNode
* node
= m_value
.First();
255 char* s
= (char*) node
->Data();
256 if (node
!= m_value
.First())
265 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
267 wxFAIL_MSG("Unimplemented");
272 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
274 wxFAIL_MSG("Unimplemented");
283 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
285 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
287 wxVariantDataLong() { m_value
= 0; }
288 wxVariantDataLong(long value
) { m_value
= value
; }
290 inline long GetValue() const { return m_value
; }
291 inline void SetValue(long value
) { m_value
= value
; }
293 virtual void Copy(wxVariantData
& data
);
294 virtual bool Eq(wxVariantData
& data
) const;
295 virtual bool Write(ostream
& str
) const;
296 virtual bool Write(wxString
& str
) const;
297 virtual bool Read(istream
& str
);
298 virtual bool Read(wxString
& str
);
299 virtual wxString
GetType() const { return "long"; };
305 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
307 void wxVariantDataLong::Copy(wxVariantData
& data
)
309 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Copy: Can't copy to this type of data" );
311 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
313 otherData
.m_value
= m_value
;
316 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
318 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Eq: argument mismatch" );
320 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
322 return (otherData
.m_value
== m_value
);
325 bool wxVariantDataLong::Write(ostream
& str
) const
333 bool wxVariantDataLong::Write(wxString
& str
) const
335 str
.Printf("%ld", m_value
);
339 bool wxVariantDataLong::Read(istream
& str
)
345 bool wxVariantDataLong::Read(wxString
& str
)
347 m_value
= atol((const char*) str
);
355 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
357 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
359 wxVariantDataReal() { m_value
= 0.0; }
360 wxVariantDataReal(double value
) { m_value
= value
; }
362 inline double GetValue() const { return m_value
; }
363 inline void SetValue(double value
) { m_value
= value
; }
365 virtual void Copy(wxVariantData
& data
);
366 virtual bool Eq(wxVariantData
& data
) const;
367 virtual bool Write(ostream
& str
) const;
368 virtual bool Write(wxString
& str
) const;
369 virtual bool Read(istream
& str
);
370 virtual bool Read(wxString
& str
);
371 virtual wxString
GetType() const { return "double"; };
377 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
379 void wxVariantDataReal::Copy(wxVariantData
& data
)
381 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Copy: Can't copy to this type of data" );
383 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
385 otherData
.m_value
= m_value
;
388 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
390 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Eq: argument mismatch" );
392 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
394 return (otherData
.m_value
== m_value
);
397 bool wxVariantDataReal::Write(ostream
& str
) const
405 bool wxVariantDataReal::Write(wxString
& str
) const
407 str
.Printf("%.4f", m_value
);
411 bool wxVariantDataReal::Read(istream
& str
)
417 bool wxVariantDataReal::Read(wxString
& str
)
419 m_value
= atof((const char*) str
);
427 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
429 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
431 wxVariantDataBool() { m_value
= 0; }
432 wxVariantDataBool(bool value
) { m_value
= value
; }
434 inline bool GetValue() const { return m_value
; }
435 inline void SetValue(bool value
) { m_value
= value
; }
437 virtual void Copy(wxVariantData
& data
);
438 virtual bool Eq(wxVariantData
& data
) const;
439 virtual bool Write(ostream
& str
) const;
440 virtual bool Write(wxString
& str
) const;
441 virtual bool Read(istream
& str
);
442 virtual bool Read(wxString
& str
);
443 virtual wxString
GetType() const { return "bool"; };
449 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
451 void wxVariantDataBool::Copy(wxVariantData
& data
)
453 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Copy: Can't copy to this type of data" );
455 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
457 otherData
.m_value
= m_value
;
460 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
462 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Eq: argument mismatch" );
464 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
466 return (otherData
.m_value
== m_value
);
469 bool wxVariantDataBool::Write(ostream
& str
) const
477 bool wxVariantDataBool::Write(wxString
& str
) const
479 str
.Printf("%d", (int) m_value
);
483 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
485 wxFAIL_MSG("Unimplemented");
486 // str >> (long) m_value;
490 bool wxVariantDataBool::Read(wxString
& str
)
492 m_value
= (atol((const char*) str
) != 0);
500 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
502 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
504 wxVariantDataChar() { m_value
= 0; }
505 wxVariantDataChar(char value
) { m_value
= value
; }
507 inline char GetValue() const { return m_value
; }
508 inline void SetValue(char value
) { m_value
= value
; }
510 virtual void Copy(wxVariantData
& data
);
511 virtual bool Eq(wxVariantData
& data
) const;
512 virtual bool Write(ostream
& str
) const;
513 virtual bool Write(wxString
& str
) const;
514 virtual bool Read(istream
& str
);
515 virtual bool Read(wxString
& str
);
516 virtual wxString
GetType() const { return "char"; };
522 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
524 void wxVariantDataChar::Copy(wxVariantData
& data
)
526 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Copy: Can't copy to this type of data" );
528 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
530 otherData
.m_value
= m_value
;
533 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
535 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Eq: argument mismatch" );
537 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
539 return (otherData
.m_value
== m_value
);
542 bool wxVariantDataChar::Write(ostream
& str
) const
550 bool wxVariantDataChar::Write(wxString
& str
) const
552 str
.Printf("%c", m_value
);
556 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
558 wxFAIL_MSG("Unimplemented");
563 bool wxVariantDataChar::Read(wxString
& str
)
565 m_value
= str
[(size_t)0];
570 * wxVariantDataString
573 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
575 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
577 wxVariantDataString() { }
578 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
580 inline wxString
GetValue() const { return m_value
; }
581 inline void SetValue(const wxString
& value
) { m_value
= value
; }
583 virtual void Copy(wxVariantData
& data
);
584 virtual bool Eq(wxVariantData
& data
) const;
585 virtual bool Write(ostream
& str
) const;
586 virtual bool Write(wxString
& str
) const;
587 virtual bool Read(istream
& str
);
588 virtual bool Read(wxString
& str
);
589 virtual wxString
GetType() const { return "string"; };
595 void wxVariantDataString::Copy(wxVariantData
& data
)
597 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Copy: Can't copy to this type of data" );
599 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
601 otherData
.m_value
= m_value
;
604 bool wxVariantDataString::Eq(wxVariantData
& data
) const
606 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Eq: argument mismatch" );
608 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
610 return (otherData
.m_value
== m_value
);
613 bool wxVariantDataString::Write(ostream
& str
) const
619 bool wxVariantDataString::Write(wxString
& str
) const
625 bool wxVariantDataString::Read(istream
& str
)
631 bool wxVariantDataString::Read(wxString
& str
)
637 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
643 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
645 // Construction & destruction
646 wxVariant::wxVariant()
648 m_data
= (wxVariantData
*) NULL
;
651 wxVariant::wxVariant(double val
)
653 m_data
= new wxVariantDataReal(val
);
656 wxVariant::wxVariant(long val
)
658 m_data
= new wxVariantDataLong(val
);
661 wxVariant::wxVariant(bool val
)
663 m_data
= new wxVariantDataBool(val
);
666 wxVariant::wxVariant(char val
)
668 m_data
= new wxVariantDataChar(val
);
671 wxVariant::wxVariant(const wxString
& val
)
673 m_data
= new wxVariantDataString(val
);
676 wxVariant::wxVariant(const char* val
)
678 m_data
= new wxVariantDataString(wxString(val
));
682 wxVariant::wxVariant(const wxStringList& val)
684 m_data = new wxVariantDataStringList(val);
688 wxVariant::wxVariant(const wxList
& val
) // List of variants
690 m_data
= new wxVariantDataList(val
);
693 wxVariant::wxVariant(const wxVariant
& variant
)
695 if (!variant
.IsNull())
697 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
698 variant
.m_data
->Copy(*m_data
);
702 wxVariant::wxVariant(wxVariantData
* data
) // User-defined data
707 wxVariant::~wxVariant()
713 // Make NULL (i.e. delete the data)
714 void wxVariant::MakeNull()
722 void wxVariant::operator= (const wxVariant
& variant
)
724 if (variant
.IsNull())
730 if (IsNull() || (GetType() != variant
.GetType()))
734 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
736 GetData()->Copy(* variant
.GetData());
739 // Assignment using data, e.g.
740 // myVariant = new wxStringVariantData("hello")
741 void wxVariant::operator= (wxVariantData
* variantData
)
744 m_data
= variantData
;
747 bool wxVariant::operator== (const wxVariant
& variant
) const
749 if (IsNull() || variant
.IsNull())
750 return (IsNull() == variant
.IsNull());
752 return (GetData()->Eq(* variant
.GetData()));
755 bool wxVariant::operator!= (const wxVariant
& variant
) const
757 return (!(*this == variant
));
761 // Specific operators
762 bool wxVariant::operator== (double value
) const
765 if (!Convert(&thisValue
))
768 return (value
== thisValue
);
771 bool wxVariant::operator!= (double value
) const
773 return (!((*this) == value
));
776 void wxVariant::operator= (double value
)
778 if (GetType() == "double")
780 ((wxVariantDataReal
*)GetData())->SetValue(value
);
786 m_data
= new wxVariantDataReal(value
);
790 bool wxVariant::operator== (long value
) const
793 if (!Convert(&thisValue
))
796 return (value
== thisValue
);
799 bool wxVariant::operator!= (long value
) const
801 return (!((*this) == value
));
804 void wxVariant::operator= (long value
)
806 if (GetType() == "long")
808 ((wxVariantDataLong
*)GetData())->SetValue(value
);
814 m_data
= new wxVariantDataLong(value
);
818 bool wxVariant::operator== (char value
) const
821 if (!Convert(&thisValue
))
824 return (value
== thisValue
);
827 bool wxVariant::operator!= (char value
) const
829 return (!((*this) == value
));
832 void wxVariant::operator= (char value
)
834 if (GetType() == "char")
836 ((wxVariantDataChar
*)GetData())->SetValue(value
);
842 m_data
= new wxVariantDataChar(value
);
846 bool wxVariant::operator== (bool value
) const
849 if (!Convert(&thisValue
))
852 return (value
== thisValue
);
855 bool wxVariant::operator!= (bool value
) const
857 return (!((*this) == value
));
860 void wxVariant::operator= (bool value
)
862 if (GetType() == "bool")
864 ((wxVariantDataBool
*)GetData())->SetValue(value
);
870 m_data
= new wxVariantDataBool(value
);
874 bool wxVariant::operator== (const wxString
& value
) const
877 if (!Convert(&thisValue
))
880 return (value
== thisValue
);
883 bool wxVariant::operator!= (const wxString
& value
) const
885 return (!((*this) == value
));
888 void wxVariant::operator= (const wxString
& value
)
890 if (GetType() == "string")
892 ((wxVariantDataString
*)GetData())->SetValue(value
);
898 m_data
= new wxVariantDataString(value
);
902 void wxVariant::operator= (const char* value
)
904 if (GetType() == "string")
906 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
912 m_data
= new wxVariantDataString(wxString(value
));
916 bool wxVariant::operator== (const wxStringList
& value
) const
918 wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" );
920 wxVariantDataStringList
other(value
);
921 return (m_data
->Eq(other
));
924 bool wxVariant::operator!= (const wxStringList
& value
) const
926 return (!((*this) == value
));
929 void wxVariant::operator= (const wxStringList
& value
)
931 if (GetType() == "stringlist")
933 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
939 m_data
= new wxVariantDataStringList(value
);
943 bool wxVariant::operator== (const wxList
& value
) const
945 wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" );
947 wxVariantDataList
other(value
);
948 return (m_data
->Eq(other
));
951 bool wxVariant::operator!= (const wxList
& value
) const
953 return (!((*this) == value
));
956 void wxVariant::operator= (const wxList
& value
)
958 if (GetType() == "list")
960 ((wxVariantDataList
*)GetData())->SetValue(value
);
966 m_data
= new wxVariantDataList(value
);
971 // Treat a list variant as an array
972 wxVariant
wxVariant::operator[] (size_t idx
) const
974 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" );
976 if (GetType() == "list")
978 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
979 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
980 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
982 else if (GetType() == "stringlist")
984 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
985 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
987 wxVariant
variant( wxString( (char*) (data
->GetValue().Nth(idx
)->Data()) ));
990 return wxNullVariant
;
993 wxVariant
& wxVariant::operator[] (size_t idx
)
995 // We can't return a reference to a variant for a string list, since the string
996 // is actually stored as a char*, not a variant.
998 wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" );
1000 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1001 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1003 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1006 // Return the number of elements in a list
1007 int wxVariant::GetCount() const
1009 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" );
1011 if (GetType() == "list")
1013 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1014 return data
->GetValue().Number();
1016 else if (GetType() == "stringlist")
1018 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1019 return data
->GetValue().Number();
1024 wxString
wxVariant::MakeString() const
1029 if (GetData()->Write(str
))
1032 return wxString("");
1037 void wxVariant::SetData(wxVariantData
* data
)
1039 if (m_data
) delete m_data
;
1044 // Returns a string representing the type of the variant,
1045 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1046 wxString
wxVariant::GetType() const
1049 return wxString("null");
1051 return m_data
->GetType();
1055 bool wxVariant::IsType(const wxString
& type
) const
1057 return (GetType() == type
);
1062 double wxVariant::GetReal() const
1065 if (Convert(& value
))
1069 wxFAIL_MSG("Could not convert to a real number");
1074 long wxVariant::GetInteger() const
1077 if (Convert(& value
))
1081 wxFAIL_MSG("Could not convert to an integer");
1086 char wxVariant::GetChar() const
1089 if (Convert(& value
))
1093 wxFAIL_MSG("Could not convert to a char");
1098 bool wxVariant::GetBool() const
1101 if (Convert(& value
))
1105 wxFAIL_MSG("Could not convert to a bool");
1110 wxString
wxVariant::GetString() const
1113 if (Convert(& value
))
1117 wxFAIL_MSG("Could not convert to a string");
1118 return wxString("");
1122 wxList
& wxVariant::GetList() const
1124 wxASSERT( (GetType() == "list") );
1126 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1129 wxStringList
& wxVariant::GetStringList() const
1131 wxASSERT( (GetType() == "stringlist") );
1133 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1137 void wxVariant::Append(const wxVariant
& value
)
1139 wxList
& list
= GetList();
1141 list
.Append(new wxVariant(value
));
1144 // Insert at front of list
1145 void wxVariant::Insert(const wxVariant
& value
)
1147 wxList
& list
= GetList();
1149 list
.Insert(new wxVariant(value
));
1152 // Returns TRUE if the variant is a member of the list
1153 bool wxVariant::Member(const wxVariant
& value
) const
1155 wxList
& list
= GetList();
1157 wxNode
* node
= list
.First();
1160 wxVariant
* other
= (wxVariant
*) node
->Data();
1161 if (value
== *other
)
1163 node
= node
->Next();
1168 // Deletes the nth element of the list
1169 bool wxVariant::Delete(int item
)
1171 wxList
& list
= GetList();
1173 wxASSERT_MSG( (item
< list
.Number()), "Invalid index to Delete" );
1174 wxNode
* node
= list
.Nth(item
);
1175 wxVariant
* variant
= (wxVariant
*) node
->Data();
1182 void wxVariant::ClearList()
1184 if (!IsNull() && (GetType() == "list"))
1186 ((wxVariantDataList
*) m_data
)->Clear();
1190 if (GetType() != "list")
1195 m_data
= new wxVariantDataList
;
1200 bool wxVariant::Convert(long* value
) const
1202 wxString
type(GetType());
1203 if (type
== "double")
1204 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1205 else if (type
== "long")
1206 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1207 else if (type
== "bool")
1208 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1209 else if (type
== "string")
1210 *value
= atol((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1217 bool wxVariant::Convert(bool* value
) const
1219 wxString
type(GetType());
1220 if (type
== "double")
1221 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1222 else if (type
== "long")
1223 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1224 else if (type
== "bool")
1225 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1226 else if (type
== "string")
1228 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1230 if (val
== "true" || val
== "yes")
1232 else if (val
== "false" || val
== "no")
1243 bool wxVariant::Convert(double* value
) const
1245 wxString
type(GetType());
1246 if (type
== "double")
1247 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1248 else if (type
== "long")
1249 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1250 else if (type
== "bool")
1251 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1252 else if (type
== "string")
1253 *value
= (double) atof((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1260 bool wxVariant::Convert(char* value
) const
1262 wxString
type(GetType());
1264 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1265 else if (type
== "long")
1266 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1267 else if (type
== "bool")
1268 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1275 bool wxVariant::Convert(wxString
* value
) const
1277 *value
= MakeString();