/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/common/variant.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/common/variant.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22