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