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 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 // -----------------------------------------------------------------
522 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
525 wxVariantDataBool() { m_value
= 0; }
526 wxVariantDataBool(bool value
) { m_value
= value
; }
528 inline bool GetValue() const { return m_value
; }
529 inline void SetValue(bool value
) { m_value
= value
; }
531 virtual bool Eq(wxVariantData
& data
) const;
532 #if wxUSE_STD_IOSTREAM
533 virtual bool Write(wxSTD ostream
& str
) const;
535 virtual bool Write(wxString
& str
) const;
536 virtual bool Read(wxString
& str
);
537 #if wxUSE_STD_IOSTREAM
538 virtual bool Read(wxSTD istream
& str
);
541 virtual bool Read(wxInputStream
& str
);
542 virtual bool Write(wxOutputStream
& str
) const;
543 #endif // wxUSE_STREAMS
544 virtual wxString
GetType() const { return wxT("bool"); }
546 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
551 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
553 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
555 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
557 return (otherData
.m_value
== m_value
);
560 #if wxUSE_STD_IOSTREAM
561 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
565 str
<< (const char*) s
.mb_str();
570 bool wxVariantDataBool::Write(wxString
& str
) const
572 str
.Printf(wxT("%d"), (int) m_value
);
576 #if wxUSE_STD_IOSTREAM
577 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
579 wxFAIL_MSG(wxT("Unimplemented"));
580 // str >> (long) m_value;
586 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
588 wxTextOutputStream
s(str
);
594 bool wxVariantDataBool::Read(wxInputStream
& str
)
596 wxTextInputStream
s(str
);
598 m_value
= s
.Read8() != 0;
601 #endif // wxUSE_STREAMS
603 bool wxVariantDataBool::Read(wxString
& str
)
605 m_value
= (wxAtol(str
) != 0);
611 wxVariant::wxVariant(bool val
, const wxString
& name
)
613 m_data
= new wxVariantDataBool(val
);
617 bool wxVariant::operator== (bool value
) const
620 if (!Convert(&thisValue
))
623 return (value
== thisValue
);
626 bool wxVariant::operator!= (bool value
) const
628 return (!((*this) == value
));
631 void wxVariant::operator= (bool value
)
633 if (GetType() == wxT("bool") &&
634 m_data
->GetRefCount() == 1)
636 ((wxVariantDataBool
*)GetData())->SetValue(value
);
641 m_data
= new wxVariantDataBool(value
);
645 bool wxVariant::GetBool() const
648 if (Convert(& value
))
652 wxFAIL_MSG(wxT("Could not convert to a bool"));
659 // -----------------------------------------------------------------
661 // -----------------------------------------------------------------
663 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
666 wxVariantDataChar() { m_value
= 0; }
667 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
669 inline wxUniChar
GetValue() const { return m_value
; }
670 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
672 virtual bool Eq(wxVariantData
& data
) const;
673 #if wxUSE_STD_IOSTREAM
674 virtual bool Read(wxSTD istream
& str
);
675 virtual bool Write(wxSTD ostream
& str
) const;
677 virtual bool Read(wxString
& str
);
678 virtual bool Write(wxString
& str
) const;
680 virtual bool Read(wxInputStream
& str
);
681 virtual bool Write(wxOutputStream
& str
) const;
682 #endif // wxUSE_STREAMS
683 virtual wxString
GetType() const { return wxT("char"); }
684 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
690 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
692 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
694 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
696 return (otherData
.m_value
== m_value
);
699 #if wxUSE_STD_IOSTREAM
700 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
702 str
<< wxString(m_value
);
707 bool wxVariantDataChar::Write(wxString
& str
) const
713 #if wxUSE_STD_IOSTREAM
714 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
716 wxFAIL_MSG(wxT("Unimplemented"));
723 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
725 wxTextOutputStream
s(str
);
727 // FIXME-UTF8: this should be just "s << m_value;" after removal of
728 // ANSI build and addition of wxUniChar to wxTextOutputStream:
729 s
<< (wxChar
)m_value
;
734 bool wxVariantDataChar::Read(wxInputStream
& str
)
736 wxTextInputStream
s(str
);
738 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
739 // ANSI build and addition of wxUniChar to wxTextInputStream:
746 #endif // wxUSE_STREAMS
748 bool wxVariantDataChar::Read(wxString
& str
)
754 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
756 m_data
= new wxVariantDataChar(val
);
760 wxVariant::wxVariant(char val
, const wxString
& name
)
762 m_data
= new wxVariantDataChar(val
);
766 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
768 m_data
= new wxVariantDataChar(val
);
772 bool wxVariant::operator==(const wxUniChar
& value
) const
775 if (!Convert(&thisValue
))
778 return (value
== thisValue
);
781 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
783 if (GetType() == wxT("char") &&
784 m_data
->GetRefCount() == 1)
786 ((wxVariantDataChar
*)GetData())->SetValue(value
);
791 m_data
= new wxVariantDataChar(value
);
797 wxUniChar
wxVariant::GetChar() const
800 if (Convert(& value
))
804 wxFAIL_MSG(wxT("Could not convert to a char"));
809 // ----------------------------------------------------------------------------
810 // wxVariantDataString
811 // ----------------------------------------------------------------------------
813 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
816 wxVariantDataString() { }
817 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
819 inline wxString
GetValue() const { return m_value
; }
820 inline void SetValue(const wxString
& value
) { m_value
= value
; }
822 virtual bool Eq(wxVariantData
& data
) const;
823 #if wxUSE_STD_IOSTREAM
824 virtual bool Write(wxSTD ostream
& str
) const;
826 virtual bool Read(wxString
& str
);
827 virtual bool Write(wxString
& str
) const;
828 #if wxUSE_STD_IOSTREAM
829 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
832 virtual bool Read(wxInputStream
& str
);
833 virtual bool Write(wxOutputStream
& str
) const;
834 #endif // wxUSE_STREAMS
835 virtual wxString
GetType() const { return wxT("string"); }
836 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
842 bool wxVariantDataString::Eq(wxVariantData
& data
) const
844 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
846 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
848 return (otherData
.m_value
== m_value
);
851 #if wxUSE_STD_IOSTREAM
852 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
854 str
<< (const char*) m_value
.mb_str();
859 bool wxVariantDataString::Write(wxString
& str
) const
866 bool wxVariantDataString::Write(wxOutputStream
& str
) const
868 // why doesn't wxOutputStream::operator<< take "const wxString&"
869 wxTextOutputStream
s(str
);
870 s
.WriteString(m_value
);
874 bool wxVariantDataString::Read(wxInputStream
& str
)
876 wxTextInputStream
s(str
);
878 m_value
= s
.ReadLine();
881 #endif // wxUSE_STREAMS
883 bool wxVariantDataString::Read(wxString
& str
)
891 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
893 m_data
= new wxVariantDataString(val
);
897 wxVariant::wxVariant(const char* val
, const wxString
& name
)
899 m_data
= new wxVariantDataString(wxString(val
));
903 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
905 m_data
= new wxVariantDataString(wxString(val
));
909 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
911 m_data
= new wxVariantDataString(val
.AsString());
915 wxVariant::wxVariant(const wxCharBuffer
& val
, const wxString
& name
)
917 m_data
= new wxVariantDataString(wxString(val
));
921 wxVariant::wxVariant(const wxWCharBuffer
& val
, const wxString
& name
)
923 m_data
= new wxVariantDataString(wxString(val
));
927 bool wxVariant::operator== (const wxString
& value
) const
930 if (!Convert(&thisValue
))
933 return value
== thisValue
;
936 bool wxVariant::operator!= (const wxString
& value
) const
938 return (!((*this) == value
));
941 wxVariant
& wxVariant::operator= (const wxString
& value
)
943 if (GetType() == wxT("string") &&
944 m_data
->GetRefCount() == 1)
946 ((wxVariantDataString
*)GetData())->SetValue(value
);
951 m_data
= new wxVariantDataString(value
);
956 wxString
wxVariant::GetString() const
959 if (!Convert(& value
))
961 wxFAIL_MSG(wxT("Could not convert to a string"));
967 // ----------------------------------------------------------------------------
968 // wxVariantDataWxObjectPtr
969 // ----------------------------------------------------------------------------
971 class wxVariantDataWxObjectPtr
: public wxVariantData
974 wxVariantDataWxObjectPtr() { }
975 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
977 inline wxObject
* GetValue() const { return m_value
; }
978 inline void SetValue(wxObject
* value
) { m_value
= value
; }
980 virtual bool Eq(wxVariantData
& data
) const;
981 #if wxUSE_STD_IOSTREAM
982 virtual bool Write(wxSTD ostream
& str
) const;
984 virtual bool Write(wxString
& str
) const;
985 #if wxUSE_STD_IOSTREAM
986 virtual bool Read(wxSTD istream
& str
);
988 virtual bool Read(wxString
& str
);
989 virtual wxString
GetType() const ;
990 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
992 virtual wxClassInfo
* GetValueClassInfo();
998 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
1000 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1002 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1004 return (otherData
.m_value
== m_value
);
1007 wxString
wxVariantDataWxObjectPtr::GetType() const
1009 wxString
returnVal(wxT("wxObject*"));
1013 returnVal
= m_value
->GetClassInfo()->GetClassName();
1014 returnVal
+= wxT("*");
1020 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1022 wxClassInfo
* returnVal
=NULL
;
1024 if (m_value
) returnVal
= m_value
->GetClassInfo();
1029 #if wxUSE_STD_IOSTREAM
1030 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1034 str
<< (const char*) s
.mb_str();
1039 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1041 str
.Printf(wxT("%s(%p)"), GetType().c_str(), wx_static_cast(void*, m_value
));
1045 #if wxUSE_STD_IOSTREAM
1046 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1053 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1061 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1063 m_data
= new wxVariantDataWxObjectPtr(val
);
1067 bool wxVariant::operator== (wxObject
* value
) const
1069 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1072 bool wxVariant::operator!= (wxObject
* value
) const
1074 return (!((*this) == (wxObject
*) value
));
1077 void wxVariant::operator= (wxObject
* value
)
1080 m_data
= new wxVariantDataWxObjectPtr(value
);
1083 wxObject
* wxVariant::GetWxObjectPtr() const
1085 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_data
)->GetValue();
1088 // ----------------------------------------------------------------------------
1089 // wxVariantDataVoidPtr
1090 // ----------------------------------------------------------------------------
1092 class wxVariantDataVoidPtr
: public wxVariantData
1095 wxVariantDataVoidPtr() { }
1096 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1098 inline void* GetValue() const { return m_value
; }
1099 inline void SetValue(void* value
) { m_value
= value
; }
1101 virtual bool Eq(wxVariantData
& data
) const;
1102 #if wxUSE_STD_IOSTREAM
1103 virtual bool Write(wxSTD ostream
& str
) const;
1105 virtual bool Write(wxString
& str
) const;
1106 #if wxUSE_STD_IOSTREAM
1107 virtual bool Read(wxSTD istream
& str
);
1109 virtual bool Read(wxString
& str
);
1110 virtual wxString
GetType() const { return wxT("void*"); }
1111 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1117 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1119 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1121 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1123 return (otherData
.m_value
== m_value
);
1126 #if wxUSE_STD_IOSTREAM
1127 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1131 str
<< (const char*) s
.mb_str();
1136 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1138 str
.Printf(wxT("%p"), m_value
);
1142 #if wxUSE_STD_IOSTREAM
1143 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1150 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1158 wxVariant::wxVariant( void* val
, const wxString
& name
)
1160 m_data
= new wxVariantDataVoidPtr(val
);
1164 bool wxVariant::operator== (void* value
) const
1166 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1169 bool wxVariant::operator!= (void* value
) const
1171 return (!((*this) == (void*) value
));
1174 void wxVariant::operator= (void* value
)
1176 if (GetType() == wxT("void*") && (m_data
->GetRefCount() == 1))
1178 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1183 m_data
= new wxVariantDataVoidPtr(value
);
1187 void* wxVariant::GetVoidPtr() const
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 str
= m_value
.Format();
1254 #if wxUSE_STD_IOSTREAM
1255 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1263 bool wxVariantDataDateTime::Read(wxString
& str
)
1265 if(! m_value
.ParseDateTime(str
.c_str()/*FIXME-UTF8*/))
1272 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1274 m_data
= new wxVariantDataDateTime(val
);
1278 bool wxVariant::operator== (const wxDateTime
& value
) const
1280 wxDateTime thisValue
;
1281 if (!Convert(&thisValue
))
1284 return value
.IsEqualTo(thisValue
);
1287 bool wxVariant::operator!= (const wxDateTime
& value
) const
1289 return (!((*this) == value
));
1292 void wxVariant::operator= (const wxDateTime
& value
)
1294 if (GetType() == wxT("datetime") &&
1295 m_data
->GetRefCount() == 1)
1297 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1302 m_data
= new wxVariantDataDateTime(value
);
1306 wxDateTime
wxVariant::GetDateTime() const
1309 if (!Convert(& value
))
1311 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1317 #endif // wxUSE_DATETIME
1319 // ----------------------------------------------------------------------------
1320 // wxVariantDataArrayString
1321 // ----------------------------------------------------------------------------
1323 class wxVariantDataArrayString
: public wxVariantData
1326 wxVariantDataArrayString() { }
1327 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1329 wxArrayString
GetValue() const { return m_value
; }
1330 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1332 virtual bool Eq(wxVariantData
& data
) const;
1333 #if wxUSE_STD_IOSTREAM
1334 virtual bool Write(wxSTD ostream
& str
) const;
1336 virtual bool Write(wxString
& str
) const;
1337 #if wxUSE_STD_IOSTREAM
1338 virtual bool Read(wxSTD istream
& str
);
1340 virtual bool Read(wxString
& str
);
1341 virtual wxString
GetType() const { return wxT("arrstring"); }
1342 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1345 wxArrayString m_value
;
1348 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1350 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1352 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1354 return otherData
.m_value
== m_value
;
1357 #if wxUSE_STD_IOSTREAM
1358 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1365 bool wxVariantDataArrayString::Write(wxString
& str
) const
1367 size_t count
= m_value
.GetCount();
1368 for ( size_t n
= 0; n
< count
; n
++ )
1380 #if wxUSE_STD_IOSTREAM
1381 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1389 bool wxVariantDataArrayString::Read(wxString
& str
)
1391 wxStringTokenizer
tk(str
, _T(";"));
1392 while ( tk
.HasMoreTokens() )
1394 m_value
.Add(tk
.GetNextToken());
1402 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1404 m_data
= new wxVariantDataArrayString(val
);
1408 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1410 wxFAIL_MSG( _T("TODO") );
1415 bool wxVariant::operator!=(const wxArrayString
& value
) const
1417 return !(*this == value
);
1420 void wxVariant::operator=(const wxArrayString
& value
)
1422 if (GetType() == wxT("arrstring") &&
1423 m_data
->GetRefCount() == 1)
1425 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1430 m_data
= new wxVariantDataArrayString(value
);
1434 wxArrayString
wxVariant::GetArrayString() const
1436 if ( GetType() == wxT("arrstring") )
1437 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1439 return wxArrayString();
1442 // ----------------------------------------------------------------------------
1443 // wxVariantDataList
1444 // ----------------------------------------------------------------------------
1446 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1449 wxVariantDataList() {}
1450 wxVariantDataList(const wxVariantList
& list
);
1451 virtual ~wxVariantDataList();
1453 wxVariantList
& GetValue() { return m_value
; }
1454 void SetValue(const wxVariantList
& value
) ;
1456 virtual bool Eq(wxVariantData
& data
) const;
1457 #if wxUSE_STD_IOSTREAM
1458 virtual bool Write(wxSTD ostream
& str
) const;
1460 virtual bool Write(wxString
& str
) const;
1461 #if wxUSE_STD_IOSTREAM
1462 virtual bool Read(wxSTD istream
& str
);
1464 virtual bool Read(wxString
& str
);
1465 virtual wxString
GetType() const { return wxT("list"); }
1469 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1471 wxVariantList m_value
;
1474 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
1479 wxVariantDataList::~wxVariantDataList()
1484 void wxVariantDataList::SetValue(const wxVariantList
& value
)
1487 wxVariantList::compatibility_iterator node
= value
.GetFirst();
1490 wxVariant
* var
= node
->GetData();
1491 m_value
.Append(new wxVariant(*var
));
1492 node
= node
->GetNext();
1496 void wxVariantDataList::Clear()
1498 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1501 wxVariant
* var
= node
->GetData();
1503 node
= node
->GetNext();
1508 bool wxVariantDataList::Eq(wxVariantData
& data
) const
1510 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
1512 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
1513 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
1514 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
1515 while (node1
&& node2
)
1517 wxVariant
* var1
= node1
->GetData();
1518 wxVariant
* var2
= node2
->GetData();
1519 if ((*var1
) != (*var2
))
1521 node1
= node1
->GetNext();
1522 node2
= node2
->GetNext();
1524 if (node1
|| node2
) return false;
1528 #if wxUSE_STD_IOSTREAM
1529 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
1533 str
<< (const char*) s
.mb_str();
1538 bool wxVariantDataList::Write(wxString
& str
) const
1540 str
= wxEmptyString
;
1541 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1544 wxVariant
* var
= node
->GetData();
1545 if (node
!= m_value
.GetFirst())
1548 str
+= var
->MakeString();
1549 node
= node
->GetNext();
1555 #if wxUSE_STD_IOSTREAM
1556 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
1558 wxFAIL_MSG(wxT("Unimplemented"));
1564 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
1566 wxFAIL_MSG(wxT("Unimplemented"));
1573 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
1575 m_data
= new wxVariantDataList(val
);
1579 bool wxVariant::operator== (const wxVariantList
& value
) const
1581 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1583 wxVariantDataList
other(value
);
1584 return (GetData()->Eq(other
));
1587 bool wxVariant::operator!= (const wxVariantList
& value
) const
1589 return (!((*this) == value
));
1592 void wxVariant::operator= (const wxVariantList
& value
)
1594 if (GetType() == wxT("list") &&
1595 m_data
->GetRefCount() == 1)
1597 ((wxVariantDataList
*)GetData())->SetValue(value
);
1602 m_data
= new wxVariantDataList(value
);
1606 wxVariantList
& wxVariant::GetList() const
1608 wxASSERT( (GetType() == wxT("list")) );
1610 return (wxVariantList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1614 void wxVariant::NullList()
1616 SetData(new wxVariantDataList());
1620 void wxVariant::Append(const wxVariant
& value
)
1622 wxVariantList
& list
= GetList();
1624 list
.Append(new wxVariant(value
));
1627 // Insert at front of list
1628 void wxVariant::Insert(const wxVariant
& value
)
1630 wxVariantList
& list
= GetList();
1632 list
.Insert(new wxVariant(value
));
1635 // Returns true if the variant is a member of the list
1636 bool wxVariant::Member(const wxVariant
& value
) const
1638 wxVariantList
& list
= GetList();
1640 wxVariantList::compatibility_iterator node
= list
.GetFirst();
1643 wxVariant
* other
= node
->GetData();
1644 if (value
== *other
)
1646 node
= node
->GetNext();
1651 // Deletes the nth element of the list
1652 bool wxVariant::Delete(size_t item
)
1654 wxVariantList
& list
= GetList();
1656 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
1657 wxVariantList::compatibility_iterator node
= list
.Item(item
);
1658 wxVariant
* variant
= node
->GetData();
1665 void wxVariant::ClearList()
1667 if (!IsNull() && (GetType() == wxT("list")))
1669 ((wxVariantDataList
*) m_data
)->Clear();
1673 if (!GetType().IsSameAs(wxT("list")))
1676 m_data
= new wxVariantDataList
;
1680 // Treat a list variant as an array
1681 wxVariant
wxVariant::operator[] (size_t idx
) const
1683 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
1685 if (GetType() == wxT("list"))
1687 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1688 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1689 return *(data
->GetValue().Item(idx
)->GetData());
1691 return wxNullVariant
;
1694 wxVariant
& wxVariant::operator[] (size_t idx
)
1696 // We can't return a reference to a variant for a string list, since the string
1697 // is actually stored as a char*, not a variant.
1699 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1701 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1702 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1704 return * (data
->GetValue().Item(idx
)->GetData());
1707 // Return the number of elements in a list
1708 size_t wxVariant::GetCount() const
1710 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
1712 if (GetType() == wxT("list"))
1714 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1715 return data
->GetValue().GetCount();
1720 // ----------------------------------------------------------------------------
1722 // ----------------------------------------------------------------------------
1724 bool wxVariant::Convert(long* value
) const
1726 wxString
type(GetType());
1727 if (type
== wxT("double"))
1728 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
1729 else if (type
== wxT("long"))
1730 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1732 else if (type
== wxT("bool"))
1733 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1735 else if (type
== wxT("string"))
1736 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
1743 bool wxVariant::Convert(bool* value
) const
1745 wxString
type(GetType());
1746 if (type
== wxT("double"))
1747 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
1748 else if (type
== wxT("long"))
1749 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1751 else if (type
== wxT("bool"))
1752 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1754 else if (type
== wxT("string"))
1756 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1758 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
1760 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
1771 bool wxVariant::Convert(double* value
) const
1773 wxString
type(GetType());
1774 if (type
== wxT("double"))
1775 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
1776 else if (type
== wxT("long"))
1777 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1779 else if (type
== wxT("bool"))
1780 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1782 else if (type
== wxT("string"))
1783 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
1790 bool wxVariant::Convert(wxUniChar
* value
) const
1792 wxString
type(GetType());
1793 if (type
== wxT("char"))
1794 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1795 else if (type
== wxT("long"))
1796 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1798 else if (type
== wxT("bool"))
1799 *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();
1840 // Fallback to string conversion
1842 return Convert(&val
) &&
1843 (value
->ParseDateTime(val
.c_str()/*FIXME-UTF8*/) ||
1844 value
->ParseDate(val
.c_str()/*FIXME-UTF8*/) ||
1845 value
->ParseTime(val
.c_str()/*FIXME-UTF8*/));
1847 #endif // wxUSE_DATETIME
1849 #endif // wxUSE_VARIANT