]>
Commit | Line | Data |
---|---|---|
8cb50e4b | 1 | ///////////////////////////////////////////////////////////////////////////// |
43f06cfd | 2 | // Name: wx/variant.h |
8cb50e4b 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$ | |
99d80019 | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
8cb50e4b JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_VARIANT_H_ | |
13 | #define _WX_VARIANT_H_ | |
14 | ||
8cb50e4b | 15 | #include "wx/defs.h" |
d5dc103f VZ |
16 | |
17 | #if wxUSE_VARIANT | |
18 | ||
8cb50e4b JS |
19 | #include "wx/object.h" |
20 | #include "wx/string.h" | |
4c3ebca9 | 21 | #include "wx/arrstr.h" |
8cb50e4b | 22 | #include "wx/list.h" |
bde626ce | 23 | #include "wx/cpp.h" |
4e00b908 | 24 | #include "wx/longlong.h" |
8cb50e4b | 25 | |
e2b87f38 VZ |
26 | #if wxUSE_DATETIME |
27 | #include "wx/datetime.h" | |
28 | #endif // wxUSE_DATETIME | |
edca7a82 | 29 | |
65f19af1 | 30 | #include "wx/iosfwrap.h" |
8cb50e4b | 31 | |
0bf14ab8 JS |
32 | class wxAny; |
33 | ||
8cb50e4b JS |
34 | /* |
35 | * wxVariantData stores the actual data in a wxVariant object, | |
36 | * to allow it to store any type of data. | |
37 | * Derive from this to provide custom data handling. | |
38 | * | |
2562c823 RR |
39 | * NB: When you construct a wxVariantData, it will have refcount |
40 | * of one. Refcount will not be further increased when | |
41 | * it is passed to wxVariant. This simulates old common | |
42 | * scenario where wxVariant took ownership of wxVariantData | |
43 | * passed to it. | |
44 | * If you create wxVariantData for other reasons than passing | |
45 | * it to wxVariant, technically you are not required to call | |
46 | * DecRef() before deleting it. | |
47 | * | |
8cb50e4b JS |
48 | * TODO: in order to replace wxPropertyValue, we would need |
49 | * to consider adding constructors that take pointers to C++ variables, | |
50 | * or removing that functionality from the wxProperty library. | |
51 | * Essentially wxPropertyValue takes on some of the wxValidator functionality | |
52 | * by storing pointers and not just actual values, allowing update of C++ data | |
53 | * to be handled automatically. Perhaps there's another way of doing this without | |
54 | * overloading wxVariant with unnecessary functionality. | |
55 | */ | |
56 | ||
cf25a599 | 57 | class WXDLLIMPEXP_BASE wxVariantData : public wxObjectRefData |
8cb50e4b | 58 | { |
2562c823 | 59 | friend class wxVariant; |
8cb50e4b | 60 | public: |
92ffc98a | 61 | wxVariantData() { } |
8cb50e4b | 62 | |
2562c823 | 63 | // Override these to provide common functionality |
8cb50e4b | 64 | virtual bool Eq(wxVariantData& data) const = 0; |
07502d73 | 65 | |
38830220 | 66 | #if wxUSE_STD_IOSTREAM |
07502d73 | 67 | virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; } |
38830220 | 68 | #endif |
07502d73 | 69 | virtual bool Write(wxString& WXUNUSED(str)) const { return false; } |
38830220 | 70 | #if wxUSE_STD_IOSTREAM |
07502d73 | 71 | virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; } |
38830220 | 72 | #endif |
07502d73 | 73 | virtual bool Read(wxString& WXUNUSED(str)) { return false; } |
8cb50e4b JS |
74 | // What type is it? Return a string name. |
75 | virtual wxString GetType() const = 0; | |
cf6ae290 RG |
76 | // If it based on wxObject return the ClassInfo. |
77 | virtual wxClassInfo* GetValueClassInfo() { return NULL; } | |
2562c823 | 78 | |
cf25a599 | 79 | // Implement this to make wxVariant::UnShare work. Returns |
c8058a09 JS |
80 | // a copy of the data. |
81 | virtual wxVariantData* Clone() const { return NULL; } | |
82 | ||
0bf14ab8 JS |
83 | #if wxUSE_ANY |
84 | // Converts value to wxAny, if possible. Return true if successful. | |
85 | virtual bool GetAsAny(wxAny* WXUNUSED(any)) const { return false; } | |
86 | #endif | |
87 | ||
2562c823 RR |
88 | protected: |
89 | // Protected dtor should make some incompatible code | |
90 | // break more louder. That is, they should do data->DecRef() | |
91 | // instead of delete data. | |
92 | virtual ~wxVariantData() { } | |
8cb50e4b JS |
93 | }; |
94 | ||
95 | /* | |
96 | * wxVariant can store any kind of data, but has some basic types | |
97 | * built in. | |
8cb50e4b | 98 | */ |
c8058a09 | 99 | |
7e6b4780 RR |
100 | class WXDLLIMPEXP_FWD_BASE wxVariant; |
101 | ||
102 | WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE); | |
8cb50e4b | 103 | |
bddd7a8d | 104 | class WXDLLIMPEXP_BASE wxVariant: public wxObject |
8cb50e4b | 105 | { |
8cb50e4b | 106 | public: |
8cb50e4b | 107 | wxVariant(); |
07502d73 | 108 | |
8cb50e4b | 109 | wxVariant(const wxVariant& variant); |
2562c823 | 110 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); |
0bf14ab8 JS |
111 | #if wxUSE_ANY |
112 | wxVariant(const wxAny& any); | |
113 | #endif | |
d3c7fc99 | 114 | virtual ~wxVariant(); |
8cb50e4b | 115 | |
2562c823 | 116 | // generic assignment |
8cb50e4b JS |
117 | void operator= (const wxVariant& variant); |
118 | ||
119 | // Assignment using data, e.g. | |
120 | // myVariant = new wxStringVariantData("hello"); | |
121 | void operator= (wxVariantData* variantData); | |
07502d73 | 122 | |
8cb50e4b JS |
123 | bool operator== (const wxVariant& variant) const; |
124 | bool operator!= (const wxVariant& variant) const; | |
125 | ||
2562c823 RR |
126 | // Sets/gets name |
127 | inline void SetName(const wxString& name) { m_name = name; } | |
128 | inline const wxString& GetName() const { return m_name; } | |
129 | ||
130 | // Tests whether there is data | |
07502d73 | 131 | bool IsNull() const; |
2562c823 RR |
132 | |
133 | // For compatibility with wxWidgets <= 2.6, this doesn't increase | |
134 | // reference count. | |
cf25a599 JS |
135 | wxVariantData* GetData() const |
136 | { | |
137 | return (wxVariantData*) m_refData; | |
138 | } | |
2562c823 RR |
139 | void SetData(wxVariantData* data) ; |
140 | ||
141 | // make a 'clone' of the object | |
cf25a599 | 142 | void Ref(const wxVariant& clone) { wxObject::Ref(clone); } |
2562c823 | 143 | |
c8058a09 JS |
144 | // ensure that the data is exclusive to this variant, and not shared |
145 | bool Unshare(); | |
146 | ||
2562c823 RR |
147 | // Make NULL (i.e. delete the data) |
148 | void MakeNull(); | |
07502d73 | 149 | |
2562c823 RR |
150 | // Delete data and name |
151 | void Clear(); | |
07502d73 | 152 | |
2562c823 RR |
153 | // Returns a string representing the type of the variant, |
154 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
155 | wxString GetType() const; | |
156 | ||
157 | bool IsType(const wxString& type) const; | |
158 | bool IsValueKindOf(const wxClassInfo* type) const; | |
159 | ||
07502d73 | 160 | // write contents to a string (e.g. for debugging) |
2562c823 | 161 | wxString MakeString() const; |
07502d73 | 162 | |
0bf14ab8 JS |
163 | #if wxUSE_ANY |
164 | wxAny GetAny() const; | |
165 | #endif | |
166 | ||
2562c823 RR |
167 | // double |
168 | wxVariant(double val, const wxString& name = wxEmptyString); | |
8cb50e4b JS |
169 | bool operator== (double value) const; |
170 | bool operator!= (double value) const; | |
171 | void operator= (double value) ; | |
2562c823 RR |
172 | inline operator double () const { return GetDouble(); } |
173 | inline double GetReal() const { return GetDouble(); } | |
174 | double GetDouble() const; | |
07502d73 | 175 | |
2562c823 RR |
176 | // long |
177 | wxVariant(long val, const wxString& name = wxEmptyString); | |
178 | wxVariant(int val, const wxString& name = wxEmptyString); | |
179 | wxVariant(short val, const wxString& name = wxEmptyString); | |
8cb50e4b JS |
180 | bool operator== (long value) const; |
181 | bool operator!= (long value) const; | |
182 | void operator= (long value) ; | |
2562c823 RR |
183 | inline operator long () const { return GetLong(); } |
184 | inline long GetInteger() const { return GetLong(); } | |
185 | long GetLong() const; | |
07502d73 WS |
186 | |
187 | // bool | |
2562c823 | 188 | wxVariant(bool val, const wxString& name = wxEmptyString); |
8cb50e4b JS |
189 | bool operator== (bool value) const; |
190 | bool operator!= (bool value) const; | |
191 | void operator= (bool value) ; | |
2562c823 RR |
192 | inline operator bool () const { return GetBool(); } |
193 | bool GetBool() const ; | |
2562c823 RR |
194 | |
195 | // wxDateTime | |
196 | #if wxUSE_DATETIME | |
07502d73 | 197 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); |
2562c823 RR |
198 | bool operator== (const wxDateTime& value) const; |
199 | bool operator!= (const wxDateTime& value) const; | |
200 | void operator= (const wxDateTime& value) ; | |
2562c823 RR |
201 | inline operator wxDateTime () const { return GetDateTime(); } |
202 | wxDateTime GetDateTime() const; | |
57493f9f | 203 | #endif |
2562c823 RR |
204 | |
205 | // wxString | |
206 | wxVariant(const wxString& val, const wxString& name = wxEmptyString); | |
af717fa8 VS |
207 | // these overloads are necessary to prevent the compiler from using bool |
208 | // version instead of wxString one: | |
209 | wxVariant(const char* val, const wxString& name = wxEmptyString); | |
210 | wxVariant(const wchar_t* val, const wxString& name = wxEmptyString); | |
211 | wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString); | |
de4983f3 VS |
212 | wxVariant(const wxScopedCharBuffer& val, const wxString& name = wxEmptyString); |
213 | wxVariant(const wxScopedWCharBuffer& val, const wxString& name = wxEmptyString); | |
af717fa8 | 214 | |
8cb50e4b JS |
215 | bool operator== (const wxString& value) const; |
216 | bool operator!= (const wxString& value) const; | |
af717fa8 VS |
217 | wxVariant& operator=(const wxString& value); |
218 | // these overloads are necessary to prevent the compiler from using bool | |
219 | // version instead of wxString one: | |
220 | wxVariant& operator=(const char* value) | |
221 | { return *this = wxString(value); } | |
222 | wxVariant& operator=(const wchar_t* value) | |
223 | { return *this = wxString(value); } | |
224 | wxVariant& operator=(const wxCStrData& value) | |
225 | { return *this = value.AsString(); } | |
226 | template<typename T> | |
de4983f3 | 227 | wxVariant& operator=(const wxScopedCharTypeBuffer<T>& value) |
af717fa8 VS |
228 | { return *this = value.data(); } |
229 | ||
2562c823 RR |
230 | inline operator wxString () const { return MakeString(); } |
231 | wxString GetString() const; | |
232 | ||
af717fa8 VS |
233 | // wxUniChar |
234 | wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString); | |
235 | wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString); | |
236 | wxVariant(char val, const wxString& name = wxEmptyString); | |
237 | wxVariant(wchar_t val, const wxString& name = wxEmptyString); | |
238 | bool operator==(const wxUniChar& value) const; | |
239 | bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); } | |
240 | bool operator==(char value) const { return *this == wxUniChar(value); } | |
241 | bool operator==(wchar_t value) const { return *this == wxUniChar(value); } | |
242 | bool operator!=(const wxUniChar& value) const { return !(*this == value); } | |
243 | bool operator!=(const wxUniCharRef& value) const { return !(*this == value); } | |
244 | bool operator!=(char value) const { return !(*this == value); } | |
245 | bool operator!=(wchar_t value) const { return !(*this == value); } | |
246 | wxVariant& operator=(const wxUniChar& value); | |
247 | wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); } | |
248 | wxVariant& operator=(char value) { return *this = wxUniChar(value); } | |
249 | wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); } | |
250 | operator wxUniChar() const { return GetChar(); } | |
251 | operator char() const { return GetChar(); } | |
252 | operator wchar_t() const { return GetChar(); } | |
253 | wxUniChar GetChar() const; | |
07502d73 | 254 | |
2562c823 RR |
255 | // wxArrayString |
256 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); | |
257 | bool operator== (const wxArrayString& value) const; | |
258 | bool operator!= (const wxArrayString& value) const; | |
259 | void operator= (const wxArrayString& value); | |
af717fa8 | 260 | operator wxArrayString () const { return GetArrayString(); } |
2562c823 RR |
261 | wxArrayString GetArrayString() const; |
262 | ||
263 | // void* | |
264 | wxVariant(void* ptr, const wxString& name = wxEmptyString); | |
265 | bool operator== (void* value) const; | |
266 | bool operator!= (void* value) const; | |
267 | void operator= (void* value); | |
af717fa8 | 268 | operator void* () const { return GetVoidPtr(); } |
2562c823 RR |
269 | void* GetVoidPtr() const; |
270 | ||
271 | // wxObject* | |
272 | wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); | |
273 | bool operator== (wxObject* value) const; | |
274 | bool operator!= (wxObject* value) const; | |
275 | void operator= (wxObject* value); | |
276 | wxObject* GetWxObjectPtr() const; | |
277 | ||
4e00b908 JS |
278 | #if wxUSE_LONGLONG |
279 | // wxLongLong | |
280 | wxVariant(wxLongLong, const wxString& name = wxEmptyString); | |
281 | bool operator==(wxLongLong value) const; | |
282 | bool operator!=(wxLongLong value) const; | |
283 | void operator=(wxLongLong value); | |
284 | operator wxLongLong() const { return GetLongLong(); } | |
285 | wxLongLong GetLongLong() const; | |
286 | ||
287 | // wxULongLong | |
288 | wxVariant(wxULongLong, const wxString& name = wxEmptyString); | |
289 | bool operator==(wxULongLong value) const; | |
290 | bool operator!=(wxULongLong value) const; | |
291 | void operator=(wxULongLong value); | |
292 | operator wxULongLong() const { return GetULongLong(); } | |
293 | wxULongLong GetULongLong() const; | |
294 | #endif | |
2562c823 | 295 | |
2562c823 RR |
296 | // ------------------------------ |
297 | // list operations | |
298 | // ------------------------------ | |
299 | ||
9a0a58f5 RR |
300 | wxVariant(const wxVariantList& val, const wxString& name = wxEmptyString); // List of variants |
301 | bool operator== (const wxVariantList& value) const; | |
302 | bool operator!= (const wxVariantList& value) const; | |
303 | void operator= (const wxVariantList& value) ; | |
8cb50e4b JS |
304 | // Treat a list variant as an array |
305 | wxVariant operator[] (size_t idx) const; | |
306 | wxVariant& operator[] (size_t idx) ; | |
9a0a58f5 | 307 | wxVariantList& GetList() const ; |
8cb50e4b JS |
308 | |
309 | // Return the number of elements in a list | |
43f06cfd | 310 | size_t GetCount() const; |
07502d73 | 311 | |
8cb50e4b JS |
312 | // Make empty list |
313 | void NullList(); | |
314 | ||
315 | // Append to list | |
316 | void Append(const wxVariant& value); | |
317 | ||
318 | // Insert at front of list | |
319 | void Insert(const wxVariant& value); | |
320 | ||
cab1a605 | 321 | // Returns true if the variant is a member of the list |
8cb50e4b JS |
322 | bool Member(const wxVariant& value) const; |
323 | ||
324 | // Deletes the nth element of the list | |
43f06cfd | 325 | bool Delete(size_t item); |
8cb50e4b JS |
326 | |
327 | // Clear list | |
328 | void ClearList(); | |
329 | ||
9708db20 | 330 | public: |
2562c823 | 331 | // Type conversion |
8cb50e4b JS |
332 | bool Convert(long* value) const; |
333 | bool Convert(bool* value) const; | |
334 | bool Convert(double* value) const; | |
335 | bool Convert(wxString* value) const; | |
af717fa8 VS |
336 | bool Convert(wxUniChar* value) const; |
337 | bool Convert(char* value) const; | |
338 | bool Convert(wchar_t* value) const; | |
e2b87f38 | 339 | #if wxUSE_DATETIME |
edca7a82 | 340 | bool Convert(wxDateTime* value) const; |
e2b87f38 | 341 | #endif // wxUSE_DATETIME |
4e00b908 JS |
342 | #if wxUSE_LONGLONG |
343 | bool Convert(wxLongLong* value) const; | |
344 | bool Convert(wxULongLong* value) const; | |
345 | #endif // wxUSE_LONGLONG | |
8cb50e4b JS |
346 | |
347 | // Attributes | |
348 | protected: | |
cf25a599 JS |
349 | virtual wxObjectRefData *CreateRefData() const; |
350 | virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; | |
351 | ||
a0a302dc | 352 | wxString m_name; |
07502d73 | 353 | |
2562c823 RR |
354 | private: |
355 | DECLARE_DYNAMIC_CLASS(wxVariant) | |
8cb50e4b JS |
356 | }; |
357 | ||
0bf14ab8 JS |
358 | |
359 | // | |
360 | // wxVariant <-> wxAny conversion code | |
361 | // | |
362 | #if wxUSE_ANY | |
363 | ||
364 | #include "wx/any.h" | |
365 | ||
366 | // In order to convert wxAny to wxVariant, we need to be able to associate | |
367 | // wxAnyValueType with a wxVariantData factory function. | |
368 | typedef wxVariantData* (*wxVariantDataFactory)(const wxAny& any); | |
369 | ||
370 | // Actual Any-to-Variant registration must be postponed to a time when all | |
371 | // global variables have been initialized. Hence this arrangement. | |
372 | // wxAnyToVariantRegistration instances are kept in global scope and | |
373 | // wxAnyValueTypeGlobals in any.cpp will use their data when the time is | |
374 | // right. | |
375 | class WXDLLIMPEXP_BASE wxAnyToVariantRegistration | |
376 | { | |
377 | public: | |
378 | wxAnyToVariantRegistration(wxVariantDataFactory factory); | |
2a227e8c | 379 | virtual ~wxAnyToVariantRegistration(); |
0bf14ab8 JS |
380 | |
381 | virtual wxAnyValueType* GetAssociatedType() = 0; | |
382 | wxVariantDataFactory GetFactory() const { return m_factory; } | |
383 | private: | |
384 | wxVariantDataFactory m_factory; | |
385 | }; | |
386 | ||
387 | template<typename T> | |
388 | class wxAnyToVariantRegistrationImpl : public wxAnyToVariantRegistration | |
389 | { | |
390 | public: | |
391 | wxAnyToVariantRegistrationImpl(wxVariantDataFactory factory) | |
392 | : wxAnyToVariantRegistration(factory) | |
393 | { | |
394 | } | |
395 | ||
396 | virtual wxAnyValueType* GetAssociatedType() | |
397 | { | |
398 | return wxAnyValueTypeImpl<T>::GetInstance(); | |
399 | } | |
400 | private: | |
401 | }; | |
402 | ||
403 | #define DECLARE_WXANY_CONVERSION() \ | |
404 | virtual bool GetAsAny(wxAny* any) const; \ | |
405 | static wxVariantData* VariantDataFactory(const wxAny& any); | |
406 | ||
153107b4 | 407 | #define _REGISTER_WXANY_CONVERSION(T, CLASSNAME, FUNC) \ |
0bf14ab8 JS |
408 | static wxAnyToVariantRegistrationImpl<T> \ |
409 | gs_##CLASSNAME##AnyToVariantRegistration = \ | |
153107b4 JS |
410 | wxAnyToVariantRegistrationImpl<T>(&FUNC); |
411 | ||
412 | #define REGISTER_WXANY_CONVERSION(T, CLASSNAME) \ | |
413 | _REGISTER_WXANY_CONVERSION(T, CLASSNAME, CLASSNAME::VariantDataFactory) | |
0bf14ab8 JS |
414 | |
415 | #define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME) \ | |
416 | bool CLASSNAME::GetAsAny(wxAny* any) const \ | |
417 | { \ | |
418 | *any = m_value; \ | |
419 | return true; \ | |
420 | } \ | |
421 | wxVariantData* CLASSNAME::VariantDataFactory(const wxAny& any) \ | |
422 | { \ | |
423 | return new CLASSNAME(wxANY_AS(any, T)); \ | |
424 | } \ | |
425 | REGISTER_WXANY_CONVERSION(T, CLASSNAME) | |
426 | ||
427 | // This is needed for wxVariantList conversion | |
428 | WX_DECLARE_LIST_WITH_DECL(wxAny, wxAnyList, class WXDLLIMPEXP_BASE); | |
429 | ||
430 | #else // if !wxUSE_ANY | |
431 | ||
432 | #define DECLARE_WXANY_CONVERSION() | |
433 | #define REGISTER_WXANY_CONVERSION(T, CLASSNAME) | |
434 | #define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME) | |
435 | ||
436 | #endif // wxUSE_ANY/!wxUSE_ANY | |
437 | ||
438 | ||
3f90a399 | 439 | #define DECLARE_VARIANT_OBJECT(classname) \ |
bde626ce | 440 | DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) |
07502d73 | 441 | |
6f5d7825 | 442 | #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \ |
39a48551 VZ |
443 | expdecl classname& operator << ( classname &object, const wxVariant &variant ); \ |
444 | expdecl wxVariant& operator << ( wxVariant &variant, const classname &object ); | |
3f90a399 RR |
445 | |
446 | #define IMPLEMENT_VARIANT_OBJECT(classname) \ | |
bde626ce | 447 | IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) |
6f5d7825 | 448 | |
55ccdb93 | 449 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \ |
3f90a399 RR |
450 | class classname##VariantData: public wxVariantData \ |
451 | { \ | |
452 | public:\ | |
453 | classname##VariantData() {} \ | |
454 | classname##VariantData( const classname &value ) { m_value = value; } \ | |
455 | \ | |
456 | classname &GetValue() { return m_value; } \ | |
457 | \ | |
458 | virtual bool Eq(wxVariantData& data) const; \ | |
459 | \ | |
460 | virtual wxString GetType() const; \ | |
461 | virtual wxClassInfo* GetValueClassInfo(); \ | |
c8058a09 JS |
462 | \ |
463 | virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \ | |
3f90a399 | 464 | \ |
0bf14ab8 | 465 | DECLARE_WXANY_CONVERSION() \ |
3f90a399 RR |
466 | protected:\ |
467 | classname m_value; \ | |
3f90a399 RR |
468 | };\ |
469 | \ | |
3f90a399 RR |
470 | wxString classname##VariantData::GetType() const\ |
471 | {\ | |
472 | return m_value.GetClassInfo()->GetClassName();\ | |
473 | }\ | |
474 | \ | |
475 | wxClassInfo* classname##VariantData::GetValueClassInfo()\ | |
476 | {\ | |
477 | return m_value.GetClassInfo();\ | |
478 | }\ | |
479 | \ | |
39a48551 | 480 | expdecl classname& operator << ( classname &value, const wxVariant &variant )\ |
3f90a399 | 481 | {\ |
3586d10f | 482 | wxASSERT( variant.GetType() == #classname );\ |
3f90a399 RR |
483 | \ |
484 | classname##VariantData *data = (classname##VariantData*) variant.GetData();\ | |
485 | value = data->GetValue();\ | |
486 | return value;\ | |
487 | }\ | |
488 | \ | |
39a48551 | 489 | expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\ |
3f90a399 RR |
490 | {\ |
491 | classname##VariantData *data = new classname##VariantData( value );\ | |
492 | variant.SetData( data );\ | |
493 | return variant;\ | |
0bf14ab8 JS |
494 | } \ |
495 | IMPLEMENT_TRIVIAL_WXANY_CONVERSION(classname, classname##VariantData) | |
3f90a399 | 496 | |
01df01eb VZ |
497 | // implements a wxVariantData-derived class using for the Eq() method the |
498 | // operator== which must have been provided by "classname" | |
55ccdb93 | 499 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \ |
5ffa72f4 | 500 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \ |
55ccdb93 VZ |
501 | \ |
502 | bool classname##VariantData::Eq(wxVariantData& data) const \ | |
503 | {\ | |
3586d10f | 504 | wxASSERT( GetType() == data.GetType() );\ |
55ccdb93 VZ |
505 | \ |
506 | classname##VariantData & otherData = (classname##VariantData &) data;\ | |
507 | \ | |
508 | return otherData.m_value == m_value;\ | |
509 | }\ | |
510 | ||
511 | ||
512 | // implements a wxVariantData-derived class using for the Eq() method a shallow | |
a3ab1c18 | 513 | // comparison (through wxObject::IsSameAs function) |
01df01eb VZ |
514 | #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \ |
515 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE) | |
55ccdb93 | 516 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \ |
5ffa72f4 | 517 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \ |
55ccdb93 VZ |
518 | \ |
519 | bool classname##VariantData::Eq(wxVariantData& data) const \ | |
520 | {\ | |
3586d10f | 521 | wxASSERT( GetType() == data.GetType() );\ |
55ccdb93 VZ |
522 | \ |
523 | classname##VariantData & otherData = (classname##VariantData &) data;\ | |
524 | \ | |
a3ab1c18 | 525 | return (otherData.m_value.IsSameAs(m_value));\ |
55ccdb93 VZ |
526 | }\ |
527 | ||
528 | ||
3f90a399 RR |
529 | // Since we want type safety wxVariant we need to fetch and dynamic_cast |
530 | // in a seemingly safe way so the compiler can check, so we define | |
531 | // a dynamic_cast /wxDynamicCast analogue. | |
cf6ae290 RG |
532 | |
533 | #define wxGetVariantCast(var,classname) \ | |
cab1a605 WS |
534 | ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\ |
535 | var.GetWxObjectPtr() : NULL)); | |
cf6ae290 | 536 | |
c8058a09 JS |
537 | // Replacement for using wxDynamicCast on a wxVariantData object |
538 | #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data) | |
539 | ||
bddd7a8d | 540 | extern wxVariant WXDLLIMPEXP_BASE wxNullVariant; |
a0a302dc | 541 | |
d5dc103f VZ |
542 | #endif // wxUSE_VARIANT |
543 | ||
544 | #endif // _WX_VARIANT_H_ |