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