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