]>
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 "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() == "list"), "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() == "list"), "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; | |
143 | return TRUE; | |
144 | } | |
145 | ||
146 | bool wxVariantDataList::Write(wxString& str) const | |
147 | { | |
148 | str = ""; | |
149 | wxNode* node = m_value.First(); | |
150 | while (node) | |
151 | { | |
152 | wxVariant* var = (wxVariant*) node->Data(); | |
153 | if (node != m_value.First()) | |
154 | str += " "; | |
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("Unimplemented"); | |
166 | // TODO | |
167 | return FALSE; | |
168 | } | |
169 | ||
170 | bool wxVariantDataList::Read(wxString& WXUNUSED(str)) | |
171 | { | |
172 | wxFAIL_MSG("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 "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() == "stringlist"), "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() == "stringlist"), "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 ((char*) node1->Data()); | |
229 | wxString str2 ((char*) 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; | |
244 | return TRUE; | |
245 | } | |
246 | ||
247 | bool wxVariantDataStringList::Write(wxString& str) const | |
248 | { | |
249 | str = ""; | |
250 | wxNode* node = m_value.First(); | |
251 | while (node) | |
252 | { | |
253 | char* s = (char*) node->Data(); | |
254 | if (node != m_value.First()) | |
255 | str += " "; | |
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("Unimplemented"); | |
266 | // TODO | |
267 | return FALSE; | |
268 | } | |
269 | ||
270 | bool wxVariantDataStringList::Read(wxString& WXUNUSED(str)) | |
271 | { | |
272 | wxFAIL_MSG("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 "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() == "long"), "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() == "long"), "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; | |
335 | return TRUE; | |
336 | } | |
337 | ||
338 | bool wxVariantDataLong::Write(wxString& str) const | |
339 | { | |
340 | str.Printf("%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 = atol((const char*) 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 "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() == "double"), "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() == "double"), "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; | |
425 | return TRUE; | |
426 | } | |
427 | ||
428 | bool wxVariantDataReal::Write(wxString& str) const | |
429 | { | |
430 | str.Printf("%.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 = atof((const char*) 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 "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() == "bool"), "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() == "bool"), "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; | |
516 | return TRUE; | |
517 | } | |
518 | ||
519 | bool wxVariantDataBool::Write(wxString& str) const | |
520 | { | |
521 | str.Printf("%d", (int) m_value); | |
522 | return TRUE; | |
523 | } | |
524 | ||
525 | bool wxVariantDataBool::Read(istream& WXUNUSED(str)) | |
526 | { | |
527 | wxFAIL_MSG("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 = (atol((const char*) 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 "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() == "char"), "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() == "char"), "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; | |
608 | return TRUE; | |
609 | } | |
610 | ||
611 | bool wxVariantDataChar::Write(wxString& str) const | |
612 | { | |
613 | str.Printf("%c", m_value); | |
614 | return TRUE; | |
615 | } | |
616 | ||
617 | bool wxVariantDataChar::Read(istream& WXUNUSED(str)) | |
618 | { | |
619 | wxFAIL_MSG("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 "string"; }; | |
678 | ||
679 | protected: | |
680 | wxString m_value; | |
681 | }; | |
682 | ||
683 | void wxVariantDataString::Copy(wxVariantData& data) | |
684 | { | |
685 | wxASSERT_MSG( (data.GetType() == "string"), "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() == "string"), "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; | |
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 | str << (const char*) m_value; | |
723 | return TRUE; | |
724 | } | |
725 | ||
726 | bool wxVariantDataString::Read(wxInputStream& str) | |
727 | { | |
728 | str >> m_value; | |
729 | return TRUE; | |
730 | } | |
731 | #endif // wxUSE_STREAMS | |
732 | ||
733 | bool wxVariantDataString::Read(wxString& str) | |
734 | { | |
735 | m_value = str; | |
736 | return TRUE; | |
737 | } | |
738 | ||
739 | #if defined(__BORLANDC__) && defined(__WIN16__) | |
740 | IMPLEMENT_DYNAMIC_CLASS(wxVariantStringData, wxVariantData) | |
741 | #else | |
742 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString, wxVariantData) | |
743 | #endif | |
744 | ||
745 | /* | |
746 | * wxVariantDataTime | |
747 | */ | |
748 | ||
749 | #if wxUSE_TIMEDATE | |
750 | ||
751 | class wxVariantDataTime: public wxVariantData | |
752 | { | |
753 | DECLARE_DYNAMIC_CLASS(wxVariantDataTime) | |
754 | public: | |
755 | wxVariantDataTime() { } | |
756 | wxVariantDataTime(const wxTime& value) { m_value = value; } | |
757 | ||
758 | inline wxTime GetValue() const { return m_value; } | |
759 | inline void SetValue(const wxTime& value) { m_value = value; } | |
760 | ||
761 | virtual void Copy(wxVariantData& data); | |
762 | virtual bool Eq(wxVariantData& data) const; | |
763 | virtual bool Write(ostream& str) const; | |
764 | virtual bool Write(wxString& str) const; | |
765 | virtual bool Read(istream& str); | |
766 | virtual bool Read(wxString& str); | |
767 | virtual wxString GetType() const { return "time"; }; | |
768 | virtual wxVariantData* Clone() { return new wxVariantDataTime; } | |
769 | ||
770 | protected: | |
771 | wxTime m_value; | |
772 | }; | |
773 | ||
774 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataTime, wxVariantData) | |
775 | ||
776 | void wxVariantDataTime::Copy(wxVariantData& data) | |
777 | { | |
778 | wxASSERT_MSG( (data.GetType() == "time"), "wxVariantDataTime::Copy: Can't copy to this type of data" ); | |
779 | ||
780 | wxVariantDataTime& otherData = (wxVariantDataTime&) data; | |
781 | ||
782 | otherData.m_value = m_value; | |
783 | } | |
784 | ||
785 | bool wxVariantDataTime::Eq(wxVariantData& data) const | |
786 | { | |
787 | wxASSERT_MSG( (data.GetType() == "time"), "wxVariantDataTime::Eq: argument mismatch" ); | |
788 | ||
789 | wxVariantDataTime& otherData = (wxVariantDataTime&) data; | |
790 | ||
791 | return (otherData.m_value == m_value); | |
792 | } | |
793 | ||
794 | bool wxVariantDataTime::Write(ostream& str) const | |
795 | { | |
796 | wxString s; | |
797 | Write(s); | |
798 | str << (const char*) s; | |
799 | return TRUE; | |
800 | } | |
801 | ||
802 | bool wxVariantDataTime::Write(wxString& str) const | |
803 | { | |
804 | char*s = m_value.FormatTime(); | |
805 | str = s; | |
806 | return TRUE; | |
807 | } | |
808 | ||
809 | bool wxVariantDataTime::Read(istream& WXUNUSED(str)) | |
810 | { | |
811 | // Not implemented | |
812 | return FALSE; | |
813 | } | |
814 | ||
815 | bool wxVariantDataTime::Read(wxString& WXUNUSED(str)) | |
816 | { | |
817 | // Not implemented | |
818 | return FALSE; | |
819 | } | |
820 | ||
821 | /* | |
822 | * wxVariantDataDate | |
823 | */ | |
824 | ||
825 | class wxVariantDataDate: public wxVariantData | |
826 | { | |
827 | DECLARE_DYNAMIC_CLASS(wxVariantDataDate) | |
828 | public: | |
829 | wxVariantDataDate() { } | |
830 | wxVariantDataDate(const wxDate& value) { m_value = value; } | |
831 | ||
832 | inline wxDate GetValue() const { return m_value; } | |
833 | inline void SetValue(const wxDate& value) { m_value = value; } | |
834 | ||
835 | virtual void Copy(wxVariantData& data); | |
836 | virtual bool Eq(wxVariantData& data) const; | |
837 | virtual bool Write(ostream& str) const; | |
838 | virtual bool Write(wxString& str) const; | |
839 | virtual bool Read(istream& str); | |
840 | virtual bool Read(wxString& str); | |
841 | virtual wxString GetType() const { return "date"; }; | |
842 | virtual wxVariantData* Clone() { return new wxVariantDataDate; } | |
843 | ||
844 | protected: | |
845 | wxDate m_value; | |
846 | }; | |
847 | ||
848 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDate, wxVariantData) | |
849 | ||
850 | void wxVariantDataDate::Copy(wxVariantData& data) | |
851 | { | |
852 | wxASSERT_MSG( (data.GetType() == "date"), "wxVariantDataDate::Copy: Can't copy to this type of data" ); | |
853 | ||
854 | wxVariantDataDate& otherData = (wxVariantDataDate&) data; | |
855 | ||
856 | otherData.m_value = m_value; | |
857 | } | |
858 | ||
859 | bool wxVariantDataDate::Eq(wxVariantData& data) const | |
860 | { | |
861 | wxASSERT_MSG( (data.GetType() == "date"), "wxVariantDataDate::Eq: argument mismatch" ); | |
862 | ||
863 | wxVariantDataDate& otherData = (wxVariantDataDate&) data; | |
864 | ||
865 | return (otherData.m_value == m_value); | |
866 | } | |
867 | ||
868 | bool wxVariantDataDate::Write(ostream& str) const | |
869 | { | |
870 | wxString s; | |
871 | Write(s); | |
872 | str << (const char*) s; | |
873 | return TRUE; | |
874 | } | |
875 | ||
876 | bool wxVariantDataDate::Write(wxString& str) const | |
877 | { | |
878 | str = m_value.FormatDate(); | |
879 | return TRUE; | |
880 | } | |
881 | ||
882 | bool wxVariantDataDate::Read(istream& WXUNUSED(str)) | |
883 | { | |
884 | // Not implemented | |
885 | return FALSE; | |
886 | } | |
887 | ||
888 | bool wxVariantDataDate::Read(wxString& WXUNUSED(str)) | |
889 | { | |
890 | // Not implemented | |
891 | return FALSE; | |
892 | } | |
893 | #endif | |
894 | // wxUSE_TIMEDATE | |
895 | ||
896 | /* | |
897 | * wxVariantDataVoidPtr | |
898 | */ | |
899 | ||
900 | class wxVariantDataVoidPtr: public wxVariantData | |
901 | { | |
902 | DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr) | |
903 | public: | |
904 | wxVariantDataVoidPtr() { } | |
905 | wxVariantDataVoidPtr(void* value) { m_value = value; } | |
906 | ||
907 | inline void* GetValue() const { return m_value; } | |
908 | inline void SetValue(void* value) { m_value = value; } | |
909 | ||
910 | virtual void Copy(wxVariantData& data); | |
911 | virtual bool Eq(wxVariantData& data) const; | |
912 | virtual bool Write(ostream& str) const; | |
913 | virtual bool Write(wxString& str) const; | |
914 | virtual bool Read(istream& str); | |
915 | virtual bool Read(wxString& str); | |
916 | virtual wxString GetType() const { return "void*"; }; | |
917 | virtual wxVariantData* Clone() { return new wxVariantDataVoidPtr; } | |
918 | ||
919 | protected: | |
920 | void* m_value; | |
921 | }; | |
922 | ||
923 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr, wxVariantData) | |
924 | ||
925 | void wxVariantDataVoidPtr::Copy(wxVariantData& data) | |
926 | { | |
927 | wxASSERT_MSG( (data.GetType() == "void*"), "wxVariantDataVoidPtr::Copy: Can't copy to this type of data" ); | |
928 | ||
929 | wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data; | |
930 | ||
931 | otherData.m_value = m_value; | |
932 | } | |
933 | ||
934 | bool wxVariantDataVoidPtr::Eq(wxVariantData& data) const | |
935 | { | |
936 | wxASSERT_MSG( (data.GetType() == "void*"), "wxVariantDataVoidPtr::Eq: argument mismatch" ); | |
937 | ||
938 | wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data; | |
939 | ||
940 | return (otherData.m_value == m_value); | |
941 | } | |
942 | ||
943 | bool wxVariantDataVoidPtr::Write(ostream& str) const | |
944 | { | |
945 | wxString s; | |
946 | Write(s); | |
947 | str << (const char*) s; | |
948 | return TRUE; | |
949 | } | |
950 | ||
951 | bool wxVariantDataVoidPtr::Write(wxString& str) const | |
952 | { | |
953 | char buf[80]; | |
954 | sprintf(buf, "%ld", (long) m_value); | |
955 | str = buf; | |
956 | return TRUE; | |
957 | } | |
958 | ||
959 | bool wxVariantDataVoidPtr::Read(istream& WXUNUSED(str)) | |
960 | { | |
961 | // Not implemented | |
962 | return FALSE; | |
963 | } | |
964 | ||
965 | bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str)) | |
966 | { | |
967 | // Not implemented | |
968 | return FALSE; | |
969 | } | |
970 | ||
971 | ||
972 | /* | |
973 | * wxVariant | |
974 | */ | |
975 | ||
976 | IMPLEMENT_DYNAMIC_CLASS(wxVariant, wxObject) | |
977 | ||
978 | // Construction & destruction | |
979 | wxVariant::wxVariant() | |
980 | { | |
981 | m_data = (wxVariantData*) NULL; | |
982 | } | |
983 | ||
984 | wxVariant::wxVariant(double val, const wxString& name) | |
985 | { | |
986 | m_data = new wxVariantDataReal(val); | |
987 | m_name = name; | |
988 | } | |
989 | ||
990 | wxVariant::wxVariant(long val, const wxString& name) | |
991 | { | |
992 | m_data = new wxVariantDataLong(val); | |
993 | m_name = name; | |
994 | } | |
995 | ||
996 | #ifdef HAVE_BOOL | |
997 | wxVariant::wxVariant(bool val, const wxString& name) | |
998 | { | |
999 | m_data = new wxVariantDataBool(val); | |
1000 | m_name = name; | |
1001 | } | |
1002 | #endif | |
1003 | ||
1004 | wxVariant::wxVariant(char val, const wxString& name) | |
1005 | { | |
1006 | m_data = new wxVariantDataChar(val); | |
1007 | m_name = name; | |
1008 | } | |
1009 | ||
1010 | wxVariant::wxVariant(const wxString& val, const wxString& name) | |
1011 | { | |
1012 | m_data = new wxVariantDataString(val); | |
1013 | m_name = name; | |
1014 | } | |
1015 | ||
1016 | wxVariant::wxVariant(const char* val, const wxString& name) | |
1017 | { | |
1018 | m_data = new wxVariantDataString(wxString(val)); | |
1019 | m_name = name; | |
1020 | } | |
1021 | ||
1022 | wxVariant::wxVariant(const wxStringList& val, const wxString& name) | |
1023 | { | |
1024 | m_data = new wxVariantDataStringList(val); | |
1025 | m_name = name; | |
1026 | } | |
1027 | ||
1028 | wxVariant::wxVariant(const wxList& val, const wxString& name) // List of variants | |
1029 | { | |
1030 | m_data = new wxVariantDataList(val); | |
1031 | m_name = name; | |
1032 | } | |
1033 | ||
1034 | #if wxUSE_TIMEDATE | |
1035 | wxVariant::wxVariant(const wxTime& val, const wxString& name) // Time | |
1036 | { | |
1037 | m_data = new wxVariantDataTime(val); | |
1038 | m_name = name; | |
1039 | } | |
1040 | ||
1041 | wxVariant::wxVariant(const wxDate& val, const wxString& name) // Date | |
1042 | { | |
1043 | m_data = new wxVariantDataDate(val); | |
1044 | m_name = name; | |
1045 | } | |
1046 | #endif | |
1047 | ||
1048 | wxVariant::wxVariant(void* val, const wxString& name) // Void ptr | |
1049 | { | |
1050 | m_data = new wxVariantDataVoidPtr(val); | |
1051 | m_name = name; | |
1052 | } | |
1053 | ||
1054 | wxVariant::wxVariant(const wxVariant& variant) | |
1055 | { | |
1056 | if (!variant.IsNull()) | |
1057 | { | |
1058 | m_data = (wxVariantData*) variant.GetData()->GetClassInfo()->CreateObject(); | |
1059 | variant.m_data->Copy(*m_data); | |
1060 | } | |
1061 | else | |
1062 | m_data = (wxVariantData*) NULL; | |
1063 | m_name = variant.m_name; | |
1064 | } | |
1065 | ||
1066 | wxVariant::wxVariant(wxVariantData* data, const wxString& name) // User-defined data | |
1067 | { | |
1068 | m_data = data; | |
1069 | m_name = name; | |
1070 | } | |
1071 | ||
1072 | wxVariant::~wxVariant() | |
1073 | { | |
1074 | delete m_data; | |
1075 | } | |
1076 | ||
1077 | ||
1078 | // Make NULL (i.e. delete the data) | |
1079 | void wxVariant::MakeNull() | |
1080 | { | |
1081 | delete m_data; | |
1082 | m_data = NULL; | |
1083 | } | |
1084 | ||
1085 | // Generic operators | |
1086 | // Assignment | |
1087 | void wxVariant::operator= (const wxVariant& variant) | |
1088 | { | |
1089 | if (variant.IsNull()) | |
1090 | { | |
1091 | MakeNull(); | |
1092 | return; | |
1093 | } | |
1094 | ||
1095 | if (IsNull() || (GetType() != variant.GetType())) | |
1096 | { | |
1097 | if (m_data) | |
1098 | delete m_data; | |
1099 | m_data = (wxVariantData*) variant.GetData()->GetClassInfo()->CreateObject(); | |
1100 | } | |
1101 | // GetData()->Copy(* variant.GetData()); | |
1102 | variant.GetData()->Copy(* GetData()); | |
1103 | } | |
1104 | ||
1105 | // Assignment using data, e.g. | |
1106 | // myVariant = new wxStringVariantData("hello") | |
1107 | void wxVariant::operator= (wxVariantData* variantData) | |
1108 | { | |
1109 | MakeNull(); | |
1110 | m_data = variantData; | |
1111 | } | |
1112 | ||
1113 | bool wxVariant::operator== (const wxVariant& variant) const | |
1114 | { | |
1115 | if (IsNull() || variant.IsNull()) | |
1116 | return (IsNull() == variant.IsNull()); | |
1117 | ||
1118 | return (GetData()->Eq(* variant.GetData())); | |
1119 | } | |
1120 | ||
1121 | bool wxVariant::operator!= (const wxVariant& variant) const | |
1122 | { | |
1123 | return (!(*this == variant)); | |
1124 | } | |
1125 | ||
1126 | ||
1127 | // Specific operators | |
1128 | bool wxVariant::operator== (double value) const | |
1129 | { | |
1130 | double thisValue; | |
1131 | if (!Convert(&thisValue)) | |
1132 | return FALSE; | |
1133 | else | |
1134 | return (value == thisValue); | |
1135 | } | |
1136 | ||
1137 | bool wxVariant::operator!= (double value) const | |
1138 | { | |
1139 | return (!((*this) == value)); | |
1140 | } | |
1141 | ||
1142 | void wxVariant::operator= (double value) | |
1143 | { | |
1144 | if (GetType() == "double") | |
1145 | { | |
1146 | ((wxVariantDataReal*)GetData())->SetValue(value); | |
1147 | } | |
1148 | else | |
1149 | { | |
1150 | if (m_data) | |
1151 | delete m_data; | |
1152 | m_data = new wxVariantDataReal(value); | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | bool wxVariant::operator== (long value) const | |
1157 | { | |
1158 | long thisValue; | |
1159 | if (!Convert(&thisValue)) | |
1160 | return FALSE; | |
1161 | else | |
1162 | return (value == thisValue); | |
1163 | } | |
1164 | ||
1165 | bool wxVariant::operator!= (long value) const | |
1166 | { | |
1167 | return (!((*this) == value)); | |
1168 | } | |
1169 | ||
1170 | void wxVariant::operator= (long value) | |
1171 | { | |
1172 | if (GetType() == "long") | |
1173 | { | |
1174 | ((wxVariantDataLong*)GetData())->SetValue(value); | |
1175 | } | |
1176 | else | |
1177 | { | |
1178 | if (m_data) | |
1179 | delete m_data; | |
1180 | m_data = new wxVariantDataLong(value); | |
1181 | } | |
1182 | } | |
1183 | ||
1184 | bool wxVariant::operator== (char value) const | |
1185 | { | |
1186 | char thisValue; | |
1187 | if (!Convert(&thisValue)) | |
1188 | return FALSE; | |
1189 | else | |
1190 | return (value == thisValue); | |
1191 | } | |
1192 | ||
1193 | bool wxVariant::operator!= (char value) const | |
1194 | { | |
1195 | return (!((*this) == value)); | |
1196 | } | |
1197 | ||
1198 | void wxVariant::operator= (char value) | |
1199 | { | |
1200 | if (GetType() == "char") | |
1201 | { | |
1202 | ((wxVariantDataChar*)GetData())->SetValue(value); | |
1203 | } | |
1204 | else | |
1205 | { | |
1206 | if (m_data) | |
1207 | delete m_data; | |
1208 | m_data = new wxVariantDataChar(value); | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | #ifdef HAVE_BOOL | |
1213 | bool wxVariant::operator== (bool value) const | |
1214 | { | |
1215 | bool thisValue; | |
1216 | if (!Convert(&thisValue)) | |
1217 | return FALSE; | |
1218 | else | |
1219 | return (value == thisValue); | |
1220 | } | |
1221 | ||
1222 | bool wxVariant::operator!= (bool value) const | |
1223 | { | |
1224 | return (!((*this) == value)); | |
1225 | } | |
1226 | ||
1227 | void wxVariant::operator= (bool value) | |
1228 | { | |
1229 | if (GetType() == "bool") | |
1230 | { | |
1231 | ((wxVariantDataBool*)GetData())->SetValue(value); | |
1232 | } | |
1233 | else | |
1234 | { | |
1235 | if (m_data) | |
1236 | delete m_data; | |
1237 | m_data = new wxVariantDataBool(value); | |
1238 | } | |
1239 | } | |
1240 | #endif // HAVE_BOOL | |
1241 | ||
1242 | bool wxVariant::operator== (const wxString& value) const | |
1243 | { | |
1244 | wxString thisValue; | |
1245 | if (!Convert(&thisValue)) | |
1246 | return FALSE; | |
1247 | else | |
1248 | return (value == thisValue); | |
1249 | } | |
1250 | ||
1251 | bool wxVariant::operator!= (const wxString& value) const | |
1252 | { | |
1253 | return (!((*this) == value)); | |
1254 | } | |
1255 | ||
1256 | void wxVariant::operator= (const wxString& value) | |
1257 | { | |
1258 | if (GetType() == "string") | |
1259 | { | |
1260 | ((wxVariantDataString*)GetData())->SetValue(value); | |
1261 | } | |
1262 | else | |
1263 | { | |
1264 | if (m_data) | |
1265 | delete m_data; | |
1266 | m_data = new wxVariantDataString(value); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | void wxVariant::operator= (const char* value) | |
1271 | { | |
1272 | if (GetType() == "string") | |
1273 | { | |
1274 | ((wxVariantDataString*)GetData())->SetValue(wxString(value)); | |
1275 | } | |
1276 | else | |
1277 | { | |
1278 | if (m_data) | |
1279 | delete m_data; | |
1280 | m_data = new wxVariantDataString(wxString(value)); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | bool wxVariant::operator== (const wxStringList& value) const | |
1285 | { | |
1286 | wxASSERT_MSG( (GetType() == "stringlist"), "Invalid type for == operator" ); | |
1287 | ||
1288 | wxVariantDataStringList other(value); | |
1289 | return (m_data->Eq(other)); | |
1290 | } | |
1291 | ||
1292 | bool wxVariant::operator!= (const wxStringList& value) const | |
1293 | { | |
1294 | return (!((*this) == value)); | |
1295 | } | |
1296 | ||
1297 | void wxVariant::operator= (const wxStringList& value) | |
1298 | { | |
1299 | if (GetType() == "stringlist") | |
1300 | { | |
1301 | ((wxVariantDataStringList*)GetData())->SetValue(value); | |
1302 | } | |
1303 | else | |
1304 | { | |
1305 | if (m_data) | |
1306 | delete m_data; | |
1307 | m_data = new wxVariantDataStringList(value); | |
1308 | } | |
1309 | } | |
1310 | ||
1311 | bool wxVariant::operator== (const wxList& value) const | |
1312 | { | |
1313 | wxASSERT_MSG( (GetType() == "list"), "Invalid type for == operator" ); | |
1314 | ||
1315 | wxVariantDataList other(value); | |
1316 | return (m_data->Eq(other)); | |
1317 | } | |
1318 | ||
1319 | bool wxVariant::operator!= (const wxList& value) const | |
1320 | { | |
1321 | return (!((*this) == value)); | |
1322 | } | |
1323 | ||
1324 | void wxVariant::operator= (const wxList& value) | |
1325 | { | |
1326 | if (GetType() == "list") | |
1327 | { | |
1328 | ((wxVariantDataList*)GetData())->SetValue(value); | |
1329 | } | |
1330 | else | |
1331 | { | |
1332 | if (m_data) | |
1333 | delete m_data; | |
1334 | m_data = new wxVariantDataList(value); | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | #if wxUSE_TIMEDATE | |
1339 | bool wxVariant::operator== (const wxTime& value) const | |
1340 | { | |
1341 | wxTime thisValue; | |
1342 | if (!Convert(&thisValue)) | |
1343 | return FALSE; | |
1344 | else | |
1345 | return (value == thisValue); | |
1346 | } | |
1347 | ||
1348 | bool wxVariant::operator!= (const wxTime& value) const | |
1349 | { | |
1350 | return (!((*this) == value)); | |
1351 | } | |
1352 | ||
1353 | void wxVariant::operator= (const wxTime& value) | |
1354 | { | |
1355 | if (GetType() == "time") | |
1356 | { | |
1357 | ((wxVariantDataTime*)GetData())->SetValue(value); | |
1358 | } | |
1359 | else | |
1360 | { | |
1361 | if (m_data) | |
1362 | delete m_data; | |
1363 | m_data = new wxVariantDataTime(value); | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | bool wxVariant::operator== (const wxDate& value) const | |
1368 | { | |
1369 | wxDate thisValue; | |
1370 | if (!Convert(&thisValue)) | |
1371 | return FALSE; | |
1372 | else | |
1373 | return (value == thisValue); | |
1374 | } | |
1375 | ||
1376 | bool wxVariant::operator!= (const wxDate& value) const | |
1377 | { | |
1378 | return (!((*this) == value)); | |
1379 | } | |
1380 | ||
1381 | void wxVariant::operator= (const wxDate& value) | |
1382 | { | |
1383 | if (GetType() == "date") | |
1384 | { | |
1385 | ((wxVariantDataTime*)GetData())->SetValue(value); | |
1386 | } | |
1387 | else | |
1388 | { | |
1389 | if (m_data) | |
1390 | delete m_data; | |
1391 | m_data = new wxVariantDataDate(value); | |
1392 | } | |
1393 | } | |
1394 | #endif | |
1395 | ||
1396 | bool wxVariant::operator== (void* value) const | |
1397 | { | |
1398 | return (value == ((wxVariantDataVoidPtr*)GetData())->GetValue()); | |
1399 | } | |
1400 | ||
1401 | bool wxVariant::operator!= (void* value) const | |
1402 | { | |
1403 | return (!((*this) == value)); | |
1404 | } | |
1405 | ||
1406 | void wxVariant::operator= (void* value) | |
1407 | { | |
1408 | if (GetType() == "void*") | |
1409 | { | |
1410 | ((wxVariantDataVoidPtr*)GetData())->SetValue(value); | |
1411 | } | |
1412 | else | |
1413 | { | |
1414 | if (m_data) | |
1415 | delete m_data; | |
1416 | m_data = new wxVariantDataVoidPtr(value); | |
1417 | } | |
1418 | } | |
1419 | ||
1420 | // Treat a list variant as an array | |
1421 | wxVariant wxVariant::operator[] (size_t idx) const | |
1422 | { | |
1423 | wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for array operator" ); | |
1424 | ||
1425 | if (GetType() == "list") | |
1426 | { | |
1427 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
1428 | wxASSERT_MSG( (idx < (size_t) data->GetValue().Number()), "Invalid index for array" ); | |
1429 | return * (wxVariant*) (data->GetValue().Nth(idx)->Data()); | |
1430 | } | |
1431 | else if (GetType() == "stringlist") | |
1432 | { | |
1433 | wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; | |
1434 | wxASSERT_MSG( (idx < (size_t) data->GetValue().Number()), "Invalid index for array" ); | |
1435 | ||
1436 | wxVariant variant( wxString( (char*) (data->GetValue().Nth(idx)->Data()) )); | |
1437 | return variant; | |
1438 | } | |
1439 | return wxNullVariant; | |
1440 | } | |
1441 | ||
1442 | wxVariant& wxVariant::operator[] (size_t idx) | |
1443 | { | |
1444 | // We can't return a reference to a variant for a string list, since the string | |
1445 | // is actually stored as a char*, not a variant. | |
1446 | ||
1447 | wxASSERT_MSG( (GetType() == "list"), "Invalid type for array operator" ); | |
1448 | ||
1449 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
1450 | wxASSERT_MSG( (idx < (size_t) data->GetValue().Number()), "Invalid index for array" ); | |
1451 | ||
1452 | return * (wxVariant*) (data->GetValue().Nth(idx)->Data()); | |
1453 | } | |
1454 | ||
1455 | // Return the number of elements in a list | |
1456 | int wxVariant::GetCount() const | |
1457 | { | |
1458 | wxASSERT_MSG( (GetType() == "list" || GetType() == "stringlist"), "Invalid type for GetCount()" ); | |
1459 | ||
1460 | if (GetType() == "list") | |
1461 | { | |
1462 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
1463 | return data->GetValue().Number(); | |
1464 | } | |
1465 | else if (GetType() == "stringlist") | |
1466 | { | |
1467 | wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; | |
1468 | return data->GetValue().Number(); | |
1469 | } | |
1470 | return 0; | |
1471 | } | |
1472 | ||
1473 | wxString wxVariant::MakeString() const | |
1474 | { | |
1475 | if (!IsNull()) | |
1476 | { | |
1477 | wxString str; | |
1478 | if (GetData()->Write(str)) | |
1479 | return str; | |
1480 | } | |
1481 | return wxString(""); | |
1482 | } | |
1483 | ||
1484 | // Accessors | |
1485 | ||
1486 | void wxVariant::SetData(wxVariantData* data) | |
1487 | { | |
1488 | if (m_data) delete m_data; | |
1489 | m_data = data; | |
1490 | } | |
1491 | ||
1492 | ||
1493 | // Returns a string representing the type of the variant, | |
1494 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
1495 | wxString wxVariant::GetType() const | |
1496 | { | |
1497 | if (IsNull()) | |
1498 | return wxString("null"); | |
1499 | else | |
1500 | return m_data->GetType(); | |
1501 | } | |
1502 | ||
1503 | ||
1504 | bool wxVariant::IsType(const wxString& type) const | |
1505 | { | |
1506 | return (GetType() == type); | |
1507 | } | |
1508 | ||
1509 | ||
1510 | // Value accessors | |
1511 | double wxVariant::GetReal() const | |
1512 | { | |
1513 | double value; | |
1514 | if (Convert(& value)) | |
1515 | return value; | |
1516 | else | |
1517 | { | |
1518 | wxFAIL_MSG("Could not convert to a real number"); | |
1519 | return 0.0; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | long wxVariant::GetInteger() const | |
1524 | { | |
1525 | long value; | |
1526 | if (Convert(& value)) | |
1527 | return value; | |
1528 | else | |
1529 | { | |
1530 | wxFAIL_MSG("Could not convert to an integer"); | |
1531 | return 0; | |
1532 | } | |
1533 | } | |
1534 | ||
1535 | char wxVariant::GetChar() const | |
1536 | { | |
1537 | char value; | |
1538 | if (Convert(& value)) | |
1539 | return value; | |
1540 | else | |
1541 | { | |
1542 | wxFAIL_MSG("Could not convert to a char"); | |
1543 | return 0; | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | bool wxVariant::GetBool() const | |
1548 | { | |
1549 | bool value; | |
1550 | if (Convert(& value)) | |
1551 | return value; | |
1552 | else | |
1553 | { | |
1554 | wxFAIL_MSG("Could not convert to a bool"); | |
1555 | return 0; | |
1556 | } | |
1557 | } | |
1558 | ||
1559 | wxString wxVariant::GetString() const | |
1560 | { | |
1561 | wxString value; | |
1562 | if (Convert(& value)) | |
1563 | return value; | |
1564 | else | |
1565 | { | |
1566 | wxFAIL_MSG("Could not convert to a string"); | |
1567 | return wxString(""); | |
1568 | } | |
1569 | } | |
1570 | ||
1571 | #if wxUSE_TIMEDATE | |
1572 | wxTime wxVariant::GetTime() const | |
1573 | { | |
1574 | wxTime value; | |
1575 | if (Convert(& value)) | |
1576 | return value; | |
1577 | else | |
1578 | { | |
1579 | wxFAIL_MSG("Could not convert to a time"); | |
1580 | return wxTime(); | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | wxDate wxVariant::GetDate() const | |
1585 | { | |
1586 | wxDate value; | |
1587 | if (Convert(& value)) | |
1588 | return value; | |
1589 | else | |
1590 | { | |
1591 | wxFAIL_MSG("Could not convert to a date"); | |
1592 | return wxDate(); | |
1593 | } | |
1594 | } | |
1595 | #endif | |
1596 | ||
1597 | void* wxVariant::GetVoidPtr() const | |
1598 | { | |
1599 | wxASSERT( (GetType() == "void*") ); | |
1600 | ||
1601 | return (void*) ((wxVariantDataVoidPtr*) m_data)->GetValue(); | |
1602 | } | |
1603 | ||
1604 | wxList& wxVariant::GetList() const | |
1605 | { | |
1606 | wxASSERT( (GetType() == "list") ); | |
1607 | ||
1608 | return (wxList&) ((wxVariantDataList*) m_data)->GetValue(); | |
1609 | } | |
1610 | ||
1611 | wxStringList& wxVariant::GetStringList() const | |
1612 | { | |
1613 | wxASSERT( (GetType() == "stringlist") ); | |
1614 | ||
1615 | return (wxStringList&) ((wxVariantDataStringList*) m_data)->GetValue(); | |
1616 | } | |
1617 | ||
1618 | // Append to list | |
1619 | void wxVariant::Append(const wxVariant& value) | |
1620 | { | |
1621 | wxList& list = GetList(); | |
1622 | ||
1623 | list.Append(new wxVariant(value)); | |
1624 | } | |
1625 | ||
1626 | // Insert at front of list | |
1627 | void wxVariant::Insert(const wxVariant& value) | |
1628 | { | |
1629 | wxList& list = GetList(); | |
1630 | ||
1631 | list.Insert(new wxVariant(value)); | |
1632 | } | |
1633 | ||
1634 | // Returns TRUE if the variant is a member of the list | |
1635 | bool wxVariant::Member(const wxVariant& value) const | |
1636 | { | |
1637 | wxList& list = GetList(); | |
1638 | ||
1639 | wxNode* node = list.First(); | |
1640 | while (node) | |
1641 | { | |
1642 | wxVariant* other = (wxVariant*) node->Data(); | |
1643 | if (value == *other) | |
1644 | return TRUE; | |
1645 | node = node->Next(); | |
1646 | } | |
1647 | return FALSE; | |
1648 | } | |
1649 | ||
1650 | // Deletes the nth element of the list | |
1651 | bool wxVariant::Delete(int item) | |
1652 | { | |
1653 | wxList& list = GetList(); | |
1654 | ||
1655 | wxASSERT_MSG( (item < list.Number()), "Invalid index to Delete" ); | |
1656 | wxNode* node = list.Nth(item); | |
1657 | wxVariant* variant = (wxVariant*) node->Data(); | |
1658 | delete variant; | |
1659 | delete node; | |
1660 | return TRUE; | |
1661 | } | |
1662 | ||
1663 | // Clear list | |
1664 | void wxVariant::ClearList() | |
1665 | { | |
1666 | if (!IsNull() && (GetType() == "list")) | |
1667 | { | |
1668 | ((wxVariantDataList*) m_data)->Clear(); | |
1669 | } | |
1670 | else | |
1671 | { | |
1672 | if (GetType() != "list") | |
1673 | { | |
1674 | delete m_data; | |
1675 | m_data = NULL; | |
1676 | } | |
1677 | m_data = new wxVariantDataList; | |
1678 | } | |
1679 | } | |
1680 | ||
1681 | // Type conversion | |
1682 | bool wxVariant::Convert(long* value) const | |
1683 | { | |
1684 | wxString type(GetType()); | |
1685 | if (type == "double") | |
1686 | *value = (long) (((wxVariantDataReal*)GetData())->GetValue()); | |
1687 | else if (type == "long") | |
1688 | *value = ((wxVariantDataLong*)GetData())->GetValue(); | |
1689 | #ifdef HAVE_BOOL | |
1690 | else if (type == "bool") | |
1691 | *value = (long) (((wxVariantDataBool*)GetData())->GetValue()); | |
1692 | #endif | |
1693 | else if (type == "string") | |
1694 | *value = atol((const char*) ((wxVariantDataString*)GetData())->GetValue()); | |
1695 | else | |
1696 | return FALSE; | |
1697 | ||
1698 | return TRUE; | |
1699 | } | |
1700 | ||
1701 | bool wxVariant::Convert(bool* value) const | |
1702 | { | |
1703 | wxString type(GetType()); | |
1704 | if (type == "double") | |
1705 | *value = ((int) (((wxVariantDataReal*)GetData())->GetValue()) != 0); | |
1706 | else if (type == "long") | |
1707 | *value = (((wxVariantDataLong*)GetData())->GetValue() != 0); | |
1708 | #ifdef HAVE_BOOL | |
1709 | else if (type == "bool") | |
1710 | *value = ((wxVariantDataBool*)GetData())->GetValue(); | |
1711 | #endif | |
1712 | else if (type == "string") | |
1713 | { | |
1714 | wxString val(((wxVariantDataString*)GetData())->GetValue()); | |
1715 | val.MakeLower(); | |
1716 | if (val == "true" || val == "yes") | |
1717 | *value = TRUE; | |
1718 | else if (val == "false" || val == "no") | |
1719 | *value = FALSE; | |
1720 | else | |
1721 | return FALSE; | |
1722 | } | |
1723 | else | |
1724 | return FALSE; | |
1725 | ||
1726 | return TRUE; | |
1727 | } | |
1728 | ||
1729 | bool wxVariant::Convert(double* value) const | |
1730 | { | |
1731 | wxString type(GetType()); | |
1732 | if (type == "double") | |
1733 | *value = ((wxVariantDataReal*)GetData())->GetValue(); | |
1734 | else if (type == "long") | |
1735 | *value = (double) (((wxVariantDataLong*)GetData())->GetValue()); | |
1736 | #ifdef HAVE_BOOL | |
1737 | else if (type == "bool") | |
1738 | *value = (double) (((wxVariantDataBool*)GetData())->GetValue()); | |
1739 | #endif | |
1740 | else if (type == "string") | |
1741 | *value = (double) atof((const char*) ((wxVariantDataString*)GetData())->GetValue()); | |
1742 | else | |
1743 | return FALSE; | |
1744 | ||
1745 | return TRUE; | |
1746 | } | |
1747 | ||
1748 | bool wxVariant::Convert(char* value) const | |
1749 | { | |
1750 | wxString type(GetType()); | |
1751 | if (type == "char") | |
1752 | *value = ((wxVariantDataChar*)GetData())->GetValue(); | |
1753 | else if (type == "long") | |
1754 | *value = (char) (((wxVariantDataLong*)GetData())->GetValue()); | |
1755 | #ifdef HAVE_BOOL | |
1756 | else if (type == "bool") | |
1757 | *value = (char) (((wxVariantDataBool*)GetData())->GetValue()); | |
1758 | #endif | |
1759 | else | |
1760 | return FALSE; | |
1761 | ||
1762 | return TRUE; | |
1763 | } | |
1764 | ||
1765 | bool wxVariant::Convert(wxString* value) const | |
1766 | { | |
1767 | *value = MakeString(); | |
1768 | return TRUE; | |
1769 | } | |
1770 | ||
1771 | #if wxUSE_TIMEDATE | |
1772 | bool wxVariant::Convert(wxTime* value) const | |
1773 | { | |
1774 | wxString type(GetType()); | |
1775 | if (type == "time") | |
1776 | *value = ((wxVariantDataTime*)GetData())->GetValue(); | |
1777 | else if (type == "date") | |
1778 | *value = wxTime(((wxVariantDataDate*)GetData())->GetValue()); | |
1779 | else | |
1780 | return FALSE; | |
1781 | ||
1782 | return TRUE; | |
1783 | } | |
1784 | ||
1785 | bool wxVariant::Convert(wxDate* value) const | |
1786 | { | |
1787 | wxString type(GetType()); | |
1788 | if (type == "date") | |
1789 | *value = ((wxVariantDataDate*)GetData())->GetValue(); | |
1790 | else | |
1791 | return FALSE; | |
1792 | ||
1793 | return TRUE; | |
1794 | } | |
1795 | #endif | |
1796 | // wxUSE_TIMEDATE |