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