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