]>
Commit | Line | Data |
---|---|---|
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 | 41 | namespace std {} |
31907d03 SC |
42 | using 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 | 52 | wxVariant WXDLLIMPEXP_BASE wxNullVariant; |
341287bf | 53 | |
2562c823 | 54 | |
7e6b4780 | 55 | #include "wx/listimpl.cpp" |
43ea4e37 | 56 | WX_DEFINE_LIST(wxVariantList) |
7e6b4780 | 57 | |
341287bf | 58 | /* |
2562c823 | 59 | * wxVariant |
341287bf JS |
60 | */ |
61 | ||
2562c823 | 62 | IMPLEMENT_DYNAMIC_CLASS(wxVariant, wxObject) |
341287bf | 63 | |
2562c823 | 64 | wxVariant::wxVariant() |
341287bf | 65 | { |
d3b9f782 | 66 | m_data = NULL; |
341287bf JS |
67 | } |
68 | ||
2562c823 | 69 | bool wxVariant::IsNull() const |
341287bf | 70 | { |
d3b9f782 | 71 | return (m_data == NULL); |
341287bf JS |
72 | } |
73 | ||
2562c823 | 74 | void wxVariant::MakeNull() |
341287bf | 75 | { |
2562c823 | 76 | UnRef(); |
341287bf JS |
77 | } |
78 | ||
2562c823 | 79 | void wxVariant::Clear() |
341287bf | 80 | { |
2562c823 | 81 | m_name = wxEmptyString; |
341287bf JS |
82 | } |
83 | ||
2562c823 RR |
84 | wxVariant::wxVariant(const wxVariant& variant) |
85 | : wxObject() | |
341287bf | 86 | { |
d3b9f782 | 87 | m_data = 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 | 95 | wxVariant::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 | 101 | wxVariant::~wxVariant() |
341287bf | 102 | { |
2562c823 | 103 | UnRef(); |
341287bf JS |
104 | } |
105 | ||
2562c823 RR |
106 | // Assignment |
107 | void 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") |
114 | void wxVariant::operator= (wxVariantData* variantData) | |
341287bf | 115 | { |
2562c823 RR |
116 | UnRef(); |
117 | m_data = variantData; | |
341287bf JS |
118 | } |
119 | ||
2562c823 | 120 | bool 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 | 131 | bool wxVariant::operator!= (const wxVariant& variant) const |
341287bf | 132 | { |
2562c823 RR |
133 | return (!(*this == variant)); |
134 | } | |
341287bf | 135 | |
2562c823 | 136 | wxString 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 | 147 | void wxVariant::SetData(wxVariantData* data) |
341287bf | 148 | { |
2562c823 RR |
149 | UnRef(); |
150 | m_data = data; | |
341287bf JS |
151 | } |
152 | ||
2562c823 | 153 | void 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 | 171 | void 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 |
182 | bool 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" | |
209 | wxString 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 | |
218 | bool wxVariant::IsType(const wxString& type) const | |
341287bf | 219 | { |
2562c823 | 220 | return (GetType() == type); |
341287bf JS |
221 | } |
222 | ||
2562c823 | 223 | bool 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 | 234 | class WXDLLIMPEXP_BASE wxVariantDataLong: public wxVariantData |
341287bf | 235 | { |
341287bf JS |
236 | public: |
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 | |
260 | protected: | |
261 | long m_value; | |
262 | }; | |
263 | ||
341287bf JS |
264 | bool 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 | 274 | bool 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 | |
283 | bool 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 | 290 | bool 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 |
298 | bool 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 |
306 | bool 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 |
314 | bool 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 | 322 | wxVariant::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 |
328 | wxVariant::wxVariant(int val, const wxString& name) |
329 | { | |
330 | m_data = new wxVariantDataLong((long)val); | |
331 | m_name = name; | |
332 | } | |
333 | ||
334 | wxVariant::wxVariant(short val, const wxString& name) | |
335 | { | |
336 | m_data = new wxVariantDataLong((long)val); | |
337 | m_name = name; | |
338 | } | |
339 | ||
340 | bool wxVariant::operator== (long value) const | |
341 | { | |
342 | long thisValue; | |
343 | if (!Convert(&thisValue)) | |
344 | return false; | |
345 | else | |
346 | return (value == thisValue); | |
347 | } | |
348 | ||
349 | bool wxVariant::operator!= (long value) const | |
350 | { | |
351 | return (!((*this) == value)); | |
352 | } | |
353 | ||
354 | void 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 | ||
368 | long 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 | ||
384 | class WXDLLIMPEXP_BASE wxVariantDoubleData: public wxVariantData | |
385 | { | |
2562c823 RR |
386 | public: |
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 |
409 | protected: |
410 | double m_value; | |
411 | }; | |
412 | ||
2562c823 | 413 | bool 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 | 423 | bool 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 | 432 | bool 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 | 439 | bool 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 | 447 | bool 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 | 454 | bool 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 | 462 | bool 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 | ||
470 | wxVariant::wxVariant(double val, const wxString& name) | |
471 | { | |
472 | m_data = new wxVariantDoubleData(val); | |
473 | m_name = name; | |
474 | } | |
475 | ||
476 | bool wxVariant::operator== (double value) const | |
477 | { | |
478 | double thisValue; | |
479 | if (!Convert(&thisValue)) | |
480 | return false; | |
481 | ||
482 | return wxIsSameDouble(value, thisValue); | |
483 | } | |
484 | ||
485 | bool wxVariant::operator!= (double value) const | |
486 | { | |
487 | return (!((*this) == value)); | |
488 | } | |
489 | ||
490 | void 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 | ||
504 | double 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 | ||
fd242375 | 520 | class WXDLLIMPEXP_BASE wxVariantDataBool: public wxVariantData |
341287bf | 521 | { |
341287bf JS |
522 | public: |
523 | wxVariantDataBool() { m_value = 0; } | |
524 | wxVariantDataBool(bool value) { m_value = value; } | |
525 | ||
526 | inline bool GetValue() const { return m_value; } | |
527 | inline void SetValue(bool value) { m_value = value; } | |
528 | ||
341287bf | 529 | virtual bool Eq(wxVariantData& data) const; |
38830220 | 530 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 531 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 532 | #endif |
341287bf | 533 | virtual bool Write(wxString& str) const; |
1ccbb61a | 534 | virtual bool Read(wxString& str); |
38830220 | 535 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 536 | virtual bool Read(wxSTD istream& str); |
38830220 | 537 | #endif |
e02afc7a | 538 | #if wxUSE_STREAMS |
75ed1d15 | 539 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 540 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 541 | #endif // wxUSE_STREAMS |
47b378bd | 542 | virtual wxString GetType() const { return wxT("bool"); } |
341287bf | 543 | |
c8058a09 | 544 | wxVariantData* Clone() const { return new wxVariantDataBool(m_value); } |
341287bf JS |
545 | protected: |
546 | bool m_value; | |
547 | }; | |
548 | ||
341287bf JS |
549 | bool wxVariantDataBool::Eq(wxVariantData& data) const |
550 | { | |
223d09f6 | 551 | wxASSERT_MSG( (data.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") ); |
341287bf JS |
552 | |
553 | wxVariantDataBool& otherData = (wxVariantDataBool&) data; | |
554 | ||
555 | return (otherData.m_value == m_value); | |
556 | } | |
557 | ||
38830220 | 558 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 559 | bool wxVariantDataBool::Write(wxSTD ostream& str) const |
341287bf JS |
560 | { |
561 | wxString s; | |
562 | Write(s); | |
783b6cfd | 563 | str << (const char*) s.mb_str(); |
cab1a605 | 564 | return true; |
341287bf | 565 | } |
38830220 | 566 | #endif |
341287bf JS |
567 | |
568 | bool wxVariantDataBool::Write(wxString& str) const | |
569 | { | |
223d09f6 | 570 | str.Printf(wxT("%d"), (int) m_value); |
cab1a605 | 571 | return true; |
341287bf JS |
572 | } |
573 | ||
38830220 | 574 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 575 | bool wxVariantDataBool::Read(wxSTD istream& WXUNUSED(str)) |
341287bf | 576 | { |
223d09f6 | 577 | wxFAIL_MSG(wxT("Unimplemented")); |
341287bf | 578 | // str >> (long) m_value; |
cab1a605 | 579 | return false; |
341287bf | 580 | } |
38830220 | 581 | #endif |
341287bf | 582 | |
e02afc7a | 583 | #if wxUSE_STREAMS |
1ccbb61a VZ |
584 | bool wxVariantDataBool::Write(wxOutputStream& str) const |
585 | { | |
fae05df5 GL |
586 | wxTextOutputStream s(str); |
587 | ||
2b004197 | 588 | s.Write8(m_value); |
cab1a605 | 589 | return true; |
1ccbb61a VZ |
590 | } |
591 | ||
75ed1d15 GL |
592 | bool wxVariantDataBool::Read(wxInputStream& str) |
593 | { | |
fae05df5 GL |
594 | wxTextInputStream s(str); |
595 | ||
a1b82138 | 596 | m_value = s.Read8() != 0; |
cab1a605 | 597 | return true; |
75ed1d15 | 598 | } |
e02afc7a | 599 | #endif // wxUSE_STREAMS |
75ed1d15 | 600 | |
341287bf JS |
601 | bool wxVariantDataBool::Read(wxString& str) |
602 | { | |
52de37c7 | 603 | m_value = (wxAtol(str) != 0); |
cab1a605 | 604 | return true; |
341287bf | 605 | } |
2562c823 RR |
606 | |
607 | // wxVariant **** | |
608 | ||
609 | wxVariant::wxVariant(bool val, const wxString& name) | |
610 | { | |
611 | m_data = new wxVariantDataBool(val); | |
612 | m_name = name; | |
613 | } | |
614 | ||
615 | bool wxVariant::operator== (bool value) const | |
616 | { | |
617 | bool thisValue; | |
618 | if (!Convert(&thisValue)) | |
619 | return false; | |
620 | else | |
621 | return (value == thisValue); | |
622 | } | |
623 | ||
624 | bool wxVariant::operator!= (bool value) const | |
625 | { | |
626 | return (!((*this) == value)); | |
627 | } | |
628 | ||
629 | void wxVariant::operator= (bool value) | |
630 | { | |
631 | if (GetType() == wxT("bool") && | |
632 | m_data->GetRefCount() == 1) | |
633 | { | |
634 | ((wxVariantDataBool*)GetData())->SetValue(value); | |
635 | } | |
636 | else | |
637 | { | |
638 | UnRef(); | |
639 | m_data = new wxVariantDataBool(value); | |
640 | } | |
641 | } | |
642 | ||
643 | bool wxVariant::GetBool() const | |
644 | { | |
645 | bool value; | |
646 | if (Convert(& value)) | |
647 | return value; | |
648 | else | |
649 | { | |
650 | wxFAIL_MSG(wxT("Could not convert to a bool")); | |
651 | return 0; | |
652 | } | |
653 | } | |
654 | ||
2562c823 RR |
655 | // ----------------------------------------------------------------- |
656 | // wxVariantDataChar | |
657 | // ----------------------------------------------------------------- | |
341287bf | 658 | |
fd242375 | 659 | class WXDLLIMPEXP_BASE wxVariantDataChar: public wxVariantData |
341287bf | 660 | { |
341287bf JS |
661 | public: |
662 | wxVariantDataChar() { m_value = 0; } | |
af717fa8 | 663 | wxVariantDataChar(const wxUniChar& value) { m_value = value; } |
341287bf | 664 | |
af717fa8 VS |
665 | inline wxUniChar GetValue() const { return m_value; } |
666 | inline void SetValue(const wxUniChar& value) { m_value = value; } | |
341287bf | 667 | |
341287bf | 668 | virtual bool Eq(wxVariantData& data) const; |
38830220 | 669 | #if wxUSE_STD_IOSTREAM |
dd107c50 VZ |
670 | virtual bool Read(wxSTD istream& str); |
671 | virtual bool Write(wxSTD ostream& str) const; | |
38830220 | 672 | #endif |
1ccbb61a | 673 | virtual bool Read(wxString& str); |
341287bf | 674 | virtual bool Write(wxString& str) const; |
e02afc7a | 675 | #if wxUSE_STREAMS |
75ed1d15 | 676 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 677 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 678 | #endif // wxUSE_STREAMS |
47b378bd | 679 | virtual wxString GetType() const { return wxT("char"); } |
c8058a09 | 680 | wxVariantData* Clone() const { return new wxVariantDataChar(m_value); } |
341287bf JS |
681 | |
682 | protected: | |
af717fa8 | 683 | wxUniChar m_value; |
341287bf JS |
684 | }; |
685 | ||
341287bf JS |
686 | bool wxVariantDataChar::Eq(wxVariantData& data) const |
687 | { | |
223d09f6 | 688 | wxASSERT_MSG( (data.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") ); |
341287bf JS |
689 | |
690 | wxVariantDataChar& otherData = (wxVariantDataChar&) data; | |
691 | ||
692 | return (otherData.m_value == m_value); | |
693 | } | |
694 | ||
38830220 | 695 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 696 | bool wxVariantDataChar::Write(wxSTD ostream& str) const |
341287bf | 697 | { |
af717fa8 | 698 | str << wxString(m_value); |
cab1a605 | 699 | return true; |
341287bf | 700 | } |
38830220 | 701 | #endif |
341287bf JS |
702 | |
703 | bool wxVariantDataChar::Write(wxString& str) const | |
704 | { | |
af717fa8 | 705 | str = m_value; |
cab1a605 | 706 | return true; |
341287bf JS |
707 | } |
708 | ||
38830220 | 709 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 710 | bool wxVariantDataChar::Read(wxSTD istream& WXUNUSED(str)) |
341287bf | 711 | { |
223d09f6 | 712 | wxFAIL_MSG(wxT("Unimplemented")); |
07502d73 | 713 | |
cab1a605 | 714 | return false; |
341287bf | 715 | } |
38830220 | 716 | #endif |
341287bf | 717 | |
e02afc7a | 718 | #if wxUSE_STREAMS |
1ccbb61a VZ |
719 | bool wxVariantDataChar::Write(wxOutputStream& str) const |
720 | { | |
fae05df5 GL |
721 | wxTextOutputStream s(str); |
722 | ||
af717fa8 VS |
723 | // FIXME-UTF8: this should be just "s << m_value;" after removal of |
724 | // ANSI build and addition of wxUniChar to wxTextOutputStream: | |
725 | s << (wxChar)m_value; | |
07502d73 | 726 | |
cab1a605 | 727 | return true; |
1ccbb61a VZ |
728 | } |
729 | ||
75ed1d15 GL |
730 | bool wxVariantDataChar::Read(wxInputStream& str) |
731 | { | |
fae05df5 GL |
732 | wxTextInputStream s(str); |
733 | ||
af717fa8 VS |
734 | // FIXME-UTF8: this should be just "s >> m_value;" after removal of |
735 | // ANSI build and addition of wxUniChar to wxTextInputStream: | |
736 | wxChar ch; | |
737 | s >> ch; | |
738 | m_value = ch; | |
71520754 | 739 | |
cab1a605 | 740 | return true; |
75ed1d15 | 741 | } |
e02afc7a | 742 | #endif // wxUSE_STREAMS |
75ed1d15 | 743 | |
341287bf JS |
744 | bool wxVariantDataChar::Read(wxString& str) |
745 | { | |
af717fa8 | 746 | m_value = str[0u]; |
cab1a605 | 747 | return true; |
341287bf JS |
748 | } |
749 | ||
af717fa8 | 750 | wxVariant::wxVariant(const wxUniChar& val, const wxString& name) |
2562c823 RR |
751 | { |
752 | m_data = new wxVariantDataChar(val); | |
753 | m_name = name; | |
754 | } | |
755 | ||
af717fa8 | 756 | wxVariant::wxVariant(char val, const wxString& name) |
2562c823 | 757 | { |
af717fa8 VS |
758 | m_data = new wxVariantDataChar(val); |
759 | m_name = name; | |
760 | } | |
761 | ||
762 | wxVariant::wxVariant(wchar_t val, const wxString& name) | |
763 | { | |
764 | m_data = new wxVariantDataChar(val); | |
765 | m_name = name; | |
766 | } | |
767 | ||
768 | bool wxVariant::operator==(const wxUniChar& value) const | |
769 | { | |
770 | wxUniChar thisValue; | |
2562c823 RR |
771 | if (!Convert(&thisValue)) |
772 | return false; | |
773 | else | |
774 | return (value == thisValue); | |
775 | } | |
776 | ||
af717fa8 | 777 | wxVariant& wxVariant::operator=(const wxUniChar& value) |
2562c823 RR |
778 | { |
779 | if (GetType() == wxT("char") && | |
780 | m_data->GetRefCount() == 1) | |
781 | { | |
782 | ((wxVariantDataChar*)GetData())->SetValue(value); | |
783 | } | |
784 | else | |
785 | { | |
786 | UnRef(); | |
787 | m_data = new wxVariantDataChar(value); | |
788 | } | |
af717fa8 VS |
789 | |
790 | return *this; | |
2562c823 RR |
791 | } |
792 | ||
af717fa8 | 793 | wxUniChar wxVariant::GetChar() const |
2562c823 | 794 | { |
af717fa8 | 795 | wxUniChar value; |
2562c823 RR |
796 | if (Convert(& value)) |
797 | return value; | |
798 | else | |
799 | { | |
800 | wxFAIL_MSG(wxT("Could not convert to a char")); | |
af717fa8 | 801 | return wxUniChar(0); |
2562c823 RR |
802 | } |
803 | } | |
804 | ||
805 | // ---------------------------------------------------------------------------- | |
806 | // wxVariantDataString | |
807 | // ---------------------------------------------------------------------------- | |
341287bf | 808 | |
fd242375 | 809 | class WXDLLIMPEXP_BASE wxVariantDataString: public wxVariantData |
341287bf | 810 | { |
341287bf JS |
811 | public: |
812 | wxVariantDataString() { } | |
813 | wxVariantDataString(const wxString& value) { m_value = value; } | |
814 | ||
815 | inline wxString GetValue() const { return m_value; } | |
816 | inline void SetValue(const wxString& value) { m_value = value; } | |
817 | ||
341287bf | 818 | virtual bool Eq(wxVariantData& data) const; |
38830220 | 819 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 820 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 821 | #endif |
1ccbb61a | 822 | virtual bool Read(wxString& str); |
341287bf | 823 | virtual bool Write(wxString& str) const; |
38830220 | 824 | #if wxUSE_STD_IOSTREAM |
47b378bd | 825 | virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; } |
38830220 | 826 | #endif |
e02afc7a | 827 | #if wxUSE_STREAMS |
75ed1d15 | 828 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 829 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 830 | #endif // wxUSE_STREAMS |
47b378bd | 831 | virtual wxString GetType() const { return wxT("string"); } |
c8058a09 | 832 | wxVariantData* Clone() const { return new wxVariantDataString(m_value); } |
341287bf JS |
833 | |
834 | protected: | |
835 | wxString m_value; | |
836 | }; | |
837 | ||
341287bf JS |
838 | bool wxVariantDataString::Eq(wxVariantData& data) const |
839 | { | |
223d09f6 | 840 | wxASSERT_MSG( (data.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") ); |
341287bf JS |
841 | |
842 | wxVariantDataString& otherData = (wxVariantDataString&) data; | |
843 | ||
844 | return (otherData.m_value == m_value); | |
845 | } | |
846 | ||
38830220 | 847 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 848 | bool wxVariantDataString::Write(wxSTD ostream& str) const |
341287bf | 849 | { |
783b6cfd | 850 | str << (const char*) m_value.mb_str(); |
cab1a605 | 851 | return true; |
341287bf | 852 | } |
38830220 | 853 | #endif |
341287bf JS |
854 | |
855 | bool wxVariantDataString::Write(wxString& str) const | |
856 | { | |
857 | str = m_value; | |
cab1a605 | 858 | return true; |
341287bf JS |
859 | } |
860 | ||
e02afc7a | 861 | #if wxUSE_STREAMS |
1ccbb61a VZ |
862 | bool wxVariantDataString::Write(wxOutputStream& str) const |
863 | { | |
783b6cfd | 864 | // why doesn't wxOutputStream::operator<< take "const wxString&" |
fae05df5 GL |
865 | wxTextOutputStream s(str); |
866 | s.WriteString(m_value); | |
cab1a605 | 867 | return true; |
1ccbb61a VZ |
868 | } |
869 | ||
75ed1d15 GL |
870 | bool wxVariantDataString::Read(wxInputStream& str) |
871 | { | |
fae05df5 GL |
872 | wxTextInputStream s(str); |
873 | ||
40ff126a | 874 | m_value = s.ReadLine(); |
cab1a605 | 875 | return true; |
75ed1d15 | 876 | } |
e02afc7a | 877 | #endif // wxUSE_STREAMS |
75ed1d15 | 878 | |
341287bf JS |
879 | bool wxVariantDataString::Read(wxString& str) |
880 | { | |
881 | m_value = str; | |
cab1a605 | 882 | return true; |
341287bf JS |
883 | } |
884 | ||
2562c823 | 885 | // wxVariant **** |
07502d73 | 886 | |
2562c823 | 887 | wxVariant::wxVariant(const wxString& val, const wxString& name) |
a0a302dc | 888 | { |
2562c823 RR |
889 | m_data = new wxVariantDataString(val); |
890 | m_name = name; | |
891 | } | |
a0a302dc | 892 | |
af717fa8 VS |
893 | wxVariant::wxVariant(const char* val, const wxString& name) |
894 | { | |
895 | m_data = new wxVariantDataString(wxString(val)); | |
896 | m_name = name; | |
897 | } | |
898 | ||
899 | wxVariant::wxVariant(const wchar_t* val, const wxString& name) | |
900 | { | |
901 | m_data = new wxVariantDataString(wxString(val)); | |
902 | m_name = name; | |
903 | } | |
904 | ||
905 | wxVariant::wxVariant(const wxCStrData& val, const wxString& name) | |
906 | { | |
907 | m_data = new wxVariantDataString(val.AsString()); | |
908 | m_name = name; | |
909 | } | |
910 | ||
de4983f3 | 911 | wxVariant::wxVariant(const wxScopedCharBuffer& val, const wxString& name) |
af717fa8 VS |
912 | { |
913 | m_data = new wxVariantDataString(wxString(val)); | |
914 | m_name = name; | |
915 | } | |
916 | ||
de4983f3 | 917 | wxVariant::wxVariant(const wxScopedWCharBuffer& val, const wxString& name) |
a0a302dc | 918 | { |
2562c823 RR |
919 | m_data = new wxVariantDataString(wxString(val)); |
920 | m_name = name; | |
921 | } | |
a0a302dc | 922 | |
2562c823 RR |
923 | bool wxVariant::operator== (const wxString& value) const |
924 | { | |
925 | wxString thisValue; | |
926 | if (!Convert(&thisValue)) | |
927 | return false; | |
a0a302dc | 928 | |
2562c823 | 929 | return value == thisValue; |
a0a302dc JS |
930 | } |
931 | ||
2562c823 | 932 | bool wxVariant::operator!= (const wxString& value) const |
a0a302dc | 933 | { |
2562c823 | 934 | return (!((*this) == value)); |
a0a302dc JS |
935 | } |
936 | ||
af717fa8 | 937 | wxVariant& wxVariant::operator= (const wxString& value) |
a0a302dc | 938 | { |
2562c823 RR |
939 | if (GetType() == wxT("string") && |
940 | m_data->GetRefCount() == 1) | |
941 | { | |
942 | ((wxVariantDataString*)GetData())->SetValue(value); | |
943 | } | |
944 | else | |
945 | { | |
946 | UnRef(); | |
947 | m_data = new wxVariantDataString(value); | |
948 | } | |
af717fa8 | 949 | return *this; |
a0a302dc JS |
950 | } |
951 | ||
2562c823 | 952 | wxString wxVariant::GetString() const |
a0a302dc | 953 | { |
2562c823 RR |
954 | wxString value; |
955 | if (!Convert(& value)) | |
956 | { | |
957 | wxFAIL_MSG(wxT("Could not convert to a string")); | |
958 | } | |
a0a302dc | 959 | |
2562c823 | 960 | return value; |
a0a302dc JS |
961 | } |
962 | ||
2562c823 RR |
963 | // ---------------------------------------------------------------------------- |
964 | // wxVariantDataWxObjectPtr | |
965 | // ---------------------------------------------------------------------------- | |
cf6ae290 RG |
966 | |
967 | class wxVariantDataWxObjectPtr: public wxVariantData | |
968 | { | |
cf6ae290 RG |
969 | public: |
970 | wxVariantDataWxObjectPtr() { } | |
971 | wxVariantDataWxObjectPtr(wxObject* value) { m_value = value; } | |
972 | ||
973 | inline wxObject* GetValue() const { return m_value; } | |
974 | inline void SetValue(wxObject* value) { m_value = value; } | |
975 | ||
cf6ae290 RG |
976 | virtual bool Eq(wxVariantData& data) const; |
977 | #if wxUSE_STD_IOSTREAM | |
978 | virtual bool Write(wxSTD ostream& str) const; | |
979 | #endif | |
980 | virtual bool Write(wxString& str) const; | |
981 | #if wxUSE_STD_IOSTREAM | |
982 | virtual bool Read(wxSTD istream& str); | |
983 | #endif | |
984 | virtual bool Read(wxString& str); | |
985 | virtual wxString GetType() const ; | |
c8058a09 | 986 | virtual wxVariantData* Clone() const { return new wxVariantDataWxObjectPtr(m_value); } |
cf6ae290 | 987 | |
3586d10f | 988 | virtual wxClassInfo* GetValueClassInfo(); |
c8058a09 | 989 | |
cf6ae290 RG |
990 | protected: |
991 | wxObject* m_value; | |
cf6ae290 RG |
992 | }; |
993 | ||
cf6ae290 RG |
994 | bool wxVariantDataWxObjectPtr::Eq(wxVariantData& data) const |
995 | { | |
3586d10f | 996 | wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") ); |
cf6ae290 RG |
997 | |
998 | wxVariantDataWxObjectPtr& otherData = (wxVariantDataWxObjectPtr&) data; | |
999 | ||
1000 | return (otherData.m_value == m_value); | |
1001 | } | |
1002 | ||
1003 | wxString wxVariantDataWxObjectPtr::GetType() const | |
1004 | { | |
3586d10f | 1005 | wxString returnVal(wxT("wxObject*")); |
c8058a09 | 1006 | |
3586d10f RR |
1007 | if (m_value) |
1008 | { | |
cf6ae290 | 1009 | returnVal = m_value->GetClassInfo()->GetClassName(); |
3586d10f | 1010 | returnVal += wxT("*"); |
cf6ae290 | 1011 | } |
c8058a09 | 1012 | |
cf6ae290 RG |
1013 | return returnVal; |
1014 | } | |
1015 | ||
1016 | wxClassInfo* wxVariantDataWxObjectPtr::GetValueClassInfo() | |
1017 | { | |
1018 | wxClassInfo* returnVal=NULL; | |
cab1a605 WS |
1019 | |
1020 | if (m_value) returnVal = m_value->GetClassInfo(); | |
cf6ae290 RG |
1021 | |
1022 | return returnVal; | |
1023 | } | |
1024 | ||
1025 | #if wxUSE_STD_IOSTREAM | |
1026 | bool wxVariantDataWxObjectPtr::Write(wxSTD ostream& str) const | |
1027 | { | |
1028 | wxString s; | |
1029 | Write(s); | |
1030 | str << (const char*) s.mb_str(); | |
cab1a605 | 1031 | return true; |
cf6ae290 RG |
1032 | } |
1033 | #endif | |
1034 | ||
1035 | bool wxVariantDataWxObjectPtr::Write(wxString& str) const | |
1036 | { | |
5c33522f | 1037 | str.Printf(wxT("%s(%p)"), GetType().c_str(), static_cast<void*>(m_value)); |
cab1a605 | 1038 | return true; |
cf6ae290 RG |
1039 | } |
1040 | ||
1041 | #if wxUSE_STD_IOSTREAM | |
1042 | bool wxVariantDataWxObjectPtr::Read(wxSTD istream& WXUNUSED(str)) | |
1043 | { | |
1044 | // Not implemented | |
cab1a605 | 1045 | return false; |
cf6ae290 RG |
1046 | } |
1047 | #endif | |
1048 | ||
1049 | bool wxVariantDataWxObjectPtr::Read(wxString& WXUNUSED(str)) | |
1050 | { | |
1051 | // Not implemented | |
cab1a605 | 1052 | return false; |
cf6ae290 RG |
1053 | } |
1054 | ||
2562c823 | 1055 | // wxVariant |
cf6ae290 | 1056 | |
2562c823 RR |
1057 | wxVariant::wxVariant( wxObject* val, const wxString& name) |
1058 | { | |
1059 | m_data = new wxVariantDataWxObjectPtr(val); | |
1060 | m_name = name; | |
1061 | } | |
edca7a82 | 1062 | |
2562c823 RR |
1063 | bool wxVariant::operator== (wxObject* value) const |
1064 | { | |
1065 | return (value == ((wxVariantDataWxObjectPtr*)GetData())->GetValue()); | |
1066 | } | |
e2b87f38 | 1067 | |
2562c823 | 1068 | bool wxVariant::operator!= (wxObject* value) const |
edca7a82 | 1069 | { |
2562c823 RR |
1070 | return (!((*this) == (wxObject*) value)); |
1071 | } | |
1072 | ||
1073 | void wxVariant::operator= (wxObject* value) | |
1074 | { | |
1075 | UnRef(); | |
1076 | m_data = new wxVariantDataWxObjectPtr(value); | |
1077 | } | |
edca7a82 | 1078 | |
2562c823 RR |
1079 | wxObject* wxVariant::GetWxObjectPtr() const |
1080 | { | |
2562c823 RR |
1081 | return (wxObject*) ((wxVariantDataWxObjectPtr*) m_data)->GetValue(); |
1082 | } | |
1083 | ||
1084 | // ---------------------------------------------------------------------------- | |
1085 | // wxVariantDataVoidPtr | |
1086 | // ---------------------------------------------------------------------------- | |
1087 | ||
1088 | class wxVariantDataVoidPtr: public wxVariantData | |
1089 | { | |
edca7a82 | 1090 | public: |
2562c823 RR |
1091 | wxVariantDataVoidPtr() { } |
1092 | wxVariantDataVoidPtr(void* value) { m_value = value; } | |
edca7a82 | 1093 | |
2562c823 RR |
1094 | inline void* GetValue() const { return m_value; } |
1095 | inline void SetValue(void* value) { m_value = value; } | |
edca7a82 | 1096 | |
edca7a82 GT |
1097 | virtual bool Eq(wxVariantData& data) const; |
1098 | #if wxUSE_STD_IOSTREAM | |
2b004197 | 1099 | virtual bool Write(wxSTD ostream& str) const; |
edca7a82 GT |
1100 | #endif |
1101 | virtual bool Write(wxString& str) const; | |
1102 | #if wxUSE_STD_IOSTREAM | |
2b004197 | 1103 | virtual bool Read(wxSTD istream& str); |
edca7a82 GT |
1104 | #endif |
1105 | virtual bool Read(wxString& str); | |
47b378bd | 1106 | virtual wxString GetType() const { return wxT("void*"); } |
c8058a09 | 1107 | virtual wxVariantData* Clone() const { return new wxVariantDataVoidPtr(m_value); } |
edca7a82 GT |
1108 | |
1109 | protected: | |
2562c823 | 1110 | void* m_value; |
2562c823 | 1111 | }; |
edca7a82 | 1112 | |
2562c823 | 1113 | bool wxVariantDataVoidPtr::Eq(wxVariantData& data) const |
edca7a82 | 1114 | { |
3586d10f | 1115 | wxASSERT_MSG( data.GetType() == wxT("void*"), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") ); |
edca7a82 | 1116 | |
2562c823 | 1117 | wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data; |
edca7a82 | 1118 | |
2562c823 | 1119 | return (otherData.m_value == m_value); |
edca7a82 GT |
1120 | } |
1121 | ||
2562c823 RR |
1122 | #if wxUSE_STD_IOSTREAM |
1123 | bool wxVariantDataVoidPtr::Write(wxSTD ostream& str) const | |
edca7a82 | 1124 | { |
2562c823 RR |
1125 | wxString s; |
1126 | Write(s); | |
1127 | str << (const char*) s.mb_str(); | |
1128 | return true; | |
edca7a82 | 1129 | } |
2562c823 | 1130 | #endif |
edca7a82 | 1131 | |
2562c823 RR |
1132 | bool wxVariantDataVoidPtr::Write(wxString& str) const |
1133 | { | |
1134 | str.Printf(wxT("%p"), m_value); | |
1135 | return true; | |
1136 | } | |
edca7a82 GT |
1137 | |
1138 | #if wxUSE_STD_IOSTREAM | |
2562c823 | 1139 | bool wxVariantDataVoidPtr::Read(wxSTD istream& WXUNUSED(str)) |
edca7a82 GT |
1140 | { |
1141 | // Not implemented | |
cab1a605 | 1142 | return false; |
edca7a82 GT |
1143 | } |
1144 | #endif | |
1145 | ||
2562c823 | 1146 | bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str)) |
edca7a82 | 1147 | { |
2562c823 RR |
1148 | // Not implemented |
1149 | return false; | |
edca7a82 GT |
1150 | } |
1151 | ||
2562c823 | 1152 | // wxVariant |
edca7a82 | 1153 | |
2562c823 | 1154 | wxVariant::wxVariant( void* val, const wxString& name) |
edca7a82 | 1155 | { |
2562c823 RR |
1156 | m_data = new wxVariantDataVoidPtr(val); |
1157 | m_name = name; | |
1158 | } | |
1159 | ||
1160 | bool wxVariant::operator== (void* value) const | |
1161 | { | |
1162 | return (value == ((wxVariantDataVoidPtr*)GetData())->GetValue()); | |
edca7a82 | 1163 | } |
edca7a82 | 1164 | |
2562c823 RR |
1165 | bool wxVariant::operator!= (void* value) const |
1166 | { | |
1167 | return (!((*this) == (void*) value)); | |
1168 | } | |
edca7a82 | 1169 | |
2562c823 | 1170 | void wxVariant::operator= (void* value) |
edca7a82 | 1171 | { |
3586d10f | 1172 | if (GetType() == wxT("void*") && (m_data->GetRefCount() == 1)) |
2562c823 RR |
1173 | { |
1174 | ((wxVariantDataVoidPtr*)GetData())->SetValue(value); | |
1175 | } | |
1176 | else | |
1177 | { | |
1178 | UnRef(); | |
1179 | m_data = new wxVariantDataVoidPtr(value); | |
1180 | } | |
edca7a82 GT |
1181 | } |
1182 | ||
2562c823 RR |
1183 | void* wxVariant::GetVoidPtr() const |
1184 | { | |
d099c754 VZ |
1185 | // handling this specially is convenient when working with COM, see #9873 |
1186 | if ( IsNull() ) | |
1187 | return NULL; | |
1188 | ||
1189 | wxASSERT( GetType() == wxT("void*") ); | |
2562c823 RR |
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 | ||
1200 | class wxVariantDataDateTime: public wxVariantData | |
fb42d7c3 VZ |
1201 | { |
1202 | public: | |
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 | |
1221 | protected: | |
2562c823 | 1222 | wxDateTime m_value; |
fb42d7c3 VZ |
1223 | }; |
1224 | ||
fb42d7c3 | 1225 | |
2562c823 | 1226 | bool 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 | 1237 | bool 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 | 1247 | bool wxVariantDataDateTime::Write(wxString& str) const |
fb42d7c3 | 1248 | { |
be53e8ae JS |
1249 | if ( m_value.IsValid() ) |
1250 | str = m_value.Format(); | |
1251 | else | |
1252 | str = wxS("Invalid"); | |
cab1a605 | 1253 | return true; |
fb42d7c3 VZ |
1254 | } |
1255 | ||
1256 | ||
1257 | #if wxUSE_STD_IOSTREAM | |
2562c823 | 1258 | bool wxVariantDataDateTime::Read(wxSTD istream& WXUNUSED(str)) |
fb42d7c3 VZ |
1259 | { |
1260 | // Not implemented | |
cab1a605 | 1261 | return false; |
fb42d7c3 VZ |
1262 | } |
1263 | #endif | |
1264 | ||
1265 | ||
2562c823 | 1266 | bool wxVariantDataDateTime::Read(wxString& str) |
fb42d7c3 | 1267 | { |
be53e8ae JS |
1268 | if ( str == wxS("Invalid") ) |
1269 | { | |
1270 | m_value = wxInvalidDateTime; | |
1271 | return true; | |
1272 | } | |
1273 | ||
c398434d VZ |
1274 | wxString::const_iterator end; |
1275 | return m_value.ParseDateTime(str, &end) && end == str.end(); | |
fb42d7c3 VZ |
1276 | } |
1277 | ||
2562c823 | 1278 | // wxVariant |
fb42d7c3 | 1279 | |
ff818ab8 RG |
1280 | wxVariant::wxVariant(const wxDateTime& val, const wxString& name) // Date |
1281 | { | |
cab1a605 WS |
1282 | m_data = new wxVariantDataDateTime(val); |
1283 | m_name = name; | |
ff818ab8 | 1284 | } |
ff818ab8 | 1285 | |
2562c823 | 1286 | bool wxVariant::operator== (const wxDateTime& value) const |
fb42d7c3 | 1287 | { |
2562c823 RR |
1288 | wxDateTime thisValue; |
1289 | if (!Convert(&thisValue)) | |
1290 | return false; | |
1291 | ||
1292 | return value.IsEqualTo(thisValue); | |
fb42d7c3 | 1293 | } |
edca7a82 | 1294 | |
2562c823 | 1295 | bool wxVariant::operator!= (const wxDateTime& value) const |
341287bf | 1296 | { |
2562c823 RR |
1297 | return (!((*this) == value)); |
1298 | } | |
1299 | ||
1300 | void wxVariant::operator= (const wxDateTime& value) | |
1301 | { | |
1302 | if (GetType() == wxT("datetime") && | |
1303 | m_data->GetRefCount() == 1) | |
341287bf | 1304 | { |
2562c823 | 1305 | ((wxVariantDataDateTime*)GetData())->SetValue(value); |
341287bf | 1306 | } |
4fabb575 | 1307 | else |
2562c823 RR |
1308 | { |
1309 | UnRef(); | |
1310 | m_data = new wxVariantDataDateTime(value); | |
1311 | } | |
341287bf JS |
1312 | } |
1313 | ||
2562c823 RR |
1314 | wxDateTime wxVariant::GetDateTime() const |
1315 | { | |
1316 | wxDateTime value; | |
1317 | if (!Convert(& value)) | |
341287bf | 1318 | { |
2562c823 | 1319 | wxFAIL_MSG(wxT("Could not convert to a datetime")); |
341287bf | 1320 | } |
3d8daa0f | 1321 | |
2562c823 | 1322 | return value; |
341287bf JS |
1323 | } |
1324 | ||
2562c823 | 1325 | #endif // wxUSE_DATETIME |
341287bf | 1326 | |
2562c823 RR |
1327 | // ---------------------------------------------------------------------------- |
1328 | // wxVariantDataArrayString | |
1329 | // ---------------------------------------------------------------------------- | |
1330 | ||
1331 | class wxVariantDataArrayString: public wxVariantData | |
341287bf | 1332 | { |
2562c823 RR |
1333 | public: |
1334 | wxVariantDataArrayString() { } | |
1335 | wxVariantDataArrayString(const wxArrayString& value) { m_value = value; } | |
341287bf | 1336 | |
2562c823 RR |
1337 | wxArrayString GetValue() const { return m_value; } |
1338 | void SetValue(const wxArrayString& value) { m_value = value; } | |
341287bf | 1339 | |
2562c823 RR |
1340 | virtual bool Eq(wxVariantData& data) const; |
1341 | #if wxUSE_STD_IOSTREAM | |
1342 | virtual bool Write(wxSTD ostream& str) const; | |
1343 | #endif | |
1344 | virtual bool Write(wxString& str) const; | |
1345 | #if wxUSE_STD_IOSTREAM | |
1346 | virtual bool Read(wxSTD istream& str); | |
1347 | #endif | |
1348 | virtual bool Read(wxString& str); | |
47b378bd | 1349 | virtual wxString GetType() const { return wxT("arrstring"); } |
c8058a09 | 1350 | virtual wxVariantData* Clone() const { return new wxVariantDataArrayString(m_value); } |
341287bf | 1351 | |
2562c823 RR |
1352 | protected: |
1353 | wxArrayString m_value; | |
2562c823 | 1354 | }; |
bc14c8b2 | 1355 | |
2562c823 | 1356 | bool wxVariantDataArrayString::Eq(wxVariantData& data) const |
341287bf | 1357 | { |
2562c823 | 1358 | wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") ); |
341287bf | 1359 | |
2562c823 | 1360 | wxVariantDataArrayString& otherData = (wxVariantDataArrayString&) data; |
341287bf | 1361 | |
2562c823 | 1362 | return otherData.m_value == m_value; |
341287bf JS |
1363 | } |
1364 | ||
2562c823 RR |
1365 | #if wxUSE_STD_IOSTREAM |
1366 | bool wxVariantDataArrayString::Write(wxSTD ostream& WXUNUSED(str)) const | |
341287bf | 1367 | { |
2562c823 RR |
1368 | // Not implemented |
1369 | return false; | |
341287bf | 1370 | } |
2562c823 | 1371 | #endif |
341287bf | 1372 | |
2562c823 | 1373 | bool wxVariantDataArrayString::Write(wxString& str) const |
341287bf | 1374 | { |
2562c823 RR |
1375 | size_t count = m_value.GetCount(); |
1376 | for ( size_t n = 0; n < count; n++ ) | |
341287bf | 1377 | { |
2562c823 RR |
1378 | if ( n ) |
1379 | str += _T(';'); | |
1380 | ||
1381 | str += m_value[n]; | |
341287bf | 1382 | } |
341287bf | 1383 | |
2562c823 | 1384 | return true; |
341287bf JS |
1385 | } |
1386 | ||
2562c823 RR |
1387 | |
1388 | #if wxUSE_STD_IOSTREAM | |
1389 | bool wxVariantDataArrayString::Read(wxSTD istream& WXUNUSED(str)) | |
341287bf | 1390 | { |
2562c823 RR |
1391 | // Not implemented |
1392 | return false; | |
341287bf | 1393 | } |
2562c823 | 1394 | #endif |
341287bf | 1395 | |
2562c823 RR |
1396 | |
1397 | bool wxVariantDataArrayString::Read(wxString& str) | |
341287bf | 1398 | { |
2562c823 RR |
1399 | wxStringTokenizer tk(str, _T(";")); |
1400 | while ( tk.HasMoreTokens() ) | |
341287bf | 1401 | { |
2562c823 | 1402 | m_value.Add(tk.GetNextToken()); |
341287bf | 1403 | } |
341287bf | 1404 | |
2562c823 | 1405 | return true; |
341287bf JS |
1406 | } |
1407 | ||
2562c823 RR |
1408 | // wxVariant |
1409 | ||
1410 | wxVariant::wxVariant(const wxArrayString& val, const wxString& name) // Strings | |
1411 | { | |
1412 | m_data = new wxVariantDataArrayString(val); | |
1413 | m_name = name; | |
341287bf JS |
1414 | } |
1415 | ||
2562c823 | 1416 | bool wxVariant::operator==(const wxArrayString& WXUNUSED(value)) const |
341287bf | 1417 | { |
2562c823 | 1418 | wxFAIL_MSG( _T("TODO") ); |
f6bcfd97 | 1419 | |
2562c823 | 1420 | return false; |
341287bf JS |
1421 | } |
1422 | ||
2562c823 | 1423 | bool wxVariant::operator!=(const wxArrayString& value) const |
341287bf | 1424 | { |
2562c823 | 1425 | return !(*this == value); |
341287bf JS |
1426 | } |
1427 | ||
2562c823 | 1428 | void wxVariant::operator=(const wxArrayString& value) |
341287bf | 1429 | { |
2562c823 RR |
1430 | if (GetType() == wxT("arrstring") && |
1431 | m_data->GetRefCount() == 1) | |
341287bf | 1432 | { |
2562c823 | 1433 | ((wxVariantDataArrayString *)GetData())->SetValue(value); |
341287bf JS |
1434 | } |
1435 | else | |
1436 | { | |
2562c823 RR |
1437 | UnRef(); |
1438 | m_data = new wxVariantDataArrayString(value); | |
341287bf JS |
1439 | } |
1440 | } | |
1441 | ||
2562c823 | 1442 | wxArrayString wxVariant::GetArrayString() const |
341287bf | 1443 | { |
2562c823 RR |
1444 | if ( GetType() == wxT("arrstring") ) |
1445 | return ((wxVariantDataArrayString *)GetData())->GetValue(); | |
1446 | ||
1447 | return wxArrayString(); | |
341287bf JS |
1448 | } |
1449 | ||
2562c823 RR |
1450 | // ---------------------------------------------------------------------------- |
1451 | // wxVariantDataList | |
1452 | // ---------------------------------------------------------------------------- | |
2c3a1064 | 1453 | |
2562c823 | 1454 | class WXDLLIMPEXP_BASE wxVariantDataList: public wxVariantData |
341287bf | 1455 | { |
2562c823 RR |
1456 | public: |
1457 | wxVariantDataList() {} | |
9a0a58f5 | 1458 | wxVariantDataList(const wxVariantList& list); |
2562c823 | 1459 | virtual ~wxVariantDataList(); |
341287bf | 1460 | |
9a0a58f5 RR |
1461 | wxVariantList& GetValue() { return m_value; } |
1462 | void SetValue(const wxVariantList& value) ; | |
341287bf | 1463 | |
2562c823 RR |
1464 | virtual bool Eq(wxVariantData& data) const; |
1465 | #if wxUSE_STD_IOSTREAM | |
1466 | virtual bool Write(wxSTD ostream& str) const; | |
1467 | #endif | |
1468 | virtual bool Write(wxString& str) const; | |
1469 | #if wxUSE_STD_IOSTREAM | |
1470 | virtual bool Read(wxSTD istream& str); | |
1471 | #endif | |
1472 | virtual bool Read(wxString& str); | |
47b378bd | 1473 | virtual wxString GetType() const { return wxT("list"); } |
52e81242 | 1474 | |
2562c823 | 1475 | void Clear(); |
341287bf | 1476 | |
c8058a09 | 1477 | wxVariantData* Clone() const { return new wxVariantDataList(m_value); } |
2562c823 | 1478 | protected: |
9a0a58f5 | 1479 | wxVariantList m_value; |
2562c823 | 1480 | }; |
341287bf | 1481 | |
9a0a58f5 | 1482 | wxVariantDataList::wxVariantDataList(const wxVariantList& list) |
341287bf | 1483 | { |
2562c823 | 1484 | SetValue(list); |
341287bf JS |
1485 | } |
1486 | ||
2562c823 | 1487 | wxVariantDataList::~wxVariantDataList() |
341287bf | 1488 | { |
2562c823 | 1489 | Clear(); |
341287bf JS |
1490 | } |
1491 | ||
9a0a58f5 | 1492 | void wxVariantDataList::SetValue(const wxVariantList& value) |
341287bf | 1493 | { |
2562c823 | 1494 | Clear(); |
9a0a58f5 | 1495 | wxVariantList::compatibility_iterator node = value.GetFirst(); |
2562c823 | 1496 | while (node) |
341287bf | 1497 | { |
9a0a58f5 | 1498 | wxVariant* var = node->GetData(); |
2562c823 RR |
1499 | m_value.Append(new wxVariant(*var)); |
1500 | node = node->GetNext(); | |
341287bf | 1501 | } |
2562c823 RR |
1502 | } |
1503 | ||
1504 | void wxVariantDataList::Clear() | |
1505 | { | |
9a0a58f5 | 1506 | wxVariantList::compatibility_iterator node = m_value.GetFirst(); |
2562c823 | 1507 | while (node) |
341287bf | 1508 | { |
9a0a58f5 | 1509 | wxVariant* var = node->GetData(); |
2562c823 RR |
1510 | delete var; |
1511 | node = node->GetNext(); | |
341287bf | 1512 | } |
2562c823 | 1513 | m_value.Clear(); |
341287bf JS |
1514 | } |
1515 | ||
2562c823 | 1516 | bool wxVariantDataList::Eq(wxVariantData& data) const |
a0a302dc | 1517 | { |
2562c823 RR |
1518 | wxASSERT_MSG( (data.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") ); |
1519 | ||
1520 | wxVariantDataList& listData = (wxVariantDataList&) data; | |
9a0a58f5 RR |
1521 | wxVariantList::compatibility_iterator node1 = m_value.GetFirst(); |
1522 | wxVariantList::compatibility_iterator node2 = listData.GetValue().GetFirst(); | |
2562c823 RR |
1523 | while (node1 && node2) |
1524 | { | |
9a0a58f5 RR |
1525 | wxVariant* var1 = node1->GetData(); |
1526 | wxVariant* var2 = node2->GetData(); | |
2562c823 RR |
1527 | if ((*var1) != (*var2)) |
1528 | return false; | |
1529 | node1 = node1->GetNext(); | |
1530 | node2 = node2->GetNext(); | |
1531 | } | |
1532 | if (node1 || node2) return false; | |
1533 | return true; | |
a0a302dc JS |
1534 | } |
1535 | ||
2562c823 RR |
1536 | #if wxUSE_STD_IOSTREAM |
1537 | bool wxVariantDataList::Write(wxSTD ostream& str) const | |
a0a302dc | 1538 | { |
2562c823 RR |
1539 | wxString s; |
1540 | Write(s); | |
1541 | str << (const char*) s.mb_str(); | |
1542 | return true; | |
a0a302dc | 1543 | } |
2562c823 | 1544 | #endif |
a0a302dc | 1545 | |
2562c823 | 1546 | bool wxVariantDataList::Write(wxString& str) const |
a0a302dc | 1547 | { |
2562c823 | 1548 | str = wxEmptyString; |
9a0a58f5 | 1549 | wxVariantList::compatibility_iterator node = m_value.GetFirst(); |
2562c823 | 1550 | while (node) |
a0a302dc | 1551 | { |
9a0a58f5 | 1552 | wxVariant* var = node->GetData(); |
2562c823 RR |
1553 | if (node != m_value.GetFirst()) |
1554 | str += wxT(" "); | |
1555 | wxString str1; | |
1556 | str += var->MakeString(); | |
1557 | node = node->GetNext(); | |
a0a302dc | 1558 | } |
2562c823 RR |
1559 | |
1560 | return true; | |
a0a302dc | 1561 | } |
2f620946 | 1562 | |
2562c823 RR |
1563 | #if wxUSE_STD_IOSTREAM |
1564 | bool wxVariantDataList::Read(wxSTD istream& WXUNUSED(str)) | |
2f620946 | 1565 | { |
2562c823 RR |
1566 | wxFAIL_MSG(wxT("Unimplemented")); |
1567 | // TODO | |
1568 | return false; | |
2f620946 | 1569 | } |
2562c823 | 1570 | #endif |
2f620946 | 1571 | |
2562c823 | 1572 | bool wxVariantDataList::Read(wxString& WXUNUSED(str)) |
2f620946 | 1573 | { |
2562c823 RR |
1574 | wxFAIL_MSG(wxT("Unimplemented")); |
1575 | // TODO | |
1576 | return false; | |
2f620946 RR |
1577 | } |
1578 | ||
2562c823 RR |
1579 | // wxVariant |
1580 | ||
9a0a58f5 | 1581 | wxVariant::wxVariant(const wxVariantList& val, const wxString& name) // List of variants |
2f620946 | 1582 | { |
2562c823 RR |
1583 | m_data = new wxVariantDataList(val); |
1584 | m_name = name; | |
2f620946 | 1585 | } |
341287bf | 1586 | |
9a0a58f5 | 1587 | bool wxVariant::operator== (const wxVariantList& value) const |
edca7a82 | 1588 | { |
2562c823 | 1589 | wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") ); |
edca7a82 | 1590 | |
2562c823 RR |
1591 | wxVariantDataList other(value); |
1592 | return (GetData()->Eq(other)); | |
edca7a82 GT |
1593 | } |
1594 | ||
9a0a58f5 | 1595 | bool wxVariant::operator!= (const wxVariantList& value) const |
edca7a82 GT |
1596 | { |
1597 | return (!((*this) == value)); | |
1598 | } | |
1599 | ||
9a0a58f5 | 1600 | void wxVariant::operator= (const wxVariantList& value) |
edca7a82 | 1601 | { |
2562c823 RR |
1602 | if (GetType() == wxT("list") && |
1603 | m_data->GetRefCount() == 1) | |
edca7a82 | 1604 | { |
2562c823 | 1605 | ((wxVariantDataList*)GetData())->SetValue(value); |
edca7a82 GT |
1606 | } |
1607 | else | |
1608 | { | |
2562c823 RR |
1609 | UnRef(); |
1610 | m_data = new wxVariantDataList(value); | |
edca7a82 GT |
1611 | } |
1612 | } | |
1613 | ||
9a0a58f5 | 1614 | wxVariantList& wxVariant::GetList() const |
edca7a82 | 1615 | { |
2562c823 | 1616 | wxASSERT( (GetType() == wxT("list")) ); |
edca7a82 | 1617 | |
9a0a58f5 | 1618 | return (wxVariantList&) ((wxVariantDataList*) m_data)->GetValue(); |
2562c823 | 1619 | } |
edca7a82 | 1620 | |
2562c823 RR |
1621 | // Make empty list |
1622 | void wxVariant::NullList() | |
edca7a82 | 1623 | { |
2562c823 | 1624 | SetData(new wxVariantDataList()); |
edca7a82 GT |
1625 | } |
1626 | ||
2562c823 RR |
1627 | // Append to list |
1628 | void wxVariant::Append(const wxVariant& value) | |
edca7a82 | 1629 | { |
9a0a58f5 | 1630 | wxVariantList& list = GetList(); |
2562c823 RR |
1631 | |
1632 | list.Append(new wxVariant(value)); | |
edca7a82 GT |
1633 | } |
1634 | ||
2562c823 RR |
1635 | // Insert at front of list |
1636 | void wxVariant::Insert(const wxVariant& value) | |
1637 | { | |
9a0a58f5 | 1638 | wxVariantList& list = GetList(); |
fb42d7c3 | 1639 | |
2562c823 RR |
1640 | list.Insert(new wxVariant(value)); |
1641 | } | |
1642 | ||
1643 | // Returns true if the variant is a member of the list | |
1644 | bool wxVariant::Member(const wxVariant& value) const | |
fb42d7c3 | 1645 | { |
9a0a58f5 | 1646 | wxVariantList& list = GetList(); |
fb42d7c3 | 1647 | |
9a0a58f5 | 1648 | wxVariantList::compatibility_iterator node = list.GetFirst(); |
2562c823 RR |
1649 | while (node) |
1650 | { | |
9a0a58f5 | 1651 | wxVariant* other = node->GetData(); |
2562c823 RR |
1652 | if (value == *other) |
1653 | return true; | |
1654 | node = node->GetNext(); | |
1655 | } | |
cab1a605 | 1656 | return false; |
fb42d7c3 VZ |
1657 | } |
1658 | ||
2562c823 RR |
1659 | // Deletes the nth element of the list |
1660 | bool wxVariant::Delete(size_t item) | |
fb42d7c3 | 1661 | { |
9a0a58f5 | 1662 | wxVariantList& list = GetList(); |
2562c823 RR |
1663 | |
1664 | wxASSERT_MSG( (item < list.GetCount()), wxT("Invalid index to Delete") ); | |
9a0a58f5 RR |
1665 | wxVariantList::compatibility_iterator node = list.Item(item); |
1666 | wxVariant* variant = node->GetData(); | |
2562c823 RR |
1667 | delete variant; |
1668 | list.Erase(node); | |
1669 | return true; | |
fb42d7c3 VZ |
1670 | } |
1671 | ||
2562c823 RR |
1672 | // Clear list |
1673 | void wxVariant::ClearList() | |
fb42d7c3 | 1674 | { |
2562c823 | 1675 | if (!IsNull() && (GetType() == wxT("list"))) |
fb42d7c3 | 1676 | { |
2562c823 | 1677 | ((wxVariantDataList*) m_data)->Clear(); |
fb42d7c3 VZ |
1678 | } |
1679 | else | |
1680 | { | |
2562c823 RR |
1681 | if (!GetType().IsSameAs(wxT("list"))) |
1682 | UnRef(); | |
fb42d7c3 | 1683 | |
2562c823 RR |
1684 | m_data = new wxVariantDataList; |
1685 | } | |
fb42d7c3 VZ |
1686 | } |
1687 | ||
60acae80 RR |
1688 | // Treat a list variant as an array |
1689 | wxVariant wxVariant::operator[] (size_t idx) const | |
1690 | { | |
60acae80 | 1691 | wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") ); |
60acae80 RR |
1692 | |
1693 | if (GetType() == wxT("list")) | |
1694 | { | |
1695 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
1696 | wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); | |
9a0a58f5 | 1697 | return *(data->GetValue().Item(idx)->GetData()); |
60acae80 | 1698 | } |
60acae80 RR |
1699 | return wxNullVariant; |
1700 | } | |
1701 | ||
1702 | wxVariant& wxVariant::operator[] (size_t idx) | |
1703 | { | |
1704 | // We can't return a reference to a variant for a string list, since the string | |
1705 | // is actually stored as a char*, not a variant. | |
1706 | ||
1707 | wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") ); | |
1708 | ||
1709 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
1710 | wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); | |
1711 | ||
9a0a58f5 | 1712 | return * (data->GetValue().Item(idx)->GetData()); |
60acae80 RR |
1713 | } |
1714 | ||
1715 | // Return the number of elements in a list | |
1716 | size_t wxVariant::GetCount() const | |
1717 | { | |
60acae80 | 1718 | wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") ); |
60acae80 RR |
1719 | |
1720 | if (GetType() == wxT("list")) | |
1721 | { | |
1722 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
1723 | return data->GetValue().GetCount(); | |
1724 | } | |
60acae80 RR |
1725 | return 0; |
1726 | } | |
1727 | ||
2562c823 | 1728 | // ---------------------------------------------------------------------------- |
341287bf | 1729 | // Type conversion |
2562c823 RR |
1730 | // ---------------------------------------------------------------------------- |
1731 | ||
341287bf JS |
1732 | bool wxVariant::Convert(long* value) const |
1733 | { | |
1734 | wxString type(GetType()); | |
223d09f6 | 1735 | if (type == wxT("double")) |
2562c823 | 1736 | *value = (long) (((wxVariantDoubleData*)GetData())->GetValue()); |
223d09f6 | 1737 | else if (type == wxT("long")) |
341287bf | 1738 | *value = ((wxVariantDataLong*)GetData())->GetValue(); |
223d09f6 | 1739 | else if (type == wxT("bool")) |
341287bf | 1740 | *value = (long) (((wxVariantDataBool*)GetData())->GetValue()); |
223d09f6 | 1741 | else if (type == wxT("string")) |
52de37c7 | 1742 | *value = wxAtol(((wxVariantDataString*)GetData())->GetValue()); |
341287bf | 1743 | else |
cab1a605 | 1744 | return false; |
341287bf | 1745 | |
cab1a605 | 1746 | return true; |
341287bf JS |
1747 | } |
1748 | ||
1749 | bool wxVariant::Convert(bool* value) const | |
1750 | { | |
1751 | wxString type(GetType()); | |
223d09f6 | 1752 | if (type == wxT("double")) |
2562c823 | 1753 | *value = ((int) (((wxVariantDoubleData*)GetData())->GetValue()) != 0); |
223d09f6 | 1754 | else if (type == wxT("long")) |
341287bf | 1755 | *value = (((wxVariantDataLong*)GetData())->GetValue() != 0); |
223d09f6 | 1756 | else if (type == wxT("bool")) |
341287bf | 1757 | *value = ((wxVariantDataBool*)GetData())->GetValue(); |
223d09f6 | 1758 | else if (type == wxT("string")) |
341287bf JS |
1759 | { |
1760 | wxString val(((wxVariantDataString*)GetData())->GetValue()); | |
1761 | val.MakeLower(); | |
b1638abf | 1762 | if (val == wxT("true") || val == wxT("yes") || val == wxT('1') ) |
cab1a605 | 1763 | *value = true; |
b1638abf | 1764 | else if (val == wxT("false") || val == wxT("no") || val == wxT('0') ) |
cab1a605 | 1765 | *value = false; |
341287bf | 1766 | else |
cab1a605 | 1767 | return false; |
341287bf JS |
1768 | } |
1769 | else | |
cab1a605 | 1770 | return false; |
341287bf | 1771 | |
cab1a605 | 1772 | return true; |
341287bf JS |
1773 | } |
1774 | ||
1775 | bool wxVariant::Convert(double* value) const | |
1776 | { | |
1777 | wxString type(GetType()); | |
223d09f6 | 1778 | if (type == wxT("double")) |
2562c823 | 1779 | *value = ((wxVariantDoubleData*)GetData())->GetValue(); |
223d09f6 | 1780 | else if (type == wxT("long")) |
341287bf | 1781 | *value = (double) (((wxVariantDataLong*)GetData())->GetValue()); |
223d09f6 | 1782 | else if (type == wxT("bool")) |
341287bf | 1783 | *value = (double) (((wxVariantDataBool*)GetData())->GetValue()); |
223d09f6 | 1784 | else if (type == wxT("string")) |
52de37c7 | 1785 | *value = (double) wxAtof(((wxVariantDataString*)GetData())->GetValue()); |
341287bf | 1786 | else |
cab1a605 | 1787 | return false; |
341287bf | 1788 | |
cab1a605 | 1789 | return true; |
341287bf JS |
1790 | } |
1791 | ||
af717fa8 | 1792 | bool wxVariant::Convert(wxUniChar* value) const |
341287bf JS |
1793 | { |
1794 | wxString type(GetType()); | |
223d09f6 | 1795 | if (type == wxT("char")) |
341287bf | 1796 | *value = ((wxVariantDataChar*)GetData())->GetValue(); |
223d09f6 | 1797 | else if (type == wxT("long")) |
341287bf | 1798 | *value = (char) (((wxVariantDataLong*)GetData())->GetValue()); |
223d09f6 | 1799 | else if (type == wxT("bool")) |
341287bf JS |
1800 | *value = (char) (((wxVariantDataBool*)GetData())->GetValue()); |
1801 | else | |
cab1a605 | 1802 | return false; |
341287bf | 1803 | |
cab1a605 | 1804 | return true; |
341287bf JS |
1805 | } |
1806 | ||
af717fa8 VS |
1807 | bool wxVariant::Convert(char* value) const |
1808 | { | |
1809 | wxUniChar ch; | |
1810 | if ( !Convert(&ch) ) | |
1811 | return false; | |
1812 | *value = ch; | |
1813 | return true; | |
1814 | } | |
1815 | ||
1816 | bool 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 |
1825 | bool wxVariant::Convert(wxString* value) const |
1826 | { | |
1827 | *value = MakeString(); | |
cab1a605 | 1828 | return true; |
341287bf JS |
1829 | } |
1830 | ||
e2b87f38 | 1831 | #if wxUSE_DATETIME |
edca7a82 GT |
1832 | bool 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 | } |
73799292 | 1840 | |
9708db20 JS |
1841 | // Fallback to string conversion |
1842 | wxString val; | |
73799292 VZ |
1843 | if ( !Convert(&val) ) |
1844 | return false; | |
1845 | ||
1846 | // Try to parse this as either date and time, only date or only time | |
1847 | // checking that the entire string was parsed | |
1848 | wxString::const_iterator end; | |
1849 | if ( value->ParseDateTime(val, &end) && end == val.end() ) | |
1850 | return true; | |
1851 | ||
1852 | if ( value->ParseDate(val, &end) && end == val.end() ) | |
1853 | return true; | |
1854 | ||
1855 | if ( value->ParseTime(val, &end) && end == val.end() ) | |
1856 | return true; | |
1857 | ||
1858 | return false; | |
edca7a82 | 1859 | } |
b1e343f2 | 1860 | #endif // wxUSE_DATETIME |
d5dc103f VZ |
1861 | |
1862 | #endif // wxUSE_VARIANT |