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