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