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
41 #include "wx/txtstrm.h"
44 #include "wx/string.h"
45 #include "wx/tokenzr.h"
47 wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
50 #include "wx/listimpl.cpp"
51 WX_DEFINE_LIST(wxVariantList
)
57 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
59 wxVariant::wxVariant()
64 bool wxVariant::IsNull() const
66 return (m_refData
== NULL
);
69 void wxVariant::MakeNull()
74 void wxVariant::Clear()
76 m_name
= wxEmptyString
;
79 wxVariant::wxVariant(const wxVariant
& variant
)
82 if (!variant
.IsNull())
85 m_name
= variant
.m_name
;
88 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
95 wxVariant::~wxVariant()
99 wxObjectRefData
*wxVariant::CreateRefData() const
101 // We cannot create any particular wxVariantData.
102 wxFAIL_MSG("wxVariant::CreateRefData() cannot be implemented");
106 wxObjectRefData
*wxVariant::CloneRefData(const wxObjectRefData
*data
) const
108 return ((wxVariantData
*) data
)->Clone();
112 void wxVariant::operator= (const wxVariant
& variant
)
115 m_name
= variant
.m_name
;
118 // myVariant = new wxStringVariantData("hello")
119 void wxVariant::operator= (wxVariantData
* variantData
)
122 m_refData
= variantData
;
125 bool wxVariant::operator== (const wxVariant
& variant
) const
127 if (IsNull() || variant
.IsNull())
128 return (IsNull() == variant
.IsNull());
130 if (GetType() != variant
.GetType())
133 return (GetData()->Eq(* variant
.GetData()));
136 bool wxVariant::operator!= (const wxVariant
& variant
) const
138 return (!(*this == variant
));
141 wxString
wxVariant::MakeString() const
146 if (GetData()->Write(str
))
149 return wxEmptyString
;
152 void wxVariant::SetData(wxVariantData
* data
)
158 bool wxVariant::Unshare()
160 if ( !m_refData
|| m_refData
->GetRefCount() == 1 )
165 return (m_refData
&& m_refData
->GetRefCount() == 1);
169 // Returns a string representing the type of the variant,
170 // e.g. "string", "bool", "list", "double", "long"
171 wxString
wxVariant::GetType() const
174 return wxString(wxT("null"));
176 return GetData()->GetType();
180 bool wxVariant::IsType(const wxString
& type
) const
182 return (GetType() == type
);
185 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
187 wxClassInfo
* info
=GetData()->GetValueClassInfo();
188 return info
? info
->IsKindOf(type
) : false ;
191 // -----------------------------------------------------------------
192 // wxVariant <-> wxAny conversion code
193 // -----------------------------------------------------------------
197 wxAnyToVariantRegistration::
198 wxAnyToVariantRegistration(wxVariantDataFactory factory
)
201 wxPreRegisterAnyToVariant(this);
204 wxAnyToVariantRegistration::~wxAnyToVariantRegistration()
208 wxVariant::wxVariant(const wxAny
& any
)
212 if ( !any
.GetAs(&variant
) )
214 wxFAIL_MSG("wxAny of this type cannot be converted to wxVariant");
221 wxAny
wxVariant::GetAny() const
227 wxVariantData
* data
= GetData();
229 if ( data
->GetAsAny(&any
) )
232 // If everything else fails, wrap the whole wxVariantData
238 // -----------------------------------------------------------------
240 // -----------------------------------------------------------------
242 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
245 wxVariantDataLong() { m_value
= 0; }
246 wxVariantDataLong(long value
) { m_value
= value
; }
248 inline long GetValue() const { return m_value
; }
249 inline void SetValue(long value
) { m_value
= value
; }
251 virtual bool Eq(wxVariantData
& data
) const;
253 virtual bool Read(wxString
& str
);
254 virtual bool Write(wxString
& str
) const;
255 #if wxUSE_STD_IOSTREAM
256 virtual bool Read(wxSTD istream
& str
);
257 virtual bool Write(wxSTD ostream
& str
) const;
260 virtual bool Read(wxInputStream
& str
);
261 virtual bool Write(wxOutputStream
&str
) const;
262 #endif // wxUSE_STREAMS
264 wxVariantData
* Clone() const { return new wxVariantDataLong(m_value
); }
266 virtual wxString
GetType() const { return wxT("long"); }
269 // Since wxAny does not have separate type for integers shorter than
270 // longlong, we do not usually implement wxVariant->wxAny conversion
271 // here (but in wxVariantDataLongLong instead).
273 DECLARE_WXANY_CONVERSION()
275 bool GetAsAny(wxAny
* any
) const
288 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(long, wxVariantDataLong
)
291 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
293 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
295 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
297 return (otherData
.m_value
== m_value
);
300 #if wxUSE_STD_IOSTREAM
301 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
305 str
<< (const char*) s
.mb_str();
310 bool wxVariantDataLong::Write(wxString
& str
) const
312 str
.Printf(wxT("%ld"), m_value
);
316 #if wxUSE_STD_IOSTREAM
317 bool wxVariantDataLong::Read(wxSTD istream
& str
)
325 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
327 wxTextOutputStream
s(str
);
329 s
.Write32((size_t)m_value
);
333 bool wxVariantDataLong::Read(wxInputStream
& str
)
335 wxTextInputStream
s(str
);
336 m_value
= s
.Read32();
339 #endif // wxUSE_STREAMS
341 bool wxVariantDataLong::Read(wxString
& str
)
343 m_value
= wxAtol(str
);
349 wxVariant::wxVariant(long val
, const wxString
& name
)
351 m_refData
= new wxVariantDataLong(val
);
355 wxVariant::wxVariant(int val
, const wxString
& name
)
357 m_refData
= new wxVariantDataLong((long)val
);
361 wxVariant::wxVariant(short val
, const wxString
& name
)
363 m_refData
= new wxVariantDataLong((long)val
);
367 bool wxVariant::operator== (long value
) const
370 if (!Convert(&thisValue
))
373 return (value
== thisValue
);
376 bool wxVariant::operator!= (long value
) const
378 return (!((*this) == value
));
381 void wxVariant::operator= (long value
)
383 if (GetType() == wxT("long") &&
384 m_refData
->GetRefCount() == 1)
386 ((wxVariantDataLong
*)GetData())->SetValue(value
);
391 m_refData
= new wxVariantDataLong(value
);
395 long wxVariant::GetLong() const
398 if (Convert(& value
))
402 wxFAIL_MSG(wxT("Could not convert to a long"));
407 // -----------------------------------------------------------------
408 // wxVariantDoubleData
409 // -----------------------------------------------------------------
411 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
414 wxVariantDoubleData() { m_value
= 0.0; }
415 wxVariantDoubleData(double value
) { m_value
= value
; }
417 inline double GetValue() const { return m_value
; }
418 inline void SetValue(double value
) { m_value
= value
; }
420 virtual bool Eq(wxVariantData
& data
) const;
421 virtual bool Read(wxString
& str
);
422 #if wxUSE_STD_IOSTREAM
423 virtual bool Write(wxSTD ostream
& str
) const;
425 virtual bool Write(wxString
& str
) const;
426 #if wxUSE_STD_IOSTREAM
427 virtual bool Read(wxSTD istream
& str
);
430 virtual bool Read(wxInputStream
& str
);
431 virtual bool Write(wxOutputStream
&str
) const;
432 #endif // wxUSE_STREAMS
433 virtual wxString
GetType() const { return wxT("double"); }
435 wxVariantData
* Clone() const { return new wxVariantDoubleData(m_value
); }
437 DECLARE_WXANY_CONVERSION()
442 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(double, wxVariantDoubleData
)
444 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
446 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
448 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
450 return wxIsSameDouble(otherData
.m_value
, m_value
);
453 #if wxUSE_STD_IOSTREAM
454 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
458 str
<< (const char*) s
.mb_str();
463 bool wxVariantDoubleData::Write(wxString
& str
) const
465 str
.Printf(wxT("%.14g"), m_value
);
469 #if wxUSE_STD_IOSTREAM
470 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
478 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
480 wxTextOutputStream
s(str
);
481 s
.WriteDouble((double)m_value
);
485 bool wxVariantDoubleData::Read(wxInputStream
& str
)
487 wxTextInputStream
s(str
);
488 m_value
= (float)s
.ReadDouble();
491 #endif // wxUSE_STREAMS
493 bool wxVariantDoubleData::Read(wxString
& str
)
495 m_value
= wxAtof(str
);
499 // wxVariant double code
501 wxVariant::wxVariant(double val
, const wxString
& name
)
503 m_refData
= new wxVariantDoubleData(val
);
507 bool wxVariant::operator== (double value
) const
510 if (!Convert(&thisValue
))
513 return wxIsSameDouble(value
, thisValue
);
516 bool wxVariant::operator!= (double value
) const
518 return (!((*this) == value
));
521 void wxVariant::operator= (double value
)
523 if (GetType() == wxT("double") &&
524 m_refData
->GetRefCount() == 1)
526 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
531 m_refData
= new wxVariantDoubleData(value
);
535 double wxVariant::GetDouble() const
538 if (Convert(& value
))
542 wxFAIL_MSG(wxT("Could not convert to a double number"));
547 // -----------------------------------------------------------------
549 // -----------------------------------------------------------------
551 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
554 wxVariantDataBool() { m_value
= 0; }
555 wxVariantDataBool(bool value
) { m_value
= value
; }
557 inline bool GetValue() const { return m_value
; }
558 inline void SetValue(bool value
) { m_value
= value
; }
560 virtual bool Eq(wxVariantData
& data
) const;
561 #if wxUSE_STD_IOSTREAM
562 virtual bool Write(wxSTD ostream
& str
) const;
564 virtual bool Write(wxString
& str
) const;
565 virtual bool Read(wxString
& str
);
566 #if wxUSE_STD_IOSTREAM
567 virtual bool Read(wxSTD istream
& str
);
570 virtual bool Read(wxInputStream
& str
);
571 virtual bool Write(wxOutputStream
& str
) const;
572 #endif // wxUSE_STREAMS
573 virtual wxString
GetType() const { return wxT("bool"); }
575 wxVariantData
* Clone() const { return new wxVariantDataBool(m_value
); }
577 DECLARE_WXANY_CONVERSION()
582 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(bool, wxVariantDataBool
)
584 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
586 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
588 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
590 return (otherData
.m_value
== m_value
);
593 #if wxUSE_STD_IOSTREAM
594 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
598 str
<< (const char*) s
.mb_str();
603 bool wxVariantDataBool::Write(wxString
& str
) const
605 str
.Printf(wxT("%d"), (int) m_value
);
609 #if wxUSE_STD_IOSTREAM
610 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
612 wxFAIL_MSG(wxT("Unimplemented"));
613 // str >> (long) m_value;
619 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
621 wxTextOutputStream
s(str
);
627 bool wxVariantDataBool::Read(wxInputStream
& str
)
629 wxTextInputStream
s(str
);
631 m_value
= s
.Read8() != 0;
634 #endif // wxUSE_STREAMS
636 bool wxVariantDataBool::Read(wxString
& str
)
638 m_value
= (wxAtol(str
) != 0);
644 wxVariant::wxVariant(bool val
, const wxString
& name
)
646 m_refData
= new wxVariantDataBool(val
);
650 bool wxVariant::operator== (bool value
) const
653 if (!Convert(&thisValue
))
656 return (value
== thisValue
);
659 bool wxVariant::operator!= (bool value
) const
661 return (!((*this) == value
));
664 void wxVariant::operator= (bool value
)
666 if (GetType() == wxT("bool") &&
667 m_refData
->GetRefCount() == 1)
669 ((wxVariantDataBool
*)GetData())->SetValue(value
);
674 m_refData
= new wxVariantDataBool(value
);
678 bool wxVariant::GetBool() const
681 if (Convert(& value
))
685 wxFAIL_MSG(wxT("Could not convert to a bool"));
690 // -----------------------------------------------------------------
692 // -----------------------------------------------------------------
694 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
697 wxVariantDataChar() { m_value
= 0; }
698 wxVariantDataChar(const wxUniChar
& value
) { m_value
= value
; }
700 inline wxUniChar
GetValue() const { return m_value
; }
701 inline void SetValue(const wxUniChar
& value
) { m_value
= value
; }
703 virtual bool Eq(wxVariantData
& data
) const;
704 #if wxUSE_STD_IOSTREAM
705 virtual bool Read(wxSTD istream
& str
);
706 virtual bool Write(wxSTD ostream
& str
) const;
708 virtual bool Read(wxString
& str
);
709 virtual bool Write(wxString
& str
) const;
711 virtual bool Read(wxInputStream
& str
);
712 virtual bool Write(wxOutputStream
& str
) const;
713 #endif // wxUSE_STREAMS
714 virtual wxString
GetType() const { return wxT("char"); }
715 wxVariantData
* Clone() const { return new wxVariantDataChar(m_value
); }
717 DECLARE_WXANY_CONVERSION()
722 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxUniChar
, wxVariantDataChar
)
724 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
726 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
728 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
730 return (otherData
.m_value
== m_value
);
733 #if wxUSE_STD_IOSTREAM
734 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
736 str
<< wxString(m_value
);
741 bool wxVariantDataChar::Write(wxString
& str
) const
747 #if wxUSE_STD_IOSTREAM
748 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
750 wxFAIL_MSG(wxT("Unimplemented"));
757 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
759 wxTextOutputStream
s(str
);
761 // FIXME-UTF8: this should be just "s << m_value;" after removal of
762 // ANSI build and addition of wxUniChar to wxTextOutputStream:
763 s
<< (wxChar
)m_value
;
768 bool wxVariantDataChar::Read(wxInputStream
& str
)
770 wxTextInputStream
s(str
);
772 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
773 // ANSI build and addition of wxUniChar to wxTextInputStream:
780 #endif // wxUSE_STREAMS
782 bool wxVariantDataChar::Read(wxString
& str
)
788 wxVariant::wxVariant(const wxUniChar
& val
, const wxString
& name
)
790 m_refData
= new wxVariantDataChar(val
);
794 wxVariant::wxVariant(char val
, const wxString
& name
)
796 m_refData
= new wxVariantDataChar(val
);
800 wxVariant::wxVariant(wchar_t val
, const wxString
& name
)
802 m_refData
= new wxVariantDataChar(val
);
806 bool wxVariant::operator==(const wxUniChar
& value
) const
809 if (!Convert(&thisValue
))
812 return (value
== thisValue
);
815 wxVariant
& wxVariant::operator=(const wxUniChar
& value
)
817 if (GetType() == wxT("char") &&
818 m_refData
->GetRefCount() == 1)
820 ((wxVariantDataChar
*)GetData())->SetValue(value
);
825 m_refData
= new wxVariantDataChar(value
);
831 wxUniChar
wxVariant::GetChar() const
834 if (Convert(& value
))
838 wxFAIL_MSG(wxT("Could not convert to a char"));
843 // ----------------------------------------------------------------------------
844 // wxVariantDataString
845 // ----------------------------------------------------------------------------
847 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
850 wxVariantDataString() { }
851 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
853 inline wxString
GetValue() const { return m_value
; }
854 inline void SetValue(const wxString
& value
) { m_value
= value
; }
856 virtual bool Eq(wxVariantData
& data
) const;
857 #if wxUSE_STD_IOSTREAM
858 virtual bool Write(wxSTD ostream
& str
) const;
860 virtual bool Read(wxString
& str
);
861 virtual bool Write(wxString
& str
) const;
862 #if wxUSE_STD_IOSTREAM
863 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
866 virtual bool Read(wxInputStream
& str
);
867 virtual bool Write(wxOutputStream
& str
) const;
868 #endif // wxUSE_STREAMS
869 virtual wxString
GetType() const { return wxT("string"); }
870 wxVariantData
* Clone() const { return new wxVariantDataString(m_value
); }
872 DECLARE_WXANY_CONVERSION()
877 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxString
, wxVariantDataString
)
880 // This allows converting string literal wxAnys to string variants
881 wxVariantData
* wxVariantDataFromConstCharPAny(const wxAny
& any
)
883 return new wxVariantDataString(wxANY_AS(any
, const char*));
886 wxVariantData
* wxVariantDataFromConstWchar_tPAny(const wxAny
& any
)
888 return new wxVariantDataString(wxANY_AS(any
, const wchar_t*));
891 _REGISTER_WXANY_CONVERSION(const char*,
893 wxVariantDataFromConstCharPAny
)
894 _REGISTER_WXANY_CONVERSION(const wchar_t*,
896 wxVariantDataFromConstWchar_tPAny
)
899 bool wxVariantDataString::Eq(wxVariantData
& data
) const
901 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
903 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
905 return (otherData
.m_value
== m_value
);
908 #if wxUSE_STD_IOSTREAM
909 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
911 str
<< (const char*) m_value
.mb_str();
916 bool wxVariantDataString::Write(wxString
& str
) const
923 bool wxVariantDataString::Write(wxOutputStream
& str
) const
925 // why doesn't wxOutputStream::operator<< take "const wxString&"
926 wxTextOutputStream
s(str
);
927 s
.WriteString(m_value
);
931 bool wxVariantDataString::Read(wxInputStream
& str
)
933 wxTextInputStream
s(str
);
935 m_value
= s
.ReadLine();
938 #endif // wxUSE_STREAMS
940 bool wxVariantDataString::Read(wxString
& str
)
948 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
950 m_refData
= new wxVariantDataString(val
);
954 wxVariant::wxVariant(const char* val
, const wxString
& name
)
956 m_refData
= new wxVariantDataString(wxString(val
));
960 wxVariant::wxVariant(const wchar_t* val
, const wxString
& name
)
962 m_refData
= new wxVariantDataString(wxString(val
));
966 wxVariant::wxVariant(const wxCStrData
& val
, const wxString
& name
)
968 m_refData
= new wxVariantDataString(val
.AsString());
972 wxVariant::wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
)
974 m_refData
= new wxVariantDataString(wxString(val
));
978 wxVariant::wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
)
980 m_refData
= new wxVariantDataString(wxString(val
));
985 wxVariant::wxVariant(const std::string
& val
, const wxString
& name
)
987 m_refData
= new wxVariantDataString(wxString(val
));
991 wxVariant::wxVariant(const wxStdWideString
& val
, const wxString
& name
)
993 m_refData
= new wxVariantDataString(wxString(val
));
996 #endif // wxUSE_STD_STRING
998 bool wxVariant::operator== (const wxString
& value
) const
1001 if (!Convert(&thisValue
))
1004 return value
== thisValue
;
1007 bool wxVariant::operator!= (const wxString
& value
) const
1009 return (!((*this) == value
));
1012 wxVariant
& wxVariant::operator= (const wxString
& value
)
1014 if (GetType() == wxT("string") &&
1015 m_refData
->GetRefCount() == 1)
1017 ((wxVariantDataString
*)GetData())->SetValue(value
);
1022 m_refData
= new wxVariantDataString(value
);
1027 wxString
wxVariant::GetString() const
1030 if (!Convert(& value
))
1032 wxFAIL_MSG(wxT("Could not convert to a string"));
1038 // ----------------------------------------------------------------------------
1039 // wxVariantDataWxObjectPtr
1040 // ----------------------------------------------------------------------------
1042 class wxVariantDataWxObjectPtr
: public wxVariantData
1045 wxVariantDataWxObjectPtr() { }
1046 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
1048 inline wxObject
* GetValue() const { return m_value
; }
1049 inline void SetValue(wxObject
* value
) { m_value
= value
; }
1051 virtual bool Eq(wxVariantData
& data
) const;
1052 #if wxUSE_STD_IOSTREAM
1053 virtual bool Write(wxSTD ostream
& str
) const;
1055 virtual bool Write(wxString
& str
) const;
1056 #if wxUSE_STD_IOSTREAM
1057 virtual bool Read(wxSTD istream
& str
);
1059 virtual bool Read(wxString
& str
);
1060 virtual wxString
GetType() const ;
1061 virtual wxVariantData
* Clone() const { return new wxVariantDataWxObjectPtr(m_value
); }
1063 virtual wxClassInfo
* GetValueClassInfo();
1065 DECLARE_WXANY_CONVERSION()
1070 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxObject
*, wxVariantDataWxObjectPtr
)
1072 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
1074 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1076 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
1078 return (otherData
.m_value
== m_value
);
1081 wxString
wxVariantDataWxObjectPtr::GetType() const
1083 wxString
returnVal(wxT("wxObject*"));
1087 returnVal
= m_value
->GetClassInfo()->GetClassName();
1088 returnVal
+= wxT("*");
1094 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
1096 wxClassInfo
* returnVal
=NULL
;
1098 if (m_value
) returnVal
= m_value
->GetClassInfo();
1103 #if wxUSE_STD_IOSTREAM
1104 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
1108 str
<< (const char*) s
.mb_str();
1113 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
1115 str
.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value
));
1119 #if wxUSE_STD_IOSTREAM
1120 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1127 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1135 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1137 m_refData
= new wxVariantDataWxObjectPtr(val
);
1141 bool wxVariant::operator== (wxObject
* value
) const
1143 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1146 bool wxVariant::operator!= (wxObject
* value
) const
1148 return (!((*this) == (wxObject
*) value
));
1151 void wxVariant::operator= (wxObject
* value
)
1154 m_refData
= new wxVariantDataWxObjectPtr(value
);
1157 wxObject
* wxVariant::GetWxObjectPtr() const
1159 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_refData
)->GetValue();
1162 // ----------------------------------------------------------------------------
1163 // wxVariantDataVoidPtr
1164 // ----------------------------------------------------------------------------
1166 class wxVariantDataVoidPtr
: public wxVariantData
1169 wxVariantDataVoidPtr() { }
1170 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1172 inline void* GetValue() const { return m_value
; }
1173 inline void SetValue(void* value
) { m_value
= value
; }
1175 virtual bool Eq(wxVariantData
& data
) const;
1176 #if wxUSE_STD_IOSTREAM
1177 virtual bool Write(wxSTD ostream
& str
) const;
1179 virtual bool Write(wxString
& str
) const;
1180 #if wxUSE_STD_IOSTREAM
1181 virtual bool Read(wxSTD istream
& str
);
1183 virtual bool Read(wxString
& str
);
1184 virtual wxString
GetType() const { return wxT("void*"); }
1185 virtual wxVariantData
* Clone() const { return new wxVariantDataVoidPtr(m_value
); }
1187 DECLARE_WXANY_CONVERSION()
1192 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(void*, wxVariantDataVoidPtr
)
1194 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1196 wxASSERT_MSG( data
.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1198 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1200 return (otherData
.m_value
== m_value
);
1203 #if wxUSE_STD_IOSTREAM
1204 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1208 str
<< (const char*) s
.mb_str();
1213 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1215 str
.Printf(wxT("%p"), m_value
);
1219 #if wxUSE_STD_IOSTREAM
1220 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1227 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1235 wxVariant::wxVariant( void* val
, const wxString
& name
)
1237 m_refData
= new wxVariantDataVoidPtr(val
);
1241 bool wxVariant::operator== (void* value
) const
1243 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1246 bool wxVariant::operator!= (void* value
) const
1248 return (!((*this) == (void*) value
));
1251 void wxVariant::operator= (void* value
)
1253 if (GetType() == wxT("void*") && (m_refData
->GetRefCount() == 1))
1255 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1260 m_refData
= new wxVariantDataVoidPtr(value
);
1264 void* wxVariant::GetVoidPtr() const
1266 // handling this specially is convenient when working with COM, see #9873
1270 wxASSERT( GetType() == wxT("void*") );
1272 return (void*) ((wxVariantDataVoidPtr
*) m_refData
)->GetValue();
1275 // ----------------------------------------------------------------------------
1276 // wxVariantDataDateTime
1277 // ----------------------------------------------------------------------------
1281 class wxVariantDataDateTime
: public wxVariantData
1284 wxVariantDataDateTime() { }
1285 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1287 inline wxDateTime
GetValue() const { return m_value
; }
1288 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1290 virtual bool Eq(wxVariantData
& data
) const;
1291 #if wxUSE_STD_IOSTREAM
1292 virtual bool Write(wxSTD ostream
& str
) const;
1294 virtual bool Write(wxString
& str
) const;
1295 #if wxUSE_STD_IOSTREAM
1296 virtual bool Read(wxSTD istream
& str
);
1298 virtual bool Read(wxString
& str
);
1299 virtual wxString
GetType() const { return wxT("datetime"); }
1300 virtual wxVariantData
* Clone() const { return new wxVariantDataDateTime(m_value
); }
1302 DECLARE_WXANY_CONVERSION()
1307 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxDateTime
, wxVariantDataDateTime
)
1309 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1311 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1313 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1315 return (otherData
.m_value
== m_value
);
1319 #if wxUSE_STD_IOSTREAM
1320 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1324 str
<< value
.c_str();
1330 bool wxVariantDataDateTime::Write(wxString
& str
) const
1332 if ( m_value
.IsValid() )
1333 str
= m_value
.Format();
1335 str
= wxS("Invalid");
1340 #if wxUSE_STD_IOSTREAM
1341 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1349 bool wxVariantDataDateTime::Read(wxString
& str
)
1351 if ( str
== wxS("Invalid") )
1353 m_value
= wxInvalidDateTime
;
1357 wxString::const_iterator end
;
1358 return m_value
.ParseDateTime(str
, &end
) && end
== str
.end();
1363 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1365 m_refData
= new wxVariantDataDateTime(val
);
1369 bool wxVariant::operator== (const wxDateTime
& value
) const
1371 wxDateTime thisValue
;
1372 if (!Convert(&thisValue
))
1375 return value
.IsEqualTo(thisValue
);
1378 bool wxVariant::operator!= (const wxDateTime
& value
) const
1380 return (!((*this) == value
));
1383 void wxVariant::operator= (const wxDateTime
& value
)
1385 if (GetType() == wxT("datetime") &&
1386 m_refData
->GetRefCount() == 1)
1388 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1393 m_refData
= new wxVariantDataDateTime(value
);
1397 wxDateTime
wxVariant::GetDateTime() const
1400 if (!Convert(& value
))
1402 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1408 #endif // wxUSE_DATETIME
1410 // ----------------------------------------------------------------------------
1411 // wxVariantDataArrayString
1412 // ----------------------------------------------------------------------------
1414 class wxVariantDataArrayString
: public wxVariantData
1417 wxVariantDataArrayString() { }
1418 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1420 wxArrayString
GetValue() const { return m_value
; }
1421 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1423 virtual bool Eq(wxVariantData
& data
) const;
1424 #if wxUSE_STD_IOSTREAM
1425 virtual bool Write(wxSTD ostream
& str
) const;
1427 virtual bool Write(wxString
& str
) const;
1428 #if wxUSE_STD_IOSTREAM
1429 virtual bool Read(wxSTD istream
& str
);
1431 virtual bool Read(wxString
& str
);
1432 virtual wxString
GetType() const { return wxT("arrstring"); }
1433 virtual wxVariantData
* Clone() const { return new wxVariantDataArrayString(m_value
); }
1435 DECLARE_WXANY_CONVERSION()
1437 wxArrayString m_value
;
1440 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(wxArrayString
, wxVariantDataArrayString
)
1442 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1444 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1446 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1448 return otherData
.m_value
== m_value
;
1451 #if wxUSE_STD_IOSTREAM
1452 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1459 bool wxVariantDataArrayString::Write(wxString
& str
) const
1461 size_t count
= m_value
.GetCount();
1462 for ( size_t n
= 0; n
< count
; n
++ )
1474 #if wxUSE_STD_IOSTREAM
1475 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1483 bool wxVariantDataArrayString::Read(wxString
& str
)
1485 wxStringTokenizer
tk(str
, wxT(";"));
1486 while ( tk
.HasMoreTokens() )
1488 m_value
.Add(tk
.GetNextToken());
1496 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1498 m_refData
= new wxVariantDataArrayString(val
);
1502 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1504 wxFAIL_MSG( wxT("TODO") );
1509 bool wxVariant::operator!=(const wxArrayString
& value
) const
1511 return !(*this == value
);
1514 void wxVariant::operator=(const wxArrayString
& value
)
1516 if (GetType() == wxT("arrstring") &&
1517 m_refData
->GetRefCount() == 1)
1519 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1524 m_refData
= new wxVariantDataArrayString(value
);
1528 wxArrayString
wxVariant::GetArrayString() const
1530 if ( GetType() == wxT("arrstring") )
1531 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1533 return wxArrayString();
1536 // ----------------------------------------------------------------------------
1537 // wxVariantDataLongLong
1538 // ----------------------------------------------------------------------------
1542 class WXDLLIMPEXP_BASE wxVariantDataLongLong
: public wxVariantData
1545 wxVariantDataLongLong() { m_value
= 0; }
1546 wxVariantDataLongLong(wxLongLong value
) { m_value
= value
; }
1548 wxLongLong
GetValue() const { return m_value
; }
1549 void SetValue(wxLongLong value
) { m_value
= value
; }
1551 virtual bool Eq(wxVariantData
& data
) const;
1553 virtual bool Read(wxString
& str
);
1554 virtual bool Write(wxString
& str
) const;
1555 #if wxUSE_STD_IOSTREAM
1556 virtual bool Read(wxSTD istream
& str
);
1557 virtual bool Write(wxSTD ostream
& str
) const;
1560 virtual bool Read(wxInputStream
& str
);
1561 virtual bool Write(wxOutputStream
&str
) const;
1562 #endif // wxUSE_STREAMS
1564 wxVariantData
* Clone() const
1566 return new wxVariantDataLongLong(m_value
);
1569 virtual wxString
GetType() const { return wxS("longlong"); }
1571 DECLARE_WXANY_CONVERSION()
1577 // wxLongLong type requires customized wxAny conversion code
1582 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1584 *any
= m_value
.GetValue();
1588 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1590 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong_t
));
1593 REGISTER_WXANY_CONVERSION(wxLongLong_t
, wxVariantDataLongLong
)
1595 #else // if !defined(wxLongLong_t)
1597 bool wxVariantDataLongLong::GetAsAny(wxAny
* any
) const
1603 wxVariantData
* wxVariantDataLongLong::VariantDataFactory(const wxAny
& any
)
1605 return new wxVariantDataLongLong(wxANY_AS(any
, wxLongLong
));
1608 REGISTER_WXANY_CONVERSION(wxLongLong
, wxVariantDataLongLong
)
1610 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1613 bool wxVariantDataLongLong::Eq(wxVariantData
& data
) const
1615 wxASSERT_MSG( (data
.GetType() == wxS("longlong")),
1616 "wxVariantDataLongLong::Eq: argument mismatch" );
1618 wxVariantDataLongLong
& otherData
= (wxVariantDataLongLong
&) data
;
1620 return (otherData
.m_value
== m_value
);
1623 #if wxUSE_STD_IOSTREAM
1624 bool wxVariantDataLongLong::Write(wxSTD ostream
& str
) const
1628 str
<< (const char*) s
.mb_str();
1633 bool wxVariantDataLongLong::Write(wxString
& str
) const
1636 str
.Printf(wxS("%lld"), m_value
.GetValue());
1643 #if wxUSE_STD_IOSTREAM
1644 bool wxVariantDataLongLong::Read(wxSTD istream
& WXUNUSED(str
))
1646 wxFAIL_MSG(wxS("Unimplemented"));
1652 bool wxVariantDataLongLong::Write(wxOutputStream
& str
) const
1654 wxTextOutputStream
s(str
);
1655 s
.Write32(m_value
.GetLo());
1656 s
.Write32(m_value
.GetHi());
1660 bool wxVariantDataLongLong::Read(wxInputStream
& str
)
1662 wxTextInputStream
s(str
);
1663 unsigned long lo
= s
.Read32();
1664 long hi
= s
.Read32();
1665 m_value
= wxLongLong(hi
, lo
);
1668 #endif // wxUSE_STREAMS
1670 bool wxVariantDataLongLong::Read(wxString
& str
)
1673 wxLongLong_t value_t
;
1674 if ( !str
.ToLongLong(&value_t
) )
1685 wxVariant::wxVariant(wxLongLong val
, const wxString
& name
)
1687 m_refData
= new wxVariantDataLongLong(val
);
1691 bool wxVariant::operator==(wxLongLong value
) const
1693 wxLongLong thisValue
;
1694 if ( !Convert(&thisValue
) )
1697 return (value
== thisValue
);
1700 bool wxVariant::operator!=(wxLongLong value
) const
1702 return (!((*this) == value
));
1705 void wxVariant::operator=(wxLongLong value
)
1707 if ( GetType() == wxS("longlong") &&
1708 m_refData
->GetRefCount() == 1 )
1710 ((wxVariantDataLongLong
*)GetData())->SetValue(value
);
1715 m_refData
= new wxVariantDataLongLong(value
);
1719 wxLongLong
wxVariant::GetLongLong() const
1722 if ( Convert(&value
) )
1728 wxFAIL_MSG(wxT("Could not convert to a long long"));
1733 #endif // wxUSE_LONGLONG
1735 // ----------------------------------------------------------------------------
1736 // wxVariantDataULongLong
1737 // ----------------------------------------------------------------------------
1741 class WXDLLIMPEXP_BASE wxVariantDataULongLong
: public wxVariantData
1744 wxVariantDataULongLong() { m_value
= 0; }
1745 wxVariantDataULongLong(wxULongLong value
) { m_value
= value
; }
1747 wxULongLong
GetValue() const { return m_value
; }
1748 void SetValue(wxULongLong value
) { m_value
= value
; }
1750 virtual bool Eq(wxVariantData
& data
) const;
1752 virtual bool Read(wxString
& str
);
1753 virtual bool Write(wxString
& str
) const;
1754 #if wxUSE_STD_IOSTREAM
1755 virtual bool Read(wxSTD istream
& str
);
1756 virtual bool Write(wxSTD ostream
& str
) const;
1759 virtual bool Read(wxInputStream
& str
);
1760 virtual bool Write(wxOutputStream
&str
) const;
1761 #endif // wxUSE_STREAMS
1763 wxVariantData
* Clone() const
1765 return new wxVariantDataULongLong(m_value
);
1768 virtual wxString
GetType() const { return wxS("ulonglong"); }
1770 DECLARE_WXANY_CONVERSION()
1772 wxULongLong m_value
;
1776 // wxULongLong type requires customized wxAny conversion code
1781 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1783 *any
= m_value
.GetValue();
1787 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1789 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong_t
));
1792 REGISTER_WXANY_CONVERSION(wxULongLong_t
, wxVariantDataULongLong
)
1794 #else // if !defined(wxLongLong_t)
1796 bool wxVariantDataULongLong::GetAsAny(wxAny
* any
) const
1802 wxVariantData
* wxVariantDataULongLong::VariantDataFactory(const wxAny
& any
)
1804 return new wxVariantDataULongLong(wxANY_AS(any
, wxULongLong
));
1807 REGISTER_WXANY_CONVERSION(wxULongLong
, wxVariantDataULongLong
)
1809 #endif // defined(wxLongLong_t)/!defined(wxLongLong_t)
1813 bool wxVariantDataULongLong::Eq(wxVariantData
& data
) const
1815 wxASSERT_MSG( (data
.GetType() == wxS("ulonglong")),
1816 "wxVariantDataULongLong::Eq: argument mismatch" );
1818 wxVariantDataULongLong
& otherData
= (wxVariantDataULongLong
&) data
;
1820 return (otherData
.m_value
== m_value
);
1823 #if wxUSE_STD_IOSTREAM
1824 bool wxVariantDataULongLong::Write(wxSTD ostream
& str
) const
1828 str
<< (const char*) s
.mb_str();
1833 bool wxVariantDataULongLong::Write(wxString
& str
) const
1836 str
.Printf(wxS("%llu"), m_value
.GetValue());
1843 #if wxUSE_STD_IOSTREAM
1844 bool wxVariantDataULongLong::Read(wxSTD istream
& WXUNUSED(str
))
1846 wxFAIL_MSG(wxS("Unimplemented"));
1852 bool wxVariantDataULongLong::Write(wxOutputStream
& str
) const
1854 wxTextOutputStream
s(str
);
1855 s
.Write32(m_value
.GetLo());
1856 s
.Write32(m_value
.GetHi());
1860 bool wxVariantDataULongLong::Read(wxInputStream
& str
)
1862 wxTextInputStream
s(str
);
1863 unsigned long lo
= s
.Read32();
1864 long hi
= s
.Read32();
1865 m_value
= wxULongLong(hi
, lo
);
1868 #endif // wxUSE_STREAMS
1870 bool wxVariantDataULongLong::Read(wxString
& str
)
1873 wxULongLong_t value_t
;
1874 if ( !str
.ToULongLong(&value_t
) )
1885 wxVariant::wxVariant(wxULongLong val
, const wxString
& name
)
1887 m_refData
= new wxVariantDataULongLong(val
);
1891 bool wxVariant::operator==(wxULongLong value
) const
1893 wxULongLong thisValue
;
1894 if ( !Convert(&thisValue
) )
1897 return (value
== thisValue
);
1900 bool wxVariant::operator!=(wxULongLong value
) const
1902 return (!((*this) == value
));
1905 void wxVariant::operator=(wxULongLong value
)
1907 if ( GetType() == wxS("ulonglong") &&
1908 m_refData
->GetRefCount() == 1 )
1910 ((wxVariantDataULongLong
*)GetData())->SetValue(value
);
1915 m_refData
= new wxVariantDataULongLong(value
);
1919 wxULongLong
wxVariant::GetULongLong() const
1922 if ( Convert(&value
) )
1928 wxFAIL_MSG(wxT("Could not convert to a long long"));
1933 #endif // wxUSE_LONGLONG
1935 // ----------------------------------------------------------------------------
1936 // wxVariantDataList
1937 // ----------------------------------------------------------------------------
1939 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1942 wxVariantDataList() {}
1943 wxVariantDataList(const wxVariantList
& list
);
1944 virtual ~wxVariantDataList();
1946 wxVariantList
& GetValue() { return m_value
; }
1947 void SetValue(const wxVariantList
& value
) ;
1949 virtual bool Eq(wxVariantData
& data
) const;
1950 #if wxUSE_STD_IOSTREAM
1951 virtual bool Write(wxSTD ostream
& str
) const;
1953 virtual bool Write(wxString
& str
) const;
1954 #if wxUSE_STD_IOSTREAM
1955 virtual bool Read(wxSTD istream
& str
);
1957 virtual bool Read(wxString
& str
);
1958 virtual wxString
GetType() const { return wxT("list"); }
1962 wxVariantData
* Clone() const { return new wxVariantDataList(m_value
); }
1964 DECLARE_WXANY_CONVERSION()
1966 wxVariantList m_value
;
1972 // Convert to/from list of wxAnys
1975 bool wxVariantDataList::GetAsAny(wxAny
* any
) const
1978 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
1981 wxVariant
* pVar
= node
->GetData();
1982 dst
.push_back(new wxAny(((const wxVariant
&)*pVar
)));
1983 node
= node
->GetNext();
1990 wxVariantData
* wxVariantDataList::VariantDataFactory(const wxAny
& any
)
1992 wxAnyList src
= wxANY_AS(any
, wxAnyList
);
1994 wxAnyList::compatibility_iterator node
= src
.GetFirst();
1997 wxAny
* pAny
= node
->GetData();
1998 dst
.push_back(new wxVariant(*pAny
));
1999 node
= node
->GetNext();
2002 return new wxVariantDataList(dst
);
2005 REGISTER_WXANY_CONVERSION(wxAnyList
, wxVariantDataList
)
2009 wxVariantDataList::wxVariantDataList(const wxVariantList
& list
)
2014 wxVariantDataList::~wxVariantDataList()
2019 void wxVariantDataList::SetValue(const wxVariantList
& value
)
2022 wxVariantList::compatibility_iterator node
= value
.GetFirst();
2025 wxVariant
* var
= node
->GetData();
2026 m_value
.Append(new wxVariant(*var
));
2027 node
= node
->GetNext();
2031 void wxVariantDataList::Clear()
2033 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2036 wxVariant
* var
= node
->GetData();
2038 node
= node
->GetNext();
2043 bool wxVariantDataList::Eq(wxVariantData
& data
) const
2045 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
2047 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
2048 wxVariantList::compatibility_iterator node1
= m_value
.GetFirst();
2049 wxVariantList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
2050 while (node1
&& node2
)
2052 wxVariant
* var1
= node1
->GetData();
2053 wxVariant
* var2
= node2
->GetData();
2054 if ((*var1
) != (*var2
))
2056 node1
= node1
->GetNext();
2057 node2
= node2
->GetNext();
2059 if (node1
|| node2
) return false;
2063 #if wxUSE_STD_IOSTREAM
2064 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
2068 str
<< (const char*) s
.mb_str();
2073 bool wxVariantDataList::Write(wxString
& str
) const
2075 str
= wxEmptyString
;
2076 wxVariantList::compatibility_iterator node
= m_value
.GetFirst();
2079 wxVariant
* var
= node
->GetData();
2080 if (node
!= m_value
.GetFirst())
2083 str
+= var
->MakeString();
2084 node
= node
->GetNext();
2090 #if wxUSE_STD_IOSTREAM
2091 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
2093 wxFAIL_MSG(wxT("Unimplemented"));
2099 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
2101 wxFAIL_MSG(wxT("Unimplemented"));
2108 wxVariant::wxVariant(const wxVariantList
& val
, const wxString
& name
) // List of variants
2110 m_refData
= new wxVariantDataList(val
);
2114 bool wxVariant::operator== (const wxVariantList
& value
) const
2116 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
2118 wxVariantDataList
other(value
);
2119 return (GetData()->Eq(other
));
2122 bool wxVariant::operator!= (const wxVariantList
& value
) const
2124 return (!((*this) == value
));
2127 void wxVariant::operator= (const wxVariantList
& value
)
2129 if (GetType() == wxT("list") &&
2130 m_refData
->GetRefCount() == 1)
2132 ((wxVariantDataList
*)GetData())->SetValue(value
);
2137 m_refData
= new wxVariantDataList(value
);
2141 wxVariantList
& wxVariant::GetList() const
2143 wxASSERT( (GetType() == wxT("list")) );
2145 return (wxVariantList
&) ((wxVariantDataList
*) m_refData
)->GetValue();
2149 void wxVariant::NullList()
2151 SetData(new wxVariantDataList());
2155 void wxVariant::Append(const wxVariant
& value
)
2157 wxVariantList
& list
= GetList();
2159 list
.Append(new wxVariant(value
));
2162 // Insert at front of list
2163 void wxVariant::Insert(const wxVariant
& value
)
2165 wxVariantList
& list
= GetList();
2167 list
.Insert(new wxVariant(value
));
2170 // Returns true if the variant is a member of the list
2171 bool wxVariant::Member(const wxVariant
& value
) const
2173 wxVariantList
& list
= GetList();
2175 wxVariantList::compatibility_iterator node
= list
.GetFirst();
2178 wxVariant
* other
= node
->GetData();
2179 if (value
== *other
)
2181 node
= node
->GetNext();
2186 // Deletes the nth element of the list
2187 bool wxVariant::Delete(size_t item
)
2189 wxVariantList
& list
= GetList();
2191 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
2192 wxVariantList::compatibility_iterator node
= list
.Item(item
);
2193 wxVariant
* variant
= node
->GetData();
2200 void wxVariant::ClearList()
2202 if (!IsNull() && (GetType() == wxT("list")))
2204 ((wxVariantDataList
*) m_refData
)->Clear();
2208 if (!GetType().IsSameAs(wxT("list")))
2211 m_refData
= new wxVariantDataList
;
2215 // Treat a list variant as an array
2216 wxVariant
wxVariant::operator[] (size_t idx
) const
2218 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
2220 if (GetType() == wxT("list"))
2222 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2223 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2224 return *(data
->GetValue().Item(idx
)->GetData());
2226 return wxNullVariant
;
2229 wxVariant
& wxVariant::operator[] (size_t idx
)
2231 // We can't return a reference to a variant for a string list, since the string
2232 // is actually stored as a char*, not a variant.
2234 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
2236 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2237 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
2239 return * (data
->GetValue().Item(idx
)->GetData());
2242 // Return the number of elements in a list
2243 size_t wxVariant::GetCount() const
2245 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
2247 if (GetType() == wxT("list"))
2249 wxVariantDataList
* data
= (wxVariantDataList
*) m_refData
;
2250 return data
->GetValue().GetCount();
2255 // ----------------------------------------------------------------------------
2257 // ----------------------------------------------------------------------------
2259 bool wxVariant::Convert(long* value
) const
2261 wxString
type(GetType());
2262 if (type
== wxS("double"))
2263 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
2264 else if (type
== wxS("long"))
2265 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2266 else if (type
== wxS("bool"))
2267 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2268 else if (type
== wxS("string"))
2269 *value
= wxAtol(((wxVariantDataString
*)GetData())->GetValue());
2271 else if (type
== wxS("longlong"))
2273 wxLongLong v
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2274 // Don't convert if return value would be vague
2275 if ( v
< LONG_MIN
|| v
> LONG_MAX
)
2277 *value
= v
.ToLong();
2279 else if (type
== wxS("ulonglong"))
2281 wxULongLong v
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2282 // Don't convert if return value would be vague
2285 *value
= (long) v
.ToULong();
2294 bool wxVariant::Convert(bool* value
) const
2296 wxString
type(GetType());
2297 if (type
== wxT("double"))
2298 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
2299 else if (type
== wxT("long"))
2300 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
2301 else if (type
== wxT("bool"))
2302 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
2303 else if (type
== wxT("string"))
2305 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
2307 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
2309 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
2320 bool wxVariant::Convert(double* value
) const
2322 wxString
type(GetType());
2323 if (type
== wxT("double"))
2324 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
2325 else if (type
== wxT("long"))
2326 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
2327 else if (type
== wxT("bool"))
2328 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
2329 else if (type
== wxT("string"))
2330 *value
= (double) wxAtof(((wxVariantDataString
*)GetData())->GetValue());
2332 else if (type
== wxS("longlong"))
2334 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue().ToDouble();
2336 else if (type
== wxS("ulonglong"))
2338 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue().ToDouble();
2347 bool wxVariant::Convert(wxUniChar
* value
) const
2349 wxString
type(GetType());
2350 if (type
== wxT("char"))
2351 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
2352 else if (type
== wxT("long"))
2353 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
2354 else if (type
== wxT("bool"))
2355 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
2356 else if (type
== wxS("string"))
2358 // Also accept strings of length 1
2359 const wxString
& str
= (((wxVariantDataString
*)GetData())->GetValue());
2360 if ( str
.length() == 1 )
2371 bool wxVariant::Convert(char* value
) const
2374 if ( !Convert(&ch
) )
2380 bool wxVariant::Convert(wchar_t* value
) const
2383 if ( !Convert(&ch
) )
2389 bool wxVariant::Convert(wxString
* value
) const
2391 *value
= MakeString();
2396 bool wxVariant::Convert(wxLongLong
* value
) const
2398 wxString
type(GetType());
2399 if (type
== wxS("longlong"))
2400 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2401 else if (type
== wxS("long"))
2402 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2403 else if (type
== wxS("string"))
2405 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2407 wxLongLong_t value_t
;
2408 if ( !s
.ToLongLong(&value_t
) )
2413 if ( !s
.ToLong(&l_value
) )
2418 else if (type
== wxS("bool"))
2419 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2420 else if (type
== wxS("double"))
2422 value
->Assign(((wxVariantDoubleData
*)GetData())->GetValue());
2424 else if (type
== wxS("ulonglong"))
2425 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2432 bool wxVariant::Convert(wxULongLong
* value
) const
2434 wxString
type(GetType());
2435 if (type
== wxS("ulonglong"))
2436 *value
= ((wxVariantDataULongLong
*)GetData())->GetValue();
2437 else if (type
== wxS("long"))
2438 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
2439 else if (type
== wxS("string"))
2441 wxString s
= ((wxVariantDataString
*)GetData())->GetValue();
2443 wxULongLong_t value_t
;
2444 if ( !s
.ToULongLong(&value_t
) )
2448 unsigned long l_value
;
2449 if ( !s
.ToULong(&l_value
) )
2454 else if (type
== wxS("bool"))
2455 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
2456 else if (type
== wxS("double"))
2458 double value_d
= ((wxVariantDoubleData
*)GetData())->GetValue();
2460 if ( value_d
< 0.0 )
2464 *value
= (wxULongLong_t
) value_d
;
2467 temp
.Assign(value_d
);
2471 else if (type
== wxS("longlong"))
2472 *value
= ((wxVariantDataLongLong
*)GetData())->GetValue();
2478 #endif // wxUSE_LONGLONG
2481 bool wxVariant::Convert(wxDateTime
* value
) const
2483 wxString
type(GetType());
2484 if (type
== wxT("datetime"))
2486 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
2490 // Fallback to string conversion
2492 if ( !Convert(&val
) )
2495 // Try to parse this as either date and time, only date or only time
2496 // checking that the entire string was parsed
2497 wxString::const_iterator end
;
2498 if ( value
->ParseDateTime(val
, &end
) && end
== val
.end() )
2501 if ( value
->ParseDate(val
, &end
) && end
== val
.end() )
2504 if ( value
->ParseTime(val
, &end
) && end
== val
.end() )
2509 #endif // wxUSE_DATETIME
2511 #endif // wxUSE_VARIANT