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 wxVariantDataDateTime(const TIME_STRUCT
* valptr
)
1204 { m_value
= wxDateTime(valptr
->hour
, valptr
->minute
, valptr
->second
); }
1205 wxVariantDataDateTime(const DATE_STRUCT
* valptr
)
1206 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1),valptr
->year
); }
1207 wxVariantDataDateTime(const TIMESTAMP_STRUCT
* valptr
)
1208 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1), valptr
->year
,
1209 valptr
->hour
, valptr
->minute
, valptr
->second
, (wxDateTime::wxDateTime_t
)valptr
->fraction
); }
1212 inline wxDateTime
GetValue() const { return m_value
; }
1213 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1215 virtual bool Eq(wxVariantData
& data
) const;
1216 #if wxUSE_STD_IOSTREAM
1217 virtual bool Write(wxSTD ostream
& str
) const;
1219 virtual bool Write(wxString
& str
) const;
1220 #if wxUSE_STD_IOSTREAM
1221 virtual bool Read(wxSTD istream
& str
);
1223 virtual bool Read(wxString
& str
);
1224 virtual wxString
GetType() const { return wxT("datetime"); }
1225 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1232 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1234 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1236 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1238 return (otherData
.m_value
== m_value
);
1242 #if wxUSE_STD_IOSTREAM
1243 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1247 str
<< value
.c_str();
1253 bool wxVariantDataDateTime::Write(wxString
& str
) const
1255 str
= m_value
.Format();
1260 #if wxUSE_STD_IOSTREAM
1261 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1269 bool wxVariantDataDateTime::Read(wxString
& str
)
1271 if(! m_value
.ParseDateTime(str
.c_str()/*FIXME-UTF8*/))
1278 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1280 m_data
= new wxVariantDataDateTime(val
);
1285 wxVariant::wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
) // Date
1287 m_data
= new wxVariantDataDateTime(valptr
);
1291 wxVariant::wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
) // Date
1293 m_data
= new wxVariantDataDateTime(valptr
);
1297 wxVariant::wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
) // Date
1299 m_data
= new wxVariantDataDateTime(valptr
);
1302 #endif // wxUSE_ODBC
1304 bool wxVariant::operator== (const wxDateTime
& value
) const
1306 wxDateTime thisValue
;
1307 if (!Convert(&thisValue
))
1310 return value
.IsEqualTo(thisValue
);
1313 bool wxVariant::operator!= (const wxDateTime
& value
) const
1315 return (!((*this) == value
));
1318 void wxVariant::operator= (const wxDateTime
& value
)
1320 if (GetType() == wxT("datetime") &&
1321 m_data
->GetRefCount() == 1)
1323 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1328 m_data
= new wxVariantDataDateTime(value
);
1333 void wxVariant::operator= (const DATE_STRUCT
* value
)
1336 m_data
= new wxVariantDataDateTime(value
);
1339 void wxVariant::operator= (const TIME_STRUCT
* value
)
1342 m_data
= new wxVariantDataDateTime(value
);
1345 void wxVariant::operator= (const TIMESTAMP_STRUCT
* value
)
1348 m_data
= new wxVariantDataDateTime(value
);
1351 #endif // wxUSE_ODBC
1353 wxDateTime
wxVariant::GetDateTime() const
1356 if (!Convert(& value
))
1358 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1364 #endif // wxUSE_DATETIME
1366 // ----------------------------------------------------------------------------
1367 // wxVariantDataArrayString
1368 // ----------------------------------------------------------------------------
1370 class wxVariantDataArrayString
: public wxVariantData
1373 wxVariantDataArrayString() { }
1374 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1376 wxArrayString
GetValue() const { return m_value
; }
1377 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1379 virtual bool Eq(wxVariantData
& data
) const;
1380 #if wxUSE_STD_IOSTREAM
1381 virtual bool Write(wxSTD ostream
& str
) const;
1383 virtual bool Write(wxString
& str
) const;
1384 #if wxUSE_STD_IOSTREAM
1385 virtual bool Read(wxSTD istream
& str
);
1387 virtual bool Read(wxString
& str
);
1388 virtual wxString
GetType() const { return wxT("arrstring"); }
1389 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1392 wxArrayString m_value
;
1395 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1397 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1399 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1401 return otherData
.m_value
== m_value
;
1404 #if wxUSE_STD_IOSTREAM
1405 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1412 bool wxVariantDataArrayString::Write(wxString
& str
) const
1414 size_t count
= m_value
.GetCount();
1415 for ( size_t n
= 0; n
< count
; n
++ )
1427 #if wxUSE_STD_IOSTREAM
1428 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1436 bool wxVariantDataArrayString::Read(wxString
& str
)
1438 wxStringTokenizer
tk(str
, _T(";"));
1439 while ( tk
.HasMoreTokens() )
1441 m_value
.Add(tk
.GetNextToken());
1449 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1451 m_data
= new wxVariantDataArrayString(val
);
1455 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1457 wxFAIL_MSG( _T("TODO") );
1462 bool wxVariant::operator!=(const wxArrayString
& value
) const
1464 return !(*this == value
);
1467 void wxVariant::operator=(const wxArrayString
& value
)
1469 if (GetType() == wxT("arrstring") &&
1470 m_data
->GetRefCount() == 1)
1472 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1477 m_data
= new wxVariantDataArrayString(value
);
1481 wxArrayString
wxVariant::GetArrayString() const
1483 if ( GetType() == wxT("arrstring") )
1484 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1486 return wxArrayString();
1489 // ----------------------------------------------------------------------------
1490 // wxVariantDataList
1491 // ----------------------------------------------------------------------------
1493 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1496 wxVariantDataList() {}
1497 wxVariantDataList(const wxVariantList
& list
);
1498 virtual ~wxVariantDataList();
1500 wxVariantList
& GetValue() { return m_value
; }
1501 void SetValue(const wxVariantList
& value
) ;
1503 virtual bool Eq(wxVariantData
& data
) const;
1504 #if wxUSE_STD_IOSTREAM
1505 virtual bool Write(wxSTD ostream
& str
) const;
1507 virtual bool Write(wxString
& str
) const;
1508 #if wxUSE_STD_IOSTREAM
1509 virtual bool Read(wxSTD istream
& str
);
1511 virtual bool Read(wxString
& str
);
1512 virtual wxString
GetType() const { return wxT("list"); }
1516 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1518 wxVariantList m_value
;
1521 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
1526 wxVariantDataList::~wxVariantDataList()
1531 void wxVariantDataList::SetValue(const wxVariantList
& value
)
1534 wxVariantList::compatibility_iterator node
= value
.GetFirst();
1537 wxVariant
* var
= node
->GetData();
1538 m_value
.Append(new wxVariant(*var
));
1539 node
= node
->GetNext();
1543 void wxVariantDataList::Clear()
1545 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1548 wxVariant
* var
= node
->GetData();
1550 node
= node
->GetNext();
1555 bool wxVariantDataList::Eq(wxVariantData
& data
) const
1557 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
1559 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
1560 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
1561 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
1562 while (node1
&& node2
)
1564 wxVariant
* var1
= node1
->GetData();
1565 wxVariant
* var2
= node2
->GetData();
1566 if ((*var1
) != (*var2
))
1568 node1
= node1
->GetNext();
1569 node2
= node2
->GetNext();
1571 if (node1
|| node2
) return false;
1575 #if wxUSE_STD_IOSTREAM
1576 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
1580 str
<< (const char*) s
.mb_str();
1585 bool wxVariantDataList::Write(wxString
& str
) const
1587 str
= wxEmptyString
;
1588 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1591 wxVariant
* var
= node
->GetData();
1592 if (node
!= m_value
.GetFirst())
1595 str
+= var
->MakeString();
1596 node
= node
->GetNext();
1602 #if wxUSE_STD_IOSTREAM
1603 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
1605 wxFAIL_MSG(wxT("Unimplemented"));
1611 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
1613 wxFAIL_MSG(wxT("Unimplemented"));
1620 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
1622 m_data
= new wxVariantDataList(val
);
1626 bool wxVariant::operator== (const wxVariantList
& value
) const
1628 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1630 wxVariantDataList
other(value
);
1631 return (GetData()->Eq(other
));
1634 bool wxVariant::operator!= (const wxVariantList
& value
) const
1636 return (!((*this) == value
));
1639 void wxVariant::operator= (const wxVariantList
& value
)
1641 if (GetType() == wxT("list") &&
1642 m_data
->GetRefCount() == 1)
1644 ((wxVariantDataList
*)GetData())->SetValue(value
);
1649 m_data
= new wxVariantDataList(value
);
1653 wxVariantList
& wxVariant::GetList() const
1655 wxASSERT( (GetType() == wxT("list")) );
1657 return (wxVariantList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1661 void wxVariant::NullList()
1663 SetData(new wxVariantDataList());
1667 void wxVariant::Append(const wxVariant
& value
)
1669 wxVariantList
& list
= GetList();
1671 list
.Append(new wxVariant(value
));
1674 // Insert at front of list
1675 void wxVariant::Insert(const wxVariant
& value
)
1677 wxVariantList
& list
= GetList();
1679 list
.Insert(new wxVariant(value
));
1682 // Returns true if the variant is a member of the list
1683 bool wxVariant::Member(const wxVariant
& value
) const
1685 wxVariantList
& list
= GetList();
1687 wxVariantList::compatibility_iterator node
= list
.GetFirst();
1690 wxVariant
* other
= node
->GetData();
1691 if (value
== *other
)
1693 node
= node
->GetNext();
1698 // Deletes the nth element of the list
1699 bool wxVariant::Delete(size_t item
)
1701 wxVariantList
& list
= GetList();
1703 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
1704 wxVariantList::compatibility_iterator node
= list
.Item(item
);
1705 wxVariant
* variant
= node
->GetData();
1712 void wxVariant::ClearList()
1714 if (!IsNull() && (GetType() == wxT("list")))
1716 ((wxVariantDataList
*) m_data
)->Clear();
1720 if (!GetType().IsSameAs(wxT("list")))
1723 m_data
= new wxVariantDataList
;
1727 // Treat a list variant as an array
1728 wxVariant
wxVariant::operator[] (size_t idx
) const
1730 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
1732 if (GetType() == wxT("list"))
1734 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1735 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1736 return *(data
->GetValue().Item(idx
)->GetData());
1738 return wxNullVariant
;
1741 wxVariant
& wxVariant::operator[] (size_t idx
)
1743 // We can't return a reference to a variant for a string list, since the string
1744 // is actually stored as a char*, not a variant.
1746 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1748 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1749 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1751 return * (data
->GetValue().Item(idx
)->GetData());
1754 // Return the number of elements in a list
1755 size_t wxVariant::GetCount() const
1757 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
1759 if (GetType() == wxT("list"))
1761 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1762 return data
->GetValue().GetCount();
1767 // ----------------------------------------------------------------------------
1769 // ----------------------------------------------------------------------------
1771 bool wxVariant::Convert(long* value
) const
1773 wxString
type(GetType());
1774 if (type
== wxT("double"))
1775 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
1776 else if (type
== wxT("long"))
1777 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1779 else if (type
== wxT("bool"))
1780 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1782 else if (type
== wxT("string"))
1783 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
1790 bool wxVariant::Convert(bool* value
) const
1792 wxString
type(GetType());
1793 if (type
== wxT("double"))
1794 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
1795 else if (type
== wxT("long"))
1796 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1798 else if (type
== wxT("bool"))
1799 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1801 else if (type
== wxT("string"))
1803 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1805 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
1807 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
1818 bool wxVariant::Convert(double* value
) const
1820 wxString
type(GetType());
1821 if (type
== wxT("double"))
1822 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
1823 else if (type
== wxT("long"))
1824 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1826 else if (type
== wxT("bool"))
1827 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1829 else if (type
== wxT("string"))
1830 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
1837 bool wxVariant::Convert(wxUniChar
* value
) const
1839 wxString
type(GetType());
1840 if (type
== wxT("char"))
1841 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1842 else if (type
== wxT("long"))
1843 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1845 else if (type
== wxT("bool"))
1846 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1854 bool wxVariant::Convert(char* value
) const
1857 if ( !Convert(&ch
) )
1863 bool wxVariant::Convert(wchar_t* value
) const
1866 if ( !Convert(&ch
) )
1872 bool wxVariant::Convert(wxString
* value
) const
1874 *value
= MakeString();
1879 bool wxVariant::Convert(wxDateTime
* value
) const
1881 wxString
type(GetType());
1882 if (type
== wxT("datetime"))
1884 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
1887 // Fallback to string conversion
1889 return Convert(&val
) &&
1890 (value
->ParseDateTime(val
.c_str()/*FIXME-UTF8*/) ||
1891 value
->ParseDate(val
.c_str()/*FIXME-UTF8*/) ||
1892 value
->ParseTime(val
.c_str()/*FIXME-UTF8*/));
1894 #endif // wxUSE_DATETIME
1896 #endif // wxUSE_VARIANT