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