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