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