1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_VARIANT_H_
13 #define _WX_VARIANT_H_
19 #include "wx/object.h"
20 #include "wx/string.h"
21 #include "wx/arrstr.h"
25 #include "wx/datetime.h"
26 #endif // wxUSE_DATETIME
29 #include "wx/db.h" // will #include sqltypes.h
32 #include "wx/iosfwrap.h"
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.
39 * NB: To prevent addition of extra vtbl pointer to wxVariantData,
40 * we don't multiple-inherit from wxObjectRefData. Instead,
41 * we simply replicate the wxObject ref-counting scheme.
43 * NB: When you construct a wxVariantData, it will have refcount
44 * of one. Refcount will not be further increased when
45 * it is passed to wxVariant. This simulates old common
46 * scenario where wxVariant took ownership of wxVariantData
48 * If you create wxVariantData for other reasons than passing
49 * it to wxVariant, technically you are not required to call
50 * DecRef() before deleting it.
52 * TODO: in order to replace wxPropertyValue, we would need
53 * to consider adding constructors that take pointers to C++ variables,
54 * or removing that functionality from the wxProperty library.
55 * Essentially wxPropertyValue takes on some of the wxValidator functionality
56 * by storing pointers and not just actual values, allowing update of C++ data
57 * to be handled automatically. Perhaps there's another way of doing this without
58 * overloading wxVariant with unnecessary functionality.
61 class WXDLLIMPEXP_BASE wxVariantData
: public wxObject
63 friend class wxVariant
;
66 : wxObject(), m_count(1)
69 // Override these to provide common functionality
70 virtual bool Eq(wxVariantData
& data
) const = 0;
72 #if wxUSE_STD_IOSTREAM
73 virtual bool Write(wxSTD ostream
& str
) const { return false; }
75 virtual bool Write(wxString
& str
) const { return false; }
76 #if wxUSE_STD_IOSTREAM
77 virtual bool Read(wxSTD istream
& str
) { return false; }
79 virtual bool Read(wxString
& str
) { return false; }
80 // What type is it? Return a string name.
81 virtual wxString
GetType() const = 0;
82 // If it based on wxObject return the ClassInfo.
83 virtual wxClassInfo
* GetValueClassInfo() { return NULL
; }
85 void IncRef() { m_count
++; }
92 int GetRefCount() const { return m_count
; }
95 // Protected dtor should make some incompatible code
96 // break more louder. That is, they should do data->DecRef()
97 // instead of delete data.
98 virtual ~wxVariantData() { }
104 DECLARE_ABSTRACT_CLASS(wxVariantData
)
108 * wxVariant can store any kind of data, but has some basic types
112 class WXDLLIMPEXP_BASE wxVariant
: public wxObject
117 wxVariant(const wxVariant
& variant
);
118 wxVariant(wxVariantData
* data
, const wxString
& name
= wxEmptyString
);
119 virtual ~wxVariant();
121 // generic assignment
122 void operator= (const wxVariant
& variant
);
124 // Assignment using data, e.g.
125 // myVariant = new wxStringVariantData("hello");
126 void operator= (wxVariantData
* variantData
);
128 bool operator== (const wxVariant
& variant
) const;
129 bool operator!= (const wxVariant
& variant
) const;
132 inline void SetName(const wxString
& name
) { m_name
= name
; }
133 inline const wxString
& GetName() const { return m_name
; }
135 // Tests whether there is data
138 // For compatibility with wxWidgets <= 2.6, this doesn't increase
140 wxVariantData
* GetData() const { return m_data
; }
141 void SetData(wxVariantData
* data
) ;
143 // make a 'clone' of the object
144 void Ref(const wxVariant
& clone
);
146 // destroy a reference
149 // Make NULL (i.e. delete the data)
152 // Delete data and name
155 // Returns a string representing the type of the variant,
156 // e.g. "string", "bool", "stringlist", "list", "double", "long"
157 wxString
GetType() const;
159 bool IsType(const wxString
& type
) const;
160 bool IsValueKindOf(const wxClassInfo
* type
) const;
162 // write contents to a string (e.g. for debugging)
163 wxString
MakeString() const;
166 wxVariant(double val
, const wxString
& name
= wxEmptyString
);
167 bool operator== (double value
) const;
168 bool operator!= (double value
) const;
169 void operator= (double value
) ;
170 inline operator double () const { return GetDouble(); }
171 inline double GetReal() const { return GetDouble(); }
172 double GetDouble() const;
175 wxVariant(long val
, const wxString
& name
= wxEmptyString
);
176 wxVariant(int val
, const wxString
& name
= wxEmptyString
);
177 wxVariant(short val
, const wxString
& name
= wxEmptyString
);
178 bool operator== (long value
) const;
179 bool operator!= (long value
) const;
180 void operator= (long value
) ;
181 inline operator long () const { return GetLong(); }
182 inline long GetInteger() const { return GetLong(); }
183 long GetLong() const;
187 wxVariant(bool val
, const wxString
& name
= wxEmptyString
);
188 bool operator== (bool value
) const;
189 bool operator!= (bool value
) const;
190 void operator= (bool value
) ;
191 inline operator bool () const { return GetBool(); }
192 bool GetBool() const ;
197 wxVariant(const wxDateTime
& val
, const wxString
& name
= wxEmptyString
);
199 wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
= wxEmptyString
);
200 wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
= wxEmptyString
);
201 wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
= wxEmptyString
);
203 bool operator== (const wxDateTime
& value
) const;
204 bool operator!= (const wxDateTime
& value
) const;
205 void operator= (const wxDateTime
& value
) ;
207 void operator= (const DATE_STRUCT
* value
) ;
208 void operator= (const TIME_STRUCT
* value
) ;
209 void operator= (const TIMESTAMP_STRUCT
* value
) ;
211 inline operator wxDateTime () const { return GetDateTime(); }
212 wxDateTime
GetDateTime() const;
216 wxVariant(const wxString
& val
, const wxString
& name
= wxEmptyString
);
217 wxVariant(const wxChar
* val
, const wxString
& name
= wxEmptyString
); // Necessary or VC++ assumes bool!
218 bool operator== (const wxString
& value
) const;
219 bool operator!= (const wxString
& value
) const;
220 void operator= (const wxString
& value
) ;
221 void operator= (const wxChar
* value
) ; // Necessary or VC++ assumes bool!
222 inline operator wxString () const { return MakeString(); }
223 wxString
GetString() const;
226 wxVariant(wxChar val
, const wxString
& name
= wxEmptyString
);
227 bool operator== (wxChar value
) const;
228 bool operator!= (wxChar value
) const;
229 void operator= (wxChar value
) ;
230 inline operator wxChar () const { return GetChar(); }
231 wxChar
GetChar() const ;
234 wxVariant(const wxArrayString
& val
, const wxString
& name
= wxEmptyString
);
235 bool operator== (const wxArrayString
& value
) const;
236 bool operator!= (const wxArrayString
& value
) const;
237 void operator= (const wxArrayString
& value
);
238 inline operator wxArrayString () const { return GetArrayString(); }
239 wxArrayString
GetArrayString() const;
242 wxVariant(void* ptr
, const wxString
& name
= wxEmptyString
);
243 bool operator== (void* value
) const;
244 bool operator!= (void* value
) const;
245 void operator= (void* value
);
246 inline operator void* () const { return GetVoidPtr(); }
247 void* GetVoidPtr() const;
250 wxVariant(wxObject
* ptr
, const wxString
& name
= wxEmptyString
);
251 bool operator== (wxObject
* value
) const;
252 bool operator!= (wxObject
* value
) const;
253 void operator= (wxObject
* value
);
254 wxObject
* GetWxObjectPtr() const;
257 #if WXWIN_COMPATIBILITY_2_4
258 wxDEPRECATED( wxVariant(const wxStringList
& val
, const wxString
& name
= wxEmptyString
) );
259 wxDEPRECATED( bool operator== (const wxStringList
& value
) const );
260 wxDEPRECATED( bool operator!= (const wxStringList
& value
) const );
261 wxDEPRECATED( void operator= (const wxStringList
& value
) );
262 wxDEPRECATED( wxStringList
& GetStringList() const );
265 // ------------------------------
267 // ------------------------------
269 wxVariant(const wxList
& val
, const wxString
& name
= wxEmptyString
); // List of variants
270 bool operator== (const wxList
& value
) const;
271 bool operator!= (const wxList
& value
) const;
272 void operator= (const wxList
& value
) ;
273 // Treat a list variant as an array
274 wxVariant
operator[] (size_t idx
) const;
275 wxVariant
& operator[] (size_t idx
) ;
276 wxList
& GetList() const ;
278 // Return the number of elements in a list
279 size_t GetCount() const;
285 void Append(const wxVariant
& value
);
287 // Insert at front of list
288 void Insert(const wxVariant
& value
);
290 // Returns true if the variant is a member of the list
291 bool Member(const wxVariant
& value
) const;
293 // Deletes the nth element of the list
294 bool Delete(size_t item
);
301 bool Convert(long* value
) const;
302 bool Convert(bool* value
) const;
303 bool Convert(double* value
) const;
304 bool Convert(wxString
* value
) const;
305 bool Convert(char* value
) const;
307 bool Convert(wxDateTime
* value
) const;
308 #endif // wxUSE_DATETIME
312 wxVariantData
* m_data
;
316 DECLARE_DYNAMIC_CLASS(wxVariant
)
319 //Since we want type safety wxVariant we need to fetch and dynamic_cast
320 //in a seemingly safe way so the compiler can check, so we define
321 //a dynamic_cast /wxDynamicCast analogue.
323 #define wxGetVariantCast(var,classname) \
324 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
325 var.GetWxObjectPtr() : NULL));
327 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
329 #endif // wxUSE_VARIANT
331 #endif // _WX_VARIANT_H_