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