1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/variant.cpp
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/variant.h"
24 #include "wx/string.h"
28 #include "wx/stream.h"
32 #if wxUSE_STD_IOSTREAM
40 #if defined(__MWERKS__) && __MSL__ >= 0x6000
46 #include "wx/txtstrm.h"
49 #include "wx/string.h"
50 #include "wx/tokenzr.h"
52 wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
55 #include "wx/listimpl.cpp"
56 WX_DEFINE_LIST(wxVariantList
)
62 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
64 wxVariant::wxVariant()
66 m_data
= (wxVariantData
*) NULL
;
69 bool wxVariant::IsNull() const
71 return (m_data
== (wxVariantData
*) NULL
);
74 void wxVariant::MakeNull()
79 void wxVariant::Clear()
81 m_name
= wxEmptyString
;
84 wxVariant::wxVariant(const wxVariant
& variant
)
87 m_data
= (wxVariantData
*) NULL
;
89 if (!variant
.IsNull())
92 m_name
= variant
.m_name
;
95 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
101 wxVariant::~wxVariant()
107 void wxVariant::operator= (const wxVariant
& variant
)
110 m_name
= variant
.m_name
;
113 // myVariant = new wxStringVariantData("hello")
114 void wxVariant::operator= (wxVariantData
* variantData
)
117 m_data
= variantData
;
120 bool wxVariant::operator== (const wxVariant
& variant
) const
122 if (IsNull() || variant
.IsNull())
123 return (IsNull() == variant
.IsNull());
125 return (GetData()->Eq(* variant
.GetData()));
128 bool wxVariant::operator!= (const wxVariant
& variant
) const
130 return (!(*this == variant
));
133 wxString
wxVariant::MakeString() const
138 if (GetData()->Write(str
))
141 return wxEmptyString
;
144 void wxVariant::SetData(wxVariantData
* data
)
150 void wxVariant::Ref(const wxVariant
& clone
)
152 // nothing to be done
153 if (m_data
== clone
.m_data
)
156 // delete reference to old data
159 // reference new data
162 m_data
= clone
.m_data
;
168 void wxVariant::UnRef()
172 wxASSERT_MSG( m_data
->m_count
> 0, _T("invalid ref data count") );
179 bool wxVariant::Unshare()
181 if ( m_data
&& m_data
->GetRefCount() > 1 )
183 // note that ref is not going to be destroyed in this case...
184 const wxVariantData
* ref
= m_data
;
187 // ... so we can still access it
188 m_data
= ref
->Clone();
190 wxASSERT_MSG( (m_data
&& m_data
->GetRefCount() == 1),
191 _T("wxVariant::AllocExclusive() failed.") );
193 if (!m_data
|| m_data
->GetRefCount() != 1)
198 //else: data is null or ref count is 1, so we are exclusive owners of m_refData anyhow
204 // Returns a string representing the type of the variant,
205 // e.g. "string", "bool", "list", "double", "long"
206 wxString
wxVariant::GetType() const
209 return wxString(wxT("null"));
211 return GetData()->GetType();
215 bool wxVariant::IsType(const wxString
& type
) const
217 return (GetType() == type
);
220 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
222 wxClassInfo
* info
=GetData()->GetValueClassInfo();
223 return info
? info
->IsKindOf(type
) : false ;
227 // -----------------------------------------------------------------
229 // -----------------------------------------------------------------
231 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
234 wxVariantDataLong() { m_value
= 0; }
235 wxVariantDataLong(long value
) { m_value
= value
; }
237 inline long GetValue() const { return m_value
; }
238 inline void SetValue(long value
) { m_value
= value
; }
240 virtual bool Eq(wxVariantData
& data
) const;
242 virtual bool Read(wxString
& str
);
243 virtual bool Write(wxString
& str
) const;
244 #if wxUSE_STD_IOSTREAM
245 virtual bool Read(wxSTD istream
& str
);
246 virtual bool Write(wxSTD ostream
& str
) const;
249 virtual bool Read(wxInputStream
& str
);
250 virtual bool Write(wxOutputStream
&str
) const;
251 #endif // wxUSE_STREAMS
253 wxVariantData
* Clone() const { return new wxVariantDataLong(m_value
); }
255 virtual wxString
GetType() const { return wxT("long"); }
261 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
263 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
265 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
267 return (otherData
.m_value
== m_value
);
270 #if wxUSE_STD_IOSTREAM
271 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
275 str
<< (const char*) s
.mb_str();
280 bool wxVariantDataLong::Write(wxString
& str
) const
282 str
.Printf(wxT("%ld"), m_value
);
286 #if wxUSE_STD_IOSTREAM
287 bool wxVariantDataLong::Read(wxSTD istream
& str
)
295 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
297 wxTextOutputStream
s(str
);
299 s
.Write32((size_t)m_value
);
303 bool wxVariantDataLong::Read(wxInputStream
& str
)
305 wxTextInputStream
s(str
);
306 m_value
= s
.Read32();
309 #endif // wxUSE_STREAMS
311 bool wxVariantDataLong::Read(wxString
& str
)
313 m_value
= wxAtol(str
);
319 wxVariant::wxVariant(long val
, const wxString
& name
)
321 m_data
= new wxVariantDataLong(val
);
325 wxVariant::wxVariant(int val
, const wxString
& name
)
327 m_data
= new wxVariantDataLong((long)val
);
331 wxVariant::wxVariant(short val
, const wxString
& name
)
333 m_data
= new wxVariantDataLong((long)val
);
337 bool wxVariant::operator== (long value
) const
340 if (!Convert(&thisValue
))
343 return (value
== thisValue
);
346 bool wxVariant::operator!= (long value
) const
348 return (!((*this) == value
));
351 void wxVariant::operator= (long value
)
353 if (GetType() == wxT("long") &&
354 m_data
->GetRefCount() == 1)
356 ((wxVariantDataLong
*)GetData())->SetValue(value
);
361 m_data
= new wxVariantDataLong(value
);
365 long wxVariant::GetLong() const
368 if (Convert(& value
))
372 wxFAIL_MSG(wxT("Could not convert to a long"));
377 // -----------------------------------------------------------------
378 // wxVariantDoubleData
379 // -----------------------------------------------------------------
381 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
384 wxVariantDoubleData() { m_value
= 0.0; }
385 wxVariantDoubleData(double value
) { m_value
= value
; }
387 inline double GetValue() const { return m_value
; }
388 inline void SetValue(double value
) { m_value
= value
; }
390 virtual bool Eq(wxVariantData
& data
) const;
391 virtual bool Read(wxString
& str
);
392 #if wxUSE_STD_IOSTREAM
393 virtual bool Write(wxSTD ostream
& str
) const;
395 virtual bool Write(wxString
& str
) const;
396 #if wxUSE_STD_IOSTREAM
397 virtual bool Read(wxSTD istream
& str
);
400 virtual bool Read(wxInputStream
& str
);
401 virtual bool Write(wxOutputStream
&str
) const;
402 #endif // wxUSE_STREAMS
403 virtual wxString
GetType() const { return wxT("double"); }
405 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
410 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
412 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
414 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
416 return wxIsSameDouble(otherData
.m_value
, m_value
);
419 #if wxUSE_STD_IOSTREAM
420 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
424 str
<< (const char*) s
.mb_str();
429 bool wxVariantDoubleData::Write(wxString
& str
) const
431 str
.Printf(wxT("%.14g"), m_value
);
435 #if wxUSE_STD_IOSTREAM
436 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
444 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
446 wxTextOutputStream
s(str
);
447 s
.WriteDouble((double)m_value
);
451 bool wxVariantDoubleData::Read(wxInputStream
& str
)
453 wxTextInputStream
s(str
);
454 m_value
= (float)s
.ReadDouble();
457 #endif // wxUSE_STREAMS
459 bool wxVariantDoubleData::Read(wxString
& str
)
461 m_value
= wxAtof(str
);
465 // wxVariant double code
467 wxVariant::wxVariant(double val
, const wxString
& name
)
469 m_data
= new wxVariantDoubleData(val
);
473 bool wxVariant::operator== (double value
) const
476 if (!Convert(&thisValue
))
479 return wxIsSameDouble(value
, thisValue
);
482 bool wxVariant::operator!= (double value
) const
484 return (!((*this) == value
));
487 void wxVariant::operator= (double value
)
489 if (GetType() == wxT("double") &&
490 m_data
->GetRefCount() == 1)
492 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
497 m_data
= new wxVariantDoubleData(value
);
501 double wxVariant::GetDouble() const
504 if (Convert(& value
))
508 wxFAIL_MSG(wxT("Could not convert to a double number"));
513 // -----------------------------------------------------------------
515 // -----------------------------------------------------------------
519 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
522 wxVariantDataBool() { m_value
= 0; }
523 wxVariantDataBool(bool value
) { m_value
= value
; }
525 inline bool GetValue() const { return m_value
; }
526 inline void SetValue(bool value
) { m_value
= value
; }
528 virtual bool Eq(wxVariantData
& data
) const;
529 #if wxUSE_STD_IOSTREAM
530 virtual bool Write(wxSTD ostream
& str
) const;
532 virtual bool Write(wxString
& str
) const;
533 virtual bool Read(wxString
& str
);
534 #if wxUSE_STD_IOSTREAM
535 virtual bool Read(wxSTD istream
& str
);
538 virtual bool Read(wxInputStream
& str
);
539 virtual bool Write(wxOutputStream
& str
) const;
540 #endif // wxUSE_STREAMS
541 virtual wxString
GetType() const { return wxT("bool"); }
543 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
548 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
550 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
552 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
554 return (otherData
.m_value
== m_value
);
557 #if wxUSE_STD_IOSTREAM
558 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
562 str
<< (const char*) s
.mb_str();
567 bool wxVariantDataBool::Write(wxString
& str
) const
569 str
.Printf(wxT("%d"), (int) m_value
);
573 #if wxUSE_STD_IOSTREAM
574 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
576 wxFAIL_MSG(wxT("Unimplemented"));
577 // str >> (long) m_value;
583 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
585 wxTextOutputStream
s(str
);
591 bool wxVariantDataBool::Read(wxInputStream
& str
)
593 wxTextInputStream
s(str
);
595 m_value
= s
.Read8() != 0;
598 #endif // wxUSE_STREAMS
600 bool wxVariantDataBool::Read(wxString
& str
)
602 m_value
= (wxAtol(str
) != 0);
608 wxVariant::wxVariant(bool val
, const wxString
& name
)
610 m_data
= new wxVariantDataBool(val
);
614 bool wxVariant::operator== (bool value
) const
617 if (!Convert(&thisValue
))
620 return (value
== thisValue
);
623 bool wxVariant::operator!= (bool value
) const
625 return (!((*this) == value
));
628 void wxVariant::operator= (bool value
)
630 if (GetType() == wxT("bool") &&
631 m_data
->GetRefCount() == 1)
633 ((wxVariantDataBool
*)GetData())->SetValue(value
);
638 m_data
= new wxVariantDataBool(value
);
642 bool wxVariant::GetBool() const
645 if (Convert(& value
))
649 wxFAIL_MSG(wxT("Could not convert to a bool"));
656 // -----------------------------------------------------------------
658 // -----------------------------------------------------------------
660 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
663 wxVariantDataChar() { m_value
= 0; }
664 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
666 inline wxUniChar
GetValue() const { return m_value
; }
667 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
669 virtual bool Eq(wxVariantData
& data
) const;
670 #if wxUSE_STD_IOSTREAM
671 virtual bool Read(wxSTD istream
& str
);
672 virtual bool Write(wxSTD ostream
& str
) const;
674 virtual bool Read(wxString
& str
);
675 virtual bool Write(wxString
& str
) const;
677 virtual bool Read(wxInputStream
& str
);
678 virtual bool Write(wxOutputStream
& str
) const;
679 #endif // wxUSE_STREAMS
680 virtual wxString
GetType() const { return wxT("char"); }
681 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
687 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
689 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
691 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
693 return (otherData
.m_value
== m_value
);
696 #if wxUSE_STD_IOSTREAM
697 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
699 str
<< wxString(m_value
);
704 bool wxVariantDataChar::Write(wxString
& str
) const
710 #if wxUSE_STD_IOSTREAM
711 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
713 wxFAIL_MSG(wxT("Unimplemented"));
720 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
722 wxTextOutputStream
s(str
);
724 // FIXME-UTF8: this should be just "s << m_value;" after removal of
725 // ANSI build and addition of wxUniChar to wxTextOutputStream:
726 s
<< (wxChar
)m_value
;
731 bool wxVariantDataChar::Read(wxInputStream
& str
)
733 wxTextInputStream
s(str
);
735 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
736 // ANSI build and addition of wxUniChar to wxTextInputStream:
743 #endif // wxUSE_STREAMS
745 bool wxVariantDataChar::Read(wxString
& str
)
751 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
753 m_data
= new wxVariantDataChar(val
);
757 wxVariant::wxVariant(char val
, const wxString
& name
)
759 m_data
= new wxVariantDataChar(val
);
763 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
765 m_data
= new wxVariantDataChar(val
);
769 bool wxVariant::operator==(const wxUniChar
& value
) const
772 if (!Convert(&thisValue
))
775 return (value
== thisValue
);
778 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
780 if (GetType() == wxT("char") &&
781 m_data
->GetRefCount() == 1)
783 ((wxVariantDataChar
*)GetData())->SetValue(value
);
788 m_data
= new wxVariantDataChar(value
);
794 wxUniChar
wxVariant::GetChar() const
797 if (Convert(& value
))
801 wxFAIL_MSG(wxT("Could not convert to a char"));
806 // ----------------------------------------------------------------------------
807 // wxVariantDataString
808 // ----------------------------------------------------------------------------
810 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
813 wxVariantDataString() { }
814 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
816 inline wxString
GetValue() const { return m_value
; }
817 inline void SetValue(const wxString
& value
) { m_value
= value
; }
819 virtual bool Eq(wxVariantData
& data
) const;
820 #if wxUSE_STD_IOSTREAM
821 virtual bool Write(wxSTD ostream
& str
) const;
823 virtual bool Read(wxString
& str
);
824 virtual bool Write(wxString
& str
) const;
825 #if wxUSE_STD_IOSTREAM
826 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
829 virtual bool Read(wxInputStream
& str
);
830 virtual bool Write(wxOutputStream
& str
) const;
831 #endif // wxUSE_STREAMS
832 virtual wxString
GetType() const { return wxT("string"); }
833 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
839 bool wxVariantDataString::Eq(wxVariantData
& data
) const
841 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
843 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
845 return (otherData
.m_value
== m_value
);
848 #if wxUSE_STD_IOSTREAM
849 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
851 str
<< (const char*) m_value
.mb_str();
856 bool wxVariantDataString::Write(wxString
& str
) const
863 bool wxVariantDataString::Write(wxOutputStream
& str
) const
865 // why doesn't wxOutputStream::operator<< take "const wxString&"
866 wxTextOutputStream
s(str
);
867 s
.WriteString(m_value
);
871 bool wxVariantDataString::Read(wxInputStream
& str
)
873 wxTextInputStream
s(str
);
875 m_value
= s
.ReadLine();
878 #endif // wxUSE_STREAMS
880 bool wxVariantDataString::Read(wxString
& str
)
888 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
890 m_data
= new wxVariantDataString(val
);
894 wxVariant::wxVariant(const char* val
, const wxString
& name
)
896 m_data
= new wxVariantDataString(wxString(val
));
900 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
902 m_data
= new wxVariantDataString(wxString(val
));
906 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
908 m_data
= new wxVariantDataString(val
.AsString());
912 wxVariant::wxVariant(const wxCharBuffer
& val
, const wxString
& name
)
914 m_data
= new wxVariantDataString(wxString(val
));
918 wxVariant::wxVariant(const wxWCharBuffer
& val
, const wxString
& name
)
920 m_data
= new wxVariantDataString(wxString(val
));
924 bool wxVariant::operator== (const wxString
& value
) const
927 if (!Convert(&thisValue
))
930 return value
== thisValue
;
933 bool wxVariant::operator!= (const wxString
& value
) const
935 return (!((*this) == value
));
938 wxVariant
& wxVariant::operator= (const wxString
& value
)
940 if (GetType() == wxT("string") &&
941 m_data
->GetRefCount() == 1)
943 ((wxVariantDataString
*)GetData())->SetValue(value
);
948 m_data
= new wxVariantDataString(value
);
953 wxString
wxVariant::GetString() const
956 if (!Convert(& value
))
958 wxFAIL_MSG(wxT("Could not convert to a string"));
964 // ----------------------------------------------------------------------------
965 // wxVariantDataWxObjectPtr
966 // ----------------------------------------------------------------------------
968 class wxVariantDataWxObjectPtr
: public wxVariantData
971 wxVariantDataWxObjectPtr() { }
972 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
974 inline wxObject
* GetValue() const { return m_value
; }
975 inline void SetValue(wxObject
* value
) { m_value
= value
; }
977 virtual bool Eq(wxVariantData
& data
) const;
978 #if wxUSE_STD_IOSTREAM
979 virtual bool Write(wxSTD ostream
& str
) const;
981 virtual bool Write(wxString
& str
) const;
982 #if wxUSE_STD_IOSTREAM
983 virtual bool Read(wxSTD istream
& str
);
985 virtual bool Read(wxString
& str
);
986 virtual wxString
GetType() const ;
987 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
989 virtual wxClassInfo
* GetValueClassInfo();
995 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
997 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
999 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1001 return (otherData
.m_value
== m_value
);
1004 wxString
wxVariantDataWxObjectPtr::GetType() const
1006 wxString
returnVal(wxT("wxObject*"));
1010 returnVal
= m_value
->GetClassInfo()->GetClassName();
1011 returnVal
+= wxT("*");
1017 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1019 wxClassInfo
* returnVal
=NULL
;
1021 if (m_value
) returnVal
= m_value
->GetClassInfo();
1026 #if wxUSE_STD_IOSTREAM
1027 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1031 str
<< (const char*) s
.mb_str();
1036 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1038 str
.Printf(wxT("%s(%p)"), GetType().c_str(), wx_static_cast(void*, m_value
));
1042 #if wxUSE_STD_IOSTREAM
1043 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1050 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1058 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1060 m_data
= new wxVariantDataWxObjectPtr(val
);
1064 bool wxVariant::operator== (wxObject
* value
) const
1066 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1069 bool wxVariant::operator!= (wxObject
* value
) const
1071 return (!((*this) == (wxObject
*) value
));
1074 void wxVariant::operator= (wxObject
* value
)
1077 m_data
= new wxVariantDataWxObjectPtr(value
);
1080 wxObject
* wxVariant::GetWxObjectPtr() const
1082 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_data
)->GetValue();
1085 // ----------------------------------------------------------------------------
1086 // wxVariantDataVoidPtr
1087 // ----------------------------------------------------------------------------
1089 class wxVariantDataVoidPtr
: public wxVariantData
1092 wxVariantDataVoidPtr() { }
1093 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1095 inline void* GetValue() const { return m_value
; }
1096 inline void SetValue(void* value
) { m_value
= value
; }
1098 virtual bool Eq(wxVariantData
& data
) const;
1099 #if wxUSE_STD_IOSTREAM
1100 virtual bool Write(wxSTD ostream
& str
) const;
1102 virtual bool Write(wxString
& str
) const;
1103 #if wxUSE_STD_IOSTREAM
1104 virtual bool Read(wxSTD istream
& str
);
1106 virtual bool Read(wxString
& str
);
1107 virtual wxString
GetType() const { return wxT("void*"); }
1108 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1114 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1116 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1118 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1120 return (otherData
.m_value
== m_value
);
1123 #if wxUSE_STD_IOSTREAM
1124 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1128 str
<< (const char*) s
.mb_str();
1133 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1135 str
.Printf(wxT("%p"), m_value
);
1139 #if wxUSE_STD_IOSTREAM
1140 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1147 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1155 wxVariant::wxVariant( void* val
, const wxString
& name
)
1157 m_data
= new wxVariantDataVoidPtr(val
);
1161 bool wxVariant::operator== (void* value
) const
1163 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1166 bool wxVariant::operator!= (void* value
) const
1168 return (!((*this) == (void*) value
));
1171 void wxVariant::operator= (void* value
)
1173 if (GetType() == wxT("void*") && (m_data
->GetRefCount() == 1))
1175 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1180 m_data
= new wxVariantDataVoidPtr(value
);
1184 void* wxVariant::GetVoidPtr() const
1186 wxASSERT( (GetType() == wxT("void*")) );
1188 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1191 // ----------------------------------------------------------------------------
1192 // wxVariantDataDateTime
1193 // ----------------------------------------------------------------------------
1197 class wxVariantDataDateTime
: public wxVariantData
1200 wxVariantDataDateTime() { }
1201 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1203 inline wxDateTime
GetValue() const { return m_value
; }
1204 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1206 virtual bool Eq(wxVariantData
& data
) const;
1207 #if wxUSE_STD_IOSTREAM
1208 virtual bool Write(wxSTD ostream
& str
) const;
1210 virtual bool Write(wxString
& str
) const;
1211 #if wxUSE_STD_IOSTREAM
1212 virtual bool Read(wxSTD istream
& str
);
1214 virtual bool Read(wxString
& str
);
1215 virtual wxString
GetType() const { return wxT("datetime"); }
1216 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1223 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1225 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1227 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1229 return (otherData
.m_value
== m_value
);
1233 #if wxUSE_STD_IOSTREAM
1234 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1238 str
<< value
.c_str();
1244 bool wxVariantDataDateTime::Write(wxString
& str
) const
1246 str
= m_value
.Format();
1251 #if wxUSE_STD_IOSTREAM
1252 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1260 bool wxVariantDataDateTime::Read(wxString
& str
)
1262 if(! m_value
.ParseDateTime(str
.c_str()/*FIXME-UTF8*/))
1269 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1271 m_data
= new wxVariantDataDateTime(val
);
1275 bool wxVariant::operator== (const wxDateTime
& value
) const
1277 wxDateTime thisValue
;
1278 if (!Convert(&thisValue
))
1281 return value
.IsEqualTo(thisValue
);
1284 bool wxVariant::operator!= (const wxDateTime
& value
) const
1286 return (!((*this) == value
));
1289 void wxVariant::operator= (const wxDateTime
& value
)
1291 if (GetType() == wxT("datetime") &&
1292 m_data
->GetRefCount() == 1)
1294 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1299 m_data
= new wxVariantDataDateTime(value
);
1303 wxDateTime
wxVariant::GetDateTime() const
1306 if (!Convert(& value
))
1308 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1314 #endif // wxUSE_DATETIME
1316 // ----------------------------------------------------------------------------
1317 // wxVariantDataArrayString
1318 // ----------------------------------------------------------------------------
1320 class wxVariantDataArrayString
: public wxVariantData
1323 wxVariantDataArrayString() { }
1324 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1326 wxArrayString
GetValue() const { return m_value
; }
1327 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1329 virtual bool Eq(wxVariantData
& data
) const;
1330 #if wxUSE_STD_IOSTREAM
1331 virtual bool Write(wxSTD ostream
& str
) const;
1333 virtual bool Write(wxString
& str
) const;
1334 #if wxUSE_STD_IOSTREAM
1335 virtual bool Read(wxSTD istream
& str
);
1337 virtual bool Read(wxString
& str
);
1338 virtual wxString
GetType() const { return wxT("arrstring"); }
1339 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1342 wxArrayString m_value
;
1345 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1347 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1349 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1351 return otherData
.m_value
== m_value
;
1354 #if wxUSE_STD_IOSTREAM
1355 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1362 bool wxVariantDataArrayString::Write(wxString
& str
) const
1364 size_t count
= m_value
.GetCount();
1365 for ( size_t n
= 0; n
< count
; n
++ )
1377 #if wxUSE_STD_IOSTREAM
1378 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1386 bool wxVariantDataArrayString::Read(wxString
& str
)
1388 wxStringTokenizer
tk(str
, _T(";"));
1389 while ( tk
.HasMoreTokens() )
1391 m_value
.Add(tk
.GetNextToken());
1399 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1401 m_data
= new wxVariantDataArrayString(val
);
1405 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1407 wxFAIL_MSG( _T("TODO") );
1412 bool wxVariant::operator!=(const wxArrayString
& value
) const
1414 return !(*this == value
);
1417 void wxVariant::operator=(const wxArrayString
& value
)
1419 if (GetType() == wxT("arrstring") &&
1420 m_data
->GetRefCount() == 1)
1422 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1427 m_data
= new wxVariantDataArrayString(value
);
1431 wxArrayString
wxVariant::GetArrayString() const
1433 if ( GetType() == wxT("arrstring") )
1434 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1436 return wxArrayString();
1439 // ----------------------------------------------------------------------------
1440 // wxVariantDataList
1441 // ----------------------------------------------------------------------------
1443 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1446 wxVariantDataList() {}
1447 wxVariantDataList(const wxVariantList
& list
);
1448 virtual ~wxVariantDataList();
1450 wxVariantList
& GetValue() { return m_value
; }
1451 void SetValue(const wxVariantList
& value
) ;
1453 virtual bool Eq(wxVariantData
& data
) const;
1454 #if wxUSE_STD_IOSTREAM
1455 virtual bool Write(wxSTD ostream
& str
) const;
1457 virtual bool Write(wxString
& str
) const;
1458 #if wxUSE_STD_IOSTREAM
1459 virtual bool Read(wxSTD istream
& str
);
1461 virtual bool Read(wxString
& str
);
1462 virtual wxString
GetType() const { return wxT("list"); }
1466 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1468 wxVariantList m_value
;
1471 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
1476 wxVariantDataList::~wxVariantDataList()
1481 void wxVariantDataList::SetValue(const wxVariantList
& value
)
1484 wxVariantList::compatibility_iterator node
= value
.GetFirst();
1487 wxVariant
* var
= node
->GetData();
1488 m_value
.Append(new wxVariant(*var
));
1489 node
= node
->GetNext();
1493 void wxVariantDataList::Clear()
1495 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1498 wxVariant
* var
= node
->GetData();
1500 node
= node
->GetNext();
1505 bool wxVariantDataList::Eq(wxVariantData
& data
) const
1507 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
1509 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
1510 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
1511 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
1512 while (node1
&& node2
)
1514 wxVariant
* var1
= node1
->GetData();
1515 wxVariant
* var2
= node2
->GetData();
1516 if ((*var1
) != (*var2
))
1518 node1
= node1
->GetNext();
1519 node2
= node2
->GetNext();
1521 if (node1
|| node2
) return false;
1525 #if wxUSE_STD_IOSTREAM
1526 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
1530 str
<< (const char*) s
.mb_str();
1535 bool wxVariantDataList::Write(wxString
& str
) const
1537 str
= wxEmptyString
;
1538 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1541 wxVariant
* var
= node
->GetData();
1542 if (node
!= m_value
.GetFirst())
1545 str
+= var
->MakeString();
1546 node
= node
->GetNext();
1552 #if wxUSE_STD_IOSTREAM
1553 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
1555 wxFAIL_MSG(wxT("Unimplemented"));
1561 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
1563 wxFAIL_MSG(wxT("Unimplemented"));
1570 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
1572 m_data
= new wxVariantDataList(val
);
1576 bool wxVariant::operator== (const wxVariantList
& value
) const
1578 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1580 wxVariantDataList
other(value
);
1581 return (GetData()->Eq(other
));
1584 bool wxVariant::operator!= (const wxVariantList
& value
) const
1586 return (!((*this) == value
));
1589 void wxVariant::operator= (const wxVariantList
& value
)
1591 if (GetType() == wxT("list") &&
1592 m_data
->GetRefCount() == 1)
1594 ((wxVariantDataList
*)GetData())->SetValue(value
);
1599 m_data
= new wxVariantDataList(value
);
1603 wxVariantList
& wxVariant::GetList() const
1605 wxASSERT( (GetType() == wxT("list")) );
1607 return (wxVariantList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1611 void wxVariant::NullList()
1613 SetData(new wxVariantDataList());
1617 void wxVariant::Append(const wxVariant
& value
)
1619 wxVariantList
& list
= GetList();
1621 list
.Append(new wxVariant(value
));
1624 // Insert at front of list
1625 void wxVariant::Insert(const wxVariant
& value
)
1627 wxVariantList
& list
= GetList();
1629 list
.Insert(new wxVariant(value
));
1632 // Returns true if the variant is a member of the list
1633 bool wxVariant::Member(const wxVariant
& value
) const
1635 wxVariantList
& list
= GetList();
1637 wxVariantList::compatibility_iterator node
= list
.GetFirst();
1640 wxVariant
* other
= node
->GetData();
1641 if (value
== *other
)
1643 node
= node
->GetNext();
1648 // Deletes the nth element of the list
1649 bool wxVariant::Delete(size_t item
)
1651 wxVariantList
& list
= GetList();
1653 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
1654 wxVariantList::compatibility_iterator node
= list
.Item(item
);
1655 wxVariant
* variant
= node
->GetData();
1662 void wxVariant::ClearList()
1664 if (!IsNull() && (GetType() == wxT("list")))
1666 ((wxVariantDataList
*) m_data
)->Clear();
1670 if (!GetType().IsSameAs(wxT("list")))
1673 m_data
= new wxVariantDataList
;
1677 // Treat a list variant as an array
1678 wxVariant
wxVariant::operator[] (size_t idx
) const
1680 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
1682 if (GetType() == wxT("list"))
1684 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1685 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1686 return *(data
->GetValue().Item(idx
)->GetData());
1688 return wxNullVariant
;
1691 wxVariant
& wxVariant::operator[] (size_t idx
)
1693 // We can't return a reference to a variant for a string list, since the string
1694 // is actually stored as a char*, not a variant.
1696 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1698 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1699 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1701 return * (data
->GetValue().Item(idx
)->GetData());
1704 // Return the number of elements in a list
1705 size_t wxVariant::GetCount() const
1707 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
1709 if (GetType() == wxT("list"))
1711 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1712 return data
->GetValue().GetCount();
1717 // ----------------------------------------------------------------------------
1719 // ----------------------------------------------------------------------------
1721 bool wxVariant::Convert(long* value
) const
1723 wxString
type(GetType());
1724 if (type
== wxT("double"))
1725 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
1726 else if (type
== wxT("long"))
1727 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1729 else if (type
== wxT("bool"))
1730 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1732 else if (type
== wxT("string"))
1733 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
1740 bool wxVariant::Convert(bool* value
) const
1742 wxString
type(GetType());
1743 if (type
== wxT("double"))
1744 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
1745 else if (type
== wxT("long"))
1746 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1748 else if (type
== wxT("bool"))
1749 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1751 else if (type
== wxT("string"))
1753 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1755 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
1757 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
1768 bool wxVariant::Convert(double* value
) const
1770 wxString
type(GetType());
1771 if (type
== wxT("double"))
1772 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
1773 else if (type
== wxT("long"))
1774 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1776 else if (type
== wxT("bool"))
1777 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1779 else if (type
== wxT("string"))
1780 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
1787 bool wxVariant::Convert(wxUniChar
* value
) const
1789 wxString
type(GetType());
1790 if (type
== wxT("char"))
1791 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1792 else if (type
== wxT("long"))
1793 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1795 else if (type
== wxT("bool"))
1796 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1804 bool wxVariant::Convert(char* value
) const
1807 if ( !Convert(&ch
) )
1813 bool wxVariant::Convert(wchar_t* value
) const
1816 if ( !Convert(&ch
) )
1822 bool wxVariant::Convert(wxString
* value
) const
1824 *value
= MakeString();
1829 bool wxVariant::Convert(wxDateTime
* value
) const
1831 wxString
type(GetType());
1832 if (type
== wxT("datetime"))
1834 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
1837 // Fallback to string conversion
1839 return Convert(&val
) &&
1840 (value
->ParseDateTime(val
.c_str()/*FIXME-UTF8*/) ||
1841 value
->ParseDate(val
.c_str()/*FIXME-UTF8*/) ||
1842 value
->ParseTime(val
.c_str()/*FIXME-UTF8*/));
1844 #endif // wxUSE_DATETIME
1846 #endif // wxUSE_VARIANT