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