]>
Commit | Line | Data |
---|---|---|
341287bf | 1 | ///////////////////////////////////////////////////////////////////////////// |
43f06cfd | 2 | // Name: src/common/variant.cpp |
341287bf JS |
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) | |
65571936 | 9 | // Licence: wxWindows licence |
341287bf JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
341287bf JS |
12 | // For compilers that support precompilation, includes "wx/wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
3f4a0c5b | 16 | #pragma hdrstop |
341287bf JS |
17 | #endif |
18 | ||
df91131c WS |
19 | #include "wx/variant.h" |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/string.h" | |
23 | #endif | |
24 | ||
be087207 RR |
25 | #if wxUSE_STD_IOSTREAM |
26 | #if wxUSE_IOSTREAMH | |
27 | #include <fstream.h> | |
28 | #else | |
29 | #include <fstream> | |
30 | #endif | |
fbc535ff | 31 | #endif |
341287bf | 32 | |
31907d03 | 33 | #if defined(__MWERKS__) && __MSL__ >= 0x6000 |
52cbcda3 | 34 | namespace std {} |
31907d03 SC |
35 | using namespace std ; |
36 | #endif | |
37 | ||
fae05df5 | 38 | #if wxUSE_STREAMS |
75ed1d15 | 39 | #include "wx/stream.h" |
fae05df5 GL |
40 | #include "wx/txtstrm.h" |
41 | #endif | |
42 | ||
341287bf | 43 | #include "wx/string.h" |
fb42d7c3 | 44 | #include "wx/tokenzr.h" |
bc14c8b2 | 45 | #include "wx/math.h" |
fb42d7c3 | 46 | |
341287bf JS |
47 | IMPLEMENT_ABSTRACT_CLASS(wxVariantData, wxObject) |
48 | ||
fd242375 | 49 | wxVariant WXDLLIMPEXP_BASE wxNullVariant; |
341287bf JS |
50 | |
51 | /* | |
52 | * wxVariantDataList | |
53 | */ | |
54 | ||
fd242375 | 55 | class WXDLLIMPEXP_BASE wxVariantDataList: public wxVariantData |
341287bf JS |
56 | { |
57 | DECLARE_DYNAMIC_CLASS(wxVariantDataList) | |
58 | public: | |
59 | wxVariantDataList() {} | |
60 | wxVariantDataList(const wxList& list); | |
61 | ~wxVariantDataList(); | |
62 | ||
9000cdfb | 63 | wxList& GetValue() { return m_value; } |
341287bf JS |
64 | void SetValue(const wxList& value) ; |
65 | ||
66 | virtual void Copy(wxVariantData& data); | |
67 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 68 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 69 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 70 | #endif |
341287bf | 71 | virtual bool Write(wxString& str) const; |
38830220 | 72 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 73 | virtual bool Read(wxSTD istream& str); |
38830220 | 74 | #endif |
341287bf | 75 | virtual bool Read(wxString& str); |
223d09f6 | 76 | virtual wxString GetType() const { return wxT("list"); }; |
341287bf JS |
77 | |
78 | void Clear(); | |
79 | ||
80 | protected: | |
81 | wxList m_value; | |
82 | }; | |
83 | ||
84 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataList, wxVariantData) | |
85 | ||
86 | wxVariantDataList::wxVariantDataList(const wxList& list) | |
87 | { | |
88 | SetValue(list); | |
89 | } | |
90 | ||
91 | wxVariantDataList::~wxVariantDataList() | |
92 | { | |
93 | Clear(); | |
94 | } | |
95 | ||
96 | void wxVariantDataList::SetValue(const wxList& value) | |
97 | { | |
98 | Clear(); | |
df5168c4 | 99 | wxList::compatibility_iterator node = value.GetFirst(); |
341287bf JS |
100 | while (node) |
101 | { | |
b1d4dd7a | 102 | wxVariant* var = (wxVariant*) node->GetData(); |
341287bf | 103 | m_value.Append(new wxVariant(*var)); |
b1d4dd7a | 104 | node = node->GetNext(); |
341287bf JS |
105 | } |
106 | } | |
107 | ||
108 | void wxVariantDataList::Clear() | |
109 | { | |
df5168c4 | 110 | wxList::compatibility_iterator node = m_value.GetFirst(); |
341287bf JS |
111 | while (node) |
112 | { | |
b1d4dd7a | 113 | wxVariant* var = (wxVariant*) node->GetData(); |
341287bf | 114 | delete var; |
b1d4dd7a | 115 | node = node->GetNext(); |
341287bf JS |
116 | } |
117 | m_value.Clear(); | |
118 | } | |
119 | ||
120 | void wxVariantDataList::Copy(wxVariantData& data) | |
121 | { | |
223d09f6 | 122 | wxASSERT_MSG( (data.GetType() == wxT("list")), wxT("wxVariantDataList::Copy: Can't copy to this type of data") ); |
341287bf JS |
123 | |
124 | wxVariantDataList& listData = (wxVariantDataList&) data; | |
125 | ||
126 | listData.Clear(); | |
df5168c4 | 127 | wxList::compatibility_iterator node = m_value.GetFirst(); |
341287bf JS |
128 | while (node) |
129 | { | |
b1d4dd7a | 130 | wxVariant* var = (wxVariant*) node->GetData(); |
341287bf | 131 | listData.m_value.Append(new wxVariant(*var)); |
b1d4dd7a | 132 | node = node->GetNext(); |
341287bf JS |
133 | } |
134 | } | |
135 | ||
136 | bool wxVariantDataList::Eq(wxVariantData& data) const | |
137 | { | |
223d09f6 | 138 | wxASSERT_MSG( (data.GetType() == wxT("list")), wxT("wxVariantDataList::Eq: argument mismatch") ); |
341287bf JS |
139 | |
140 | wxVariantDataList& listData = (wxVariantDataList&) data; | |
df5168c4 MB |
141 | wxList::compatibility_iterator node1 = m_value.GetFirst(); |
142 | wxList::compatibility_iterator node2 = listData.GetValue().GetFirst(); | |
341287bf JS |
143 | while (node1 && node2) |
144 | { | |
b1d4dd7a RL |
145 | wxVariant* var1 = (wxVariant*) node1->GetData(); |
146 | wxVariant* var2 = (wxVariant*) node2->GetData(); | |
341287bf | 147 | if ((*var1) != (*var2)) |
cab1a605 | 148 | return false; |
b1d4dd7a RL |
149 | node1 = node1->GetNext(); |
150 | node2 = node2->GetNext(); | |
341287bf | 151 | } |
cab1a605 WS |
152 | if (node1 || node2) return false; |
153 | return true; | |
341287bf JS |
154 | } |
155 | ||
38830220 | 156 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 157 | bool wxVariantDataList::Write(wxSTD ostream& str) const |
341287bf JS |
158 | { |
159 | wxString s; | |
160 | Write(s); | |
783b6cfd | 161 | str << (const char*) s.mb_str(); |
cab1a605 | 162 | return true; |
341287bf | 163 | } |
38830220 | 164 | #endif |
341287bf JS |
165 | |
166 | bool wxVariantDataList::Write(wxString& str) const | |
167 | { | |
525d8583 | 168 | str = wxEmptyString; |
df5168c4 | 169 | wxList::compatibility_iterator node = m_value.GetFirst(); |
341287bf JS |
170 | while (node) |
171 | { | |
b1d4dd7a RL |
172 | wxVariant* var = (wxVariant*) node->GetData(); |
173 | if (node != m_value.GetFirst()) | |
223d09f6 | 174 | str += wxT(" "); |
341287bf JS |
175 | wxString str1; |
176 | str += var->MakeString(); | |
b1d4dd7a | 177 | node = node->GetNext(); |
341287bf JS |
178 | } |
179 | ||
cab1a605 | 180 | return true; |
341287bf JS |
181 | } |
182 | ||
38830220 | 183 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 184 | bool wxVariantDataList::Read(wxSTD istream& WXUNUSED(str)) |
341287bf | 185 | { |
223d09f6 | 186 | wxFAIL_MSG(wxT("Unimplemented")); |
341287bf | 187 | // TODO |
cab1a605 | 188 | return false; |
341287bf | 189 | } |
38830220 | 190 | #endif |
341287bf JS |
191 | |
192 | bool wxVariantDataList::Read(wxString& WXUNUSED(str)) | |
193 | { | |
223d09f6 | 194 | wxFAIL_MSG(wxT("Unimplemented")); |
341287bf | 195 | // TODO |
cab1a605 | 196 | return false; |
341287bf | 197 | } |
2c3a1064 | 198 | #if WXWIN_COMPATIBILITY_2_4 |
341287bf JS |
199 | |
200 | /* | |
201 | * wxVariantDataStringList | |
202 | */ | |
203 | ||
fd242375 | 204 | class WXDLLIMPEXP_BASE wxVariantDataStringList: public wxVariantData |
341287bf JS |
205 | { |
206 | DECLARE_DYNAMIC_CLASS(wxVariantDataStringList) | |
207 | public: | |
208 | wxVariantDataStringList() {} | |
209 | wxVariantDataStringList(const wxStringList& list) { m_value = list; } | |
210 | ||
211 | wxStringList& GetValue() const { return (wxStringList&) m_value; } | |
212 | void SetValue(const wxStringList& value); | |
213 | ||
214 | virtual void Copy(wxVariantData& data); | |
215 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 216 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 217 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 218 | #endif |
341287bf | 219 | virtual bool Write(wxString& str) const; |
38830220 | 220 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 221 | virtual bool Read(wxSTD istream& str); |
38830220 | 222 | #endif |
341287bf | 223 | virtual bool Read(wxString& str); |
223d09f6 | 224 | virtual wxString GetType() const { return wxT("stringlist"); }; |
341287bf JS |
225 | |
226 | protected: | |
227 | wxStringList m_value; | |
228 | }; | |
229 | ||
230 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataStringList, wxVariantData) | |
231 | ||
232 | void wxVariantDataStringList::SetValue(const wxStringList& value) | |
233 | { | |
234 | m_value = value; | |
235 | } | |
236 | ||
237 | void wxVariantDataStringList::Copy(wxVariantData& data) | |
238 | { | |
223d09f6 | 239 | wxASSERT_MSG( (data.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Copy: Can't copy to this type of data") ); |
341287bf JS |
240 | |
241 | wxVariantDataStringList& listData = (wxVariantDataStringList&) data; | |
242 | ||
243 | listData.m_value = m_value ; | |
244 | } | |
245 | ||
246 | bool wxVariantDataStringList::Eq(wxVariantData& data) const | |
247 | { | |
223d09f6 | 248 | wxASSERT_MSG( (data.GetType() == wxT("stringlist")), wxT("wxVariantDataStringList::Eq: argument mismatch") ); |
341287bf JS |
249 | |
250 | wxVariantDataStringList& listData = (wxVariantDataStringList&) data; | |
df5168c4 MB |
251 | wxStringList::compatibility_iterator node1 = m_value.GetFirst(); |
252 | wxStringList::compatibility_iterator node2 = listData.GetValue().GetFirst(); | |
341287bf JS |
253 | while (node1 && node2) |
254 | { | |
b1d4dd7a RL |
255 | wxString str1 ( node1->GetData() ); |
256 | wxString str2 ( node2->GetData() ); | |
341287bf | 257 | if (str1 != str2) |
cab1a605 | 258 | return false; |
b1d4dd7a RL |
259 | node1 = node1->GetNext(); |
260 | node2 = node2->GetNext(); | |
341287bf | 261 | } |
cab1a605 WS |
262 | if (node1 || node2) return false; |
263 | return true; | |
341287bf JS |
264 | } |
265 | ||
38830220 | 266 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 267 | bool wxVariantDataStringList::Write(wxSTD ostream& str) const |
341287bf JS |
268 | { |
269 | wxString s; | |
270 | Write(s); | |
783b6cfd | 271 | str << (const char*) s.mb_str(); |
cab1a605 | 272 | return true; |
341287bf | 273 | } |
38830220 | 274 | #endif |
341287bf JS |
275 | |
276 | bool wxVariantDataStringList::Write(wxString& str) const | |
277 | { | |
b1d4dd7a | 278 | str.Empty(); |
df5168c4 | 279 | wxStringList::compatibility_iterator node = m_value.GetFirst(); |
341287bf JS |
280 | while (node) |
281 | { | |
df5168c4 | 282 | const wxChar* s = node->GetData(); |
b1d4dd7a | 283 | if (node != m_value.GetFirst()) |
223d09f6 | 284 | str += wxT(" "); |
341287bf | 285 | str += s; |
b1d4dd7a | 286 | node = node->GetNext(); |
341287bf JS |
287 | } |
288 | ||
cab1a605 | 289 | return true; |
341287bf JS |
290 | } |
291 | ||
38830220 | 292 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 293 | bool wxVariantDataStringList::Read(wxSTD istream& WXUNUSED(str)) |
341287bf | 294 | { |
223d09f6 | 295 | wxFAIL_MSG(wxT("Unimplemented")); |
341287bf | 296 | // TODO |
cab1a605 | 297 | return false; |
341287bf | 298 | } |
38830220 | 299 | #endif |
341287bf JS |
300 | |
301 | bool wxVariantDataStringList::Read(wxString& WXUNUSED(str)) | |
302 | { | |
223d09f6 | 303 | wxFAIL_MSG(wxT("Unimplemented")); |
341287bf | 304 | // TODO |
cab1a605 | 305 | return false; |
341287bf JS |
306 | } |
307 | ||
489f6cf7 | 308 | #endif //2.4 compat |
2c3a1064 | 309 | |
341287bf JS |
310 | /* |
311 | * wxVariantDataLong | |
312 | */ | |
313 | ||
fd242375 | 314 | class WXDLLIMPEXP_BASE wxVariantDataLong: public wxVariantData |
341287bf JS |
315 | { |
316 | DECLARE_DYNAMIC_CLASS(wxVariantDataLong) | |
317 | public: | |
318 | wxVariantDataLong() { m_value = 0; } | |
319 | wxVariantDataLong(long value) { m_value = value; } | |
320 | ||
321 | inline long GetValue() const { return m_value; } | |
322 | inline void SetValue(long value) { m_value = value; } | |
323 | ||
324 | virtual void Copy(wxVariantData& data); | |
325 | virtual bool Eq(wxVariantData& data) const; | |
1ccbb61a VZ |
326 | |
327 | virtual bool Read(wxString& str); | |
341287bf | 328 | virtual bool Write(wxString& str) const; |
38830220 | 329 | #if wxUSE_STD_IOSTREAM |
dd107c50 VZ |
330 | virtual bool Read(wxSTD istream& str); |
331 | virtual bool Write(wxSTD ostream& str) const; | |
38830220 | 332 | #endif |
e02afc7a | 333 | #if wxUSE_STREAMS |
75ed1d15 | 334 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 335 | virtual bool Write(wxOutputStream &str) const; |
e02afc7a | 336 | #endif // wxUSE_STREAMS |
1ccbb61a | 337 | |
223d09f6 | 338 | virtual wxString GetType() const { return wxT("long"); }; |
341287bf JS |
339 | |
340 | protected: | |
341 | long m_value; | |
342 | }; | |
343 | ||
344 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataLong, wxVariantData) | |
345 | ||
346 | void wxVariantDataLong::Copy(wxVariantData& data) | |
347 | { | |
223d09f6 | 348 | wxASSERT_MSG( (data.GetType() == wxT("long")), wxT("wxVariantDataLong::Copy: Can't copy to this type of data") ); |
341287bf JS |
349 | |
350 | wxVariantDataLong& otherData = (wxVariantDataLong&) data; | |
351 | ||
352 | otherData.m_value = m_value; | |
353 | } | |
354 | ||
355 | bool wxVariantDataLong::Eq(wxVariantData& data) const | |
356 | { | |
223d09f6 | 357 | wxASSERT_MSG( (data.GetType() == wxT("long")), wxT("wxVariantDataLong::Eq: argument mismatch") ); |
341287bf JS |
358 | |
359 | wxVariantDataLong& otherData = (wxVariantDataLong&) data; | |
360 | ||
361 | return (otherData.m_value == m_value); | |
362 | } | |
363 | ||
38830220 | 364 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 365 | bool wxVariantDataLong::Write(wxSTD ostream& str) const |
341287bf JS |
366 | { |
367 | wxString s; | |
368 | Write(s); | |
783b6cfd | 369 | str << (const char*) s.mb_str(); |
cab1a605 | 370 | return true; |
341287bf | 371 | } |
38830220 | 372 | #endif |
341287bf JS |
373 | |
374 | bool wxVariantDataLong::Write(wxString& str) const | |
375 | { | |
223d09f6 | 376 | str.Printf(wxT("%ld"), m_value); |
cab1a605 | 377 | return true; |
341287bf JS |
378 | } |
379 | ||
38830220 | 380 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 381 | bool wxVariantDataLong::Read(wxSTD istream& str) |
341287bf JS |
382 | { |
383 | str >> m_value; | |
cab1a605 | 384 | return true; |
341287bf | 385 | } |
38830220 | 386 | #endif |
341287bf | 387 | |
e02afc7a | 388 | #if wxUSE_STREAMS |
1ccbb61a VZ |
389 | bool wxVariantDataLong::Write(wxOutputStream& str) const |
390 | { | |
fae05df5 GL |
391 | wxTextOutputStream s(str); |
392 | ||
479cd5de | 393 | s.Write32((size_t)m_value); |
cab1a605 | 394 | return true; |
1ccbb61a VZ |
395 | } |
396 | ||
75ed1d15 GL |
397 | bool wxVariantDataLong::Read(wxInputStream& str) |
398 | { | |
fae05df5 GL |
399 | wxTextInputStream s(str); |
400 | m_value = s.Read32(); | |
cab1a605 | 401 | return true; |
75ed1d15 | 402 | } |
e02afc7a | 403 | #endif // wxUSE_STREAMS |
75ed1d15 | 404 | |
341287bf JS |
405 | bool wxVariantDataLong::Read(wxString& str) |
406 | { | |
783b6cfd | 407 | m_value = wxAtol((const wxChar*) str); |
cab1a605 | 408 | return true; |
341287bf JS |
409 | } |
410 | ||
411 | /* | |
412 | * wxVariantDataReal | |
413 | */ | |
414 | ||
fd242375 | 415 | class WXDLLIMPEXP_BASE wxVariantDataReal: public wxVariantData |
341287bf JS |
416 | { |
417 | DECLARE_DYNAMIC_CLASS(wxVariantDataReal) | |
418 | public: | |
419 | wxVariantDataReal() { m_value = 0.0; } | |
420 | wxVariantDataReal(double value) { m_value = value; } | |
421 | ||
422 | inline double GetValue() const { return m_value; } | |
423 | inline void SetValue(double value) { m_value = value; } | |
424 | ||
425 | virtual void Copy(wxVariantData& data); | |
426 | virtual bool Eq(wxVariantData& data) const; | |
1ccbb61a | 427 | virtual bool Read(wxString& str); |
38830220 | 428 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 429 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 430 | #endif |
341287bf | 431 | virtual bool Write(wxString& str) const; |
38830220 | 432 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 433 | virtual bool Read(wxSTD istream& str); |
38830220 | 434 | #endif |
e02afc7a | 435 | #if wxUSE_STREAMS |
75ed1d15 | 436 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 437 | virtual bool Write(wxOutputStream &str) const; |
e02afc7a | 438 | #endif // wxUSE_STREAMS |
223d09f6 | 439 | virtual wxString GetType() const { return wxT("double"); }; |
341287bf JS |
440 | |
441 | protected: | |
442 | double m_value; | |
443 | }; | |
444 | ||
445 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataReal, wxVariantData) | |
446 | ||
447 | void wxVariantDataReal::Copy(wxVariantData& data) | |
448 | { | |
223d09f6 | 449 | wxASSERT_MSG( (data.GetType() == wxT("double")), wxT("wxVariantDataReal::Copy: Can't copy to this type of data") ); |
341287bf JS |
450 | |
451 | wxVariantDataReal& otherData = (wxVariantDataReal&) data; | |
452 | ||
453 | otherData.m_value = m_value; | |
454 | } | |
455 | ||
456 | bool wxVariantDataReal::Eq(wxVariantData& data) const | |
457 | { | |
223d09f6 | 458 | wxASSERT_MSG( (data.GetType() == wxT("double")), wxT("wxVariantDataReal::Eq: argument mismatch") ); |
341287bf JS |
459 | |
460 | wxVariantDataReal& otherData = (wxVariantDataReal&) data; | |
461 | ||
bc14c8b2 | 462 | return wxIsSameDouble(otherData.m_value, m_value); |
341287bf JS |
463 | } |
464 | ||
38830220 | 465 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 466 | bool wxVariantDataReal::Write(wxSTD ostream& str) const |
341287bf JS |
467 | { |
468 | wxString s; | |
469 | Write(s); | |
783b6cfd | 470 | str << (const char*) s.mb_str(); |
cab1a605 | 471 | return true; |
341287bf | 472 | } |
38830220 | 473 | #endif |
341287bf JS |
474 | |
475 | bool wxVariantDataReal::Write(wxString& str) const | |
476 | { | |
654056ee | 477 | str.Printf(wxT("%.14g"), m_value); |
cab1a605 | 478 | return true; |
341287bf JS |
479 | } |
480 | ||
38830220 | 481 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 482 | bool wxVariantDataReal::Read(wxSTD istream& str) |
341287bf JS |
483 | { |
484 | str >> m_value; | |
cab1a605 | 485 | return true; |
341287bf | 486 | } |
38830220 | 487 | #endif |
341287bf | 488 | |
e02afc7a | 489 | #if wxUSE_STREAMS |
1ccbb61a VZ |
490 | bool wxVariantDataReal::Write(wxOutputStream& str) const |
491 | { | |
fae05df5 GL |
492 | wxTextOutputStream s(str); |
493 | s.WriteDouble((double)m_value); | |
cab1a605 | 494 | return true; |
1ccbb61a VZ |
495 | } |
496 | ||
75ed1d15 GL |
497 | bool wxVariantDataReal::Read(wxInputStream& str) |
498 | { | |
fae05df5 GL |
499 | wxTextInputStream s(str); |
500 | m_value = (float)s.ReadDouble(); | |
cab1a605 | 501 | return true; |
75ed1d15 | 502 | } |
e02afc7a | 503 | #endif // wxUSE_STREAMS |
75ed1d15 | 504 | |
341287bf JS |
505 | bool wxVariantDataReal::Read(wxString& str) |
506 | { | |
783b6cfd | 507 | m_value = wxAtof((const wxChar*) str); |
cab1a605 | 508 | return true; |
341287bf JS |
509 | } |
510 | ||
57493f9f | 511 | #ifdef HAVE_BOOL |
341287bf JS |
512 | /* |
513 | * wxVariantDataBool | |
514 | */ | |
515 | ||
fd242375 | 516 | class WXDLLIMPEXP_BASE wxVariantDataBool: public wxVariantData |
341287bf JS |
517 | { |
518 | DECLARE_DYNAMIC_CLASS(wxVariantDataBool) | |
519 | public: | |
520 | wxVariantDataBool() { m_value = 0; } | |
521 | wxVariantDataBool(bool value) { m_value = value; } | |
522 | ||
523 | inline bool GetValue() const { return m_value; } | |
524 | inline void SetValue(bool value) { m_value = value; } | |
525 | ||
526 | virtual void Copy(wxVariantData& data); | |
527 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 528 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 529 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 530 | #endif |
341287bf | 531 | virtual bool Write(wxString& str) const; |
1ccbb61a | 532 | virtual bool Read(wxString& str); |
38830220 | 533 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 534 | virtual bool Read(wxSTD istream& str); |
38830220 | 535 | #endif |
e02afc7a | 536 | #if wxUSE_STREAMS |
75ed1d15 | 537 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 538 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 539 | #endif // wxUSE_STREAMS |
223d09f6 | 540 | virtual wxString GetType() const { return wxT("bool"); }; |
341287bf JS |
541 | |
542 | protected: | |
543 | bool m_value; | |
544 | }; | |
545 | ||
546 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataBool, wxVariantData) | |
547 | ||
548 | void wxVariantDataBool::Copy(wxVariantData& data) | |
549 | { | |
223d09f6 | 550 | wxASSERT_MSG( (data.GetType() == wxT("bool")), wxT("wxVariantDataBool::Copy: Can't copy to this type of data") ); |
341287bf JS |
551 | |
552 | wxVariantDataBool& otherData = (wxVariantDataBool&) data; | |
553 | ||
554 | otherData.m_value = m_value; | |
555 | } | |
556 | ||
557 | bool wxVariantDataBool::Eq(wxVariantData& data) const | |
558 | { | |
223d09f6 | 559 | wxASSERT_MSG( (data.GetType() == wxT("bool")), wxT("wxVariantDataBool::Eq: argument mismatch") ); |
341287bf JS |
560 | |
561 | wxVariantDataBool& otherData = (wxVariantDataBool&) data; | |
562 | ||
563 | return (otherData.m_value == m_value); | |
564 | } | |
565 | ||
38830220 | 566 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 567 | bool wxVariantDataBool::Write(wxSTD ostream& str) const |
341287bf JS |
568 | { |
569 | wxString s; | |
570 | Write(s); | |
783b6cfd | 571 | str << (const char*) s.mb_str(); |
cab1a605 | 572 | return true; |
341287bf | 573 | } |
38830220 | 574 | #endif |
341287bf JS |
575 | |
576 | bool wxVariantDataBool::Write(wxString& str) const | |
577 | { | |
223d09f6 | 578 | str.Printf(wxT("%d"), (int) m_value); |
cab1a605 | 579 | return true; |
341287bf JS |
580 | } |
581 | ||
38830220 | 582 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 583 | bool wxVariantDataBool::Read(wxSTD istream& WXUNUSED(str)) |
341287bf | 584 | { |
223d09f6 | 585 | wxFAIL_MSG(wxT("Unimplemented")); |
341287bf | 586 | // str >> (long) m_value; |
cab1a605 | 587 | return false; |
341287bf | 588 | } |
38830220 | 589 | #endif |
341287bf | 590 | |
e02afc7a | 591 | #if wxUSE_STREAMS |
1ccbb61a VZ |
592 | bool wxVariantDataBool::Write(wxOutputStream& str) const |
593 | { | |
fae05df5 GL |
594 | wxTextOutputStream s(str); |
595 | ||
2b004197 | 596 | s.Write8(m_value); |
cab1a605 | 597 | return true; |
1ccbb61a VZ |
598 | } |
599 | ||
75ed1d15 GL |
600 | bool wxVariantDataBool::Read(wxInputStream& str) |
601 | { | |
fae05df5 GL |
602 | wxTextInputStream s(str); |
603 | ||
a1b82138 | 604 | m_value = s.Read8() != 0; |
cab1a605 | 605 | return true; |
75ed1d15 | 606 | } |
e02afc7a | 607 | #endif // wxUSE_STREAMS |
75ed1d15 | 608 | |
341287bf JS |
609 | bool wxVariantDataBool::Read(wxString& str) |
610 | { | |
783b6cfd | 611 | m_value = (wxAtol((const wxChar*) str) != 0); |
cab1a605 | 612 | return true; |
341287bf | 613 | } |
57493f9f | 614 | #endif // HAVE_BOOL |
341287bf JS |
615 | |
616 | /* | |
617 | * wxVariantDataChar | |
618 | */ | |
619 | ||
fd242375 | 620 | class WXDLLIMPEXP_BASE wxVariantDataChar: public wxVariantData |
341287bf JS |
621 | { |
622 | DECLARE_DYNAMIC_CLASS(wxVariantDataChar) | |
623 | public: | |
624 | wxVariantDataChar() { m_value = 0; } | |
625 | wxVariantDataChar(char value) { m_value = value; } | |
626 | ||
627 | inline char GetValue() const { return m_value; } | |
628 | inline void SetValue(char value) { m_value = value; } | |
629 | ||
630 | virtual void Copy(wxVariantData& data); | |
631 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 632 | #if wxUSE_STD_IOSTREAM |
dd107c50 VZ |
633 | virtual bool Read(wxSTD istream& str); |
634 | virtual bool Write(wxSTD ostream& str) const; | |
38830220 | 635 | #endif |
1ccbb61a | 636 | virtual bool Read(wxString& str); |
341287bf | 637 | virtual bool Write(wxString& str) const; |
e02afc7a | 638 | #if wxUSE_STREAMS |
75ed1d15 | 639 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 640 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 641 | #endif // wxUSE_STREAMS |
223d09f6 | 642 | virtual wxString GetType() const { return wxT("char"); }; |
341287bf JS |
643 | |
644 | protected: | |
645 | char m_value; | |
646 | }; | |
647 | ||
648 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataChar, wxVariantData) | |
649 | ||
650 | void wxVariantDataChar::Copy(wxVariantData& data) | |
651 | { | |
223d09f6 | 652 | wxASSERT_MSG( (data.GetType() == wxT("char")), wxT("wxVariantDataChar::Copy: Can't copy to this type of data") ); |
341287bf JS |
653 | |
654 | wxVariantDataChar& otherData = (wxVariantDataChar&) data; | |
655 | ||
656 | otherData.m_value = m_value; | |
657 | } | |
658 | ||
659 | bool wxVariantDataChar::Eq(wxVariantData& data) const | |
660 | { | |
223d09f6 | 661 | wxASSERT_MSG( (data.GetType() == wxT("char")), wxT("wxVariantDataChar::Eq: argument mismatch") ); |
341287bf JS |
662 | |
663 | wxVariantDataChar& otherData = (wxVariantDataChar&) data; | |
664 | ||
665 | return (otherData.m_value == m_value); | |
666 | } | |
667 | ||
38830220 | 668 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 669 | bool wxVariantDataChar::Write(wxSTD ostream& str) const |
341287bf JS |
670 | { |
671 | wxString s; | |
672 | Write(s); | |
783b6cfd | 673 | str << (const char*) s.mb_str(); |
cab1a605 | 674 | return true; |
341287bf | 675 | } |
38830220 | 676 | #endif |
341287bf JS |
677 | |
678 | bool wxVariantDataChar::Write(wxString& str) const | |
679 | { | |
223d09f6 | 680 | str.Printf(wxT("%c"), m_value); |
cab1a605 | 681 | return true; |
341287bf JS |
682 | } |
683 | ||
38830220 | 684 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 685 | bool wxVariantDataChar::Read(wxSTD istream& WXUNUSED(str)) |
341287bf | 686 | { |
223d09f6 | 687 | wxFAIL_MSG(wxT("Unimplemented")); |
341287bf | 688 | // str >> m_value; |
cab1a605 | 689 | return false; |
341287bf | 690 | } |
38830220 | 691 | #endif |
341287bf | 692 | |
e02afc7a | 693 | #if wxUSE_STREAMS |
1ccbb61a VZ |
694 | bool wxVariantDataChar::Write(wxOutputStream& str) const |
695 | { | |
fae05df5 GL |
696 | wxTextOutputStream s(str); |
697 | ||
698 | s.Write8(m_value); | |
cab1a605 | 699 | return true; |
1ccbb61a VZ |
700 | } |
701 | ||
75ed1d15 GL |
702 | bool wxVariantDataChar::Read(wxInputStream& str) |
703 | { | |
fae05df5 GL |
704 | wxTextInputStream s(str); |
705 | ||
706 | m_value = s.Read8(); | |
cab1a605 | 707 | return true; |
75ed1d15 | 708 | } |
e02afc7a | 709 | #endif // wxUSE_STREAMS |
75ed1d15 | 710 | |
341287bf JS |
711 | bool wxVariantDataChar::Read(wxString& str) |
712 | { | |
fcd8dd3f | 713 | m_value = str.ToAscii()[size_t(0)]; |
cab1a605 | 714 | return true; |
341287bf JS |
715 | } |
716 | ||
717 | /* | |
718 | * wxVariantDataString | |
719 | */ | |
720 | ||
fd242375 | 721 | class WXDLLIMPEXP_BASE wxVariantDataString: public wxVariantData |
341287bf JS |
722 | { |
723 | DECLARE_DYNAMIC_CLASS(wxVariantDataString) | |
724 | public: | |
725 | wxVariantDataString() { } | |
726 | wxVariantDataString(const wxString& value) { m_value = value; } | |
727 | ||
728 | inline wxString GetValue() const { return m_value; } | |
729 | inline void SetValue(const wxString& value) { m_value = value; } | |
730 | ||
731 | virtual void Copy(wxVariantData& data); | |
732 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 733 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 734 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 735 | #endif |
1ccbb61a | 736 | virtual bool Read(wxString& str); |
341287bf | 737 | virtual bool Write(wxString& str) const; |
38830220 | 738 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 739 | virtual bool Read(wxSTD istream& str); |
38830220 | 740 | #endif |
e02afc7a | 741 | #if wxUSE_STREAMS |
75ed1d15 | 742 | virtual bool Read(wxInputStream& str); |
1ccbb61a | 743 | virtual bool Write(wxOutputStream& str) const; |
e02afc7a | 744 | #endif // wxUSE_STREAMS |
223d09f6 | 745 | virtual wxString GetType() const { return wxT("string"); }; |
341287bf JS |
746 | |
747 | protected: | |
748 | wxString m_value; | |
749 | }; | |
750 | ||
751 | void wxVariantDataString::Copy(wxVariantData& data) | |
752 | { | |
223d09f6 | 753 | wxASSERT_MSG( (data.GetType() == wxT("string")), wxT("wxVariantDataString::Copy: Can't copy to this type of data") ); |
341287bf JS |
754 | |
755 | wxVariantDataString& otherData = (wxVariantDataString&) data; | |
756 | ||
757 | otherData.m_value = m_value; | |
758 | } | |
759 | ||
760 | bool wxVariantDataString::Eq(wxVariantData& data) const | |
761 | { | |
223d09f6 | 762 | wxASSERT_MSG( (data.GetType() == wxT("string")), wxT("wxVariantDataString::Eq: argument mismatch") ); |
341287bf JS |
763 | |
764 | wxVariantDataString& otherData = (wxVariantDataString&) data; | |
765 | ||
766 | return (otherData.m_value == m_value); | |
767 | } | |
768 | ||
38830220 | 769 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 770 | bool wxVariantDataString::Write(wxSTD ostream& str) const |
341287bf | 771 | { |
783b6cfd | 772 | str << (const char*) m_value.mb_str(); |
cab1a605 | 773 | return true; |
341287bf | 774 | } |
38830220 | 775 | #endif |
341287bf JS |
776 | |
777 | bool wxVariantDataString::Write(wxString& str) const | |
778 | { | |
779 | str = m_value; | |
cab1a605 | 780 | return true; |
341287bf JS |
781 | } |
782 | ||
38830220 | 783 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 784 | bool wxVariantDataString::Read(wxSTD istream& str) |
341287bf JS |
785 | { |
786 | str >> m_value; | |
cab1a605 | 787 | return true; |
341287bf | 788 | } |
38830220 | 789 | #endif |
341287bf | 790 | |
e02afc7a | 791 | #if wxUSE_STREAMS |
1ccbb61a VZ |
792 | bool wxVariantDataString::Write(wxOutputStream& str) const |
793 | { | |
783b6cfd | 794 | // why doesn't wxOutputStream::operator<< take "const wxString&" |
fae05df5 GL |
795 | wxTextOutputStream s(str); |
796 | s.WriteString(m_value); | |
cab1a605 | 797 | return true; |
1ccbb61a VZ |
798 | } |
799 | ||
75ed1d15 GL |
800 | bool wxVariantDataString::Read(wxInputStream& str) |
801 | { | |
fae05df5 GL |
802 | wxTextInputStream s(str); |
803 | ||
40ff126a | 804 | m_value = s.ReadLine(); |
cab1a605 | 805 | return true; |
75ed1d15 | 806 | } |
e02afc7a | 807 | #endif // wxUSE_STREAMS |
75ed1d15 | 808 | |
341287bf JS |
809 | bool wxVariantDataString::Read(wxString& str) |
810 | { | |
811 | m_value = str; | |
cab1a605 | 812 | return true; |
341287bf JS |
813 | } |
814 | ||
815 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataString, wxVariantData) | |
816 | ||
a0a302dc JS |
817 | /* |
818 | * wxVariantDataVoidPtr | |
819 | */ | |
820 | ||
821 | class wxVariantDataVoidPtr: public wxVariantData | |
822 | { | |
823 | DECLARE_DYNAMIC_CLASS(wxVariantDataVoidPtr) | |
824 | public: | |
825 | wxVariantDataVoidPtr() { } | |
826 | wxVariantDataVoidPtr(void* value) { m_value = value; } | |
827 | ||
828 | inline void* GetValue() const { return m_value; } | |
829 | inline void SetValue(void* value) { m_value = value; } | |
830 | ||
831 | virtual void Copy(wxVariantData& data); | |
832 | virtual bool Eq(wxVariantData& data) const; | |
38830220 | 833 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 834 | virtual bool Write(wxSTD ostream& str) const; |
38830220 | 835 | #endif |
a0a302dc | 836 | virtual bool Write(wxString& str) const; |
38830220 | 837 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 838 | virtual bool Read(wxSTD istream& str); |
38830220 | 839 | #endif |
a0a302dc | 840 | virtual bool Read(wxString& str); |
223d09f6 | 841 | virtual wxString GetType() const { return wxT("void*"); }; |
b1d4dd7a | 842 | virtual wxVariantData* Clone() { return new wxVariantDataVoidPtr; } |
a0a302dc JS |
843 | |
844 | protected: | |
845 | void* m_value; | |
22f3361e VZ |
846 | |
847 | DECLARE_NO_COPY_CLASS(wxVariantDataVoidPtr) | |
a0a302dc JS |
848 | }; |
849 | ||
850 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataVoidPtr, wxVariantData) | |
851 | ||
852 | void wxVariantDataVoidPtr::Copy(wxVariantData& data) | |
853 | { | |
223d09f6 | 854 | wxASSERT_MSG( (data.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Copy: Can't copy to this type of data") ); |
a0a302dc JS |
855 | |
856 | wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data; | |
857 | ||
858 | otherData.m_value = m_value; | |
859 | } | |
860 | ||
861 | bool wxVariantDataVoidPtr::Eq(wxVariantData& data) const | |
862 | { | |
223d09f6 | 863 | wxASSERT_MSG( (data.GetType() == wxT("void*")), wxT("wxVariantDataVoidPtr::Eq: argument mismatch") ); |
a0a302dc JS |
864 | |
865 | wxVariantDataVoidPtr& otherData = (wxVariantDataVoidPtr&) data; | |
866 | ||
867 | return (otherData.m_value == m_value); | |
868 | } | |
869 | ||
38830220 | 870 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 871 | bool wxVariantDataVoidPtr::Write(wxSTD ostream& str) const |
a0a302dc JS |
872 | { |
873 | wxString s; | |
874 | Write(s); | |
783b6cfd | 875 | str << (const char*) s.mb_str(); |
cab1a605 | 876 | return true; |
a0a302dc | 877 | } |
38830220 | 878 | #endif |
a0a302dc JS |
879 | |
880 | bool wxVariantDataVoidPtr::Write(wxString& str) const | |
881 | { | |
bc14c8b2 | 882 | str.Printf(wxT("%p"), m_value); |
cab1a605 | 883 | return true; |
a0a302dc JS |
884 | } |
885 | ||
38830220 | 886 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 887 | bool wxVariantDataVoidPtr::Read(wxSTD istream& WXUNUSED(str)) |
a0a302dc JS |
888 | { |
889 | // Not implemented | |
cab1a605 | 890 | return false; |
a0a302dc | 891 | } |
38830220 | 892 | #endif |
a0a302dc | 893 | |
8b53e5a2 | 894 | bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str)) |
a0a302dc JS |
895 | { |
896 | // Not implemented | |
cab1a605 | 897 | return false; |
a0a302dc JS |
898 | } |
899 | ||
cf6ae290 RG |
900 | /* |
901 | * wxVariantDataWxObjectPtr | |
902 | */ | |
903 | ||
904 | class wxVariantDataWxObjectPtr: public wxVariantData | |
905 | { | |
906 | DECLARE_DYNAMIC_CLASS(wxVariantDataWxObjectPtr) | |
907 | public: | |
908 | wxVariantDataWxObjectPtr() { } | |
909 | wxVariantDataWxObjectPtr(wxObject* value) { m_value = value; } | |
910 | ||
911 | inline wxObject* GetValue() const { return m_value; } | |
912 | inline void SetValue(wxObject* value) { m_value = value; } | |
913 | ||
914 | virtual void Copy(wxVariantData& data); | |
915 | virtual bool Eq(wxVariantData& data) const; | |
916 | #if wxUSE_STD_IOSTREAM | |
917 | virtual bool Write(wxSTD ostream& str) const; | |
918 | #endif | |
919 | virtual bool Write(wxString& str) const; | |
920 | #if wxUSE_STD_IOSTREAM | |
921 | virtual bool Read(wxSTD istream& str); | |
922 | #endif | |
923 | virtual bool Read(wxString& str); | |
924 | virtual wxString GetType() const ; | |
925 | virtual wxVariantData* Clone() { return new wxVariantDataWxObjectPtr; } | |
926 | ||
cab1a605 | 927 | virtual wxClassInfo* GetValueClassInfo() ; |
cf6ae290 RG |
928 | protected: |
929 | wxObject* m_value; | |
930 | ||
931 | DECLARE_NO_COPY_CLASS(wxVariantDataWxObjectPtr) | |
932 | }; | |
933 | ||
934 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataWxObjectPtr, wxVariantData) | |
935 | ||
936 | void wxVariantDataWxObjectPtr::Copy(wxVariantData& data) | |
937 | { | |
938 | wxASSERT_MSG( wxIsKindOf((&data), wxVariantDataWxObjectPtr) ,\ | |
939 | wxT("wxVariantDataWxObjectPtr::Copy: Can't copy to this type of data") \ | |
940 | ); | |
941 | ||
942 | wxVariantDataWxObjectPtr& otherData = (wxVariantDataWxObjectPtr&) data; | |
943 | ||
944 | otherData.m_value = m_value; | |
945 | } | |
946 | ||
947 | bool wxVariantDataWxObjectPtr::Eq(wxVariantData& data) const | |
948 | { | |
949 | wxASSERT_MSG( wxIsKindOf((&data), wxVariantDataWxObjectPtr), wxT("wxVariantDataWxObjectPtr::Eq: argument mismatch") ); | |
950 | ||
951 | wxVariantDataWxObjectPtr& otherData = (wxVariantDataWxObjectPtr&) data; | |
952 | ||
953 | return (otherData.m_value == m_value); | |
954 | } | |
955 | ||
956 | wxString wxVariantDataWxObjectPtr::GetType() const | |
957 | { | |
958 | wxString returnVal(wxT("wxObject")); | |
959 | if (m_value) { | |
960 | returnVal = m_value->GetClassInfo()->GetClassName(); | |
961 | } | |
962 | return returnVal; | |
963 | } | |
964 | ||
965 | wxClassInfo* wxVariantDataWxObjectPtr::GetValueClassInfo() | |
966 | { | |
967 | wxClassInfo* returnVal=NULL; | |
cab1a605 WS |
968 | |
969 | if (m_value) returnVal = m_value->GetClassInfo(); | |
cf6ae290 RG |
970 | |
971 | return returnVal; | |
972 | } | |
973 | ||
974 | #if wxUSE_STD_IOSTREAM | |
975 | bool wxVariantDataWxObjectPtr::Write(wxSTD ostream& str) const | |
976 | { | |
977 | wxString s; | |
978 | Write(s); | |
979 | str << (const char*) s.mb_str(); | |
cab1a605 | 980 | return true; |
cf6ae290 RG |
981 | } |
982 | #endif | |
983 | ||
984 | bool wxVariantDataWxObjectPtr::Write(wxString& str) const | |
985 | { | |
bc14c8b2 | 986 | str.Printf(wxT("%s(%p)"), GetType().c_str(), m_value); |
cab1a605 | 987 | return true; |
cf6ae290 RG |
988 | } |
989 | ||
990 | #if wxUSE_STD_IOSTREAM | |
991 | bool wxVariantDataWxObjectPtr::Read(wxSTD istream& WXUNUSED(str)) | |
992 | { | |
993 | // Not implemented | |
cab1a605 | 994 | return false; |
cf6ae290 RG |
995 | } |
996 | #endif | |
997 | ||
998 | bool wxVariantDataWxObjectPtr::Read(wxString& WXUNUSED(str)) | |
999 | { | |
1000 | // Not implemented | |
cab1a605 | 1001 | return false; |
cf6ae290 RG |
1002 | } |
1003 | ||
1004 | ||
edca7a82 GT |
1005 | /* |
1006 | * wxVariantDataDateTime | |
1007 | */ | |
1008 | ||
e2b87f38 VZ |
1009 | #if wxUSE_DATETIME |
1010 | ||
edca7a82 GT |
1011 | class wxVariantDataDateTime: public wxVariantData |
1012 | { | |
1013 | DECLARE_DYNAMIC_CLASS(wxVariantDataDateTime) | |
1014 | ||
1015 | public: | |
1016 | wxVariantDataDateTime() { } | |
1017 | wxVariantDataDateTime(const wxDateTime& value) { m_value = value; } | |
1018 | #if wxUSE_ODBC | |
2b004197 | 1019 | wxVariantDataDateTime(const TIME_STRUCT* valptr) |
edca7a82 | 1020 | { m_value = wxDateTime(valptr->hour, valptr->minute, valptr->second); } |
2b004197 | 1021 | wxVariantDataDateTime(const DATE_STRUCT* valptr) |
edca7a82 | 1022 | { m_value = wxDateTime(valptr->day, (wxDateTime::Month) (valptr->month - 1),valptr->year); } |
2b004197 | 1023 | wxVariantDataDateTime(const TIMESTAMP_STRUCT* valptr) |
edca7a82 | 1024 | { m_value = wxDateTime(valptr->day, (wxDateTime::Month) (valptr->month - 1), valptr->year, |
254a2129 | 1025 | valptr->hour, valptr->minute, valptr->second, (wxDateTime::wxDateTime_t)valptr->fraction ); } |
edca7a82 GT |
1026 | #endif //ODBC |
1027 | ||
1028 | inline wxDateTime GetValue() const { return m_value; } | |
1029 | inline void SetValue(const wxDateTime& value) { m_value = value; } | |
1030 | ||
1031 | virtual void Copy(wxVariantData& data); | |
1032 | virtual bool Eq(wxVariantData& data) const; | |
1033 | #if wxUSE_STD_IOSTREAM | |
2b004197 | 1034 | virtual bool Write(wxSTD ostream& str) const; |
edca7a82 GT |
1035 | #endif |
1036 | virtual bool Write(wxString& str) const; | |
1037 | #if wxUSE_STD_IOSTREAM | |
2b004197 | 1038 | virtual bool Read(wxSTD istream& str); |
edca7a82 GT |
1039 | #endif |
1040 | virtual bool Read(wxString& str); | |
1041 | virtual wxString GetType() const { return wxT("datetime"); }; | |
1042 | virtual wxVariantData* Clone() { return new wxVariantDataDateTime; } | |
1043 | ||
1044 | protected: | |
1045 | wxDateTime m_value; | |
1046 | }; | |
1047 | ||
1048 | ||
1049 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDateTime, wxVariantData) | |
1050 | ||
1051 | void wxVariantDataDateTime::Copy(wxVariantData& data) | |
1052 | { | |
1053 | wxASSERT_MSG( (data.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Copy: Can't copy to this type of data") ); | |
1054 | ||
1055 | wxVariantDataDateTime& otherData = (wxVariantDataDateTime&) data; | |
1056 | ||
1057 | otherData.m_value = m_value; | |
1058 | } | |
1059 | ||
1060 | ||
1061 | bool wxVariantDataDateTime::Eq(wxVariantData& data) const | |
1062 | { | |
1063 | wxASSERT_MSG( (data.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") ); | |
1064 | ||
1065 | wxVariantDataDateTime& otherData = (wxVariantDataDateTime&) data; | |
1066 | ||
1067 | return (otherData.m_value == m_value); | |
1068 | } | |
1069 | ||
1070 | ||
1071 | #if wxUSE_STD_IOSTREAM | |
5eb1c08e | 1072 | bool wxVariantDataDateTime::Write(wxSTD ostream& WXUNUSED(str)) const |
edca7a82 GT |
1073 | { |
1074 | // Not implemented | |
cab1a605 | 1075 | return false; |
edca7a82 GT |
1076 | } |
1077 | #endif | |
1078 | ||
1079 | ||
1080 | bool wxVariantDataDateTime::Write(wxString& str) const | |
1081 | { | |
1082 | str = m_value.Format(); | |
cab1a605 | 1083 | return true; |
edca7a82 GT |
1084 | } |
1085 | ||
1086 | ||
1087 | #if wxUSE_STD_IOSTREAM | |
2b004197 | 1088 | bool wxVariantDataDateTime::Read(wxSTD istream& WXUNUSED(str)) |
edca7a82 GT |
1089 | { |
1090 | // Not implemented | |
cab1a605 | 1091 | return false; |
edca7a82 GT |
1092 | } |
1093 | #endif | |
1094 | ||
1095 | ||
1096 | bool wxVariantDataDateTime::Read(wxString& str) | |
1097 | { | |
1098 | if(! m_value.ParseDateTime(str)) | |
cab1a605 WS |
1099 | return false; |
1100 | return true; | |
edca7a82 GT |
1101 | } |
1102 | ||
e2b87f38 VZ |
1103 | #endif // wxUSE_DATETIME |
1104 | ||
fb42d7c3 VZ |
1105 | // ---------------------------------------------------------------------------- |
1106 | // wxVariantDataArrayString | |
1107 | // ---------------------------------------------------------------------------- | |
1108 | ||
1109 | class wxVariantDataArrayString: public wxVariantData | |
1110 | { | |
1111 | public: | |
1112 | wxVariantDataArrayString() { } | |
1113 | wxVariantDataArrayString(const wxArrayString& value) { m_value = value; } | |
1114 | ||
1115 | wxArrayString GetValue() const { return m_value; } | |
1116 | void SetValue(const wxArrayString& value) { m_value = value; } | |
1117 | ||
1118 | virtual void Copy(wxVariantData& data); | |
1119 | virtual bool Eq(wxVariantData& data) const; | |
1120 | #if wxUSE_STD_IOSTREAM | |
1121 | virtual bool Write(wxSTD ostream& str) const; | |
1122 | #endif | |
1123 | virtual bool Write(wxString& str) const; | |
1124 | #if wxUSE_STD_IOSTREAM | |
1125 | virtual bool Read(wxSTD istream& str); | |
1126 | #endif | |
1127 | virtual bool Read(wxString& str); | |
1128 | virtual wxString GetType() const { return wxT("arrstring"); }; | |
1129 | virtual wxVariantData* Clone() { return new wxVariantDataArrayString; } | |
1130 | ||
1131 | protected: | |
1132 | wxArrayString m_value; | |
1133 | ||
1134 | DECLARE_DYNAMIC_CLASS(wxVariantDataArrayString) | |
1135 | }; | |
1136 | ||
1137 | IMPLEMENT_DYNAMIC_CLASS(wxVariantDataArrayString, wxVariantData) | |
1138 | ||
1139 | void wxVariantDataArrayString::Copy(wxVariantData& data) | |
1140 | { | |
1141 | wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataArrayString::Copy: Can't copy to this type of data") ); | |
1142 | ||
1143 | wxVariantDataArrayString& otherData = (wxVariantDataArrayString&) data; | |
1144 | ||
1145 | otherData.m_value = m_value; | |
1146 | } | |
1147 | ||
1148 | ||
1149 | bool wxVariantDataArrayString::Eq(wxVariantData& data) const | |
1150 | { | |
1151 | wxASSERT_MSG( data.GetType() == GetType(), wxT("wxVariantDataArrayString::Eq: argument mismatch") ); | |
1152 | ||
1153 | wxVariantDataArrayString& otherData = (wxVariantDataArrayString&) data; | |
1154 | ||
1155 | return otherData.m_value == m_value; | |
1156 | } | |
1157 | ||
1158 | ||
1159 | #if wxUSE_STD_IOSTREAM | |
5eb1c08e | 1160 | bool wxVariantDataArrayString::Write(wxSTD ostream& WXUNUSED(str)) const |
fb42d7c3 VZ |
1161 | { |
1162 | // Not implemented | |
cab1a605 | 1163 | return false; |
fb42d7c3 VZ |
1164 | } |
1165 | #endif | |
1166 | ||
1167 | ||
1168 | bool wxVariantDataArrayString::Write(wxString& str) const | |
1169 | { | |
1170 | size_t count = m_value.GetCount(); | |
1171 | for ( size_t n = 0; n < count; n++ ) | |
1172 | { | |
1173 | if ( n ) | |
1174 | str += _T(';'); | |
1175 | ||
1176 | str += m_value[n]; | |
1177 | } | |
1178 | ||
cab1a605 | 1179 | return true; |
fb42d7c3 VZ |
1180 | } |
1181 | ||
1182 | ||
1183 | #if wxUSE_STD_IOSTREAM | |
1184 | bool wxVariantDataArrayString::Read(wxSTD istream& WXUNUSED(str)) | |
1185 | { | |
1186 | // Not implemented | |
cab1a605 | 1187 | return false; |
fb42d7c3 VZ |
1188 | } |
1189 | #endif | |
1190 | ||
1191 | ||
1192 | bool wxVariantDataArrayString::Read(wxString& str) | |
1193 | { | |
1194 | wxStringTokenizer tk(str, _T(";")); | |
1195 | while ( tk.HasMoreTokens() ) | |
1196 | { | |
1197 | m_value.Add(tk.GetNextToken()); | |
1198 | } | |
1199 | ||
cab1a605 | 1200 | return true; |
fb42d7c3 VZ |
1201 | } |
1202 | ||
1203 | ||
a0a302dc | 1204 | |
341287bf JS |
1205 | /* |
1206 | * wxVariant | |
1207 | */ | |
1208 | ||
1209 | IMPLEMENT_DYNAMIC_CLASS(wxVariant, wxObject) | |
1210 | ||
1211 | // Construction & destruction | |
1212 | wxVariant::wxVariant() | |
1213 | { | |
1214 | m_data = (wxVariantData*) NULL; | |
1215 | } | |
1216 | ||
a0a302dc | 1217 | wxVariant::wxVariant(double val, const wxString& name) |
341287bf JS |
1218 | { |
1219 | m_data = new wxVariantDataReal(val); | |
a0a302dc | 1220 | m_name = name; |
341287bf JS |
1221 | } |
1222 | ||
a0a302dc | 1223 | wxVariant::wxVariant(long val, const wxString& name) |
341287bf JS |
1224 | { |
1225 | m_data = new wxVariantDataLong(val); | |
a0a302dc | 1226 | m_name = name; |
341287bf JS |
1227 | } |
1228 | ||
57493f9f | 1229 | #ifdef HAVE_BOOL |
a0a302dc | 1230 | wxVariant::wxVariant(bool val, const wxString& name) |
341287bf JS |
1231 | { |
1232 | m_data = new wxVariantDataBool(val); | |
a0a302dc | 1233 | m_name = name; |
341287bf | 1234 | } |
57493f9f | 1235 | #endif |
341287bf | 1236 | |
a0a302dc | 1237 | wxVariant::wxVariant(char val, const wxString& name) |
341287bf JS |
1238 | { |
1239 | m_data = new wxVariantDataChar(val); | |
a0a302dc | 1240 | m_name = name; |
341287bf JS |
1241 | } |
1242 | ||
a0a302dc | 1243 | wxVariant::wxVariant(const wxString& val, const wxString& name) |
341287bf JS |
1244 | { |
1245 | m_data = new wxVariantDataString(val); | |
a0a302dc | 1246 | m_name = name; |
341287bf JS |
1247 | } |
1248 | ||
783b6cfd | 1249 | wxVariant::wxVariant(const wxChar* val, const wxString& name) |
341287bf JS |
1250 | { |
1251 | m_data = new wxVariantDataString(wxString(val)); | |
a0a302dc | 1252 | m_name = name; |
341287bf JS |
1253 | } |
1254 | ||
2c3a1064 RN |
1255 | #if WXWIN_COMPATIBILITY_2_4 |
1256 | ||
a0a302dc | 1257 | wxVariant::wxVariant(const wxStringList& val, const wxString& name) |
341287bf JS |
1258 | { |
1259 | m_data = new wxVariantDataStringList(val); | |
a0a302dc | 1260 | m_name = name; |
341287bf | 1261 | } |
341287bf | 1262 | |
2c3a1064 RN |
1263 | #endif |
1264 | ||
a0a302dc | 1265 | wxVariant::wxVariant(const wxList& val, const wxString& name) // List of variants |
341287bf JS |
1266 | { |
1267 | m_data = new wxVariantDataList(val); | |
a0a302dc JS |
1268 | m_name = name; |
1269 | } | |
1270 | ||
b41f79f5 JJ |
1271 | wxVariant::wxVariant( void* val, const wxString& name) |
1272 | { | |
cab1a605 WS |
1273 | m_data = new wxVariantDataVoidPtr(val); |
1274 | m_name = name; | |
b41f79f5 JJ |
1275 | } |
1276 | ||
cf6ae290 RG |
1277 | wxVariant::wxVariant( wxObject* val, const wxString& name) |
1278 | { | |
cab1a605 WS |
1279 | m_data = new wxVariantDataWxObjectPtr(val); |
1280 | m_name = name; | |
cf6ae290 RG |
1281 | } |
1282 | ||
ff818ab8 RG |
1283 | #if wxUSE_DATETIME |
1284 | wxVariant::wxVariant(const wxDateTime& val, const wxString& name) // Date | |
1285 | { | |
cab1a605 WS |
1286 | m_data = new wxVariantDataDateTime(val); |
1287 | m_name = name; | |
ff818ab8 | 1288 | } |
cab1a605 | 1289 | #endif // wxUSE_DATETIME |
ff818ab8 | 1290 | |
edca7a82 GT |
1291 | #if wxUSE_ODBC |
1292 | wxVariant::wxVariant(const TIME_STRUCT* valptr, const wxString& name) // Date | |
1293 | { | |
1294 | m_data = new wxVariantDataDateTime(valptr); | |
1295 | m_name = name; | |
1296 | } | |
1297 | ||
1298 | wxVariant::wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name) // Date | |
1299 | { | |
1300 | m_data = new wxVariantDataDateTime(valptr); | |
1301 | m_name = name; | |
1302 | } | |
1303 | ||
1304 | wxVariant::wxVariant(const DATE_STRUCT* valptr, const wxString& name) // Date | |
1305 | { | |
1306 | m_data = new wxVariantDataDateTime(valptr); | |
1307 | m_name = name; | |
1308 | } | |
fb42d7c3 VZ |
1309 | #endif // wxUSE_ODBC |
1310 | ||
1311 | wxVariant::wxVariant(const wxArrayString& val, const wxString& name) // Strings | |
1312 | { | |
1313 | m_data = new wxVariantDataArrayString(val); | |
1314 | m_name = name; | |
1315 | } | |
edca7a82 | 1316 | |
341287bf | 1317 | wxVariant::wxVariant(const wxVariant& variant) |
d84afea9 | 1318 | : wxObject() |
341287bf JS |
1319 | { |
1320 | if (!variant.IsNull()) | |
1321 | { | |
1322 | m_data = (wxVariantData*) variant.GetData()->GetClassInfo()->CreateObject(); | |
1323 | variant.m_data->Copy(*m_data); | |
1324 | } | |
4fabb575 JS |
1325 | else |
1326 | m_data = (wxVariantData*) NULL; | |
a0a302dc | 1327 | m_name = variant.m_name; |
341287bf JS |
1328 | } |
1329 | ||
a0a302dc | 1330 | wxVariant::wxVariant(wxVariantData* data, const wxString& name) // User-defined data |
341287bf JS |
1331 | { |
1332 | m_data = data; | |
a0a302dc | 1333 | m_name = name; |
341287bf JS |
1334 | } |
1335 | ||
1336 | wxVariant::~wxVariant() | |
1337 | { | |
1338 | delete m_data; | |
1339 | } | |
1340 | ||
1341 | ||
1342 | // Make NULL (i.e. delete the data) | |
1343 | void wxVariant::MakeNull() | |
1344 | { | |
1345 | delete m_data; | |
1346 | m_data = NULL; | |
1347 | } | |
1348 | ||
1349 | // Generic operators | |
1350 | // Assignment | |
1351 | void wxVariant::operator= (const wxVariant& variant) | |
1352 | { | |
1353 | if (variant.IsNull()) | |
1354 | { | |
1355 | MakeNull(); | |
1356 | return; | |
1357 | } | |
1358 | ||
1359 | if (IsNull() || (GetType() != variant.GetType())) | |
1360 | { | |
1361 | if (m_data) | |
1362 | delete m_data; | |
1363 | m_data = (wxVariantData*) variant.GetData()->GetClassInfo()->CreateObject(); | |
1364 | } | |
3d8daa0f | 1365 | |
4fabb575 | 1366 | variant.GetData()->Copy(* GetData()); |
3d8daa0f VZ |
1367 | |
1368 | m_name = variant.m_name; | |
341287bf JS |
1369 | } |
1370 | ||
1371 | // Assignment using data, e.g. | |
1372 | // myVariant = new wxStringVariantData("hello") | |
1373 | void wxVariant::operator= (wxVariantData* variantData) | |
1374 | { | |
1375 | MakeNull(); | |
1376 | m_data = variantData; | |
1377 | } | |
1378 | ||
1379 | bool wxVariant::operator== (const wxVariant& variant) const | |
1380 | { | |
1381 | if (IsNull() || variant.IsNull()) | |
1382 | return (IsNull() == variant.IsNull()); | |
1383 | ||
1384 | return (GetData()->Eq(* variant.GetData())); | |
1385 | } | |
1386 | ||
1387 | bool wxVariant::operator!= (const wxVariant& variant) const | |
1388 | { | |
1389 | return (!(*this == variant)); | |
1390 | } | |
1391 | ||
1392 | ||
1393 | // Specific operators | |
1394 | bool wxVariant::operator== (double value) const | |
1395 | { | |
1396 | double thisValue; | |
1397 | if (!Convert(&thisValue)) | |
cab1a605 | 1398 | return false; |
bc14c8b2 VZ |
1399 | |
1400 | return wxIsSameDouble(value, thisValue); | |
341287bf JS |
1401 | } |
1402 | ||
1403 | bool wxVariant::operator!= (double value) const | |
1404 | { | |
1405 | return (!((*this) == value)); | |
1406 | } | |
1407 | ||
1408 | void wxVariant::operator= (double value) | |
1409 | { | |
223d09f6 | 1410 | if (GetType() == wxT("double")) |
341287bf JS |
1411 | { |
1412 | ((wxVariantDataReal*)GetData())->SetValue(value); | |
1413 | } | |
1414 | else | |
1415 | { | |
1416 | if (m_data) | |
1417 | delete m_data; | |
1418 | m_data = new wxVariantDataReal(value); | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | bool wxVariant::operator== (long value) const | |
1423 | { | |
1424 | long thisValue; | |
1425 | if (!Convert(&thisValue)) | |
cab1a605 | 1426 | return false; |
341287bf JS |
1427 | else |
1428 | return (value == thisValue); | |
1429 | } | |
1430 | ||
1431 | bool wxVariant::operator!= (long value) const | |
1432 | { | |
1433 | return (!((*this) == value)); | |
1434 | } | |
1435 | ||
1436 | void wxVariant::operator= (long value) | |
1437 | { | |
223d09f6 | 1438 | if (GetType() == wxT("long")) |
341287bf JS |
1439 | { |
1440 | ((wxVariantDataLong*)GetData())->SetValue(value); | |
1441 | } | |
1442 | else | |
1443 | { | |
1444 | if (m_data) | |
1445 | delete m_data; | |
1446 | m_data = new wxVariantDataLong(value); | |
1447 | } | |
1448 | } | |
1449 | ||
1450 | bool wxVariant::operator== (char value) const | |
1451 | { | |
1452 | char thisValue; | |
1453 | if (!Convert(&thisValue)) | |
cab1a605 | 1454 | return false; |
341287bf JS |
1455 | else |
1456 | return (value == thisValue); | |
1457 | } | |
1458 | ||
1459 | bool wxVariant::operator!= (char value) const | |
1460 | { | |
1461 | return (!((*this) == value)); | |
1462 | } | |
1463 | ||
1464 | void wxVariant::operator= (char value) | |
1465 | { | |
223d09f6 | 1466 | if (GetType() == wxT("char")) |
341287bf JS |
1467 | { |
1468 | ((wxVariantDataChar*)GetData())->SetValue(value); | |
1469 | } | |
1470 | else | |
1471 | { | |
1472 | if (m_data) | |
1473 | delete m_data; | |
1474 | m_data = new wxVariantDataChar(value); | |
1475 | } | |
1476 | } | |
1477 | ||
57493f9f | 1478 | #ifdef HAVE_BOOL |
341287bf JS |
1479 | bool wxVariant::operator== (bool value) const |
1480 | { | |
1481 | bool thisValue; | |
1482 | if (!Convert(&thisValue)) | |
cab1a605 | 1483 | return false; |
341287bf JS |
1484 | else |
1485 | return (value == thisValue); | |
1486 | } | |
1487 | ||
1488 | bool wxVariant::operator!= (bool value) const | |
1489 | { | |
1490 | return (!((*this) == value)); | |
1491 | } | |
1492 | ||
1493 | void wxVariant::operator= (bool value) | |
1494 | { | |
223d09f6 | 1495 | if (GetType() == wxT("bool")) |
341287bf JS |
1496 | { |
1497 | ((wxVariantDataBool*)GetData())->SetValue(value); | |
1498 | } | |
1499 | else | |
1500 | { | |
1501 | if (m_data) | |
1502 | delete m_data; | |
1503 | m_data = new wxVariantDataBool(value); | |
1504 | } | |
1505 | } | |
57493f9f | 1506 | #endif // HAVE_BOOL |
341287bf JS |
1507 | |
1508 | bool wxVariant::operator== (const wxString& value) const | |
1509 | { | |
1510 | wxString thisValue; | |
1511 | if (!Convert(&thisValue)) | |
cab1a605 | 1512 | return false; |
f6bcfd97 BP |
1513 | |
1514 | return value == thisValue; | |
341287bf JS |
1515 | } |
1516 | ||
1517 | bool wxVariant::operator!= (const wxString& value) const | |
1518 | { | |
1519 | return (!((*this) == value)); | |
1520 | } | |
1521 | ||
1522 | void wxVariant::operator= (const wxString& value) | |
1523 | { | |
223d09f6 | 1524 | if (GetType() == wxT("string")) |
341287bf JS |
1525 | { |
1526 | ((wxVariantDataString*)GetData())->SetValue(value); | |
1527 | } | |
1528 | else | |
1529 | { | |
1530 | if (m_data) | |
1531 | delete m_data; | |
1532 | m_data = new wxVariantDataString(value); | |
1533 | } | |
1534 | } | |
1535 | ||
783b6cfd | 1536 | void wxVariant::operator= (const wxChar* value) |
341287bf | 1537 | { |
223d09f6 | 1538 | if (GetType() == wxT("string")) |
341287bf JS |
1539 | { |
1540 | ((wxVariantDataString*)GetData())->SetValue(wxString(value)); | |
1541 | } | |
1542 | else | |
1543 | { | |
1544 | if (m_data) | |
1545 | delete m_data; | |
1546 | m_data = new wxVariantDataString(wxString(value)); | |
1547 | } | |
1548 | } | |
1549 | ||
2c3a1064 RN |
1550 | #if WXWIN_COMPATIBILITY_2_4 |
1551 | ||
341287bf JS |
1552 | bool wxVariant::operator== (const wxStringList& value) const |
1553 | { | |
223d09f6 | 1554 | wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") ); |
341287bf JS |
1555 | |
1556 | wxVariantDataStringList other(value); | |
1557 | return (m_data->Eq(other)); | |
1558 | } | |
1559 | ||
1560 | bool wxVariant::operator!= (const wxStringList& value) const | |
1561 | { | |
52e81242 RN |
1562 | wxASSERT_MSG( (GetType() == wxT("stringlist")), wxT("Invalid type for == operator") ); |
1563 | ||
1564 | wxVariantDataStringList other(value); | |
1565 | return !(m_data->Eq(other)); | |
341287bf JS |
1566 | } |
1567 | ||
1568 | void wxVariant::operator= (const wxStringList& value) | |
1569 | { | |
223d09f6 | 1570 | if (GetType() == wxT("stringlist")) |
341287bf JS |
1571 | { |
1572 | ((wxVariantDataStringList*)GetData())->SetValue(value); | |
1573 | } | |
1574 | else | |
1575 | { | |
1576 | if (m_data) | |
1577 | delete m_data; | |
1578 | m_data = new wxVariantDataStringList(value); | |
1579 | } | |
1580 | } | |
1581 | ||
2c3a1064 RN |
1582 | #endif |
1583 | ||
341287bf JS |
1584 | bool wxVariant::operator== (const wxList& value) const |
1585 | { | |
223d09f6 | 1586 | wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for == operator") ); |
341287bf JS |
1587 | |
1588 | wxVariantDataList other(value); | |
1589 | return (m_data->Eq(other)); | |
1590 | } | |
1591 | ||
1592 | bool wxVariant::operator!= (const wxList& value) const | |
1593 | { | |
1594 | return (!((*this) == value)); | |
1595 | } | |
1596 | ||
1597 | void wxVariant::operator= (const wxList& value) | |
1598 | { | |
223d09f6 | 1599 | if (GetType() == wxT("list")) |
341287bf JS |
1600 | { |
1601 | ((wxVariantDataList*)GetData())->SetValue(value); | |
1602 | } | |
1603 | else | |
1604 | { | |
1605 | if (m_data) | |
1606 | delete m_data; | |
1607 | m_data = new wxVariantDataList(value); | |
1608 | } | |
1609 | } | |
1610 | ||
a0a302dc JS |
1611 | bool wxVariant::operator== (void* value) const |
1612 | { | |
1613 | return (value == ((wxVariantDataVoidPtr*)GetData())->GetValue()); | |
1614 | } | |
1615 | ||
1616 | bool wxVariant::operator!= (void* value) const | |
1617 | { | |
f6bcfd97 | 1618 | return (!((*this) == (void*) value)); |
a0a302dc JS |
1619 | } |
1620 | ||
1621 | void wxVariant::operator= (void* value) | |
1622 | { | |
223d09f6 | 1623 | if (GetType() == wxT("void*")) |
a0a302dc JS |
1624 | { |
1625 | ((wxVariantDataVoidPtr*)GetData())->SetValue(value); | |
1626 | } | |
1627 | else | |
1628 | { | |
1629 | if (m_data) | |
1630 | delete m_data; | |
1631 | m_data = new wxVariantDataVoidPtr(value); | |
1632 | } | |
1633 | } | |
341287bf | 1634 | |
e2b87f38 | 1635 | #if wxUSE_DATETIME |
edca7a82 GT |
1636 | bool wxVariant::operator== (const wxDateTime& value) const |
1637 | { | |
1638 | wxDateTime thisValue; | |
1639 | if (!Convert(&thisValue)) | |
cab1a605 | 1640 | return false; |
edca7a82 GT |
1641 | |
1642 | return value.IsEqualTo(thisValue); | |
1643 | } | |
1644 | ||
1645 | bool wxVariant::operator!= (const wxDateTime& value) const | |
1646 | { | |
1647 | return (!((*this) == value)); | |
1648 | } | |
1649 | ||
1650 | void wxVariant::operator= (const wxDateTime& value) | |
1651 | { | |
1652 | if (GetType() == wxT("datetime")) | |
1653 | { | |
1654 | ((wxVariantDataDateTime*)GetData())->SetValue(value); | |
1655 | } | |
1656 | else | |
1657 | { | |
1658 | if (m_data) | |
1659 | delete m_data; | |
1660 | m_data = new wxVariantDataDateTime(value); | |
1661 | } | |
1662 | } | |
e2b87f38 | 1663 | #endif // wxUSE_DATETIME |
edca7a82 | 1664 | |
edca7a82 GT |
1665 | #if wxUSE_ODBC |
1666 | void wxVariant::operator= (const DATE_STRUCT* value) | |
1667 | { | |
1668 | if (m_data) | |
1669 | delete m_data; | |
1670 | m_data = new wxVariantDataDateTime(value); | |
1671 | } | |
1672 | ||
1673 | ||
1674 | void wxVariant::operator= (const TIME_STRUCT* value) | |
1675 | { | |
1676 | if (m_data) | |
1677 | delete m_data; | |
1678 | m_data = new wxVariantDataDateTime(value); | |
1679 | } | |
1680 | ||
1681 | ||
1682 | void wxVariant::operator= (const TIMESTAMP_STRUCT* value) | |
1683 | { | |
1684 | if (m_data) | |
1685 | delete m_data; | |
1686 | m_data = new wxVariantDataDateTime(value); | |
1687 | } | |
1688 | ||
fb42d7c3 VZ |
1689 | #endif // wxUSE_ODBC |
1690 | ||
574c939e | 1691 | bool wxVariant::operator==(const wxArrayString& WXUNUSED(value)) const |
fb42d7c3 VZ |
1692 | { |
1693 | wxFAIL_MSG( _T("TODO") ); | |
1694 | ||
cab1a605 | 1695 | return false; |
fb42d7c3 VZ |
1696 | } |
1697 | ||
1698 | bool wxVariant::operator!=(const wxArrayString& value) const | |
1699 | { | |
1700 | return !(*this == value); | |
1701 | } | |
1702 | ||
1703 | void wxVariant::operator=(const wxArrayString& value) | |
1704 | { | |
1705 | if (GetType() == wxT("arrstring")) | |
1706 | { | |
1707 | ((wxVariantDataArrayString *)GetData())->SetValue(value); | |
1708 | } | |
1709 | else | |
1710 | { | |
1711 | delete m_data; | |
1712 | m_data = new wxVariantDataArrayString(value); | |
1713 | } | |
1714 | } | |
1715 | ||
1716 | wxArrayString wxVariant::GetArrayString() const | |
1717 | { | |
1718 | if ( GetType() == wxT("arrstring") ) | |
1719 | return ((wxVariantDataArrayString *)GetData())->GetValue(); | |
1720 | ||
1721 | return wxArrayString(); | |
1722 | } | |
1723 | ||
edca7a82 | 1724 | |
341287bf JS |
1725 | // Treat a list variant as an array |
1726 | wxVariant wxVariant::operator[] (size_t idx) const | |
1727 | { | |
2c3a1064 | 1728 | #if WXWIN_COMPATIBILITY_2_4 |
223d09f6 | 1729 | wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for array operator") ); |
2c3a1064 RN |
1730 | #else |
1731 | wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for array operator") ); | |
1732 | #endif | |
341287bf | 1733 | |
223d09f6 | 1734 | if (GetType() == wxT("list")) |
341287bf JS |
1735 | { |
1736 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
43f06cfd | 1737 | wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); |
b1d4dd7a | 1738 | return * (wxVariant*) (data->GetValue().Item(idx)->GetData()); |
341287bf | 1739 | } |
2c3a1064 | 1740 | #if WXWIN_COMPATIBILITY_2_4 |
223d09f6 | 1741 | else if (GetType() == wxT("stringlist")) |
341287bf JS |
1742 | { |
1743 | wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; | |
43f06cfd | 1744 | wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); |
341287bf | 1745 | |
254a2129 WS |
1746 | wxString str( (const wxChar*) (data->GetValue().Item(idx)->GetData()) ); |
1747 | wxVariant variant( str ); | |
341287bf JS |
1748 | return variant; |
1749 | } | |
2c3a1064 | 1750 | #endif |
341287bf JS |
1751 | return wxNullVariant; |
1752 | } | |
1753 | ||
1754 | wxVariant& wxVariant::operator[] (size_t idx) | |
1755 | { | |
1756 | // We can't return a reference to a variant for a string list, since the string | |
1757 | // is actually stored as a char*, not a variant. | |
1758 | ||
223d09f6 | 1759 | wxASSERT_MSG( (GetType() == wxT("list")), wxT("Invalid type for array operator") ); |
341287bf JS |
1760 | |
1761 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
43f06cfd | 1762 | wxASSERT_MSG( (idx < data->GetValue().GetCount()), wxT("Invalid index for array") ); |
341287bf | 1763 | |
b1d4dd7a | 1764 | return * (wxVariant*) (data->GetValue().Item(idx)->GetData()); |
341287bf JS |
1765 | } |
1766 | ||
1767 | // Return the number of elements in a list | |
43f06cfd | 1768 | size_t wxVariant::GetCount() const |
341287bf | 1769 | { |
2c3a1064 | 1770 | #if WXWIN_COMPATIBILITY_2_4 |
223d09f6 | 1771 | wxASSERT_MSG( (GetType() == wxT("list") || GetType() == wxT("stringlist")), wxT("Invalid type for GetCount()") ); |
2c3a1064 RN |
1772 | #else |
1773 | wxASSERT_MSG( GetType() == wxT("list"), wxT("Invalid type for GetCount()") ); | |
1774 | #endif | |
341287bf | 1775 | |
223d09f6 | 1776 | if (GetType() == wxT("list")) |
341287bf JS |
1777 | { |
1778 | wxVariantDataList* data = (wxVariantDataList*) m_data; | |
b1d4dd7a | 1779 | return data->GetValue().GetCount(); |
341287bf | 1780 | } |
2c3a1064 | 1781 | #if WXWIN_COMPATIBILITY_2_4 |
223d09f6 | 1782 | else if (GetType() == wxT("stringlist")) |
341287bf JS |
1783 | { |
1784 | wxVariantDataStringList* data = (wxVariantDataStringList*) m_data; | |
b1d4dd7a | 1785 | return data->GetValue().GetCount(); |
341287bf | 1786 | } |
2c3a1064 | 1787 | #endif |
341287bf JS |
1788 | return 0; |
1789 | } | |
1790 | ||
1791 | wxString wxVariant::MakeString() const | |
1792 | { | |
1793 | if (!IsNull()) | |
1794 | { | |
1795 | wxString str; | |
1796 | if (GetData()->Write(str)) | |
1797 | return str; | |
1798 | } | |
525d8583 | 1799 | return wxEmptyString; |
341287bf JS |
1800 | } |
1801 | ||
1802 | // Accessors | |
1803 | ||
1804 | void wxVariant::SetData(wxVariantData* data) | |
1805 | { | |
1806 | if (m_data) delete m_data; | |
1807 | m_data = data; | |
1808 | } | |
1809 | ||
1810 | ||
1811 | // Returns a string representing the type of the variant, | |
2c3a1064 | 1812 | // e.g. "string", "bool", "list", "double", "long" |
341287bf JS |
1813 | wxString wxVariant::GetType() const |
1814 | { | |
1815 | if (IsNull()) | |
223d09f6 | 1816 | return wxString(wxT("null")); |
341287bf JS |
1817 | else |
1818 | return m_data->GetType(); | |
1819 | } | |
1820 | ||
1821 | ||
1822 | bool wxVariant::IsType(const wxString& type) const | |
1823 | { | |
1824 | return (GetType() == type); | |
1825 | } | |
1826 | ||
cf6ae290 RG |
1827 | bool wxVariant::IsValueKindOf(const wxClassInfo* type) const |
1828 | { | |
cab1a605 | 1829 | wxClassInfo* info=m_data->GetValueClassInfo(); |
cf6ae290 RG |
1830 | return info ? info->IsKindOf(type) : false ; |
1831 | } | |
1832 | ||
341287bf JS |
1833 | |
1834 | // Value accessors | |
1835 | double wxVariant::GetReal() const | |
1836 | { | |
1837 | double value; | |
1838 | if (Convert(& value)) | |
1839 | return value; | |
1840 | else | |
1841 | { | |
223d09f6 | 1842 | wxFAIL_MSG(wxT("Could not convert to a real number")); |
341287bf JS |
1843 | return 0.0; |
1844 | } | |
1845 | } | |
1846 | ||
1847 | long wxVariant::GetInteger() const | |
1848 | { | |
1849 | long value; | |
1850 | if (Convert(& value)) | |
1851 | return value; | |
1852 | else | |
1853 | { | |
223d09f6 | 1854 | wxFAIL_MSG(wxT("Could not convert to an integer")); |
341287bf JS |
1855 | return 0; |
1856 | } | |
1857 | } | |
1858 | ||
1859 | char wxVariant::GetChar() const | |
1860 | { | |
1861 | char value; | |
1862 | if (Convert(& value)) | |
1863 | return value; | |
1864 | else | |
1865 | { | |
223d09f6 | 1866 | wxFAIL_MSG(wxT("Could not convert to a char")); |
341287bf JS |
1867 | return 0; |
1868 | } | |
1869 | } | |
1870 | ||
1871 | bool wxVariant::GetBool() const | |
1872 | { | |
1873 | bool value; | |
1874 | if (Convert(& value)) | |
1875 | return value; | |
1876 | else | |
1877 | { | |
223d09f6 | 1878 | wxFAIL_MSG(wxT("Could not convert to a bool")); |
341287bf JS |
1879 | return 0; |
1880 | } | |
1881 | } | |
1882 | ||
1883 | wxString wxVariant::GetString() const | |
1884 | { | |
1885 | wxString value; | |
f6bcfd97 | 1886 | if (!Convert(& value)) |
341287bf | 1887 | { |
223d09f6 | 1888 | wxFAIL_MSG(wxT("Could not convert to a string")); |
341287bf | 1889 | } |
f6bcfd97 BP |
1890 | |
1891 | return value; | |
341287bf JS |
1892 | } |
1893 | ||
a0a302dc JS |
1894 | void* wxVariant::GetVoidPtr() const |
1895 | { | |
223d09f6 | 1896 | wxASSERT( (GetType() == wxT("void*")) ); |
a0a302dc JS |
1897 | |
1898 | return (void*) ((wxVariantDataVoidPtr*) m_data)->GetValue(); | |
1899 | } | |
1900 | ||
cab1a605 | 1901 | wxObject* wxVariant::GetWxObjectPtr() |
cf6ae290 RG |
1902 | { |
1903 | wxASSERT(wxIsKindOf(m_data, wxVariantDataWxObjectPtr)); | |
1904 | return (wxObject*) ((wxVariantDataWxObjectPtr*) m_data)->GetValue(); | |
1905 | } | |
1906 | ||
e2b87f38 | 1907 | #if wxUSE_DATETIME |
edca7a82 GT |
1908 | wxDateTime wxVariant::GetDateTime() const |
1909 | { | |
1910 | wxDateTime value; | |
1911 | if (!Convert(& value)) | |
1912 | { | |
1913 | wxFAIL_MSG(wxT("Could not convert to a datetime")); | |
1914 | } | |
1915 | ||
1916 | return value; | |
1917 | } | |
e2b87f38 | 1918 | #endif // wxUSE_DATETIME |
edca7a82 | 1919 | |
341287bf JS |
1920 | wxList& wxVariant::GetList() const |
1921 | { | |
223d09f6 | 1922 | wxASSERT( (GetType() == wxT("list")) ); |
341287bf JS |
1923 | |
1924 | return (wxList&) ((wxVariantDataList*) m_data)->GetValue(); | |
1925 | } | |
1926 | ||
2c3a1064 RN |
1927 | #if WXWIN_COMPATIBILITY_2_4 |
1928 | ||
341287bf JS |
1929 | wxStringList& wxVariant::GetStringList() const |
1930 | { | |
223d09f6 | 1931 | wxASSERT( (GetType() == wxT("stringlist")) ); |
341287bf JS |
1932 | |
1933 | return (wxStringList&) ((wxVariantDataStringList*) m_data)->GetValue(); | |
1934 | } | |
1935 | ||
2c3a1064 RN |
1936 | #endif |
1937 | ||
5dfe6069 VZ |
1938 | // Make empty list |
1939 | void wxVariant::NullList() | |
1940 | { | |
1941 | SetData(new wxVariantDataList()); | |
4115960d | 1942 | } |
5dfe6069 | 1943 | |
341287bf JS |
1944 | // Append to list |
1945 | void wxVariant::Append(const wxVariant& value) | |
1946 | { | |
1947 | wxList& list = GetList(); | |
1948 | ||
1949 | list.Append(new wxVariant(value)); | |
1950 | } | |
1951 | ||
1952 | // Insert at front of list | |
1953 | void wxVariant::Insert(const wxVariant& value) | |
1954 | { | |
1955 | wxList& list = GetList(); | |
1956 | ||
1957 | list.Insert(new wxVariant(value)); | |
1958 | } | |
1959 | ||
cab1a605 | 1960 | // Returns true if the variant is a member of the list |
341287bf JS |
1961 | bool wxVariant::Member(const wxVariant& value) const |
1962 | { | |
1963 | wxList& list = GetList(); | |
1964 | ||
df5168c4 | 1965 | wxList::compatibility_iterator node = list.GetFirst(); |
341287bf JS |
1966 | while (node) |
1967 | { | |
b1d4dd7a | 1968 | wxVariant* other = (wxVariant*) node->GetData(); |
341287bf | 1969 | if (value == *other) |
cab1a605 | 1970 | return true; |
b1d4dd7a | 1971 | node = node->GetNext(); |
341287bf | 1972 | } |
cab1a605 | 1973 | return false; |
341287bf JS |
1974 | } |
1975 | ||
1976 | // Deletes the nth element of the list | |
43f06cfd | 1977 | bool wxVariant::Delete(size_t item) |
341287bf JS |
1978 | { |
1979 | wxList& list = GetList(); | |
1980 | ||
43f06cfd | 1981 | wxASSERT_MSG( (item < list.GetCount()), wxT("Invalid index to Delete") ); |
df5168c4 | 1982 | wxList::compatibility_iterator node = list.Item(item); |
b1d4dd7a | 1983 | wxVariant* variant = (wxVariant*) node->GetData(); |
341287bf | 1984 | delete variant; |
df5168c4 | 1985 | list.Erase(node); |
cab1a605 | 1986 | return true; |
341287bf JS |
1987 | } |
1988 | ||
1989 | // Clear list | |
1990 | void wxVariant::ClearList() | |
1991 | { | |
223d09f6 | 1992 | if (!IsNull() && (GetType() == wxT("list"))) |
341287bf JS |
1993 | { |
1994 | ((wxVariantDataList*) m_data)->Clear(); | |
1995 | } | |
1996 | else | |
1997 | { | |
489f6cf7 | 1998 | if (!GetType().IsSameAs(wxT("list"))) |
341287bf JS |
1999 | { |
2000 | delete m_data; | |
2001 | m_data = NULL; | |
2002 | } | |
2003 | m_data = new wxVariantDataList; | |
2004 | } | |
2005 | } | |
2006 | ||
2007 | // Type conversion | |
2008 | bool wxVariant::Convert(long* value) const | |
2009 | { | |
2010 | wxString type(GetType()); | |
223d09f6 | 2011 | if (type == wxT("double")) |
341287bf | 2012 | *value = (long) (((wxVariantDataReal*)GetData())->GetValue()); |
223d09f6 | 2013 | else if (type == wxT("long")) |
341287bf | 2014 | *value = ((wxVariantDataLong*)GetData())->GetValue(); |
862416e0 | 2015 | #ifdef HAVE_BOOL |
223d09f6 | 2016 | else if (type == wxT("bool")) |
341287bf | 2017 | *value = (long) (((wxVariantDataBool*)GetData())->GetValue()); |
862416e0 | 2018 | #endif |
223d09f6 | 2019 | else if (type == wxT("string")) |
783b6cfd | 2020 | *value = wxAtol((const wxChar*) ((wxVariantDataString*)GetData())->GetValue()); |
341287bf | 2021 | else |
cab1a605 | 2022 | return false; |
341287bf | 2023 | |
cab1a605 | 2024 | return true; |
341287bf JS |
2025 | } |
2026 | ||
2027 | bool wxVariant::Convert(bool* value) const | |
2028 | { | |
2029 | wxString type(GetType()); | |
223d09f6 | 2030 | if (type == wxT("double")) |
341287bf | 2031 | *value = ((int) (((wxVariantDataReal*)GetData())->GetValue()) != 0); |
223d09f6 | 2032 | else if (type == wxT("long")) |
341287bf | 2033 | *value = (((wxVariantDataLong*)GetData())->GetValue() != 0); |
862416e0 | 2034 | #ifdef HAVE_BOOL |
223d09f6 | 2035 | else if (type == wxT("bool")) |
341287bf | 2036 | *value = ((wxVariantDataBool*)GetData())->GetValue(); |
862416e0 | 2037 | #endif |
223d09f6 | 2038 | else if (type == wxT("string")) |
341287bf JS |
2039 | { |
2040 | wxString val(((wxVariantDataString*)GetData())->GetValue()); | |
2041 | val.MakeLower(); | |
b1638abf | 2042 | if (val == wxT("true") || val == wxT("yes") || val == wxT('1') ) |
cab1a605 | 2043 | *value = true; |
b1638abf | 2044 | else if (val == wxT("false") || val == wxT("no") || val == wxT('0') ) |
cab1a605 | 2045 | *value = false; |
341287bf | 2046 | else |
cab1a605 | 2047 | return false; |
341287bf JS |
2048 | } |
2049 | else | |
cab1a605 | 2050 | return false; |
341287bf | 2051 | |
cab1a605 | 2052 | return true; |
341287bf JS |
2053 | } |
2054 | ||
2055 | bool wxVariant::Convert(double* value) const | |
2056 | { | |
2057 | wxString type(GetType()); | |
223d09f6 | 2058 | if (type == wxT("double")) |
341287bf | 2059 | *value = ((wxVariantDataReal*)GetData())->GetValue(); |
223d09f6 | 2060 | else if (type == wxT("long")) |
341287bf | 2061 | *value = (double) (((wxVariantDataLong*)GetData())->GetValue()); |
862416e0 | 2062 | #ifdef HAVE_BOOL |
223d09f6 | 2063 | else if (type == wxT("bool")) |
341287bf | 2064 | *value = (double) (((wxVariantDataBool*)GetData())->GetValue()); |
862416e0 | 2065 | #endif |
223d09f6 | 2066 | else if (type == wxT("string")) |
783b6cfd | 2067 | *value = (double) wxAtof((const wxChar*) ((wxVariantDataString*)GetData())->GetValue()); |
341287bf | 2068 | else |
cab1a605 | 2069 | return false; |
341287bf | 2070 | |
cab1a605 | 2071 | return true; |
341287bf JS |
2072 | } |
2073 | ||
2074 | bool wxVariant::Convert(char* value) const | |
2075 | { | |
2076 | wxString type(GetType()); | |
223d09f6 | 2077 | if (type == wxT("char")) |
341287bf | 2078 | *value = ((wxVariantDataChar*)GetData())->GetValue(); |
223d09f6 | 2079 | else if (type == wxT("long")) |
341287bf | 2080 | *value = (char) (((wxVariantDataLong*)GetData())->GetValue()); |
862416e0 | 2081 | #ifdef HAVE_BOOL |
223d09f6 | 2082 | else if (type == wxT("bool")) |
341287bf | 2083 | *value = (char) (((wxVariantDataBool*)GetData())->GetValue()); |
862416e0 | 2084 | #endif |
341287bf | 2085 | else |
cab1a605 | 2086 | return false; |
341287bf | 2087 | |
cab1a605 | 2088 | return true; |
341287bf JS |
2089 | } |
2090 | ||
2091 | bool wxVariant::Convert(wxString* value) const | |
2092 | { | |
2093 | *value = MakeString(); | |
cab1a605 | 2094 | return true; |
341287bf JS |
2095 | } |
2096 | ||
e2b87f38 | 2097 | #if wxUSE_DATETIME |
edca7a82 GT |
2098 | bool wxVariant::Convert(wxDateTime* value) const |
2099 | { | |
2100 | wxString type(GetType()); | |
2101 | if (type == wxT("datetime")) | |
9708db20 | 2102 | { |
edca7a82 | 2103 | *value = ((wxVariantDataDateTime*)GetData())->GetValue(); |
cab1a605 | 2104 | return true; |
dc259b79 | 2105 | } |
9708db20 JS |
2106 | // Fallback to string conversion |
2107 | wxString val; | |
9a8a9e99 VZ |
2108 | return Convert(&val) && |
2109 | (value->ParseDateTime(val) || value->ParseDate(val)); | |
edca7a82 | 2110 | } |
b1e343f2 | 2111 | #endif // wxUSE_DATETIME |