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"
23 #if wxUSE_STD_IOSTREAM
31 #if defined(__MWERKS__) && __MSL__ >= 0x6000
36 #include "wx/stream.h"
37 #include "wx/txtstrm.h"
40 #include "wx/string.h"
41 #include "wx/tokenzr.h"
43 #include "wx/variant.h"
45 IMPLEMENT_ABSTRACT_CLASS(wxVariantData
, wxObject
)
47 wxVariant WXDLLEXPORT wxNullVariant
;
53 class WXDLLEXPORT wxVariantDataList
: public wxVariantData
55 DECLARE_DYNAMIC_CLASS(wxVariantDataList
)
57 wxVariantDataList() {}
58 wxVariantDataList(const wxList
& list
);
61 wxList
& GetValue() const { return (wxList
&) m_value
; }
62 void SetValue(const wxList
& value
) ;
64 virtual void Copy(wxVariantData
& data
);
65 virtual bool Eq(wxVariantData
& data
) const;
66 #if wxUSE_STD_IOSTREAM
67 virtual bool Write(wxSTD ostream
& str
) const;
69 virtual bool Write(wxString
& str
) const;
70 #if wxUSE_STD_IOSTREAM
71 virtual bool Read(wxSTD istream
& str
);
73 virtual bool Read(wxString
& str
);
74 virtual wxString
GetType() const { return wxT("list"); };
82 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList
, wxVariantData
)
84 wxVariantDataList::wxVariantDataList(const wxList
& list
)
89 wxVariantDataList::~wxVariantDataList()
94 void wxVariantDataList::SetValue(const wxList
& value
)
97 wxNode
* node
= value
.GetFirst();
100 wxVariant
* var
= (wxVariant
*) node
->GetData();
101 m_value
.Append(new wxVariant(*var
));
102 node
= node
->GetNext();
106 void wxVariantDataList::Clear()
108 wxNode
* node
= m_value
.GetFirst();
111 wxVariant
* var
= (wxVariant
*) node
->GetData();
113 node
= node
->GetNext();
118 void wxVariantDataList::Copy(wxVariantData
& data
)
120 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") );
122 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
125 wxNode
* node
= m_value
.GetFirst();
128 wxVariant
* var
= (wxVariant
*) node
->GetData();
129 listData
.m_value
.Append(new wxVariant(*var
));
130 node
= node
->GetNext();
134 bool wxVariantDataList::Eq(wxVariantData
& data
) const
136 wxASSERT_MSG( (data
.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
138 wxVariantDataList
& listData
= (wxVariantDataList
&) data
;
139 wxNode
* node1
= m_value
.GetFirst();
140 wxNode
* node2
= listData
.GetValue().GetFirst();
141 while (node1
&& node2
)
143 wxVariant
* var1
= (wxVariant
*) node1
->GetData();
144 wxVariant
* var2
= (wxVariant
*) node2
->GetData();
145 if ((*var1
) != (*var2
))
147 node1
= node1
->GetNext();
148 node2
= node2
->GetNext();
150 if (node1
|| node2
) return FALSE
;
154 #if wxUSE_STD_IOSTREAM
155 bool wxVariantDataList::Write(wxSTD ostream
& str
) const
159 str
<< (const char*) s
.mb_str();
164 bool wxVariantDataList::Write(wxString
& str
) const
167 wxNode
* node
= m_value
.GetFirst();
170 wxVariant
* var
= (wxVariant
*) node
->GetData();
171 if (node
!= m_value
.GetFirst())
174 str
+= var
->MakeString();
175 node
= node
->GetNext();
181 #if wxUSE_STD_IOSTREAM
182 bool wxVariantDataList::Read(wxSTD istream
& WXUNUSED(str
))
184 wxFAIL_MSG(wxT("Unimplemented"));
190 bool wxVariantDataList::Read(wxString
& WXUNUSED(str
))
192 wxFAIL_MSG(wxT("Unimplemented"));
198 * wxVariantDataStringList
201 class WXDLLEXPORT wxVariantDataStringList
: public wxVariantData
203 DECLARE_DYNAMIC_CLASS(wxVariantDataStringList
)
205 wxVariantDataStringList() {}
206 wxVariantDataStringList(const wxStringList
& list
) { m_value
= list
; }
208 wxStringList
& GetValue() const { return (wxStringList
&) m_value
; }
209 void SetValue(const wxStringList
& value
);
211 virtual void Copy(wxVariantData
& data
);
212 virtual bool Eq(wxVariantData
& data
) const;
213 #if wxUSE_STD_IOSTREAM
214 virtual bool Write(wxSTD ostream
& str
) const;
216 virtual bool Write(wxString
& str
) const;
217 #if wxUSE_STD_IOSTREAM
218 virtual bool Read(wxSTD istream
& str
);
220 virtual bool Read(wxString
& str
);
221 virtual wxString
GetType() const { return wxT("stringlist"); };
224 wxStringList m_value
;
227 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList
, wxVariantData
)
229 void wxVariantDataStringList::SetValue(const wxStringList
& value
)
234 void wxVariantDataStringList::Copy(wxVariantData
& data
)
236 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") );
238 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
240 listData
.m_value
= m_value
;
243 bool wxVariantDataStringList::Eq(wxVariantData
& data
) const
245 wxASSERT_MSG( (data
.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") );
247 wxVariantDataStringList
& listData
= (wxVariantDataStringList
&) data
;
248 wxStringList::Node
*node1
= m_value
.GetFirst();
249 wxStringList::Node
*node2
= listData
.GetValue().GetFirst();
250 while (node1
&& node2
)
252 wxString
str1 ( node1
->GetData() );
253 wxString
str2 ( node2
->GetData() );
256 node1
= node1
->GetNext();
257 node2
= node2
->GetNext();
259 if (node1
|| node2
) return FALSE
;
263 #if wxUSE_STD_IOSTREAM
264 bool wxVariantDataStringList::Write(wxSTD ostream
& str
) const
268 str
<< (const char*) s
.mb_str();
273 bool wxVariantDataStringList::Write(wxString
& str
) const
276 wxStringList::Node
*node
= m_value
.GetFirst();
279 wxChar
* s
= node
->GetData();
280 if (node
!= m_value
.GetFirst())
283 node
= node
->GetNext();
289 #if wxUSE_STD_IOSTREAM
290 bool wxVariantDataStringList::Read(wxSTD istream
& WXUNUSED(str
))
292 wxFAIL_MSG(wxT("Unimplemented"));
298 bool wxVariantDataStringList::Read(wxString
& WXUNUSED(str
))
300 wxFAIL_MSG(wxT("Unimplemented"));
309 class WXDLLEXPORT wxVariantDataLong
: public wxVariantData
311 DECLARE_DYNAMIC_CLASS(wxVariantDataLong
)
313 wxVariantDataLong() { m_value
= 0; }
314 wxVariantDataLong(long value
) { m_value
= value
; }
316 inline long GetValue() const { return m_value
; }
317 inline void SetValue(long value
) { m_value
= value
; }
319 virtual void Copy(wxVariantData
& data
);
320 virtual bool Eq(wxVariantData
& data
) const;
322 virtual bool Read(wxString
& str
);
323 virtual bool Write(wxString
& str
) const;
324 #if wxUSE_STD_IOSTREAM
325 virtual bool Read(wxSTD istream
& str
);
326 virtual bool Write(wxSTD ostream
& str
) const;
329 virtual bool Read(wxInputStream
& str
);
330 virtual bool Write(wxOutputStream
&str
) const;
331 #endif // wxUSE_STREAMS
333 virtual wxString
GetType() const { return wxT("long"); };
339 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong
, wxVariantData
)
341 void wxVariantDataLong::Copy(wxVariantData
& data
)
343 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") );
345 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
347 otherData
.m_value
= m_value
;
350 bool wxVariantDataLong::Eq(wxVariantData
& data
) const
352 wxASSERT_MSG( (data
.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
354 wxVariantDataLong
& otherData
= (wxVariantDataLong
&) data
;
356 return (otherData
.m_value
== m_value
);
359 #if wxUSE_STD_IOSTREAM
360 bool wxVariantDataLong::Write(wxSTD ostream
& str
) const
364 str
<< (const char*) s
.mb_str();
369 bool wxVariantDataLong::Write(wxString
& str
) const
371 str
.Printf(wxT("%ld"), m_value
);
375 #if wxUSE_STD_IOSTREAM
376 bool wxVariantDataLong::Read(wxSTD istream
& str
)
384 bool wxVariantDataLong::Write(wxOutputStream
& str
) const
386 wxTextOutputStream
s(str
);
388 s
.Write32((size_t)m_value
);
392 bool wxVariantDataLong::Read(wxInputStream
& str
)
394 wxTextInputStream
s(str
);
395 m_value
= s
.Read32();
398 #endif // wxUSE_STREAMS
400 bool wxVariantDataLong::Read(wxString
& str
)
402 m_value
= wxAtol((const wxChar
*) str
);
410 class WXDLLEXPORT wxVariantDataReal
: public wxVariantData
412 DECLARE_DYNAMIC_CLASS(wxVariantDataReal
)
414 wxVariantDataReal() { m_value
= 0.0; }
415 wxVariantDataReal(double value
) { m_value
= value
; }
417 inline double GetValue() const { return m_value
; }
418 inline void SetValue(double value
) { m_value
= value
; }
420 virtual void Copy(wxVariantData
& data
);
421 virtual bool Eq(wxVariantData
& data
) const;
422 virtual bool Read(wxString
& str
);
423 #if wxUSE_STD_IOSTREAM
424 virtual bool Write(wxSTD ostream
& str
) const;
426 virtual bool Write(wxString
& str
) const;
427 #if wxUSE_STD_IOSTREAM
428 virtual bool Read(wxSTD istream
& str
);
431 virtual bool Read(wxInputStream
& str
);
432 virtual bool Write(wxOutputStream
&str
) const;
433 #endif // wxUSE_STREAMS
434 virtual wxString
GetType() const { return wxT("double"); };
440 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal
, wxVariantData
)
442 void wxVariantDataReal::Copy(wxVariantData
& data
)
444 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") );
446 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
448 otherData
.m_value
= m_value
;
451 bool wxVariantDataReal::Eq(wxVariantData
& data
) const
453 wxASSERT_MSG( (data
.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") );
455 wxVariantDataReal
& otherData
= (wxVariantDataReal
&) data
;
457 return (otherData
.m_value
== m_value
);
460 #if wxUSE_STD_IOSTREAM
461 bool wxVariantDataReal::Write(wxSTD ostream
& str
) const
465 str
<< (const char*) s
.mb_str();
470 bool wxVariantDataReal::Write(wxString
& str
) const
472 str
.Printf(wxT("%.4f"), m_value
);
476 #if wxUSE_STD_IOSTREAM
477 bool wxVariantDataReal::Read(wxSTD istream
& str
)
485 bool wxVariantDataReal::Write(wxOutputStream
& str
) const
487 wxTextOutputStream
s(str
);
488 s
.WriteDouble((double)m_value
);
492 bool wxVariantDataReal::Read(wxInputStream
& str
)
494 wxTextInputStream
s(str
);
495 m_value
= (float)s
.ReadDouble();
498 #endif // wxUSE_STREAMS
500 bool wxVariantDataReal::Read(wxString
& str
)
502 m_value
= wxAtof((const wxChar
*) str
);
511 class WXDLLEXPORT wxVariantDataBool
: public wxVariantData
513 DECLARE_DYNAMIC_CLASS(wxVariantDataBool
)
515 wxVariantDataBool() { m_value
= 0; }
516 wxVariantDataBool(bool value
) { m_value
= value
; }
518 inline bool GetValue() const { return m_value
; }
519 inline void SetValue(bool value
) { m_value
= value
; }
521 virtual void Copy(wxVariantData
& data
);
522 virtual bool Eq(wxVariantData
& data
) const;
523 #if wxUSE_STD_IOSTREAM
524 virtual bool Write(wxSTD ostream
& str
) const;
526 virtual bool Write(wxString
& str
) const;
527 virtual bool Read(wxString
& str
);
528 #if wxUSE_STD_IOSTREAM
529 virtual bool Read(wxSTD istream
& str
);
532 virtual bool Read(wxInputStream
& str
);
533 virtual bool Write(wxOutputStream
& str
) const;
534 #endif // wxUSE_STREAMS
535 virtual wxString
GetType() const { return wxT("bool"); };
541 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool
, wxVariantData
)
543 void wxVariantDataBool::Copy(wxVariantData
& data
)
545 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") );
547 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
549 otherData
.m_value
= m_value
;
552 bool wxVariantDataBool::Eq(wxVariantData
& data
) const
554 wxASSERT_MSG( (data
.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
556 wxVariantDataBool
& otherData
= (wxVariantDataBool
&) data
;
558 return (otherData
.m_value
== m_value
);
561 #if wxUSE_STD_IOSTREAM
562 bool wxVariantDataBool::Write(wxSTD ostream
& str
) const
566 str
<< (const char*) s
.mb_str();
571 bool wxVariantDataBool::Write(wxString
& str
) const
573 str
.Printf(wxT("%d"), (int) m_value
);
577 #if wxUSE_STD_IOSTREAM
578 bool wxVariantDataBool::Read(wxSTD istream
& WXUNUSED(str
))
580 wxFAIL_MSG(wxT("Unimplemented"));
581 // str >> (long) m_value;
587 bool wxVariantDataBool::Write(wxOutputStream
& str
) const
589 wxTextOutputStream
s(str
);
595 bool wxVariantDataBool::Read(wxInputStream
& str
)
597 wxTextInputStream
s(str
);
599 m_value
= s
.Read8() != 0;
602 #endif // wxUSE_STREAMS
604 bool wxVariantDataBool::Read(wxString
& str
)
606 m_value
= (wxAtol((const wxChar
*) str
) != 0);
615 class WXDLLEXPORT wxVariantDataChar
: public wxVariantData
617 DECLARE_DYNAMIC_CLASS(wxVariantDataChar
)
619 wxVariantDataChar() { m_value
= 0; }
620 wxVariantDataChar(char value
) { m_value
= value
; }
622 inline char GetValue() const { return m_value
; }
623 inline void SetValue(char value
) { m_value
= value
; }
625 virtual void Copy(wxVariantData
& data
);
626 virtual bool Eq(wxVariantData
& data
) const;
627 #if wxUSE_STD_IOSTREAM
628 virtual bool Read(wxSTD istream
& str
);
629 virtual bool Write(wxSTD ostream
& str
) const;
631 virtual bool Read(wxString
& str
);
632 virtual bool Write(wxString
& str
) const;
634 virtual bool Read(wxInputStream
& str
);
635 virtual bool Write(wxOutputStream
& str
) const;
636 #endif // wxUSE_STREAMS
637 virtual wxString
GetType() const { return wxT("char"); };
643 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar
, wxVariantData
)
645 void wxVariantDataChar::Copy(wxVariantData
& data
)
647 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") );
649 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
651 otherData
.m_value
= m_value
;
654 bool wxVariantDataChar::Eq(wxVariantData
& data
) const
656 wxASSERT_MSG( (data
.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
658 wxVariantDataChar
& otherData
= (wxVariantDataChar
&) data
;
660 return (otherData
.m_value
== m_value
);
663 #if wxUSE_STD_IOSTREAM
664 bool wxVariantDataChar::Write(wxSTD ostream
& str
) const
668 str
<< (const char*) s
.mb_str();
673 bool wxVariantDataChar::Write(wxString
& str
) const
675 str
.Printf(wxT("%c"), m_value
);
679 #if wxUSE_STD_IOSTREAM
680 bool wxVariantDataChar::Read(wxSTD istream
& WXUNUSED(str
))
682 wxFAIL_MSG(wxT("Unimplemented"));
689 bool wxVariantDataChar::Write(wxOutputStream
& str
) const
691 wxTextOutputStream
s(str
);
697 bool wxVariantDataChar::Read(wxInputStream
& str
)
699 wxTextInputStream
s(str
);
704 #endif // wxUSE_STREAMS
706 bool wxVariantDataChar::Read(wxString
& str
)
708 m_value
= str
[(size_t)0];
713 * wxVariantDataString
716 #if defined(__BORLANDC__) && defined(__WIN16__)
717 // Change name because of truncation
718 #define wxVariantDataString wxVariantStringData
721 class WXDLLEXPORT wxVariantDataString
: public wxVariantData
723 #if defined(__BORLANDC__) && defined(__WIN16__)
724 DECLARE_DYNAMIC_CLASS(wxVariantStringData
)
726 DECLARE_DYNAMIC_CLASS(wxVariantDataString
)
729 wxVariantDataString() { }
730 wxVariantDataString(const wxString
& value
) { m_value
= value
; }
732 inline wxString
GetValue() const { return m_value
; }
733 inline void SetValue(const wxString
& value
) { m_value
= value
; }
735 virtual void Copy(wxVariantData
& data
);
736 virtual bool Eq(wxVariantData
& data
) const;
737 #if wxUSE_STD_IOSTREAM
738 virtual bool Write(wxSTD ostream
& str
) const;
740 virtual bool Read(wxString
& str
);
741 virtual bool Write(wxString
& str
) const;
742 #if wxUSE_STD_IOSTREAM
743 virtual bool Read(wxSTD istream
& str
);
746 virtual bool Read(wxInputStream
& str
);
747 virtual bool Write(wxOutputStream
& str
) const;
748 #endif // wxUSE_STREAMS
749 virtual wxString
GetType() const { return wxT("string"); };
755 void wxVariantDataString::Copy(wxVariantData
& data
)
757 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") );
759 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
761 otherData
.m_value
= m_value
;
764 bool wxVariantDataString::Eq(wxVariantData
& data
) const
766 wxASSERT_MSG( (data
.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
768 wxVariantDataString
& otherData
= (wxVariantDataString
&) data
;
770 return (otherData
.m_value
== m_value
);
773 #if wxUSE_STD_IOSTREAM
774 bool wxVariantDataString::Write(wxSTD ostream
& str
) const
776 str
<< (const char*) m_value
.mb_str();
781 bool wxVariantDataString::Write(wxString
& str
) const
787 #if wxUSE_STD_IOSTREAM
788 bool wxVariantDataString::Read(wxSTD istream
& str
)
796 bool wxVariantDataString::Write(wxOutputStream
& str
) const
798 // why doesn't wxOutputStream::operator<< take "const wxString&"
799 wxTextOutputStream
s(str
);
800 s
.WriteString(m_value
);
804 bool wxVariantDataString::Read(wxInputStream
& str
)
806 wxTextInputStream
s(str
);
808 m_value
= s
.ReadString();
811 #endif // wxUSE_STREAMS
813 bool wxVariantDataString::Read(wxString
& str
)
819 #if defined(__BORLANDC__) && defined(__WIN16__)
820 IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData
, wxVariantData
)
822 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString
, wxVariantData
)
826 * wxVariantDataVoidPtr
829 class wxVariantDataVoidPtr
: public wxVariantData
831 DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr
)
833 wxVariantDataVoidPtr() { }
834 wxVariantDataVoidPtr(void* value
) { m_value
= value
; }
836 inline void* GetValue() const { return m_value
; }
837 inline void SetValue(void* value
) { m_value
= value
; }
839 virtual void Copy(wxVariantData
& data
);
840 virtual bool Eq(wxVariantData
& data
) const;
841 #if wxUSE_STD_IOSTREAM
842 virtual bool Write(wxSTD ostream
& str
) const;
844 virtual bool Write(wxString
& str
) const;
845 #if wxUSE_STD_IOSTREAM
846 virtual bool Read(wxSTD istream
& str
);
848 virtual bool Read(wxString
& str
);
849 virtual wxString
GetType() const { return wxT("void*"); };
850 virtual wxVariantData
* Clone() { return new wxVariantDataVoidPtr
; }
855 DECLARE_NO_COPY_CLASS(wxVariantDataVoidPtr
)
858 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr
, wxVariantData
)
860 void wxVariantDataVoidPtr::Copy(wxVariantData
& data
)
862 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") );
864 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
866 otherData
.m_value
= m_value
;
869 bool wxVariantDataVoidPtr::Eq(wxVariantData
& data
) const
871 wxASSERT_MSG( (data
.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
873 wxVariantDataVoidPtr
& otherData
= (wxVariantDataVoidPtr
&) data
;
875 return (otherData
.m_value
== m_value
);
878 #if wxUSE_STD_IOSTREAM
879 bool wxVariantDataVoidPtr::Write(wxSTD ostream
& str
) const
883 str
<< (const char*) s
.mb_str();
888 bool wxVariantDataVoidPtr::Write(wxString
& str
) const
890 str
.Printf(wxT("%ld"), (long) m_value
);
894 #if wxUSE_STD_IOSTREAM
895 bool wxVariantDataVoidPtr::Read(wxSTD istream
& WXUNUSED(str
))
902 bool wxVariantDataVoidPtr::Read(wxString
& WXUNUSED(str
))
909 * wxVariantDataDateTime
914 class wxVariantDataDateTime
: public wxVariantData
916 DECLARE_DYNAMIC_CLASS(wxVariantDataDateTime
)
919 wxVariantDataDateTime() { }
920 wxVariantDataDateTime(const wxDateTime
& value
) { m_value
= value
; }
922 wxVariantDataDateTime(const TIME_STRUCT
* valptr
)
923 { m_value
= wxDateTime(valptr
->hour
, valptr
->minute
, valptr
->second
); }
924 wxVariantDataDateTime(const DATE_STRUCT
* valptr
)
925 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1),valptr
->year
); }
926 wxVariantDataDateTime(const TIMESTAMP_STRUCT
* valptr
)
927 { m_value
= wxDateTime(valptr
->day
, (wxDateTime::Month
) (valptr
->month
- 1), valptr
->year
,
928 valptr
->hour
, valptr
->minute
, valptr
->second
, valptr
->fraction
); }
931 inline wxDateTime
GetValue() const { return m_value
; }
932 inline void SetValue(const wxDateTime
& value
) { m_value
= value
; }
934 virtual void Copy(wxVariantData
& data
);
935 virtual bool Eq(wxVariantData
& data
) const;
936 #if wxUSE_STD_IOSTREAM
937 virtual bool Write(wxSTD ostream
& str
) const;
939 virtual bool Write(wxString
& str
) const;
940 #if wxUSE_STD_IOSTREAM
941 virtual bool Read(wxSTD istream
& str
);
943 virtual bool Read(wxString
& str
);
944 virtual wxString
GetType() const { return wxT("datetime"); };
945 virtual wxVariantData
* Clone() { return new wxVariantDataDateTime
; }
952 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDateTime
, wxVariantData
)
954 void wxVariantDataDateTime::Copy(wxVariantData
& data
)
956 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Copy: Can't copy to this type of data") );
958 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
960 otherData
.m_value
= m_value
;
964 bool wxVariantDataDateTime::Eq(wxVariantData
& data
) const
966 wxASSERT_MSG( (data
.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
968 wxVariantDataDateTime
& otherData
= (wxVariantDataDateTime
&) data
;
970 return (otherData
.m_value
== m_value
);
974 #if wxUSE_STD_IOSTREAM
975 bool wxVariantDataDateTime::Write(wxSTD ostream
& str
) const
983 bool wxVariantDataDateTime::Write(wxString
& str
) const
985 str
= m_value
.Format();
990 #if wxUSE_STD_IOSTREAM
991 bool wxVariantDataDateTime::Read(wxSTD istream
& WXUNUSED(str
))
999 bool wxVariantDataDateTime::Read(wxString
& str
)
1001 if(! m_value
.ParseDateTime(str
))
1006 #endif // wxUSE_DATETIME
1008 // ----------------------------------------------------------------------------
1009 // wxVariantDataArrayString
1010 // ----------------------------------------------------------------------------
1012 class wxVariantDataArrayString
: public wxVariantData
1015 wxVariantDataArrayString() { }
1016 wxVariantDataArrayString(const wxArrayString
& value
) { m_value
= value
; }
1018 wxArrayString
GetValue() const { return m_value
; }
1019 void SetValue(const wxArrayString
& value
) { m_value
= value
; }
1021 virtual void Copy(wxVariantData
& data
);
1022 virtual bool Eq(wxVariantData
& data
) const;
1023 #if wxUSE_STD_IOSTREAM
1024 virtual bool Write(wxSTD ostream
& str
) const;
1026 virtual bool Write(wxString
& str
) const;
1027 #if wxUSE_STD_IOSTREAM
1028 virtual bool Read(wxSTD istream
& str
);
1030 virtual bool Read(wxString
& str
);
1031 virtual wxString
GetType() const { return wxT("arrstring"); };
1032 virtual wxVariantData
* Clone() { return new wxVariantDataArrayString
; }
1035 wxArrayString m_value
;
1037 DECLARE_DYNAMIC_CLASS(wxVariantDataArrayString
)
1040 IMPLEMENT_DYNAMIC_CLASS(wxVariantDataArrayString
, wxVariantData
)
1042 void wxVariantDataArrayString::Copy(wxVariantData
& data
)
1044 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Copy: Can't copy to this type of data") );
1046 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1048 otherData
.m_value
= m_value
;
1052 bool wxVariantDataArrayString::Eq(wxVariantData
& data
) const
1054 wxASSERT_MSG( data
.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1056 wxVariantDataArrayString
& otherData
= (wxVariantDataArrayString
&) data
;
1058 return otherData
.m_value
== m_value
;
1062 #if wxUSE_STD_IOSTREAM
1063 bool wxVariantDataArrayString::Write(wxSTD ostream
& str
) const
1071 bool wxVariantDataArrayString::Write(wxString
& str
) const
1073 size_t count
= m_value
.GetCount();
1074 for ( size_t n
= 0; n
< count
; n
++ )
1086 #if wxUSE_STD_IOSTREAM
1087 bool wxVariantDataArrayString::Read(wxSTD istream
& WXUNUSED(str
))
1095 bool wxVariantDataArrayString::Read(wxString
& str
)
1097 wxStringTokenizer
tk(str
, _T(";"));
1098 while ( tk
.HasMoreTokens() )
1100 m_value
.Add(tk
.GetNextToken());
1112 IMPLEMENT_DYNAMIC_CLASS(wxVariant
, wxObject
)
1114 // Construction & destruction
1115 wxVariant::wxVariant()
1117 m_data
= (wxVariantData
*) NULL
;
1120 wxVariant::wxVariant(double val
, const wxString
& name
)
1122 m_data
= new wxVariantDataReal(val
);
1126 wxVariant::wxVariant(long val
, const wxString
& name
)
1128 m_data
= new wxVariantDataLong(val
);
1133 wxVariant::wxVariant(bool val
, const wxString
& name
)
1135 m_data
= new wxVariantDataBool(val
);
1140 wxVariant::wxVariant(char val
, const wxString
& name
)
1142 m_data
= new wxVariantDataChar(val
);
1146 wxVariant::wxVariant(const wxString
& val
, const wxString
& name
)
1148 m_data
= new wxVariantDataString(val
);
1152 wxVariant::wxVariant(const wxChar
* val
, const wxString
& name
)
1154 m_data
= new wxVariantDataString(wxString(val
));
1158 wxVariant::wxVariant(const wxStringList
& val
, const wxString
& name
)
1160 m_data
= new wxVariantDataStringList(val
);
1164 wxVariant::wxVariant(const wxList
& val
, const wxString
& name
) // List of variants
1166 m_data
= new wxVariantDataList(val
);
1170 wxVariant::wxVariant( void* val
, const wxString
& name
)
1172 m_data
= new wxVariantDataVoidPtr(val
);
1177 wxVariant::wxVariant(const wxDateTime
& val
, const wxString
& name
) // Date
1179 m_data
= new wxVariantDataDateTime(val
);
1182 #endif // wxUSE_DATETIME
1185 wxVariant::wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
) // Date
1187 m_data
= new wxVariantDataDateTime(valptr
);
1191 wxVariant::wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
) // Date
1193 m_data
= new wxVariantDataDateTime(valptr
);
1197 wxVariant::wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
) // Date
1199 m_data
= new wxVariantDataDateTime(valptr
);
1202 #endif // wxUSE_ODBC
1204 wxVariant::wxVariant(const wxArrayString
& val
, const wxString
& name
) // Strings
1206 m_data
= new wxVariantDataArrayString(val
);
1210 wxVariant::wxVariant(const wxVariant
& variant
)
1213 if (!variant
.IsNull())
1215 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1216 variant
.m_data
->Copy(*m_data
);
1219 m_data
= (wxVariantData
*) NULL
;
1220 m_name
= variant
.m_name
;
1223 wxVariant::wxVariant(wxVariantData
* data
, const wxString
& name
) // User-defined data
1229 wxVariant::~wxVariant()
1235 // Make NULL (i.e. delete the data)
1236 void wxVariant::MakeNull()
1242 // Generic operators
1244 void wxVariant::operator= (const wxVariant
& variant
)
1246 if (variant
.IsNull())
1252 if (IsNull() || (GetType() != variant
.GetType()))
1256 m_data
= (wxVariantData
*) variant
.GetData()->GetClassInfo()->CreateObject();
1259 variant
.GetData()->Copy(* GetData());
1261 m_name
= variant
.m_name
;
1264 // Assignment using data, e.g.
1265 // myVariant = new wxStringVariantData("hello")
1266 void wxVariant::operator= (wxVariantData
* variantData
)
1269 m_data
= variantData
;
1272 bool wxVariant::operator== (const wxVariant
& variant
) const
1274 if (IsNull() || variant
.IsNull())
1275 return (IsNull() == variant
.IsNull());
1277 return (GetData()->Eq(* variant
.GetData()));
1280 bool wxVariant::operator!= (const wxVariant
& variant
) const
1282 return (!(*this == variant
));
1286 // Specific operators
1287 bool wxVariant::operator== (double value
) const
1290 if (!Convert(&thisValue
))
1293 return (value
== thisValue
);
1296 bool wxVariant::operator!= (double value
) const
1298 return (!((*this) == value
));
1301 void wxVariant::operator= (double value
)
1303 if (GetType() == wxT("double"))
1305 ((wxVariantDataReal
*)GetData())->SetValue(value
);
1311 m_data
= new wxVariantDataReal(value
);
1315 bool wxVariant::operator== (long value
) const
1318 if (!Convert(&thisValue
))
1321 return (value
== thisValue
);
1324 bool wxVariant::operator!= (long value
) const
1326 return (!((*this) == value
));
1329 void wxVariant::operator= (long value
)
1331 if (GetType() == wxT("long"))
1333 ((wxVariantDataLong
*)GetData())->SetValue(value
);
1339 m_data
= new wxVariantDataLong(value
);
1343 bool wxVariant::operator== (char value
) const
1346 if (!Convert(&thisValue
))
1349 return (value
== thisValue
);
1352 bool wxVariant::operator!= (char value
) const
1354 return (!((*this) == value
));
1357 void wxVariant::operator= (char value
)
1359 if (GetType() == wxT("char"))
1361 ((wxVariantDataChar
*)GetData())->SetValue(value
);
1367 m_data
= new wxVariantDataChar(value
);
1372 bool wxVariant::operator== (bool value
) const
1375 if (!Convert(&thisValue
))
1378 return (value
== thisValue
);
1381 bool wxVariant::operator!= (bool value
) const
1383 return (!((*this) == value
));
1386 void wxVariant::operator= (bool value
)
1388 if (GetType() == wxT("bool"))
1390 ((wxVariantDataBool
*)GetData())->SetValue(value
);
1396 m_data
= new wxVariantDataBool(value
);
1401 bool wxVariant::operator== (const wxString
& value
) const
1404 if (!Convert(&thisValue
))
1407 return value
== thisValue
;
1410 bool wxVariant::operator!= (const wxString
& value
) const
1412 return (!((*this) == value
));
1415 void wxVariant::operator= (const wxString
& value
)
1417 if (GetType() == wxT("string"))
1419 ((wxVariantDataString
*)GetData())->SetValue(value
);
1425 m_data
= new wxVariantDataString(value
);
1429 void wxVariant::operator= (const wxChar
* value
)
1431 if (GetType() == wxT("string"))
1433 ((wxVariantDataString
*)GetData())->SetValue(wxString(value
));
1439 m_data
= new wxVariantDataString(wxString(value
));
1443 bool wxVariant::operator== (const wxStringList
& value
) const
1445 wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") );
1447 wxVariantDataStringList
other(value
);
1448 return (m_data
->Eq(other
));
1451 bool wxVariant::operator!= (const wxStringList
& value
) const
1453 return (!((*this) == value
));
1456 void wxVariant::operator= (const wxStringList
& value
)
1458 if (GetType() == wxT("stringlist"))
1460 ((wxVariantDataStringList
*)GetData())->SetValue(value
);
1466 m_data
= new wxVariantDataStringList(value
);
1470 bool wxVariant::operator== (const wxList
& value
) const
1472 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1474 wxVariantDataList
other(value
);
1475 return (m_data
->Eq(other
));
1478 bool wxVariant::operator!= (const wxList
& value
) const
1480 return (!((*this) == value
));
1483 void wxVariant::operator= (const wxList
& value
)
1485 if (GetType() == wxT("list"))
1487 ((wxVariantDataList
*)GetData())->SetValue(value
);
1493 m_data
= new wxVariantDataList(value
);
1497 bool wxVariant::operator== (void* value
) const
1499 return (value
== ((wxVariantDataVoidPtr
*)GetData())->GetValue());
1502 bool wxVariant::operator!= (void* value
) const
1504 return (!((*this) == (void*) value
));
1507 void wxVariant::operator= (void* value
)
1509 if (GetType() == wxT("void*"))
1511 ((wxVariantDataVoidPtr
*)GetData())->SetValue(value
);
1517 m_data
= new wxVariantDataVoidPtr(value
);
1522 bool wxVariant::operator== (const wxDateTime
& value
) const
1524 wxDateTime thisValue
;
1525 if (!Convert(&thisValue
))
1528 return value
.IsEqualTo(thisValue
);
1531 bool wxVariant::operator!= (const wxDateTime
& value
) const
1533 return (!((*this) == value
));
1536 void wxVariant::operator= (const wxDateTime
& value
)
1538 if (GetType() == wxT("datetime"))
1540 ((wxVariantDataDateTime
*)GetData())->SetValue(value
);
1546 m_data
= new wxVariantDataDateTime(value
);
1549 #endif // wxUSE_DATETIME
1552 void wxVariant::operator= (const DATE_STRUCT
* value
)
1556 m_data
= new wxVariantDataDateTime(value
);
1560 void wxVariant::operator= (const TIME_STRUCT
* value
)
1564 m_data
= new wxVariantDataDateTime(value
);
1568 void wxVariant::operator= (const TIMESTAMP_STRUCT
* value
)
1572 m_data
= new wxVariantDataDateTime(value
);
1575 #endif // wxUSE_ODBC
1577 bool wxVariant::operator==(const wxArrayString
& WXUNUSED(value
)) const
1579 wxFAIL_MSG( _T("TODO") );
1584 bool wxVariant::operator!=(const wxArrayString
& value
) const
1586 return !(*this == value
);
1589 void wxVariant::operator=(const wxArrayString
& value
)
1591 if (GetType() == wxT("arrstring"))
1593 ((wxVariantDataArrayString
*)GetData())->SetValue(value
);
1598 m_data
= new wxVariantDataArrayString(value
);
1602 wxArrayString
wxVariant::GetArrayString() const
1604 if ( GetType() == wxT("arrstring") )
1605 return ((wxVariantDataArrayString
*)GetData())->GetValue();
1607 return wxArrayString();
1611 // Treat a list variant as an array
1612 wxVariant
wxVariant::operator[] (size_t idx
) const
1614 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") );
1616 if (GetType() == wxT("list"))
1618 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1619 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1620 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1622 else if (GetType() == wxT("stringlist"))
1624 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1625 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1627 wxVariant
variant( wxString( (wxChar
*) (data
->GetValue().Item(idx
)->GetData()) ));
1630 return wxNullVariant
;
1633 wxVariant
& wxVariant::operator[] (size_t idx
)
1635 // We can't return a reference to a variant for a string list, since the string
1636 // is actually stored as a char*, not a variant.
1638 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1640 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1641 wxASSERT_MSG( (idx
< (size_t) data
->GetValue().GetCount()), wxT("Invalid index for array") );
1643 return * (wxVariant
*) (data
->GetValue().Item(idx
)->GetData());
1646 // Return the number of elements in a list
1647 int wxVariant::GetCount() const
1649 wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") );
1651 if (GetType() == wxT("list"))
1653 wxVariantDataList
* data
= (wxVariantDataList
*) m_data
;
1654 return data
->GetValue().GetCount();
1656 else if (GetType() == wxT("stringlist"))
1658 wxVariantDataStringList
* data
= (wxVariantDataStringList
*) m_data
;
1659 return data
->GetValue().GetCount();
1664 wxString
wxVariant::MakeString() const
1669 if (GetData()->Write(str
))
1672 return wxString(wxT(""));
1677 void wxVariant::SetData(wxVariantData
* data
)
1679 if (m_data
) delete m_data
;
1684 // Returns a string representing the type of the variant,
1685 // e.g. "string", "bool", "stringlist", "list", "double", "long"
1686 wxString
wxVariant::GetType() const
1689 return wxString(wxT("null"));
1691 return m_data
->GetType();
1695 bool wxVariant::IsType(const wxString
& type
) const
1697 return (GetType() == type
);
1702 double wxVariant::GetReal() const
1705 if (Convert(& value
))
1709 wxFAIL_MSG(wxT("Could not convert to a real number"));
1714 long wxVariant::GetInteger() const
1717 if (Convert(& value
))
1721 wxFAIL_MSG(wxT("Could not convert to an integer"));
1726 char wxVariant::GetChar() const
1729 if (Convert(& value
))
1733 wxFAIL_MSG(wxT("Could not convert to a char"));
1738 bool wxVariant::GetBool() const
1741 if (Convert(& value
))
1745 wxFAIL_MSG(wxT("Could not convert to a bool"));
1750 wxString
wxVariant::GetString() const
1753 if (!Convert(& value
))
1755 wxFAIL_MSG(wxT("Could not convert to a string"));
1761 void* wxVariant::GetVoidPtr() const
1763 wxASSERT( (GetType() == wxT("void*")) );
1765 return (void*) ((wxVariantDataVoidPtr
*) m_data
)->GetValue();
1769 wxDateTime
wxVariant::GetDateTime() const
1772 if (!Convert(& value
))
1774 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1779 #endif // wxUSE_DATETIME
1781 wxList
& wxVariant::GetList() const
1783 wxASSERT( (GetType() == wxT("list")) );
1785 return (wxList
&) ((wxVariantDataList
*) m_data
)->GetValue();
1788 wxStringList
& wxVariant::GetStringList() const
1790 wxASSERT( (GetType() == wxT("stringlist")) );
1792 return (wxStringList
&) ((wxVariantDataStringList
*) m_data
)->GetValue();
1796 void wxVariant::NullList()
1798 SetData(new wxVariantDataList());
1802 void wxVariant::Append(const wxVariant
& value
)
1804 wxList
& list
= GetList();
1806 list
.Append(new wxVariant(value
));
1809 // Insert at front of list
1810 void wxVariant::Insert(const wxVariant
& value
)
1812 wxList
& list
= GetList();
1814 list
.Insert(new wxVariant(value
));
1817 // Returns TRUE if the variant is a member of the list
1818 bool wxVariant::Member(const wxVariant
& value
) const
1820 wxList
& list
= GetList();
1822 wxNode
* node
= list
.GetFirst();
1825 wxVariant
* other
= (wxVariant
*) node
->GetData();
1826 if (value
== *other
)
1828 node
= node
->GetNext();
1833 // Deletes the nth element of the list
1834 bool wxVariant::Delete(int item
)
1836 wxList
& list
= GetList();
1838 wxASSERT_MSG( (item
< (int) list
.GetCount()), wxT("Invalid index to Delete") );
1839 wxNode
* node
= list
.Item(item
);
1840 wxVariant
* variant
= (wxVariant
*) node
->GetData();
1847 void wxVariant::ClearList()
1849 if (!IsNull() && (GetType() == wxT("list")))
1851 ((wxVariantDataList
*) m_data
)->Clear();
1855 if (GetType() != wxT("list"))
1860 m_data
= new wxVariantDataList
;
1865 bool wxVariant::Convert(long* value
) const
1867 wxString
type(GetType());
1868 if (type
== wxT("double"))
1869 *value
= (long) (((wxVariantDataReal
*)GetData())->GetValue());
1870 else if (type
== wxT("long"))
1871 *value
= ((wxVariantDataLong
*)GetData())->GetValue();
1873 else if (type
== wxT("bool"))
1874 *value
= (long) (((wxVariantDataBool
*)GetData())->GetValue());
1876 else if (type
== wxT("string"))
1877 *value
= wxAtol((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1884 bool wxVariant::Convert(bool* value
) const
1886 wxString
type(GetType());
1887 if (type
== wxT("double"))
1888 *value
= ((int) (((wxVariantDataReal
*)GetData())->GetValue()) != 0);
1889 else if (type
== wxT("long"))
1890 *value
= (((wxVariantDataLong
*)GetData())->GetValue() != 0);
1892 else if (type
== wxT("bool"))
1893 *value
= ((wxVariantDataBool
*)GetData())->GetValue();
1895 else if (type
== wxT("string"))
1897 wxString
val(((wxVariantDataString
*)GetData())->GetValue());
1899 if (val
== wxT("TRUE") || val
== wxT("yes"))
1901 else if (val
== wxT("FALSE") || val
== wxT("no"))
1912 bool wxVariant::Convert(double* value
) const
1914 wxString
type(GetType());
1915 if (type
== wxT("double"))
1916 *value
= ((wxVariantDataReal
*)GetData())->GetValue();
1917 else if (type
== wxT("long"))
1918 *value
= (double) (((wxVariantDataLong
*)GetData())->GetValue());
1920 else if (type
== wxT("bool"))
1921 *value
= (double) (((wxVariantDataBool
*)GetData())->GetValue());
1923 else if (type
== wxT("string"))
1924 *value
= (double) wxAtof((const wxChar
*) ((wxVariantDataString
*)GetData())->GetValue());
1931 bool wxVariant::Convert(char* value
) const
1933 wxString
type(GetType());
1934 if (type
== wxT("char"))
1935 *value
= ((wxVariantDataChar
*)GetData())->GetValue();
1936 else if (type
== wxT("long"))
1937 *value
= (char) (((wxVariantDataLong
*)GetData())->GetValue());
1939 else if (type
== wxT("bool"))
1940 *value
= (char) (((wxVariantDataBool
*)GetData())->GetValue());
1948 bool wxVariant::Convert(wxString
* value
) const
1950 *value
= MakeString();
1955 bool wxVariant::Convert(wxDateTime
* value
) const
1957 wxString
type(GetType());
1958 if (type
== wxT("datetime"))
1960 *value
= ((wxVariantDataDateTime
*)GetData())->GetValue();
1963 // Fallback to string conversion
1965 return Convert(&val
) && (value
->ParseDate(val
));
1967 #endif // wxUSE_DATETIME