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