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