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"
27 #include "wx/stream.h"
31 #if wxUSE_STD_IOSTREAM
39 #if defined(__MWERKS__) && __MSL__ >= 0x6000
45 #include "wx/txtstrm.h"
48 #include "wx/string.h"
49 #include "wx/tokenzr.h"
51 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
53 wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
60 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
62 wxVariant::wxVariant()
64 m_data
= (wxVariantData
*) NULL
;
67 bool wxVariant::IsNull() const
69 return (m_data
== (wxVariantData
*) NULL
);
72 void wxVariant::MakeNull()
77 void wxVariant::Clear()
79 m_name
= wxEmptyString
;
82 wxVariant::wxVariant(const wxVariant
& variant
)
85 m_data
= (wxVariantData
*) NULL
;
87 if (!variant
.IsNull())
90 m_name
= variant
.m_name
;
93 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
99 wxVariant::~wxVariant()
105 void wxVariant::operator= (const wxVariant
& variant
)
108 m_name
= variant
.m_name
;
111 // myVariant = new wxStringVariantData("hello")
112 void wxVariant::operator= (wxVariantData
* variantData
)
115 m_data
= variantData
;
118 bool wxVariant::operator== (const wxVariant
& variant
) const
120 if (IsNull() || variant
.IsNull())
121 return (IsNull() == variant
.IsNull());
123 return (GetData()->Eq(* variant
.GetData()));
126 bool wxVariant::operator!= (const wxVariant
& variant
) const
128 return (!(*this == variant
));
132 wxString
wxVariant::MakeString() const
137 if (GetData()->Write(str
))
140 return wxEmptyString
;
143 void wxVariant::SetData(wxVariantData
* data
)
149 void wxVariant::Ref(const wxVariant
& clone
)
151 // nothing to be done
152 if (m_data
== clone
.m_data
)
155 // delete reference to old data
158 // reference new data
161 m_data
= clone
.m_data
;
167 void wxVariant::UnRef()
171 wxASSERT_MSG( m_data
->m_count
> 0, _T("invalid ref data count") );
179 // Returns a string representing the type of the variant,
180 // e.g. "string", "bool", "list", "double", "long"
181 wxString
wxVariant::GetType() const
184 return wxString(wxT("null"));
186 return GetData()->GetType();
190 bool wxVariant::IsType(const wxString
& type
) const
192 return (GetType() == type
);
195 bool wxVariant::IsValueKindOf(const wxClassInfo
* type
) const
197 wxClassInfo
* info
=GetData()->GetValueClassInfo();
198 return info
? info
->IsKindOf(type
) : false ;
202 // -----------------------------------------------------------------
204 // -----------------------------------------------------------------
206 class WXDLLIMPEXP_BASE wxVariantDataLong
: public wxVariantData
208 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
210 wxVariantDataLong() { m_value
= 0; }
211 wxVariantDataLong(long value
) { m_value
= value
; }
213 inline long GetValue() const { return m_value
; }
214 inline void SetValue(long value
) { m_value
= value
; }
216 virtual bool Eq(wxVariantData
& data
) const;
218 virtual bool Read(wxString
& str
);
219 virtual bool Write(wxString
& str
) const;
220 #if wxUSE_STD_IOSTREAM
221 virtual bool Read(wxSTD istream
& str
);
222 virtual bool Write(wxSTD ostream
& str
) const;
225 virtual bool Read(wxInputStream
& str
);
226 virtual bool Write(wxOutputStream
&str
) const;
227 #endif // wxUSE_STREAMS
229 virtual wxString
GetType() const { return wxT("long"); };
235 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
237 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
239 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
241 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
243 return (otherData
.m_value
== m_value
);
246 #if wxUSE_STD_IOSTREAM
247 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
251 str
<< (const char*) s
.mb_str();
256 bool wxVariantDataLong::Write(wxString
& str
) const
258 str
.Printf(wxT("%ld"), m_value
);
262 #if wxUSE_STD_IOSTREAM
263 bool wxVariantDataLong::Read(wxSTD istream
& str
)
271 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
273 wxTextOutputStream
s(str
);
275 s
.Write32((size_t)m_value
);
279 bool wxVariantDataLong::Read(wxInputStream
& str
)
281 wxTextInputStream
s(str
);
282 m_value
= s
.Read32();
285 #endif // wxUSE_STREAMS
287 bool wxVariantDataLong::Read(wxString
& str
)
289 m_value
= wxAtol((const wxChar
*) str
);
295 wxVariant::wxVariant(long val
, const wxString
& name
)
297 m_data
= new wxVariantDataLong(val
);
301 wxVariant::wxVariant(int val
, const wxString
& name
)
303 m_data
= new wxVariantDataLong((long)val
);
307 wxVariant::wxVariant(short val
, const wxString
& name
)
309 m_data
= new wxVariantDataLong((long)val
);
313 bool wxVariant::operator== (long value
) const
316 if (!Convert(&thisValue
))
319 return (value
== thisValue
);
322 bool wxVariant::operator!= (long value
) const
324 return (!((*this) == value
));
327 void wxVariant::operator= (long value
)
329 if (GetType() == wxT("long") &&
330 m_data
->GetRefCount() == 1)
332 ((wxVariantDataLong
*)GetData())->SetValue(value
);
337 m_data
= new wxVariantDataLong(value
);
341 long wxVariant::GetLong() const
344 if (Convert(& value
))
348 wxFAIL_MSG(wxT("Could not convert to a long"));
353 // -----------------------------------------------------------------
354 // wxVariantDoubleData
355 // -----------------------------------------------------------------
357 class WXDLLIMPEXP_BASE wxVariantDoubleData
: public wxVariantData
359 DECLARE_DYNAMIC_CLASS(wxVariantDoubleData
)
361 wxVariantDoubleData() { m_value
= 0.0; }
362 wxVariantDoubleData(double value
) { m_value
= value
; }
364 inline double GetValue() const { return m_value
; }
365 inline void SetValue(double value
) { m_value
= value
; }
367 virtual bool Eq(wxVariantData
& data
) const;
368 virtual bool Read(wxString
& str
);
369 #if wxUSE_STD_IOSTREAM
370 virtual bool Write(wxSTD ostream
& str
) const;
372 virtual bool Write(wxString
& str
) const;
373 #if wxUSE_STD_IOSTREAM
374 virtual bool Read(wxSTD istream
& str
);
377 virtual bool Read(wxInputStream
& str
);
378 virtual bool Write(wxOutputStream
&str
) const;
379 #endif // wxUSE_STREAMS
380 virtual wxString
GetType() const { return wxT("double"); };
386 IMPLEMENT_DYNAMIC_CLASS(wxVariantDoubleData
, wxVariantData
)
388 bool wxVariantDoubleData::Eq(wxVariantData
& data
) const
390 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
392 wxVariantDoubleData
& otherData
= (wxVariantDoubleData
&) data
;
394 return wxIsSameDouble(otherData
.m_value
, m_value
);
397 #if wxUSE_STD_IOSTREAM
398 bool wxVariantDoubleData::Write(wxSTD ostream
& str
) const
402 str
<< (const char*) s
.mb_str();
407 bool wxVariantDoubleData::Write(wxString
& str
) const
409 str
.Printf(wxT("%.14g"), m_value
);
413 #if wxUSE_STD_IOSTREAM
414 bool wxVariantDoubleData::Read(wxSTD istream
& str
)
422 bool wxVariantDoubleData::Write(wxOutputStream
& str
) const
424 wxTextOutputStream
s(str
);
425 s
.WriteDouble((double)m_value
);
429 bool wxVariantDoubleData::Read(wxInputStream
& str
)
431 wxTextInputStream
s(str
);
432 m_value
= (float)s
.ReadDouble();
435 #endif // wxUSE_STREAMS
437 bool wxVariantDoubleData::Read(wxString
& str
)
439 m_value
= wxAtof((const wxChar
*) str
);
443 // wxVariant double code
445 wxVariant::wxVariant(double val
, const wxString
& name
)
447 m_data
= new wxVariantDoubleData(val
);
451 bool wxVariant::operator== (double value
) const
454 if (!Convert(&thisValue
))
457 return wxIsSameDouble(value
, thisValue
);
460 bool wxVariant::operator!= (double value
) const
462 return (!((*this) == value
));
465 void wxVariant::operator= (double value
)
467 if (GetType() == wxT("double") &&
468 m_data
->GetRefCount() == 1)
470 ((wxVariantDoubleData
*)GetData())->SetValue(value
);
475 m_data
= new wxVariantDoubleData(value
);
479 double wxVariant::GetDouble() const
482 if (Convert(& value
))
486 wxFAIL_MSG(wxT("Could not convert to a double number"));
491 // -----------------------------------------------------------------
493 // -----------------------------------------------------------------
497 class WXDLLIMPEXP_BASE wxVariantDataBool
: public wxVariantData
499 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
501 wxVariantDataBool() { m_value
= 0; }
502 wxVariantDataBool(bool value
) { m_value
= value
; }
504 inline bool GetValue() const { return m_value
; }
505 inline void SetValue(bool value
) { m_value
= value
; }
507 virtual bool Eq(wxVariantData
& data
) const;
508 #if wxUSE_STD_IOSTREAM
509 virtual bool Write(wxSTD ostream
& str
) const;
511 virtual bool Write(wxString
& str
) const;
512 virtual bool Read(wxString
& str
);
513 #if wxUSE_STD_IOSTREAM
514 virtual bool Read(wxSTD istream
& str
);
517 virtual bool Read(wxInputStream
& str
);
518 virtual bool Write(wxOutputStream
& str
) const;
519 #endif // wxUSE_STREAMS
520 virtual wxString
GetType() const { return wxT("bool"); };
526 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
528 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
530 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
532 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
534 return (otherData
.m_value
== m_value
);
537 #if wxUSE_STD_IOSTREAM
538 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
542 str
<< (const char*) s
.mb_str();
547 bool wxVariantDataBool::Write(wxString
& str
) const
549 str
.Printf(wxT("%d"), (int) m_value
);
553 #if wxUSE_STD_IOSTREAM
554 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
556 wxFAIL_MSG(wxT("Unimplemented"));
557 // str >> (long) m_value;
563 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
565 wxTextOutputStream
s(str
);
571 bool wxVariantDataBool::Read(wxInputStream
& str
)
573 wxTextInputStream
s(str
);
575 m_value
= s
.Read8() != 0;
578 #endif // wxUSE_STREAMS
580 bool wxVariantDataBool::Read(wxString
& str
)
582 m_value
= (wxAtol((const wxChar
*) str
) != 0);
588 wxVariant::wxVariant(bool val
, const wxString
& name
)
590 m_data
= new wxVariantDataBool(val
);
594 bool wxVariant::operator== (bool value
) const
597 if (!Convert(&thisValue
))
600 return (value
== thisValue
);
603 bool wxVariant::operator!= (bool value
) const
605 return (!((*this) == value
));
608 void wxVariant::operator= (bool value
)
610 if (GetType() == wxT("bool") &&
611 m_data
->GetRefCount() == 1)
613 ((wxVariantDataBool
*)GetData())->SetValue(value
);
618 m_data
= new wxVariantDataBool(value
);
622 bool wxVariant::GetBool() const
625 if (Convert(& value
))
629 wxFAIL_MSG(wxT("Could not convert to a bool"));
636 // -----------------------------------------------------------------
638 // -----------------------------------------------------------------
640 class WXDLLIMPEXP_BASE wxVariantDataChar
: public wxVariantData
642 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
644 wxVariantDataChar() { m_value
= 0; }
645 wxVariantDataChar(wxChar value
) { m_value
= value
; }
647 inline wxChar
GetValue() const { return m_value
; }
648 inline void SetValue(wxChar value
) { m_value
= value
; }
650 virtual bool Eq(wxVariantData
& data
) const;
651 #if wxUSE_STD_IOSTREAM
652 virtual bool Read(wxSTD istream
& str
);
653 virtual bool Write(wxSTD ostream
& str
) const;
655 virtual bool Read(wxString
& str
);
656 virtual bool Write(wxString
& str
) const;
658 virtual bool Read(wxInputStream
& str
);
659 virtual bool Write(wxOutputStream
& str
) const;
660 #endif // wxUSE_STREAMS
661 virtual wxString
GetType() const { return wxT("char"); };
667 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
669 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
671 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
673 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
675 return (otherData
.m_value
== m_value
);
678 #if wxUSE_STD_IOSTREAM
679 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
683 str
<< (const char*) s
.mb_str();
688 bool wxVariantDataChar::Write(wxString
& str
) const
690 str
.Printf(wxT("%c"), m_value
);
694 #if wxUSE_STD_IOSTREAM
695 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
697 wxFAIL_MSG(wxT("Unimplemented"));
704 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
706 wxTextOutputStream
s(str
);
713 bool wxVariantDataChar::Read(wxInputStream
& str
)
715 wxTextInputStream
s(str
);
721 #endif // wxUSE_STREAMS
723 bool wxVariantDataChar::Read(wxString
& str
)
725 m_value
= str
[size_t(0)];
729 wxVariant::wxVariant(wxChar val
, const wxString
& name
)
731 m_data
= new wxVariantDataChar(val
);
735 bool wxVariant::operator== (wxChar value
) const
738 if (!Convert(&thisValue
))
741 return (value
== thisValue
);
744 bool wxVariant::operator!= (wxChar value
) const
746 return (!((*this) == value
));
749 void wxVariant::operator= (wxChar value
)
751 if (GetType() == wxT("char") &&
752 m_data
->GetRefCount() == 1)
754 ((wxVariantDataChar
*)GetData())->SetValue(value
);
759 m_data
= new wxVariantDataChar(value
);
763 wxChar
wxVariant::GetChar() const
766 if (Convert(& value
))
770 wxFAIL_MSG(wxT("Could not convert to a char"));
775 // ----------------------------------------------------------------------------
776 // wxVariantDataString
777 // ----------------------------------------------------------------------------
779 class WXDLLIMPEXP_BASE wxVariantDataString
: public wxVariantData
781 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
783 wxVariantDataString() { }
784 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
786 inline wxString
GetValue() const { return m_value
; }
787 inline void SetValue(const wxString
& value
) { m_value
= value
; }
789 virtual bool Eq(wxVariantData
& data
) const;
790 #if wxUSE_STD_IOSTREAM
791 virtual bool Write(wxSTD ostream
& str
) const;
793 virtual bool Read(wxString
& str
);
794 virtual bool Write(wxString
& str
) const;
795 #if wxUSE_STD_IOSTREAM
796 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; };
799 virtual bool Read(wxInputStream
& str
);
800 virtual bool Write(wxOutputStream
& str
) const;
801 #endif // wxUSE_STREAMS
802 virtual wxString
GetType() const { return wxT("string"); };
808 bool wxVariantDataString::Eq(wxVariantData
& data
) const
810 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
812 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
814 return (otherData
.m_value
== m_value
);
817 #if wxUSE_STD_IOSTREAM
818 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
820 str
<< (const char*) m_value
.mb_str();
825 bool wxVariantDataString::Write(wxString
& str
) const
832 bool wxVariantDataString::Write(wxOutputStream
& str
) const
834 // why doesn't wxOutputStream::operator<< take "const wxString&"
835 wxTextOutputStream
s(str
);
836 s
.WriteString(m_value
);
840 bool wxVariantDataString::Read(wxInputStream
& str
)
842 wxTextInputStream
s(str
);
844 m_value
= s
.ReadLine();
847 #endif // wxUSE_STREAMS
849 bool wxVariantDataString::Read(wxString
& str
)
855 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
859 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
861 m_data
= new wxVariantDataString(val
);
865 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
867 m_data
= new wxVariantDataString(wxString(val
));
871 bool wxVariant::operator== (const wxString
& value
) const
874 if (!Convert(&thisValue
))
877 return value
== thisValue
;
880 bool wxVariant::operator!= (const wxString
& value
) const
882 return (!((*this) == value
));
885 void wxVariant::operator= (const wxString
& value
)
887 if (GetType() == wxT("string") &&
888 m_data
->GetRefCount() == 1)
890 ((wxVariantDataString
*)GetData())->SetValue(value
);
895 m_data
= new wxVariantDataString(value
);
899 void wxVariant::operator= (const wxChar
* value
)
901 if (GetType() == wxT("string") &&
902 m_data
->GetRefCount() == 1)
904 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
909 m_data
= new wxVariantDataString(wxString(value
));
913 wxString
wxVariant::GetString() const
916 if (!Convert(& value
))
918 wxFAIL_MSG(wxT("Could not convert to a string"));
924 // ----------------------------------------------------------------------------
925 // wxVariantDataWxObjectPtr
926 // ----------------------------------------------------------------------------
928 class wxVariantDataWxObjectPtr
: public wxVariantData
930 DECLARE_DYNAMIC_CLASS(wxVariantDataWxObjectPtr
)
932 wxVariantDataWxObjectPtr() { }
933 wxVariantDataWxObjectPtr(wxObject
* value
) { m_value
= value
; }
935 inline wxObject
* GetValue() const { return m_value
; }
936 inline void SetValue(wxObject
* value
) { m_value
= value
; }
938 virtual bool Eq(wxVariantData
& data
) const;
939 #if wxUSE_STD_IOSTREAM
940 virtual bool Write(wxSTD ostream
& str
) const;
942 virtual bool Write(wxString
& str
) const;
943 #if wxUSE_STD_IOSTREAM
944 virtual bool Read(wxSTD istream
& str
);
946 virtual bool Read(wxString
& str
);
947 virtual wxString
GetType() const ;
948 virtual wxVariantData
* Clone() { return new wxVariantDataWxObjectPtr
; }
950 virtual wxClassInfo
* GetValueClassInfo() ;
954 DECLARE_NO_COPY_CLASS(wxVariantDataWxObjectPtr
)
957 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataWxObjectPtr
, wxVariantData
)
959 bool wxVariantDataWxObjectPtr::Eq(wxVariantData
& data
) const
961 wxASSERT_MSG( wxIsKindOf((&data
), wxVariantDataWxObjectPtr
), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
963 wxVariantDataWxObjectPtr
& otherData
= (wxVariantDataWxObjectPtr
&) data
;
965 return (otherData
.m_value
== m_value
);
968 wxString
wxVariantDataWxObjectPtr::GetType() const
970 wxString
returnVal(wxT("wxObject"));
972 returnVal
= m_value
->GetClassInfo()->GetClassName();
977 wxClassInfo
* wxVariantDataWxObjectPtr::GetValueClassInfo()
979 wxClassInfo
* returnVal
=NULL
;
981 if (m_value
) returnVal
= m_value
->GetClassInfo();
986 #if wxUSE_STD_IOSTREAM
987 bool wxVariantDataWxObjectPtr::Write(wxSTD ostream
& str
) const
991 str
<< (const char*) s
.mb_str();
996 bool wxVariantDataWxObjectPtr::Write(wxString
& str
) const
998 str
.Printf(wxT("%s(%p)"), GetType().c_str(), wx_static_cast(void*, m_value
));
1002 #if wxUSE_STD_IOSTREAM
1003 bool wxVariantDataWxObjectPtr::Read(wxSTD istream
& WXUNUSED(str
))
1010 bool wxVariantDataWxObjectPtr::Read(wxString
& WXUNUSED(str
))
1018 wxVariant::wxVariant( wxObject
* val
, const wxString
& name
)
1020 m_data
= new wxVariantDataWxObjectPtr(val
);
1024 bool wxVariant::operator== (wxObject
* value
) const
1026 return (value
== ((wxVariantDataWxObjectPtr
*)GetData())->GetValue());
1029 bool wxVariant::operator!= (wxObject
* value
) const
1031 return (!((*this) == (wxObject
*) value
));
1034 void wxVariant::operator= (wxObject
* value
)
1037 m_data
= new wxVariantDataWxObjectPtr(value
);
1040 wxObject
* wxVariant::GetWxObjectPtr() const
1042 wxASSERT(wxIsKindOf(GetData(), wxVariantDataWxObjectPtr
));
1043 return (wxObject
*) ((wxVariantDataWxObjectPtr
*) m_data
)->GetValue();
1046 // ----------------------------------------------------------------------------
1047 // wxVariantDataVoidPtr
1048 // ----------------------------------------------------------------------------
1050 class wxVariantDataVoidPtr
: public wxVariantData
1052 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
1054 wxVariantDataVoidPtr() { }
1055 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
1057 inline void* GetValue() const { return m_value
; }
1058 inline void SetValue(void* value
) { m_value
= value
; }
1060 virtual bool Eq(wxVariantData
& data
) const;
1061 #if wxUSE_STD_IOSTREAM
1062 virtual bool Write(wxSTD ostream
& str
) const;
1064 virtual bool Write(wxString
& str
) const;
1065 #if wxUSE_STD_IOSTREAM
1066 virtual bool Read(wxSTD istream
& str
);
1068 virtual bool Read(wxString
& str
);
1069 virtual wxString
GetType() const { return wxT("void*"); };
1070 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
1075 DECLARE_NO_COPY_CLASS(wxVariantDataVoidPtr
)
1078 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
1080 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
1082 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1084 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
1086 return (otherData
.m_value
== m_value
);
1089 #if wxUSE_STD_IOSTREAM
1090 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
1094 str
<< (const char*) s
.mb_str();
1099 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
1101 str
.Printf(wxT("%p"), m_value
);
1105 #if wxUSE_STD_IOSTREAM
1106 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
1113 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
1121 wxVariant::wxVariant( void* val
, const wxString
& name
)
1123 m_data
= new wxVariantDataVoidPtr(val
);
1127 bool wxVariant::operator== (void* value
) const
1129 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1132 bool wxVariant::operator!= (void* value
) const
1134 return (!((*this) == (void*) value
));
1137 void wxVariant::operator= (void* value
)
1139 if (GetType() == wxT("void*") &&
1140 m_data
->GetRefCount() == 1)
1142 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1147 m_data
= new wxVariantDataVoidPtr(value
);
1151 void* wxVariant::GetVoidPtr() const
1153 wxASSERT( (GetType() == wxT("void*")) );
1155 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1158 // ----------------------------------------------------------------------------
1159 // wxVariantDataDateTime
1160 // ----------------------------------------------------------------------------
1164 class wxVariantDataDateTime
: public wxVariantData
1166 DECLARE_DYNAMIC_CLASS(wxVariantDataDateTime
)
1169 wxVariantDataDateTime() { }
1170 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
1172 wxVariantDataDateTime(const TIME_STRUCT
* valptr
)
1173 { m_value
= wxDateTime(valptr
->hour
, valptr
->minute
, valptr
->second
); }
1174 wxVariantDataDateTime(const DATE_STRUCT
* valptr
)
1175 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1),valptr
->year
); }
1176 wxVariantDataDateTime(const TIMESTAMP_STRUCT
* valptr
)
1177 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1), valptr
->year
,
1178 valptr
->hour
, valptr
->minute
, valptr
->second
, (wxDateTime::wxDateTime_t
)valptr
->fraction
); }
1181 inline wxDateTime
GetValue() const { return m_value
; }
1182 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
1184 virtual bool Eq(wxVariantData
& data
) const;
1185 #if wxUSE_STD_IOSTREAM
1186 virtual bool Write(wxSTD ostream
& str
) const;
1188 virtual bool Write(wxString
& str
) const;
1189 #if wxUSE_STD_IOSTREAM
1190 virtual bool Read(wxSTD istream
& str
);
1192 virtual bool Read(wxString
& str
);
1193 virtual wxString
GetType() const { return wxT("datetime"); };
1194 virtual wxVariantData
* Clone() { return new wxVariantDataDateTime
; }
1201 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDateTime
, wxVariantData
)
1203 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
1205 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1207 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
1209 return (otherData
.m_value
== m_value
);
1213 #if wxUSE_STD_IOSTREAM
1214 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
1218 str
<< value
.c_str();
1224 bool wxVariantDataDateTime::Write(wxString
& str
) const
1226 str
= m_value
.Format();
1231 #if wxUSE_STD_IOSTREAM
1232 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
1240 bool wxVariantDataDateTime::Read(wxString
& str
)
1242 if(! m_value
.ParseDateTime(str
))
1249 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1251 m_data
= new wxVariantDataDateTime(val
);
1256 wxVariant::wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
) // Date
1258 m_data
= new wxVariantDataDateTime(valptr
);
1262 wxVariant::wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
) // Date
1264 m_data
= new wxVariantDataDateTime(valptr
);
1268 wxVariant::wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
) // Date
1270 m_data
= new wxVariantDataDateTime(valptr
);
1273 #endif // wxUSE_ODBC
1275 bool wxVariant::operator== (const wxDateTime
& value
) const
1277 wxDateTime thisValue
;
1278 if (!Convert(&thisValue
))
1281 return value
.IsEqualTo(thisValue
);
1284 bool wxVariant::operator!= (const wxDateTime
& value
) const
1286 return (!((*this) == value
));
1289 void wxVariant::operator= (const wxDateTime
& value
)
1291 if (GetType() == wxT("datetime") &&
1292 m_data
->GetRefCount() == 1)
1294 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1299 m_data
= new wxVariantDataDateTime(value
);
1304 void wxVariant::operator= (const DATE_STRUCT
* value
)
1307 m_data
= new wxVariantDataDateTime(value
);
1310 void wxVariant::operator= (const TIME_STRUCT
* value
)
1313 m_data
= new wxVariantDataDateTime(value
);
1316 void wxVariant::operator= (const TIMESTAMP_STRUCT
* value
)
1319 m_data
= new wxVariantDataDateTime(value
);
1322 #endif // wxUSE_ODBC
1324 wxDateTime
wxVariant::GetDateTime() const
1327 if (!Convert(& value
))
1329 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1335 #endif // wxUSE_DATETIME
1337 // ----------------------------------------------------------------------------
1338 // wxVariantDataArrayString
1339 // ----------------------------------------------------------------------------
1341 class wxVariantDataArrayString
: public wxVariantData
1344 wxVariantDataArrayString() { }
1345 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1347 wxArrayString
GetValue() const { return m_value
; }
1348 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1350 virtual bool Eq(wxVariantData
& data
) const;
1351 #if wxUSE_STD_IOSTREAM
1352 virtual bool Write(wxSTD ostream
& str
) const;
1354 virtual bool Write(wxString
& str
) const;
1355 #if wxUSE_STD_IOSTREAM
1356 virtual bool Read(wxSTD istream
& str
);
1358 virtual bool Read(wxString
& str
);
1359 virtual wxString
GetType() const { return wxT("arrstring"); };
1360 virtual wxVariantData
* Clone() { return new wxVariantDataArrayString
; }
1363 wxArrayString m_value
;
1365 DECLARE_DYNAMIC_CLASS(wxVariantDataArrayString
)
1368 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataArrayString
, wxVariantData
)
1370 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1372 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1374 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1376 return otherData
.m_value
== m_value
;
1379 #if wxUSE_STD_IOSTREAM
1380 bool wxVariantDataArrayString::Write(wxSTD ostream
& WXUNUSED(str
)) const
1387 bool wxVariantDataArrayString::Write(wxString
& str
) const
1389 size_t count
= m_value
.GetCount();
1390 for ( size_t n
= 0; n
< count
; n
++ )
1402 #if wxUSE_STD_IOSTREAM
1403 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1411 bool wxVariantDataArrayString::Read(wxString
& str
)
1413 wxStringTokenizer
tk(str
, _T(";"));
1414 while ( tk
.HasMoreTokens() )
1416 m_value
.Add(tk
.GetNextToken());
1424 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1426 m_data
= new wxVariantDataArrayString(val
);
1430 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1432 wxFAIL_MSG( _T("TODO") );
1437 bool wxVariant::operator!=(const wxArrayString
& value
) const
1439 return !(*this == value
);
1442 void wxVariant::operator=(const wxArrayString
& value
)
1444 if (GetType() == wxT("arrstring") &&
1445 m_data
->GetRefCount() == 1)
1447 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1452 m_data
= new wxVariantDataArrayString(value
);
1456 wxArrayString
wxVariant::GetArrayString() const
1458 if ( GetType() == wxT("arrstring") )
1459 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1461 return wxArrayString();
1464 // ----------------------------------------------------------------------------
1465 // wxVariantDataList
1466 // ----------------------------------------------------------------------------
1468 class WXDLLIMPEXP_BASE wxVariantDataList
: public wxVariantData
1470 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
1472 wxVariantDataList() {}
1473 wxVariantDataList(const wxList
& list
);
1474 virtual ~wxVariantDataList();
1476 wxList
& GetValue() { return m_value
; }
1477 void SetValue(const wxList
& value
) ;
1479 virtual bool Eq(wxVariantData
& data
) const;
1480 #if wxUSE_STD_IOSTREAM
1481 virtual bool Write(wxSTD ostream
& str
) const;
1483 virtual bool Write(wxString
& str
) const;
1484 #if wxUSE_STD_IOSTREAM
1485 virtual bool Read(wxSTD istream
& str
);
1487 virtual bool Read(wxString
& str
);
1488 virtual wxString
GetType() const { return wxT("list"); };
1496 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
1498 wxVariantDataList::wxVariantDataList(const wxList
& list
)
1503 wxVariantDataList::~wxVariantDataList()
1508 void wxVariantDataList::SetValue(const wxList
& value
)
1511 wxList::compatibility_iterator node
= value
.GetFirst();
1514 wxVariant
* var
= (wxVariant
*) node
->GetData();
1515 m_value
.Append(new wxVariant(*var
));
1516 node
= node
->GetNext();
1520 void wxVariantDataList::Clear()
1522 wxList::compatibility_iterator node
= m_value
.GetFirst();
1525 wxVariant
* var
= (wxVariant
*) node
->GetData();
1527 node
= node
->GetNext();
1532 bool wxVariantDataList::Eq(wxVariantData
& data
) const
1534 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
1536 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
1537 wxList::compatibility_iterator node1
= m_value
.GetFirst();
1538 wxList::compatibility_iterator node2
= listData
.GetValue().GetFirst();
1539 while (node1
&& node2
)
1541 wxVariant
* var1
= (wxVariant
*) node1
->GetData();
1542 wxVariant
* var2
= (wxVariant
*) node2
->GetData();
1543 if ((*var1
) != (*var2
))
1545 node1
= node1
->GetNext();
1546 node2
= node2
->GetNext();
1548 if (node1
|| node2
) return false;
1552 #if wxUSE_STD_IOSTREAM
1553 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
1557 str
<< (const char*) s
.mb_str();
1562 bool wxVariantDataList::Write(wxString
& str
) const
1564 str
= wxEmptyString
;
1565 wxList::compatibility_iterator node
= m_value
.GetFirst();
1568 wxVariant
* var
= (wxVariant
*) node
->GetData();
1569 if (node
!= m_value
.GetFirst())
1572 str
+= var
->MakeString();
1573 node
= node
->GetNext();
1579 #if wxUSE_STD_IOSTREAM
1580 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
1582 wxFAIL_MSG(wxT("Unimplemented"));
1588 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
1590 wxFAIL_MSG(wxT("Unimplemented"));
1597 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1599 m_data
= new wxVariantDataList(val
);
1603 bool wxVariant::operator== (const wxList
& value
) const
1605 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1607 wxVariantDataList
other(value
);
1608 return (GetData()->Eq(other
));
1611 bool wxVariant::operator!= (const wxList
& value
) const
1613 return (!((*this) == value
));
1616 void wxVariant::operator= (const wxList
& value
)
1618 if (GetType() == wxT("list") &&
1619 m_data
->GetRefCount() == 1)
1621 ((wxVariantDataList
*)GetData())->SetValue(value
);
1626 m_data
= new wxVariantDataList(value
);
1630 wxList
& wxVariant::GetList() const
1632 wxASSERT( (GetType() == wxT("list")) );
1634 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1638 void wxVariant::NullList()
1640 SetData(new wxVariantDataList());
1644 void wxVariant::Append(const wxVariant
& value
)
1646 wxList
& list
= GetList();
1648 list
.Append(new wxVariant(value
));
1651 // Insert at front of list
1652 void wxVariant::Insert(const wxVariant
& value
)
1654 wxList
& list
= GetList();
1656 list
.Insert(new wxVariant(value
));
1659 // Returns true if the variant is a member of the list
1660 bool wxVariant::Member(const wxVariant
& value
) const
1662 wxList
& list
= GetList();
1664 wxList::compatibility_iterator node
= list
.GetFirst();
1667 wxVariant
* other
= (wxVariant
*) node
->GetData();
1668 if (value
== *other
)
1670 node
= node
->GetNext();
1675 // Deletes the nth element of the list
1676 bool wxVariant::Delete(size_t item
)
1678 wxList
& list
= GetList();
1680 wxASSERT_MSG( (item
< list
.GetCount()), wxT("Invalid index to Delete") );
1681 wxList::compatibility_iterator node
= list
.Item(item
);
1682 wxVariant
* variant
= (wxVariant
*) node
->GetData();
1689 void wxVariant::ClearList()
1691 if (!IsNull() && (GetType() == wxT("list")))
1693 ((wxVariantDataList
*) m_data
)->Clear();
1697 if (!GetType().IsSameAs(wxT("list")))
1700 m_data
= new wxVariantDataList
;
1704 // Treat a list variant as an array
1705 wxVariant
wxVariant::operator[] (size_t idx
) const
1707 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
1709 if (GetType() == wxT("list"))
1711 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1712 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1713 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1715 return wxNullVariant
;
1718 wxVariant
& wxVariant::operator[] (size_t idx
)
1720 // We can't return a reference to a variant for a string list, since the string
1721 // is actually stored as a char*, not a variant.
1723 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1725 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1726 wxASSERT_MSG( (idx
< data
->GetValue().GetCount()), wxT("Invalid index for array") );
1728 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1731 // Return the number of elements in a list
1732 size_t wxVariant::GetCount() const
1734 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
1736 if (GetType() == wxT("list"))
1738 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1739 return data
->GetValue().GetCount();
1744 // ----------------------------------------------------------------------------
1746 // ----------------------------------------------------------------------------
1748 bool wxVariant::Convert(long* value
) const
1750 wxString
type(GetType());
1751 if (type
== wxT("double"))
1752 *value
= (long) (((wxVariantDoubleData
*)GetData())->GetValue());
1753 else if (type
== wxT("long"))
1754 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1756 else if (type
== wxT("bool"))
1757 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1759 else if (type
== wxT("string"))
1760 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1767 bool wxVariant::Convert(bool* value
) const
1769 wxString
type(GetType());
1770 if (type
== wxT("double"))
1771 *value
= ((int) (((wxVariantDoubleData
*)GetData())->GetValue()) != 0);
1772 else if (type
== wxT("long"))
1773 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1775 else if (type
== wxT("bool"))
1776 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1778 else if (type
== wxT("string"))
1780 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1782 if (val
== wxT("true") || val
== wxT("yes") || val
== wxT('1') )
1784 else if (val
== wxT("false") || val
== wxT("no") || val
== wxT('0') )
1795 bool wxVariant::Convert(double* value
) const
1797 wxString
type(GetType());
1798 if (type
== wxT("double"))
1799 *value
= ((wxVariantDoubleData
*)GetData())->GetValue();
1800 else if (type
== wxT("long"))
1801 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1803 else if (type
== wxT("bool"))
1804 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1806 else if (type
== wxT("string"))
1807 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1814 bool wxVariant::Convert(wxChar
* value
) const
1816 wxString
type(GetType());
1817 if (type
== wxT("char"))
1818 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1819 else if (type
== wxT("long"))
1820 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1822 else if (type
== wxT("bool"))
1823 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1831 bool wxVariant::Convert(wxString
* value
) const
1833 *value
= MakeString();
1838 bool wxVariant::Convert(wxDateTime
* value
) const
1840 wxString
type(GetType());
1841 if (type
== wxT("datetime"))
1843 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
1846 // Fallback to string conversion
1848 return Convert(&val
) &&
1849 (value
->ParseDateTime(val
) || value
->ParseDate(val
) || value
->ParseTime(val
));
1851 #endif // wxUSE_DATETIME
1853 #endif // wxUSE_VARIANT