1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/variant.cpp
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/variant.h"
24 #include "wx/string.h"
28 #include "wx/stream.h"
32 #if wxUSE_STD_IOSTREAM
40 #if defined(__MWERKS__) && __MSL__ >= 0x6000
46 #include "wx/txtstrm.h"
49 #include "wx/string.h"
50 #include "wx/tokenzr.h"
52 wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
55 #include "wx/listimpl.cpp"
56 WX_DEFINE_LIST(wxVariantList
)
62 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
64 wxVariant::wxVariant()
69 bool wxVariant::IsNull() const
71 return (m_refData
== NULL
);
74 void wxVariant::MakeNull()
79 void wxVariant::Clear()
81 m_name
= wxEmptyString
;
84 wxVariant::wxVariant(const wxVariant
& variant
)
87 if (!variant
.IsNull())
90 m_name
= variant
.m_name
;
93 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
100 wxVariant::~wxVariant()
104 wxObjectRefData
*wxVariant::CreateRefData() const
106 // We cannot create any particular wxVariantData.
107 wxFAIL_MSG("wxVariant::CreateRefData() cannot be implemented");
111 wxObjectRefData
*wxVariant::CloneRefData(const wxObjectRefData
*data
) const
113 return ((wxVariantData
*) data
)->Clone();
117 void wxVariant::operator= (const wxVariant
& variant
)
120 m_name
= variant
.m_name
;
123 // myVariant = new wxStringVariantData("hello")
124 void wxVariant::operator= (wxVariantData
* variantData
)
127 m_refData
= variantData
;
130 bool wxVariant::operator== (const wxVariant
& variant
) const
132 if (IsNull() || variant
.IsNull())
133 return (IsNull() == variant
.IsNull());
135 if (GetType() != variant
.GetType())
138 return (GetData()->Eq(* variant
.GetData()));
141 bool wxVariant::operator!= (const wxVariant
& variant
) const
143 return (!(*this == variant
));
146 wxString
wxVariant::MakeString() const
151 if (GetData()->Write(str
))
154 return wxEmptyString
;
157 void wxVariant::SetData(wxVariantData
* data
)
163 bool wxVariant::Unshare()
165 if ( !m_refData
|| m_refData
->GetRefCount() == 1 )
170 return (m_refData
&& m_refData
->GetRefCount() == 1);
174 // Returns a string representing the type of the variant,
175 // e.g. "string", "bool", "list", "double", "long"
176 wxString
wxVariant::GetType() const
179 return wxString(wxT("null"));
181 return GetData()->GetType();
185 bool wxVariant::IsType(const wxString
& type
) const
187 return (GetType() == type
);
190 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
192 wxClassInfo
* info
=GetData()->GetValueClassInfo();
193 return info
? info
->IsKindOf(type
) : false ;
196 // -----------------------------------------------------------------
197 // wxVariant <-> wxAny conversion code
198 // -----------------------------------------------------------------
202 wxAnyToVariantRegistration::
203 wxAnyToVariantRegistration(wxVariantDataFactory factory
)
206 wxPreRegisterAnyToVariant(this);
209 wxAnyToVariantRegistration::~wxAnyToVariantRegistration()
213 wxVariant::wxVariant(const wxAny
& any
)
217 if ( !any
.GetAs(&variant
) )
219 wxFAIL_MSG("wxAny of this type cannot be converted to wxVariant");
226 wxAny
wxVariant::GetAny() const
232 wxVariantData
* data
= GetData();
234 if ( data
->GetAsAny(&any
) )
237 // If everything else fails, wrap the whole wxVariantData
243 // -----------------------------------------------------------------
245 // -----------------------------------------------------------------
247 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
250 wxVariantDataLong() { m_value
= 0; }
251 wxVariantDataLong(long value
) { m_value
= value
; }
253 inline long GetValue() const { return m_value
; }
254 inline void SetValue(long value
) { m_value
= value
; }
256 virtual bool Eq(wxVariantData
& data
) const;
258 virtual bool Read(wxString
& str
);
259 virtual bool Write(wxString
& str
) const;
260 #if wxUSE_STD_IOSTREAM
261 virtual bool Read(wxSTD istream
& str
);
262 virtual bool Write(wxSTD ostream
& str
) const;
265 virtual bool Read(wxInputStream
& str
);
266 virtual bool Write(wxOutputStream
&str
) const;
267 #endif // wxUSE_STREAMS
269 wxVariantData
* Clone() const { return new wxVariantDataLong(m_value
); }
271 virtual wxString
GetType() const { return wxT("long"); }
273 // Since wxAny does not have separate type for integers shorter than
274 // longlong, we do not usually implement wxVariant->wxAny conversion
275 // here (but in wxVariantDataLongLong instead).
277 DECLARE_WXANY_CONVERSION()
279 bool GetAsAny(wxAny
* any
) const
291 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(long, wxVariantDataLong
)
294 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
296 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
298 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
300 return (otherData
.m_value
== m_value
);
303 #if wxUSE_STD_IOSTREAM
304 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
308 str
<< (const char*) s
.mb_str();
313 bool wxVariantDataLong::Write(wxString
& str
) const
315 str
.Printf(wxT("%ld"), m_value
);
319 #if wxUSE_STD_IOSTREAM
320 bool wxVariantDataLong::Read(wxSTD istream
& str
)
328 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
330 wxTextOutputStream
s(str
);
332 s
.Write32((size_t)m_value
);
336 bool wxVariantDataLong::Read(wxInputStream
& str
)
338 wxTextInputStream
s(str
);
339 m_value
= s
.Read32();
342 #endif // wxUSE_STREAMS
344 bool wxVariantDataLong::Read(wxString
& str
)
346 m_value
= wxAtol(str
);
352 wxVariant::wxVariant(long val
, const wxString
& name
)
354 m_refData
= new wxVariantDataLong(val
);
358 wxVariant::wxVariant(int val
, const wxString
& name
)
360 m_refData
= new wxVariantDataLong((long)val
);
364 wxVariant::wxVariant(short val
, const wxString
& name
)
366 m_refData
= new wxVariantDataLong((long)val
);
370 bool wxVariant::operator== (long value
) const
373 if (!Convert(&thisValue
))
376 return (value
== thisValue
);
379 bool wxVariant::operator!= (long value
) const
381 return (!((*this) == value
));
384 void wxVariant::operator= (long value
)
386 if (GetType() == wxT("long") &&
387 m_refData
->GetRefCount() == 1)
389 ((wxVariantDataLong
*)GetData())->SetValue(value
);
394 m_refData
= new wxVariantDataLong(value
);
398 long wxVariant::GetLong() const
401 if (Convert(& value
))
405 wxFAIL_MSG(wxT("Could not convert to a long"));
410 // -----------------------------------------------------------------
411 // wxVariantDoubleData
412 // -----------------------------------------------------------------
414 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
417 wxVariantDoubleData() { m_value
= 0.0; }
418 wxVariantDoubleData(double value
) { m_value
= value
; }
420 inline double GetValue() const { return m_value
; }
421 inline void SetValue(double value
) { m_value
= value
; }
423 virtual bool Eq(wxVariantData
& data
) const;
424 virtual bool Read(wxString
& str
);
425 #if wxUSE_STD_IOSTREAM
426 virtual bool Write(wxSTD ostream
& str
) const;
428 virtual bool Write(wxString
& str
) const;
429 #if wxUSE_STD_IOSTREAM
430 virtual bool Read(wxSTD istream
& str
);
433 virtual bool Read(wxInputStream
& str
);
434 virtual bool Write(wxOutputStream
&str
) const;
435 #endif // wxUSE_STREAMS
436 virtual wxString
GetType() const { return wxT("double"); }
438 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
440 DECLARE_WXANY_CONVERSION()
445 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(double, wxVariantDoubleData
)
447 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
449 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
451 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
453 return wxIsSameDouble(otherData
.m_value
, m_value
);
456 #if wxUSE_STD_IOSTREAM
457 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
461 str
<< (const char*) s
.mb_str();
466 bool wxVariantDoubleData::Write(wxString
& str
) const
468 str
.Printf(wxT("%.14g"), m_value
);
472 #if wxUSE_STD_IOSTREAM
473 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
481 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
483 wxTextOutputStream
s(str
);
484 s
.WriteDouble((double)m_value
);
488 bool wxVariantDoubleData::Read(wxInputStream
& str
)
490 wxTextInputStream
s(str
);
491 m_value
= (float)s
.ReadDouble();
494 #endif // wxUSE_STREAMS
496 bool wxVariantDoubleData::Read(wxString
& str
)
498 m_value
= wxAtof(str
);
502 // wxVariant double code
504 wxVariant::wxVariant(double val
, const wxString
& name
)
506 m_refData
= new wxVariantDoubleData(val
);
510 bool wxVariant::operator== (double value
) const
513 if (!Convert(&thisValue
))
516 return wxIsSameDouble(value
, thisValue
);
519 bool wxVariant::operator!= (double value
) const
521 return (!((*this) == value
));
524 void wxVariant::operator= (double value
)
526 if (GetType() == wxT("double") &&
527 m_refData
->GetRefCount() == 1)
529 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
534 m_refData
= new wxVariantDoubleData(value
);
538 double wxVariant::GetDouble() const
541 if (Convert(& value
))
545 wxFAIL_MSG(wxT("Could not convert to a double number"));
550 // -----------------------------------------------------------------
552 // -----------------------------------------------------------------
554 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
557 wxVariantDataBool() { m_value
= 0; }
558 wxVariantDataBool(bool value
) { m_value
= value
; }
560 inline bool GetValue() const { return m_value
; }
561 inline void SetValue(bool value
) { m_value
= value
; }
563 virtual bool Eq(wxVariantData
& data
) const;
564 #if wxUSE_STD_IOSTREAM
565 virtual bool Write(wxSTD ostream
& str
) const;
567 virtual bool Write(wxString
& str
) const;
568 virtual bool Read(wxString
& str
);
569 #if wxUSE_STD_IOSTREAM
570 virtual bool Read(wxSTD istream
& str
);
573 virtual bool Read(wxInputStream
& str
);
574 virtual bool Write(wxOutputStream
& str
) const;
575 #endif // wxUSE_STREAMS
576 virtual wxString
GetType() const { return wxT("bool"); }
578 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
580 DECLARE_WXANY_CONVERSION()
585 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(bool, wxVariantDataBool
)
587 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
589 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
591 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
593 return (otherData
.m_value
== m_value
);
596 #if wxUSE_STD_IOSTREAM
597 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
601 str
<< (const char*) s
.mb_str();
606 bool wxVariantDataBool::Write(wxString
& str
) const
608 str
.Printf(wxT("%d"), (int) m_value
);
612 #if wxUSE_STD_IOSTREAM
613 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
615 wxFAIL_MSG(wxT("Unimplemented"));
616 // str >> (long) m_value;
622 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
624 wxTextOutputStream
s(str
);
630 bool wxVariantDataBool::Read(wxInputStream
& str
)
632 wxTextInputStream
s(str
);
634 m_value
= s
.Read8() != 0;
637 #endif // wxUSE_STREAMS
639 bool wxVariantDataBool::Read(wxString
& str
)
641 m_value
= (wxAtol(str
) != 0);
647 wxVariant::wxVariant(bool val
, const wxString
& name
)
649 m_refData
= new wxVariantDataBool(val
);
653 bool wxVariant::operator== (bool value
) const
656 if (!Convert(&thisValue
))
659 return (value
== thisValue
);
662 bool wxVariant::operator!= (bool value
) const
664 return (!((*this) == value
));
667 void wxVariant::operator= (bool value
)
669 if (GetType() == wxT("bool") &&
670 m_refData
->GetRefCount() == 1)
672 ((wxVariantDataBool
*)GetData())->SetValue(value
);
677 m_refData
= new wxVariantDataBool(value
);
681 bool wxVariant::GetBool() const
684 if (Convert(& value
))
688 wxFAIL_MSG(wxT("Could not convert to a bool"));
693 // -----------------------------------------------------------------
695 // -----------------------------------------------------------------
697 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
700 wxVariantDataChar() { m_value
= 0; }
701 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
703 inline wxUniChar
GetValue() const { return m_value
; }
704 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
706 virtual bool Eq(wxVariantData
& data
) const;
707 #if wxUSE_STD_IOSTREAM
708 virtual bool Read(wxSTD istream
& str
);
709 virtual bool Write(wxSTD ostream
& str
) const;
711 virtual bool Read(wxString
& str
);
712 virtual bool Write(wxString
& str
) const;
714 virtual bool Read(wxInputStream
& str
);
715 virtual bool Write(wxOutputStream
& str
) const;
716 #endif // wxUSE_STREAMS
717 virtual wxString
GetType() const { return wxT("char"); }
718 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
720 DECLARE_WXANY_CONVERSION()
725 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxUniChar
, wxVariantDataChar
)
727 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
729 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
731 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
733 return (otherData
.m_value
== m_value
);
736 #if wxUSE_STD_IOSTREAM
737 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
739 str
<< wxString(m_value
);
744 bool wxVariantDataChar::Write(wxString
& str
) const
750 #if wxUSE_STD_IOSTREAM
751 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
753 wxFAIL_MSG(wxT("Unimplemented"));
760 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
762 wxTextOutputStream
s(str
);
764 // FIXME-UTF8: this should be just "s << m_value;" after removal of
765 // ANSI build and addition of wxUniChar to wxTextOutputStream:
766 s
<< (wxChar
)m_value
;
771 bool wxVariantDataChar::Read(wxInputStream
& str
)
773 wxTextInputStream
s(str
);
775 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
776 // ANSI build and addition of wxUniChar to wxTextInputStream:
783 #endif // wxUSE_STREAMS
785 bool wxVariantDataChar::Read(wxString
& str
)
791 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
793 m_refData
= new wxVariantDataChar(val
);
797 wxVariant::wxVariant(char val
, const wxString
& name
)
799 m_refData
= new wxVariantDataChar(val
);
803 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
805 m_refData
= new wxVariantDataChar(val
);
809 bool wxVariant::operator==(const wxUniChar
& value
) const
812 if (!Convert(&thisValue
))
815 return (value
== thisValue
);
818 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
820 if (GetType() == wxT("char") &&
821 m_refData
->GetRefCount() == 1)
823 ((wxVariantDataChar
*)GetData())->SetValue(value
);
828 m_refData
= new wxVariantDataChar(value
);
834 wxUniChar
wxVariant::GetChar() const
837 if (Convert(& value
))
841 wxFAIL_MSG(wxT("Could not convert to a char"));
846 // ----------------------------------------------------------------------------
847 // wxVariantDataString
848 // ----------------------------------------------------------------------------
850 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
853 wxVariantDataString() { }
854 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
856 inline wxString
GetValue() const { return m_value
; }
857 inline void SetValue(const wxString
& value
) { m_value
= value
; }
859 virtual bool Eq(wxVariantData
& data
) const;
860 #if wxUSE_STD_IOSTREAM
861 virtual bool Write(wxSTD ostream
& str
) const;
863 virtual bool Read(wxString
& str
);
864 virtual bool Write(wxString
& str
) const;
865 #if wxUSE_STD_IOSTREAM
866 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
869 virtual bool Read(wxInputStream
& str
);
870 virtual bool Write(wxOutputStream
& str
) const;
871 #endif // wxUSE_STREAMS
872 virtual wxString
GetType() const { return wxT("string"); }
873 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
875 DECLARE_WXANY_CONVERSION()
880 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxString
, wxVariantDataString
)
882 bool wxVariantDataString::Eq(wxVariantData
& data
) const
884 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
886 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
888 return (otherData
.m_value
== m_value
);
891 #if wxUSE_STD_IOSTREAM
892 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
894 str
<< (const char*) m_value
.mb_str();
899 bool wxVariantDataString::Write(wxString
& str
) const
906 bool wxVariantDataString::Write(wxOutputStream
& str
) const
908 // why doesn't wxOutputStream::operator<< take "const wxString&"
909 wxTextOutputStream
s(str
);
910 s
.WriteString(m_value
);
914 bool wxVariantDataString::Read(wxInputStream
& str
)
916 wxTextInputStream
s(str
);
918 m_value
= s
.ReadLine();
921 #endif // wxUSE_STREAMS
923 bool wxVariantDataString::Read(wxString
& str
)
931 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
933 m_refData
= new wxVariantDataString(val
);
937 wxVariant::wxVariant(const char* val
, const wxString
& name
)
939 m_refData
= new wxVariantDataString(wxString(val
));
943 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
945 m_refData
= new wxVariantDataString(wxString(val
));
949 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
951 m_refData
= new wxVariantDataString(val
.AsString());
955 wxVariant::wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
)
957 m_refData
= new wxVariantDataString(wxString(val
));
961 wxVariant::wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
)
963 m_refData
= new wxVariantDataString(wxString(val
));
967 bool wxVariant::operator== (const wxString
& value
) const
970 if (!Convert(&thisValue
))
973 return value
== thisValue
;
976 bool wxVariant::operator!= (const wxString
& value
) const
978 return (!((*this) == value
));
981 wxVariant
& wxVariant::operator= (const wxString
& value
)
983 if (GetType() == wxT("string") &&
984 m_refData
->GetRefCount() == 1)
986 ((wxVariantDataString
*)GetData())->SetValue(value
);
991 m_refData
= new wxVariantDataString(value
);
996 wxString
wxVariant::GetString() const
999 if (!Convert(& value
))
1001 wxFAIL_MSG(wxT("Could not convert to a string"));
1007 // ----------------------------------------------------------------------------
1008 // wxVariantDataWxObjectPtr
1009 // ----------------------------------------------------------------------------
1011 class wxVariantDataWxObjectPtr
: public wxVariantData
1014 wxVariantDataWxObjectPtr() { }
1015 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
1017 inline wxObject
* GetValue() const { return m_value
; }
1018 inline void SetValue(wxObject
* value
) { m_value
= value
; }
1020 virtual bool Eq(wxVariantData
& data
) const;
1021 #if wxUSE_STD_IOSTREAM
1022 virtual bool Write(wxSTD ostream
& str
) const;
1024 virtual bool Write(wxString
& str
) const;
1025 #if wxUSE_STD_IOSTREAM
1026 virtual bool Read(wxSTD istream
& str
);
1028 virtual bool Read(wxString
& str
);
1029 virtual wxString
GetType() const ;
1030 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
1032 virtual wxClassInfo
* GetValueClassInfo();
1034 DECLARE_WXANY_CONVERSION()
1039 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxObject
*, wxVariantDataWxObjectPtr
)
1041 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
1043 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1045 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1047 return (otherData
.m_value
== m_value
);
1050 wxString
wxVariantDataWxObjectPtr::GetType() const
1052 wxString
returnVal(wxT("wxObject*"));
1056 returnVal
= m_value
->GetClassInfo()->GetClassName();
1057 returnVal
+= wxT("*");
1063 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1065 wxClassInfo
* returnVal
=NULL
;
1067 if (m_value
) returnVal
= m_value
->GetClassInfo();
1072 #if wxUSE_STD_IOSTREAM
1073 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1077 str
<< (const char*) s
.mb_str();
1082 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1084 str
.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value
));
1088 #if wxUSE_STD_IOSTREAM
1089 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1096 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1104 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1106 m_refData
= new wxVariantDataWxObjectPtr(val
);
1110 bool wxVariant::operator== (wxObject
* value
) const
1112 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1115 bool wxVariant::operator!= (wxObject
* value
) const
1117 return (!((*this) == (wxObject
*) value
));
1120 void wxVariant::operator= (wxObject
* value
)
1123 m_refData
= new wxVariantDataWxObjectPtr(value
);
1126 wxObject
* wxVariant::GetWxObjectPtr() const
1128 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_refData
)->GetValue();
1131 // ----------------------------------------------------------------------------
1132 // wxVariantDataVoidPtr
1133 // ----------------------------------------------------------------------------
1135 class wxVariantDataVoidPtr
: public wxVariantData
1138 wxVariantDataVoidPtr() { }
1139 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1141 inline void* GetValue() const { return m_value
; }
1142 inline void SetValue(void* value
) { m_value
= value
; }
1144 virtual bool Eq(wxVariantData
& data
) const;
1145 #if wxUSE_STD_IOSTREAM
1146 virtual bool Write(wxSTD ostream
& str
) const;
1148 virtual bool Write(wxString
& str
) const;
1149 #if wxUSE_STD_IOSTREAM
1150 virtual bool Read(wxSTD istream
& str
);
1152 virtual bool Read(wxString
& str
);
1153 virtual wxString
GetType() const { return wxT("void*"); }
1154 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1156 DECLARE_WXANY_CONVERSION()
1161 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(void*, wxVariantDataVoidPtr
)
1163 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1165 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1167 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1169 return (otherData
.m_value
== m_value
);
1172 #if wxUSE_STD_IOSTREAM
1173 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1177 str
<< (const char*) s
.mb_str();
1182 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1184 str
.Printf(wxT("%p"), m_value
);
1188 #if wxUSE_STD_IOSTREAM
1189 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1196 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1204 wxVariant::wxVariant( void* val
, const wxString
& name
)
1206 m_refData
= new wxVariantDataVoidPtr(val
);
1210 bool wxVariant::operator== (void* value
) const
1212 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1215 bool wxVariant::operator!= (void* value
) const
1217 return (!((*this) == (void*) value
));
1220 void wxVariant::operator= (void* value
)
1222 if (GetType() == wxT("void*") && (m_refData
->GetRefCount() == 1))
1224 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1229 m_refData
= new wxVariantDataVoidPtr(value
);
1233 void* wxVariant::GetVoidPtr() const
1235 // handling this specially is convenient when working with COM, see #9873
1239 wxASSERT( GetType() == wxT("void*") );
1241 return (void*) ((wxVariantDataVoidPtr
*) m_refData
)->GetValue();
1244 // ----------------------------------------------------------------------------
1245 // wxVariantDataDateTime
1246 // ----------------------------------------------------------------------------
1250 class wxVariantDataDateTime
: public wxVariantData
1253 wxVariantDataDateTime() { }
1254 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1256 inline wxDateTime
GetValue() const { return m_value
; }
1257 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1259 virtual bool Eq(wxVariantData
& data
) const;
1260 #if wxUSE_STD_IOSTREAM
1261 virtual bool Write(wxSTD ostream
& str
) const;
1263 virtual bool Write(wxString
& str
) const;
1264 #if wxUSE_STD_IOSTREAM
1265 virtual bool Read(wxSTD istream
& str
);
1267 virtual bool Read(wxString
& str
);
1268 virtual wxString
GetType() const { return wxT("datetime"); }
1269 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1271 DECLARE_WXANY_CONVERSION()
1276 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxDateTime
, wxVariantDataDateTime
)
1278 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1280 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1282 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1284 return (otherData
.m_value
== m_value
);
1288 #if wxUSE_STD_IOSTREAM
1289 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1293 str
<< value
.c_str();
1299 bool wxVariantDataDateTime::Write(wxString
& str
) const
1301 if ( m_value
.IsValid() )
1302 str
= m_value
.Format();
1304 str
= wxS("Invalid");
1309 #if wxUSE_STD_IOSTREAM
1310 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1318 bool wxVariantDataDateTime::Read(wxString
& str
)
1320 if ( str
== wxS("Invalid") )
1322 m_value
= wxInvalidDateTime
;
1326 wxString::const_iterator end
;
1327 return m_value
.ParseDateTime(str
, &end
) && end
== str
.end();
1332 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1334 m_refData
= new wxVariantDataDateTime(val
);
1338 bool wxVariant::operator== (const wxDateTime
& value
) const
1340 wxDateTime thisValue
;
1341 if (!Convert(&thisValue
))
1344 return value
.IsEqualTo(thisValue
);
1347 bool wxVariant::operator!= (const wxDateTime
& value
) const
1349 return (!((*this) == value
));
1352 void wxVariant::operator= (const wxDateTime
& value
)
1354 if (GetType() == wxT("datetime") &&
1355 m_refData
->GetRefCount() == 1)
1357 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1362 m_refData
= new wxVariantDataDateTime(value
);
1366 wxDateTime
wxVariant::GetDateTime() const
1369 if (!Convert(& value
))
1371 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1377 #endif // wxUSE_DATETIME
1379 // ----------------------------------------------------------------------------
1380 // wxVariantDataArrayString
1381 // ----------------------------------------------------------------------------
1383 class wxVariantDataArrayString
: public wxVariantData
1386 wxVariantDataArrayString() { }
1387 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1389 wxArrayString
GetValue() const { return m_value
; }
1390 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1392 virtual bool Eq(wxVariantData
& data
) const;
1393 #if wxUSE_STD_IOSTREAM
1394 virtual bool Write(wxSTD ostream
& str
) const;
1396 virtual bool Write(wxString
& str
) const;
1397 #if wxUSE_STD_IOSTREAM
1398 virtual bool Read(wxSTD istream
& str
);
1400 virtual bool Read(wxString
& str
);
1401 virtual wxString
GetType() const { return wxT("arrstring"); }
1402 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1404 DECLARE_WXANY_CONVERSION()
1406 wxArrayString m_value
;
1409 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxArrayString
, wxVariantDataArrayString
)
1411 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1413 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1415 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1417 return otherData
.m_value
== m_value
;
1420 #if wxUSE_STD_IOSTREAM
1421 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1428 bool wxVariantDataArrayString::Write(wxString
& str
) const
1430 size_t count
= m_value
.GetCount();
1431 for ( size_t n
= 0; n
< count
; n
++ )
1443 #if wxUSE_STD_IOSTREAM
1444 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1452 bool wxVariantDataArrayString::Read(wxString
& str
)
1454 wxStringTokenizer
tk(str
, wxT(";"));
1455 while ( tk
.HasMoreTokens() )
1457 m_value
.Add(tk
.GetNextToken());
1465 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1467 m_refData
= new wxVariantDataArrayString(val
);
1471 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1473 wxFAIL_MSG( wxT("TODO") );
1478 bool wxVariant::operator!=(const wxArrayString
& value
) const
1480 return !(*this == value
);
1483 void wxVariant::operator=(const wxArrayString
& value
)
1485 if (GetType() == wxT("arrstring") &&
1486 m_refData
->GetRefCount() == 1)
1488 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1493 m_refData
= new wxVariantDataArrayString(value
);
1497 wxArrayString
wxVariant::GetArrayString() const
1499 if ( GetType() == wxT("arrstring") )
1500 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1502 return wxArrayString();
1505 // ----------------------------------------------------------------------------
1506 // wxVariantDataLongLong
1507 // ----------------------------------------------------------------------------
1511 class WXDLLIMPEXP_BASE wxVariantDataLongLong
: public wxVariantData
1514 wxVariantDataLongLong() { m_value
= 0; }
1515 wxVariantDataLongLong(wxLongLong value
) { m_value
= value
; }
1517 wxLongLong
GetValue() const { return m_value
; }
1518 void SetValue(wxLongLong value
) { m_value
= value
; }
1520 virtual bool Eq(wxVariantData
& data
) const;
1522 virtual bool Read(wxString
& str
);
1523 virtual bool Write(wxString
& str
) const;
1524 #if wxUSE_STD_IOSTREAM
1525 virtual bool Read(wxSTD istream
& str
);
1526 virtual bool Write(wxSTD ostream
& str
) const;
1529 virtual bool Read(wxInputStream
& str
);
1530 virtual bool Write(wxOutputStream
&str
) const;
1531 #endif // wxUSE_STREAMS
1533 wxVariantData
* Clone() const
1535 return new wxVariantDataLongLong(m_value
);
1538 virtual wxString
GetType() const { return wxS("longlong"); }
1540 DECLARE_WXANY_CONVERSION()
1546 // wxLongLong type requires customized wxAny conversion code
1551 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1553 *any
= m_value
.GetValue();
1557 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1559 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong_t
));
1562 REGISTER_WXANY_CONVERSION(wxLongLong_t
, wxVariantDataLongLong
)
1564 #else // if !defined(wxLongLong_t)
1566 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1572 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1574 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong
));
1577 REGISTER_WXANY_CONVERSION(wxLongLong
, wxVariantDataLongLong
)
1579 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1582 bool wxVariantDataLongLong::Eq(wxVariantData
& data
) const
1584 wxASSERT_MSG( (data
.GetType() == wxS("longlong")),
1585 "wxVariantDataLongLong::Eq: argument mismatch" );
1587 wxVariantDataLongLong
& otherData
= (wxVariantDataLongLong
&) data
;
1589 return (otherData
.m_value
== m_value
);
1592 #if wxUSE_STD_IOSTREAM
1593 bool wxVariantDataLongLong::Write(wxSTD ostream
& str
) const
1597 str
<< (const char*) s
.mb_str();
1602 bool wxVariantDataLongLong::Write(wxString
& str
) const
1605 str
.Printf(wxS("%lld"), m_value
.GetValue());
1612 #if wxUSE_STD_IOSTREAM
1613 bool wxVariantDataLongLong::Read(wxSTD istream
& WXUNUSED(str
))
1615 wxFAIL_MSG(wxS("Unimplemented"));
1621 bool wxVariantDataLongLong::Write(wxOutputStream
& str
) const
1623 wxTextOutputStream
s(str
);
1624 s
.Write32(m_value
.GetLo());
1625 s
.Write32(m_value
.GetHi());
1629 bool wxVariantDataLongLong::Read(wxInputStream
& str
)
1631 wxTextInputStream
s(str
);
1632 unsigned long lo
= s
.Read32();
1633 long hi
= s
.Read32();
1634 m_value
= wxLongLong(hi
, lo
);
1637 #endif // wxUSE_STREAMS
1639 bool wxVariantDataLongLong::Read(wxString
& str
)
1642 wxLongLong_t value_t
;
1643 if ( !str
.ToLongLong(&value_t
) )
1654 wxVariant::wxVariant(wxLongLong val
, const wxString
& name
)
1656 m_refData
= new wxVariantDataLongLong(val
);
1660 bool wxVariant::operator==(wxLongLong value
) const
1662 wxLongLong thisValue
;
1663 if ( !Convert(&thisValue
) )
1666 return (value
== thisValue
);
1669 bool wxVariant::operator!=(wxLongLong value
) const
1671 return (!((*this) == value
));
1674 void wxVariant::operator=(wxLongLong value
)
1676 if ( GetType() == wxS("longlong") &&
1677 m_refData
->GetRefCount() == 1 )
1679 ((wxVariantDataLongLong
*)GetData())->SetValue(value
);
1684 m_refData
= new wxVariantDataLongLong(value
);
1688 wxLongLong
wxVariant::GetLongLong() const
1691 if ( Convert(&value
) )
1697 wxFAIL_MSG(wxT("Could not convert to a long long"));
1702 #endif // wxUSE_LONGLONG
1704 // ----------------------------------------------------------------------------
1705 // wxVariantDataULongLong
1706 // ----------------------------------------------------------------------------
1710 class WXDLLIMPEXP_BASE wxVariantDataULongLong
: public wxVariantData
1713 wxVariantDataULongLong() { m_value
= 0; }
1714 wxVariantDataULongLong(wxULongLong value
) { m_value
= value
; }
1716 wxULongLong
GetValue() const { return m_value
; }
1717 void SetValue(wxULongLong value
) { m_value
= value
; }
1719 virtual bool Eq(wxVariantData
& data
) const;
1721 virtual bool Read(wxString
& str
);
1722 virtual bool Write(wxString
& str
) const;
1723 #if wxUSE_STD_IOSTREAM
1724 virtual bool Read(wxSTD istream
& str
);
1725 virtual bool Write(wxSTD ostream
& str
) const;
1728 virtual bool Read(wxInputStream
& str
);
1729 virtual bool Write(wxOutputStream
&str
) const;
1730 #endif // wxUSE_STREAMS
1732 wxVariantData
* Clone() const
1734 return new wxVariantDataULongLong(m_value
);
1737 virtual wxString
GetType() const { return wxS("ulonglong"); }
1739 DECLARE_WXANY_CONVERSION()
1741 wxULongLong m_value
;
1745 // wxULongLong type requires customized wxAny conversion code
1750 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1752 *any
= m_value
.GetValue();
1756 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1758 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong_t
));
1761 REGISTER_WXANY_CONVERSION(wxULongLong_t
, wxVariantDataULongLong
)
1763 #else // if !defined(wxLongLong_t)
1765 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1771 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1773 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong
));
1776 REGISTER_WXANY_CONVERSION(wxULongLong
, wxVariantDataULongLong
)
1778 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1782 bool wxVariantDataULongLong::Eq(wxVariantData
& data
) const
1784 wxASSERT_MSG( (data
.GetType() == wxS("ulonglong")),
1785 "wxVariantDataULongLong::Eq: argument mismatch" );
1787 wxVariantDataULongLong
& otherData
= (wxVariantDataULongLong
&) data
;
1789 return (otherData
.m_value
== m_value
);
1792 #if wxUSE_STD_IOSTREAM
1793 bool wxVariantDataULongLong::Write(wxSTD ostream
& str
) const
1797 str
<< (const char*) s
.mb_str();
1802 bool wxVariantDataULongLong::Write(wxString
& str
) const
1805 str
.Printf(wxS("%llu"), m_value
.GetValue());
1812 #if wxUSE_STD_IOSTREAM
1813 bool wxVariantDataULongLong::Read(wxSTD istream
& WXUNUSED(str
))
1815 wxFAIL_MSG(wxS("Unimplemented"));
1821 bool wxVariantDataULongLong::Write(wxOutputStream
& str
) const
1823 wxTextOutputStream
s(str
);
1824 s
.Write32(m_value
.GetLo());
1825 s
.Write32(m_value
.GetHi());
1829 bool wxVariantDataULongLong::Read(wxInputStream
& str
)
1831 wxTextInputStream
s(str
);
1832 unsigned long lo
= s
.Read32();
1833 long hi
= s
.Read32();
1834 m_value
= wxULongLong(hi
, lo
);
1837 #endif // wxUSE_STREAMS
1839 bool wxVariantDataULongLong::Read(wxString
& str
)
1842 wxULongLong_t value_t
;
1843 if ( !str
.ToULongLong(&value_t
) )
1854 wxVariant::wxVariant(wxULongLong val
, const wxString
& name
)
1856 m_refData
= new wxVariantDataULongLong(val
);
1860 bool wxVariant::operator==(wxULongLong value
) const
1862 wxULongLong thisValue
;
1863 if ( !Convert(&thisValue
) )
1866 return (value
== thisValue
);
1869 bool wxVariant::operator!=(wxULongLong value
) const
1871 return (!((*this) == value
));
1874 void wxVariant::operator=(wxULongLong value
)
1876 if ( GetType() == wxS("ulonglong") &&
1877 m_refData
->GetRefCount() == 1 )
1879 ((wxVariantDataULongLong
*)GetData())->SetValue(value
);
1884 m_refData
= new wxVariantDataULongLong(value
);
1888 wxULongLong
wxVariant::GetULongLong() const
1891 if ( Convert(&value
) )
1897 wxFAIL_MSG(wxT("Could not convert to a long long"));
1902 #endif // wxUSE_LONGLONG
1904 // ----------------------------------------------------------------------------
1905 // wxVariantDataList
1906 // ----------------------------------------------------------------------------
1908 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1911 wxVariantDataList() {}
1912 wxVariantDataList(const wxVariantList
& list
);
1913 virtual ~wxVariantDataList();
1915 wxVariantList
& GetValue() { return m_value
; }
1916 void SetValue(const wxVariantList
& value
) ;
1918 virtual bool Eq(wxVariantData
& data
) const;
1919 #if wxUSE_STD_IOSTREAM
1920 virtual bool Write(wxSTD ostream
& str
) const;
1922 virtual bool Write(wxString
& str
) const;
1923 #if wxUSE_STD_IOSTREAM
1924 virtual bool Read(wxSTD istream
& str
);
1926 virtual bool Read(wxString
& str
);
1927 virtual wxString
GetType() const { return wxT("list"); }
1931 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1933 DECLARE_WXANY_CONVERSION()
1935 wxVariantList m_value
;
1941 // Convert to/from list of wxAnys
1944 WX_DEFINE_LIST(wxAnyList
)
1946 bool wxVariantDataList::GetAsAny(wxAny
* any
) const
1949 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1952 wxVariant
* pVar
= node
->GetData();
1953 dst
.push_back(new wxAny(((const wxVariant
&)*pVar
)));
1954 node
= node
->GetNext();
1961 wxVariantData
* wxVariantDataList::VariantDataFactory(const wxAny
& any
)
1963 wxAnyList src
= wxANY_AS(any
, wxAnyList
);
1965 wxAnyList::compatibility_iterator node
= src
.GetFirst();
1968 wxAny
* pAny
= node
->GetData();
1969 dst
.push_back(new wxVariant(*pAny
));
1970 node
= node
->GetNext();
1973 return new wxVariantDataList(dst
);
1976 REGISTER_WXANY_CONVERSION(wxAnyList
, wxVariantDataList
)
1980 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
1985 wxVariantDataList::~wxVariantDataList()
1990 void wxVariantDataList::SetValue(const wxVariantList
& value
)
1993 wxVariantList::compatibility_iterator node
= value
.GetFirst();
1996 wxVariant
* var
= node
->GetData();
1997 m_value
.Append(new wxVariant(*var
));
1998 node
= node
->GetNext();
2002 void wxVariantDataList::Clear()
2004 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2007 wxVariant
* var
= node
->GetData();
2009 node
= node
->GetNext();
2014 bool wxVariantDataList::Eq(wxVariantData
& data
) const
2016 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
2018 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
2019 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
2020 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
2021 while (node1
&& node2
)
2023 wxVariant
* var1
= node1
->GetData();
2024 wxVariant
* var2
= node2
->GetData();
2025 if ((*var1
) != (*var2
))
2027 node1
= node1
->GetNext();
2028 node2
= node2
->GetNext();
2030 if (node1
|| node2
) return false;
2034 #if wxUSE_STD_IOSTREAM
2035 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
2039 str
<< (const char*) s
.mb_str();
2044 bool wxVariantDataList::Write(wxString
& str
) const
2046 str
= wxEmptyString
;
2047 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2050 wxVariant
* var
= node
->GetData();
2051 if (node
!= m_value
.GetFirst())
2054 str
+= var
->MakeString();
2055 node
= node
->GetNext();
2061 #if wxUSE_STD_IOSTREAM
2062 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
2064 wxFAIL_MSG(wxT("Unimplemented"));
2070 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
2072 wxFAIL_MSG(wxT("Unimplemented"));
2079 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
2081 m_refData
= new wxVariantDataList(val
);
2085 bool wxVariant::operator== (const wxVariantList
& value
) const
2087 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
2089 wxVariantDataList
other(value
);
2090 return (GetData()->Eq(other
));
2093 bool wxVariant::operator!= (const wxVariantList
& value
) const
2095 return (!((*this) == value
));
2098 void wxVariant::operator= (const wxVariantList
& value
)
2100 if (GetType() == wxT("list") &&
2101 m_refData
->GetRefCount() == 1)
2103 ((wxVariantDataList
*)GetData())->SetValue(value
);
2108 m_refData
= new wxVariantDataList(value
);
2112 wxVariantList
& wxVariant::GetList() const
2114 wxASSERT( (GetType() == wxT("list")) );
2116 return (wxVariantList
&) ((wxVariantDataList
*) m_refData
)->GetValue();
2120 void wxVariant::NullList()
2122 SetData(new wxVariantDataList());
2126 void wxVariant::Append(const wxVariant
& value
)
2128 wxVariantList
& list
= GetList();
2130 list
.Append(new wxVariant(value
));
2133 // Insert at front of list
2134 void wxVariant::Insert(const wxVariant
& value
)
2136 wxVariantList
& list
= GetList();
2138 list
.Insert(new wxVariant(value
));
2141 // Returns true if the variant is a member of the list
2142 bool wxVariant::Member(const wxVariant
& value
) const
2144 wxVariantList
& list
= GetList();
2146 wxVariantList::compatibility_iterator node
= list
.GetFirst();
2149 wxVariant
* other
= node
->GetData();
2150 if (value
== *other
)
2152 node
= node
->GetNext();
2157 // Deletes the nth element of the list
2158 bool wxVariant::Delete(size_t item
)
2160 wxVariantList
& list
= GetList();
2162 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
2163 wxVariantList::compatibility_iterator node
= list
.Item(item
);
2164 wxVariant
* variant
= node
->GetData();
2171 void wxVariant::ClearList()
2173 if (!IsNull() && (GetType() == wxT("list")))
2175 ((wxVariantDataList
*) m_refData
)->Clear();
2179 if (!GetType().IsSameAs(wxT("list")))
2182 m_refData
= new wxVariantDataList
;
2186 // Treat a list variant as an array
2187 wxVariant
wxVariant::operator[] (size_t idx
) const
2189 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
2191 if (GetType() == wxT("list"))
2193 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2194 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2195 return *(data
->GetValue().Item(idx
)->GetData());
2197 return wxNullVariant
;
2200 wxVariant
& wxVariant::operator[] (size_t idx
)
2202 // We can't return a reference to a variant for a string list, since the string
2203 // is actually stored as a char*, not a variant.
2205 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
2207 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2208 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2210 return * (data
->GetValue().Item(idx
)->GetData());
2213 // Return the number of elements in a list
2214 size_t wxVariant::GetCount() const
2216 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
2218 if (GetType() == wxT("list"))
2220 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2221 return data
->GetValue().GetCount();
2226 // ----------------------------------------------------------------------------
2228 // ----------------------------------------------------------------------------
2230 bool wxVariant::Convert(long* value
) const
2232 wxString
type(GetType());
2233 if (type
== wxS("double"))
2234 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
2235 else if (type
== wxS("long"))
2236 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2237 else if (type
== wxS("bool"))
2238 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2239 else if (type
== wxS("string"))
2240 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
2242 else if (type
== wxS("longlong"))
2244 wxLongLong v
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2245 // Don't convert if return value would be vague
2246 if ( v
< LONG_MIN
|| v
> LONG_MAX
)
2248 *value
= v
.ToLong();
2250 else if (type
== wxS("ulonglong"))
2252 wxULongLong v
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2253 // Don't convert if return value would be vague
2256 *value
= (long) v
.ToULong();
2265 bool wxVariant::Convert(bool* value
) const
2267 wxString
type(GetType());
2268 if (type
== wxT("double"))
2269 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
2270 else if (type
== wxT("long"))
2271 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2272 else if (type
== wxT("bool"))
2273 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2274 else if (type
== wxT("string"))
2276 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2278 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
2280 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
2291 bool wxVariant::Convert(double* value
) const
2293 wxString
type(GetType());
2294 if (type
== wxT("double"))
2295 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
2296 else if (type
== wxT("long"))
2297 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2298 else if (type
== wxT("bool"))
2299 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2300 else if (type
== wxT("string"))
2301 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
2303 else if (type
== wxS("longlong"))
2305 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue().ToDouble();
2307 else if (type
== wxS("ulonglong"))
2309 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue().ToDouble();
2318 bool wxVariant::Convert(wxUniChar
* value
) const
2320 wxString
type(GetType());
2321 if (type
== wxT("char"))
2322 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2323 else if (type
== wxT("long"))
2324 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2325 else if (type
== wxT("bool"))
2326 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2333 bool wxVariant::Convert(char* value
) const
2336 if ( !Convert(&ch
) )
2342 bool wxVariant::Convert(wchar_t* value
) const
2345 if ( !Convert(&ch
) )
2351 bool wxVariant::Convert(wxString
* value
) const
2353 *value
= MakeString();
2358 bool wxVariant::Convert(wxLongLong
* value
) const
2360 wxString
type(GetType());
2361 if (type
== wxS("longlong"))
2362 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2363 else if (type
== wxS("long"))
2364 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2365 else if (type
== wxS("string"))
2367 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2369 wxLongLong_t value_t
;
2370 if ( !s
.ToLongLong(&value_t
) )
2375 if ( !s
.ToLong(&l_value
) )
2380 else if (type
== wxS("bool"))
2381 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2382 else if (type
== wxS("double"))
2384 value
->Assign(((wxVariantDoubleData
*)GetData())->GetValue());
2386 else if (type
== wxS("ulonglong"))
2387 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2394 bool wxVariant::Convert(wxULongLong
* value
) const
2396 wxString
type(GetType());
2397 if (type
== wxS("ulonglong"))
2398 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2399 else if (type
== wxS("long"))
2400 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2401 else if (type
== wxS("string"))
2403 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2405 wxULongLong_t value_t
;
2406 if ( !s
.ToULongLong(&value_t
) )
2410 unsigned long l_value
;
2411 if ( !s
.ToULong(&l_value
) )
2416 else if (type
== wxS("bool"))
2417 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2418 else if (type
== wxS("double"))
2420 double value_d
= ((wxVariantDoubleData
*)GetData())->GetValue();
2422 if ( value_d
< 0.0 )
2426 *value
= (wxULongLong_t
) value_d
;
2429 temp
.Assign(value_d
);
2433 else if (type
== wxS("longlong"))
2434 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2440 #endif // wxUSE_LONGLONG
2443 bool wxVariant::Convert(wxDateTime
* value
) const
2445 wxString
type(GetType());
2446 if (type
== wxT("datetime"))
2448 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2452 // Fallback to string conversion
2454 if ( !Convert(&val
) )
2457 // Try to parse this as either date and time, only date or only time
2458 // checking that the entire string was parsed
2459 wxString::const_iterator end
;
2460 if ( value
->ParseDateTime(val
, &end
) && end
== val
.end() )
2463 if ( value
->ParseDate(val
, &end
) && end
== val
.end() )
2466 if ( value
->ParseTime(val
, &end
) && end
== val
.end() )
2471 #endif // wxUSE_DATETIME
2473 #endif // wxUSE_VARIANT