]> git.saurik.com Git - wxWidgets.git/blame - src/common/variant.cpp
Committing modified version of jwiesemann's patch (see #11223):
[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 1345 if ( n )
9a83f860 1346 str += wxT(';');
2562c823
RR
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{
9a83f860 1366 wxStringTokenizer tk(str, wxT(";"));
2562c823 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{
9a83f860 1385 wxFAIL_MSG( wxT("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
4e00b908
JS
1417// ----------------------------------------------------------------------------
1418// wxVariantDataLongLong
1419// ----------------------------------------------------------------------------
1420
1421#if wxUSE_LONGLONG
1422
1423class WXDLLIMPEXP_BASE wxVariantDataLongLong : public wxVariantData
1424{
1425public:
1426 wxVariantDataLongLong() { m_value = 0; }
1427 wxVariantDataLongLong(wxLongLong value) { m_value = value; }
1428
1429 wxLongLong GetValue() const { return m_value; }
1430 void SetValue(wxLongLong value) { m_value = value; }
1431
1432 virtual bool Eq(wxVariantData& data) const;
1433
1434 virtual bool Read(wxString& str);
1435 virtual bool Write(wxString& str) const;
1436#if wxUSE_STD_IOSTREAM
1437 virtual bool Read(wxSTD istream& str);
1438 virtual bool Write(wxSTD ostream& str) const;
1439#endif
1440#if wxUSE_STREAMS
1441 virtual bool Read(wxInputStream& str);
1442 virtual bool Write(wxOutputStream &str) const;
1443#endif // wxUSE_STREAMS
1444
1445 wxVariantData* Clone() const
1446 {
1447 return new wxVariantDataLongLong(m_value);
1448 }
1449
1450 virtual wxString GetType() const { return wxS("longlong"); }
1451
1452protected:
1453 wxLongLong m_value;
1454};
1455
1456bool wxVariantDataLongLong::Eq(wxVariantData& data) const
1457{
03647350 1458 wxASSERT_MSG( (data.GetType() == wxS("longlong")),
4e00b908
JS
1459 "wxVariantDataLongLong::Eq: argument mismatch" );
1460
1461 wxVariantDataLongLong& otherData = (wxVariantDataLongLong&) data;
1462
1463 return (otherData.m_value == m_value);
1464}
1465
1466#if wxUSE_STD_IOSTREAM
1467bool wxVariantDataLongLong::Write(wxSTD ostream& str) const
1468{
1469 wxString s;
1470 Write(s);
1471 str << (const char*) s.mb_str();
1472 return true;
1473}
1474#endif
1475
1476bool wxVariantDataLongLong::Write(wxString& str) const
1477{
f81bd288
JS
1478#ifdef wxLongLong_t
1479 str.Printf(wxS("%lld"), m_value.GetValue());
4e00b908 1480 return true;
f81bd288
JS
1481#else
1482 return false;
1483#endif
4e00b908
JS
1484}
1485
1486#if wxUSE_STD_IOSTREAM
1487bool wxVariantDataLongLong::Read(wxSTD istream& WXUNUSED(str))
1488{
1489 wxFAIL_MSG(wxS("Unimplemented"));
1490 return false;
1491}
1492#endif
1493
1494#if wxUSE_STREAMS
1495bool wxVariantDataLongLong::Write(wxOutputStream& str) const
1496{
1497 wxTextOutputStream s(str);
1498 s.Write32(m_value.GetLo());
1499 s.Write32(m_value.GetHi());
1500 return true;
1501}
1502
1503bool wxVariantDataLongLong::Read(wxInputStream& str)
1504{
1505 wxTextInputStream s(str);
1506 unsigned long lo = s.Read32();
1507 long hi = s.Read32();
1508 m_value = wxLongLong(hi, lo);
1509 return true;
1510}
1511#endif // wxUSE_STREAMS
1512
1513bool wxVariantDataLongLong::Read(wxString& str)
1514{
1515#ifdef wxLongLong_t
1516 wxLongLong_t value_t;
1517 if ( !str.ToLongLong(&value_t) )
1518 return false;
1519 m_value = value_t;
1520 return true;
1521#else
1522 return false;
1523#endif
1524}
1525
1526// wxVariant
1527
1528wxVariant::wxVariant(wxLongLong val, const wxString& name)
1529{
1530 m_refData = new wxVariantDataLongLong(val);
1531 m_name = name;
1532}
1533
1534bool wxVariant::operator==(wxLongLong value) const
1535{
1536 wxLongLong thisValue;
1537 if ( !Convert(&thisValue) )
1538 return false;
1539 else
1540 return (value == thisValue);
1541}
1542
1543bool wxVariant::operator!=(wxLongLong value) const
1544{
1545 return (!((*this) == value));
1546}
1547
1548void wxVariant::operator=(wxLongLong value)
1549{
1550 if ( GetType() == wxS("longlong") &&
1551 m_refData->GetRefCount() == 1 )
1552 {
1553 ((wxVariantDataLongLong*)GetData())->SetValue(value);
1554 }
1555 else
1556 {
1557 UnRef();
1558 m_refData = new wxVariantDataLongLong(value);
1559 }
1560}
1561
1562wxLongLong wxVariant::GetLongLong() const
1563{
1564 wxLongLong value;
1565 if ( Convert(&value) )
1566 {
1567 return value;
1568 }
1569 else
1570 {
1571 wxFAIL_MSG(wxT("Could not convert to a long long"));
1572 return 0;
1573 }
1574}
1575
1576#endif // wxUSE_LONGLONG
1577
1578// ----------------------------------------------------------------------------
1579// wxVariantDataULongLong
1580// ----------------------------------------------------------------------------
1581
1582#if wxUSE_LONGLONG
1583
1584class WXDLLIMPEXP_BASE wxVariantDataULongLong : public wxVariantData
1585{
1586public:
1587 wxVariantDataULongLong() { m_value = 0; }
1588 wxVariantDataULongLong(wxULongLong value) { m_value = value; }
1589
1590 wxULongLong GetValue() const { return m_value; }
1591 void SetValue(wxULongLong value) { m_value = value; }
1592
1593 virtual bool Eq(wxVariantData& data) const;
1594
1595 virtual bool Read(wxString& str);
1596 virtual bool Write(wxString& str) const;
1597#if wxUSE_STD_IOSTREAM
1598 virtual bool Read(wxSTD istream& str);
1599 virtual bool Write(wxSTD ostream& str) const;
1600#endif
1601#if wxUSE_STREAMS
1602 virtual bool Read(wxInputStream& str);
1603 virtual bool Write(wxOutputStream &str) const;
1604#endif // wxUSE_STREAMS
1605
1606 wxVariantData* Clone() const
1607 {
1608 return new wxVariantDataULongLong(m_value);
1609 }
1610
1611 virtual wxString GetType() const { return wxS("ulonglong"); }
1612
1613protected:
1614 wxULongLong m_value;
1615};
1616
1617bool wxVariantDataULongLong::Eq(wxVariantData& data) const
1618{
03647350 1619 wxASSERT_MSG( (data.GetType() == wxS("ulonglong")),
4e00b908
JS
1620 "wxVariantDataULongLong::Eq: argument mismatch" );
1621
1622 wxVariantDataULongLong& otherData = (wxVariantDataULongLong&) data;
1623
1624 return (otherData.m_value == m_value);
1625}
1626
1627#if wxUSE_STD_IOSTREAM
1628bool wxVariantDataULongLong::Write(wxSTD ostream& str) const
1629{
1630 wxString s;
1631 Write(s);
1632 str << (const char*) s.mb_str();
1633 return true;
1634}
1635#endif
1636
1637bool wxVariantDataULongLong::Write(wxString& str) const
1638{
f81bd288
JS
1639#ifdef wxLongLong_t
1640 str.Printf(wxS("%llu"), m_value.GetValue());
4e00b908 1641 return true;
f81bd288
JS
1642#else
1643 return false;
1644#endif
4e00b908
JS
1645}
1646
1647#if wxUSE_STD_IOSTREAM
1648bool wxVariantDataULongLong::Read(wxSTD istream& WXUNUSED(str))
1649{
1650 wxFAIL_MSG(wxS("Unimplemented"));
1651 return false;
1652}
1653#endif
1654
1655#if wxUSE_STREAMS
1656bool wxVariantDataULongLong::Write(wxOutputStream& str) const
1657{
1658 wxTextOutputStream s(str);
1659 s.Write32(m_value.GetLo());
1660 s.Write32(m_value.GetHi());
1661 return true;
1662}
1663
1664bool wxVariantDataULongLong::Read(wxInputStream& str)
1665{
1666 wxTextInputStream s(str);
1667 unsigned long lo = s.Read32();
1668 long hi = s.Read32();
1669 m_value = wxULongLong(hi, lo);
1670 return true;
1671}
1672#endif // wxUSE_STREAMS
1673
1674bool wxVariantDataULongLong::Read(wxString& str)
1675{
1676#ifdef wxLongLong_t
1677 wxULongLong_t value_t;
1678 if ( !str.ToULongLong(&value_t) )
1679 return false;
1680 m_value = value_t;
1681 return true;
1682#else
1683 return false;
1684#endif
1685}
1686
1687// wxVariant
1688
1689wxVariant::wxVariant(wxULongLong val, const wxString& name)
1690{
1691 m_refData = new wxVariantDataULongLong(val);
1692 m_name = name;
1693}
1694
1695bool wxVariant::operator==(wxULongLong value) const
1696{
1697 wxULongLong thisValue;
1698 if ( !Convert(&thisValue) )
1699 return false;
1700 else
1701 return (value == thisValue);
1702}
1703
1704bool wxVariant::operator!=(wxULongLong value) const
1705{
1706 return (!((*this) == value));
1707}
1708
1709void wxVariant::operator=(wxULongLong value)
1710{
1711 if ( GetType() == wxS("ulonglong") &&
1712 m_refData->GetRefCount() == 1 )
1713 {
1714 ((wxVariantDataULongLong*)GetData())->SetValue(value);
1715 }
1716 else
1717 {
1718 UnRef();
1719 m_refData = new wxVariantDataULongLong(value);
1720 }
1721}
1722
1723wxULongLong wxVariant::GetULongLong() const
1724{
1725 wxULongLong value;
1726 if ( Convert(&value) )
1727 {
1728 return value;
1729 }
1730 else
1731 {
1732 wxFAIL_MSG(wxT("Could not convert to a long long"));
1733 return 0;
1734 }
1735}
1736
1737#endif // wxUSE_LONGLONG
1738
2562c823
RR
1739// ----------------------------------------------------------------------------
1740// wxVariantDataList
1741// ----------------------------------------------------------------------------
2c3a1064 1742
2562c823 1743class WXDLLIMPEXP_BASE wxVariantDataList: public wxVariantData
341287bf 1744{
2562c823
RR
1745public:
1746 wxVariantDataList() {}
9a0a58f5 1747 wxVariantDataList(const wxVariantList& list);
2562c823 1748 virtual ~wxVariantDataList();
341287bf 1749
9a0a58f5
RR
1750 wxVariantList& GetValue() { return m_value; }
1751 void SetValue(const wxVariantList& value) ;
341287bf 1752
2562c823
RR
1753 virtual bool Eq(wxVariantData& data) const;
1754#if wxUSE_STD_IOSTREAM
1755 virtual bool Write(wxSTD ostream& str) const;
1756#endif
1757 virtual bool Write(wxString& str) const;
1758#if wxUSE_STD_IOSTREAM
1759 virtual bool Read(wxSTD istream& str);
1760#endif
1761 virtual bool Read(wxString& str);
47b378bd 1762 virtual wxString GetType() const { return wxT("list"); }
52e81242 1763
2562c823 1764 void Clear();
341287bf 1765
c8058a09 1766 wxVariantData* Clone() const { return new wxVariantDataList(m_value); }
2562c823 1767protected:
9a0a58f5 1768 wxVariantList m_value;
2562c823 1769};
341287bf 1770
9a0a58f5 1771wxVariantDataList::wxVariantDataList(const wxVariantList& list)
341287bf 1772{
2562c823 1773 SetValue(list);
341287bf
JS
1774}
1775
2562c823 1776wxVariantDataList::~wxVariantDataList()
341287bf 1777{
2562c823 1778 Clear();
341287bf
JS
1779}
1780
9a0a58f5 1781void wxVariantDataList::SetValue(const wxVariantList& value)
341287bf 1782{
2562c823 1783 Clear();
9a0a58f5 1784 wxVariantList::compatibility_iterator node = value.GetFirst();
2562c823 1785 while (node)
341287bf 1786 {
9a0a58f5 1787 wxVariant* var = node->GetData();
2562c823
RR
1788 m_value.Append(new wxVariant(*var));
1789 node = node->GetNext();
341287bf 1790 }
2562c823
RR
1791}
1792
1793void wxVariantDataList::Clear()
1794{
9a0a58f5 1795 wxVariantList::compatibility_iterator node = m_value.GetFirst();
2562c823 1796 while (node)
341287bf 1797 {
9a0a58f5 1798 wxVariant* var = node->GetData();
2562c823
RR
1799 delete var;
1800 node = node->GetNext();
341287bf 1801 }
2562c823 1802 m_value.Clear();
341287bf
JS
1803}
1804
2562c823 1805bool wxVariantDataList::Eq(wxVariantData& data) const
a0a302dc 1806{
2562c823
RR
1807 wxASSERT_MSG( (data.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") );
1808
1809 wxVariantDataList& listData = (wxVariantDataList&) data;
9a0a58f5
RR
1810 wxVariantList::compatibility_iterator node1 = m_value.GetFirst();
1811 wxVariantList::compatibility_iterator node2 = listData.GetValue().GetFirst();
2562c823
RR
1812 while (node1 && node2)
1813 {
9a0a58f5
RR
1814 wxVariant* var1 = node1->GetData();
1815 wxVariant* var2 = node2->GetData();
2562c823
RR
1816 if ((*var1) != (*var2))
1817 return false;
1818 node1 = node1->GetNext();
1819 node2 = node2->GetNext();
1820 }
1821 if (node1 || node2) return false;
1822 return true;
a0a302dc
JS
1823}
1824
2562c823
RR
1825#if wxUSE_STD_IOSTREAM
1826bool wxVariantDataList::Write(wxSTD ostream& str) const
a0a302dc 1827{
2562c823
RR
1828 wxString s;
1829 Write(s);
1830 str << (const char*) s.mb_str();
1831 return true;
a0a302dc 1832}
2562c823 1833#endif
a0a302dc 1834
2562c823 1835bool wxVariantDataList::Write(wxString& str) const
a0a302dc 1836{
2562c823 1837 str = wxEmptyString;
9a0a58f5 1838 wxVariantList::compatibility_iterator node = m_value.GetFirst();
2562c823 1839 while (node)
a0a302dc 1840 {
9a0a58f5 1841 wxVariant* var = node->GetData();
2562c823
RR
1842 if (node != m_value.GetFirst())
1843 str += wxT(" ");
1844 wxString str1;
1845 str += var->MakeString();
1846 node = node->GetNext();
a0a302dc 1847 }
2562c823
RR
1848
1849 return true;
a0a302dc 1850}
2f620946 1851
2562c823
RR
1852#if wxUSE_STD_IOSTREAM
1853bool wxVariantDataList::Read(wxSTD istream& WXUNUSED(str))
2f620946 1854{
2562c823
RR
1855 wxFAIL_MSG(wxT("Unimplemented"));
1856 // TODO
1857 return false;
2f620946 1858}
2562c823 1859#endif
2f620946 1860
2562c823 1861bool wxVariantDataList::Read(wxString& WXUNUSED(str))
2f620946 1862{
2562c823
RR
1863 wxFAIL_MSG(wxT("Unimplemented"));
1864 // TODO
1865 return false;
2f620946
RR
1866}
1867
2562c823
RR
1868// wxVariant
1869
9a0a58f5 1870wxVariant::wxVariant(const wxVariantList& val, const wxString& name) // List of variants
2f620946 1871{
cf25a599 1872 m_refData = new wxVariantDataList(val);
2562c823 1873 m_name = name;
2f620946 1874}
341287bf 1875
9a0a58f5 1876bool wxVariant::operator== (const wxVariantList& value) const
edca7a82 1877{
2562c823 1878 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") );
edca7a82 1879
2562c823
RR
1880 wxVariantDataList other(value);
1881 return (GetData()->Eq(other));
edca7a82
GT
1882}
1883
9a0a58f5 1884bool wxVariant::operator!= (const wxVariantList& value) const
edca7a82
GT
1885{
1886 return (!((*this) == value));
1887}
1888
9a0a58f5 1889void wxVariant::operator= (const wxVariantList& value)
edca7a82 1890{
2562c823 1891 if (GetType() == wxT("list") &&
cf25a599 1892 m_refData->GetRefCount() == 1)
edca7a82 1893 {
2562c823 1894 ((wxVariantDataList*)GetData())->SetValue(value);
edca7a82
GT
1895 }
1896 else
1897 {
2562c823 1898 UnRef();
cf25a599 1899 m_refData = new wxVariantDataList(value);
edca7a82
GT
1900 }
1901}
1902
9a0a58f5 1903wxVariantList& wxVariant::GetList() const
edca7a82 1904{
2562c823 1905 wxASSERT( (GetType() == wxT("list")) );
edca7a82 1906
cf25a599 1907 return (wxVariantList&) ((wxVariantDataList*) m_refData)->GetValue();
2562c823 1908}
edca7a82 1909
2562c823
RR
1910// Make empty list
1911void wxVariant::NullList()
edca7a82 1912{
2562c823 1913 SetData(new wxVariantDataList());
edca7a82
GT
1914}
1915
2562c823
RR
1916// Append to list
1917void wxVariant::Append(const wxVariant& value)
edca7a82 1918{
9a0a58f5 1919 wxVariantList& list = GetList();
2562c823
RR
1920
1921 list.Append(new wxVariant(value));
edca7a82
GT
1922}
1923
2562c823
RR
1924// Insert at front of list
1925void wxVariant::Insert(const wxVariant& value)
1926{
9a0a58f5 1927 wxVariantList& list = GetList();
fb42d7c3 1928
2562c823
RR
1929 list.Insert(new wxVariant(value));
1930}
1931
1932// Returns true if the variant is a member of the list
1933bool wxVariant::Member(const wxVariant& value) const
fb42d7c3 1934{
9a0a58f5 1935 wxVariantList& list = GetList();
fb42d7c3 1936
9a0a58f5 1937 wxVariantList::compatibility_iterator node = list.GetFirst();
2562c823
RR
1938 while (node)
1939 {
9a0a58f5 1940 wxVariant* other = node->GetData();
2562c823
RR
1941 if (value == *other)
1942 return true;
1943 node = node->GetNext();
1944 }
cab1a605 1945 return false;
fb42d7c3
VZ
1946}
1947
2562c823
RR
1948// Deletes the nth element of the list
1949bool wxVariant::Delete(size_t item)
fb42d7c3 1950{
9a0a58f5 1951 wxVariantList& list = GetList();
2562c823
RR
1952
1953 wxASSERT_MSG( (item < list.GetCount()), wxT("Invalid index to Delete") );
9a0a58f5
RR
1954 wxVariantList::compatibility_iterator node = list.Item(item);
1955 wxVariant* variant = node->GetData();
2562c823
RR
1956 delete variant;
1957 list.Erase(node);
1958 return true;
fb42d7c3
VZ
1959}
1960
2562c823
RR
1961// Clear list
1962void wxVariant::ClearList()
fb42d7c3 1963{
2562c823 1964 if (!IsNull() && (GetType() == wxT("list")))
fb42d7c3 1965 {
cf25a599 1966 ((wxVariantDataList*) m_refData)->Clear();
fb42d7c3
VZ
1967 }
1968 else
1969 {
2562c823
RR
1970 if (!GetType().IsSameAs(wxT("list")))
1971 UnRef();
fb42d7c3 1972
cf25a599 1973 m_refData = new wxVariantDataList;
2562c823 1974 }
fb42d7c3
VZ
1975}
1976
60acae80
RR
1977// Treat a list variant as an array
1978wxVariant wxVariant::operator[] (size_t idx) const
1979{
60acae80 1980 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") );
60acae80
RR
1981
1982 if (GetType() == wxT("list"))
1983 {
cf25a599 1984 wxVariantDataList* data = (wxVariantDataList*) m_refData;
60acae80 1985 wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") );
9a0a58f5 1986 return *(data->GetValue().Item(idx)->GetData());
60acae80 1987 }
60acae80
RR
1988 return wxNullVariant;
1989}
1990
1991wxVariant& wxVariant::operator[] (size_t idx)
1992{
1993 // We can't return a reference to a variant for a string list, since the string
1994 // is actually stored as a char*, not a variant.
1995
1996 wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") );
1997
cf25a599 1998 wxVariantDataList* data = (wxVariantDataList*) m_refData;
60acae80
RR
1999 wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") );
2000
9a0a58f5 2001 return * (data->GetValue().Item(idx)->GetData());
60acae80
RR
2002}
2003
2004// Return the number of elements in a list
2005size_t wxVariant::GetCount() const
2006{
60acae80 2007 wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") );
60acae80
RR
2008
2009 if (GetType() == wxT("list"))
2010 {
cf25a599 2011 wxVariantDataList* data = (wxVariantDataList*) m_refData;
60acae80
RR
2012 return data->GetValue().GetCount();
2013 }
60acae80
RR
2014 return 0;
2015}
2016
2562c823 2017// ----------------------------------------------------------------------------
341287bf 2018// Type conversion
2562c823
RR
2019// ----------------------------------------------------------------------------
2020
341287bf
JS
2021bool wxVariant::Convert(long* value) const
2022{
2023 wxString type(GetType());
4e00b908 2024 if (type == wxS("double"))
2562c823 2025 *value = (long) (((wxVariantDoubleData*)GetData())->GetValue());
4e00b908 2026 else if (type == wxS("long"))
341287bf 2027 *value = ((wxVariantDataLong*)GetData())->GetValue();
4e00b908 2028 else if (type == wxS("bool"))
341287bf 2029 *value = (long) (((wxVariantDataBool*)GetData())->GetValue());
4e00b908 2030 else if (type == wxS("string"))
52de37c7 2031 *value = wxAtol(((wxVariantDataString*)GetData())->GetValue());
4e00b908
JS
2032#if wxUSE_LONGLONG
2033 else if (type == wxS("longlong"))
2034 {
2035 wxLongLong v = ((wxVariantDataLongLong*)GetData())->GetValue();
2036 // Don't convert if return value would be vague
2037 if ( v < LONG_MIN || v > LONG_MAX )
2038 return false;
2039 *value = v.ToLong();
2040 }
2041 else if (type == wxS("ulonglong"))
2042 {
2043 wxULongLong v = ((wxVariantDataULongLong*)GetData())->GetValue();
2044 // Don't convert if return value would be vague
2045 if ( v.GetHi() )
2046 return false;
2047 *value = (long) v.ToULong();
2048 }
2049#endif
341287bf 2050 else
cab1a605 2051 return false;
341287bf 2052
cab1a605 2053 return true;
341287bf
JS
2054}
2055
2056bool wxVariant::Convert(bool* value) const
2057{
2058 wxString type(GetType());
223d09f6 2059 if (type == wxT("double"))
2562c823 2060 *value = ((int) (((wxVariantDoubleData*)GetData())->GetValue()) != 0);
223d09f6 2061 else if (type == wxT("long"))
341287bf 2062 *value = (((wxVariantDataLong*)GetData())->GetValue() != 0);
223d09f6 2063 else if (type == wxT("bool"))
341287bf 2064 *value = ((wxVariantDataBool*)GetData())->GetValue();
223d09f6 2065 else if (type == wxT("string"))
341287bf
JS
2066 {
2067 wxString val(((wxVariantDataString*)GetData())->GetValue());
2068 val.MakeLower();
b1638abf 2069 if (val == wxT("true") || val == wxT("yes") || val == wxT('1') )
cab1a605 2070 *value = true;
b1638abf 2071 else if (val == wxT("false") || val == wxT("no") || val == wxT('0') )
cab1a605 2072 *value = false;
341287bf 2073 else
cab1a605 2074 return false;
341287bf
JS
2075 }
2076 else
cab1a605 2077 return false;
341287bf 2078
cab1a605 2079 return true;
341287bf
JS
2080}
2081
2082bool wxVariant::Convert(double* value) const
2083{
2084 wxString type(GetType());
223d09f6 2085 if (type == wxT("double"))
2562c823 2086 *value = ((wxVariantDoubleData*)GetData())->GetValue();
223d09f6 2087 else if (type == wxT("long"))
341287bf 2088 *value = (double) (((wxVariantDataLong*)GetData())->GetValue());
223d09f6 2089 else if (type == wxT("bool"))
341287bf 2090 *value = (double) (((wxVariantDataBool*)GetData())->GetValue());
223d09f6 2091 else if (type == wxT("string"))
52de37c7 2092 *value = (double) wxAtof(((wxVariantDataString*)GetData())->GetValue());
4e00b908
JS
2093#if wxUSE_LONGLONG
2094 else if (type == wxS("longlong"))
2095 {
2096 *value = ((wxVariantDataLongLong*)GetData())->GetValue().ToDouble();
2097 }
2098 else if (type == wxS("ulonglong"))
2099 {
2100 *value = ((wxVariantDataULongLong*)GetData())->GetValue().ToDouble();
2101 }
2102#endif
341287bf 2103 else
cab1a605 2104 return false;
341287bf 2105
cab1a605 2106 return true;
341287bf
JS
2107}
2108
af717fa8 2109bool wxVariant::Convert(wxUniChar* value) const
341287bf
JS
2110{
2111 wxString type(GetType());
223d09f6 2112 if (type == wxT("char"))
341287bf 2113 *value = ((wxVariantDataChar*)GetData())->GetValue();
223d09f6 2114 else if (type == wxT("long"))
341287bf 2115 *value = (char) (((wxVariantDataLong*)GetData())->GetValue());
223d09f6 2116 else if (type == wxT("bool"))
341287bf
JS
2117 *value = (char) (((wxVariantDataBool*)GetData())->GetValue());
2118 else
cab1a605 2119 return false;
341287bf 2120
cab1a605 2121 return true;
341287bf
JS
2122}
2123
af717fa8
VS
2124bool wxVariant::Convert(char* value) const
2125{
2126 wxUniChar ch;
2127 if ( !Convert(&ch) )
2128 return false;
2129 *value = ch;
2130 return true;
2131}
2132
2133bool wxVariant::Convert(wchar_t* value) const
2134{
2135 wxUniChar ch;
2136 if ( !Convert(&ch) )
2137 return false;
2138 *value = ch;
2139 return true;
2140}
2141
341287bf
JS
2142bool wxVariant::Convert(wxString* value) const
2143{
2144 *value = MakeString();
cab1a605 2145 return true;
341287bf
JS
2146}
2147
4e00b908
JS
2148#if wxUSE_LONGLONG
2149bool wxVariant::Convert(wxLongLong* value) const
2150{
2151 wxString type(GetType());
2152 if (type == wxS("longlong"))
2153 *value = ((wxVariantDataLongLong*)GetData())->GetValue();
2154 else if (type == wxS("long"))
2155 *value = ((wxVariantDataLong*)GetData())->GetValue();
2156 else if (type == wxS("string"))
2157 {
2158 wxString s = ((wxVariantDataString*)GetData())->GetValue();
2159#ifdef wxLongLong_t
2160 wxLongLong_t value_t;
2161 if ( !s.ToLongLong(&value_t) )
2162 return false;
2163 *value = value_t;
2164#else
2165 long l_value;
2166 if ( !s.ToLong(&l_value) )
2167 return false;
2168 *value = l_value;
2169#endif
2170 }
2171 else if (type == wxS("bool"))
2172 *value = (long) (((wxVariantDataBool*)GetData())->GetValue());
2173 else if (type == wxS("double"))
2174 {
2175 value->Assign(((wxVariantDoubleData*)GetData())->GetValue());
2176 }
2177 else if (type == wxS("ulonglong"))
2178 *value = ((wxVariantDataULongLong*)GetData())->GetValue();
2179 else
2180 return false;
2181
2182 return true;
2183}
2184
2185bool wxVariant::Convert(wxULongLong* value) const
2186{
2187 wxString type(GetType());
2188 if (type == wxS("ulonglong"))
2189 *value = ((wxVariantDataULongLong*)GetData())->GetValue();
2190 else if (type == wxS("long"))
2191 *value = ((wxVariantDataLong*)GetData())->GetValue();
2192 else if (type == wxS("string"))
2193 {
2194 wxString s = ((wxVariantDataString*)GetData())->GetValue();
2195#ifdef wxLongLong_t
2196 wxULongLong_t value_t;
2197 if ( !s.ToULongLong(&value_t) )
2198 return false;
2199 *value = value_t;
2200#else
2201 unsigned long l_value;
2202 if ( !s.ToULong(&l_value) )
2203 return false;
2204 *value = l_value;
2205#endif
2206 }
2207 else if (type == wxS("bool"))
2208 *value = (long) (((wxVariantDataBool*)GetData())->GetValue());
2209 else if (type == wxS("double"))
2210 {
2211 double value_d = ((wxVariantDoubleData*)GetData())->GetValue();
2212
2213 if ( value_d < 0.0 )
2214 return false;
2215
2216#ifdef wxLongLong_t
2217 *value = (wxULongLong_t) value_d;
2218#else
2219 wxLongLong temp;
2220 temp.Assign(value_d);
2221 *value = temp;
2222#endif
2223 }
2224 else if (type == wxS("longlong"))
2225 *value = ((wxVariantDataLongLong*)GetData())->GetValue();
2226 else
2227 return false;
2228
2229 return true;
2230}
2231#endif // wxUSE_LONGLONG
2232
e2b87f38 2233#if wxUSE_DATETIME
edca7a82
GT
2234bool wxVariant::Convert(wxDateTime* value) const
2235{
2236 wxString type(GetType());
2237 if (type == wxT("datetime"))
9708db20 2238 {
edca7a82 2239 *value = ((wxVariantDataDateTime*)GetData())->GetValue();
cab1a605 2240 return true;
dc259b79 2241 }
73799292 2242
9708db20
JS
2243 // Fallback to string conversion
2244 wxString val;
73799292
VZ
2245 if ( !Convert(&val) )
2246 return false;
2247
2248 // Try to parse this as either date and time, only date or only time
2249 // checking that the entire string was parsed
2250 wxString::const_iterator end;
2251 if ( value->ParseDateTime(val, &end) && end == val.end() )
2252 return true;
2253
2254 if ( value->ParseDate(val, &end) && end == val.end() )
2255 return true;
2256
2257 if ( value->ParseTime(val, &end) && end == val.end() )
2258 return true;
2259
2260 return false;
edca7a82 2261}
b1e343f2 2262#endif // wxUSE_DATETIME
d5dc103f
VZ
2263
2264#endif // wxUSE_VARIANT