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