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