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_refData
== NULL
);
74 void wxVariant::MakeNull()
79 void wxVariant::Clear()
81 m_name
= wxEmptyString
;
84 wxVariant::wxVariant(const wxVariant
& variant
)
87 if (!variant
.IsNull())
90 m_name
= variant
.m_name
;
93 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
100 wxVariant::~wxVariant()
104 wxObjectRefData
*wxVariant::CreateRefData() const
106 // We cannot create any particular wxVariantData.
107 wxFAIL_MSG("wxVariant::CreateRefData() cannot be implemented");
111 wxObjectRefData
*wxVariant::CloneRefData(const wxObjectRefData
*data
) const
113 return ((wxVariantData
*) data
)->Clone();
117 void wxVariant::operator= (const wxVariant
& variant
)
120 m_name
= variant
.m_name
;
123 // myVariant = new wxStringVariantData("hello")
124 void wxVariant::operator= (wxVariantData
* variantData
)
127 m_refData
= variantData
;
130 bool wxVariant::operator== (const wxVariant
& variant
) const
132 if (IsNull() || variant
.IsNull())
133 return (IsNull() == variant
.IsNull());
135 if (GetType() != variant
.GetType())
138 return (GetData()->Eq(* variant
.GetData()));
141 bool wxVariant::operator!= (const wxVariant
& variant
) const
143 return (!(*this == variant
));
146 wxString
wxVariant::MakeString() const
151 if (GetData()->Write(str
))
154 return wxEmptyString
;
157 void wxVariant::SetData(wxVariantData
* data
)
163 bool wxVariant::Unshare()
165 if ( !m_refData
|| m_refData
->GetRefCount() == 1 )
170 return (m_refData
&& m_refData
->GetRefCount() == 1);
174 // Returns a string representing the type of the variant,
175 // e.g. "string", "bool", "list", "double", "long"
176 wxString
wxVariant::GetType() const
179 return wxString(wxT("null"));
181 return GetData()->GetType();
185 bool wxVariant::IsType(const wxString
& type
) const
187 return (GetType() == type
);
190 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
192 wxClassInfo
* info
=GetData()->GetValueClassInfo();
193 return info
? info
->IsKindOf(type
) : false ;
196 // -----------------------------------------------------------------
197 // wxVariant <-> wxAny conversion code
198 // -----------------------------------------------------------------
202 wxAnyToVariantRegistration::
203 wxAnyToVariantRegistration(wxVariantDataFactory factory
)
206 wxPreRegisterAnyToVariant(this);
209 wxVariant::wxVariant(const wxAny
& any
)
213 if ( !any
.GetAs(&variant
) )
215 wxFAIL_MSG("wxAny of this type cannot be converted to wxVariant");
222 wxAny
wxVariant::GetAny() const
229 wxVariantData
* data
= GetData();
231 if ( data
->GetAsAny(&any
) )
234 // If everything else fails, wrap the whole wxVariantData
240 // -----------------------------------------------------------------
242 // -----------------------------------------------------------------
244 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
247 wxVariantDataLong() { m_value
= 0; }
248 wxVariantDataLong(long value
) { m_value
= value
; }
250 inline long GetValue() const { return m_value
; }
251 inline void SetValue(long value
) { m_value
= value
; }
253 virtual bool Eq(wxVariantData
& data
) const;
255 virtual bool Read(wxString
& str
);
256 virtual bool Write(wxString
& str
) const;
257 #if wxUSE_STD_IOSTREAM
258 virtual bool Read(wxSTD istream
& str
);
259 virtual bool Write(wxSTD ostream
& str
) const;
262 virtual bool Read(wxInputStream
& str
);
263 virtual bool Write(wxOutputStream
&str
) const;
264 #endif // wxUSE_STREAMS
266 wxVariantData
* Clone() const { return new wxVariantDataLong(m_value
); }
268 virtual wxString
GetType() const { return wxT("long"); }
270 // Since wxAny does not have separate type for integers shorter than
271 // longlong, we do not usually implement wxVariant->wxAny conversion
272 // here (but in wxVariantDataLongLong instead).
274 DECLARE_WXANY_CONVERSION()
276 bool GetAsAny(wxAny
* any
) const
288 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(long, wxVariantDataLong
)
291 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
293 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
295 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
297 return (otherData
.m_value
== m_value
);
300 #if wxUSE_STD_IOSTREAM
301 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
305 str
<< (const char*) s
.mb_str();
310 bool wxVariantDataLong::Write(wxString
& str
) const
312 str
.Printf(wxT("%ld"), m_value
);
316 #if wxUSE_STD_IOSTREAM
317 bool wxVariantDataLong::Read(wxSTD istream
& str
)
325 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
327 wxTextOutputStream
s(str
);
329 s
.Write32((size_t)m_value
);
333 bool wxVariantDataLong::Read(wxInputStream
& str
)
335 wxTextInputStream
s(str
);
336 m_value
= s
.Read32();
339 #endif // wxUSE_STREAMS
341 bool wxVariantDataLong::Read(wxString
& str
)
343 m_value
= wxAtol(str
);
349 wxVariant::wxVariant(long val
, const wxString
& name
)
351 m_refData
= new wxVariantDataLong(val
);
355 wxVariant::wxVariant(int val
, const wxString
& name
)
357 m_refData
= new wxVariantDataLong((long)val
);
361 wxVariant::wxVariant(short val
, const wxString
& name
)
363 m_refData
= new wxVariantDataLong((long)val
);
367 bool wxVariant::operator== (long value
) const
370 if (!Convert(&thisValue
))
373 return (value
== thisValue
);
376 bool wxVariant::operator!= (long value
) const
378 return (!((*this) == value
));
381 void wxVariant::operator= (long value
)
383 if (GetType() == wxT("long") &&
384 m_refData
->GetRefCount() == 1)
386 ((wxVariantDataLong
*)GetData())->SetValue(value
);
391 m_refData
= new wxVariantDataLong(value
);
395 long wxVariant::GetLong() const
398 if (Convert(& value
))
402 wxFAIL_MSG(wxT("Could not convert to a long"));
407 // -----------------------------------------------------------------
408 // wxVariantDoubleData
409 // -----------------------------------------------------------------
411 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
414 wxVariantDoubleData() { m_value
= 0.0; }
415 wxVariantDoubleData(double value
) { m_value
= value
; }
417 inline double GetValue() const { return m_value
; }
418 inline void SetValue(double value
) { m_value
= value
; }
420 virtual bool Eq(wxVariantData
& data
) const;
421 virtual bool Read(wxString
& str
);
422 #if wxUSE_STD_IOSTREAM
423 virtual bool Write(wxSTD ostream
& str
) const;
425 virtual bool Write(wxString
& str
) const;
426 #if wxUSE_STD_IOSTREAM
427 virtual bool Read(wxSTD istream
& str
);
430 virtual bool Read(wxInputStream
& str
);
431 virtual bool Write(wxOutputStream
&str
) const;
432 #endif // wxUSE_STREAMS
433 virtual wxString
GetType() const { return wxT("double"); }
435 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
437 DECLARE_WXANY_CONVERSION()
442 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(double, wxVariantDoubleData
)
444 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
446 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
448 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
450 return wxIsSameDouble(otherData
.m_value
, m_value
);
453 #if wxUSE_STD_IOSTREAM
454 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
458 str
<< (const char*) s
.mb_str();
463 bool wxVariantDoubleData::Write(wxString
& str
) const
465 str
.Printf(wxT("%.14g"), m_value
);
469 #if wxUSE_STD_IOSTREAM
470 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
478 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
480 wxTextOutputStream
s(str
);
481 s
.WriteDouble((double)m_value
);
485 bool wxVariantDoubleData::Read(wxInputStream
& str
)
487 wxTextInputStream
s(str
);
488 m_value
= (float)s
.ReadDouble();
491 #endif // wxUSE_STREAMS
493 bool wxVariantDoubleData::Read(wxString
& str
)
495 m_value
= wxAtof(str
);
499 // wxVariant double code
501 wxVariant::wxVariant(double val
, const wxString
& name
)
503 m_refData
= new wxVariantDoubleData(val
);
507 bool wxVariant::operator== (double value
) const
510 if (!Convert(&thisValue
))
513 return wxIsSameDouble(value
, thisValue
);
516 bool wxVariant::operator!= (double value
) const
518 return (!((*this) == value
));
521 void wxVariant::operator= (double value
)
523 if (GetType() == wxT("double") &&
524 m_refData
->GetRefCount() == 1)
526 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
531 m_refData
= new wxVariantDoubleData(value
);
535 double wxVariant::GetDouble() const
538 if (Convert(& value
))
542 wxFAIL_MSG(wxT("Could not convert to a double number"));
547 // -----------------------------------------------------------------
549 // -----------------------------------------------------------------
551 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
554 wxVariantDataBool() { m_value
= 0; }
555 wxVariantDataBool(bool value
) { m_value
= value
; }
557 inline bool GetValue() const { return m_value
; }
558 inline void SetValue(bool value
) { m_value
= value
; }
560 virtual bool Eq(wxVariantData
& data
) const;
561 #if wxUSE_STD_IOSTREAM
562 virtual bool Write(wxSTD ostream
& str
) const;
564 virtual bool Write(wxString
& str
) const;
565 virtual bool Read(wxString
& str
);
566 #if wxUSE_STD_IOSTREAM
567 virtual bool Read(wxSTD istream
& str
);
570 virtual bool Read(wxInputStream
& str
);
571 virtual bool Write(wxOutputStream
& str
) const;
572 #endif // wxUSE_STREAMS
573 virtual wxString
GetType() const { return wxT("bool"); }
575 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
577 DECLARE_WXANY_CONVERSION()
582 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(bool, wxVariantDataBool
)
584 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
586 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
588 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
590 return (otherData
.m_value
== m_value
);
593 #if wxUSE_STD_IOSTREAM
594 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
598 str
<< (const char*) s
.mb_str();
603 bool wxVariantDataBool::Write(wxString
& str
) const
605 str
.Printf(wxT("%d"), (int) m_value
);
609 #if wxUSE_STD_IOSTREAM
610 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
612 wxFAIL_MSG(wxT("Unimplemented"));
613 // str >> (long) m_value;
619 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
621 wxTextOutputStream
s(str
);
627 bool wxVariantDataBool::Read(wxInputStream
& str
)
629 wxTextInputStream
s(str
);
631 m_value
= s
.Read8() != 0;
634 #endif // wxUSE_STREAMS
636 bool wxVariantDataBool::Read(wxString
& str
)
638 m_value
= (wxAtol(str
) != 0);
644 wxVariant::wxVariant(bool val
, const wxString
& name
)
646 m_refData
= new wxVariantDataBool(val
);
650 bool wxVariant::operator== (bool value
) const
653 if (!Convert(&thisValue
))
656 return (value
== thisValue
);
659 bool wxVariant::operator!= (bool value
) const
661 return (!((*this) == value
));
664 void wxVariant::operator= (bool value
)
666 if (GetType() == wxT("bool") &&
667 m_refData
->GetRefCount() == 1)
669 ((wxVariantDataBool
*)GetData())->SetValue(value
);
674 m_refData
= new wxVariantDataBool(value
);
678 bool wxVariant::GetBool() const
681 if (Convert(& value
))
685 wxFAIL_MSG(wxT("Could not convert to a bool"));
690 // -----------------------------------------------------------------
692 // -----------------------------------------------------------------
694 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
697 wxVariantDataChar() { m_value
= 0; }
698 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
700 inline wxUniChar
GetValue() const { return m_value
; }
701 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
703 virtual bool Eq(wxVariantData
& data
) const;
704 #if wxUSE_STD_IOSTREAM
705 virtual bool Read(wxSTD istream
& str
);
706 virtual bool Write(wxSTD ostream
& str
) const;
708 virtual bool Read(wxString
& str
);
709 virtual bool Write(wxString
& str
) const;
711 virtual bool Read(wxInputStream
& str
);
712 virtual bool Write(wxOutputStream
& str
) const;
713 #endif // wxUSE_STREAMS
714 virtual wxString
GetType() const { return wxT("char"); }
715 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
717 DECLARE_WXANY_CONVERSION()
722 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxUniChar
, wxVariantDataChar
)
724 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
726 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
728 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
730 return (otherData
.m_value
== m_value
);
733 #if wxUSE_STD_IOSTREAM
734 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
736 str
<< wxString(m_value
);
741 bool wxVariantDataChar::Write(wxString
& str
) const
747 #if wxUSE_STD_IOSTREAM
748 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
750 wxFAIL_MSG(wxT("Unimplemented"));
757 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
759 wxTextOutputStream
s(str
);
761 // FIXME-UTF8: this should be just "s << m_value;" after removal of
762 // ANSI build and addition of wxUniChar to wxTextOutputStream:
763 s
<< (wxChar
)m_value
;
768 bool wxVariantDataChar::Read(wxInputStream
& str
)
770 wxTextInputStream
s(str
);
772 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
773 // ANSI build and addition of wxUniChar to wxTextInputStream:
780 #endif // wxUSE_STREAMS
782 bool wxVariantDataChar::Read(wxString
& str
)
788 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
790 m_refData
= new wxVariantDataChar(val
);
794 wxVariant::wxVariant(char val
, const wxString
& name
)
796 m_refData
= new wxVariantDataChar(val
);
800 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
802 m_refData
= new wxVariantDataChar(val
);
806 bool wxVariant::operator==(const wxUniChar
& value
) const
809 if (!Convert(&thisValue
))
812 return (value
== thisValue
);
815 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
817 if (GetType() == wxT("char") &&
818 m_refData
->GetRefCount() == 1)
820 ((wxVariantDataChar
*)GetData())->SetValue(value
);
825 m_refData
= new wxVariantDataChar(value
);
831 wxUniChar
wxVariant::GetChar() const
834 if (Convert(& value
))
838 wxFAIL_MSG(wxT("Could not convert to a char"));
843 // ----------------------------------------------------------------------------
844 // wxVariantDataString
845 // ----------------------------------------------------------------------------
847 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
850 wxVariantDataString() { }
851 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
853 inline wxString
GetValue() const { return m_value
; }
854 inline void SetValue(const wxString
& value
) { m_value
= value
; }
856 virtual bool Eq(wxVariantData
& data
) const;
857 #if wxUSE_STD_IOSTREAM
858 virtual bool Write(wxSTD ostream
& str
) const;
860 virtual bool Read(wxString
& str
);
861 virtual bool Write(wxString
& str
) const;
862 #if wxUSE_STD_IOSTREAM
863 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
866 virtual bool Read(wxInputStream
& str
);
867 virtual bool Write(wxOutputStream
& str
) const;
868 #endif // wxUSE_STREAMS
869 virtual wxString
GetType() const { return wxT("string"); }
870 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
872 DECLARE_WXANY_CONVERSION()
877 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxString
, wxVariantDataString
)
879 bool wxVariantDataString::Eq(wxVariantData
& data
) const
881 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
883 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
885 return (otherData
.m_value
== m_value
);
888 #if wxUSE_STD_IOSTREAM
889 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
891 str
<< (const char*) m_value
.mb_str();
896 bool wxVariantDataString::Write(wxString
& str
) const
903 bool wxVariantDataString::Write(wxOutputStream
& str
) const
905 // why doesn't wxOutputStream::operator<< take "const wxString&"
906 wxTextOutputStream
s(str
);
907 s
.WriteString(m_value
);
911 bool wxVariantDataString::Read(wxInputStream
& str
)
913 wxTextInputStream
s(str
);
915 m_value
= s
.ReadLine();
918 #endif // wxUSE_STREAMS
920 bool wxVariantDataString::Read(wxString
& str
)
928 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
930 m_refData
= new wxVariantDataString(val
);
934 wxVariant::wxVariant(const char* val
, const wxString
& name
)
936 m_refData
= new wxVariantDataString(wxString(val
));
940 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
942 m_refData
= new wxVariantDataString(wxString(val
));
946 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
948 m_refData
= new wxVariantDataString(val
.AsString());
952 wxVariant::wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
)
954 m_refData
= new wxVariantDataString(wxString(val
));
958 wxVariant::wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
)
960 m_refData
= new wxVariantDataString(wxString(val
));
964 bool wxVariant::operator== (const wxString
& value
) const
967 if (!Convert(&thisValue
))
970 return value
== thisValue
;
973 bool wxVariant::operator!= (const wxString
& value
) const
975 return (!((*this) == value
));
978 wxVariant
& wxVariant::operator= (const wxString
& value
)
980 if (GetType() == wxT("string") &&
981 m_refData
->GetRefCount() == 1)
983 ((wxVariantDataString
*)GetData())->SetValue(value
);
988 m_refData
= new wxVariantDataString(value
);
993 wxString
wxVariant::GetString() const
996 if (!Convert(& value
))
998 wxFAIL_MSG(wxT("Could not convert to a string"));
1004 // ----------------------------------------------------------------------------
1005 // wxVariantDataWxObjectPtr
1006 // ----------------------------------------------------------------------------
1008 class wxVariantDataWxObjectPtr
: public wxVariantData
1011 wxVariantDataWxObjectPtr() { }
1012 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
1014 inline wxObject
* GetValue() const { return m_value
; }
1015 inline void SetValue(wxObject
* value
) { m_value
= value
; }
1017 virtual bool Eq(wxVariantData
& data
) const;
1018 #if wxUSE_STD_IOSTREAM
1019 virtual bool Write(wxSTD ostream
& str
) const;
1021 virtual bool Write(wxString
& str
) const;
1022 #if wxUSE_STD_IOSTREAM
1023 virtual bool Read(wxSTD istream
& str
);
1025 virtual bool Read(wxString
& str
);
1026 virtual wxString
GetType() const ;
1027 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
1029 virtual wxClassInfo
* GetValueClassInfo();
1031 DECLARE_WXANY_CONVERSION()
1036 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxObject
*, wxVariantDataWxObjectPtr
)
1038 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
1040 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1042 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1044 return (otherData
.m_value
== m_value
);
1047 wxString
wxVariantDataWxObjectPtr::GetType() const
1049 wxString
returnVal(wxT("wxObject*"));
1053 returnVal
= m_value
->GetClassInfo()->GetClassName();
1054 returnVal
+= wxT("*");
1060 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1062 wxClassInfo
* returnVal
=NULL
;
1064 if (m_value
) returnVal
= m_value
->GetClassInfo();
1069 #if wxUSE_STD_IOSTREAM
1070 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1074 str
<< (const char*) s
.mb_str();
1079 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1081 str
.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value
));
1085 #if wxUSE_STD_IOSTREAM
1086 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1093 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1101 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1103 m_refData
= new wxVariantDataWxObjectPtr(val
);
1107 bool wxVariant::operator== (wxObject
* value
) const
1109 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1112 bool wxVariant::operator!= (wxObject
* value
) const
1114 return (!((*this) == (wxObject
*) value
));
1117 void wxVariant::operator= (wxObject
* value
)
1120 m_refData
= new wxVariantDataWxObjectPtr(value
);
1123 wxObject
* wxVariant::GetWxObjectPtr() const
1125 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_refData
)->GetValue();
1128 // ----------------------------------------------------------------------------
1129 // wxVariantDataVoidPtr
1130 // ----------------------------------------------------------------------------
1132 class wxVariantDataVoidPtr
: public wxVariantData
1135 wxVariantDataVoidPtr() { }
1136 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1138 inline void* GetValue() const { return m_value
; }
1139 inline void SetValue(void* value
) { m_value
= value
; }
1141 virtual bool Eq(wxVariantData
& data
) const;
1142 #if wxUSE_STD_IOSTREAM
1143 virtual bool Write(wxSTD ostream
& str
) const;
1145 virtual bool Write(wxString
& str
) const;
1146 #if wxUSE_STD_IOSTREAM
1147 virtual bool Read(wxSTD istream
& str
);
1149 virtual bool Read(wxString
& str
);
1150 virtual wxString
GetType() const { return wxT("void*"); }
1151 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1153 DECLARE_WXANY_CONVERSION()
1158 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(void*, wxVariantDataVoidPtr
)
1160 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1162 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1164 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1166 return (otherData
.m_value
== m_value
);
1169 #if wxUSE_STD_IOSTREAM
1170 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1174 str
<< (const char*) s
.mb_str();
1179 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1181 str
.Printf(wxT("%p"), m_value
);
1185 #if wxUSE_STD_IOSTREAM
1186 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1193 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1201 wxVariant::wxVariant( void* val
, const wxString
& name
)
1203 m_refData
= new wxVariantDataVoidPtr(val
);
1207 bool wxVariant::operator== (void* value
) const
1209 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1212 bool wxVariant::operator!= (void* value
) const
1214 return (!((*this) == (void*) value
));
1217 void wxVariant::operator= (void* value
)
1219 if (GetType() == wxT("void*") && (m_refData
->GetRefCount() == 1))
1221 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1226 m_refData
= new wxVariantDataVoidPtr(value
);
1230 void* wxVariant::GetVoidPtr() const
1232 // handling this specially is convenient when working with COM, see #9873
1236 wxASSERT( GetType() == wxT("void*") );
1238 return (void*) ((wxVariantDataVoidPtr
*) m_refData
)->GetValue();
1241 // ----------------------------------------------------------------------------
1242 // wxVariantDataDateTime
1243 // ----------------------------------------------------------------------------
1247 class wxVariantDataDateTime
: public wxVariantData
1250 wxVariantDataDateTime() { }
1251 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1253 inline wxDateTime
GetValue() const { return m_value
; }
1254 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1256 virtual bool Eq(wxVariantData
& data
) const;
1257 #if wxUSE_STD_IOSTREAM
1258 virtual bool Write(wxSTD ostream
& str
) const;
1260 virtual bool Write(wxString
& str
) const;
1261 #if wxUSE_STD_IOSTREAM
1262 virtual bool Read(wxSTD istream
& str
);
1264 virtual bool Read(wxString
& str
);
1265 virtual wxString
GetType() const { return wxT("datetime"); }
1266 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1268 DECLARE_WXANY_CONVERSION()
1273 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxDateTime
, wxVariantDataDateTime
)
1275 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1277 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1279 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1281 return (otherData
.m_value
== m_value
);
1285 #if wxUSE_STD_IOSTREAM
1286 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1290 str
<< value
.c_str();
1296 bool wxVariantDataDateTime::Write(wxString
& str
) const
1298 if ( m_value
.IsValid() )
1299 str
= m_value
.Format();
1301 str
= wxS("Invalid");
1306 #if wxUSE_STD_IOSTREAM
1307 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1315 bool wxVariantDataDateTime::Read(wxString
& str
)
1317 if ( str
== wxS("Invalid") )
1319 m_value
= wxInvalidDateTime
;
1323 wxString::const_iterator end
;
1324 return m_value
.ParseDateTime(str
, &end
) && end
== str
.end();
1329 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1331 m_refData
= new wxVariantDataDateTime(val
);
1335 bool wxVariant::operator== (const wxDateTime
& value
) const
1337 wxDateTime thisValue
;
1338 if (!Convert(&thisValue
))
1341 return value
.IsEqualTo(thisValue
);
1344 bool wxVariant::operator!= (const wxDateTime
& value
) const
1346 return (!((*this) == value
));
1349 void wxVariant::operator= (const wxDateTime
& value
)
1351 if (GetType() == wxT("datetime") &&
1352 m_refData
->GetRefCount() == 1)
1354 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1359 m_refData
= new wxVariantDataDateTime(value
);
1363 wxDateTime
wxVariant::GetDateTime() const
1366 if (!Convert(& value
))
1368 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1374 #endif // wxUSE_DATETIME
1376 // ----------------------------------------------------------------------------
1377 // wxVariantDataArrayString
1378 // ----------------------------------------------------------------------------
1380 class wxVariantDataArrayString
: public wxVariantData
1383 wxVariantDataArrayString() { }
1384 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1386 wxArrayString
GetValue() const { return m_value
; }
1387 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1389 virtual bool Eq(wxVariantData
& data
) const;
1390 #if wxUSE_STD_IOSTREAM
1391 virtual bool Write(wxSTD ostream
& str
) const;
1393 virtual bool Write(wxString
& str
) const;
1394 #if wxUSE_STD_IOSTREAM
1395 virtual bool Read(wxSTD istream
& str
);
1397 virtual bool Read(wxString
& str
);
1398 virtual wxString
GetType() const { return wxT("arrstring"); }
1399 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1401 DECLARE_WXANY_CONVERSION()
1403 wxArrayString m_value
;
1406 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxArrayString
, wxVariantDataArrayString
)
1408 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1410 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1412 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1414 return otherData
.m_value
== m_value
;
1417 #if wxUSE_STD_IOSTREAM
1418 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1425 bool wxVariantDataArrayString::Write(wxString
& str
) const
1427 size_t count
= m_value
.GetCount();
1428 for ( size_t n
= 0; n
< count
; n
++ )
1440 #if wxUSE_STD_IOSTREAM
1441 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1449 bool wxVariantDataArrayString::Read(wxString
& str
)
1451 wxStringTokenizer
tk(str
, wxT(";"));
1452 while ( tk
.HasMoreTokens() )
1454 m_value
.Add(tk
.GetNextToken());
1462 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1464 m_refData
= new wxVariantDataArrayString(val
);
1468 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1470 wxFAIL_MSG( wxT("TODO") );
1475 bool wxVariant::operator!=(const wxArrayString
& value
) const
1477 return !(*this == value
);
1480 void wxVariant::operator=(const wxArrayString
& value
)
1482 if (GetType() == wxT("arrstring") &&
1483 m_refData
->GetRefCount() == 1)
1485 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1490 m_refData
= new wxVariantDataArrayString(value
);
1494 wxArrayString
wxVariant::GetArrayString() const
1496 if ( GetType() == wxT("arrstring") )
1497 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1499 return wxArrayString();
1502 // ----------------------------------------------------------------------------
1503 // wxVariantDataLongLong
1504 // ----------------------------------------------------------------------------
1508 class WXDLLIMPEXP_BASE wxVariantDataLongLong
: public wxVariantData
1511 wxVariantDataLongLong() { m_value
= 0; }
1512 wxVariantDataLongLong(wxLongLong value
) { m_value
= value
; }
1514 wxLongLong
GetValue() const { return m_value
; }
1515 void SetValue(wxLongLong value
) { m_value
= value
; }
1517 virtual bool Eq(wxVariantData
& data
) const;
1519 virtual bool Read(wxString
& str
);
1520 virtual bool Write(wxString
& str
) const;
1521 #if wxUSE_STD_IOSTREAM
1522 virtual bool Read(wxSTD istream
& str
);
1523 virtual bool Write(wxSTD ostream
& str
) const;
1526 virtual bool Read(wxInputStream
& str
);
1527 virtual bool Write(wxOutputStream
&str
) const;
1528 #endif // wxUSE_STREAMS
1530 wxVariantData
* Clone() const
1532 return new wxVariantDataLongLong(m_value
);
1535 virtual wxString
GetType() const { return wxS("longlong"); }
1537 DECLARE_WXANY_CONVERSION()
1543 // wxLongLong type requires customized wxAny conversion code
1548 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1550 *any
= m_value
.GetValue();
1554 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1556 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong_t
));
1559 REGISTER_WXANY_CONVERSION(wxLongLong_t
, wxVariantDataLongLong
)
1561 #else // if !defined(wxLongLong_t)
1563 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1569 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1571 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong
));
1574 REGISTER_WXANY_CONVERSION(wxLongLong
, wxVariantDataLongLong
)
1576 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1579 bool wxVariantDataLongLong::Eq(wxVariantData
& data
) const
1581 wxASSERT_MSG( (data
.GetType() == wxS("longlong")),
1582 "wxVariantDataLongLong::Eq: argument mismatch" );
1584 wxVariantDataLongLong
& otherData
= (wxVariantDataLongLong
&) data
;
1586 return (otherData
.m_value
== m_value
);
1589 #if wxUSE_STD_IOSTREAM
1590 bool wxVariantDataLongLong::Write(wxSTD ostream
& str
) const
1594 str
<< (const char*) s
.mb_str();
1599 bool wxVariantDataLongLong::Write(wxString
& str
) const
1602 str
.Printf(wxS("%lld"), m_value
.GetValue());
1609 #if wxUSE_STD_IOSTREAM
1610 bool wxVariantDataLongLong::Read(wxSTD istream
& WXUNUSED(str
))
1612 wxFAIL_MSG(wxS("Unimplemented"));
1618 bool wxVariantDataLongLong::Write(wxOutputStream
& str
) const
1620 wxTextOutputStream
s(str
);
1621 s
.Write32(m_value
.GetLo());
1622 s
.Write32(m_value
.GetHi());
1626 bool wxVariantDataLongLong::Read(wxInputStream
& str
)
1628 wxTextInputStream
s(str
);
1629 unsigned long lo
= s
.Read32();
1630 long hi
= s
.Read32();
1631 m_value
= wxLongLong(hi
, lo
);
1634 #endif // wxUSE_STREAMS
1636 bool wxVariantDataLongLong::Read(wxString
& str
)
1639 wxLongLong_t value_t
;
1640 if ( !str
.ToLongLong(&value_t
) )
1651 wxVariant::wxVariant(wxLongLong val
, const wxString
& name
)
1653 m_refData
= new wxVariantDataLongLong(val
);
1657 bool wxVariant::operator==(wxLongLong value
) const
1659 wxLongLong thisValue
;
1660 if ( !Convert(&thisValue
) )
1663 return (value
== thisValue
);
1666 bool wxVariant::operator!=(wxLongLong value
) const
1668 return (!((*this) == value
));
1671 void wxVariant::operator=(wxLongLong value
)
1673 if ( GetType() == wxS("longlong") &&
1674 m_refData
->GetRefCount() == 1 )
1676 ((wxVariantDataLongLong
*)GetData())->SetValue(value
);
1681 m_refData
= new wxVariantDataLongLong(value
);
1685 wxLongLong
wxVariant::GetLongLong() const
1688 if ( Convert(&value
) )
1694 wxFAIL_MSG(wxT("Could not convert to a long long"));
1699 #endif // wxUSE_LONGLONG
1701 // ----------------------------------------------------------------------------
1702 // wxVariantDataULongLong
1703 // ----------------------------------------------------------------------------
1707 class WXDLLIMPEXP_BASE wxVariantDataULongLong
: public wxVariantData
1710 wxVariantDataULongLong() { m_value
= 0; }
1711 wxVariantDataULongLong(wxULongLong value
) { m_value
= value
; }
1713 wxULongLong
GetValue() const { return m_value
; }
1714 void SetValue(wxULongLong value
) { m_value
= value
; }
1716 virtual bool Eq(wxVariantData
& data
) const;
1718 virtual bool Read(wxString
& str
);
1719 virtual bool Write(wxString
& str
) const;
1720 #if wxUSE_STD_IOSTREAM
1721 virtual bool Read(wxSTD istream
& str
);
1722 virtual bool Write(wxSTD ostream
& str
) const;
1725 virtual bool Read(wxInputStream
& str
);
1726 virtual bool Write(wxOutputStream
&str
) const;
1727 #endif // wxUSE_STREAMS
1729 wxVariantData
* Clone() const
1731 return new wxVariantDataULongLong(m_value
);
1734 virtual wxString
GetType() const { return wxS("ulonglong"); }
1736 DECLARE_WXANY_CONVERSION()
1738 wxULongLong m_value
;
1742 // wxULongLong type requires customized wxAny conversion code
1747 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1749 *any
= m_value
.GetValue();
1753 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1755 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong_t
));
1758 REGISTER_WXANY_CONVERSION(wxULongLong_t
, wxVariantDataULongLong
)
1760 #else // if !defined(wxLongLong_t)
1762 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1768 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1770 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong
));
1773 REGISTER_WXANY_CONVERSION(wxULongLong
, wxVariantDataULongLong
)
1775 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1779 bool wxVariantDataULongLong::Eq(wxVariantData
& data
) const
1781 wxASSERT_MSG( (data
.GetType() == wxS("ulonglong")),
1782 "wxVariantDataULongLong::Eq: argument mismatch" );
1784 wxVariantDataULongLong
& otherData
= (wxVariantDataULongLong
&) data
;
1786 return (otherData
.m_value
== m_value
);
1789 #if wxUSE_STD_IOSTREAM
1790 bool wxVariantDataULongLong::Write(wxSTD ostream
& str
) const
1794 str
<< (const char*) s
.mb_str();
1799 bool wxVariantDataULongLong::Write(wxString
& str
) const
1802 str
.Printf(wxS("%llu"), m_value
.GetValue());
1809 #if wxUSE_STD_IOSTREAM
1810 bool wxVariantDataULongLong::Read(wxSTD istream
& WXUNUSED(str
))
1812 wxFAIL_MSG(wxS("Unimplemented"));
1818 bool wxVariantDataULongLong::Write(wxOutputStream
& str
) const
1820 wxTextOutputStream
s(str
);
1821 s
.Write32(m_value
.GetLo());
1822 s
.Write32(m_value
.GetHi());
1826 bool wxVariantDataULongLong::Read(wxInputStream
& str
)
1828 wxTextInputStream
s(str
);
1829 unsigned long lo
= s
.Read32();
1830 long hi
= s
.Read32();
1831 m_value
= wxULongLong(hi
, lo
);
1834 #endif // wxUSE_STREAMS
1836 bool wxVariantDataULongLong::Read(wxString
& str
)
1839 wxULongLong_t value_t
;
1840 if ( !str
.ToULongLong(&value_t
) )
1851 wxVariant::wxVariant(wxULongLong val
, const wxString
& name
)
1853 m_refData
= new wxVariantDataULongLong(val
);
1857 bool wxVariant::operator==(wxULongLong value
) const
1859 wxULongLong thisValue
;
1860 if ( !Convert(&thisValue
) )
1863 return (value
== thisValue
);
1866 bool wxVariant::operator!=(wxULongLong value
) const
1868 return (!((*this) == value
));
1871 void wxVariant::operator=(wxULongLong value
)
1873 if ( GetType() == wxS("ulonglong") &&
1874 m_refData
->GetRefCount() == 1 )
1876 ((wxVariantDataULongLong
*)GetData())->SetValue(value
);
1881 m_refData
= new wxVariantDataULongLong(value
);
1885 wxULongLong
wxVariant::GetULongLong() const
1888 if ( Convert(&value
) )
1894 wxFAIL_MSG(wxT("Could not convert to a long long"));
1899 #endif // wxUSE_LONGLONG
1901 // ----------------------------------------------------------------------------
1902 // wxVariantDataList
1903 // ----------------------------------------------------------------------------
1905 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1908 wxVariantDataList() {}
1909 wxVariantDataList(const wxVariantList
& list
);
1910 virtual ~wxVariantDataList();
1912 wxVariantList
& GetValue() { return m_value
; }
1913 void SetValue(const wxVariantList
& value
) ;
1915 virtual bool Eq(wxVariantData
& data
) const;
1916 #if wxUSE_STD_IOSTREAM
1917 virtual bool Write(wxSTD ostream
& str
) const;
1919 virtual bool Write(wxString
& str
) const;
1920 #if wxUSE_STD_IOSTREAM
1921 virtual bool Read(wxSTD istream
& str
);
1923 virtual bool Read(wxString
& str
);
1924 virtual wxString
GetType() const { return wxT("list"); }
1928 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1930 DECLARE_WXANY_CONVERSION()
1932 wxVariantList m_value
;
1938 // Convert to/from list of wxAnys
1941 WX_DEFINE_LIST(wxAnyList
)
1943 bool wxVariantDataList::GetAsAny(wxAny
* any
) const
1946 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1949 wxVariant
* pVar
= node
->GetData();
1950 dst
.push_back(new wxAny(((const wxVariant
&)*pVar
)));
1951 node
= node
->GetNext();
1958 wxVariantData
* wxVariantDataList::VariantDataFactory(const wxAny
& any
)
1960 wxAnyList src
= wxANY_AS(any
, wxAnyList
);
1962 wxAnyList::compatibility_iterator node
= src
.GetFirst();
1965 wxAny
* pAny
= node
->GetData();
1966 dst
.push_back(new wxVariant(*pAny
));
1967 node
= node
->GetNext();
1970 return new wxVariantDataList(dst
);
1973 REGISTER_WXANY_CONVERSION(wxAnyList
, wxVariantDataList
)
1977 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
1982 wxVariantDataList::~wxVariantDataList()
1987 void wxVariantDataList::SetValue(const wxVariantList
& value
)
1990 wxVariantList::compatibility_iterator node
= value
.GetFirst();
1993 wxVariant
* var
= node
->GetData();
1994 m_value
.Append(new wxVariant(*var
));
1995 node
= node
->GetNext();
1999 void wxVariantDataList::Clear()
2001 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2004 wxVariant
* var
= node
->GetData();
2006 node
= node
->GetNext();
2011 bool wxVariantDataList::Eq(wxVariantData
& data
) const
2013 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
2015 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
2016 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
2017 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
2018 while (node1
&& node2
)
2020 wxVariant
* var1
= node1
->GetData();
2021 wxVariant
* var2
= node2
->GetData();
2022 if ((*var1
) != (*var2
))
2024 node1
= node1
->GetNext();
2025 node2
= node2
->GetNext();
2027 if (node1
|| node2
) return false;
2031 #if wxUSE_STD_IOSTREAM
2032 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
2036 str
<< (const char*) s
.mb_str();
2041 bool wxVariantDataList::Write(wxString
& str
) const
2043 str
= wxEmptyString
;
2044 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2047 wxVariant
* var
= node
->GetData();
2048 if (node
!= m_value
.GetFirst())
2051 str
+= var
->MakeString();
2052 node
= node
->GetNext();
2058 #if wxUSE_STD_IOSTREAM
2059 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
2061 wxFAIL_MSG(wxT("Unimplemented"));
2067 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
2069 wxFAIL_MSG(wxT("Unimplemented"));
2076 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
2078 m_refData
= new wxVariantDataList(val
);
2082 bool wxVariant::operator== (const wxVariantList
& value
) const
2084 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
2086 wxVariantDataList
other(value
);
2087 return (GetData()->Eq(other
));
2090 bool wxVariant::operator!= (const wxVariantList
& value
) const
2092 return (!((*this) == value
));
2095 void wxVariant::operator= (const wxVariantList
& value
)
2097 if (GetType() == wxT("list") &&
2098 m_refData
->GetRefCount() == 1)
2100 ((wxVariantDataList
*)GetData())->SetValue(value
);
2105 m_refData
= new wxVariantDataList(value
);
2109 wxVariantList
& wxVariant::GetList() const
2111 wxASSERT( (GetType() == wxT("list")) );
2113 return (wxVariantList
&) ((wxVariantDataList
*) m_refData
)->GetValue();
2117 void wxVariant::NullList()
2119 SetData(new wxVariantDataList());
2123 void wxVariant::Append(const wxVariant
& value
)
2125 wxVariantList
& list
= GetList();
2127 list
.Append(new wxVariant(value
));
2130 // Insert at front of list
2131 void wxVariant::Insert(const wxVariant
& value
)
2133 wxVariantList
& list
= GetList();
2135 list
.Insert(new wxVariant(value
));
2138 // Returns true if the variant is a member of the list
2139 bool wxVariant::Member(const wxVariant
& value
) const
2141 wxVariantList
& list
= GetList();
2143 wxVariantList::compatibility_iterator node
= list
.GetFirst();
2146 wxVariant
* other
= node
->GetData();
2147 if (value
== *other
)
2149 node
= node
->GetNext();
2154 // Deletes the nth element of the list
2155 bool wxVariant::Delete(size_t item
)
2157 wxVariantList
& list
= GetList();
2159 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
2160 wxVariantList::compatibility_iterator node
= list
.Item(item
);
2161 wxVariant
* variant
= node
->GetData();
2168 void wxVariant::ClearList()
2170 if (!IsNull() && (GetType() == wxT("list")))
2172 ((wxVariantDataList
*) m_refData
)->Clear();
2176 if (!GetType().IsSameAs(wxT("list")))
2179 m_refData
= new wxVariantDataList
;
2183 // Treat a list variant as an array
2184 wxVariant
wxVariant::operator[] (size_t idx
) const
2186 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
2188 if (GetType() == wxT("list"))
2190 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2191 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2192 return *(data
->GetValue().Item(idx
)->GetData());
2194 return wxNullVariant
;
2197 wxVariant
& wxVariant::operator[] (size_t idx
)
2199 // We can't return a reference to a variant for a string list, since the string
2200 // is actually stored as a char*, not a variant.
2202 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
2204 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2205 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2207 return * (data
->GetValue().Item(idx
)->GetData());
2210 // Return the number of elements in a list
2211 size_t wxVariant::GetCount() const
2213 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
2215 if (GetType() == wxT("list"))
2217 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2218 return data
->GetValue().GetCount();
2223 // ----------------------------------------------------------------------------
2225 // ----------------------------------------------------------------------------
2227 bool wxVariant::Convert(long* value
) const
2229 wxString
type(GetType());
2230 if (type
== wxS("double"))
2231 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
2232 else if (type
== wxS("long"))
2233 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2234 else if (type
== wxS("bool"))
2235 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2236 else if (type
== wxS("string"))
2237 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
2239 else if (type
== wxS("longlong"))
2241 wxLongLong v
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2242 // Don't convert if return value would be vague
2243 if ( v
< LONG_MIN
|| v
> LONG_MAX
)
2245 *value
= v
.ToLong();
2247 else if (type
== wxS("ulonglong"))
2249 wxULongLong v
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2250 // Don't convert if return value would be vague
2253 *value
= (long) v
.ToULong();
2262 bool wxVariant::Convert(bool* value
) const
2264 wxString
type(GetType());
2265 if (type
== wxT("double"))
2266 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
2267 else if (type
== wxT("long"))
2268 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2269 else if (type
== wxT("bool"))
2270 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2271 else if (type
== wxT("string"))
2273 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2275 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
2277 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
2288 bool wxVariant::Convert(double* value
) const
2290 wxString
type(GetType());
2291 if (type
== wxT("double"))
2292 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
2293 else if (type
== wxT("long"))
2294 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2295 else if (type
== wxT("bool"))
2296 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2297 else if (type
== wxT("string"))
2298 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
2300 else if (type
== wxS("longlong"))
2302 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue().ToDouble();
2304 else if (type
== wxS("ulonglong"))
2306 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue().ToDouble();
2315 bool wxVariant::Convert(wxUniChar
* value
) const
2317 wxString
type(GetType());
2318 if (type
== wxT("char"))
2319 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2320 else if (type
== wxT("long"))
2321 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2322 else if (type
== wxT("bool"))
2323 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2330 bool wxVariant::Convert(char* value
) const
2333 if ( !Convert(&ch
) )
2339 bool wxVariant::Convert(wchar_t* value
) const
2342 if ( !Convert(&ch
) )
2348 bool wxVariant::Convert(wxString
* value
) const
2350 *value
= MakeString();
2355 bool wxVariant::Convert(wxLongLong
* value
) const
2357 wxString
type(GetType());
2358 if (type
== wxS("longlong"))
2359 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2360 else if (type
== wxS("long"))
2361 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2362 else if (type
== wxS("string"))
2364 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2366 wxLongLong_t value_t
;
2367 if ( !s
.ToLongLong(&value_t
) )
2372 if ( !s
.ToLong(&l_value
) )
2377 else if (type
== wxS("bool"))
2378 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2379 else if (type
== wxS("double"))
2381 value
->Assign(((wxVariantDoubleData
*)GetData())->GetValue());
2383 else if (type
== wxS("ulonglong"))
2384 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2391 bool wxVariant::Convert(wxULongLong
* value
) const
2393 wxString
type(GetType());
2394 if (type
== wxS("ulonglong"))
2395 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2396 else if (type
== wxS("long"))
2397 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2398 else if (type
== wxS("string"))
2400 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2402 wxULongLong_t value_t
;
2403 if ( !s
.ToULongLong(&value_t
) )
2407 unsigned long l_value
;
2408 if ( !s
.ToULong(&l_value
) )
2413 else if (type
== wxS("bool"))
2414 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2415 else if (type
== wxS("double"))
2417 double value_d
= ((wxVariantDoubleData
*)GetData())->GetValue();
2419 if ( value_d
< 0.0 )
2423 *value
= (wxULongLong_t
) value_d
;
2426 temp
.Assign(value_d
);
2430 else if (type
== wxS("longlong"))
2431 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2437 #endif // wxUSE_LONGLONG
2440 bool wxVariant::Convert(wxDateTime
* value
) const
2442 wxString
type(GetType());
2443 if (type
== wxT("datetime"))
2445 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2449 // Fallback to string conversion
2451 if ( !Convert(&val
) )
2454 // Try to parse this as either date and time, only date or only time
2455 // checking that the entire string was parsed
2456 wxString::const_iterator end
;
2457 if ( value
->ParseDateTime(val
, &end
) && end
== val
.end() )
2460 if ( value
->ParseDate(val
, &end
) && end
== val
.end() )
2463 if ( value
->ParseTime(val
, &end
) && end
== val
.end() )
2468 #endif // wxUSE_DATETIME
2470 #endif // wxUSE_VARIANT