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