]> git.saurik.com Git - wxWidgets.git/blame - include/wx/variant.h
update to 2.9.0
[wxWidgets.git] / include / wx / variant.h
CommitLineData
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
3586d10f 62class WXDLLIMPEXP_BASE wxVariantData
8cb50e4b 63{
2562c823 64 friend class wxVariant;
8cb50e4b 65public:
3586d10f 66 wxVariantData() : m_count(1) { }
8cb50e4b 67
2562c823 68 // Override these to provide common functionality
8cb50e4b 69 virtual bool Eq(wxVariantData& data) const = 0;
07502d73 70
38830220 71#if wxUSE_STD_IOSTREAM
07502d73 72 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
38830220 73#endif
07502d73 74 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
38830220 75#if wxUSE_STD_IOSTREAM
07502d73 76 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
38830220 77#endif
07502d73 78 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
8cb50e4b
JS
79 // What type is it? Return a string name.
80 virtual wxString GetType() const = 0;
cf6ae290
RG
81 // If it based on wxObject return the ClassInfo.
82 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
2562c823 83
c8058a09
JS
84 // Implement this to make wxVariant::AllocExcusive work. Returns
85 // a copy of the data.
86 virtual wxVariantData* Clone() const { return NULL; }
87
2562c823
RR
88 void IncRef() { m_count++; }
89 void DecRef()
90 {
91 if ( --m_count == 0 )
92 delete this;
93 }
94
95 int GetRefCount() const { return m_count; }
96
97protected:
98 // Protected dtor should make some incompatible code
99 // break more louder. That is, they should do data->DecRef()
100 // instead of delete data.
101 virtual ~wxVariantData() { }
102
103private:
104 int m_count;
8cb50e4b
JS
105};
106
107/*
108 * wxVariant can store any kind of data, but has some basic types
109 * built in.
8cb50e4b 110 */
c8058a09 111
7e6b4780
RR
112class WXDLLIMPEXP_FWD_BASE wxVariant;
113
114WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE);
8cb50e4b 115
bddd7a8d 116class WXDLLIMPEXP_BASE wxVariant: public wxObject
8cb50e4b 117{
8cb50e4b 118public:
8cb50e4b 119 wxVariant();
07502d73 120
8cb50e4b 121 wxVariant(const wxVariant& variant);
2562c823 122 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
d3c7fc99 123 virtual ~wxVariant();
8cb50e4b 124
2562c823 125 // generic assignment
8cb50e4b
JS
126 void operator= (const wxVariant& variant);
127
128 // Assignment using data, e.g.
129 // myVariant = new wxStringVariantData("hello");
130 void operator= (wxVariantData* variantData);
07502d73 131
8cb50e4b
JS
132 bool operator== (const wxVariant& variant) const;
133 bool operator!= (const wxVariant& variant) const;
134
2562c823
RR
135 // Sets/gets name
136 inline void SetName(const wxString& name) { m_name = name; }
137 inline const wxString& GetName() const { return m_name; }
138
139 // Tests whether there is data
07502d73 140 bool IsNull() const;
2562c823
RR
141
142 // For compatibility with wxWidgets <= 2.6, this doesn't increase
143 // reference count.
144 wxVariantData* GetData() const { return m_data; }
145 void SetData(wxVariantData* data) ;
146
147 // make a 'clone' of the object
148 void Ref(const wxVariant& clone);
149
150 // destroy a reference
151 void UnRef();
152
c8058a09
JS
153 // ensure that the data is exclusive to this variant, and not shared
154 bool Unshare();
155
2562c823
RR
156 // Make NULL (i.e. delete the data)
157 void MakeNull();
07502d73 158
2562c823
RR
159 // Delete data and name
160 void Clear();
07502d73 161
2562c823
RR
162 // Returns a string representing the type of the variant,
163 // e.g. "string", "bool", "stringlist", "list", "double", "long"
164 wxString GetType() const;
165
166 bool IsType(const wxString& type) const;
167 bool IsValueKindOf(const wxClassInfo* type) const;
168
07502d73 169 // write contents to a string (e.g. for debugging)
2562c823 170 wxString MakeString() const;
07502d73 171
2562c823
RR
172 // double
173 wxVariant(double val, const wxString& name = wxEmptyString);
8cb50e4b
JS
174 bool operator== (double value) const;
175 bool operator!= (double value) const;
176 void operator= (double value) ;
2562c823
RR
177 inline operator double () const { return GetDouble(); }
178 inline double GetReal() const { return GetDouble(); }
179 double GetDouble() const;
07502d73 180
2562c823
RR
181 // long
182 wxVariant(long val, const wxString& name = wxEmptyString);
183 wxVariant(int val, const wxString& name = wxEmptyString);
184 wxVariant(short val, const wxString& name = wxEmptyString);
8cb50e4b
JS
185 bool operator== (long value) const;
186 bool operator!= (long value) const;
187 void operator= (long value) ;
2562c823
RR
188 inline operator long () const { return GetLong(); }
189 inline long GetInteger() const { return GetLong(); }
190 long GetLong() const;
07502d73
WS
191
192 // bool
57493f9f 193#ifdef HAVE_BOOL
2562c823 194 wxVariant(bool val, const wxString& name = wxEmptyString);
8cb50e4b
JS
195 bool operator== (bool value) const;
196 bool operator!= (bool value) const;
197 void operator= (bool value) ;
2562c823
RR
198 inline operator bool () const { return GetBool(); }
199 bool GetBool() const ;
200#endif
201
202 // wxDateTime
203#if wxUSE_DATETIME
07502d73 204 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
2562c823
RR
205#if wxUSE_ODBC
206 wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString);
207 wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString);
208 wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString);
209#endif
210 bool operator== (const wxDateTime& value) const;
211 bool operator!= (const wxDateTime& value) const;
212 void operator= (const wxDateTime& value) ;
213#if wxUSE_ODBC
214 void operator= (const DATE_STRUCT* value) ;
215 void operator= (const TIME_STRUCT* value) ;
216 void operator= (const TIMESTAMP_STRUCT* value) ;
217#endif
218 inline operator wxDateTime () const { return GetDateTime(); }
219 wxDateTime GetDateTime() const;
57493f9f 220#endif
2562c823
RR
221
222 // wxString
223 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
af717fa8
VS
224 // these overloads are necessary to prevent the compiler from using bool
225 // version instead of wxString one:
226 wxVariant(const char* val, const wxString& name = wxEmptyString);
227 wxVariant(const wchar_t* val, const wxString& name = wxEmptyString);
228 wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString);
229 wxVariant(const wxCharBuffer& val, const wxString& name = wxEmptyString);
230 wxVariant(const wxWCharBuffer& val, const wxString& name = wxEmptyString);
231
8cb50e4b
JS
232 bool operator== (const wxString& value) const;
233 bool operator!= (const wxString& value) const;
af717fa8
VS
234 wxVariant& operator=(const wxString& value);
235 // these overloads are necessary to prevent the compiler from using bool
236 // version instead of wxString one:
237 wxVariant& operator=(const char* value)
238 { return *this = wxString(value); }
239 wxVariant& operator=(const wchar_t* value)
240 { return *this = wxString(value); }
241 wxVariant& operator=(const wxCStrData& value)
242 { return *this = value.AsString(); }
243 template<typename T>
244 wxVariant& operator=(const wxCharTypeBuffer<T>& value)
245 { return *this = value.data(); }
246
2562c823
RR
247 inline operator wxString () const { return MakeString(); }
248 wxString GetString() const;
249
af717fa8
VS
250 // wxUniChar
251 wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString);
252 wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString);
253 wxVariant(char val, const wxString& name = wxEmptyString);
254 wxVariant(wchar_t val, const wxString& name = wxEmptyString);
255 bool operator==(const wxUniChar& value) const;
256 bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); }
257 bool operator==(char value) const { return *this == wxUniChar(value); }
258 bool operator==(wchar_t value) const { return *this == wxUniChar(value); }
259 bool operator!=(const wxUniChar& value) const { return !(*this == value); }
260 bool operator!=(const wxUniCharRef& value) const { return !(*this == value); }
261 bool operator!=(char value) const { return !(*this == value); }
262 bool operator!=(wchar_t value) const { return !(*this == value); }
263 wxVariant& operator=(const wxUniChar& value);
264 wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); }
265 wxVariant& operator=(char value) { return *this = wxUniChar(value); }
266 wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); }
267 operator wxUniChar() const { return GetChar(); }
268 operator char() const { return GetChar(); }
269 operator wchar_t() const { return GetChar(); }
270 wxUniChar GetChar() const;
07502d73 271
2562c823
RR
272 // wxArrayString
273 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
274 bool operator== (const wxArrayString& value) const;
275 bool operator!= (const wxArrayString& value) const;
276 void operator= (const wxArrayString& value);
af717fa8 277 operator wxArrayString () const { return GetArrayString(); }
2562c823
RR
278 wxArrayString GetArrayString() const;
279
280 // void*
281 wxVariant(void* ptr, const wxString& name = wxEmptyString);
282 bool operator== (void* value) const;
283 bool operator!= (void* value) const;
284 void operator= (void* value);
af717fa8 285 operator void* () const { return GetVoidPtr(); }
2562c823
RR
286 void* GetVoidPtr() const;
287
288 // wxObject*
289 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString);
290 bool operator== (wxObject* value) const;
291 bool operator!= (wxObject* value) const;
292 void operator= (wxObject* value);
293 wxObject* GetWxObjectPtr() const;
294
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 330public:
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
8cb50e4b
JS
342
343// Attributes
344protected:
345 wxVariantData* m_data;
a0a302dc 346 wxString m_name;
07502d73 347
2562c823
RR
348private:
349 DECLARE_DYNAMIC_CLASS(wxVariant)
8cb50e4b
JS
350};
351
3f90a399 352#define DECLARE_VARIANT_OBJECT(classname) \
bde626ce 353 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
07502d73 354
6f5d7825 355#define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
39a48551
VZ
356expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
357expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
3f90a399
RR
358
359#define IMPLEMENT_VARIANT_OBJECT(classname) \
bde626ce 360 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
6f5d7825 361
55ccdb93 362#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
3f90a399
RR
363class classname##VariantData: public wxVariantData \
364{ \
365public:\
366 classname##VariantData() {} \
367 classname##VariantData( const classname &value ) { m_value = value; } \
368\
369 classname &GetValue() { return m_value; } \
370\
371 virtual bool Eq(wxVariantData& data) const; \
372\
373 virtual wxString GetType() const; \
374 virtual wxClassInfo* GetValueClassInfo(); \
c8058a09
JS
375\
376 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
3f90a399
RR
377\
378protected:\
379 classname m_value; \
3f90a399
RR
380};\
381\
3f90a399
RR
382wxString classname##VariantData::GetType() const\
383{\
384 return m_value.GetClassInfo()->GetClassName();\
385}\
386\
387wxClassInfo* classname##VariantData::GetValueClassInfo()\
388{\
389 return m_value.GetClassInfo();\
390}\
391\
39a48551 392expdecl classname& operator << ( classname &value, const wxVariant &variant )\
3f90a399 393{\
3586d10f 394 wxASSERT( variant.GetType() == #classname );\
3f90a399
RR
395 \
396 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
397 value = data->GetValue();\
398 return value;\
399}\
400\
39a48551 401expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
3f90a399
RR
402{\
403 classname##VariantData *data = new classname##VariantData( value );\
404 variant.SetData( data );\
405 return variant;\
406}
407
01df01eb
VZ
408// implements a wxVariantData-derived class using for the Eq() method the
409// operator== which must have been provided by "classname"
55ccdb93 410#define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
5ffa72f4 411IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
55ccdb93
VZ
412\
413bool classname##VariantData::Eq(wxVariantData& data) const \
414{\
3586d10f 415 wxASSERT( GetType() == data.GetType() );\
55ccdb93
VZ
416\
417 classname##VariantData & otherData = (classname##VariantData &) data;\
418\
419 return otherData.m_value == m_value;\
420}\
421
422
423// implements a wxVariantData-derived class using for the Eq() method a shallow
a3ab1c18 424// comparison (through wxObject::IsSameAs function)
01df01eb
VZ
425#define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
426 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
55ccdb93 427#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
5ffa72f4 428IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
55ccdb93
VZ
429\
430bool classname##VariantData::Eq(wxVariantData& data) const \
431{\
3586d10f 432 wxASSERT( GetType() == data.GetType() );\
55ccdb93
VZ
433\
434 classname##VariantData & otherData = (classname##VariantData &) data;\
435\
a3ab1c18 436 return (otherData.m_value.IsSameAs(m_value));\
55ccdb93
VZ
437}\
438
439
3f90a399
RR
440// Since we want type safety wxVariant we need to fetch and dynamic_cast
441// in a seemingly safe way so the compiler can check, so we define
442// a dynamic_cast /wxDynamicCast analogue.
cf6ae290
RG
443
444#define wxGetVariantCast(var,classname) \
cab1a605
WS
445 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
446 var.GetWxObjectPtr() : NULL));
cf6ae290 447
c8058a09
JS
448// Replacement for using wxDynamicCast on a wxVariantData object
449#define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
450
bddd7a8d 451extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
a0a302dc 452
d5dc103f
VZ
453#endif // wxUSE_VARIANT
454
455#endif // _WX_VARIANT_H_