1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/variant.cpp
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 #include "wx/variant.h"
23 #include "wx/string.h"
27 #include "wx/stream.h"
31 #if wxUSE_STD_IOSTREAM
40 #include "wx/txtstrm.h"
43 #include "wx/string.h"
44 #include "wx/tokenzr.h"
46 wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
49 #include "wx/listimpl.cpp"
50 WX_DEFINE_LIST(wxVariantList
)
56 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
58 wxVariant::wxVariant()
63 bool wxVariant::IsNull() const
65 return (m_refData
== NULL
);
68 void wxVariant::MakeNull()
73 void wxVariant::Clear()
75 m_name
= wxEmptyString
;
78 wxVariant::wxVariant(const wxVariant
& variant
)
81 if (!variant
.IsNull())
84 m_name
= variant
.m_name
;
87 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
94 wxVariant::~wxVariant()
98 wxObjectRefData
*wxVariant::CreateRefData() const
100 // We cannot create any particular wxVariantData.
101 wxFAIL_MSG("wxVariant::CreateRefData() cannot be implemented");
105 wxObjectRefData
*wxVariant::CloneRefData(const wxObjectRefData
*data
) const
107 return ((wxVariantData
*) data
)->Clone();
111 void wxVariant::operator= (const wxVariant
& variant
)
114 m_name
= variant
.m_name
;
117 // myVariant = new wxStringVariantData("hello")
118 void wxVariant::operator= (wxVariantData
* variantData
)
121 m_refData
= variantData
;
124 bool wxVariant::operator== (const wxVariant
& variant
) const
126 if (IsNull() || variant
.IsNull())
127 return (IsNull() == variant
.IsNull());
129 if (GetType() != variant
.GetType())
132 return (GetData()->Eq(* variant
.GetData()));
135 bool wxVariant::operator!= (const wxVariant
& variant
) const
137 return (!(*this == variant
));
140 wxString
wxVariant::MakeString() const
145 if (GetData()->Write(str
))
148 return wxEmptyString
;
151 void wxVariant::SetData(wxVariantData
* data
)
157 bool wxVariant::Unshare()
159 if ( !m_refData
|| m_refData
->GetRefCount() == 1 )
164 return (m_refData
&& m_refData
->GetRefCount() == 1);
168 // Returns a string representing the type of the variant,
169 // e.g. "string", "bool", "list", "double", "long"
170 wxString
wxVariant::GetType() const
173 return wxString(wxT("null"));
175 return GetData()->GetType();
179 bool wxVariant::IsType(const wxString
& type
) const
181 return (GetType() == type
);
184 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
186 wxClassInfo
* info
=GetData()->GetValueClassInfo();
187 return info
? info
->IsKindOf(type
) : false ;
190 // -----------------------------------------------------------------
191 // wxVariant <-> wxAny conversion code
192 // -----------------------------------------------------------------
196 wxAnyToVariantRegistration::
197 wxAnyToVariantRegistration(wxVariantDataFactory factory
)
200 wxPreRegisterAnyToVariant(this);
203 wxAnyToVariantRegistration::~wxAnyToVariantRegistration()
207 wxVariant::wxVariant(const wxAny
& any
)
211 if ( !any
.GetAs(&variant
) )
213 wxFAIL_MSG("wxAny of this type cannot be converted to wxVariant");
220 wxAny
wxVariant::GetAny() const
226 wxVariantData
* data
= GetData();
228 if ( data
->GetAsAny(&any
) )
231 // If everything else fails, wrap the whole wxVariantData
237 // -----------------------------------------------------------------
239 // -----------------------------------------------------------------
241 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
244 wxVariantDataLong() { m_value
= 0; }
245 wxVariantDataLong(long value
) { m_value
= value
; }
247 inline long GetValue() const { return m_value
; }
248 inline void SetValue(long value
) { m_value
= value
; }
250 virtual bool Eq(wxVariantData
& data
) const;
252 virtual bool Read(wxString
& str
);
253 virtual bool Write(wxString
& str
) const;
254 #if wxUSE_STD_IOSTREAM
255 virtual bool Read(wxSTD istream
& str
);
256 virtual bool Write(wxSTD ostream
& str
) const;
259 virtual bool Read(wxInputStream
& str
);
260 virtual bool Write(wxOutputStream
&str
) const;
261 #endif // wxUSE_STREAMS
263 wxVariantData
* Clone() const { return new wxVariantDataLong(m_value
); }
265 virtual wxString
GetType() const { return wxT("long"); }
268 // Since wxAny does not have separate type for integers shorter than
269 // longlong, we do not usually implement wxVariant->wxAny conversion
270 // here (but in wxVariantDataLongLong instead).
272 DECLARE_WXANY_CONVERSION()
274 bool GetAsAny(wxAny
* any
) const
287 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(long, wxVariantDataLong
)
290 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
292 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
294 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
296 return (otherData
.m_value
== m_value
);
299 #if wxUSE_STD_IOSTREAM
300 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
304 str
<< (const char*) s
.mb_str();
309 bool wxVariantDataLong::Write(wxString
& str
) const
311 str
.Printf(wxT("%ld"), m_value
);
315 #if wxUSE_STD_IOSTREAM
316 bool wxVariantDataLong::Read(wxSTD istream
& str
)
324 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
326 wxTextOutputStream
s(str
);
328 s
.Write32((size_t)m_value
);
332 bool wxVariantDataLong::Read(wxInputStream
& str
)
334 wxTextInputStream
s(str
);
335 m_value
= s
.Read32();
338 #endif // wxUSE_STREAMS
340 bool wxVariantDataLong::Read(wxString
& str
)
342 m_value
= wxAtol(str
);
348 wxVariant::wxVariant(long val
, const wxString
& name
)
350 m_refData
= new wxVariantDataLong(val
);
354 wxVariant::wxVariant(int val
, const wxString
& name
)
356 m_refData
= new wxVariantDataLong((long)val
);
360 wxVariant::wxVariant(short val
, const wxString
& name
)
362 m_refData
= new wxVariantDataLong((long)val
);
366 bool wxVariant::operator== (long value
) const
369 if (!Convert(&thisValue
))
372 return (value
== thisValue
);
375 bool wxVariant::operator!= (long value
) const
377 return (!((*this) == value
));
380 void wxVariant::operator= (long value
)
382 if (GetType() == wxT("long") &&
383 m_refData
->GetRefCount() == 1)
385 ((wxVariantDataLong
*)GetData())->SetValue(value
);
390 m_refData
= new wxVariantDataLong(value
);
394 long wxVariant::GetLong() const
397 if (Convert(& value
))
401 wxFAIL_MSG(wxT("Could not convert to a long"));
406 // -----------------------------------------------------------------
407 // wxVariantDoubleData
408 // -----------------------------------------------------------------
410 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
413 wxVariantDoubleData() { m_value
= 0.0; }
414 wxVariantDoubleData(double value
) { m_value
= value
; }
416 inline double GetValue() const { return m_value
; }
417 inline void SetValue(double value
) { m_value
= value
; }
419 virtual bool Eq(wxVariantData
& data
) const;
420 virtual bool Read(wxString
& str
);
421 #if wxUSE_STD_IOSTREAM
422 virtual bool Write(wxSTD ostream
& str
) const;
424 virtual bool Write(wxString
& str
) const;
425 #if wxUSE_STD_IOSTREAM
426 virtual bool Read(wxSTD istream
& str
);
429 virtual bool Read(wxInputStream
& str
);
430 virtual bool Write(wxOutputStream
&str
) const;
431 #endif // wxUSE_STREAMS
432 virtual wxString
GetType() const { return wxT("double"); }
434 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
436 DECLARE_WXANY_CONVERSION()
441 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(double, wxVariantDoubleData
)
443 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
445 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
447 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
449 return wxIsSameDouble(otherData
.m_value
, m_value
);
452 #if wxUSE_STD_IOSTREAM
453 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
457 str
<< (const char*) s
.mb_str();
462 bool wxVariantDoubleData::Write(wxString
& str
) const
464 str
.Printf(wxT("%.14g"), m_value
);
468 #if wxUSE_STD_IOSTREAM
469 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
477 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
479 wxTextOutputStream
s(str
);
480 s
.WriteDouble((double)m_value
);
484 bool wxVariantDoubleData::Read(wxInputStream
& str
)
486 wxTextInputStream
s(str
);
487 m_value
= (float)s
.ReadDouble();
490 #endif // wxUSE_STREAMS
492 bool wxVariantDoubleData::Read(wxString
& str
)
494 m_value
= wxAtof(str
);
498 // wxVariant double code
500 wxVariant::wxVariant(double val
, const wxString
& name
)
502 m_refData
= new wxVariantDoubleData(val
);
506 bool wxVariant::operator== (double value
) const
509 if (!Convert(&thisValue
))
512 return wxIsSameDouble(value
, thisValue
);
515 bool wxVariant::operator!= (double value
) const
517 return (!((*this) == value
));
520 void wxVariant::operator= (double value
)
522 if (GetType() == wxT("double") &&
523 m_refData
->GetRefCount() == 1)
525 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
530 m_refData
= new wxVariantDoubleData(value
);
534 double wxVariant::GetDouble() const
537 if (Convert(& value
))
541 wxFAIL_MSG(wxT("Could not convert to a double number"));
546 // -----------------------------------------------------------------
548 // -----------------------------------------------------------------
550 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
553 wxVariantDataBool() { m_value
= 0; }
554 wxVariantDataBool(bool value
) { m_value
= value
; }
556 inline bool GetValue() const { return m_value
; }
557 inline void SetValue(bool value
) { m_value
= value
; }
559 virtual bool Eq(wxVariantData
& data
) const;
560 #if wxUSE_STD_IOSTREAM
561 virtual bool Write(wxSTD ostream
& str
) const;
563 virtual bool Write(wxString
& str
) const;
564 virtual bool Read(wxString
& str
);
565 #if wxUSE_STD_IOSTREAM
566 virtual bool Read(wxSTD istream
& str
);
569 virtual bool Read(wxInputStream
& str
);
570 virtual bool Write(wxOutputStream
& str
) const;
571 #endif // wxUSE_STREAMS
572 virtual wxString
GetType() const { return wxT("bool"); }
574 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
576 DECLARE_WXANY_CONVERSION()
581 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(bool, wxVariantDataBool
)
583 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
585 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
587 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
589 return (otherData
.m_value
== m_value
);
592 #if wxUSE_STD_IOSTREAM
593 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
597 str
<< (const char*) s
.mb_str();
602 bool wxVariantDataBool::Write(wxString
& str
) const
604 str
.Printf(wxT("%d"), (int) m_value
);
608 #if wxUSE_STD_IOSTREAM
609 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
611 wxFAIL_MSG(wxT("Unimplemented"));
612 // str >> (long) m_value;
618 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
620 wxTextOutputStream
s(str
);
626 bool wxVariantDataBool::Read(wxInputStream
& str
)
628 wxTextInputStream
s(str
);
630 m_value
= s
.Read8() != 0;
633 #endif // wxUSE_STREAMS
635 bool wxVariantDataBool::Read(wxString
& str
)
637 m_value
= (wxAtol(str
) != 0);
643 wxVariant::wxVariant(bool val
, const wxString
& name
)
645 m_refData
= new wxVariantDataBool(val
);
649 bool wxVariant::operator== (bool value
) const
652 if (!Convert(&thisValue
))
655 return (value
== thisValue
);
658 bool wxVariant::operator!= (bool value
) const
660 return (!((*this) == value
));
663 void wxVariant::operator= (bool value
)
665 if (GetType() == wxT("bool") &&
666 m_refData
->GetRefCount() == 1)
668 ((wxVariantDataBool
*)GetData())->SetValue(value
);
673 m_refData
= new wxVariantDataBool(value
);
677 bool wxVariant::GetBool() const
680 if (Convert(& value
))
684 wxFAIL_MSG(wxT("Could not convert to a bool"));
689 // -----------------------------------------------------------------
691 // -----------------------------------------------------------------
693 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
696 wxVariantDataChar() { m_value
= 0; }
697 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
699 inline wxUniChar
GetValue() const { return m_value
; }
700 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
702 virtual bool Eq(wxVariantData
& data
) const;
703 #if wxUSE_STD_IOSTREAM
704 virtual bool Read(wxSTD istream
& str
);
705 virtual bool Write(wxSTD ostream
& str
) const;
707 virtual bool Read(wxString
& str
);
708 virtual bool Write(wxString
& str
) const;
710 virtual bool Read(wxInputStream
& str
);
711 virtual bool Write(wxOutputStream
& str
) const;
712 #endif // wxUSE_STREAMS
713 virtual wxString
GetType() const { return wxT("char"); }
714 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
716 DECLARE_WXANY_CONVERSION()
721 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxUniChar
, wxVariantDataChar
)
723 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
725 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
727 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
729 return (otherData
.m_value
== m_value
);
732 #if wxUSE_STD_IOSTREAM
733 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
735 str
<< wxString(m_value
);
740 bool wxVariantDataChar::Write(wxString
& str
) const
746 #if wxUSE_STD_IOSTREAM
747 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
749 wxFAIL_MSG(wxT("Unimplemented"));
756 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
758 wxTextOutputStream
s(str
);
760 // FIXME-UTF8: this should be just "s << m_value;" after removal of
761 // ANSI build and addition of wxUniChar to wxTextOutputStream:
762 s
<< (wxChar
)m_value
;
767 bool wxVariantDataChar::Read(wxInputStream
& str
)
769 wxTextInputStream
s(str
);
771 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
772 // ANSI build and addition of wxUniChar to wxTextInputStream:
779 #endif // wxUSE_STREAMS
781 bool wxVariantDataChar::Read(wxString
& str
)
787 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
789 m_refData
= new wxVariantDataChar(val
);
793 wxVariant::wxVariant(char val
, const wxString
& name
)
795 m_refData
= new wxVariantDataChar(val
);
799 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
801 m_refData
= new wxVariantDataChar(val
);
805 bool wxVariant::operator==(const wxUniChar
& value
) const
808 if (!Convert(&thisValue
))
811 return (value
== thisValue
);
814 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
816 if (GetType() == wxT("char") &&
817 m_refData
->GetRefCount() == 1)
819 ((wxVariantDataChar
*)GetData())->SetValue(value
);
824 m_refData
= new wxVariantDataChar(value
);
830 wxUniChar
wxVariant::GetChar() const
833 if (Convert(& value
))
837 wxFAIL_MSG(wxT("Could not convert to a char"));
842 // ----------------------------------------------------------------------------
843 // wxVariantDataString
844 // ----------------------------------------------------------------------------
846 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
849 wxVariantDataString() { }
850 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
852 inline wxString
GetValue() const { return m_value
; }
853 inline void SetValue(const wxString
& value
) { m_value
= value
; }
855 virtual bool Eq(wxVariantData
& data
) const;
856 #if wxUSE_STD_IOSTREAM
857 virtual bool Write(wxSTD ostream
& str
) const;
859 virtual bool Read(wxString
& str
);
860 virtual bool Write(wxString
& str
) const;
861 #if wxUSE_STD_IOSTREAM
862 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
865 virtual bool Read(wxInputStream
& str
);
866 virtual bool Write(wxOutputStream
& str
) const;
867 #endif // wxUSE_STREAMS
868 virtual wxString
GetType() const { return wxT("string"); }
869 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
871 DECLARE_WXANY_CONVERSION()
876 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxString
, wxVariantDataString
)
879 // This allows converting string literal wxAnys to string variants
880 wxVariantData
* wxVariantDataFromConstCharPAny(const wxAny
& any
)
882 return new wxVariantDataString(wxANY_AS(any
, const char*));
885 wxVariantData
* wxVariantDataFromConstWchar_tPAny(const wxAny
& any
)
887 return new wxVariantDataString(wxANY_AS(any
, const wchar_t*));
890 _REGISTER_WXANY_CONVERSION(const char*,
892 wxVariantDataFromConstCharPAny
)
893 _REGISTER_WXANY_CONVERSION(const wchar_t*,
895 wxVariantDataFromConstWchar_tPAny
)
898 bool wxVariantDataString::Eq(wxVariantData
& data
) const
900 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
902 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
904 return (otherData
.m_value
== m_value
);
907 #if wxUSE_STD_IOSTREAM
908 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
910 str
<< (const char*) m_value
.mb_str();
915 bool wxVariantDataString::Write(wxString
& str
) const
922 bool wxVariantDataString::Write(wxOutputStream
& str
) const
924 // why doesn't wxOutputStream::operator<< take "const wxString&"
925 wxTextOutputStream
s(str
);
926 s
.WriteString(m_value
);
930 bool wxVariantDataString::Read(wxInputStream
& str
)
932 wxTextInputStream
s(str
);
934 m_value
= s
.ReadLine();
937 #endif // wxUSE_STREAMS
939 bool wxVariantDataString::Read(wxString
& str
)
947 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
949 m_refData
= new wxVariantDataString(val
);
953 wxVariant::wxVariant(const char* val
, const wxString
& name
)
955 m_refData
= new wxVariantDataString(wxString(val
));
959 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
961 m_refData
= new wxVariantDataString(wxString(val
));
965 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
967 m_refData
= new wxVariantDataString(val
.AsString());
971 wxVariant::wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
)
973 m_refData
= new wxVariantDataString(wxString(val
));
977 wxVariant::wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
)
979 m_refData
= new wxVariantDataString(wxString(val
));
984 wxVariant::wxVariant(const std::string
& val
, const wxString
& name
)
986 m_refData
= new wxVariantDataString(wxString(val
));
990 wxVariant::wxVariant(const wxStdWideString
& val
, const wxString
& name
)
992 m_refData
= new wxVariantDataString(wxString(val
));
995 #endif // wxUSE_STD_STRING
997 bool wxVariant::operator== (const wxString
& value
) const
1000 if (!Convert(&thisValue
))
1003 return value
== thisValue
;
1006 bool wxVariant::operator!= (const wxString
& value
) const
1008 return (!((*this) == value
));
1011 wxVariant
& wxVariant::operator= (const wxString
& value
)
1013 if (GetType() == wxT("string") &&
1014 m_refData
->GetRefCount() == 1)
1016 ((wxVariantDataString
*)GetData())->SetValue(value
);
1021 m_refData
= new wxVariantDataString(value
);
1026 wxString
wxVariant::GetString() const
1029 if (!Convert(& value
))
1031 wxFAIL_MSG(wxT("Could not convert to a string"));
1037 // ----------------------------------------------------------------------------
1038 // wxVariantDataWxObjectPtr
1039 // ----------------------------------------------------------------------------
1041 class wxVariantDataWxObjectPtr
: public wxVariantData
1044 wxVariantDataWxObjectPtr() { }
1045 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
1047 inline wxObject
* GetValue() const { return m_value
; }
1048 inline void SetValue(wxObject
* value
) { m_value
= value
; }
1050 virtual bool Eq(wxVariantData
& data
) const;
1051 #if wxUSE_STD_IOSTREAM
1052 virtual bool Write(wxSTD ostream
& str
) const;
1054 virtual bool Write(wxString
& str
) const;
1055 #if wxUSE_STD_IOSTREAM
1056 virtual bool Read(wxSTD istream
& str
);
1058 virtual bool Read(wxString
& str
);
1059 virtual wxString
GetType() const ;
1060 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
1062 virtual wxClassInfo
* GetValueClassInfo();
1064 DECLARE_WXANY_CONVERSION()
1069 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxObject
*, wxVariantDataWxObjectPtr
)
1071 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
1073 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1075 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1077 return (otherData
.m_value
== m_value
);
1080 wxString
wxVariantDataWxObjectPtr::GetType() const
1082 wxString
returnVal(wxT("wxObject*"));
1086 returnVal
= m_value
->GetClassInfo()->GetClassName();
1087 returnVal
+= wxT("*");
1093 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1095 wxClassInfo
* returnVal
=NULL
;
1097 if (m_value
) returnVal
= m_value
->GetClassInfo();
1102 #if wxUSE_STD_IOSTREAM
1103 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1107 str
<< (const char*) s
.mb_str();
1112 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1114 str
.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value
));
1118 #if wxUSE_STD_IOSTREAM
1119 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1126 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1134 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1136 m_refData
= new wxVariantDataWxObjectPtr(val
);
1140 bool wxVariant::operator== (wxObject
* value
) const
1142 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1145 bool wxVariant::operator!= (wxObject
* value
) const
1147 return (!((*this) == (wxObject
*) value
));
1150 void wxVariant::operator= (wxObject
* value
)
1153 m_refData
= new wxVariantDataWxObjectPtr(value
);
1156 wxObject
* wxVariant::GetWxObjectPtr() const
1158 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_refData
)->GetValue();
1161 // ----------------------------------------------------------------------------
1162 // wxVariantDataVoidPtr
1163 // ----------------------------------------------------------------------------
1165 class wxVariantDataVoidPtr
: public wxVariantData
1168 wxVariantDataVoidPtr() { }
1169 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1171 inline void* GetValue() const { return m_value
; }
1172 inline void SetValue(void* value
) { m_value
= value
; }
1174 virtual bool Eq(wxVariantData
& data
) const;
1175 #if wxUSE_STD_IOSTREAM
1176 virtual bool Write(wxSTD ostream
& str
) const;
1178 virtual bool Write(wxString
& str
) const;
1179 #if wxUSE_STD_IOSTREAM
1180 virtual bool Read(wxSTD istream
& str
);
1182 virtual bool Read(wxString
& str
);
1183 virtual wxString
GetType() const { return wxT("void*"); }
1184 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1186 DECLARE_WXANY_CONVERSION()
1191 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(void*, wxVariantDataVoidPtr
)
1193 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1195 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1197 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1199 return (otherData
.m_value
== m_value
);
1202 #if wxUSE_STD_IOSTREAM
1203 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1207 str
<< (const char*) s
.mb_str();
1212 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1214 str
.Printf(wxT("%p"), m_value
);
1218 #if wxUSE_STD_IOSTREAM
1219 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1226 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1234 wxVariant::wxVariant( void* val
, const wxString
& name
)
1236 m_refData
= new wxVariantDataVoidPtr(val
);
1240 bool wxVariant::operator== (void* value
) const
1242 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1245 bool wxVariant::operator!= (void* value
) const
1247 return (!((*this) == (void*) value
));
1250 void wxVariant::operator= (void* value
)
1252 if (GetType() == wxT("void*") && (m_refData
->GetRefCount() == 1))
1254 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1259 m_refData
= new wxVariantDataVoidPtr(value
);
1263 void* wxVariant::GetVoidPtr() const
1265 // handling this specially is convenient when working with COM, see #9873
1269 wxASSERT( GetType() == wxT("void*") );
1271 return (void*) ((wxVariantDataVoidPtr
*) m_refData
)->GetValue();
1274 // ----------------------------------------------------------------------------
1275 // wxVariantDataDateTime
1276 // ----------------------------------------------------------------------------
1280 class wxVariantDataDateTime
: public wxVariantData
1283 wxVariantDataDateTime() { }
1284 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1286 inline wxDateTime
GetValue() const { return m_value
; }
1287 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1289 virtual bool Eq(wxVariantData
& data
) const;
1290 #if wxUSE_STD_IOSTREAM
1291 virtual bool Write(wxSTD ostream
& str
) const;
1293 virtual bool Write(wxString
& str
) const;
1294 #if wxUSE_STD_IOSTREAM
1295 virtual bool Read(wxSTD istream
& str
);
1297 virtual bool Read(wxString
& str
);
1298 virtual wxString
GetType() const { return wxT("datetime"); }
1299 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1301 DECLARE_WXANY_CONVERSION()
1306 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxDateTime
, wxVariantDataDateTime
)
1308 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1310 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1312 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1314 return (otherData
.m_value
== m_value
);
1318 #if wxUSE_STD_IOSTREAM
1319 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1323 str
<< value
.c_str();
1329 bool wxVariantDataDateTime::Write(wxString
& str
) const
1331 if ( m_value
.IsValid() )
1332 str
= m_value
.Format();
1334 str
= wxS("Invalid");
1339 #if wxUSE_STD_IOSTREAM
1340 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1348 bool wxVariantDataDateTime::Read(wxString
& str
)
1350 if ( str
== wxS("Invalid") )
1352 m_value
= wxInvalidDateTime
;
1356 wxString::const_iterator end
;
1357 return m_value
.ParseDateTime(str
, &end
) && end
== str
.end();
1362 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1364 m_refData
= new wxVariantDataDateTime(val
);
1368 bool wxVariant::operator== (const wxDateTime
& value
) const
1370 wxDateTime thisValue
;
1371 if (!Convert(&thisValue
))
1374 return value
.IsEqualTo(thisValue
);
1377 bool wxVariant::operator!= (const wxDateTime
& value
) const
1379 return (!((*this) == value
));
1382 void wxVariant::operator= (const wxDateTime
& value
)
1384 if (GetType() == wxT("datetime") &&
1385 m_refData
->GetRefCount() == 1)
1387 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1392 m_refData
= new wxVariantDataDateTime(value
);
1396 wxDateTime
wxVariant::GetDateTime() const
1399 if (!Convert(& value
))
1401 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1407 #endif // wxUSE_DATETIME
1409 // ----------------------------------------------------------------------------
1410 // wxVariantDataArrayString
1411 // ----------------------------------------------------------------------------
1413 class wxVariantDataArrayString
: public wxVariantData
1416 wxVariantDataArrayString() { }
1417 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1419 wxArrayString
GetValue() const { return m_value
; }
1420 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1422 virtual bool Eq(wxVariantData
& data
) const;
1423 #if wxUSE_STD_IOSTREAM
1424 virtual bool Write(wxSTD ostream
& str
) const;
1426 virtual bool Write(wxString
& str
) const;
1427 #if wxUSE_STD_IOSTREAM
1428 virtual bool Read(wxSTD istream
& str
);
1430 virtual bool Read(wxString
& str
);
1431 virtual wxString
GetType() const { return wxT("arrstring"); }
1432 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1434 DECLARE_WXANY_CONVERSION()
1436 wxArrayString m_value
;
1439 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxArrayString
, wxVariantDataArrayString
)
1441 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1443 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1445 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1447 return otherData
.m_value
== m_value
;
1450 #if wxUSE_STD_IOSTREAM
1451 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1458 bool wxVariantDataArrayString::Write(wxString
& str
) const
1460 size_t count
= m_value
.GetCount();
1461 for ( size_t n
= 0; n
< count
; n
++ )
1473 #if wxUSE_STD_IOSTREAM
1474 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1482 bool wxVariantDataArrayString::Read(wxString
& str
)
1484 wxStringTokenizer
tk(str
, wxT(";"));
1485 while ( tk
.HasMoreTokens() )
1487 m_value
.Add(tk
.GetNextToken());
1495 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1497 m_refData
= new wxVariantDataArrayString(val
);
1501 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1503 wxFAIL_MSG( wxT("TODO") );
1508 bool wxVariant::operator!=(const wxArrayString
& value
) const
1510 return !(*this == value
);
1513 void wxVariant::operator=(const wxArrayString
& value
)
1515 if (GetType() == wxT("arrstring") &&
1516 m_refData
->GetRefCount() == 1)
1518 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1523 m_refData
= new wxVariantDataArrayString(value
);
1527 wxArrayString
wxVariant::GetArrayString() const
1529 if ( GetType() == wxT("arrstring") )
1530 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1532 return wxArrayString();
1535 // ----------------------------------------------------------------------------
1536 // wxVariantDataLongLong
1537 // ----------------------------------------------------------------------------
1541 class WXDLLIMPEXP_BASE wxVariantDataLongLong
: public wxVariantData
1544 wxVariantDataLongLong() { m_value
= 0; }
1545 wxVariantDataLongLong(wxLongLong value
) { m_value
= value
; }
1547 wxLongLong
GetValue() const { return m_value
; }
1548 void SetValue(wxLongLong value
) { m_value
= value
; }
1550 virtual bool Eq(wxVariantData
& data
) const;
1552 virtual bool Read(wxString
& str
);
1553 virtual bool Write(wxString
& str
) const;
1554 #if wxUSE_STD_IOSTREAM
1555 virtual bool Read(wxSTD istream
& str
);
1556 virtual bool Write(wxSTD ostream
& str
) const;
1559 virtual bool Read(wxInputStream
& str
);
1560 virtual bool Write(wxOutputStream
&str
) const;
1561 #endif // wxUSE_STREAMS
1563 wxVariantData
* Clone() const
1565 return new wxVariantDataLongLong(m_value
);
1568 virtual wxString
GetType() const { return wxS("longlong"); }
1570 DECLARE_WXANY_CONVERSION()
1576 // wxLongLong type requires customized wxAny conversion code
1581 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1583 *any
= m_value
.GetValue();
1587 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1589 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong_t
));
1592 REGISTER_WXANY_CONVERSION(wxLongLong_t
, wxVariantDataLongLong
)
1594 #else // if !defined(wxLongLong_t)
1596 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1602 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1604 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong
));
1607 REGISTER_WXANY_CONVERSION(wxLongLong
, wxVariantDataLongLong
)
1609 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1612 bool wxVariantDataLongLong::Eq(wxVariantData
& data
) const
1614 wxASSERT_MSG( (data
.GetType() == wxS("longlong")),
1615 "wxVariantDataLongLong::Eq: argument mismatch" );
1617 wxVariantDataLongLong
& otherData
= (wxVariantDataLongLong
&) data
;
1619 return (otherData
.m_value
== m_value
);
1622 #if wxUSE_STD_IOSTREAM
1623 bool wxVariantDataLongLong::Write(wxSTD ostream
& str
) const
1627 str
<< (const char*) s
.mb_str();
1632 bool wxVariantDataLongLong::Write(wxString
& str
) const
1635 str
.Printf(wxS("%lld"), m_value
.GetValue());
1642 #if wxUSE_STD_IOSTREAM
1643 bool wxVariantDataLongLong::Read(wxSTD istream
& WXUNUSED(str
))
1645 wxFAIL_MSG(wxS("Unimplemented"));
1651 bool wxVariantDataLongLong::Write(wxOutputStream
& str
) const
1653 wxTextOutputStream
s(str
);
1654 s
.Write32(m_value
.GetLo());
1655 s
.Write32(m_value
.GetHi());
1659 bool wxVariantDataLongLong::Read(wxInputStream
& str
)
1661 wxTextInputStream
s(str
);
1662 unsigned long lo
= s
.Read32();
1663 long hi
= s
.Read32();
1664 m_value
= wxLongLong(hi
, lo
);
1667 #endif // wxUSE_STREAMS
1669 bool wxVariantDataLongLong::Read(wxString
& str
)
1672 wxLongLong_t value_t
;
1673 if ( !str
.ToLongLong(&value_t
) )
1684 wxVariant::wxVariant(wxLongLong val
, const wxString
& name
)
1686 m_refData
= new wxVariantDataLongLong(val
);
1690 bool wxVariant::operator==(wxLongLong value
) const
1692 wxLongLong thisValue
;
1693 if ( !Convert(&thisValue
) )
1696 return (value
== thisValue
);
1699 bool wxVariant::operator!=(wxLongLong value
) const
1701 return (!((*this) == value
));
1704 void wxVariant::operator=(wxLongLong value
)
1706 if ( GetType() == wxS("longlong") &&
1707 m_refData
->GetRefCount() == 1 )
1709 ((wxVariantDataLongLong
*)GetData())->SetValue(value
);
1714 m_refData
= new wxVariantDataLongLong(value
);
1718 wxLongLong
wxVariant::GetLongLong() const
1721 if ( Convert(&value
) )
1727 wxFAIL_MSG(wxT("Could not convert to a long long"));
1732 #endif // wxUSE_LONGLONG
1734 // ----------------------------------------------------------------------------
1735 // wxVariantDataULongLong
1736 // ----------------------------------------------------------------------------
1740 class WXDLLIMPEXP_BASE wxVariantDataULongLong
: public wxVariantData
1743 wxVariantDataULongLong() { m_value
= 0; }
1744 wxVariantDataULongLong(wxULongLong value
) { m_value
= value
; }
1746 wxULongLong
GetValue() const { return m_value
; }
1747 void SetValue(wxULongLong value
) { m_value
= value
; }
1749 virtual bool Eq(wxVariantData
& data
) const;
1751 virtual bool Read(wxString
& str
);
1752 virtual bool Write(wxString
& str
) const;
1753 #if wxUSE_STD_IOSTREAM
1754 virtual bool Read(wxSTD istream
& str
);
1755 virtual bool Write(wxSTD ostream
& str
) const;
1758 virtual bool Read(wxInputStream
& str
);
1759 virtual bool Write(wxOutputStream
&str
) const;
1760 #endif // wxUSE_STREAMS
1762 wxVariantData
* Clone() const
1764 return new wxVariantDataULongLong(m_value
);
1767 virtual wxString
GetType() const { return wxS("ulonglong"); }
1769 DECLARE_WXANY_CONVERSION()
1771 wxULongLong m_value
;
1775 // wxULongLong type requires customized wxAny conversion code
1780 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1782 *any
= m_value
.GetValue();
1786 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1788 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong_t
));
1791 REGISTER_WXANY_CONVERSION(wxULongLong_t
, wxVariantDataULongLong
)
1793 #else // if !defined(wxLongLong_t)
1795 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1801 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1803 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong
));
1806 REGISTER_WXANY_CONVERSION(wxULongLong
, wxVariantDataULongLong
)
1808 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1812 bool wxVariantDataULongLong::Eq(wxVariantData
& data
) const
1814 wxASSERT_MSG( (data
.GetType() == wxS("ulonglong")),
1815 "wxVariantDataULongLong::Eq: argument mismatch" );
1817 wxVariantDataULongLong
& otherData
= (wxVariantDataULongLong
&) data
;
1819 return (otherData
.m_value
== m_value
);
1822 #if wxUSE_STD_IOSTREAM
1823 bool wxVariantDataULongLong::Write(wxSTD ostream
& str
) const
1827 str
<< (const char*) s
.mb_str();
1832 bool wxVariantDataULongLong::Write(wxString
& str
) const
1835 str
.Printf(wxS("%llu"), m_value
.GetValue());
1842 #if wxUSE_STD_IOSTREAM
1843 bool wxVariantDataULongLong::Read(wxSTD istream
& WXUNUSED(str
))
1845 wxFAIL_MSG(wxS("Unimplemented"));
1851 bool wxVariantDataULongLong::Write(wxOutputStream
& str
) const
1853 wxTextOutputStream
s(str
);
1854 s
.Write32(m_value
.GetLo());
1855 s
.Write32(m_value
.GetHi());
1859 bool wxVariantDataULongLong::Read(wxInputStream
& str
)
1861 wxTextInputStream
s(str
);
1862 unsigned long lo
= s
.Read32();
1863 long hi
= s
.Read32();
1864 m_value
= wxULongLong(hi
, lo
);
1867 #endif // wxUSE_STREAMS
1869 bool wxVariantDataULongLong::Read(wxString
& str
)
1872 wxULongLong_t value_t
;
1873 if ( !str
.ToULongLong(&value_t
) )
1884 wxVariant::wxVariant(wxULongLong val
, const wxString
& name
)
1886 m_refData
= new wxVariantDataULongLong(val
);
1890 bool wxVariant::operator==(wxULongLong value
) const
1892 wxULongLong thisValue
;
1893 if ( !Convert(&thisValue
) )
1896 return (value
== thisValue
);
1899 bool wxVariant::operator!=(wxULongLong value
) const
1901 return (!((*this) == value
));
1904 void wxVariant::operator=(wxULongLong value
)
1906 if ( GetType() == wxS("ulonglong") &&
1907 m_refData
->GetRefCount() == 1 )
1909 ((wxVariantDataULongLong
*)GetData())->SetValue(value
);
1914 m_refData
= new wxVariantDataULongLong(value
);
1918 wxULongLong
wxVariant::GetULongLong() const
1921 if ( Convert(&value
) )
1927 wxFAIL_MSG(wxT("Could not convert to a long long"));
1932 #endif // wxUSE_LONGLONG
1934 // ----------------------------------------------------------------------------
1935 // wxVariantDataList
1936 // ----------------------------------------------------------------------------
1938 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1941 wxVariantDataList() {}
1942 wxVariantDataList(const wxVariantList
& list
);
1943 virtual ~wxVariantDataList();
1945 wxVariantList
& GetValue() { return m_value
; }
1946 void SetValue(const wxVariantList
& value
) ;
1948 virtual bool Eq(wxVariantData
& data
) const;
1949 #if wxUSE_STD_IOSTREAM
1950 virtual bool Write(wxSTD ostream
& str
) const;
1952 virtual bool Write(wxString
& str
) const;
1953 #if wxUSE_STD_IOSTREAM
1954 virtual bool Read(wxSTD istream
& str
);
1956 virtual bool Read(wxString
& str
);
1957 virtual wxString
GetType() const { return wxT("list"); }
1961 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1963 DECLARE_WXANY_CONVERSION()
1965 wxVariantList m_value
;
1971 // Convert to/from list of wxAnys
1974 bool wxVariantDataList::GetAsAny(wxAny
* any
) const
1977 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1980 wxVariant
* pVar
= node
->GetData();
1981 dst
.push_back(new wxAny(((const wxVariant
&)*pVar
)));
1982 node
= node
->GetNext();
1989 wxVariantData
* wxVariantDataList::VariantDataFactory(const wxAny
& any
)
1991 wxAnyList src
= wxANY_AS(any
, wxAnyList
);
1993 wxAnyList::compatibility_iterator node
= src
.GetFirst();
1996 wxAny
* pAny
= node
->GetData();
1997 dst
.push_back(new wxVariant(*pAny
));
1998 node
= node
->GetNext();
2001 return new wxVariantDataList(dst
);
2004 REGISTER_WXANY_CONVERSION(wxAnyList
, wxVariantDataList
)
2008 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
2013 wxVariantDataList::~wxVariantDataList()
2018 void wxVariantDataList::SetValue(const wxVariantList
& value
)
2021 wxVariantList::compatibility_iterator node
= value
.GetFirst();
2024 wxVariant
* var
= node
->GetData();
2025 m_value
.Append(new wxVariant(*var
));
2026 node
= node
->GetNext();
2030 void wxVariantDataList::Clear()
2032 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2035 wxVariant
* var
= node
->GetData();
2037 node
= node
->GetNext();
2042 bool wxVariantDataList::Eq(wxVariantData
& data
) const
2044 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
2046 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
2047 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
2048 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
2049 while (node1
&& node2
)
2051 wxVariant
* var1
= node1
->GetData();
2052 wxVariant
* var2
= node2
->GetData();
2053 if ((*var1
) != (*var2
))
2055 node1
= node1
->GetNext();
2056 node2
= node2
->GetNext();
2058 if (node1
|| node2
) return false;
2062 #if wxUSE_STD_IOSTREAM
2063 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
2067 str
<< (const char*) s
.mb_str();
2072 bool wxVariantDataList::Write(wxString
& str
) const
2074 str
= wxEmptyString
;
2075 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2078 wxVariant
* var
= node
->GetData();
2079 if (node
!= m_value
.GetFirst())
2082 str
+= var
->MakeString();
2083 node
= node
->GetNext();
2089 #if wxUSE_STD_IOSTREAM
2090 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
2092 wxFAIL_MSG(wxT("Unimplemented"));
2098 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
2100 wxFAIL_MSG(wxT("Unimplemented"));
2107 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
2109 m_refData
= new wxVariantDataList(val
);
2113 bool wxVariant::operator== (const wxVariantList
& value
) const
2115 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
2117 wxVariantDataList
other(value
);
2118 return (GetData()->Eq(other
));
2121 bool wxVariant::operator!= (const wxVariantList
& value
) const
2123 return (!((*this) == value
));
2126 void wxVariant::operator= (const wxVariantList
& value
)
2128 if (GetType() == wxT("list") &&
2129 m_refData
->GetRefCount() == 1)
2131 ((wxVariantDataList
*)GetData())->SetValue(value
);
2136 m_refData
= new wxVariantDataList(value
);
2140 wxVariantList
& wxVariant::GetList() const
2142 wxASSERT( (GetType() == wxT("list")) );
2144 return (wxVariantList
&) ((wxVariantDataList
*) m_refData
)->GetValue();
2148 void wxVariant::NullList()
2150 SetData(new wxVariantDataList());
2154 void wxVariant::Append(const wxVariant
& value
)
2156 wxVariantList
& list
= GetList();
2158 list
.Append(new wxVariant(value
));
2161 // Insert at front of list
2162 void wxVariant::Insert(const wxVariant
& value
)
2164 wxVariantList
& list
= GetList();
2166 list
.Insert(new wxVariant(value
));
2169 // Returns true if the variant is a member of the list
2170 bool wxVariant::Member(const wxVariant
& value
) const
2172 wxVariantList
& list
= GetList();
2174 wxVariantList::compatibility_iterator node
= list
.GetFirst();
2177 wxVariant
* other
= node
->GetData();
2178 if (value
== *other
)
2180 node
= node
->GetNext();
2185 // Deletes the nth element of the list
2186 bool wxVariant::Delete(size_t item
)
2188 wxVariantList
& list
= GetList();
2190 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
2191 wxVariantList::compatibility_iterator node
= list
.Item(item
);
2192 wxVariant
* variant
= node
->GetData();
2199 void wxVariant::ClearList()
2201 if (!IsNull() && (GetType() == wxT("list")))
2203 ((wxVariantDataList
*) m_refData
)->Clear();
2207 if (!GetType().IsSameAs(wxT("list")))
2210 m_refData
= new wxVariantDataList
;
2214 // Treat a list variant as an array
2215 wxVariant
wxVariant::operator[] (size_t idx
) const
2217 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
2219 if (GetType() == wxT("list"))
2221 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2222 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2223 return *(data
->GetValue().Item(idx
)->GetData());
2225 return wxNullVariant
;
2228 wxVariant
& wxVariant::operator[] (size_t idx
)
2230 // We can't return a reference to a variant for a string list, since the string
2231 // is actually stored as a char*, not a variant.
2233 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
2235 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2236 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2238 return * (data
->GetValue().Item(idx
)->GetData());
2241 // Return the number of elements in a list
2242 size_t wxVariant::GetCount() const
2244 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
2246 if (GetType() == wxT("list"))
2248 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2249 return data
->GetValue().GetCount();
2254 // ----------------------------------------------------------------------------
2256 // ----------------------------------------------------------------------------
2258 bool wxVariant::Convert(long* value
) const
2260 wxString
type(GetType());
2261 if (type
== wxS("double"))
2262 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
2263 else if (type
== wxS("long"))
2264 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2265 else if (type
== wxS("bool"))
2266 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2267 else if (type
== wxS("string"))
2268 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
2270 else if (type
== wxS("longlong"))
2272 wxLongLong v
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2273 // Don't convert if return value would be vague
2274 if ( v
< LONG_MIN
|| v
> LONG_MAX
)
2276 *value
= v
.ToLong();
2278 else if (type
== wxS("ulonglong"))
2280 wxULongLong v
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2281 // Don't convert if return value would be vague
2284 *value
= (long) v
.ToULong();
2293 bool wxVariant::Convert(bool* value
) const
2295 wxString
type(GetType());
2296 if (type
== wxT("double"))
2297 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
2298 else if (type
== wxT("long"))
2299 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2300 else if (type
== wxT("bool"))
2301 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2302 else if (type
== wxT("string"))
2304 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2306 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
2308 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
2319 bool wxVariant::Convert(double* value
) const
2321 wxString
type(GetType());
2322 if (type
== wxT("double"))
2323 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
2324 else if (type
== wxT("long"))
2325 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2326 else if (type
== wxT("bool"))
2327 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2328 else if (type
== wxT("string"))
2329 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
2331 else if (type
== wxS("longlong"))
2333 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue().ToDouble();
2335 else if (type
== wxS("ulonglong"))
2337 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue().ToDouble();
2346 bool wxVariant::Convert(wxUniChar
* value
) const
2348 wxString
type(GetType());
2349 if (type
== wxT("char"))
2350 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2351 else if (type
== wxT("long"))
2352 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2353 else if (type
== wxT("bool"))
2354 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2355 else if (type
== wxS("string"))
2357 // Also accept strings of length 1
2358 const wxString
& str
= (((wxVariantDataString
*)GetData())->GetValue());
2359 if ( str
.length() == 1 )
2370 bool wxVariant::Convert(char* value
) const
2373 if ( !Convert(&ch
) )
2379 bool wxVariant::Convert(wchar_t* value
) const
2382 if ( !Convert(&ch
) )
2388 bool wxVariant::Convert(wxString
* value
) const
2390 *value
= MakeString();
2395 bool wxVariant::Convert(wxLongLong
* value
) const
2397 wxString
type(GetType());
2398 if (type
== wxS("longlong"))
2399 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2400 else if (type
== wxS("long"))
2401 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2402 else if (type
== wxS("string"))
2404 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2406 wxLongLong_t value_t
;
2407 if ( !s
.ToLongLong(&value_t
) )
2412 if ( !s
.ToLong(&l_value
) )
2417 else if (type
== wxS("bool"))
2418 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2419 else if (type
== wxS("double"))
2421 value
->Assign(((wxVariantDoubleData
*)GetData())->GetValue());
2423 else if (type
== wxS("ulonglong"))
2424 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2431 bool wxVariant::Convert(wxULongLong
* value
) const
2433 wxString
type(GetType());
2434 if (type
== wxS("ulonglong"))
2435 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2436 else if (type
== wxS("long"))
2437 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2438 else if (type
== wxS("string"))
2440 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2442 wxULongLong_t value_t
;
2443 if ( !s
.ToULongLong(&value_t
) )
2447 unsigned long l_value
;
2448 if ( !s
.ToULong(&l_value
) )
2453 else if (type
== wxS("bool"))
2454 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2455 else if (type
== wxS("double"))
2457 double value_d
= ((wxVariantDoubleData
*)GetData())->GetValue();
2459 if ( value_d
< 0.0 )
2463 *value
= (wxULongLong_t
) value_d
;
2466 temp
.Assign(value_d
);
2470 else if (type
== wxS("longlong"))
2471 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2477 #endif // wxUSE_LONGLONG
2480 bool wxVariant::Convert(wxDateTime
* value
) const
2482 wxString
type(GetType());
2483 if (type
== wxT("datetime"))
2485 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2489 // Fallback to string conversion
2491 if ( !Convert(&val
) )
2494 // Try to parse this as either date and time, only date or only time
2495 // checking that the entire string was parsed
2496 wxString::const_iterator end
;
2497 if ( value
->ParseDateTime(val
, &end
) && end
== val
.end() )
2500 if ( value
->ParseDate(val
, &end
) && end
== val
.end() )
2503 if ( value
->ParseTime(val
, &end
) && end
== val
.end() )
2508 #endif // wxUSE_DATETIME
2510 #endif // wxUSE_VARIANT