]>
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" |
8cb50e4b | 24 | |
e2b87f38 VZ |
25 | #if wxUSE_DATETIME |
26 | #include "wx/datetime.h" | |
27 | #endif // wxUSE_DATETIME | |
edca7a82 GT |
28 | |
29 | #if wxUSE_ODBC | |
30 | #include "wx/db.h" // will #include sqltypes.h | |
31 | #endif //ODBC | |
32 | ||
65f19af1 | 33 | #include "wx/iosfwrap.h" |
8cb50e4b JS |
34 | |
35 | /* | |
36 | * wxVariantData stores the actual data in a wxVariant object, | |
37 | * to allow it to store any type of data. | |
38 | * Derive from this to provide custom data handling. | |
39 | * | |
2562c823 RR |
40 | * NB: To prevent addition of extra vtbl pointer to wxVariantData, |
41 | * we don't multiple-inherit from wxObjectRefData. Instead, | |
42 | * we simply replicate the wxObject ref-counting scheme. | |
43 | * | |
44 | * NB: When you construct a wxVariantData, it will have refcount | |
45 | * of one. Refcount will not be further increased when | |
46 | * it is passed to wxVariant. This simulates old common | |
47 | * scenario where wxVariant took ownership of wxVariantData | |
48 | * passed to it. | |
49 | * If you create wxVariantData for other reasons than passing | |
50 | * it to wxVariant, technically you are not required to call | |
51 | * DecRef() before deleting it. | |
52 | * | |
8cb50e4b JS |
53 | * TODO: in order to replace wxPropertyValue, we would need |
54 | * to consider adding constructors that take pointers to C++ variables, | |
55 | * or removing that functionality from the wxProperty library. | |
56 | * Essentially wxPropertyValue takes on some of the wxValidator functionality | |
57 | * by storing pointers and not just actual values, allowing update of C++ data | |
58 | * to be handled automatically. Perhaps there's another way of doing this without | |
59 | * overloading wxVariant with unnecessary functionality. | |
60 | */ | |
61 | ||
bddd7a8d | 62 | class WXDLLIMPEXP_BASE wxVariantData: public wxObject |
8cb50e4b | 63 | { |
2562c823 | 64 | friend class wxVariant; |
8cb50e4b | 65 | public: |
2562c823 RR |
66 | wxVariantData() |
67 | : wxObject(), m_count(1) | |
68 | { } | |
8cb50e4b | 69 | |
2562c823 | 70 | // Override these to provide common functionality |
8cb50e4b | 71 | virtual bool Eq(wxVariantData& data) const = 0; |
07502d73 | 72 | |
38830220 | 73 | #if wxUSE_STD_IOSTREAM |
07502d73 | 74 | virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; } |
38830220 | 75 | #endif |
07502d73 | 76 | virtual bool Write(wxString& WXUNUSED(str)) const { return false; } |
38830220 | 77 | #if wxUSE_STD_IOSTREAM |
07502d73 | 78 | virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; } |
38830220 | 79 | #endif |
07502d73 | 80 | virtual bool Read(wxString& WXUNUSED(str)) { return false; } |
8cb50e4b JS |
81 | // What type is it? Return a string name. |
82 | virtual wxString GetType() const = 0; | |
cf6ae290 RG |
83 | // If it based on wxObject return the ClassInfo. |
84 | virtual wxClassInfo* GetValueClassInfo() { return NULL; } | |
2562c823 RR |
85 | |
86 | void IncRef() { m_count++; } | |
87 | void DecRef() | |
88 | { | |
89 | if ( --m_count == 0 ) | |
90 | delete this; | |
91 | } | |
92 | ||
93 | int GetRefCount() const { return m_count; } | |
94 | ||
95 | protected: | |
96 | // Protected dtor should make some incompatible code | |
97 | // break more louder. That is, they should do data->DecRef() | |
98 | // instead of delete data. | |
99 | virtual ~wxVariantData() { } | |
100 | ||
101 | private: | |
102 | int m_count; | |
07502d73 | 103 | |
2562c823 RR |
104 | private: |
105 | DECLARE_ABSTRACT_CLASS(wxVariantData) | |
8cb50e4b JS |
106 | }; |
107 | ||
108 | /* | |
109 | * wxVariant can store any kind of data, but has some basic types | |
110 | * built in. | |
8cb50e4b JS |
111 | */ |
112 | ||
bddd7a8d | 113 | class WXDLLIMPEXP_BASE wxVariant: public wxObject |
8cb50e4b | 114 | { |
8cb50e4b | 115 | public: |
8cb50e4b | 116 | wxVariant(); |
07502d73 | 117 | |
8cb50e4b | 118 | wxVariant(const wxVariant& variant); |
2562c823 | 119 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); |
d3c7fc99 | 120 | virtual ~wxVariant(); |
8cb50e4b | 121 | |
2562c823 | 122 | // generic assignment |
8cb50e4b JS |
123 | void operator= (const wxVariant& variant); |
124 | ||
125 | // Assignment using data, e.g. | |
126 | // myVariant = new wxStringVariantData("hello"); | |
127 | void operator= (wxVariantData* variantData); | |
07502d73 | 128 | |
8cb50e4b JS |
129 | bool operator== (const wxVariant& variant) const; |
130 | bool operator!= (const wxVariant& variant) const; | |
131 | ||
2562c823 RR |
132 | // Sets/gets name |
133 | inline void SetName(const wxString& name) { m_name = name; } | |
134 | inline const wxString& GetName() const { return m_name; } | |
135 | ||
136 | // Tests whether there is data | |
07502d73 | 137 | bool IsNull() const; |
2562c823 RR |
138 | |
139 | // For compatibility with wxWidgets <= 2.6, this doesn't increase | |
140 | // reference count. | |
141 | wxVariantData* GetData() const { return m_data; } | |
142 | void SetData(wxVariantData* data) ; | |
143 | ||
144 | // make a 'clone' of the object | |
145 | void Ref(const wxVariant& clone); | |
146 | ||
147 | // destroy a reference | |
148 | void UnRef(); | |
149 | ||
150 | // Make NULL (i.e. delete the data) | |
151 | void MakeNull(); | |
07502d73 | 152 | |
2562c823 RR |
153 | // Delete data and name |
154 | void Clear(); | |
07502d73 | 155 | |
2562c823 RR |
156 | // Returns a string representing the type of the variant, |
157 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
158 | wxString GetType() const; | |
159 | ||
160 | bool IsType(const wxString& type) const; | |
161 | bool IsValueKindOf(const wxClassInfo* type) const; | |
162 | ||
07502d73 | 163 | // write contents to a string (e.g. for debugging) |
2562c823 | 164 | wxString MakeString() const; |
07502d73 | 165 | |
2562c823 RR |
166 | // double |
167 | wxVariant(double val, const wxString& name = wxEmptyString); | |
8cb50e4b JS |
168 | bool operator== (double value) const; |
169 | bool operator!= (double value) const; | |
170 | void operator= (double value) ; | |
2562c823 RR |
171 | inline operator double () const { return GetDouble(); } |
172 | inline double GetReal() const { return GetDouble(); } | |
173 | double GetDouble() const; | |
07502d73 | 174 | |
2562c823 RR |
175 | // long |
176 | wxVariant(long val, const wxString& name = wxEmptyString); | |
177 | wxVariant(int val, const wxString& name = wxEmptyString); | |
178 | wxVariant(short val, const wxString& name = wxEmptyString); | |
8cb50e4b JS |
179 | bool operator== (long value) const; |
180 | bool operator!= (long value) const; | |
181 | void operator= (long value) ; | |
2562c823 RR |
182 | inline operator long () const { return GetLong(); } |
183 | inline long GetInteger() const { return GetLong(); } | |
184 | long GetLong() const; | |
07502d73 WS |
185 | |
186 | // bool | |
57493f9f | 187 | #ifdef HAVE_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 ; | |
194 | #endif | |
195 | ||
196 | // wxDateTime | |
197 | #if wxUSE_DATETIME | |
07502d73 | 198 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); |
2562c823 RR |
199 | #if wxUSE_ODBC |
200 | wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); | |
201 | wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); | |
202 | wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); | |
203 | #endif | |
204 | bool operator== (const wxDateTime& value) const; | |
205 | bool operator!= (const wxDateTime& value) const; | |
206 | void operator= (const wxDateTime& value) ; | |
207 | #if wxUSE_ODBC | |
208 | void operator= (const DATE_STRUCT* value) ; | |
209 | void operator= (const TIME_STRUCT* value) ; | |
210 | void operator= (const TIMESTAMP_STRUCT* value) ; | |
211 | #endif | |
212 | inline operator wxDateTime () const { return GetDateTime(); } | |
213 | wxDateTime GetDateTime() const; | |
57493f9f | 214 | #endif |
2562c823 RR |
215 | |
216 | // wxString | |
217 | wxVariant(const wxString& val, const wxString& name = wxEmptyString); | |
218 | wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool! | |
8cb50e4b JS |
219 | bool operator== (const wxString& value) const; |
220 | bool operator!= (const wxString& value) const; | |
221 | void operator= (const wxString& value) ; | |
2ed57eb7 | 222 | void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool! |
2562c823 RR |
223 | inline operator wxString () const { return MakeString(); } |
224 | wxString GetString() const; | |
225 | ||
71520754 RR |
226 | // wxChar |
227 | wxVariant(wxChar val, const wxString& name = wxEmptyString); | |
228 | bool operator== (wxChar value) const; | |
229 | bool operator!= (wxChar value) const; | |
230 | void operator= (wxChar value) ; | |
231 | inline operator wxChar () const { return GetChar(); } | |
232 | wxChar GetChar() const ; | |
07502d73 | 233 | |
2562c823 RR |
234 | // wxArrayString |
235 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); | |
236 | bool operator== (const wxArrayString& value) const; | |
237 | bool operator!= (const wxArrayString& value) const; | |
238 | void operator= (const wxArrayString& value); | |
239 | inline operator wxArrayString () const { return GetArrayString(); } | |
240 | wxArrayString GetArrayString() const; | |
241 | ||
242 | // void* | |
243 | wxVariant(void* ptr, const wxString& name = wxEmptyString); | |
244 | bool operator== (void* value) const; | |
245 | bool operator!= (void* value) const; | |
246 | void operator= (void* value); | |
247 | inline operator void* () const { return GetVoidPtr(); } | |
248 | void* GetVoidPtr() const; | |
249 | ||
250 | // wxObject* | |
251 | wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); | |
252 | bool operator== (wxObject* value) const; | |
253 | bool operator!= (wxObject* value) const; | |
254 | void operator= (wxObject* value); | |
255 | wxObject* GetWxObjectPtr() const; | |
256 | ||
257 | ||
2562c823 RR |
258 | // ------------------------------ |
259 | // list operations | |
260 | // ------------------------------ | |
261 | ||
262 | wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants | |
8cb50e4b JS |
263 | bool operator== (const wxList& value) const; |
264 | bool operator!= (const wxList& value) const; | |
265 | void operator= (const wxList& value) ; | |
8cb50e4b JS |
266 | // Treat a list variant as an array |
267 | wxVariant operator[] (size_t idx) const; | |
268 | wxVariant& operator[] (size_t idx) ; | |
2562c823 | 269 | wxList& GetList() const ; |
8cb50e4b JS |
270 | |
271 | // Return the number of elements in a list | |
43f06cfd | 272 | size_t GetCount() const; |
07502d73 | 273 | |
8cb50e4b JS |
274 | // Make empty list |
275 | void NullList(); | |
276 | ||
277 | // Append to list | |
278 | void Append(const wxVariant& value); | |
279 | ||
280 | // Insert at front of list | |
281 | void Insert(const wxVariant& value); | |
282 | ||
cab1a605 | 283 | // Returns true if the variant is a member of the list |
8cb50e4b JS |
284 | bool Member(const wxVariant& value) const; |
285 | ||
286 | // Deletes the nth element of the list | |
43f06cfd | 287 | bool Delete(size_t item); |
8cb50e4b JS |
288 | |
289 | // Clear list | |
290 | void ClearList(); | |
291 | ||
9708db20 | 292 | public: |
2562c823 | 293 | // Type conversion |
8cb50e4b JS |
294 | bool Convert(long* value) const; |
295 | bool Convert(bool* value) const; | |
296 | bool Convert(double* value) const; | |
297 | bool Convert(wxString* value) const; | |
38f82bf6 | 298 | bool Convert(wxChar* value) const; |
e2b87f38 | 299 | #if wxUSE_DATETIME |
edca7a82 | 300 | bool Convert(wxDateTime* value) const; |
e2b87f38 | 301 | #endif // wxUSE_DATETIME |
8cb50e4b JS |
302 | |
303 | // Attributes | |
304 | protected: | |
305 | wxVariantData* m_data; | |
a0a302dc | 306 | wxString m_name; |
07502d73 | 307 | |
2562c823 RR |
308 | private: |
309 | DECLARE_DYNAMIC_CLASS(wxVariant) | |
8cb50e4b JS |
310 | }; |
311 | ||
3f90a399 | 312 | #define DECLARE_VARIANT_OBJECT(classname) \ |
bde626ce | 313 | DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) |
07502d73 | 314 | |
6f5d7825 | 315 | #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \ |
39a48551 VZ |
316 | expdecl classname& operator << ( classname &object, const wxVariant &variant ); \ |
317 | expdecl wxVariant& operator << ( wxVariant &variant, const classname &object ); | |
3f90a399 RR |
318 | |
319 | #define IMPLEMENT_VARIANT_OBJECT(classname) \ | |
bde626ce | 320 | IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) |
6f5d7825 | 321 | |
55ccdb93 | 322 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \ |
3f90a399 RR |
323 | class classname##VariantData: public wxVariantData \ |
324 | { \ | |
325 | public:\ | |
326 | classname##VariantData() {} \ | |
327 | classname##VariantData( const classname &value ) { m_value = value; } \ | |
328 | \ | |
329 | classname &GetValue() { return m_value; } \ | |
330 | \ | |
331 | virtual bool Eq(wxVariantData& data) const; \ | |
332 | \ | |
333 | virtual wxString GetType() const; \ | |
334 | virtual wxClassInfo* GetValueClassInfo(); \ | |
335 | \ | |
336 | protected:\ | |
337 | classname m_value; \ | |
338 | \ | |
339 | private: \ | |
340 | DECLARE_CLASS(classname##VariantData) \ | |
341 | };\ | |
342 | \ | |
343 | IMPLEMENT_CLASS(classname##VariantData, wxVariantData)\ | |
344 | \ | |
3f90a399 RR |
345 | wxString classname##VariantData::GetType() const\ |
346 | {\ | |
347 | return m_value.GetClassInfo()->GetClassName();\ | |
348 | }\ | |
349 | \ | |
350 | wxClassInfo* classname##VariantData::GetValueClassInfo()\ | |
351 | {\ | |
352 | return m_value.GetClassInfo();\ | |
353 | }\ | |
354 | \ | |
39a48551 | 355 | expdecl classname& operator << ( classname &value, const wxVariant &variant )\ |
3f90a399 RR |
356 | {\ |
357 | wxASSERT( wxIsKindOf( variant.GetData(), classname##VariantData ) );\ | |
358 | \ | |
359 | classname##VariantData *data = (classname##VariantData*) variant.GetData();\ | |
360 | value = data->GetValue();\ | |
361 | return value;\ | |
362 | }\ | |
363 | \ | |
39a48551 | 364 | expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\ |
3f90a399 RR |
365 | {\ |
366 | classname##VariantData *data = new classname##VariantData( value );\ | |
367 | variant.SetData( data );\ | |
368 | return variant;\ | |
369 | } | |
370 | ||
01df01eb VZ |
371 | // implements a wxVariantData-derived class using for the Eq() method the |
372 | // operator== which must have been provided by "classname" | |
55ccdb93 | 373 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \ |
5ffa72f4 | 374 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \ |
55ccdb93 VZ |
375 | \ |
376 | bool classname##VariantData::Eq(wxVariantData& data) const \ | |
377 | {\ | |
378 | wxASSERT( wxIsKindOf((&data), classname##VariantData) );\ | |
379 | \ | |
380 | classname##VariantData & otherData = (classname##VariantData &) data;\ | |
381 | \ | |
382 | return otherData.m_value == m_value;\ | |
383 | }\ | |
384 | ||
385 | ||
386 | // implements a wxVariantData-derived class using for the Eq() method a shallow | |
a3ab1c18 | 387 | // comparison (through wxObject::IsSameAs function) |
01df01eb VZ |
388 | #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \ |
389 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE) | |
55ccdb93 | 390 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \ |
5ffa72f4 | 391 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \ |
55ccdb93 VZ |
392 | \ |
393 | bool classname##VariantData::Eq(wxVariantData& data) const \ | |
394 | {\ | |
395 | wxASSERT( wxIsKindOf((&data), classname##VariantData) );\ | |
396 | \ | |
397 | classname##VariantData & otherData = (classname##VariantData &) data;\ | |
398 | \ | |
a3ab1c18 | 399 | return (otherData.m_value.IsSameAs(m_value));\ |
55ccdb93 VZ |
400 | }\ |
401 | ||
402 | ||
3f90a399 RR |
403 | // Since we want type safety wxVariant we need to fetch and dynamic_cast |
404 | // in a seemingly safe way so the compiler can check, so we define | |
405 | // a dynamic_cast /wxDynamicCast analogue. | |
cf6ae290 RG |
406 | |
407 | #define wxGetVariantCast(var,classname) \ | |
cab1a605 WS |
408 | ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\ |
409 | var.GetWxObjectPtr() : NULL)); | |
cf6ae290 | 410 | |
bddd7a8d | 411 | extern wxVariant WXDLLIMPEXP_BASE wxNullVariant; |
a0a302dc | 412 | |
d5dc103f VZ |
413 | #endif // wxUSE_VARIANT |
414 | ||
415 | #endif // _WX_VARIANT_H_ |