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