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