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"); }
274 // Since wxAny does not have separate type for integers shorter than
275 // longlong, we do not usually implement wxVariant->wxAny conversion
276 // here (but in wxVariantDataLongLong instead).
278 DECLARE_WXANY_CONVERSION()
280 bool GetAsAny(wxAny
* any
) const
293 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(long, wxVariantDataLong
)
296 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
298 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
300 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
302 return (otherData
.m_value
== m_value
);
305 #if wxUSE_STD_IOSTREAM
306 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
310 str
<< (const char*) s
.mb_str();
315 bool wxVariantDataLong::Write(wxString
& str
) const
317 str
.Printf(wxT("%ld"), m_value
);
321 #if wxUSE_STD_IOSTREAM
322 bool wxVariantDataLong::Read(wxSTD istream
& str
)
330 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
332 wxTextOutputStream
s(str
);
334 s
.Write32((size_t)m_value
);
338 bool wxVariantDataLong::Read(wxInputStream
& str
)
340 wxTextInputStream
s(str
);
341 m_value
= s
.Read32();
344 #endif // wxUSE_STREAMS
346 bool wxVariantDataLong::Read(wxString
& str
)
348 m_value
= wxAtol(str
);
354 wxVariant::wxVariant(long val
, const wxString
& name
)
356 m_refData
= new wxVariantDataLong(val
);
360 wxVariant::wxVariant(int val
, const wxString
& name
)
362 m_refData
= new wxVariantDataLong((long)val
);
366 wxVariant::wxVariant(short val
, const wxString
& name
)
368 m_refData
= new wxVariantDataLong((long)val
);
372 bool wxVariant::operator== (long value
) const
375 if (!Convert(&thisValue
))
378 return (value
== thisValue
);
381 bool wxVariant::operator!= (long value
) const
383 return (!((*this) == value
));
386 void wxVariant::operator= (long value
)
388 if (GetType() == wxT("long") &&
389 m_refData
->GetRefCount() == 1)
391 ((wxVariantDataLong
*)GetData())->SetValue(value
);
396 m_refData
= new wxVariantDataLong(value
);
400 long wxVariant::GetLong() const
403 if (Convert(& value
))
407 wxFAIL_MSG(wxT("Could not convert to a long"));
412 // -----------------------------------------------------------------
413 // wxVariantDoubleData
414 // -----------------------------------------------------------------
416 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
419 wxVariantDoubleData() { m_value
= 0.0; }
420 wxVariantDoubleData(double value
) { m_value
= value
; }
422 inline double GetValue() const { return m_value
; }
423 inline void SetValue(double value
) { m_value
= value
; }
425 virtual bool Eq(wxVariantData
& data
) const;
426 virtual bool Read(wxString
& str
);
427 #if wxUSE_STD_IOSTREAM
428 virtual bool Write(wxSTD ostream
& str
) const;
430 virtual bool Write(wxString
& str
) const;
431 #if wxUSE_STD_IOSTREAM
432 virtual bool Read(wxSTD istream
& str
);
435 virtual bool Read(wxInputStream
& str
);
436 virtual bool Write(wxOutputStream
&str
) const;
437 #endif // wxUSE_STREAMS
438 virtual wxString
GetType() const { return wxT("double"); }
440 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
442 DECLARE_WXANY_CONVERSION()
447 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(double, wxVariantDoubleData
)
449 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
451 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
453 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
455 return wxIsSameDouble(otherData
.m_value
, m_value
);
458 #if wxUSE_STD_IOSTREAM
459 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
463 str
<< (const char*) s
.mb_str();
468 bool wxVariantDoubleData::Write(wxString
& str
) const
470 str
.Printf(wxT("%.14g"), m_value
);
474 #if wxUSE_STD_IOSTREAM
475 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
483 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
485 wxTextOutputStream
s(str
);
486 s
.WriteDouble((double)m_value
);
490 bool wxVariantDoubleData::Read(wxInputStream
& str
)
492 wxTextInputStream
s(str
);
493 m_value
= (float)s
.ReadDouble();
496 #endif // wxUSE_STREAMS
498 bool wxVariantDoubleData::Read(wxString
& str
)
500 m_value
= wxAtof(str
);
504 // wxVariant double code
506 wxVariant::wxVariant(double val
, const wxString
& name
)
508 m_refData
= new wxVariantDoubleData(val
);
512 bool wxVariant::operator== (double value
) const
515 if (!Convert(&thisValue
))
518 return wxIsSameDouble(value
, thisValue
);
521 bool wxVariant::operator!= (double value
) const
523 return (!((*this) == value
));
526 void wxVariant::operator= (double value
)
528 if (GetType() == wxT("double") &&
529 m_refData
->GetRefCount() == 1)
531 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
536 m_refData
= new wxVariantDoubleData(value
);
540 double wxVariant::GetDouble() const
543 if (Convert(& value
))
547 wxFAIL_MSG(wxT("Could not convert to a double number"));
552 // -----------------------------------------------------------------
554 // -----------------------------------------------------------------
556 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
559 wxVariantDataBool() { m_value
= 0; }
560 wxVariantDataBool(bool value
) { m_value
= value
; }
562 inline bool GetValue() const { return m_value
; }
563 inline void SetValue(bool value
) { m_value
= value
; }
565 virtual bool Eq(wxVariantData
& data
) const;
566 #if wxUSE_STD_IOSTREAM
567 virtual bool Write(wxSTD ostream
& str
) const;
569 virtual bool Write(wxString
& str
) const;
570 virtual bool Read(wxString
& str
);
571 #if wxUSE_STD_IOSTREAM
572 virtual bool Read(wxSTD istream
& str
);
575 virtual bool Read(wxInputStream
& str
);
576 virtual bool Write(wxOutputStream
& str
) const;
577 #endif // wxUSE_STREAMS
578 virtual wxString
GetType() const { return wxT("bool"); }
580 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
582 DECLARE_WXANY_CONVERSION()
587 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(bool, wxVariantDataBool
)
589 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
591 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
593 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
595 return (otherData
.m_value
== m_value
);
598 #if wxUSE_STD_IOSTREAM
599 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
603 str
<< (const char*) s
.mb_str();
608 bool wxVariantDataBool::Write(wxString
& str
) const
610 str
.Printf(wxT("%d"), (int) m_value
);
614 #if wxUSE_STD_IOSTREAM
615 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
617 wxFAIL_MSG(wxT("Unimplemented"));
618 // str >> (long) m_value;
624 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
626 wxTextOutputStream
s(str
);
632 bool wxVariantDataBool::Read(wxInputStream
& str
)
634 wxTextInputStream
s(str
);
636 m_value
= s
.Read8() != 0;
639 #endif // wxUSE_STREAMS
641 bool wxVariantDataBool::Read(wxString
& str
)
643 m_value
= (wxAtol(str
) != 0);
649 wxVariant::wxVariant(bool val
, const wxString
& name
)
651 m_refData
= new wxVariantDataBool(val
);
655 bool wxVariant::operator== (bool value
) const
658 if (!Convert(&thisValue
))
661 return (value
== thisValue
);
664 bool wxVariant::operator!= (bool value
) const
666 return (!((*this) == value
));
669 void wxVariant::operator= (bool value
)
671 if (GetType() == wxT("bool") &&
672 m_refData
->GetRefCount() == 1)
674 ((wxVariantDataBool
*)GetData())->SetValue(value
);
679 m_refData
= new wxVariantDataBool(value
);
683 bool wxVariant::GetBool() const
686 if (Convert(& value
))
690 wxFAIL_MSG(wxT("Could not convert to a bool"));
695 // -----------------------------------------------------------------
697 // -----------------------------------------------------------------
699 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
702 wxVariantDataChar() { m_value
= 0; }
703 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
705 inline wxUniChar
GetValue() const { return m_value
; }
706 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
708 virtual bool Eq(wxVariantData
& data
) const;
709 #if wxUSE_STD_IOSTREAM
710 virtual bool Read(wxSTD istream
& str
);
711 virtual bool Write(wxSTD ostream
& str
) const;
713 virtual bool Read(wxString
& str
);
714 virtual bool Write(wxString
& str
) const;
716 virtual bool Read(wxInputStream
& str
);
717 virtual bool Write(wxOutputStream
& str
) const;
718 #endif // wxUSE_STREAMS
719 virtual wxString
GetType() const { return wxT("char"); }
720 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
722 DECLARE_WXANY_CONVERSION()
727 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxUniChar
, wxVariantDataChar
)
729 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
731 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
733 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
735 return (otherData
.m_value
== m_value
);
738 #if wxUSE_STD_IOSTREAM
739 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
741 str
<< wxString(m_value
);
746 bool wxVariantDataChar::Write(wxString
& str
) const
752 #if wxUSE_STD_IOSTREAM
753 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
755 wxFAIL_MSG(wxT("Unimplemented"));
762 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
764 wxTextOutputStream
s(str
);
766 // FIXME-UTF8: this should be just "s << m_value;" after removal of
767 // ANSI build and addition of wxUniChar to wxTextOutputStream:
768 s
<< (wxChar
)m_value
;
773 bool wxVariantDataChar::Read(wxInputStream
& str
)
775 wxTextInputStream
s(str
);
777 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
778 // ANSI build and addition of wxUniChar to wxTextInputStream:
785 #endif // wxUSE_STREAMS
787 bool wxVariantDataChar::Read(wxString
& str
)
793 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
795 m_refData
= new wxVariantDataChar(val
);
799 wxVariant::wxVariant(char val
, const wxString
& name
)
801 m_refData
= new wxVariantDataChar(val
);
805 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
807 m_refData
= new wxVariantDataChar(val
);
811 bool wxVariant::operator==(const wxUniChar
& value
) const
814 if (!Convert(&thisValue
))
817 return (value
== thisValue
);
820 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
822 if (GetType() == wxT("char") &&
823 m_refData
->GetRefCount() == 1)
825 ((wxVariantDataChar
*)GetData())->SetValue(value
);
830 m_refData
= new wxVariantDataChar(value
);
836 wxUniChar
wxVariant::GetChar() const
839 if (Convert(& value
))
843 wxFAIL_MSG(wxT("Could not convert to a char"));
848 // ----------------------------------------------------------------------------
849 // wxVariantDataString
850 // ----------------------------------------------------------------------------
852 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
855 wxVariantDataString() { }
856 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
858 inline wxString
GetValue() const { return m_value
; }
859 inline void SetValue(const wxString
& value
) { m_value
= value
; }
861 virtual bool Eq(wxVariantData
& data
) const;
862 #if wxUSE_STD_IOSTREAM
863 virtual bool Write(wxSTD ostream
& str
) const;
865 virtual bool Read(wxString
& str
);
866 virtual bool Write(wxString
& str
) const;
867 #if wxUSE_STD_IOSTREAM
868 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
871 virtual bool Read(wxInputStream
& str
);
872 virtual bool Write(wxOutputStream
& str
) const;
873 #endif // wxUSE_STREAMS
874 virtual wxString
GetType() const { return wxT("string"); }
875 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
877 DECLARE_WXANY_CONVERSION()
882 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxString
, wxVariantDataString
)
885 // This allows converting string literal wxAnys to string variants
886 wxVariantData
* wxVariantDataFromConstCharPAny(const wxAny
& any
)
888 return new wxVariantDataString(wxANY_AS(any
, const char*));
891 wxVariantData
* wxVariantDataFromConstWchar_tPAny(const wxAny
& any
)
893 return new wxVariantDataString(wxANY_AS(any
, const wchar_t*));
896 _REGISTER_WXANY_CONVERSION(const char*,
898 wxVariantDataFromConstCharPAny
)
899 _REGISTER_WXANY_CONVERSION(const wchar_t*,
901 wxVariantDataFromConstWchar_tPAny
)
904 bool wxVariantDataString::Eq(wxVariantData
& data
) const
906 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
908 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
910 return (otherData
.m_value
== m_value
);
913 #if wxUSE_STD_IOSTREAM
914 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
916 str
<< (const char*) m_value
.mb_str();
921 bool wxVariantDataString::Write(wxString
& str
) const
928 bool wxVariantDataString::Write(wxOutputStream
& str
) const
930 // why doesn't wxOutputStream::operator<< take "const wxString&"
931 wxTextOutputStream
s(str
);
932 s
.WriteString(m_value
);
936 bool wxVariantDataString::Read(wxInputStream
& str
)
938 wxTextInputStream
s(str
);
940 m_value
= s
.ReadLine();
943 #endif // wxUSE_STREAMS
945 bool wxVariantDataString::Read(wxString
& str
)
953 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
955 m_refData
= new wxVariantDataString(val
);
959 wxVariant::wxVariant(const char* val
, const wxString
& name
)
961 m_refData
= new wxVariantDataString(wxString(val
));
965 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
967 m_refData
= new wxVariantDataString(wxString(val
));
971 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
973 m_refData
= new wxVariantDataString(val
.AsString());
977 wxVariant::wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
)
979 m_refData
= new wxVariantDataString(wxString(val
));
983 wxVariant::wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
)
985 m_refData
= new wxVariantDataString(wxString(val
));
990 wxVariant::wxVariant(const std::string
& val
, const wxString
& name
)
992 m_refData
= new wxVariantDataString(wxString(val
));
996 wxVariant::wxVariant(const wxStdWideString
& val
, const wxString
& name
)
998 m_refData
= new wxVariantDataString(wxString(val
));
1001 #endif // wxUSE_STD_STRING
1003 bool wxVariant::operator== (const wxString
& value
) const
1006 if (!Convert(&thisValue
))
1009 return value
== thisValue
;
1012 bool wxVariant::operator!= (const wxString
& value
) const
1014 return (!((*this) == value
));
1017 wxVariant
& wxVariant::operator= (const wxString
& value
)
1019 if (GetType() == wxT("string") &&
1020 m_refData
->GetRefCount() == 1)
1022 ((wxVariantDataString
*)GetData())->SetValue(value
);
1027 m_refData
= new wxVariantDataString(value
);
1032 wxString
wxVariant::GetString() const
1035 if (!Convert(& value
))
1037 wxFAIL_MSG(wxT("Could not convert to a string"));
1043 // ----------------------------------------------------------------------------
1044 // wxVariantDataWxObjectPtr
1045 // ----------------------------------------------------------------------------
1047 class wxVariantDataWxObjectPtr
: public wxVariantData
1050 wxVariantDataWxObjectPtr() { }
1051 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
1053 inline wxObject
* GetValue() const { return m_value
; }
1054 inline void SetValue(wxObject
* value
) { m_value
= value
; }
1056 virtual bool Eq(wxVariantData
& data
) const;
1057 #if wxUSE_STD_IOSTREAM
1058 virtual bool Write(wxSTD ostream
& str
) const;
1060 virtual bool Write(wxString
& str
) const;
1061 #if wxUSE_STD_IOSTREAM
1062 virtual bool Read(wxSTD istream
& str
);
1064 virtual bool Read(wxString
& str
);
1065 virtual wxString
GetType() const ;
1066 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
1068 virtual wxClassInfo
* GetValueClassInfo();
1070 DECLARE_WXANY_CONVERSION()
1075 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxObject
*, wxVariantDataWxObjectPtr
)
1077 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
1079 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1081 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1083 return (otherData
.m_value
== m_value
);
1086 wxString
wxVariantDataWxObjectPtr::GetType() const
1088 wxString
returnVal(wxT("wxObject*"));
1092 returnVal
= m_value
->GetClassInfo()->GetClassName();
1093 returnVal
+= wxT("*");
1099 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1101 wxClassInfo
* returnVal
=NULL
;
1103 if (m_value
) returnVal
= m_value
->GetClassInfo();
1108 #if wxUSE_STD_IOSTREAM
1109 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1113 str
<< (const char*) s
.mb_str();
1118 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1120 str
.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value
));
1124 #if wxUSE_STD_IOSTREAM
1125 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1132 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1140 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1142 m_refData
= new wxVariantDataWxObjectPtr(val
);
1146 bool wxVariant::operator== (wxObject
* value
) const
1148 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1151 bool wxVariant::operator!= (wxObject
* value
) const
1153 return (!((*this) == (wxObject
*) value
));
1156 void wxVariant::operator= (wxObject
* value
)
1159 m_refData
= new wxVariantDataWxObjectPtr(value
);
1162 wxObject
* wxVariant::GetWxObjectPtr() const
1164 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_refData
)->GetValue();
1167 // ----------------------------------------------------------------------------
1168 // wxVariantDataVoidPtr
1169 // ----------------------------------------------------------------------------
1171 class wxVariantDataVoidPtr
: public wxVariantData
1174 wxVariantDataVoidPtr() { }
1175 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1177 inline void* GetValue() const { return m_value
; }
1178 inline void SetValue(void* value
) { m_value
= value
; }
1180 virtual bool Eq(wxVariantData
& data
) const;
1181 #if wxUSE_STD_IOSTREAM
1182 virtual bool Write(wxSTD ostream
& str
) const;
1184 virtual bool Write(wxString
& str
) const;
1185 #if wxUSE_STD_IOSTREAM
1186 virtual bool Read(wxSTD istream
& str
);
1188 virtual bool Read(wxString
& str
);
1189 virtual wxString
GetType() const { return wxT("void*"); }
1190 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1192 DECLARE_WXANY_CONVERSION()
1197 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(void*, wxVariantDataVoidPtr
)
1199 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1201 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1203 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1205 return (otherData
.m_value
== m_value
);
1208 #if wxUSE_STD_IOSTREAM
1209 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1213 str
<< (const char*) s
.mb_str();
1218 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1220 str
.Printf(wxT("%p"), m_value
);
1224 #if wxUSE_STD_IOSTREAM
1225 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1232 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1240 wxVariant::wxVariant( void* val
, const wxString
& name
)
1242 m_refData
= new wxVariantDataVoidPtr(val
);
1246 bool wxVariant::operator== (void* value
) const
1248 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1251 bool wxVariant::operator!= (void* value
) const
1253 return (!((*this) == (void*) value
));
1256 void wxVariant::operator= (void* value
)
1258 if (GetType() == wxT("void*") && (m_refData
->GetRefCount() == 1))
1260 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1265 m_refData
= new wxVariantDataVoidPtr(value
);
1269 void* wxVariant::GetVoidPtr() const
1271 // handling this specially is convenient when working with COM, see #9873
1275 wxASSERT( GetType() == wxT("void*") );
1277 return (void*) ((wxVariantDataVoidPtr
*) m_refData
)->GetValue();
1280 // ----------------------------------------------------------------------------
1281 // wxVariantDataDateTime
1282 // ----------------------------------------------------------------------------
1286 class wxVariantDataDateTime
: public wxVariantData
1289 wxVariantDataDateTime() { }
1290 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1292 inline wxDateTime
GetValue() const { return m_value
; }
1293 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1295 virtual bool Eq(wxVariantData
& data
) const;
1296 #if wxUSE_STD_IOSTREAM
1297 virtual bool Write(wxSTD ostream
& str
) const;
1299 virtual bool Write(wxString
& str
) const;
1300 #if wxUSE_STD_IOSTREAM
1301 virtual bool Read(wxSTD istream
& str
);
1303 virtual bool Read(wxString
& str
);
1304 virtual wxString
GetType() const { return wxT("datetime"); }
1305 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1307 DECLARE_WXANY_CONVERSION()
1312 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxDateTime
, wxVariantDataDateTime
)
1314 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1316 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1318 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1320 return (otherData
.m_value
== m_value
);
1324 #if wxUSE_STD_IOSTREAM
1325 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1329 str
<< value
.c_str();
1335 bool wxVariantDataDateTime::Write(wxString
& str
) const
1337 if ( m_value
.IsValid() )
1338 str
= m_value
.Format();
1340 str
= wxS("Invalid");
1345 #if wxUSE_STD_IOSTREAM
1346 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1354 bool wxVariantDataDateTime::Read(wxString
& str
)
1356 if ( str
== wxS("Invalid") )
1358 m_value
= wxInvalidDateTime
;
1362 wxString::const_iterator end
;
1363 return m_value
.ParseDateTime(str
, &end
) && end
== str
.end();
1368 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1370 m_refData
= new wxVariantDataDateTime(val
);
1374 bool wxVariant::operator== (const wxDateTime
& value
) const
1376 wxDateTime thisValue
;
1377 if (!Convert(&thisValue
))
1380 return value
.IsEqualTo(thisValue
);
1383 bool wxVariant::operator!= (const wxDateTime
& value
) const
1385 return (!((*this) == value
));
1388 void wxVariant::operator= (const wxDateTime
& value
)
1390 if (GetType() == wxT("datetime") &&
1391 m_refData
->GetRefCount() == 1)
1393 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1398 m_refData
= new wxVariantDataDateTime(value
);
1402 wxDateTime
wxVariant::GetDateTime() const
1405 if (!Convert(& value
))
1407 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1413 #endif // wxUSE_DATETIME
1415 // ----------------------------------------------------------------------------
1416 // wxVariantDataArrayString
1417 // ----------------------------------------------------------------------------
1419 class wxVariantDataArrayString
: public wxVariantData
1422 wxVariantDataArrayString() { }
1423 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1425 wxArrayString
GetValue() const { return m_value
; }
1426 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1428 virtual bool Eq(wxVariantData
& data
) const;
1429 #if wxUSE_STD_IOSTREAM
1430 virtual bool Write(wxSTD ostream
& str
) const;
1432 virtual bool Write(wxString
& str
) const;
1433 #if wxUSE_STD_IOSTREAM
1434 virtual bool Read(wxSTD istream
& str
);
1436 virtual bool Read(wxString
& str
);
1437 virtual wxString
GetType() const { return wxT("arrstring"); }
1438 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1440 DECLARE_WXANY_CONVERSION()
1442 wxArrayString m_value
;
1445 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxArrayString
, wxVariantDataArrayString
)
1447 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1449 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1451 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1453 return otherData
.m_value
== m_value
;
1456 #if wxUSE_STD_IOSTREAM
1457 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1464 bool wxVariantDataArrayString::Write(wxString
& str
) const
1466 size_t count
= m_value
.GetCount();
1467 for ( size_t n
= 0; n
< count
; n
++ )
1479 #if wxUSE_STD_IOSTREAM
1480 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1488 bool wxVariantDataArrayString::Read(wxString
& str
)
1490 wxStringTokenizer
tk(str
, wxT(";"));
1491 while ( tk
.HasMoreTokens() )
1493 m_value
.Add(tk
.GetNextToken());
1501 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1503 m_refData
= new wxVariantDataArrayString(val
);
1507 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1509 wxFAIL_MSG( wxT("TODO") );
1514 bool wxVariant::operator!=(const wxArrayString
& value
) const
1516 return !(*this == value
);
1519 void wxVariant::operator=(const wxArrayString
& value
)
1521 if (GetType() == wxT("arrstring") &&
1522 m_refData
->GetRefCount() == 1)
1524 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1529 m_refData
= new wxVariantDataArrayString(value
);
1533 wxArrayString
wxVariant::GetArrayString() const
1535 if ( GetType() == wxT("arrstring") )
1536 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1538 return wxArrayString();
1541 // ----------------------------------------------------------------------------
1542 // wxVariantDataLongLong
1543 // ----------------------------------------------------------------------------
1547 class WXDLLIMPEXP_BASE wxVariantDataLongLong
: public wxVariantData
1550 wxVariantDataLongLong() { m_value
= 0; }
1551 wxVariantDataLongLong(wxLongLong value
) { m_value
= value
; }
1553 wxLongLong
GetValue() const { return m_value
; }
1554 void SetValue(wxLongLong value
) { m_value
= value
; }
1556 virtual bool Eq(wxVariantData
& data
) const;
1558 virtual bool Read(wxString
& str
);
1559 virtual bool Write(wxString
& str
) const;
1560 #if wxUSE_STD_IOSTREAM
1561 virtual bool Read(wxSTD istream
& str
);
1562 virtual bool Write(wxSTD ostream
& str
) const;
1565 virtual bool Read(wxInputStream
& str
);
1566 virtual bool Write(wxOutputStream
&str
) const;
1567 #endif // wxUSE_STREAMS
1569 wxVariantData
* Clone() const
1571 return new wxVariantDataLongLong(m_value
);
1574 virtual wxString
GetType() const { return wxS("longlong"); }
1576 DECLARE_WXANY_CONVERSION()
1582 // wxLongLong type requires customized wxAny conversion code
1587 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1589 *any
= m_value
.GetValue();
1593 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1595 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong_t
));
1598 REGISTER_WXANY_CONVERSION(wxLongLong_t
, wxVariantDataLongLong
)
1600 #else // if !defined(wxLongLong_t)
1602 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1608 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1610 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong
));
1613 REGISTER_WXANY_CONVERSION(wxLongLong
, wxVariantDataLongLong
)
1615 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1618 bool wxVariantDataLongLong::Eq(wxVariantData
& data
) const
1620 wxASSERT_MSG( (data
.GetType() == wxS("longlong")),
1621 "wxVariantDataLongLong::Eq: argument mismatch" );
1623 wxVariantDataLongLong
& otherData
= (wxVariantDataLongLong
&) data
;
1625 return (otherData
.m_value
== m_value
);
1628 #if wxUSE_STD_IOSTREAM
1629 bool wxVariantDataLongLong::Write(wxSTD ostream
& str
) const
1633 str
<< (const char*) s
.mb_str();
1638 bool wxVariantDataLongLong::Write(wxString
& str
) const
1641 str
.Printf(wxS("%lld"), m_value
.GetValue());
1648 #if wxUSE_STD_IOSTREAM
1649 bool wxVariantDataLongLong::Read(wxSTD istream
& WXUNUSED(str
))
1651 wxFAIL_MSG(wxS("Unimplemented"));
1657 bool wxVariantDataLongLong::Write(wxOutputStream
& str
) const
1659 wxTextOutputStream
s(str
);
1660 s
.Write32(m_value
.GetLo());
1661 s
.Write32(m_value
.GetHi());
1665 bool wxVariantDataLongLong::Read(wxInputStream
& str
)
1667 wxTextInputStream
s(str
);
1668 unsigned long lo
= s
.Read32();
1669 long hi
= s
.Read32();
1670 m_value
= wxLongLong(hi
, lo
);
1673 #endif // wxUSE_STREAMS
1675 bool wxVariantDataLongLong::Read(wxString
& str
)
1678 wxLongLong_t value_t
;
1679 if ( !str
.ToLongLong(&value_t
) )
1690 wxVariant::wxVariant(wxLongLong val
, const wxString
& name
)
1692 m_refData
= new wxVariantDataLongLong(val
);
1696 bool wxVariant::operator==(wxLongLong value
) const
1698 wxLongLong thisValue
;
1699 if ( !Convert(&thisValue
) )
1702 return (value
== thisValue
);
1705 bool wxVariant::operator!=(wxLongLong value
) const
1707 return (!((*this) == value
));
1710 void wxVariant::operator=(wxLongLong value
)
1712 if ( GetType() == wxS("longlong") &&
1713 m_refData
->GetRefCount() == 1 )
1715 ((wxVariantDataLongLong
*)GetData())->SetValue(value
);
1720 m_refData
= new wxVariantDataLongLong(value
);
1724 wxLongLong
wxVariant::GetLongLong() const
1727 if ( Convert(&value
) )
1733 wxFAIL_MSG(wxT("Could not convert to a long long"));
1738 #endif // wxUSE_LONGLONG
1740 // ----------------------------------------------------------------------------
1741 // wxVariantDataULongLong
1742 // ----------------------------------------------------------------------------
1746 class WXDLLIMPEXP_BASE wxVariantDataULongLong
: public wxVariantData
1749 wxVariantDataULongLong() { m_value
= 0; }
1750 wxVariantDataULongLong(wxULongLong value
) { m_value
= value
; }
1752 wxULongLong
GetValue() const { return m_value
; }
1753 void SetValue(wxULongLong value
) { m_value
= value
; }
1755 virtual bool Eq(wxVariantData
& data
) const;
1757 virtual bool Read(wxString
& str
);
1758 virtual bool Write(wxString
& str
) const;
1759 #if wxUSE_STD_IOSTREAM
1760 virtual bool Read(wxSTD istream
& str
);
1761 virtual bool Write(wxSTD ostream
& str
) const;
1764 virtual bool Read(wxInputStream
& str
);
1765 virtual bool Write(wxOutputStream
&str
) const;
1766 #endif // wxUSE_STREAMS
1768 wxVariantData
* Clone() const
1770 return new wxVariantDataULongLong(m_value
);
1773 virtual wxString
GetType() const { return wxS("ulonglong"); }
1775 DECLARE_WXANY_CONVERSION()
1777 wxULongLong m_value
;
1781 // wxULongLong type requires customized wxAny conversion code
1786 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1788 *any
= m_value
.GetValue();
1792 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1794 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong_t
));
1797 REGISTER_WXANY_CONVERSION(wxULongLong_t
, wxVariantDataULongLong
)
1799 #else // if !defined(wxLongLong_t)
1801 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1807 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1809 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong
));
1812 REGISTER_WXANY_CONVERSION(wxULongLong
, wxVariantDataULongLong
)
1814 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1818 bool wxVariantDataULongLong::Eq(wxVariantData
& data
) const
1820 wxASSERT_MSG( (data
.GetType() == wxS("ulonglong")),
1821 "wxVariantDataULongLong::Eq: argument mismatch" );
1823 wxVariantDataULongLong
& otherData
= (wxVariantDataULongLong
&) data
;
1825 return (otherData
.m_value
== m_value
);
1828 #if wxUSE_STD_IOSTREAM
1829 bool wxVariantDataULongLong::Write(wxSTD ostream
& str
) const
1833 str
<< (const char*) s
.mb_str();
1838 bool wxVariantDataULongLong::Write(wxString
& str
) const
1841 str
.Printf(wxS("%llu"), m_value
.GetValue());
1848 #if wxUSE_STD_IOSTREAM
1849 bool wxVariantDataULongLong::Read(wxSTD istream
& WXUNUSED(str
))
1851 wxFAIL_MSG(wxS("Unimplemented"));
1857 bool wxVariantDataULongLong::Write(wxOutputStream
& str
) const
1859 wxTextOutputStream
s(str
);
1860 s
.Write32(m_value
.GetLo());
1861 s
.Write32(m_value
.GetHi());
1865 bool wxVariantDataULongLong::Read(wxInputStream
& str
)
1867 wxTextInputStream
s(str
);
1868 unsigned long lo
= s
.Read32();
1869 long hi
= s
.Read32();
1870 m_value
= wxULongLong(hi
, lo
);
1873 #endif // wxUSE_STREAMS
1875 bool wxVariantDataULongLong::Read(wxString
& str
)
1878 wxULongLong_t value_t
;
1879 if ( !str
.ToULongLong(&value_t
) )
1890 wxVariant::wxVariant(wxULongLong val
, const wxString
& name
)
1892 m_refData
= new wxVariantDataULongLong(val
);
1896 bool wxVariant::operator==(wxULongLong value
) const
1898 wxULongLong thisValue
;
1899 if ( !Convert(&thisValue
) )
1902 return (value
== thisValue
);
1905 bool wxVariant::operator!=(wxULongLong value
) const
1907 return (!((*this) == value
));
1910 void wxVariant::operator=(wxULongLong value
)
1912 if ( GetType() == wxS("ulonglong") &&
1913 m_refData
->GetRefCount() == 1 )
1915 ((wxVariantDataULongLong
*)GetData())->SetValue(value
);
1920 m_refData
= new wxVariantDataULongLong(value
);
1924 wxULongLong
wxVariant::GetULongLong() const
1927 if ( Convert(&value
) )
1933 wxFAIL_MSG(wxT("Could not convert to a long long"));
1938 #endif // wxUSE_LONGLONG
1940 // ----------------------------------------------------------------------------
1941 // wxVariantDataList
1942 // ----------------------------------------------------------------------------
1944 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1947 wxVariantDataList() {}
1948 wxVariantDataList(const wxVariantList
& list
);
1949 virtual ~wxVariantDataList();
1951 wxVariantList
& GetValue() { return m_value
; }
1952 void SetValue(const wxVariantList
& value
) ;
1954 virtual bool Eq(wxVariantData
& data
) const;
1955 #if wxUSE_STD_IOSTREAM
1956 virtual bool Write(wxSTD ostream
& str
) const;
1958 virtual bool Write(wxString
& str
) const;
1959 #if wxUSE_STD_IOSTREAM
1960 virtual bool Read(wxSTD istream
& str
);
1962 virtual bool Read(wxString
& str
);
1963 virtual wxString
GetType() const { return wxT("list"); }
1967 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1969 DECLARE_WXANY_CONVERSION()
1971 wxVariantList m_value
;
1977 // Convert to/from list of wxAnys
1980 WX_DEFINE_LIST(wxAnyList
)
1982 bool wxVariantDataList::GetAsAny(wxAny
* any
) const
1985 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1988 wxVariant
* pVar
= node
->GetData();
1989 dst
.push_back(new wxAny(((const wxVariant
&)*pVar
)));
1990 node
= node
->GetNext();
1997 wxVariantData
* wxVariantDataList::VariantDataFactory(const wxAny
& any
)
1999 wxAnyList src
= wxANY_AS(any
, wxAnyList
);
2001 wxAnyList::compatibility_iterator node
= src
.GetFirst();
2004 wxAny
* pAny
= node
->GetData();
2005 dst
.push_back(new wxVariant(*pAny
));
2006 node
= node
->GetNext();
2009 return new wxVariantDataList(dst
);
2012 REGISTER_WXANY_CONVERSION(wxAnyList
, wxVariantDataList
)
2016 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
2021 wxVariantDataList::~wxVariantDataList()
2026 void wxVariantDataList::SetValue(const wxVariantList
& value
)
2029 wxVariantList::compatibility_iterator node
= value
.GetFirst();
2032 wxVariant
* var
= node
->GetData();
2033 m_value
.Append(new wxVariant(*var
));
2034 node
= node
->GetNext();
2038 void wxVariantDataList::Clear()
2040 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2043 wxVariant
* var
= node
->GetData();
2045 node
= node
->GetNext();
2050 bool wxVariantDataList::Eq(wxVariantData
& data
) const
2052 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
2054 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
2055 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
2056 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
2057 while (node1
&& node2
)
2059 wxVariant
* var1
= node1
->GetData();
2060 wxVariant
* var2
= node2
->GetData();
2061 if ((*var1
) != (*var2
))
2063 node1
= node1
->GetNext();
2064 node2
= node2
->GetNext();
2066 if (node1
|| node2
) return false;
2070 #if wxUSE_STD_IOSTREAM
2071 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
2075 str
<< (const char*) s
.mb_str();
2080 bool wxVariantDataList::Write(wxString
& str
) const
2082 str
= wxEmptyString
;
2083 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2086 wxVariant
* var
= node
->GetData();
2087 if (node
!= m_value
.GetFirst())
2090 str
+= var
->MakeString();
2091 node
= node
->GetNext();
2097 #if wxUSE_STD_IOSTREAM
2098 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
2100 wxFAIL_MSG(wxT("Unimplemented"));
2106 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
2108 wxFAIL_MSG(wxT("Unimplemented"));
2115 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
2117 m_refData
= new wxVariantDataList(val
);
2121 bool wxVariant::operator== (const wxVariantList
& value
) const
2123 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
2125 wxVariantDataList
other(value
);
2126 return (GetData()->Eq(other
));
2129 bool wxVariant::operator!= (const wxVariantList
& value
) const
2131 return (!((*this) == value
));
2134 void wxVariant::operator= (const wxVariantList
& value
)
2136 if (GetType() == wxT("list") &&
2137 m_refData
->GetRefCount() == 1)
2139 ((wxVariantDataList
*)GetData())->SetValue(value
);
2144 m_refData
= new wxVariantDataList(value
);
2148 wxVariantList
& wxVariant::GetList() const
2150 wxASSERT( (GetType() == wxT("list")) );
2152 return (wxVariantList
&) ((wxVariantDataList
*) m_refData
)->GetValue();
2156 void wxVariant::NullList()
2158 SetData(new wxVariantDataList());
2162 void wxVariant::Append(const wxVariant
& value
)
2164 wxVariantList
& list
= GetList();
2166 list
.Append(new wxVariant(value
));
2169 // Insert at front of list
2170 void wxVariant::Insert(const wxVariant
& value
)
2172 wxVariantList
& list
= GetList();
2174 list
.Insert(new wxVariant(value
));
2177 // Returns true if the variant is a member of the list
2178 bool wxVariant::Member(const wxVariant
& value
) const
2180 wxVariantList
& list
= GetList();
2182 wxVariantList::compatibility_iterator node
= list
.GetFirst();
2185 wxVariant
* other
= node
->GetData();
2186 if (value
== *other
)
2188 node
= node
->GetNext();
2193 // Deletes the nth element of the list
2194 bool wxVariant::Delete(size_t item
)
2196 wxVariantList
& list
= GetList();
2198 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
2199 wxVariantList::compatibility_iterator node
= list
.Item(item
);
2200 wxVariant
* variant
= node
->GetData();
2207 void wxVariant::ClearList()
2209 if (!IsNull() && (GetType() == wxT("list")))
2211 ((wxVariantDataList
*) m_refData
)->Clear();
2215 if (!GetType().IsSameAs(wxT("list")))
2218 m_refData
= new wxVariantDataList
;
2222 // Treat a list variant as an array
2223 wxVariant
wxVariant::operator[] (size_t idx
) const
2225 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
2227 if (GetType() == wxT("list"))
2229 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2230 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2231 return *(data
->GetValue().Item(idx
)->GetData());
2233 return wxNullVariant
;
2236 wxVariant
& wxVariant::operator[] (size_t idx
)
2238 // We can't return a reference to a variant for a string list, since the string
2239 // is actually stored as a char*, not a variant.
2241 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
2243 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2244 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2246 return * (data
->GetValue().Item(idx
)->GetData());
2249 // Return the number of elements in a list
2250 size_t wxVariant::GetCount() const
2252 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
2254 if (GetType() == wxT("list"))
2256 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2257 return data
->GetValue().GetCount();
2262 // ----------------------------------------------------------------------------
2264 // ----------------------------------------------------------------------------
2266 bool wxVariant::Convert(long* value
) const
2268 wxString
type(GetType());
2269 if (type
== wxS("double"))
2270 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
2271 else if (type
== wxS("long"))
2272 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2273 else if (type
== wxS("bool"))
2274 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2275 else if (type
== wxS("string"))
2276 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
2278 else if (type
== wxS("longlong"))
2280 wxLongLong v
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2281 // Don't convert if return value would be vague
2282 if ( v
< LONG_MIN
|| v
> LONG_MAX
)
2284 *value
= v
.ToLong();
2286 else if (type
== wxS("ulonglong"))
2288 wxULongLong v
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2289 // Don't convert if return value would be vague
2292 *value
= (long) v
.ToULong();
2301 bool wxVariant::Convert(bool* value
) const
2303 wxString
type(GetType());
2304 if (type
== wxT("double"))
2305 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
2306 else if (type
== wxT("long"))
2307 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2308 else if (type
== wxT("bool"))
2309 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2310 else if (type
== wxT("string"))
2312 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2314 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
2316 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
2327 bool wxVariant::Convert(double* value
) const
2329 wxString
type(GetType());
2330 if (type
== wxT("double"))
2331 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
2332 else if (type
== wxT("long"))
2333 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2334 else if (type
== wxT("bool"))
2335 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2336 else if (type
== wxT("string"))
2337 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
2339 else if (type
== wxS("longlong"))
2341 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue().ToDouble();
2343 else if (type
== wxS("ulonglong"))
2345 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue().ToDouble();
2354 bool wxVariant::Convert(wxUniChar
* value
) const
2356 wxString
type(GetType());
2357 if (type
== wxT("char"))
2358 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2359 else if (type
== wxT("long"))
2360 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2361 else if (type
== wxT("bool"))
2362 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2363 else if (type
== wxS("string"))
2365 // Also accept strings of length 1
2366 const wxString
& str
= (((wxVariantDataString
*)GetData())->GetValue());
2367 if ( str
.length() == 1 )
2378 bool wxVariant::Convert(char* value
) const
2381 if ( !Convert(&ch
) )
2387 bool wxVariant::Convert(wchar_t* value
) const
2390 if ( !Convert(&ch
) )
2396 bool wxVariant::Convert(wxString
* value
) const
2398 *value
= MakeString();
2403 bool wxVariant::Convert(wxLongLong
* value
) const
2405 wxString
type(GetType());
2406 if (type
== wxS("longlong"))
2407 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2408 else if (type
== wxS("long"))
2409 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2410 else if (type
== wxS("string"))
2412 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2414 wxLongLong_t value_t
;
2415 if ( !s
.ToLongLong(&value_t
) )
2420 if ( !s
.ToLong(&l_value
) )
2425 else if (type
== wxS("bool"))
2426 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2427 else if (type
== wxS("double"))
2429 value
->Assign(((wxVariantDoubleData
*)GetData())->GetValue());
2431 else if (type
== wxS("ulonglong"))
2432 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2439 bool wxVariant::Convert(wxULongLong
* value
) const
2441 wxString
type(GetType());
2442 if (type
== wxS("ulonglong"))
2443 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2444 else if (type
== wxS("long"))
2445 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2446 else if (type
== wxS("string"))
2448 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2450 wxULongLong_t value_t
;
2451 if ( !s
.ToULongLong(&value_t
) )
2455 unsigned long l_value
;
2456 if ( !s
.ToULong(&l_value
) )
2461 else if (type
== wxS("bool"))
2462 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2463 else if (type
== wxS("double"))
2465 double value_d
= ((wxVariantDoubleData
*)GetData())->GetValue();
2467 if ( value_d
< 0.0 )
2471 *value
= (wxULongLong_t
) value_d
;
2474 temp
.Assign(value_d
);
2478 else if (type
== wxS("longlong"))
2479 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2485 #endif // wxUSE_LONGLONG
2488 bool wxVariant::Convert(wxDateTime
* value
) const
2490 wxString
type(GetType());
2491 if (type
== wxT("datetime"))
2493 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2497 // Fallback to string conversion
2499 if ( !Convert(&val
) )
2502 // Try to parse this as either date and time, only date or only time
2503 // checking that the entire string was parsed
2504 wxString::const_iterator end
;
2505 if ( value
->ParseDateTime(val
, &end
) && end
== val
.end() )
2508 if ( value
->ParseDate(val
, &end
) && end
== val
.end() )
2511 if ( value
->ParseTime(val
, &end
) && end
== val
.end() )
2516 #endif // wxUSE_DATETIME
2518 #endif // wxUSE_VARIANT