1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "variant.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/stream.h"
30 #include "wx/string.h"
31 #include "wx/variant.h"
33 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
35 wxVariant WXDLLEXPORT wxNullVariant
;
41 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
43 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
45 wxVariantDataList() {}
46 wxVariantDataList(const wxList
& list
);
49 wxList
& GetValue() const { return (wxList
&) m_value
; }
50 void SetValue(const wxList
& value
) ;
52 virtual void Copy(wxVariantData
& data
);
53 virtual bool Eq(wxVariantData
& data
) const;
54 virtual bool Write(ostream
& str
) const;
55 virtual bool Write(wxString
& str
) const;
56 virtual bool Read(istream
& str
);
57 virtual bool Read(wxString
& str
);
58 virtual wxString
GetType() const { return "list"; };
66 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
68 wxVariantDataList::wxVariantDataList(const wxList
& list
)
73 wxVariantDataList::~wxVariantDataList()
78 void wxVariantDataList::SetValue(const wxList
& value
)
81 wxNode
* node
= value
.First();
84 wxVariant
* var
= (wxVariant
*) node
->Data();
85 m_value
.Append(new wxVariant(*var
));
90 void wxVariantDataList::Clear()
92 wxNode
* node
= m_value
.First();
95 wxVariant
* var
= (wxVariant
*) node
->Data();
102 void wxVariantDataList::Copy(wxVariantData
& data
)
104 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Copy: Can't copy to this type of data" );
106 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
109 wxNode
* node
= m_value
.First();
112 wxVariant
* var
= (wxVariant
*) node
->Data();
113 listData
.m_value
.Append(new wxVariant(*var
));
118 bool wxVariantDataList::Eq(wxVariantData
& data
) const
120 wxASSERT_MSG( (data
.GetType() == "list"), "wxVariantDataList::Eq: argument mismatch" );
122 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
123 wxNode
* node1
= m_value
.First();
124 wxNode
* node2
= listData
.GetValue().First();
125 while (node1
&& node2
)
127 wxVariant
* var1
= (wxVariant
*) node1
->Data();
128 wxVariant
* var2
= (wxVariant
*) node2
->Data();
129 if ((*var1
) != (*var2
))
131 node1
= node1
->Next();
132 node2
= node2
->Next();
134 if (node1
|| node2
) return FALSE
;
138 bool wxVariantDataList::Write(ostream
& str
) const
142 str
<< (const char*) s
;
146 bool wxVariantDataList::Write(wxString
& str
) const
149 wxNode
* node
= m_value
.First();
152 wxVariant
* var
= (wxVariant
*) node
->Data();
153 if (node
!= m_value
.First())
156 str
+= var
->MakeString();
163 bool wxVariantDataList::Read(istream
& WXUNUSED(str
))
165 wxFAIL_MSG("Unimplemented");
170 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
172 wxFAIL_MSG("Unimplemented");
178 * wxVariantDataStringList
181 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
183 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
185 wxVariantDataStringList() {}
186 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
188 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
189 void SetValue(const wxStringList
& value
);
191 virtual void Copy(wxVariantData
& data
);
192 virtual bool Eq(wxVariantData
& data
) const;
193 virtual bool Write(ostream
& str
) const;
194 virtual bool Write(wxString
& str
) const;
195 virtual bool Read(istream
& str
);
196 virtual bool Read(wxString
& str
);
197 virtual wxString
GetType() const { return "stringlist"; };
200 wxStringList m_value
;
203 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
205 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
210 void wxVariantDataStringList::Copy(wxVariantData
& data
)
212 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Copy: Can't copy to this type of data" );
214 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
216 listData
.m_value
= m_value
;
219 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
221 wxASSERT_MSG( (data
.GetType() == "stringlist"), "wxVariantDataStringList::Eq: argument mismatch" );
223 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
224 wxNode
* node1
= m_value
.First();
225 wxNode
* node2
= listData
.GetValue().First();
226 while (node1
&& node2
)
228 wxString
str1 ((char*) node1
->Data());
229 wxString
str2 ((char*) node2
->Data());
232 node1
= node1
->Next();
233 node2
= node2
->Next();
235 if (node1
|| node2
) return FALSE
;
239 bool wxVariantDataStringList::Write(ostream
& str
) const
243 str
<< (const char*) s
;
247 bool wxVariantDataStringList::Write(wxString
& str
) const
250 wxNode
* node
= m_value
.First();
253 char* s
= (char*) node
->Data();
254 if (node
!= m_value
.First())
263 bool wxVariantDataStringList::Read(istream
& WXUNUSED(str
))
265 wxFAIL_MSG("Unimplemented");
270 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
272 wxFAIL_MSG("Unimplemented");
281 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
283 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
285 wxVariantDataLong() { m_value
= 0; }
286 wxVariantDataLong(long value
) { m_value
= value
; }
288 inline long GetValue() const { return m_value
; }
289 inline void SetValue(long value
) { m_value
= value
; }
291 virtual void Copy(wxVariantData
& data
);
292 virtual bool Eq(wxVariantData
& data
) const;
294 virtual bool Read(wxString
& str
);
295 virtual bool Write(wxString
& str
) const;
296 virtual bool Read(istream
& str
);
297 virtual bool Write(ostream
& str
) const;
300 virtual bool Read(wxInputStream
& str
);
301 virtual bool Write(wxOutputStream
&str
) const;
302 #endif // wxUSE_STREAMS
304 virtual wxString
GetType() const { return "long"; };
310 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
312 void wxVariantDataLong::Copy(wxVariantData
& data
)
314 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Copy: Can't copy to this type of data" );
316 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
318 otherData
.m_value
= m_value
;
321 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
323 wxASSERT_MSG( (data
.GetType() == "long"), "wxVariantDataLong::Eq: argument mismatch" );
325 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
327 return (otherData
.m_value
== m_value
);
330 bool wxVariantDataLong::Write(ostream
& str
) const
334 str
<< (const char*) s
;
338 bool wxVariantDataLong::Write(wxString
& str
) const
340 str
.Printf("%ld", m_value
);
344 bool wxVariantDataLong::Read(istream
& str
)
351 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
357 bool wxVariantDataLong::Read(wxInputStream
& str
)
362 #endif // wxUSE_STREAMS
364 bool wxVariantDataLong::Read(wxString
& str
)
366 m_value
= atol((const char*) str
);
374 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
376 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
378 wxVariantDataReal() { m_value
= 0.0; }
379 wxVariantDataReal(double value
) { m_value
= value
; }
381 inline double GetValue() const { return m_value
; }
382 inline void SetValue(double value
) { m_value
= value
; }
384 virtual void Copy(wxVariantData
& data
);
385 virtual bool Eq(wxVariantData
& data
) const;
386 virtual bool Read(wxString
& str
);
387 virtual bool Write(ostream
& str
) const;
388 virtual bool Write(wxString
& str
) const;
389 virtual bool Read(istream
& str
);
391 virtual bool Read(wxInputStream
& str
);
392 virtual bool Write(wxOutputStream
&str
) const;
393 #endif // wxUSE_STREAMS
394 virtual wxString
GetType() const { return "double"; };
400 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
402 void wxVariantDataReal::Copy(wxVariantData
& data
)
404 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Copy: Can't copy to this type of data" );
406 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
408 otherData
.m_value
= m_value
;
411 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
413 wxASSERT_MSG( (data
.GetType() == "double"), "wxVariantDataReal::Eq: argument mismatch" );
415 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
417 return (otherData
.m_value
== m_value
);
420 bool wxVariantDataReal::Write(ostream
& str
) const
424 str
<< (const char*) s
;
428 bool wxVariantDataReal::Write(wxString
& str
) const
430 str
.Printf("%.4f", m_value
);
434 bool wxVariantDataReal::Read(istream
& str
)
441 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
447 bool wxVariantDataReal::Read(wxInputStream
& str
)
449 str
>> (float&)m_value
;
452 #endif // wxUSE_STREAMS
454 bool wxVariantDataReal::Read(wxString
& str
)
456 m_value
= atof((const char*) str
);
464 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
466 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
468 wxVariantDataBool() { m_value
= 0; }
469 wxVariantDataBool(bool value
) { m_value
= value
; }
471 inline bool GetValue() const { return m_value
; }
472 inline void SetValue(bool value
) { m_value
= value
; }
474 virtual void Copy(wxVariantData
& data
);
475 virtual bool Eq(wxVariantData
& data
) const;
476 virtual bool Write(ostream
& str
) const;
477 virtual bool Write(wxString
& str
) const;
478 virtual bool Read(wxString
& str
);
479 virtual bool Read(istream
& str
);
481 virtual bool Read(wxInputStream
& str
);
482 virtual bool Write(wxOutputStream
& str
) const;
483 #endif // wxUSE_STREAMS
484 virtual wxString
GetType() const { return "bool"; };
490 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
492 void wxVariantDataBool::Copy(wxVariantData
& data
)
494 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Copy: Can't copy to this type of data" );
496 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
498 otherData
.m_value
= m_value
;
501 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
503 wxASSERT_MSG( (data
.GetType() == "bool"), "wxVariantDataBool::Eq: argument mismatch" );
505 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
507 return (otherData
.m_value
== m_value
);
510 bool wxVariantDataBool::Write(ostream
& str
) const
514 str
<< (const char*) s
;
518 bool wxVariantDataBool::Write(wxString
& str
) const
520 str
.Printf("%d", (int) m_value
);
524 bool wxVariantDataBool::Read(istream
& WXUNUSED(str
))
526 wxFAIL_MSG("Unimplemented");
527 // str >> (long) m_value;
532 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
534 str
<< (char)m_value
;
538 bool wxVariantDataBool::Read(wxInputStream
& str
)
540 str
>> (char&)m_value
;
543 #endif // wxUSE_STREAMS
545 bool wxVariantDataBool::Read(wxString
& str
)
547 m_value
= (atol((const char*) str
) != 0);
555 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
557 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
559 wxVariantDataChar() { m_value
= 0; }
560 wxVariantDataChar(char value
) { m_value
= value
; }
562 inline char GetValue() const { return m_value
; }
563 inline void SetValue(char value
) { m_value
= value
; }
565 virtual void Copy(wxVariantData
& data
);
566 virtual bool Eq(wxVariantData
& data
) const;
567 virtual bool Read(istream
& str
);
568 virtual bool Write(ostream
& str
) const;
569 virtual bool Read(wxString
& str
);
570 virtual bool Write(wxString
& str
) const;
572 virtual bool Read(wxInputStream
& str
);
573 virtual bool Write(wxOutputStream
& str
) const;
574 #endif // wxUSE_STREAMS
575 virtual wxString
GetType() const { return "char"; };
581 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
583 void wxVariantDataChar::Copy(wxVariantData
& data
)
585 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Copy: Can't copy to this type of data" );
587 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
589 otherData
.m_value
= m_value
;
592 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
594 wxASSERT_MSG( (data
.GetType() == "char"), "wxVariantDataChar::Eq: argument mismatch" );
596 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
598 return (otherData
.m_value
== m_value
);
601 bool wxVariantDataChar::Write(ostream
& str
) const
605 str
<< (const char*) s
;
609 bool wxVariantDataChar::Write(wxString
& str
) const
611 str
.Printf("%c", m_value
);
615 bool wxVariantDataChar::Read(istream
& WXUNUSED(str
))
617 wxFAIL_MSG("Unimplemented");
623 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
629 bool wxVariantDataChar::Read(wxInputStream
& str
)
634 #endif // wxUSE_STREAMS
636 bool wxVariantDataChar::Read(wxString
& str
)
638 m_value
= str
[(size_t)0];
643 * wxVariantDataString
646 #if defined(__BORLANDC__) && defined(__WIN16__)
647 // Change name because of truncation
648 #define wxVariantDataString wxVariantStringData
651 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
653 #if defined(__BORLANDC__) && defined(__WIN16__)
654 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
656 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
659 wxVariantDataString() { }
660 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
662 inline wxString
GetValue() const { return m_value
; }
663 inline void SetValue(const wxString
& value
) { m_value
= value
; }
665 virtual void Copy(wxVariantData
& data
);
666 virtual bool Eq(wxVariantData
& data
) const;
667 virtual bool Write(ostream
& str
) const;
668 virtual bool Read(wxString
& str
);
669 virtual bool Write(wxString
& str
) const;
670 virtual bool Read(istream
& str
);
672 virtual bool Read(wxInputStream
& str
);
673 virtual bool Write(wxOutputStream
& str
) const;
674 #endif // wxUSE_STREAMS
675 virtual wxString
GetType() const { return "string"; };
681 void wxVariantDataString::Copy(wxVariantData
& data
)
683 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Copy: Can't copy to this type of data" );
685 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
687 otherData
.m_value
= m_value
;
690 bool wxVariantDataString::Eq(wxVariantData
& data
) const
692 wxASSERT_MSG( (data
.GetType() == "string"), "wxVariantDataString::Eq: argument mismatch" );
694 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
696 return (otherData
.m_value
== m_value
);
699 bool wxVariantDataString::Write(ostream
& str
) const
701 str
<< (const char*) m_value
;
705 bool wxVariantDataString::Write(wxString
& str
) const
711 bool wxVariantDataString::Read(istream
& str
)
718 bool wxVariantDataString::Write(wxOutputStream
& str
) const
720 str
<< (const char*) m_value
;
724 bool wxVariantDataString::Read(wxInputStream
& str
)
729 #endif // wxUSE_STREAMS
731 bool wxVariantDataString::Read(wxString
& str
)
737 #if defined(__BORLANDC__) && defined(__WIN16__)
738 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
740 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
749 class wxVariantDataTime
: public wxVariantData
751 DECLARE_DYNAMIC_CLASS(wxVariantDataTime
)
753 wxVariantDataTime() { }
754 wxVariantDataTime(const wxTime
& value
) { m_value
= value
; }
756 inline wxTime
GetValue() const { return m_value
; }
757 inline void SetValue(const wxTime
& value
) { m_value
= value
; }
759 virtual void Copy(wxVariantData
& data
);
760 virtual bool Eq(wxVariantData
& data
) const;
761 virtual bool Write(ostream
& str
) const;
762 virtual bool Write(wxString
& str
) const;
763 virtual bool Read(istream
& str
);
764 virtual bool Read(wxString
& str
);
765 virtual wxString
GetType() const { return "time"; };
766 virtual wxVariantData
* Clone() { return new wxVariantDataTime
; }
772 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime
, wxVariantData
)
774 void wxVariantDataTime::Copy(wxVariantData
& data
)
776 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Copy: Can't copy to this type of data" );
778 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
780 otherData
.m_value
= m_value
;
783 bool wxVariantDataTime::Eq(wxVariantData
& data
) const
785 wxASSERT_MSG( (data
.GetType() == "time"), "wxVariantDataTime::Eq: argument mismatch" );
787 wxVariantDataTime
& otherData
= (wxVariantDataTime
&) data
;
789 return (otherData
.m_value
== m_value
);
792 bool wxVariantDataTime::Write(ostream
& str
) const
796 str
<< (const char*) s
;
800 bool wxVariantDataTime::Write(wxString
& str
) const
802 char*s
= m_value
.FormatTime();
807 bool wxVariantDataTime::Read(istream
& WXUNUSED(str
))
813 bool wxVariantDataTime::Read(wxString
& WXUNUSED(str
))
823 class wxVariantDataDate
: public wxVariantData
825 DECLARE_DYNAMIC_CLASS(wxVariantDataDate
)
827 wxVariantDataDate() { }
828 wxVariantDataDate(const wxDate
& value
) { m_value
= value
; }
830 inline wxDate
GetValue() const { return m_value
; }
831 inline void SetValue(const wxDate
& value
) { m_value
= value
; }
833 virtual void Copy(wxVariantData
& data
);
834 virtual bool Eq(wxVariantData
& data
) const;
835 virtual bool Write(ostream
& str
) const;
836 virtual bool Write(wxString
& str
) const;
837 virtual bool Read(istream
& str
);
838 virtual bool Read(wxString
& str
);
839 virtual wxString
GetType() const { return "date"; };
840 virtual wxVariantData
* Clone() { return new wxVariantDataDate
; }
846 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate
, wxVariantData
)
848 void wxVariantDataDate::Copy(wxVariantData
& data
)
850 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Copy: Can't copy to this type of data" );
852 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
854 otherData
.m_value
= m_value
;
857 bool wxVariantDataDate::Eq(wxVariantData
& data
) const
859 wxASSERT_MSG( (data
.GetType() == "date"), "wxVariantDataDate::Eq: argument mismatch" );
861 wxVariantDataDate
& otherData
= (wxVariantDataDate
&) data
;
863 return (otherData
.m_value
== m_value
);
866 bool wxVariantDataDate::Write(ostream
& str
) const
870 str
<< (const char*) s
;
874 bool wxVariantDataDate::Write(wxString
& str
) const
876 str
= m_value
.FormatDate();
880 bool wxVariantDataDate::Read(istream
& WXUNUSED(str
))
886 bool wxVariantDataDate::Read(wxString
& WXUNUSED(str
))
895 * wxVariantDataVoidPtr
898 class wxVariantDataVoidPtr
: public wxVariantData
900 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
902 wxVariantDataVoidPtr() { }
903 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
905 inline void* GetValue() const { return m_value
; }
906 inline void SetValue(void* value
) { m_value
= value
; }
908 virtual void Copy(wxVariantData
& data
);
909 virtual bool Eq(wxVariantData
& data
) const;
910 virtual bool Write(ostream
& str
) const;
911 virtual bool Write(wxString
& str
) const;
912 virtual bool Read(istream
& str
);
913 virtual bool Read(wxString
& str
);
914 virtual wxString
GetType() const { return "void*"; };
915 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
921 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
923 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
925 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Copy: Can't copy to this type of data" );
927 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
929 otherData
.m_value
= m_value
;
932 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
934 wxASSERT_MSG( (data
.GetType() == "void*"), "wxVariantDataVoidPtr::Eq: argument mismatch" );
936 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
938 return (otherData
.m_value
== m_value
);
941 bool wxVariantDataVoidPtr::Write(ostream
& str
) const
945 str
<< (const char*) s
;
949 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
952 sprintf(buf
, "%ld", (long) m_value
);
957 bool wxVariantDataVoidPtr::Read(istream
& WXUNUSED(str
))
963 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
974 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
976 // Construction & destruction
977 wxVariant::wxVariant()
979 m_data
= (wxVariantData
*) NULL
;
982 wxVariant::wxVariant(double val
, const wxString
& name
)
984 m_data
= new wxVariantDataReal(val
);
988 wxVariant::wxVariant(long val
, const wxString
& name
)
990 m_data
= new wxVariantDataLong(val
);
994 wxVariant::wxVariant(bool val
, const wxString
& name
)
996 m_data
= new wxVariantDataBool(val
);
1000 wxVariant::wxVariant(char val
, const wxString
& name
)
1002 m_data
= new wxVariantDataChar(val
);
1006 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1008 m_data
= new wxVariantDataString(val
);
1012 wxVariant::wxVariant(const char* val
, const wxString
& name
)
1014 m_data
= new wxVariantDataString(wxString(val
));
1018 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1020 m_data
= new wxVariantDataStringList(val
);
1024 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1026 m_data
= new wxVariantDataList(val
);
1031 wxVariant::wxVariant(const wxTime
& val
, const wxString
& name
) // Time
1033 m_data
= new wxVariantDataTime(val
);
1037 wxVariant::wxVariant(const wxDate
& val
, const wxString
& name
) // Date
1039 m_data
= new wxVariantDataDate(val
);
1044 wxVariant::wxVariant(void* val
, const wxString
& name
) // Void ptr
1046 m_data
= new wxVariantDataVoidPtr(val
);
1050 wxVariant::wxVariant(const wxVariant
& variant
)
1052 if (!variant
.IsNull())
1054 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1055 variant
.m_data
->Copy(*m_data
);
1058 m_data
= (wxVariantData
*) NULL
;
1059 m_name
= variant
.m_name
;
1062 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1068 wxVariant::~wxVariant()
1074 // Make NULL (i.e. delete the data)
1075 void wxVariant::MakeNull()
1081 // Generic operators
1083 void wxVariant::operator= (const wxVariant
& variant
)
1085 if (variant
.IsNull())
1091 if (IsNull() || (GetType() != variant
.GetType()))
1095 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1097 // GetData()->Copy(* variant.GetData());
1098 variant
.GetData()->Copy(* GetData());
1101 // Assignment using data, e.g.
1102 // myVariant = new wxStringVariantData("hello")
1103 void wxVariant::operator= (wxVariantData
* variantData
)
1106 m_data
= variantData
;
1109 bool wxVariant::operator== (const wxVariant
& variant
) const
1111 if (IsNull() || variant
.IsNull())
1112 return (IsNull() == variant
.IsNull());
1114 return (GetData()->Eq(* variant
.GetData()));
1117 bool wxVariant::operator!= (const wxVariant
& variant
) const
1119 return (!(*this == variant
));
1123 // Specific operators
1124 bool wxVariant::operator== (double value
) const
1127 if (!Convert(&thisValue
))
1130 return (value
== thisValue
);
1133 bool wxVariant::operator!= (double value
) const
1135 return (!((*this) == value
));
1138 void wxVariant::operator= (double value
)
1140 if (GetType() == "double")
1142 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1148 m_data
= new wxVariantDataReal(value
);
1152 bool wxVariant::operator== (long value
) const
1155 if (!Convert(&thisValue
))
1158 return (value
== thisValue
);
1161 bool wxVariant::operator!= (long value
) const
1163 return (!((*this) == value
));
1166 void wxVariant::operator= (long value
)
1168 if (GetType() == "long")
1170 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1176 m_data
= new wxVariantDataLong(value
);
1180 bool wxVariant::operator== (char value
) const
1183 if (!Convert(&thisValue
))
1186 return (value
== thisValue
);
1189 bool wxVariant::operator!= (char value
) const
1191 return (!((*this) == value
));
1194 void wxVariant::operator= (char value
)
1196 if (GetType() == "char")
1198 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1204 m_data
= new wxVariantDataChar(value
);
1208 bool wxVariant::operator== (bool value
) const
1211 if (!Convert(&thisValue
))
1214 return (value
== thisValue
);
1217 bool wxVariant::operator!= (bool value
) const
1219 return (!((*this) == value
));
1222 void wxVariant::operator= (bool value
)
1224 if (GetType() == "bool")
1226 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1232 m_data
= new wxVariantDataBool(value
);
1236 bool wxVariant::operator== (const wxString
& value
) const
1239 if (!Convert(&thisValue
))
1242 return (value
== thisValue
);
1245 bool wxVariant::operator!= (const wxString
& value
) const
1247 return (!((*this) == value
));
1250 void wxVariant::operator= (const wxString
& value
)
1252 if (GetType() == "string")
1254 ((wxVariantDataString
*)GetData())->SetValue(value
);
1260 m_data
= new wxVariantDataString(value
);
1264 void wxVariant::operator= (const char* value
)
1266 if (GetType() == "string")
1268 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1274 m_data
= new wxVariantDataString(wxString(value
));
1278 bool wxVariant::operator== (const wxStringList
& value
) const
1280 wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" );
1282 wxVariantDataStringList
other(value
);
1283 return (m_data
->Eq(other
));
1286 bool wxVariant::operator!= (const wxStringList
& value
) const
1288 return (!((*this) == value
));
1291 void wxVariant::operator= (const wxStringList
& value
)
1293 if (GetType() == "stringlist")
1295 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1301 m_data
= new wxVariantDataStringList(value
);
1305 bool wxVariant::operator== (const wxList
& value
) const
1307 wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" );
1309 wxVariantDataList
other(value
);
1310 return (m_data
->Eq(other
));
1313 bool wxVariant::operator!= (const wxList
& value
) const
1315 return (!((*this) == value
));
1318 void wxVariant::operator= (const wxList
& value
)
1320 if (GetType() == "list")
1322 ((wxVariantDataList
*)GetData())->SetValue(value
);
1328 m_data
= new wxVariantDataList(value
);
1333 bool wxVariant::operator== (const wxTime
& value
) const
1336 if (!Convert(&thisValue
))
1339 return (value
== thisValue
);
1342 bool wxVariant::operator!= (const wxTime
& value
) const
1344 return (!((*this) == value
));
1347 void wxVariant::operator= (const wxTime
& value
)
1349 if (GetType() == "time")
1351 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1357 m_data
= new wxVariantDataTime(value
);
1361 bool wxVariant::operator== (const wxDate
& value
) const
1364 if (!Convert(&thisValue
))
1367 return (value
== thisValue
);
1370 bool wxVariant::operator!= (const wxDate
& value
) const
1372 return (!((*this) == value
));
1375 void wxVariant::operator= (const wxDate
& value
)
1377 if (GetType() == "date")
1379 ((wxVariantDataTime
*)GetData())->SetValue(value
);
1385 m_data
= new wxVariantDataDate(value
);
1390 bool wxVariant::operator== (void* value
) const
1392 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1395 bool wxVariant::operator!= (void* value
) const
1397 return (!((*this) == value
));
1400 void wxVariant::operator= (void* value
)
1402 if (GetType() == "void*")
1404 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1410 m_data
= new wxVariantDataVoidPtr(value
);
1414 // Treat a list variant as an array
1415 wxVariant
wxVariant::operator[] (size_t idx
) const
1417 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" );
1419 if (GetType() == "list")
1421 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1422 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1423 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1425 else if (GetType() == "stringlist")
1427 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1428 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1430 wxVariant
variant( wxString( (char*) (data
->GetValue().Nth(idx
)->Data()) ));
1433 return wxNullVariant
;
1436 wxVariant
& wxVariant::operator[] (size_t idx
)
1438 // We can't return a reference to a variant for a string list, since the string
1439 // is actually stored as a char*, not a variant.
1441 wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" );
1443 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1444 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().Number()), "Invalid index for array" );
1446 return * (wxVariant
*) (data
->GetValue().Nth(idx
)->Data());
1449 // Return the number of elements in a list
1450 int wxVariant::GetCount() const
1452 wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" );
1454 if (GetType() == "list")
1456 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1457 return data
->GetValue().Number();
1459 else if (GetType() == "stringlist")
1461 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1462 return data
->GetValue().Number();
1467 wxString
wxVariant::MakeString() const
1472 if (GetData()->Write(str
))
1475 return wxString("");
1480 void wxVariant::SetData(wxVariantData
* data
)
1482 if (m_data
) delete m_data
;
1487 // Returns a string representing the type of the variant,
1488 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1489 wxString
wxVariant::GetType() const
1492 return wxString("null");
1494 return m_data
->GetType();
1498 bool wxVariant::IsType(const wxString
& type
) const
1500 return (GetType() == type
);
1505 double wxVariant::GetReal() const
1508 if (Convert(& value
))
1512 wxFAIL_MSG("Could not convert to a real number");
1517 long wxVariant::GetInteger() const
1520 if (Convert(& value
))
1524 wxFAIL_MSG("Could not convert to an integer");
1529 char wxVariant::GetChar() const
1532 if (Convert(& value
))
1536 wxFAIL_MSG("Could not convert to a char");
1541 bool wxVariant::GetBool() const
1544 if (Convert(& value
))
1548 wxFAIL_MSG("Could not convert to a bool");
1553 wxString
wxVariant::GetString() const
1556 if (Convert(& value
))
1560 wxFAIL_MSG("Could not convert to a string");
1561 return wxString("");
1566 wxTime
wxVariant::GetTime() const
1569 if (Convert(& value
))
1573 wxFAIL_MSG("Could not convert to a time");
1578 wxDate
wxVariant::GetDate() const
1581 if (Convert(& value
))
1585 wxFAIL_MSG("Could not convert to a date");
1591 void* wxVariant::GetVoidPtr() const
1593 wxASSERT( (GetType() == "void*") );
1595 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1598 wxList
& wxVariant::GetList() const
1600 wxASSERT( (GetType() == "list") );
1602 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1605 wxStringList
& wxVariant::GetStringList() const
1607 wxASSERT( (GetType() == "stringlist") );
1609 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1613 void wxVariant::Append(const wxVariant
& value
)
1615 wxList
& list
= GetList();
1617 list
.Append(new wxVariant(value
));
1620 // Insert at front of list
1621 void wxVariant::Insert(const wxVariant
& value
)
1623 wxList
& list
= GetList();
1625 list
.Insert(new wxVariant(value
));
1628 // Returns TRUE if the variant is a member of the list
1629 bool wxVariant::Member(const wxVariant
& value
) const
1631 wxList
& list
= GetList();
1633 wxNode
* node
= list
.First();
1636 wxVariant
* other
= (wxVariant
*) node
->Data();
1637 if (value
== *other
)
1639 node
= node
->Next();
1644 // Deletes the nth element of the list
1645 bool wxVariant::Delete(int item
)
1647 wxList
& list
= GetList();
1649 wxASSERT_MSG( (item
< list
.Number()), "Invalid index to Delete" );
1650 wxNode
* node
= list
.Nth(item
);
1651 wxVariant
* variant
= (wxVariant
*) node
->Data();
1658 void wxVariant::ClearList()
1660 if (!IsNull() && (GetType() == "list"))
1662 ((wxVariantDataList
*) m_data
)->Clear();
1666 if (GetType() != "list")
1671 m_data
= new wxVariantDataList
;
1676 bool wxVariant::Convert(long* value
) const
1678 wxString
type(GetType());
1679 if (type
== "double")
1680 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1681 else if (type
== "long")
1682 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1683 else if (type
== "bool")
1684 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1685 else if (type
== "string")
1686 *value
= atol((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1693 bool wxVariant::Convert(bool* value
) const
1695 wxString
type(GetType());
1696 if (type
== "double")
1697 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1698 else if (type
== "long")
1699 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1700 else if (type
== "bool")
1701 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1702 else if (type
== "string")
1704 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1706 if (val
== "true" || val
== "yes")
1708 else if (val
== "false" || val
== "no")
1719 bool wxVariant::Convert(double* value
) const
1721 wxString
type(GetType());
1722 if (type
== "double")
1723 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1724 else if (type
== "long")
1725 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1726 else if (type
== "bool")
1727 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1728 else if (type
== "string")
1729 *value
= (double) atof((const char*) ((wxVariantDataString
*)GetData())->GetValue());
1736 bool wxVariant::Convert(char* value
) const
1738 wxString
type(GetType());
1740 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1741 else if (type
== "long")
1742 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1743 else if (type
== "bool")
1744 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1751 bool wxVariant::Convert(wxString
* value
) const
1753 *value
= MakeString();
1758 bool wxVariant::Convert(wxTime
* value
) const
1760 wxString
type(GetType());
1762 *value
= ((wxVariantDataTime
*)GetData())->GetValue();
1763 else if (type
== "date")
1764 *value
= wxTime(((wxVariantDataDate
*)GetData())->GetValue());
1771 bool wxVariant::Convert(wxDate
* value
) const
1773 wxString
type(GetType());
1775 *value
= ((wxVariantDataDate
*)GetData())->GetValue();