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 wxAnyToVariantRegistration::~wxAnyToVariantRegistration()
213 wxVariant::wxVariant(const wxAny
& any
)
217 if ( !any
.GetAs(&variant
) )
219 wxFAIL_MSG("wxAny of this type cannot be converted to wxVariant");
226 wxAny
wxVariant::GetAny() const
232 wxVariantData
* data
= GetData();
234 if ( data
->GetAsAny(&any
) )
237 // If everything else fails, wrap the whole wxVariantData
243 // -----------------------------------------------------------------
245 // -----------------------------------------------------------------
247 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
250 wxVariantDataLong() { m_value
= 0; }
251 wxVariantDataLong(long value
) { m_value
= value
; }
253 inline long GetValue() const { return m_value
; }
254 inline void SetValue(long value
) { m_value
= value
; }
256 virtual bool Eq(wxVariantData
& data
) const;
258 virtual bool Read(wxString
& str
);
259 virtual bool Write(wxString
& str
) const;
260 #if wxUSE_STD_IOSTREAM
261 virtual bool Read(wxSTD istream
& str
);
262 virtual bool Write(wxSTD ostream
& str
) const;
265 virtual bool Read(wxInputStream
& str
);
266 virtual bool Write(wxOutputStream
&str
) const;
267 #endif // wxUSE_STREAMS
269 wxVariantData
* Clone() const { return new wxVariantDataLong(m_value
); }
271 virtual wxString
GetType() const { return wxT("long"); }
273 // Since wxAny does not have separate type for integers shorter than
274 // longlong, we do not usually implement wxVariant->wxAny conversion
275 // here (but in wxVariantDataLongLong instead).
277 DECLARE_WXANY_CONVERSION()
279 bool GetAsAny(wxAny
* any
) const
291 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(long, wxVariantDataLong
)
294 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
296 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
298 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
300 return (otherData
.m_value
== m_value
);
303 #if wxUSE_STD_IOSTREAM
304 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
308 str
<< (const char*) s
.mb_str();
313 bool wxVariantDataLong::Write(wxString
& str
) const
315 str
.Printf(wxT("%ld"), m_value
);
319 #if wxUSE_STD_IOSTREAM
320 bool wxVariantDataLong::Read(wxSTD istream
& str
)
328 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
330 wxTextOutputStream
s(str
);
332 s
.Write32((size_t)m_value
);
336 bool wxVariantDataLong::Read(wxInputStream
& str
)
338 wxTextInputStream
s(str
);
339 m_value
= s
.Read32();
342 #endif // wxUSE_STREAMS
344 bool wxVariantDataLong::Read(wxString
& str
)
346 m_value
= wxAtol(str
);
352 wxVariant::wxVariant(long val
, const wxString
& name
)
354 m_refData
= new wxVariantDataLong(val
);
358 wxVariant::wxVariant(int val
, const wxString
& name
)
360 m_refData
= new wxVariantDataLong((long)val
);
364 wxVariant::wxVariant(short val
, const wxString
& name
)
366 m_refData
= new wxVariantDataLong((long)val
);
370 bool wxVariant::operator== (long value
) const
373 if (!Convert(&thisValue
))
376 return (value
== thisValue
);
379 bool wxVariant::operator!= (long value
) const
381 return (!((*this) == value
));
384 void wxVariant::operator= (long value
)
386 if (GetType() == wxT("long") &&
387 m_refData
->GetRefCount() == 1)
389 ((wxVariantDataLong
*)GetData())->SetValue(value
);
394 m_refData
= new wxVariantDataLong(value
);
398 long wxVariant::GetLong() const
401 if (Convert(& value
))
405 wxFAIL_MSG(wxT("Could not convert to a long"));
410 // -----------------------------------------------------------------
411 // wxVariantDoubleData
412 // -----------------------------------------------------------------
414 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
417 wxVariantDoubleData() { m_value
= 0.0; }
418 wxVariantDoubleData(double value
) { m_value
= value
; }
420 inline double GetValue() const { return m_value
; }
421 inline void SetValue(double value
) { m_value
= value
; }
423 virtual bool Eq(wxVariantData
& data
) const;
424 virtual bool Read(wxString
& str
);
425 #if wxUSE_STD_IOSTREAM
426 virtual bool Write(wxSTD ostream
& str
) const;
428 virtual bool Write(wxString
& str
) const;
429 #if wxUSE_STD_IOSTREAM
430 virtual bool Read(wxSTD istream
& str
);
433 virtual bool Read(wxInputStream
& str
);
434 virtual bool Write(wxOutputStream
&str
) const;
435 #endif // wxUSE_STREAMS
436 virtual wxString
GetType() const { return wxT("double"); }
438 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
440 DECLARE_WXANY_CONVERSION()
445 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(double, wxVariantDoubleData
)
447 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
449 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
451 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
453 return wxIsSameDouble(otherData
.m_value
, m_value
);
456 #if wxUSE_STD_IOSTREAM
457 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
461 str
<< (const char*) s
.mb_str();
466 bool wxVariantDoubleData::Write(wxString
& str
) const
468 str
.Printf(wxT("%.14g"), m_value
);
472 #if wxUSE_STD_IOSTREAM
473 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
481 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
483 wxTextOutputStream
s(str
);
484 s
.WriteDouble((double)m_value
);
488 bool wxVariantDoubleData::Read(wxInputStream
& str
)
490 wxTextInputStream
s(str
);
491 m_value
= (float)s
.ReadDouble();
494 #endif // wxUSE_STREAMS
496 bool wxVariantDoubleData::Read(wxString
& str
)
498 m_value
= wxAtof(str
);
502 // wxVariant double code
504 wxVariant::wxVariant(double val
, const wxString
& name
)
506 m_refData
= new wxVariantDoubleData(val
);
510 bool wxVariant::operator== (double value
) const
513 if (!Convert(&thisValue
))
516 return wxIsSameDouble(value
, thisValue
);
519 bool wxVariant::operator!= (double value
) const
521 return (!((*this) == value
));
524 void wxVariant::operator= (double value
)
526 if (GetType() == wxT("double") &&
527 m_refData
->GetRefCount() == 1)
529 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
534 m_refData
= new wxVariantDoubleData(value
);
538 double wxVariant::GetDouble() const
541 if (Convert(& value
))
545 wxFAIL_MSG(wxT("Could not convert to a double number"));
550 // -----------------------------------------------------------------
552 // -----------------------------------------------------------------
554 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
557 wxVariantDataBool() { m_value
= 0; }
558 wxVariantDataBool(bool value
) { m_value
= value
; }
560 inline bool GetValue() const { return m_value
; }
561 inline void SetValue(bool value
) { m_value
= value
; }
563 virtual bool Eq(wxVariantData
& data
) const;
564 #if wxUSE_STD_IOSTREAM
565 virtual bool Write(wxSTD ostream
& str
) const;
567 virtual bool Write(wxString
& str
) const;
568 virtual bool Read(wxString
& str
);
569 #if wxUSE_STD_IOSTREAM
570 virtual bool Read(wxSTD istream
& str
);
573 virtual bool Read(wxInputStream
& str
);
574 virtual bool Write(wxOutputStream
& str
) const;
575 #endif // wxUSE_STREAMS
576 virtual wxString
GetType() const { return wxT("bool"); }
578 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
580 DECLARE_WXANY_CONVERSION()
585 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(bool, wxVariantDataBool
)
587 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
589 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
591 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
593 return (otherData
.m_value
== m_value
);
596 #if wxUSE_STD_IOSTREAM
597 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
601 str
<< (const char*) s
.mb_str();
606 bool wxVariantDataBool::Write(wxString
& str
) const
608 str
.Printf(wxT("%d"), (int) m_value
);
612 #if wxUSE_STD_IOSTREAM
613 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
615 wxFAIL_MSG(wxT("Unimplemented"));
616 // str >> (long) m_value;
622 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
624 wxTextOutputStream
s(str
);
630 bool wxVariantDataBool::Read(wxInputStream
& str
)
632 wxTextInputStream
s(str
);
634 m_value
= s
.Read8() != 0;
637 #endif // wxUSE_STREAMS
639 bool wxVariantDataBool::Read(wxString
& str
)
641 m_value
= (wxAtol(str
) != 0);
647 wxVariant::wxVariant(bool val
, const wxString
& name
)
649 m_refData
= new wxVariantDataBool(val
);
653 bool wxVariant::operator== (bool value
) const
656 if (!Convert(&thisValue
))
659 return (value
== thisValue
);
662 bool wxVariant::operator!= (bool value
) const
664 return (!((*this) == value
));
667 void wxVariant::operator= (bool value
)
669 if (GetType() == wxT("bool") &&
670 m_refData
->GetRefCount() == 1)
672 ((wxVariantDataBool
*)GetData())->SetValue(value
);
677 m_refData
= new wxVariantDataBool(value
);
681 bool wxVariant::GetBool() const
684 if (Convert(& value
))
688 wxFAIL_MSG(wxT("Could not convert to a bool"));
693 // -----------------------------------------------------------------
695 // -----------------------------------------------------------------
697 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
700 wxVariantDataChar() { m_value
= 0; }
701 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
703 inline wxUniChar
GetValue() const { return m_value
; }
704 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
706 virtual bool Eq(wxVariantData
& data
) const;
707 #if wxUSE_STD_IOSTREAM
708 virtual bool Read(wxSTD istream
& str
);
709 virtual bool Write(wxSTD ostream
& str
) const;
711 virtual bool Read(wxString
& str
);
712 virtual bool Write(wxString
& str
) const;
714 virtual bool Read(wxInputStream
& str
);
715 virtual bool Write(wxOutputStream
& str
) const;
716 #endif // wxUSE_STREAMS
717 virtual wxString
GetType() const { return wxT("char"); }
718 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
720 DECLARE_WXANY_CONVERSION()
725 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxUniChar
, wxVariantDataChar
)
727 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
729 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
731 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
733 return (otherData
.m_value
== m_value
);
736 #if wxUSE_STD_IOSTREAM
737 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
739 str
<< wxString(m_value
);
744 bool wxVariantDataChar::Write(wxString
& str
) const
750 #if wxUSE_STD_IOSTREAM
751 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
753 wxFAIL_MSG(wxT("Unimplemented"));
760 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
762 wxTextOutputStream
s(str
);
764 // FIXME-UTF8: this should be just "s << m_value;" after removal of
765 // ANSI build and addition of wxUniChar to wxTextOutputStream:
766 s
<< (wxChar
)m_value
;
771 bool wxVariantDataChar::Read(wxInputStream
& str
)
773 wxTextInputStream
s(str
);
775 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
776 // ANSI build and addition of wxUniChar to wxTextInputStream:
783 #endif // wxUSE_STREAMS
785 bool wxVariantDataChar::Read(wxString
& str
)
791 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
793 m_refData
= new wxVariantDataChar(val
);
797 wxVariant::wxVariant(char val
, const wxString
& name
)
799 m_refData
= new wxVariantDataChar(val
);
803 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
805 m_refData
= new wxVariantDataChar(val
);
809 bool wxVariant::operator==(const wxUniChar
& value
) const
812 if (!Convert(&thisValue
))
815 return (value
== thisValue
);
818 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
820 if (GetType() == wxT("char") &&
821 m_refData
->GetRefCount() == 1)
823 ((wxVariantDataChar
*)GetData())->SetValue(value
);
828 m_refData
= new wxVariantDataChar(value
);
834 wxUniChar
wxVariant::GetChar() const
837 if (Convert(& value
))
841 wxFAIL_MSG(wxT("Could not convert to a char"));
846 // ----------------------------------------------------------------------------
847 // wxVariantDataString
848 // ----------------------------------------------------------------------------
850 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
853 wxVariantDataString() { }
854 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
856 inline wxString
GetValue() const { return m_value
; }
857 inline void SetValue(const wxString
& value
) { m_value
= value
; }
859 virtual bool Eq(wxVariantData
& data
) const;
860 #if wxUSE_STD_IOSTREAM
861 virtual bool Write(wxSTD ostream
& str
) const;
863 virtual bool Read(wxString
& str
);
864 virtual bool Write(wxString
& str
) const;
865 #if wxUSE_STD_IOSTREAM
866 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
869 virtual bool Read(wxInputStream
& str
);
870 virtual bool Write(wxOutputStream
& str
) const;
871 #endif // wxUSE_STREAMS
872 virtual wxString
GetType() const { return wxT("string"); }
873 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
875 DECLARE_WXANY_CONVERSION()
880 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxString
, wxVariantDataString
)
883 // This allows converting string literal wxAnys to string variants
884 wxVariantData
* wxVariantDataFromConstCharPAny(const wxAny
& any
)
886 return new wxVariantDataString(wxANY_AS(any
, const char*));
889 wxVariantData
* wxVariantDataFromConstWchar_tPAny(const wxAny
& any
)
891 return new wxVariantDataString(wxANY_AS(any
, const wchar_t*));
894 _REGISTER_WXANY_CONVERSION(const char*,
896 wxVariantDataFromConstCharPAny
)
897 _REGISTER_WXANY_CONVERSION(const wchar_t*,
899 wxVariantDataFromConstWchar_tPAny
)
902 bool wxVariantDataString::Eq(wxVariantData
& data
) const
904 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
906 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
908 return (otherData
.m_value
== m_value
);
911 #if wxUSE_STD_IOSTREAM
912 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
914 str
<< (const char*) m_value
.mb_str();
919 bool wxVariantDataString::Write(wxString
& str
) const
926 bool wxVariantDataString::Write(wxOutputStream
& str
) const
928 // why doesn't wxOutputStream::operator<< take "const wxString&"
929 wxTextOutputStream
s(str
);
930 s
.WriteString(m_value
);
934 bool wxVariantDataString::Read(wxInputStream
& str
)
936 wxTextInputStream
s(str
);
938 m_value
= s
.ReadLine();
941 #endif // wxUSE_STREAMS
943 bool wxVariantDataString::Read(wxString
& str
)
951 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
953 m_refData
= new wxVariantDataString(val
);
957 wxVariant::wxVariant(const char* val
, const wxString
& name
)
959 m_refData
= new wxVariantDataString(wxString(val
));
963 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
965 m_refData
= new wxVariantDataString(wxString(val
));
969 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
971 m_refData
= new wxVariantDataString(val
.AsString());
975 wxVariant::wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
)
977 m_refData
= new wxVariantDataString(wxString(val
));
981 wxVariant::wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
)
983 m_refData
= new wxVariantDataString(wxString(val
));
987 bool wxVariant::operator== (const wxString
& value
) const
990 if (!Convert(&thisValue
))
993 return value
== thisValue
;
996 bool wxVariant::operator!= (const wxString
& value
) const
998 return (!((*this) == value
));
1001 wxVariant
& wxVariant::operator= (const wxString
& value
)
1003 if (GetType() == wxT("string") &&
1004 m_refData
->GetRefCount() == 1)
1006 ((wxVariantDataString
*)GetData())->SetValue(value
);
1011 m_refData
= new wxVariantDataString(value
);
1016 wxString
wxVariant::GetString() const
1019 if (!Convert(& value
))
1021 wxFAIL_MSG(wxT("Could not convert to a string"));
1027 // ----------------------------------------------------------------------------
1028 // wxVariantDataWxObjectPtr
1029 // ----------------------------------------------------------------------------
1031 class wxVariantDataWxObjectPtr
: public wxVariantData
1034 wxVariantDataWxObjectPtr() { }
1035 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
1037 inline wxObject
* GetValue() const { return m_value
; }
1038 inline void SetValue(wxObject
* value
) { m_value
= value
; }
1040 virtual bool Eq(wxVariantData
& data
) const;
1041 #if wxUSE_STD_IOSTREAM
1042 virtual bool Write(wxSTD ostream
& str
) const;
1044 virtual bool Write(wxString
& str
) const;
1045 #if wxUSE_STD_IOSTREAM
1046 virtual bool Read(wxSTD istream
& str
);
1048 virtual bool Read(wxString
& str
);
1049 virtual wxString
GetType() const ;
1050 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
1052 virtual wxClassInfo
* GetValueClassInfo();
1054 DECLARE_WXANY_CONVERSION()
1059 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxObject
*, wxVariantDataWxObjectPtr
)
1061 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
1063 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1065 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1067 return (otherData
.m_value
== m_value
);
1070 wxString
wxVariantDataWxObjectPtr::GetType() const
1072 wxString
returnVal(wxT("wxObject*"));
1076 returnVal
= m_value
->GetClassInfo()->GetClassName();
1077 returnVal
+= wxT("*");
1083 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1085 wxClassInfo
* returnVal
=NULL
;
1087 if (m_value
) returnVal
= m_value
->GetClassInfo();
1092 #if wxUSE_STD_IOSTREAM
1093 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1097 str
<< (const char*) s
.mb_str();
1102 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1104 str
.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value
));
1108 #if wxUSE_STD_IOSTREAM
1109 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1116 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1124 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1126 m_refData
= new wxVariantDataWxObjectPtr(val
);
1130 bool wxVariant::operator== (wxObject
* value
) const
1132 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1135 bool wxVariant::operator!= (wxObject
* value
) const
1137 return (!((*this) == (wxObject
*) value
));
1140 void wxVariant::operator= (wxObject
* value
)
1143 m_refData
= new wxVariantDataWxObjectPtr(value
);
1146 wxObject
* wxVariant::GetWxObjectPtr() const
1148 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_refData
)->GetValue();
1151 // ----------------------------------------------------------------------------
1152 // wxVariantDataVoidPtr
1153 // ----------------------------------------------------------------------------
1155 class wxVariantDataVoidPtr
: public wxVariantData
1158 wxVariantDataVoidPtr() { }
1159 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1161 inline void* GetValue() const { return m_value
; }
1162 inline void SetValue(void* value
) { m_value
= value
; }
1164 virtual bool Eq(wxVariantData
& data
) const;
1165 #if wxUSE_STD_IOSTREAM
1166 virtual bool Write(wxSTD ostream
& str
) const;
1168 virtual bool Write(wxString
& str
) const;
1169 #if wxUSE_STD_IOSTREAM
1170 virtual bool Read(wxSTD istream
& str
);
1172 virtual bool Read(wxString
& str
);
1173 virtual wxString
GetType() const { return wxT("void*"); }
1174 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1176 DECLARE_WXANY_CONVERSION()
1181 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(void*, wxVariantDataVoidPtr
)
1183 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1185 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1187 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1189 return (otherData
.m_value
== m_value
);
1192 #if wxUSE_STD_IOSTREAM
1193 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1197 str
<< (const char*) s
.mb_str();
1202 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1204 str
.Printf(wxT("%p"), m_value
);
1208 #if wxUSE_STD_IOSTREAM
1209 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1216 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1224 wxVariant::wxVariant( void* val
, const wxString
& name
)
1226 m_refData
= new wxVariantDataVoidPtr(val
);
1230 bool wxVariant::operator== (void* value
) const
1232 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1235 bool wxVariant::operator!= (void* value
) const
1237 return (!((*this) == (void*) value
));
1240 void wxVariant::operator= (void* value
)
1242 if (GetType() == wxT("void*") && (m_refData
->GetRefCount() == 1))
1244 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1249 m_refData
= new wxVariantDataVoidPtr(value
);
1253 void* wxVariant::GetVoidPtr() const
1255 // handling this specially is convenient when working with COM, see #9873
1259 wxASSERT( GetType() == wxT("void*") );
1261 return (void*) ((wxVariantDataVoidPtr
*) m_refData
)->GetValue();
1264 // ----------------------------------------------------------------------------
1265 // wxVariantDataDateTime
1266 // ----------------------------------------------------------------------------
1270 class wxVariantDataDateTime
: public wxVariantData
1273 wxVariantDataDateTime() { }
1274 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1276 inline wxDateTime
GetValue() const { return m_value
; }
1277 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1279 virtual bool Eq(wxVariantData
& data
) const;
1280 #if wxUSE_STD_IOSTREAM
1281 virtual bool Write(wxSTD ostream
& str
) const;
1283 virtual bool Write(wxString
& str
) const;
1284 #if wxUSE_STD_IOSTREAM
1285 virtual bool Read(wxSTD istream
& str
);
1287 virtual bool Read(wxString
& str
);
1288 virtual wxString
GetType() const { return wxT("datetime"); }
1289 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1291 DECLARE_WXANY_CONVERSION()
1296 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxDateTime
, wxVariantDataDateTime
)
1298 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1300 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1302 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1304 return (otherData
.m_value
== m_value
);
1308 #if wxUSE_STD_IOSTREAM
1309 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1313 str
<< value
.c_str();
1319 bool wxVariantDataDateTime::Write(wxString
& str
) const
1321 if ( m_value
.IsValid() )
1322 str
= m_value
.Format();
1324 str
= wxS("Invalid");
1329 #if wxUSE_STD_IOSTREAM
1330 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1338 bool wxVariantDataDateTime::Read(wxString
& str
)
1340 if ( str
== wxS("Invalid") )
1342 m_value
= wxInvalidDateTime
;
1346 wxString::const_iterator end
;
1347 return m_value
.ParseDateTime(str
, &end
) && end
== str
.end();
1352 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1354 m_refData
= new wxVariantDataDateTime(val
);
1358 bool wxVariant::operator== (const wxDateTime
& value
) const
1360 wxDateTime thisValue
;
1361 if (!Convert(&thisValue
))
1364 return value
.IsEqualTo(thisValue
);
1367 bool wxVariant::operator!= (const wxDateTime
& value
) const
1369 return (!((*this) == value
));
1372 void wxVariant::operator= (const wxDateTime
& value
)
1374 if (GetType() == wxT("datetime") &&
1375 m_refData
->GetRefCount() == 1)
1377 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1382 m_refData
= new wxVariantDataDateTime(value
);
1386 wxDateTime
wxVariant::GetDateTime() const
1389 if (!Convert(& value
))
1391 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1397 #endif // wxUSE_DATETIME
1399 // ----------------------------------------------------------------------------
1400 // wxVariantDataArrayString
1401 // ----------------------------------------------------------------------------
1403 class wxVariantDataArrayString
: public wxVariantData
1406 wxVariantDataArrayString() { }
1407 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1409 wxArrayString
GetValue() const { return m_value
; }
1410 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1412 virtual bool Eq(wxVariantData
& data
) const;
1413 #if wxUSE_STD_IOSTREAM
1414 virtual bool Write(wxSTD ostream
& str
) const;
1416 virtual bool Write(wxString
& str
) const;
1417 #if wxUSE_STD_IOSTREAM
1418 virtual bool Read(wxSTD istream
& str
);
1420 virtual bool Read(wxString
& str
);
1421 virtual wxString
GetType() const { return wxT("arrstring"); }
1422 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1424 DECLARE_WXANY_CONVERSION()
1426 wxArrayString m_value
;
1429 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxArrayString
, wxVariantDataArrayString
)
1431 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1433 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1435 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1437 return otherData
.m_value
== m_value
;
1440 #if wxUSE_STD_IOSTREAM
1441 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1448 bool wxVariantDataArrayString::Write(wxString
& str
) const
1450 size_t count
= m_value
.GetCount();
1451 for ( size_t n
= 0; n
< count
; n
++ )
1463 #if wxUSE_STD_IOSTREAM
1464 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1472 bool wxVariantDataArrayString::Read(wxString
& str
)
1474 wxStringTokenizer
tk(str
, wxT(";"));
1475 while ( tk
.HasMoreTokens() )
1477 m_value
.Add(tk
.GetNextToken());
1485 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1487 m_refData
= new wxVariantDataArrayString(val
);
1491 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1493 wxFAIL_MSG( wxT("TODO") );
1498 bool wxVariant::operator!=(const wxArrayString
& value
) const
1500 return !(*this == value
);
1503 void wxVariant::operator=(const wxArrayString
& value
)
1505 if (GetType() == wxT("arrstring") &&
1506 m_refData
->GetRefCount() == 1)
1508 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1513 m_refData
= new wxVariantDataArrayString(value
);
1517 wxArrayString
wxVariant::GetArrayString() const
1519 if ( GetType() == wxT("arrstring") )
1520 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1522 return wxArrayString();
1525 // ----------------------------------------------------------------------------
1526 // wxVariantDataLongLong
1527 // ----------------------------------------------------------------------------
1531 class WXDLLIMPEXP_BASE wxVariantDataLongLong
: public wxVariantData
1534 wxVariantDataLongLong() { m_value
= 0; }
1535 wxVariantDataLongLong(wxLongLong value
) { m_value
= value
; }
1537 wxLongLong
GetValue() const { return m_value
; }
1538 void SetValue(wxLongLong value
) { m_value
= value
; }
1540 virtual bool Eq(wxVariantData
& data
) const;
1542 virtual bool Read(wxString
& str
);
1543 virtual bool Write(wxString
& str
) const;
1544 #if wxUSE_STD_IOSTREAM
1545 virtual bool Read(wxSTD istream
& str
);
1546 virtual bool Write(wxSTD ostream
& str
) const;
1549 virtual bool Read(wxInputStream
& str
);
1550 virtual bool Write(wxOutputStream
&str
) const;
1551 #endif // wxUSE_STREAMS
1553 wxVariantData
* Clone() const
1555 return new wxVariantDataLongLong(m_value
);
1558 virtual wxString
GetType() const { return wxS("longlong"); }
1560 DECLARE_WXANY_CONVERSION()
1566 // wxLongLong type requires customized wxAny conversion code
1571 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1573 *any
= m_value
.GetValue();
1577 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1579 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong_t
));
1582 REGISTER_WXANY_CONVERSION(wxLongLong_t
, wxVariantDataLongLong
)
1584 #else // if !defined(wxLongLong_t)
1586 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1592 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1594 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong
));
1597 REGISTER_WXANY_CONVERSION(wxLongLong
, wxVariantDataLongLong
)
1599 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1602 bool wxVariantDataLongLong::Eq(wxVariantData
& data
) const
1604 wxASSERT_MSG( (data
.GetType() == wxS("longlong")),
1605 "wxVariantDataLongLong::Eq: argument mismatch" );
1607 wxVariantDataLongLong
& otherData
= (wxVariantDataLongLong
&) data
;
1609 return (otherData
.m_value
== m_value
);
1612 #if wxUSE_STD_IOSTREAM
1613 bool wxVariantDataLongLong::Write(wxSTD ostream
& str
) const
1617 str
<< (const char*) s
.mb_str();
1622 bool wxVariantDataLongLong::Write(wxString
& str
) const
1625 str
.Printf(wxS("%lld"), m_value
.GetValue());
1632 #if wxUSE_STD_IOSTREAM
1633 bool wxVariantDataLongLong::Read(wxSTD istream
& WXUNUSED(str
))
1635 wxFAIL_MSG(wxS("Unimplemented"));
1641 bool wxVariantDataLongLong::Write(wxOutputStream
& str
) const
1643 wxTextOutputStream
s(str
);
1644 s
.Write32(m_value
.GetLo());
1645 s
.Write32(m_value
.GetHi());
1649 bool wxVariantDataLongLong::Read(wxInputStream
& str
)
1651 wxTextInputStream
s(str
);
1652 unsigned long lo
= s
.Read32();
1653 long hi
= s
.Read32();
1654 m_value
= wxLongLong(hi
, lo
);
1657 #endif // wxUSE_STREAMS
1659 bool wxVariantDataLongLong::Read(wxString
& str
)
1662 wxLongLong_t value_t
;
1663 if ( !str
.ToLongLong(&value_t
) )
1674 wxVariant::wxVariant(wxLongLong val
, const wxString
& name
)
1676 m_refData
= new wxVariantDataLongLong(val
);
1680 bool wxVariant::operator==(wxLongLong value
) const
1682 wxLongLong thisValue
;
1683 if ( !Convert(&thisValue
) )
1686 return (value
== thisValue
);
1689 bool wxVariant::operator!=(wxLongLong value
) const
1691 return (!((*this) == value
));
1694 void wxVariant::operator=(wxLongLong value
)
1696 if ( GetType() == wxS("longlong") &&
1697 m_refData
->GetRefCount() == 1 )
1699 ((wxVariantDataLongLong
*)GetData())->SetValue(value
);
1704 m_refData
= new wxVariantDataLongLong(value
);
1708 wxLongLong
wxVariant::GetLongLong() const
1711 if ( Convert(&value
) )
1717 wxFAIL_MSG(wxT("Could not convert to a long long"));
1722 #endif // wxUSE_LONGLONG
1724 // ----------------------------------------------------------------------------
1725 // wxVariantDataULongLong
1726 // ----------------------------------------------------------------------------
1730 class WXDLLIMPEXP_BASE wxVariantDataULongLong
: public wxVariantData
1733 wxVariantDataULongLong() { m_value
= 0; }
1734 wxVariantDataULongLong(wxULongLong value
) { m_value
= value
; }
1736 wxULongLong
GetValue() const { return m_value
; }
1737 void SetValue(wxULongLong value
) { m_value
= value
; }
1739 virtual bool Eq(wxVariantData
& data
) const;
1741 virtual bool Read(wxString
& str
);
1742 virtual bool Write(wxString
& str
) const;
1743 #if wxUSE_STD_IOSTREAM
1744 virtual bool Read(wxSTD istream
& str
);
1745 virtual bool Write(wxSTD ostream
& str
) const;
1748 virtual bool Read(wxInputStream
& str
);
1749 virtual bool Write(wxOutputStream
&str
) const;
1750 #endif // wxUSE_STREAMS
1752 wxVariantData
* Clone() const
1754 return new wxVariantDataULongLong(m_value
);
1757 virtual wxString
GetType() const { return wxS("ulonglong"); }
1759 DECLARE_WXANY_CONVERSION()
1761 wxULongLong m_value
;
1765 // wxULongLong type requires customized wxAny conversion code
1770 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1772 *any
= m_value
.GetValue();
1776 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1778 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong_t
));
1781 REGISTER_WXANY_CONVERSION(wxULongLong_t
, wxVariantDataULongLong
)
1783 #else // if !defined(wxLongLong_t)
1785 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1791 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1793 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong
));
1796 REGISTER_WXANY_CONVERSION(wxULongLong
, wxVariantDataULongLong
)
1798 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1802 bool wxVariantDataULongLong::Eq(wxVariantData
& data
) const
1804 wxASSERT_MSG( (data
.GetType() == wxS("ulonglong")),
1805 "wxVariantDataULongLong::Eq: argument mismatch" );
1807 wxVariantDataULongLong
& otherData
= (wxVariantDataULongLong
&) data
;
1809 return (otherData
.m_value
== m_value
);
1812 #if wxUSE_STD_IOSTREAM
1813 bool wxVariantDataULongLong::Write(wxSTD ostream
& str
) const
1817 str
<< (const char*) s
.mb_str();
1822 bool wxVariantDataULongLong::Write(wxString
& str
) const
1825 str
.Printf(wxS("%llu"), m_value
.GetValue());
1832 #if wxUSE_STD_IOSTREAM
1833 bool wxVariantDataULongLong::Read(wxSTD istream
& WXUNUSED(str
))
1835 wxFAIL_MSG(wxS("Unimplemented"));
1841 bool wxVariantDataULongLong::Write(wxOutputStream
& str
) const
1843 wxTextOutputStream
s(str
);
1844 s
.Write32(m_value
.GetLo());
1845 s
.Write32(m_value
.GetHi());
1849 bool wxVariantDataULongLong::Read(wxInputStream
& str
)
1851 wxTextInputStream
s(str
);
1852 unsigned long lo
= s
.Read32();
1853 long hi
= s
.Read32();
1854 m_value
= wxULongLong(hi
, lo
);
1857 #endif // wxUSE_STREAMS
1859 bool wxVariantDataULongLong::Read(wxString
& str
)
1862 wxULongLong_t value_t
;
1863 if ( !str
.ToULongLong(&value_t
) )
1874 wxVariant::wxVariant(wxULongLong val
, const wxString
& name
)
1876 m_refData
= new wxVariantDataULongLong(val
);
1880 bool wxVariant::operator==(wxULongLong value
) const
1882 wxULongLong thisValue
;
1883 if ( !Convert(&thisValue
) )
1886 return (value
== thisValue
);
1889 bool wxVariant::operator!=(wxULongLong value
) const
1891 return (!((*this) == value
));
1894 void wxVariant::operator=(wxULongLong value
)
1896 if ( GetType() == wxS("ulonglong") &&
1897 m_refData
->GetRefCount() == 1 )
1899 ((wxVariantDataULongLong
*)GetData())->SetValue(value
);
1904 m_refData
= new wxVariantDataULongLong(value
);
1908 wxULongLong
wxVariant::GetULongLong() const
1911 if ( Convert(&value
) )
1917 wxFAIL_MSG(wxT("Could not convert to a long long"));
1922 #endif // wxUSE_LONGLONG
1924 // ----------------------------------------------------------------------------
1925 // wxVariantDataList
1926 // ----------------------------------------------------------------------------
1928 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1931 wxVariantDataList() {}
1932 wxVariantDataList(const wxVariantList
& list
);
1933 virtual ~wxVariantDataList();
1935 wxVariantList
& GetValue() { return m_value
; }
1936 void SetValue(const wxVariantList
& value
) ;
1938 virtual bool Eq(wxVariantData
& data
) const;
1939 #if wxUSE_STD_IOSTREAM
1940 virtual bool Write(wxSTD ostream
& str
) const;
1942 virtual bool Write(wxString
& str
) const;
1943 #if wxUSE_STD_IOSTREAM
1944 virtual bool Read(wxSTD istream
& str
);
1946 virtual bool Read(wxString
& str
);
1947 virtual wxString
GetType() const { return wxT("list"); }
1951 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1953 DECLARE_WXANY_CONVERSION()
1955 wxVariantList m_value
;
1961 // Convert to/from list of wxAnys
1964 WX_DEFINE_LIST(wxAnyList
)
1966 bool wxVariantDataList::GetAsAny(wxAny
* any
) const
1969 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1972 wxVariant
* pVar
= node
->GetData();
1973 dst
.push_back(new wxAny(((const wxVariant
&)*pVar
)));
1974 node
= node
->GetNext();
1981 wxVariantData
* wxVariantDataList::VariantDataFactory(const wxAny
& any
)
1983 wxAnyList src
= wxANY_AS(any
, wxAnyList
);
1985 wxAnyList::compatibility_iterator node
= src
.GetFirst();
1988 wxAny
* pAny
= node
->GetData();
1989 dst
.push_back(new wxVariant(*pAny
));
1990 node
= node
->GetNext();
1993 return new wxVariantDataList(dst
);
1996 REGISTER_WXANY_CONVERSION(wxAnyList
, wxVariantDataList
)
2000 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
2005 wxVariantDataList::~wxVariantDataList()
2010 void wxVariantDataList::SetValue(const wxVariantList
& value
)
2013 wxVariantList::compatibility_iterator node
= value
.GetFirst();
2016 wxVariant
* var
= node
->GetData();
2017 m_value
.Append(new wxVariant(*var
));
2018 node
= node
->GetNext();
2022 void wxVariantDataList::Clear()
2024 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2027 wxVariant
* var
= node
->GetData();
2029 node
= node
->GetNext();
2034 bool wxVariantDataList::Eq(wxVariantData
& data
) const
2036 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
2038 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
2039 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
2040 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
2041 while (node1
&& node2
)
2043 wxVariant
* var1
= node1
->GetData();
2044 wxVariant
* var2
= node2
->GetData();
2045 if ((*var1
) != (*var2
))
2047 node1
= node1
->GetNext();
2048 node2
= node2
->GetNext();
2050 if (node1
|| node2
) return false;
2054 #if wxUSE_STD_IOSTREAM
2055 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
2059 str
<< (const char*) s
.mb_str();
2064 bool wxVariantDataList::Write(wxString
& str
) const
2066 str
= wxEmptyString
;
2067 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2070 wxVariant
* var
= node
->GetData();
2071 if (node
!= m_value
.GetFirst())
2074 str
+= var
->MakeString();
2075 node
= node
->GetNext();
2081 #if wxUSE_STD_IOSTREAM
2082 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
2084 wxFAIL_MSG(wxT("Unimplemented"));
2090 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
2092 wxFAIL_MSG(wxT("Unimplemented"));
2099 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
2101 m_refData
= new wxVariantDataList(val
);
2105 bool wxVariant::operator== (const wxVariantList
& value
) const
2107 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
2109 wxVariantDataList
other(value
);
2110 return (GetData()->Eq(other
));
2113 bool wxVariant::operator!= (const wxVariantList
& value
) const
2115 return (!((*this) == value
));
2118 void wxVariant::operator= (const wxVariantList
& value
)
2120 if (GetType() == wxT("list") &&
2121 m_refData
->GetRefCount() == 1)
2123 ((wxVariantDataList
*)GetData())->SetValue(value
);
2128 m_refData
= new wxVariantDataList(value
);
2132 wxVariantList
& wxVariant::GetList() const
2134 wxASSERT( (GetType() == wxT("list")) );
2136 return (wxVariantList
&) ((wxVariantDataList
*) m_refData
)->GetValue();
2140 void wxVariant::NullList()
2142 SetData(new wxVariantDataList());
2146 void wxVariant::Append(const wxVariant
& value
)
2148 wxVariantList
& list
= GetList();
2150 list
.Append(new wxVariant(value
));
2153 // Insert at front of list
2154 void wxVariant::Insert(const wxVariant
& value
)
2156 wxVariantList
& list
= GetList();
2158 list
.Insert(new wxVariant(value
));
2161 // Returns true if the variant is a member of the list
2162 bool wxVariant::Member(const wxVariant
& value
) const
2164 wxVariantList
& list
= GetList();
2166 wxVariantList::compatibility_iterator node
= list
.GetFirst();
2169 wxVariant
* other
= node
->GetData();
2170 if (value
== *other
)
2172 node
= node
->GetNext();
2177 // Deletes the nth element of the list
2178 bool wxVariant::Delete(size_t item
)
2180 wxVariantList
& list
= GetList();
2182 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
2183 wxVariantList::compatibility_iterator node
= list
.Item(item
);
2184 wxVariant
* variant
= node
->GetData();
2191 void wxVariant::ClearList()
2193 if (!IsNull() && (GetType() == wxT("list")))
2195 ((wxVariantDataList
*) m_refData
)->Clear();
2199 if (!GetType().IsSameAs(wxT("list")))
2202 m_refData
= new wxVariantDataList
;
2206 // Treat a list variant as an array
2207 wxVariant
wxVariant::operator[] (size_t idx
) const
2209 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
2211 if (GetType() == wxT("list"))
2213 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2214 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2215 return *(data
->GetValue().Item(idx
)->GetData());
2217 return wxNullVariant
;
2220 wxVariant
& wxVariant::operator[] (size_t idx
)
2222 // We can't return a reference to a variant for a string list, since the string
2223 // is actually stored as a char*, not a variant.
2225 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
2227 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2228 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2230 return * (data
->GetValue().Item(idx
)->GetData());
2233 // Return the number of elements in a list
2234 size_t wxVariant::GetCount() const
2236 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
2238 if (GetType() == wxT("list"))
2240 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2241 return data
->GetValue().GetCount();
2246 // ----------------------------------------------------------------------------
2248 // ----------------------------------------------------------------------------
2250 bool wxVariant::Convert(long* value
) const
2252 wxString
type(GetType());
2253 if (type
== wxS("double"))
2254 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
2255 else if (type
== wxS("long"))
2256 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2257 else if (type
== wxS("bool"))
2258 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2259 else if (type
== wxS("string"))
2260 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
2262 else if (type
== wxS("longlong"))
2264 wxLongLong v
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2265 // Don't convert if return value would be vague
2266 if ( v
< LONG_MIN
|| v
> LONG_MAX
)
2268 *value
= v
.ToLong();
2270 else if (type
== wxS("ulonglong"))
2272 wxULongLong v
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2273 // Don't convert if return value would be vague
2276 *value
= (long) v
.ToULong();
2285 bool wxVariant::Convert(bool* value
) const
2287 wxString
type(GetType());
2288 if (type
== wxT("double"))
2289 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
2290 else if (type
== wxT("long"))
2291 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2292 else if (type
== wxT("bool"))
2293 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2294 else if (type
== wxT("string"))
2296 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2298 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
2300 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
2311 bool wxVariant::Convert(double* value
) const
2313 wxString
type(GetType());
2314 if (type
== wxT("double"))
2315 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
2316 else if (type
== wxT("long"))
2317 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2318 else if (type
== wxT("bool"))
2319 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2320 else if (type
== wxT("string"))
2321 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
2323 else if (type
== wxS("longlong"))
2325 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue().ToDouble();
2327 else if (type
== wxS("ulonglong"))
2329 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue().ToDouble();
2338 bool wxVariant::Convert(wxUniChar
* value
) const
2340 wxString
type(GetType());
2341 if (type
== wxT("char"))
2342 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2343 else if (type
== wxT("long"))
2344 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2345 else if (type
== wxT("bool"))
2346 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2353 bool wxVariant::Convert(char* value
) const
2356 if ( !Convert(&ch
) )
2362 bool wxVariant::Convert(wchar_t* value
) const
2365 if ( !Convert(&ch
) )
2371 bool wxVariant::Convert(wxString
* value
) const
2373 *value
= MakeString();
2378 bool wxVariant::Convert(wxLongLong
* value
) const
2380 wxString
type(GetType());
2381 if (type
== wxS("longlong"))
2382 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2383 else if (type
== wxS("long"))
2384 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2385 else if (type
== wxS("string"))
2387 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2389 wxLongLong_t value_t
;
2390 if ( !s
.ToLongLong(&value_t
) )
2395 if ( !s
.ToLong(&l_value
) )
2400 else if (type
== wxS("bool"))
2401 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2402 else if (type
== wxS("double"))
2404 value
->Assign(((wxVariantDoubleData
*)GetData())->GetValue());
2406 else if (type
== wxS("ulonglong"))
2407 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2414 bool wxVariant::Convert(wxULongLong
* value
) const
2416 wxString
type(GetType());
2417 if (type
== wxS("ulonglong"))
2418 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2419 else if (type
== wxS("long"))
2420 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2421 else if (type
== wxS("string"))
2423 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2425 wxULongLong_t value_t
;
2426 if ( !s
.ToULongLong(&value_t
) )
2430 unsigned long l_value
;
2431 if ( !s
.ToULong(&l_value
) )
2436 else if (type
== wxS("bool"))
2437 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2438 else if (type
== wxS("double"))
2440 double value_d
= ((wxVariantDoubleData
*)GetData())->GetValue();
2442 if ( value_d
< 0.0 )
2446 *value
= (wxULongLong_t
) value_d
;
2449 temp
.Assign(value_d
);
2453 else if (type
== wxS("longlong"))
2454 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2460 #endif // wxUSE_LONGLONG
2463 bool wxVariant::Convert(wxDateTime
* value
) const
2465 wxString
type(GetType());
2466 if (type
== wxT("datetime"))
2468 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2472 // Fallback to string conversion
2474 if ( !Convert(&val
) )
2477 // Try to parse this as either date and time, only date or only time
2478 // checking that the entire string was parsed
2479 wxString::const_iterator end
;
2480 if ( value
->ParseDateTime(val
, &end
) && end
== val
.end() )
2483 if ( value
->ParseDate(val
, &end
) && end
== val
.end() )
2486 if ( value
->ParseTime(val
, &end
) && end
== val
.end() )
2491 #endif // wxUSE_DATETIME
2493 #endif // wxUSE_VARIANT