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()
69 bool wxVariant::IsNull() const
71 return (m_data
== NULL
);
74 void wxVariant::MakeNull()
79 void wxVariant::Clear()
81 m_name
= wxEmptyString
;
84 wxVariant::wxVariant(const wxVariant
& variant
)
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 if (GetType() != variant
.GetType())
128 return (GetData()->Eq(* variant
.GetData()));
131 bool wxVariant::operator!= (const wxVariant
& variant
) const
133 return (!(*this == variant
));
136 wxString
wxVariant::MakeString() const
141 if (GetData()->Write(str
))
144 return wxEmptyString
;
147 void wxVariant::SetData(wxVariantData
* data
)
153 void wxVariant::Ref(const wxVariant
& clone
)
155 // nothing to be done
156 if (m_data
== clone
.m_data
)
159 // delete reference to old data
162 // reference new data
165 m_data
= clone
.m_data
;
171 void wxVariant::UnRef()
175 wxASSERT_MSG( m_data
->m_count
> 0, _T("invalid ref data count") );
182 bool wxVariant::Unshare()
184 if ( m_data
&& m_data
->GetRefCount() > 1 )
186 // note that ref is not going to be destroyed in this case...
187 const wxVariantData
* ref
= m_data
;
190 // ... so we can still access it
191 m_data
= ref
->Clone();
193 wxASSERT_MSG( (m_data
&& m_data
->GetRefCount() == 1),
194 _T("wxVariant::AllocExclusive() failed.") );
196 if (!m_data
|| m_data
->GetRefCount() != 1)
201 //else: data is null or ref count is 1, so we are exclusive owners of m_refData anyhow
207 // Returns a string representing the type of the variant,
208 // e.g. "string", "bool", "list", "double", "long"
209 wxString
wxVariant::GetType() const
212 return wxString(wxT("null"));
214 return GetData()->GetType();
218 bool wxVariant::IsType(const wxString
& type
) const
220 return (GetType() == type
);
223 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
225 wxClassInfo
* info
=GetData()->GetValueClassInfo();
226 return info
? info
->IsKindOf(type
) : false ;
230 // -----------------------------------------------------------------
232 // -----------------------------------------------------------------
234 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
237 wxVariantDataLong() { m_value
= 0; }
238 wxVariantDataLong(long value
) { m_value
= value
; }
240 inline long GetValue() const { return m_value
; }
241 inline void SetValue(long value
) { m_value
= value
; }
243 virtual bool Eq(wxVariantData
& data
) const;
245 virtual bool Read(wxString
& str
);
246 virtual bool Write(wxString
& str
) const;
247 #if wxUSE_STD_IOSTREAM
248 virtual bool Read(wxSTD istream
& str
);
249 virtual bool Write(wxSTD ostream
& str
) const;
252 virtual bool Read(wxInputStream
& str
);
253 virtual bool Write(wxOutputStream
&str
) const;
254 #endif // wxUSE_STREAMS
256 wxVariantData
* Clone() const { return new wxVariantDataLong(m_value
); }
258 virtual wxString
GetType() const { return wxT("long"); }
264 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
266 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
268 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
270 return (otherData
.m_value
== m_value
);
273 #if wxUSE_STD_IOSTREAM
274 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
278 str
<< (const char*) s
.mb_str();
283 bool wxVariantDataLong::Write(wxString
& str
) const
285 str
.Printf(wxT("%ld"), m_value
);
289 #if wxUSE_STD_IOSTREAM
290 bool wxVariantDataLong::Read(wxSTD istream
& str
)
298 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
300 wxTextOutputStream
s(str
);
302 s
.Write32((size_t)m_value
);
306 bool wxVariantDataLong::Read(wxInputStream
& str
)
308 wxTextInputStream
s(str
);
309 m_value
= s
.Read32();
312 #endif // wxUSE_STREAMS
314 bool wxVariantDataLong::Read(wxString
& str
)
316 m_value
= wxAtol(str
);
322 wxVariant::wxVariant(long val
, const wxString
& name
)
324 m_data
= new wxVariantDataLong(val
);
328 wxVariant::wxVariant(int val
, const wxString
& name
)
330 m_data
= new wxVariantDataLong((long)val
);
334 wxVariant::wxVariant(short val
, const wxString
& name
)
336 m_data
= new wxVariantDataLong((long)val
);
340 bool wxVariant::operator== (long value
) const
343 if (!Convert(&thisValue
))
346 return (value
== thisValue
);
349 bool wxVariant::operator!= (long value
) const
351 return (!((*this) == value
));
354 void wxVariant::operator= (long value
)
356 if (GetType() == wxT("long") &&
357 m_data
->GetRefCount() == 1)
359 ((wxVariantDataLong
*)GetData())->SetValue(value
);
364 m_data
= new wxVariantDataLong(value
);
368 long wxVariant::GetLong() const
371 if (Convert(& value
))
375 wxFAIL_MSG(wxT("Could not convert to a long"));
380 // -----------------------------------------------------------------
381 // wxVariantDoubleData
382 // -----------------------------------------------------------------
384 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
387 wxVariantDoubleData() { m_value
= 0.0; }
388 wxVariantDoubleData(double value
) { m_value
= value
; }
390 inline double GetValue() const { return m_value
; }
391 inline void SetValue(double value
) { m_value
= value
; }
393 virtual bool Eq(wxVariantData
& data
) const;
394 virtual bool Read(wxString
& str
);
395 #if wxUSE_STD_IOSTREAM
396 virtual bool Write(wxSTD ostream
& str
) const;
398 virtual bool Write(wxString
& str
) const;
399 #if wxUSE_STD_IOSTREAM
400 virtual bool Read(wxSTD istream
& str
);
403 virtual bool Read(wxInputStream
& str
);
404 virtual bool Write(wxOutputStream
&str
) const;
405 #endif // wxUSE_STREAMS
406 virtual wxString
GetType() const { return wxT("double"); }
408 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
413 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
415 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
417 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
419 return wxIsSameDouble(otherData
.m_value
, m_value
);
422 #if wxUSE_STD_IOSTREAM
423 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
427 str
<< (const char*) s
.mb_str();
432 bool wxVariantDoubleData::Write(wxString
& str
) const
434 str
.Printf(wxT("%.14g"), m_value
);
438 #if wxUSE_STD_IOSTREAM
439 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
447 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
449 wxTextOutputStream
s(str
);
450 s
.WriteDouble((double)m_value
);
454 bool wxVariantDoubleData::Read(wxInputStream
& str
)
456 wxTextInputStream
s(str
);
457 m_value
= (float)s
.ReadDouble();
460 #endif // wxUSE_STREAMS
462 bool wxVariantDoubleData::Read(wxString
& str
)
464 m_value
= wxAtof(str
);
468 // wxVariant double code
470 wxVariant::wxVariant(double val
, const wxString
& name
)
472 m_data
= new wxVariantDoubleData(val
);
476 bool wxVariant::operator== (double value
) const
479 if (!Convert(&thisValue
))
482 return wxIsSameDouble(value
, thisValue
);
485 bool wxVariant::operator!= (double value
) const
487 return (!((*this) == value
));
490 void wxVariant::operator= (double value
)
492 if (GetType() == wxT("double") &&
493 m_data
->GetRefCount() == 1)
495 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
500 m_data
= new wxVariantDoubleData(value
);
504 double wxVariant::GetDouble() const
507 if (Convert(& value
))
511 wxFAIL_MSG(wxT("Could not convert to a double number"));
516 // -----------------------------------------------------------------
518 // -----------------------------------------------------------------
520 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
523 wxVariantDataBool() { m_value
= 0; }
524 wxVariantDataBool(bool value
) { m_value
= value
; }
526 inline bool GetValue() const { return m_value
; }
527 inline void SetValue(bool value
) { m_value
= value
; }
529 virtual bool Eq(wxVariantData
& data
) const;
530 #if wxUSE_STD_IOSTREAM
531 virtual bool Write(wxSTD ostream
& str
) const;
533 virtual bool Write(wxString
& str
) const;
534 virtual bool Read(wxString
& str
);
535 #if wxUSE_STD_IOSTREAM
536 virtual bool Read(wxSTD istream
& str
);
539 virtual bool Read(wxInputStream
& str
);
540 virtual bool Write(wxOutputStream
& str
) const;
541 #endif // wxUSE_STREAMS
542 virtual wxString
GetType() const { return wxT("bool"); }
544 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
549 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
551 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
553 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
555 return (otherData
.m_value
== m_value
);
558 #if wxUSE_STD_IOSTREAM
559 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
563 str
<< (const char*) s
.mb_str();
568 bool wxVariantDataBool::Write(wxString
& str
) const
570 str
.Printf(wxT("%d"), (int) m_value
);
574 #if wxUSE_STD_IOSTREAM
575 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
577 wxFAIL_MSG(wxT("Unimplemented"));
578 // str >> (long) m_value;
584 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
586 wxTextOutputStream
s(str
);
592 bool wxVariantDataBool::Read(wxInputStream
& str
)
594 wxTextInputStream
s(str
);
596 m_value
= s
.Read8() != 0;
599 #endif // wxUSE_STREAMS
601 bool wxVariantDataBool::Read(wxString
& str
)
603 m_value
= (wxAtol(str
) != 0);
609 wxVariant::wxVariant(bool val
, const wxString
& name
)
611 m_data
= new wxVariantDataBool(val
);
615 bool wxVariant::operator== (bool value
) const
618 if (!Convert(&thisValue
))
621 return (value
== thisValue
);
624 bool wxVariant::operator!= (bool value
) const
626 return (!((*this) == value
));
629 void wxVariant::operator= (bool value
)
631 if (GetType() == wxT("bool") &&
632 m_data
->GetRefCount() == 1)
634 ((wxVariantDataBool
*)GetData())->SetValue(value
);
639 m_data
= new wxVariantDataBool(value
);
643 bool wxVariant::GetBool() const
646 if (Convert(& value
))
650 wxFAIL_MSG(wxT("Could not convert to a bool"));
655 // -----------------------------------------------------------------
657 // -----------------------------------------------------------------
659 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
662 wxVariantDataChar() { m_value
= 0; }
663 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
665 inline wxUniChar
GetValue() const { return m_value
; }
666 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
668 virtual bool Eq(wxVariantData
& data
) const;
669 #if wxUSE_STD_IOSTREAM
670 virtual bool Read(wxSTD istream
& str
);
671 virtual bool Write(wxSTD ostream
& str
) const;
673 virtual bool Read(wxString
& str
);
674 virtual bool Write(wxString
& str
) const;
676 virtual bool Read(wxInputStream
& str
);
677 virtual bool Write(wxOutputStream
& str
) const;
678 #endif // wxUSE_STREAMS
679 virtual wxString
GetType() const { return wxT("char"); }
680 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
686 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
688 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
690 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
692 return (otherData
.m_value
== m_value
);
695 #if wxUSE_STD_IOSTREAM
696 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
698 str
<< wxString(m_value
);
703 bool wxVariantDataChar::Write(wxString
& str
) const
709 #if wxUSE_STD_IOSTREAM
710 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
712 wxFAIL_MSG(wxT("Unimplemented"));
719 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
721 wxTextOutputStream
s(str
);
723 // FIXME-UTF8: this should be just "s << m_value;" after removal of
724 // ANSI build and addition of wxUniChar to wxTextOutputStream:
725 s
<< (wxChar
)m_value
;
730 bool wxVariantDataChar::Read(wxInputStream
& str
)
732 wxTextInputStream
s(str
);
734 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
735 // ANSI build and addition of wxUniChar to wxTextInputStream:
742 #endif // wxUSE_STREAMS
744 bool wxVariantDataChar::Read(wxString
& str
)
750 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
752 m_data
= new wxVariantDataChar(val
);
756 wxVariant::wxVariant(char val
, const wxString
& name
)
758 m_data
= new wxVariantDataChar(val
);
762 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
764 m_data
= new wxVariantDataChar(val
);
768 bool wxVariant::operator==(const wxUniChar
& value
) const
771 if (!Convert(&thisValue
))
774 return (value
== thisValue
);
777 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
779 if (GetType() == wxT("char") &&
780 m_data
->GetRefCount() == 1)
782 ((wxVariantDataChar
*)GetData())->SetValue(value
);
787 m_data
= new wxVariantDataChar(value
);
793 wxUniChar
wxVariant::GetChar() const
796 if (Convert(& value
))
800 wxFAIL_MSG(wxT("Could not convert to a char"));
805 // ----------------------------------------------------------------------------
806 // wxVariantDataString
807 // ----------------------------------------------------------------------------
809 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
812 wxVariantDataString() { }
813 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
815 inline wxString
GetValue() const { return m_value
; }
816 inline void SetValue(const wxString
& value
) { m_value
= value
; }
818 virtual bool Eq(wxVariantData
& data
) const;
819 #if wxUSE_STD_IOSTREAM
820 virtual bool Write(wxSTD ostream
& str
) const;
822 virtual bool Read(wxString
& str
);
823 virtual bool Write(wxString
& str
) const;
824 #if wxUSE_STD_IOSTREAM
825 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
828 virtual bool Read(wxInputStream
& str
);
829 virtual bool Write(wxOutputStream
& str
) const;
830 #endif // wxUSE_STREAMS
831 virtual wxString
GetType() const { return wxT("string"); }
832 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
838 bool wxVariantDataString::Eq(wxVariantData
& data
) const
840 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
842 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
844 return (otherData
.m_value
== m_value
);
847 #if wxUSE_STD_IOSTREAM
848 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
850 str
<< (const char*) m_value
.mb_str();
855 bool wxVariantDataString::Write(wxString
& str
) const
862 bool wxVariantDataString::Write(wxOutputStream
& str
) const
864 // why doesn't wxOutputStream::operator<< take "const wxString&"
865 wxTextOutputStream
s(str
);
866 s
.WriteString(m_value
);
870 bool wxVariantDataString::Read(wxInputStream
& str
)
872 wxTextInputStream
s(str
);
874 m_value
= s
.ReadLine();
877 #endif // wxUSE_STREAMS
879 bool wxVariantDataString::Read(wxString
& str
)
887 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
889 m_data
= new wxVariantDataString(val
);
893 wxVariant::wxVariant(const char* val
, const wxString
& name
)
895 m_data
= new wxVariantDataString(wxString(val
));
899 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
901 m_data
= new wxVariantDataString(wxString(val
));
905 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
907 m_data
= new wxVariantDataString(val
.AsString());
911 wxVariant::wxVariant(const wxCharBuffer
& val
, const wxString
& name
)
913 m_data
= new wxVariantDataString(wxString(val
));
917 wxVariant::wxVariant(const wxWCharBuffer
& val
, const wxString
& name
)
919 m_data
= new wxVariantDataString(wxString(val
));
923 bool wxVariant::operator== (const wxString
& value
) const
926 if (!Convert(&thisValue
))
929 return value
== thisValue
;
932 bool wxVariant::operator!= (const wxString
& value
) const
934 return (!((*this) == value
));
937 wxVariant
& wxVariant::operator= (const wxString
& value
)
939 if (GetType() == wxT("string") &&
940 m_data
->GetRefCount() == 1)
942 ((wxVariantDataString
*)GetData())->SetValue(value
);
947 m_data
= new wxVariantDataString(value
);
952 wxString
wxVariant::GetString() const
955 if (!Convert(& value
))
957 wxFAIL_MSG(wxT("Could not convert to a string"));
963 // ----------------------------------------------------------------------------
964 // wxVariantDataWxObjectPtr
965 // ----------------------------------------------------------------------------
967 class wxVariantDataWxObjectPtr
: public wxVariantData
970 wxVariantDataWxObjectPtr() { }
971 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
973 inline wxObject
* GetValue() const { return m_value
; }
974 inline void SetValue(wxObject
* value
) { m_value
= value
; }
976 virtual bool Eq(wxVariantData
& data
) const;
977 #if wxUSE_STD_IOSTREAM
978 virtual bool Write(wxSTD ostream
& str
) const;
980 virtual bool Write(wxString
& str
) const;
981 #if wxUSE_STD_IOSTREAM
982 virtual bool Read(wxSTD istream
& str
);
984 virtual bool Read(wxString
& str
);
985 virtual wxString
GetType() const ;
986 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
988 virtual wxClassInfo
* GetValueClassInfo();
994 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
996 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
998 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1000 return (otherData
.m_value
== m_value
);
1003 wxString
wxVariantDataWxObjectPtr::GetType() const
1005 wxString
returnVal(wxT("wxObject*"));
1009 returnVal
= m_value
->GetClassInfo()->GetClassName();
1010 returnVal
+= wxT("*");
1016 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1018 wxClassInfo
* returnVal
=NULL
;
1020 if (m_value
) returnVal
= m_value
->GetClassInfo();
1025 #if wxUSE_STD_IOSTREAM
1026 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1030 str
<< (const char*) s
.mb_str();
1035 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1037 str
.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value
));
1041 #if wxUSE_STD_IOSTREAM
1042 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1049 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1057 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1059 m_data
= new wxVariantDataWxObjectPtr(val
);
1063 bool wxVariant::operator== (wxObject
* value
) const
1065 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1068 bool wxVariant::operator!= (wxObject
* value
) const
1070 return (!((*this) == (wxObject
*) value
));
1073 void wxVariant::operator= (wxObject
* value
)
1076 m_data
= new wxVariantDataWxObjectPtr(value
);
1079 wxObject
* wxVariant::GetWxObjectPtr() const
1081 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_data
)->GetValue();
1084 // ----------------------------------------------------------------------------
1085 // wxVariantDataVoidPtr
1086 // ----------------------------------------------------------------------------
1088 class wxVariantDataVoidPtr
: public wxVariantData
1091 wxVariantDataVoidPtr() { }
1092 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1094 inline void* GetValue() const { return m_value
; }
1095 inline void SetValue(void* value
) { m_value
= value
; }
1097 virtual bool Eq(wxVariantData
& data
) const;
1098 #if wxUSE_STD_IOSTREAM
1099 virtual bool Write(wxSTD ostream
& str
) const;
1101 virtual bool Write(wxString
& str
) const;
1102 #if wxUSE_STD_IOSTREAM
1103 virtual bool Read(wxSTD istream
& str
);
1105 virtual bool Read(wxString
& str
);
1106 virtual wxString
GetType() const { return wxT("void*"); }
1107 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1113 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1115 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1117 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1119 return (otherData
.m_value
== m_value
);
1122 #if wxUSE_STD_IOSTREAM
1123 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1127 str
<< (const char*) s
.mb_str();
1132 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1134 str
.Printf(wxT("%p"), m_value
);
1138 #if wxUSE_STD_IOSTREAM
1139 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1146 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1154 wxVariant::wxVariant( void* val
, const wxString
& name
)
1156 m_data
= new wxVariantDataVoidPtr(val
);
1160 bool wxVariant::operator== (void* value
) const
1162 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1165 bool wxVariant::operator!= (void* value
) const
1167 return (!((*this) == (void*) value
));
1170 void wxVariant::operator= (void* value
)
1172 if (GetType() == wxT("void*") && (m_data
->GetRefCount() == 1))
1174 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1179 m_data
= new wxVariantDataVoidPtr(value
);
1183 void* wxVariant::GetVoidPtr() const
1185 // handling this specially is convenient when working with COM, see #9873
1189 wxASSERT( GetType() == wxT("void*") );
1191 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1194 // ----------------------------------------------------------------------------
1195 // wxVariantDataDateTime
1196 // ----------------------------------------------------------------------------
1200 class wxVariantDataDateTime
: public wxVariantData
1203 wxVariantDataDateTime() { }
1204 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1206 inline wxDateTime
GetValue() const { return m_value
; }
1207 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1209 virtual bool Eq(wxVariantData
& data
) const;
1210 #if wxUSE_STD_IOSTREAM
1211 virtual bool Write(wxSTD ostream
& str
) const;
1213 virtual bool Write(wxString
& str
) const;
1214 #if wxUSE_STD_IOSTREAM
1215 virtual bool Read(wxSTD istream
& str
);
1217 virtual bool Read(wxString
& str
);
1218 virtual wxString
GetType() const { return wxT("datetime"); }
1219 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1226 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1228 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1230 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1232 return (otherData
.m_value
== m_value
);
1236 #if wxUSE_STD_IOSTREAM
1237 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1241 str
<< value
.c_str();
1247 bool wxVariantDataDateTime::Write(wxString
& str
) const
1249 if ( m_value
.IsValid() )
1250 str
= m_value
.Format();
1252 str
= wxS("Invalid");
1257 #if wxUSE_STD_IOSTREAM
1258 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1266 bool wxVariantDataDateTime::Read(wxString
& str
)
1268 if ( str
== wxS("Invalid") )
1270 m_value
= wxInvalidDateTime
;
1274 wxString::const_iterator end
;
1275 return m_value
.ParseDateTime(str
, &end
) && end
== str
.end();
1280 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1282 m_data
= new wxVariantDataDateTime(val
);
1286 bool wxVariant::operator== (const wxDateTime
& value
) const
1288 wxDateTime thisValue
;
1289 if (!Convert(&thisValue
))
1292 return value
.IsEqualTo(thisValue
);
1295 bool wxVariant::operator!= (const wxDateTime
& value
) const
1297 return (!((*this) == value
));
1300 void wxVariant::operator= (const wxDateTime
& value
)
1302 if (GetType() == wxT("datetime") &&
1303 m_data
->GetRefCount() == 1)
1305 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1310 m_data
= new wxVariantDataDateTime(value
);
1314 wxDateTime
wxVariant::GetDateTime() const
1317 if (!Convert(& value
))
1319 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1325 #endif // wxUSE_DATETIME
1327 // ----------------------------------------------------------------------------
1328 // wxVariantDataArrayString
1329 // ----------------------------------------------------------------------------
1331 class wxVariantDataArrayString
: public wxVariantData
1334 wxVariantDataArrayString() { }
1335 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1337 wxArrayString
GetValue() const { return m_value
; }
1338 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1340 virtual bool Eq(wxVariantData
& data
) const;
1341 #if wxUSE_STD_IOSTREAM
1342 virtual bool Write(wxSTD ostream
& str
) const;
1344 virtual bool Write(wxString
& str
) const;
1345 #if wxUSE_STD_IOSTREAM
1346 virtual bool Read(wxSTD istream
& str
);
1348 virtual bool Read(wxString
& str
);
1349 virtual wxString
GetType() const { return wxT("arrstring"); }
1350 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1353 wxArrayString m_value
;
1356 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1358 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1360 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1362 return otherData
.m_value
== m_value
;
1365 #if wxUSE_STD_IOSTREAM
1366 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1373 bool wxVariantDataArrayString::Write(wxString
& str
) const
1375 size_t count
= m_value
.GetCount();
1376 for ( size_t n
= 0; n
< count
; n
++ )
1388 #if wxUSE_STD_IOSTREAM
1389 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1397 bool wxVariantDataArrayString::Read(wxString
& str
)
1399 wxStringTokenizer
tk(str
, _T(";"));
1400 while ( tk
.HasMoreTokens() )
1402 m_value
.Add(tk
.GetNextToken());
1410 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1412 m_data
= new wxVariantDataArrayString(val
);
1416 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1418 wxFAIL_MSG( _T("TODO") );
1423 bool wxVariant::operator!=(const wxArrayString
& value
) const
1425 return !(*this == value
);
1428 void wxVariant::operator=(const wxArrayString
& value
)
1430 if (GetType() == wxT("arrstring") &&
1431 m_data
->GetRefCount() == 1)
1433 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1438 m_data
= new wxVariantDataArrayString(value
);
1442 wxArrayString
wxVariant::GetArrayString() const
1444 if ( GetType() == wxT("arrstring") )
1445 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1447 return wxArrayString();
1450 // ----------------------------------------------------------------------------
1451 // wxVariantDataList
1452 // ----------------------------------------------------------------------------
1454 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1457 wxVariantDataList() {}
1458 wxVariantDataList(const wxVariantList
& list
);
1459 virtual ~wxVariantDataList();
1461 wxVariantList
& GetValue() { return m_value
; }
1462 void SetValue(const wxVariantList
& value
) ;
1464 virtual bool Eq(wxVariantData
& data
) const;
1465 #if wxUSE_STD_IOSTREAM
1466 virtual bool Write(wxSTD ostream
& str
) const;
1468 virtual bool Write(wxString
& str
) const;
1469 #if wxUSE_STD_IOSTREAM
1470 virtual bool Read(wxSTD istream
& str
);
1472 virtual bool Read(wxString
& str
);
1473 virtual wxString
GetType() const { return wxT("list"); }
1477 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1479 wxVariantList m_value
;
1482 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
1487 wxVariantDataList::~wxVariantDataList()
1492 void wxVariantDataList::SetValue(const wxVariantList
& value
)
1495 wxVariantList::compatibility_iterator node
= value
.GetFirst();
1498 wxVariant
* var
= node
->GetData();
1499 m_value
.Append(new wxVariant(*var
));
1500 node
= node
->GetNext();
1504 void wxVariantDataList::Clear()
1506 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1509 wxVariant
* var
= node
->GetData();
1511 node
= node
->GetNext();
1516 bool wxVariantDataList::Eq(wxVariantData
& data
) const
1518 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
1520 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
1521 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
1522 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
1523 while (node1
&& node2
)
1525 wxVariant
* var1
= node1
->GetData();
1526 wxVariant
* var2
= node2
->GetData();
1527 if ((*var1
) != (*var2
))
1529 node1
= node1
->GetNext();
1530 node2
= node2
->GetNext();
1532 if (node1
|| node2
) return false;
1536 #if wxUSE_STD_IOSTREAM
1537 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
1541 str
<< (const char*) s
.mb_str();
1546 bool wxVariantDataList::Write(wxString
& str
) const
1548 str
= wxEmptyString
;
1549 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1552 wxVariant
* var
= node
->GetData();
1553 if (node
!= m_value
.GetFirst())
1556 str
+= var
->MakeString();
1557 node
= node
->GetNext();
1563 #if wxUSE_STD_IOSTREAM
1564 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
1566 wxFAIL_MSG(wxT("Unimplemented"));
1572 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
1574 wxFAIL_MSG(wxT("Unimplemented"));
1581 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
1583 m_data
= new wxVariantDataList(val
);
1587 bool wxVariant::operator== (const wxVariantList
& value
) const
1589 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1591 wxVariantDataList
other(value
);
1592 return (GetData()->Eq(other
));
1595 bool wxVariant::operator!= (const wxVariantList
& value
) const
1597 return (!((*this) == value
));
1600 void wxVariant::operator= (const wxVariantList
& value
)
1602 if (GetType() == wxT("list") &&
1603 m_data
->GetRefCount() == 1)
1605 ((wxVariantDataList
*)GetData())->SetValue(value
);
1610 m_data
= new wxVariantDataList(value
);
1614 wxVariantList
& wxVariant::GetList() const
1616 wxASSERT( (GetType() == wxT("list")) );
1618 return (wxVariantList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1622 void wxVariant::NullList()
1624 SetData(new wxVariantDataList());
1628 void wxVariant::Append(const wxVariant
& value
)
1630 wxVariantList
& list
= GetList();
1632 list
.Append(new wxVariant(value
));
1635 // Insert at front of list
1636 void wxVariant::Insert(const wxVariant
& value
)
1638 wxVariantList
& list
= GetList();
1640 list
.Insert(new wxVariant(value
));
1643 // Returns true if the variant is a member of the list
1644 bool wxVariant::Member(const wxVariant
& value
) const
1646 wxVariantList
& list
= GetList();
1648 wxVariantList::compatibility_iterator node
= list
.GetFirst();
1651 wxVariant
* other
= node
->GetData();
1652 if (value
== *other
)
1654 node
= node
->GetNext();
1659 // Deletes the nth element of the list
1660 bool wxVariant::Delete(size_t item
)
1662 wxVariantList
& list
= GetList();
1664 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
1665 wxVariantList::compatibility_iterator node
= list
.Item(item
);
1666 wxVariant
* variant
= node
->GetData();
1673 void wxVariant::ClearList()
1675 if (!IsNull() && (GetType() == wxT("list")))
1677 ((wxVariantDataList
*) m_data
)->Clear();
1681 if (!GetType().IsSameAs(wxT("list")))
1684 m_data
= new wxVariantDataList
;
1688 // Treat a list variant as an array
1689 wxVariant
wxVariant::operator[] (size_t idx
) const
1691 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
1693 if (GetType() == wxT("list"))
1695 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1696 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1697 return *(data
->GetValue().Item(idx
)->GetData());
1699 return wxNullVariant
;
1702 wxVariant
& wxVariant::operator[] (size_t idx
)
1704 // We can't return a reference to a variant for a string list, since the string
1705 // is actually stored as a char*, not a variant.
1707 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1709 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1710 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1712 return * (data
->GetValue().Item(idx
)->GetData());
1715 // Return the number of elements in a list
1716 size_t wxVariant::GetCount() const
1718 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
1720 if (GetType() == wxT("list"))
1722 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1723 return data
->GetValue().GetCount();
1728 // ----------------------------------------------------------------------------
1730 // ----------------------------------------------------------------------------
1732 bool wxVariant::Convert(long* value
) const
1734 wxString
type(GetType());
1735 if (type
== wxT("double"))
1736 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
1737 else if (type
== wxT("long"))
1738 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1739 else if (type
== wxT("bool"))
1740 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1741 else if (type
== wxT("string"))
1742 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
1749 bool wxVariant::Convert(bool* value
) const
1751 wxString
type(GetType());
1752 if (type
== wxT("double"))
1753 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
1754 else if (type
== wxT("long"))
1755 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1756 else if (type
== wxT("bool"))
1757 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1758 else if (type
== wxT("string"))
1760 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1762 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
1764 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
1775 bool wxVariant::Convert(double* value
) const
1777 wxString
type(GetType());
1778 if (type
== wxT("double"))
1779 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
1780 else if (type
== wxT("long"))
1781 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1782 else if (type
== wxT("bool"))
1783 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1784 else if (type
== wxT("string"))
1785 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
1792 bool wxVariant::Convert(wxUniChar
* value
) const
1794 wxString
type(GetType());
1795 if (type
== wxT("char"))
1796 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1797 else if (type
== wxT("long"))
1798 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1799 else if (type
== wxT("bool"))
1800 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1807 bool wxVariant::Convert(char* value
) const
1810 if ( !Convert(&ch
) )
1816 bool wxVariant::Convert(wchar_t* value
) const
1819 if ( !Convert(&ch
) )
1825 bool wxVariant::Convert(wxString
* value
) const
1827 *value
= MakeString();
1832 bool wxVariant::Convert(wxDateTime
* value
) const
1834 wxString
type(GetType());
1835 if (type
== wxT("datetime"))
1837 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
1841 // Fallback to string conversion
1843 if ( !Convert(&val
) )
1846 // Try to parse this as either date and time, only date or only time
1847 // checking that the entire string was parsed
1848 wxString::const_iterator end
;
1849 if ( value
->ParseDateTime(val
, &end
) && end
== val
.end() )
1852 if ( value
->ParseDate(val
, &end
) && end
== val
.end() )
1855 if ( value
->ParseTime(val
, &end
) && end
== val
.end() )
1860 #endif // wxUSE_DATETIME
1862 #endif // wxUSE_VARIANT