]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/variant.cpp
fix assert when adding an entry to an already full file history (closes #10118)
[wxWidgets.git] / src / common / variant.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/variant.cpp
3// Purpose: wxVariant class, container for any type
4// Author: Julian Smart
5// Modified by:
6// Created: 10/09/98
7// RCS-ID: $Id$
8// Copyright: (c)
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#include "wx/variant.h"
20
21#if wxUSE_VARIANT
22
23#ifndef WX_PRECOMP
24 #include "wx/string.h"
25 #include "wx/math.h"
26 #include "wx/crt.h"
27 #if wxUSE_STREAMS
28 #include "wx/stream.h"
29 #endif
30#endif
31
32#if wxUSE_STD_IOSTREAM
33 #if wxUSE_IOSTREAMH
34 #include <fstream.h>
35 #else
36 #include <fstream>
37 #endif
38#endif
39
40#if defined(__MWERKS__) && __MSL__ >= 0x6000
41namespace std {}
42using namespace std ;
43#endif
44
45#if wxUSE_STREAMS
46 #include "wx/txtstrm.h"
47#endif
48
49#include "wx/string.h"
50#include "wx/tokenzr.h"
51
52wxVariant WXDLLIMPEXP_BASE wxNullVariant;
53
54
55#include "wx/listimpl.cpp"
56WX_DEFINE_LIST(wxVariantList)
57
58/*
59 * wxVariant
60 */
61
62IMPLEMENT_DYNAMIC_CLASS(wxVariant, wxObject)
63
64wxVariant::wxVariant()
65{
66 m_data = (wxVariantData*) NULL;
67}
68
69bool wxVariant::IsNull() const
70{
71 return (m_data == (wxVariantData*) NULL);
72}
73
74void wxVariant::MakeNull()
75{
76 UnRef();
77}
78
79void wxVariant::Clear()
80{
81 m_name = wxEmptyString;
82}
83
84wxVariant::wxVariant(const wxVariant& variant)
85 : wxObject()
86{
87 m_data = (wxVariantData*) NULL;
88
89 if (!variant.IsNull())
90 Ref(variant);
91
92 m_name = variant.m_name;
93}
94
95wxVariant::wxVariant(wxVariantData* data, const wxString& name) // User-defined data
96{
97 m_data = data;
98 m_name = name;
99}
100
101wxVariant::~wxVariant()
102{
103 UnRef();
104}
105
106// Assignment
107void wxVariant::operator= (const wxVariant& variant)
108{
109 Ref(variant);
110 m_name = variant.m_name;
111}
112
113// myVariant = new wxStringVariantData("hello")
114void wxVariant::operator= (wxVariantData* variantData)
115{
116 UnRef();
117 m_data = variantData;
118}
119
120bool wxVariant::operator== (const wxVariant& variant) const
121{
122 if (IsNull() || variant.IsNull())
123 return (IsNull() == variant.IsNull());
124
125 if (GetType() != variant.GetType())
126 return false;
127
128 return (GetData()->Eq(* variant.GetData()));
129}
130
131bool wxVariant::operator!= (const wxVariant& variant) const
132{
133 return (!(*this == variant));
134}
135
136wxString wxVariant::MakeString() const
137{
138 if (!IsNull())
139 {
140 wxString str;
141 if (GetData()->Write(str))
142 return str;
143 }
144 return wxEmptyString;
145}
146
147void wxVariant::SetData(wxVariantData* data)
148{
149 UnRef();
150 m_data = data;
151}
152
153void wxVariant::Ref(const wxVariant& clone)
154{
155 // nothing to be done
156 if (m_data == clone.m_data)
157 return;
158
159 // delete reference to old data
160 UnRef();
161
162 // reference new data
163 if ( clone.m_data )
164 {
165 m_data = clone.m_data;
166 m_data->m_count++;
167 }
168}
169
170
171void wxVariant::UnRef()
172{
173 if ( m_data )
174 {
175 wxASSERT_MSG( m_data->m_count > 0, _T("invalid ref data count") );
176
177 m_data->DecRef();
178 m_data = NULL;
179 }
180}
181
182bool wxVariant::Unshare()
183{
184 if ( m_data && m_data->GetRefCount() > 1 )
185 {
186 // note that ref is not going to be destroyed in this case...
187 const wxVariantData* ref = m_data;
188 UnRef();
189
190 // ... so we can still access it
191 m_data = ref->Clone();
192
193 wxASSERT_MSG( (m_data && m_data->GetRefCount() == 1),
194 _T("wxVariant::AllocExclusive() failed.") );
195
196 if (!m_data || m_data->GetRefCount() != 1)
197 return false;
198 else
199 return true;
200 }
201 //else: data is null or ref count is 1, so we are exclusive owners of m_refData anyhow
202 else
203 return true;
204}
205
206
207// Returns a string representing the type of the variant,
208// e.g. "string", "bool", "list", "double", "long"
209wxString wxVariant::GetType() const
210{
211 if (IsNull())
212 return wxString(wxT("null"));
213 else
214 return GetData()->GetType();
215}
216
217
218bool wxVariant::IsType(const wxString& type) const
219{
220 return (GetType() == type);
221}
222
223bool wxVariant::IsValueKindOf(const wxClassInfo* type) const
224{
225 wxClassInfo* info=GetData()->GetValueClassInfo();
226 return info ? info->IsKindOf(type) : false ;
227}
228
229
230// -----------------------------------------------------------------
231// wxVariantDataLong
232// -----------------------------------------------------------------
233
234class WXDLLIMPEXP_BASE wxVariantDataLong: public wxVariantData
235{
236public:
237 wxVariantDataLong() { m_value = 0; }
238 wxVariantDataLong(long value) { m_value = value; }
239
240 inline long GetValue() const { return m_value; }
241 inline void SetValue(long value) { m_value = value; }
242
243 virtual bool Eq(wxVariantData& data) const;
244
245 virtual bool Read(wxString& str);
246 virtual bool Write(wxString& str) const;
247#if wxUSE_STD_IOSTREAM
248 virtual bool Read(wxSTD istream& str);
249 virtual bool Write(wxSTD ostream& str) const;
250#endif
251#if wxUSE_STREAMS
252 virtual bool Read(wxInputStream& str);
253 virtual bool Write(wxOutputStream &str) const;
254#endif // wxUSE_STREAMS
255
256 wxVariantData* Clone() const { return new wxVariantDataLong(m_value); }
257
258 virtual wxString GetType() const { return wxT("long"); }
259
260protected:
261 long m_value;
262};
263
264bool wxVariantDataLong::Eq(wxVariantData& data) const
265{
266 wxASSERT_MSG( (data.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") );
267
268 wxVariantDataLong& otherData = (wxVariantDataLong&) data;
269
270 return (otherData.m_value == m_value);
271}
272
273#if wxUSE_STD_IOSTREAM
274bool wxVariantDataLong::Write(wxSTD ostream& str) const
275{
276 wxString s;
277 Write(s);
278 str << (const char*) s.mb_str();
279 return true;
280}
281#endif
282
283bool wxVariantDataLong::Write(wxString& str) const
284{
285 str.Printf(wxT("%ld"), m_value);
286 return true;
287}
288
289#if wxUSE_STD_IOSTREAM
290bool wxVariantDataLong::Read(wxSTD istream& str)
291{
292 str >> m_value;
293 return true;
294}
295#endif
296
297#if wxUSE_STREAMS
298bool wxVariantDataLong::Write(wxOutputStream& str) const
299{
300 wxTextOutputStream s(str);
301
302 s.Write32((size_t)m_value);
303 return true;
304}
305
306bool wxVariantDataLong::Read(wxInputStream& str)
307{
308 wxTextInputStream s(str);
309 m_value = s.Read32();
310 return true;
311}
312#endif // wxUSE_STREAMS
313
314bool wxVariantDataLong::Read(wxString& str)
315{
316 m_value = wxAtol(str);
317 return true;
318}
319
320// wxVariant
321
322wxVariant::wxVariant(long val, const wxString& name)
323{
324 m_data = new wxVariantDataLong(val);
325 m_name = name;
326}
327
328wxVariant::wxVariant(int val, const wxString& name)
329{
330 m_data = new wxVariantDataLong((long)val);
331 m_name = name;
332}
333
334wxVariant::wxVariant(short val, const wxString& name)
335{
336 m_data = new wxVariantDataLong((long)val);
337 m_name = name;
338}
339
340bool wxVariant::operator== (long value) const
341{
342 long thisValue;
343 if (!Convert(&thisValue))
344 return false;
345 else
346 return (value == thisValue);
347}
348
349bool wxVariant::operator!= (long value) const
350{
351 return (!((*this) == value));
352}
353
354void wxVariant::operator= (long value)
355{
356 if (GetType() == wxT("long") &&
357 m_data->GetRefCount() == 1)
358 {
359 ((wxVariantDataLong*)GetData())->SetValue(value);
360 }
361 else
362 {
363 UnRef();
364 m_data = new wxVariantDataLong(value);
365 }
366}
367
368long wxVariant::GetLong() const
369{
370 long value;
371 if (Convert(& value))
372 return value;
373 else
374 {
375 wxFAIL_MSG(wxT("Could not convert to a long"));
376 return 0;
377 }
378}
379
380// -----------------------------------------------------------------
381// wxVariantDoubleData
382// -----------------------------------------------------------------
383
384class WXDLLIMPEXP_BASE wxVariantDoubleData: public wxVariantData
385{
386public:
387 wxVariantDoubleData() { m_value = 0.0; }
388 wxVariantDoubleData(double value) { m_value = value; }
389
390 inline double GetValue() const { return m_value; }
391 inline void SetValue(double value) { m_value = value; }
392
393 virtual bool Eq(wxVariantData& data) const;
394 virtual bool Read(wxString& str);
395#if wxUSE_STD_IOSTREAM
396 virtual bool Write(wxSTD ostream& str) const;
397#endif
398 virtual bool Write(wxString& str) const;
399#if wxUSE_STD_IOSTREAM
400 virtual bool Read(wxSTD istream& str);
401#endif
402#if wxUSE_STREAMS
403 virtual bool Read(wxInputStream& str);
404 virtual bool Write(wxOutputStream &str) const;
405#endif // wxUSE_STREAMS
406 virtual wxString GetType() const { return wxT("double"); }
407
408 wxVariantData* Clone() const { return new wxVariantDoubleData(m_value); }
409protected:
410 double m_value;
411};
412
413bool wxVariantDoubleData::Eq(wxVariantData& data) const
414{
415 wxASSERT_MSG( (data.GetType() == wxT("double")), wxT("wxVariantDoubleData::Eq: argument mismatch") );
416
417 wxVariantDoubleData& otherData = (wxVariantDoubleData&) data;
418
419 return wxIsSameDouble(otherData.m_value, m_value);
420}
421
422#if wxUSE_STD_IOSTREAM
423bool wxVariantDoubleData::Write(wxSTD ostream& str) const
424{
425 wxString s;
426 Write(s);
427 str << (const char*) s.mb_str();
428 return true;
429}
430#endif
431
432bool wxVariantDoubleData::Write(wxString& str) const
433{
434 str.Printf(wxT("%.14g"), m_value);
435 return true;
436}
437
438#if wxUSE_STD_IOSTREAM
439bool wxVariantDoubleData::Read(wxSTD istream& str)
440{
441 str >> m_value;
442 return true;
443}
444#endif
445
446#if wxUSE_STREAMS
447bool wxVariantDoubleData::Write(wxOutputStream& str) const
448{
449 wxTextOutputStream s(str);
450 s.WriteDouble((double)m_value);
451 return true;
452}
453
454bool wxVariantDoubleData::Read(wxInputStream& str)
455{
456 wxTextInputStream s(str);
457 m_value = (float)s.ReadDouble();
458 return true;
459}
460#endif // wxUSE_STREAMS
461
462bool wxVariantDoubleData::Read(wxString& str)
463{
464 m_value = wxAtof(str);
465 return true;
466}
467
468// wxVariant double code
469
470wxVariant::wxVariant(double val, const wxString& name)
471{
472 m_data = new wxVariantDoubleData(val);
473 m_name = name;
474}
475
476bool wxVariant::operator== (double value) const
477{
478 double thisValue;
479 if (!Convert(&thisValue))
480 return false;
481
482 return wxIsSameDouble(value, thisValue);
483}
484
485bool wxVariant::operator!= (double value) const
486{
487 return (!((*this) == value));
488}
489
490void wxVariant::operator= (double value)
491{
492 if (GetType() == wxT("double") &&
493 m_data->GetRefCount() == 1)
494 {
495 ((wxVariantDoubleData*)GetData())->SetValue(value);
496 }
497 else
498 {
499 UnRef();
500 m_data = new wxVariantDoubleData(value);
501 }
502}
503
504double wxVariant::GetDouble() const
505{
506 double value;
507 if (Convert(& value))
508 return value;
509 else
510 {
511 wxFAIL_MSG(wxT("Could not convert to a double number"));
512 return 0.0;
513 }
514}
515
516// -----------------------------------------------------------------
517// wxVariantBoolData
518// -----------------------------------------------------------------
519
520#ifdef HAVE_BOOL
521
522class WXDLLIMPEXP_BASE wxVariantDataBool: public wxVariantData
523{
524public:
525 wxVariantDataBool() { m_value = 0; }
526 wxVariantDataBool(bool value) { m_value = value; }
527
528 inline bool GetValue() const { return m_value; }
529 inline void SetValue(bool value) { m_value = value; }
530
531 virtual bool Eq(wxVariantData& data) const;
532#if wxUSE_STD_IOSTREAM
533 virtual bool Write(wxSTD ostream& str) const;
534#endif
535 virtual bool Write(wxString& str) const;
536 virtual bool Read(wxString& str);
537#if wxUSE_STD_IOSTREAM
538 virtual bool Read(wxSTD istream& str);
539#endif
540#if wxUSE_STREAMS
541 virtual bool Read(wxInputStream& str);
542 virtual bool Write(wxOutputStream& str) const;
543#endif // wxUSE_STREAMS
544 virtual wxString GetType() const { return wxT("bool"); }
545
546 wxVariantData* Clone() const { return new wxVariantDataBool(m_value); }
547protected:
548 bool m_value;
549};
550
551bool wxVariantDataBool::Eq(wxVariantData& data) const
552{
553 wxASSERT_MSG( (data.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") );
554
555 wxVariantDataBool& otherData = (wxVariantDataBool&) data;
556
557 return (otherData.m_value == m_value);
558}
559
560#if wxUSE_STD_IOSTREAM
561bool wxVariantDataBool::Write(wxSTD ostream& str) const
562{
563 wxString s;
564 Write(s);
565 str << (const char*) s.mb_str();
566 return true;
567}
568#endif
569
570bool wxVariantDataBool::Write(wxString& str) const
571{
572 str.Printf(wxT("%d"), (int) m_value);
573 return true;
574}
575
576#if wxUSE_STD_IOSTREAM
577bool wxVariantDataBool::Read(wxSTD istream& WXUNUSED(str))
578{
579 wxFAIL_MSG(wxT("Unimplemented"));
580// str >> (long) m_value;
581 return false;
582}
583#endif
584
585#if wxUSE_STREAMS
586bool wxVariantDataBool::Write(wxOutputStream& str) const
587{
588 wxTextOutputStream s(str);
589
590 s.Write8(m_value);
591 return true;
592}
593
594bool wxVariantDataBool::Read(wxInputStream& str)
595{
596 wxTextInputStream s(str);
597
598 m_value = s.Read8() != 0;
599 return true;
600}
601#endif // wxUSE_STREAMS
602
603bool wxVariantDataBool::Read(wxString& str)
604{
605 m_value = (wxAtol(str) != 0);
606 return true;
607}
608
609// wxVariant ****
610
611wxVariant::wxVariant(bool val, const wxString& name)
612{
613 m_data = new wxVariantDataBool(val);
614 m_name = name;
615}
616
617bool wxVariant::operator== (bool value) const
618{
619 bool thisValue;
620 if (!Convert(&thisValue))
621 return false;
622 else
623 return (value == thisValue);
624}
625
626bool wxVariant::operator!= (bool value) const
627{
628 return (!((*this) == value));
629}
630
631void wxVariant::operator= (bool value)
632{
633 if (GetType() == wxT("bool") &&
634 m_data->GetRefCount() == 1)
635 {
636 ((wxVariantDataBool*)GetData())->SetValue(value);
637 }
638 else
639 {
640 UnRef();
641 m_data = new wxVariantDataBool(value);
642 }
643}
644
645bool wxVariant::GetBool() const
646{
647 bool value;
648 if (Convert(& value))
649 return value;
650 else
651 {
652 wxFAIL_MSG(wxT("Could not convert to a bool"));
653 return 0;
654 }
655}
656
657#endif // HAVE_BOOL
658
659// -----------------------------------------------------------------
660// wxVariantDataChar
661// -----------------------------------------------------------------
662
663class WXDLLIMPEXP_BASE wxVariantDataChar: public wxVariantData
664{
665public:
666 wxVariantDataChar() { m_value = 0; }
667 wxVariantDataChar(const wxUniChar& value) { m_value = value; }
668
669 inline wxUniChar GetValue() const { return m_value; }
670 inline void SetValue(const wxUniChar& value) { m_value = value; }
671
672 virtual bool Eq(wxVariantData& data) const;
673#if wxUSE_STD_IOSTREAM
674 virtual bool Read(wxSTD istream& str);
675 virtual bool Write(wxSTD ostream& str) const;
676#endif
677 virtual bool Read(wxString& str);
678 virtual bool Write(wxString& str) const;
679#if wxUSE_STREAMS
680 virtual bool Read(wxInputStream& str);
681 virtual bool Write(wxOutputStream& str) const;
682#endif // wxUSE_STREAMS
683 virtual wxString GetType() const { return wxT("char"); }
684 wxVariantData* Clone() const { return new wxVariantDataChar(m_value); }
685
686protected:
687 wxUniChar m_value;
688};
689
690bool wxVariantDataChar::Eq(wxVariantData& data) const
691{
692 wxASSERT_MSG( (data.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") );
693
694 wxVariantDataChar& otherData = (wxVariantDataChar&) data;
695
696 return (otherData.m_value == m_value);
697}
698
699#if wxUSE_STD_IOSTREAM
700bool wxVariantDataChar::Write(wxSTD ostream& str) const
701{
702 str << wxString(m_value);
703 return true;
704}
705#endif
706
707bool wxVariantDataChar::Write(wxString& str) const
708{
709 str = m_value;
710 return true;
711}
712
713#if wxUSE_STD_IOSTREAM
714bool wxVariantDataChar::Read(wxSTD istream& WXUNUSED(str))
715{
716 wxFAIL_MSG(wxT("Unimplemented"));
717
718 return false;
719}
720#endif
721
722#if wxUSE_STREAMS
723bool wxVariantDataChar::Write(wxOutputStream& str) const
724{
725 wxTextOutputStream s(str);
726
727 // FIXME-UTF8: this should be just "s << m_value;" after removal of
728 // ANSI build and addition of wxUniChar to wxTextOutputStream:
729 s << (wxChar)m_value;
730
731 return true;
732}
733
734bool wxVariantDataChar::Read(wxInputStream& str)
735{
736 wxTextInputStream s(str);
737
738 // FIXME-UTF8: this should be just "s >> m_value;" after removal of
739 // ANSI build and addition of wxUniChar to wxTextInputStream:
740 wxChar ch;
741 s >> ch;
742 m_value = ch;
743
744 return true;
745}
746#endif // wxUSE_STREAMS
747
748bool wxVariantDataChar::Read(wxString& str)
749{
750 m_value = str[0u];
751 return true;
752}
753
754wxVariant::wxVariant(const wxUniChar& val, const wxString& name)
755{
756 m_data = new wxVariantDataChar(val);
757 m_name = name;
758}
759
760wxVariant::wxVariant(char val, const wxString& name)
761{
762 m_data = new wxVariantDataChar(val);
763 m_name = name;
764}
765
766wxVariant::wxVariant(wchar_t val, const wxString& name)
767{
768 m_data = new wxVariantDataChar(val);
769 m_name = name;
770}
771
772bool wxVariant::operator==(const wxUniChar& value) const
773{
774 wxUniChar thisValue;
775 if (!Convert(&thisValue))
776 return false;
777 else
778 return (value == thisValue);
779}
780
781wxVariant& wxVariant::operator=(const wxUniChar& value)
782{
783 if (GetType() == wxT("char") &&
784 m_data->GetRefCount() == 1)
785 {
786 ((wxVariantDataChar*)GetData())->SetValue(value);
787 }
788 else
789 {
790 UnRef();
791 m_data = new wxVariantDataChar(value);
792 }
793
794 return *this;
795}
796
797wxUniChar wxVariant::GetChar() const
798{
799 wxUniChar value;
800 if (Convert(& value))
801 return value;
802 else
803 {
804 wxFAIL_MSG(wxT("Could not convert to a char"));
805 return wxUniChar(0);
806 }
807}
808
809// ----------------------------------------------------------------------------
810// wxVariantDataString
811// ----------------------------------------------------------------------------
812
813class WXDLLIMPEXP_BASE wxVariantDataString: public wxVariantData
814{
815public:
816 wxVariantDataString() { }
817 wxVariantDataString(const wxString& value) { m_value = value; }
818
819 inline wxString GetValue() const { return m_value; }
820 inline void SetValue(const wxString& value) { m_value = value; }
821
822 virtual bool Eq(wxVariantData& data) const;
823#if wxUSE_STD_IOSTREAM
824 virtual bool Write(wxSTD ostream& str) const;
825#endif
826 virtual bool Read(wxString& str);
827 virtual bool Write(wxString& str) const;
828#if wxUSE_STD_IOSTREAM
829 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
830#endif
831#if wxUSE_STREAMS
832 virtual bool Read(wxInputStream& str);
833 virtual bool Write(wxOutputStream& str) const;
834#endif // wxUSE_STREAMS
835 virtual wxString GetType() const { return wxT("string"); }
836 wxVariantData* Clone() const { return new wxVariantDataString(m_value); }
837
838protected:
839 wxString m_value;
840};
841
842bool wxVariantDataString::Eq(wxVariantData& data) const
843{
844 wxASSERT_MSG( (data.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") );
845
846 wxVariantDataString& otherData = (wxVariantDataString&) data;
847
848 return (otherData.m_value == m_value);
849}
850
851#if wxUSE_STD_IOSTREAM
852bool wxVariantDataString::Write(wxSTD ostream& str) const
853{
854 str << (const char*) m_value.mb_str();
855 return true;
856}
857#endif
858
859bool wxVariantDataString::Write(wxString& str) const
860{
861 str = m_value;
862 return true;
863}
864
865#if wxUSE_STREAMS
866bool wxVariantDataString::Write(wxOutputStream& str) const
867{
868 // why doesn't wxOutputStream::operator<< take "const wxString&"
869 wxTextOutputStream s(str);
870 s.WriteString(m_value);
871 return true;
872}
873
874bool wxVariantDataString::Read(wxInputStream& str)
875{
876 wxTextInputStream s(str);
877
878 m_value = s.ReadLine();
879 return true;
880}
881#endif // wxUSE_STREAMS
882
883bool wxVariantDataString::Read(wxString& str)
884{
885 m_value = str;
886 return true;
887}
888
889// wxVariant ****
890
891wxVariant::wxVariant(const wxString& val, const wxString& name)
892{
893 m_data = new wxVariantDataString(val);
894 m_name = name;
895}
896
897wxVariant::wxVariant(const char* val, const wxString& name)
898{
899 m_data = new wxVariantDataString(wxString(val));
900 m_name = name;
901}
902
903wxVariant::wxVariant(const wchar_t* val, const wxString& name)
904{
905 m_data = new wxVariantDataString(wxString(val));
906 m_name = name;
907}
908
909wxVariant::wxVariant(const wxCStrData& val, const wxString& name)
910{
911 m_data = new wxVariantDataString(val.AsString());
912 m_name = name;
913}
914
915wxVariant::wxVariant(const wxCharBuffer& val, const wxString& name)
916{
917 m_data = new wxVariantDataString(wxString(val));
918 m_name = name;
919}
920
921wxVariant::wxVariant(const wxWCharBuffer& val, const wxString& name)
922{
923 m_data = new wxVariantDataString(wxString(val));
924 m_name = name;
925}
926
927bool wxVariant::operator== (const wxString& value) const
928{
929 wxString thisValue;
930 if (!Convert(&thisValue))
931 return false;
932
933 return value == thisValue;
934}
935
936bool wxVariant::operator!= (const wxString& value) const
937{
938 return (!((*this) == value));
939}
940
941wxVariant& wxVariant::operator= (const wxString& value)
942{
943 if (GetType() == wxT("string") &&
944 m_data->GetRefCount() == 1)
945 {
946 ((wxVariantDataString*)GetData())->SetValue(value);
947 }
948 else
949 {
950 UnRef();
951 m_data = new wxVariantDataString(value);
952 }
953 return *this;
954}
955
956wxString wxVariant::GetString() const
957{
958 wxString value;
959 if (!Convert(& value))
960 {
961 wxFAIL_MSG(wxT("Could not convert to a string"));
962 }
963
964 return value;
965}
966
967// ----------------------------------------------------------------------------
968// wxVariantDataWxObjectPtr
969// ----------------------------------------------------------------------------
970
971class wxVariantDataWxObjectPtr: public wxVariantData
972{
973public:
974 wxVariantDataWxObjectPtr() { }
975 wxVariantDataWxObjectPtr(wxObject* value) { m_value = value; }
976
977 inline wxObject* GetValue() const { return m_value; }
978 inline void SetValue(wxObject* value) { m_value = value; }
979
980 virtual bool Eq(wxVariantData& data) const;
981#if wxUSE_STD_IOSTREAM
982 virtual bool Write(wxSTD ostream& str) const;
983#endif
984 virtual bool Write(wxString& str) const;
985#if wxUSE_STD_IOSTREAM
986 virtual bool Read(wxSTD istream& str);
987#endif
988 virtual bool Read(wxString& str);
989 virtual wxString GetType() const ;
990 virtual wxVariantData* Clone() const { return new wxVariantDataWxObjectPtr(m_value); }
991
992 virtual wxClassInfo* GetValueClassInfo();
993
994protected:
995 wxObject* m_value;
996};
997
998bool wxVariantDataWxObjectPtr::Eq(wxVariantData& data) const
999{
1000 wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") );
1001
1002 wxVariantDataWxObjectPtr& otherData = (wxVariantDataWxObjectPtr&) data;
1003
1004 return (otherData.m_value == m_value);
1005}
1006
1007wxString wxVariantDataWxObjectPtr::GetType() const
1008{
1009 wxString returnVal(wxT("wxObject*"));
1010
1011 if (m_value)
1012 {
1013 returnVal = m_value->GetClassInfo()->GetClassName();
1014 returnVal += wxT("*");
1015 }
1016
1017 return returnVal;
1018}
1019
1020wxClassInfo* wxVariantDataWxObjectPtr::GetValueClassInfo()
1021{
1022 wxClassInfo* returnVal=NULL;
1023
1024 if (m_value) returnVal = m_value->GetClassInfo();
1025
1026 return returnVal;
1027}
1028
1029#if wxUSE_STD_IOSTREAM
1030bool wxVariantDataWxObjectPtr::Write(wxSTD ostream& str) const
1031{
1032 wxString s;
1033 Write(s);
1034 str << (const char*) s.mb_str();
1035 return true;
1036}
1037#endif
1038
1039bool wxVariantDataWxObjectPtr::Write(wxString& str) const
1040{
1041 str.Printf(wxT("%s(%p)"), GetType().c_str(), wx_static_cast(void*, m_value));
1042 return true;
1043}
1044
1045#if wxUSE_STD_IOSTREAM
1046bool wxVariantDataWxObjectPtr::Read(wxSTD istream& WXUNUSED(str))
1047{
1048 // Not implemented
1049 return false;
1050}
1051#endif
1052
1053bool wxVariantDataWxObjectPtr::Read(wxString& WXUNUSED(str))
1054{
1055 // Not implemented
1056 return false;
1057}
1058
1059// wxVariant
1060
1061wxVariant::wxVariant( wxObject* val, const wxString& name)
1062{
1063 m_data = new wxVariantDataWxObjectPtr(val);
1064 m_name = name;
1065}
1066
1067bool wxVariant::operator== (wxObject* value) const
1068{
1069 return (value == ((wxVariantDataWxObjectPtr*)GetData())->GetValue());
1070}
1071
1072bool wxVariant::operator!= (wxObject* value) const
1073{
1074 return (!((*this) == (wxObject*) value));
1075}
1076
1077void wxVariant::operator= (wxObject* value)
1078{
1079 UnRef();
1080 m_data = new wxVariantDataWxObjectPtr(value);
1081}
1082
1083wxObject* wxVariant::GetWxObjectPtr() const
1084{
1085 return (wxObject*) ((wxVariantDataWxObjectPtr*) m_data)->GetValue();
1086}
1087
1088// ----------------------------------------------------------------------------
1089// wxVariantDataVoidPtr
1090// ----------------------------------------------------------------------------
1091
1092class wxVariantDataVoidPtr: public wxVariantData
1093{
1094public:
1095 wxVariantDataVoidPtr() { }
1096 wxVariantDataVoidPtr(void* value) { m_value = value; }
1097
1098 inline void* GetValue() const { return m_value; }
1099 inline void SetValue(void* value) { m_value = value; }
1100
1101 virtual bool Eq(wxVariantData& data) const;
1102#if wxUSE_STD_IOSTREAM
1103 virtual bool Write(wxSTD ostream& str) const;
1104#endif
1105 virtual bool Write(wxString& str) const;
1106#if wxUSE_STD_IOSTREAM
1107 virtual bool Read(wxSTD istream& str);
1108#endif
1109 virtual bool Read(wxString& str);
1110 virtual wxString GetType() const { return wxT("void*"); }
1111 virtual wxVariantData* Clone() const { return new wxVariantDataVoidPtr(m_value); }
1112
1113protected:
1114 void* m_value;
1115};
1116
1117bool wxVariantDataVoidPtr::Eq(wxVariantData& data) const
1118{
1119 wxASSERT_MSG( data.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") );
1120
1121 wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data;
1122
1123 return (otherData.m_value == m_value);
1124}
1125
1126#if wxUSE_STD_IOSTREAM
1127bool wxVariantDataVoidPtr::Write(wxSTD ostream& str) const
1128{
1129 wxString s;
1130 Write(s);
1131 str << (const char*) s.mb_str();
1132 return true;
1133}
1134#endif
1135
1136bool wxVariantDataVoidPtr::Write(wxString& str) const
1137{
1138 str.Printf(wxT("%p"), m_value);
1139 return true;
1140}
1141
1142#if wxUSE_STD_IOSTREAM
1143bool wxVariantDataVoidPtr::Read(wxSTD istream& WXUNUSED(str))
1144{
1145 // Not implemented
1146 return false;
1147}
1148#endif
1149
1150bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str))
1151{
1152 // Not implemented
1153 return false;
1154}
1155
1156// wxVariant
1157
1158wxVariant::wxVariant( void* val, const wxString& name)
1159{
1160 m_data = new wxVariantDataVoidPtr(val);
1161 m_name = name;
1162}
1163
1164bool wxVariant::operator== (void* value) const
1165{
1166 return (value == ((wxVariantDataVoidPtr*)GetData())->GetValue());
1167}
1168
1169bool wxVariant::operator!= (void* value) const
1170{
1171 return (!((*this) == (void*) value));
1172}
1173
1174void wxVariant::operator= (void* value)
1175{
1176 if (GetType() == wxT("void*") && (m_data->GetRefCount() == 1))
1177 {
1178 ((wxVariantDataVoidPtr*)GetData())->SetValue(value);
1179 }
1180 else
1181 {
1182 UnRef();
1183 m_data = new wxVariantDataVoidPtr(value);
1184 }
1185}
1186
1187void* wxVariant::GetVoidPtr() const
1188{
1189 wxASSERT( (GetType() == wxT("void*")) );
1190
1191 return (void*) ((wxVariantDataVoidPtr*) m_data)->GetValue();
1192}
1193
1194// ----------------------------------------------------------------------------
1195// wxVariantDataDateTime
1196// ----------------------------------------------------------------------------
1197
1198#if wxUSE_DATETIME
1199
1200class wxVariantDataDateTime: public wxVariantData
1201{
1202public:
1203 wxVariantDataDateTime() { }
1204 wxVariantDataDateTime(const wxDateTime& value) { m_value = value; }
1205
1206 inline wxDateTime GetValue() const { return m_value; }
1207 inline void SetValue(const wxDateTime& value) { m_value = value; }
1208
1209 virtual bool Eq(wxVariantData& data) const;
1210#if wxUSE_STD_IOSTREAM
1211 virtual bool Write(wxSTD ostream& str) const;
1212#endif
1213 virtual bool Write(wxString& str) const;
1214#if wxUSE_STD_IOSTREAM
1215 virtual bool Read(wxSTD istream& str);
1216#endif
1217 virtual bool Read(wxString& str);
1218 virtual wxString GetType() const { return wxT("datetime"); }
1219 virtual wxVariantData* Clone() const { return new wxVariantDataDateTime(m_value); }
1220
1221protected:
1222 wxDateTime m_value;
1223};
1224
1225
1226bool wxVariantDataDateTime::Eq(wxVariantData& data) const
1227{
1228 wxASSERT_MSG( (data.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") );
1229
1230 wxVariantDataDateTime& otherData = (wxVariantDataDateTime&) data;
1231
1232 return (otherData.m_value == m_value);
1233}
1234
1235
1236#if wxUSE_STD_IOSTREAM
1237bool wxVariantDataDateTime::Write(wxSTD ostream& str) const
1238{
1239 wxString value;
1240 Write( value );
1241 str << value.c_str();
1242 return true;
1243}
1244#endif
1245
1246
1247bool wxVariantDataDateTime::Write(wxString& str) const
1248{
1249 str = m_value.Format();
1250 return true;
1251}
1252
1253
1254#if wxUSE_STD_IOSTREAM
1255bool wxVariantDataDateTime::Read(wxSTD istream& WXUNUSED(str))
1256{
1257 // Not implemented
1258 return false;
1259}
1260#endif
1261
1262
1263bool wxVariantDataDateTime::Read(wxString& str)
1264{
1265 if(! m_value.ParseDateTime(str.c_str()/*FIXME-UTF8*/))
1266 return false;
1267 return true;
1268}
1269
1270// wxVariant
1271
1272wxVariant::wxVariant(const wxDateTime& val, const wxString& name) // Date
1273{
1274 m_data = new wxVariantDataDateTime(val);
1275 m_name = name;
1276}
1277
1278bool wxVariant::operator== (const wxDateTime& value) const
1279{
1280 wxDateTime thisValue;
1281 if (!Convert(&thisValue))
1282 return false;
1283
1284 return value.IsEqualTo(thisValue);
1285}
1286
1287bool wxVariant::operator!= (const wxDateTime& value) const
1288{
1289 return (!((*this) == value));
1290}
1291
1292void wxVariant::operator= (const wxDateTime& value)
1293{
1294 if (GetType() == wxT("datetime") &&
1295 m_data->GetRefCount() == 1)
1296 {
1297 ((wxVariantDataDateTime*)GetData())->SetValue(value);
1298 }
1299 else
1300 {
1301 UnRef();
1302 m_data = new wxVariantDataDateTime(value);
1303 }
1304}
1305
1306wxDateTime wxVariant::GetDateTime() const
1307{
1308 wxDateTime value;
1309 if (!Convert(& value))
1310 {
1311 wxFAIL_MSG(wxT("Could not convert to a datetime"));
1312 }
1313
1314 return value;
1315}
1316
1317#endif // wxUSE_DATETIME
1318
1319// ----------------------------------------------------------------------------
1320// wxVariantDataArrayString
1321// ----------------------------------------------------------------------------
1322
1323class wxVariantDataArrayString: public wxVariantData
1324{
1325public:
1326 wxVariantDataArrayString() { }
1327 wxVariantDataArrayString(const wxArrayString& value) { m_value = value; }
1328
1329 wxArrayString GetValue() const { return m_value; }
1330 void SetValue(const wxArrayString& value) { m_value = value; }
1331
1332 virtual bool Eq(wxVariantData& data) const;
1333#if wxUSE_STD_IOSTREAM
1334 virtual bool Write(wxSTD ostream& str) const;
1335#endif
1336 virtual bool Write(wxString& str) const;
1337#if wxUSE_STD_IOSTREAM
1338 virtual bool Read(wxSTD istream& str);
1339#endif
1340 virtual bool Read(wxString& str);
1341 virtual wxString GetType() const { return wxT("arrstring"); }
1342 virtual wxVariantData* Clone() const { return new wxVariantDataArrayString(m_value); }
1343
1344protected:
1345 wxArrayString m_value;
1346};
1347
1348bool wxVariantDataArrayString::Eq(wxVariantData& data) const
1349{
1350 wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") );
1351
1352 wxVariantDataArrayString& otherData = (wxVariantDataArrayString&) data;
1353
1354 return otherData.m_value == m_value;
1355}
1356
1357#if wxUSE_STD_IOSTREAM
1358bool wxVariantDataArrayString::Write(wxSTD ostream& WXUNUSED(str)) const
1359{
1360 // Not implemented
1361 return false;
1362}
1363#endif
1364
1365bool wxVariantDataArrayString::Write(wxString& str) const
1366{
1367 size_t count = m_value.GetCount();
1368 for ( size_t n = 0; n < count; n++ )
1369 {
1370 if ( n )
1371 str += _T(';');
1372
1373 str += m_value[n];
1374 }
1375
1376 return true;
1377}
1378
1379
1380#if wxUSE_STD_IOSTREAM
1381bool wxVariantDataArrayString::Read(wxSTD istream& WXUNUSED(str))
1382{
1383 // Not implemented
1384 return false;
1385}
1386#endif
1387
1388
1389bool wxVariantDataArrayString::Read(wxString& str)
1390{
1391 wxStringTokenizer tk(str, _T(";"));
1392 while ( tk.HasMoreTokens() )
1393 {
1394 m_value.Add(tk.GetNextToken());
1395 }
1396
1397 return true;
1398}
1399
1400// wxVariant
1401
1402wxVariant::wxVariant(const wxArrayString& val, const wxString& name) // Strings
1403{
1404 m_data = new wxVariantDataArrayString(val);
1405 m_name = name;
1406}
1407
1408bool wxVariant::operator==(const wxArrayString& WXUNUSED(value)) const
1409{
1410 wxFAIL_MSG( _T("TODO") );
1411
1412 return false;
1413}
1414
1415bool wxVariant::operator!=(const wxArrayString& value) const
1416{
1417 return !(*this == value);
1418}
1419
1420void wxVariant::operator=(const wxArrayString& value)
1421{
1422 if (GetType() == wxT("arrstring") &&
1423 m_data->GetRefCount() == 1)
1424 {
1425 ((wxVariantDataArrayString *)GetData())->SetValue(value);
1426 }
1427 else
1428 {
1429 UnRef();
1430 m_data = new wxVariantDataArrayString(value);
1431 }
1432}
1433
1434wxArrayString wxVariant::GetArrayString() const
1435{
1436 if ( GetType() == wxT("arrstring") )
1437 return ((wxVariantDataArrayString *)GetData())->GetValue();
1438
1439 return wxArrayString();
1440}
1441
1442// ----------------------------------------------------------------------------
1443// wxVariantDataList
1444// ----------------------------------------------------------------------------
1445
1446class WXDLLIMPEXP_BASE wxVariantDataList: public wxVariantData
1447{
1448public:
1449 wxVariantDataList() {}
1450 wxVariantDataList(const wxVariantList& list);
1451 virtual ~wxVariantDataList();
1452
1453 wxVariantList& GetValue() { return m_value; }
1454 void SetValue(const wxVariantList& value) ;
1455
1456 virtual bool Eq(wxVariantData& data) const;
1457#if wxUSE_STD_IOSTREAM
1458 virtual bool Write(wxSTD ostream& str) const;
1459#endif
1460 virtual bool Write(wxString& str) const;
1461#if wxUSE_STD_IOSTREAM
1462 virtual bool Read(wxSTD istream& str);
1463#endif
1464 virtual bool Read(wxString& str);
1465 virtual wxString GetType() const { return wxT("list"); }
1466
1467 void Clear();
1468
1469 wxVariantData* Clone() const { return new wxVariantDataList(m_value); }
1470protected:
1471 wxVariantList m_value;
1472};
1473
1474wxVariantDataList::wxVariantDataList(const wxVariantList& list)
1475{
1476 SetValue(list);
1477}
1478
1479wxVariantDataList::~wxVariantDataList()
1480{
1481 Clear();
1482}
1483
1484void wxVariantDataList::SetValue(const wxVariantList& value)
1485{
1486 Clear();
1487 wxVariantList::compatibility_iterator node = value.GetFirst();
1488 while (node)
1489 {
1490 wxVariant* var = node->GetData();
1491 m_value.Append(new wxVariant(*var));
1492 node = node->GetNext();
1493 }
1494}
1495
1496void wxVariantDataList::Clear()
1497{
1498 wxVariantList::compatibility_iterator node = m_value.GetFirst();
1499 while (node)
1500 {
1501 wxVariant* var = node->GetData();
1502 delete var;
1503 node = node->GetNext();
1504 }
1505 m_value.Clear();
1506}
1507
1508bool wxVariantDataList::Eq(wxVariantData& data) const
1509{
1510 wxASSERT_MSG( (data.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
1511
1512 wxVariantDataList& listData = (wxVariantDataList&) data;
1513 wxVariantList::compatibility_iterator node1 = m_value.GetFirst();
1514 wxVariantList::compatibility_iterator node2 = listData.GetValue().GetFirst();
1515 while (node1 && node2)
1516 {
1517 wxVariant* var1 = node1->GetData();
1518 wxVariant* var2 = node2->GetData();
1519 if ((*var1) != (*var2))
1520 return false;
1521 node1 = node1->GetNext();
1522 node2 = node2->GetNext();
1523 }
1524 if (node1 || node2) return false;
1525 return true;
1526}
1527
1528#if wxUSE_STD_IOSTREAM
1529bool wxVariantDataList::Write(wxSTD ostream& str) const
1530{
1531 wxString s;
1532 Write(s);
1533 str << (const char*) s.mb_str();
1534 return true;
1535}
1536#endif
1537
1538bool wxVariantDataList::Write(wxString& str) const
1539{
1540 str = wxEmptyString;
1541 wxVariantList::compatibility_iterator node = m_value.GetFirst();
1542 while (node)
1543 {
1544 wxVariant* var = node->GetData();
1545 if (node != m_value.GetFirst())
1546 str += wxT(" ");
1547 wxString str1;
1548 str += var->MakeString();
1549 node = node->GetNext();
1550 }
1551
1552 return true;
1553}
1554
1555#if wxUSE_STD_IOSTREAM
1556bool wxVariantDataList::Read(wxSTD istream& WXUNUSED(str))
1557{
1558 wxFAIL_MSG(wxT("Unimplemented"));
1559 // TODO
1560 return false;
1561}
1562#endif
1563
1564bool wxVariantDataList::Read(wxString& WXUNUSED(str))
1565{
1566 wxFAIL_MSG(wxT("Unimplemented"));
1567 // TODO
1568 return false;
1569}
1570
1571// wxVariant
1572
1573wxVariant::wxVariant(const wxVariantList& val, const wxString& name) // List of variants
1574{
1575 m_data = new wxVariantDataList(val);
1576 m_name = name;
1577}
1578
1579bool wxVariant::operator== (const wxVariantList& value) const
1580{
1581 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
1582
1583 wxVariantDataList other(value);
1584 return (GetData()->Eq(other));
1585}
1586
1587bool wxVariant::operator!= (const wxVariantList& value) const
1588{
1589 return (!((*this) == value));
1590}
1591
1592void wxVariant::operator= (const wxVariantList& value)
1593{
1594 if (GetType() == wxT("list") &&
1595 m_data->GetRefCount() == 1)
1596 {
1597 ((wxVariantDataList*)GetData())->SetValue(value);
1598 }
1599 else
1600 {
1601 UnRef();
1602 m_data = new wxVariantDataList(value);
1603 }
1604}
1605
1606wxVariantList& wxVariant::GetList() const
1607{
1608 wxASSERT( (GetType() == wxT("list")) );
1609
1610 return (wxVariantList&) ((wxVariantDataList*) m_data)->GetValue();
1611}
1612
1613// Make empty list
1614void wxVariant::NullList()
1615{
1616 SetData(new wxVariantDataList());
1617}
1618
1619// Append to list
1620void wxVariant::Append(const wxVariant& value)
1621{
1622 wxVariantList& list = GetList();
1623
1624 list.Append(new wxVariant(value));
1625}
1626
1627// Insert at front of list
1628void wxVariant::Insert(const wxVariant& value)
1629{
1630 wxVariantList& list = GetList();
1631
1632 list.Insert(new wxVariant(value));
1633}
1634
1635// Returns true if the variant is a member of the list
1636bool wxVariant::Member(const wxVariant& value) const
1637{
1638 wxVariantList& list = GetList();
1639
1640 wxVariantList::compatibility_iterator node = list.GetFirst();
1641 while (node)
1642 {
1643 wxVariant* other = node->GetData();
1644 if (value == *other)
1645 return true;
1646 node = node->GetNext();
1647 }
1648 return false;
1649}
1650
1651// Deletes the nth element of the list
1652bool wxVariant::Delete(size_t item)
1653{
1654 wxVariantList& list = GetList();
1655
1656 wxASSERT_MSG( (item < list.GetCount()), wxT("Invalid index to Delete") );
1657 wxVariantList::compatibility_iterator node = list.Item(item);
1658 wxVariant* variant = node->GetData();
1659 delete variant;
1660 list.Erase(node);
1661 return true;
1662}
1663
1664// Clear list
1665void wxVariant::ClearList()
1666{
1667 if (!IsNull() && (GetType() == wxT("list")))
1668 {
1669 ((wxVariantDataList*) m_data)->Clear();
1670 }
1671 else
1672 {
1673 if (!GetType().IsSameAs(wxT("list")))
1674 UnRef();
1675
1676 m_data = new wxVariantDataList;
1677 }
1678}
1679
1680// Treat a list variant as an array
1681wxVariant wxVariant::operator[] (size_t idx) const
1682{
1683 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
1684
1685 if (GetType() == wxT("list"))
1686 {
1687 wxVariantDataList* data = (wxVariantDataList*) m_data;
1688 wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") );
1689 return *(data->GetValue().Item(idx)->GetData());
1690 }
1691 return wxNullVariant;
1692}
1693
1694wxVariant& wxVariant::operator[] (size_t idx)
1695{
1696 // We can't return a reference to a variant for a string list, since the string
1697 // is actually stored as a char*, not a variant.
1698
1699 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1700
1701 wxVariantDataList* data = (wxVariantDataList*) m_data;
1702 wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") );
1703
1704 return * (data->GetValue().Item(idx)->GetData());
1705}
1706
1707// Return the number of elements in a list
1708size_t wxVariant::GetCount() const
1709{
1710 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
1711
1712 if (GetType() == wxT("list"))
1713 {
1714 wxVariantDataList* data = (wxVariantDataList*) m_data;
1715 return data->GetValue().GetCount();
1716 }
1717 return 0;
1718}
1719
1720// ----------------------------------------------------------------------------
1721// Type conversion
1722// ----------------------------------------------------------------------------
1723
1724bool wxVariant::Convert(long* value) const
1725{
1726 wxString type(GetType());
1727 if (type == wxT("double"))
1728 *value = (long) (((wxVariantDoubleData*)GetData())->GetValue());
1729 else if (type == wxT("long"))
1730 *value = ((wxVariantDataLong*)GetData())->GetValue();
1731#ifdef HAVE_BOOL
1732 else if (type == wxT("bool"))
1733 *value = (long) (((wxVariantDataBool*)GetData())->GetValue());
1734#endif
1735 else if (type == wxT("string"))
1736 *value = wxAtol(((wxVariantDataString*)GetData())->GetValue());
1737 else
1738 return false;
1739
1740 return true;
1741}
1742
1743bool wxVariant::Convert(bool* value) const
1744{
1745 wxString type(GetType());
1746 if (type == wxT("double"))
1747 *value = ((int) (((wxVariantDoubleData*)GetData())->GetValue()) != 0);
1748 else if (type == wxT("long"))
1749 *value = (((wxVariantDataLong*)GetData())->GetValue() != 0);
1750#ifdef HAVE_BOOL
1751 else if (type == wxT("bool"))
1752 *value = ((wxVariantDataBool*)GetData())->GetValue();
1753#endif
1754 else if (type == wxT("string"))
1755 {
1756 wxString val(((wxVariantDataString*)GetData())->GetValue());
1757 val.MakeLower();
1758 if (val == wxT("true") || val == wxT("yes") || val == wxT('1') )
1759 *value = true;
1760 else if (val == wxT("false") || val == wxT("no") || val == wxT('0') )
1761 *value = false;
1762 else
1763 return false;
1764 }
1765 else
1766 return false;
1767
1768 return true;
1769}
1770
1771bool wxVariant::Convert(double* value) const
1772{
1773 wxString type(GetType());
1774 if (type == wxT("double"))
1775 *value = ((wxVariantDoubleData*)GetData())->GetValue();
1776 else if (type == wxT("long"))
1777 *value = (double) (((wxVariantDataLong*)GetData())->GetValue());
1778#ifdef HAVE_BOOL
1779 else if (type == wxT("bool"))
1780 *value = (double) (((wxVariantDataBool*)GetData())->GetValue());
1781#endif
1782 else if (type == wxT("string"))
1783 *value = (double) wxAtof(((wxVariantDataString*)GetData())->GetValue());
1784 else
1785 return false;
1786
1787 return true;
1788}
1789
1790bool wxVariant::Convert(wxUniChar* value) const
1791{
1792 wxString type(GetType());
1793 if (type == wxT("char"))
1794 *value = ((wxVariantDataChar*)GetData())->GetValue();
1795 else if (type == wxT("long"))
1796 *value = (char) (((wxVariantDataLong*)GetData())->GetValue());
1797#ifdef HAVE_BOOL
1798 else if (type == wxT("bool"))
1799 *value = (char) (((wxVariantDataBool*)GetData())->GetValue());
1800#endif
1801 else
1802 return false;
1803
1804 return true;
1805}
1806
1807bool wxVariant::Convert(char* value) const
1808{
1809 wxUniChar ch;
1810 if ( !Convert(&ch) )
1811 return false;
1812 *value = ch;
1813 return true;
1814}
1815
1816bool wxVariant::Convert(wchar_t* value) const
1817{
1818 wxUniChar ch;
1819 if ( !Convert(&ch) )
1820 return false;
1821 *value = ch;
1822 return true;
1823}
1824
1825bool wxVariant::Convert(wxString* value) const
1826{
1827 *value = MakeString();
1828 return true;
1829}
1830
1831#if wxUSE_DATETIME
1832bool wxVariant::Convert(wxDateTime* value) const
1833{
1834 wxString type(GetType());
1835 if (type == wxT("datetime"))
1836 {
1837 *value = ((wxVariantDataDateTime*)GetData())->GetValue();
1838 return true;
1839 }
1840 // Fallback to string conversion
1841 wxString val;
1842 return Convert(&val) &&
1843 (value->ParseDateTime(val.c_str()/*FIXME-UTF8*/) ||
1844 value->ParseDate(val.c_str()/*FIXME-UTF8*/) ||
1845 value->ParseTime(val.c_str()/*FIXME-UTF8*/));
1846}
1847#endif // wxUSE_DATETIME
1848
1849#endif // wxUSE_VARIANT