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"
26 #include "wx/datetime.h"
27 #endif // wxUSE_DATETIME
30 #include "wx/db.h" // will #include sqltypes.h
33 #include "wx/iosfwrap.h"
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.
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.
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
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.
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.
62 class WXDLLIMPEXP_BASE wxVariantData
: public wxObject
64 friend class wxVariant
;
67 : wxObject(), m_count(1)
70 // Override these to provide common functionality
71 virtual bool Eq(wxVariantData
& data
) const = 0;
73 #if wxUSE_STD_IOSTREAM
74 virtual bool Write(wxSTD ostream
& WXUNUSED(str
)) const { return false; }
76 virtual bool Write(wxString
& WXUNUSED(str
)) const { return false; }
77 #if wxUSE_STD_IOSTREAM
78 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
80 virtual bool Read(wxString
& WXUNUSED(str
)) { return false; }
81 // What type is it? Return a string name.
82 virtual wxString
GetType() const = 0;
83 // If it based on wxObject return the ClassInfo.
84 virtual wxClassInfo
* GetValueClassInfo() { return NULL
; }
86 void IncRef() { m_count
++; }
93 int GetRefCount() const { return m_count
; }
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() { }
105 DECLARE_ABSTRACT_CLASS(wxVariantData
)
109 * wxVariant can store any kind of data, but has some basic types
113 class WXDLLIMPEXP_BASE wxVariant
: public wxObject
118 wxVariant(const wxVariant
& variant
);
119 wxVariant(wxVariantData
* data
, const wxString
& name
= wxEmptyString
);
120 virtual ~wxVariant();
122 // generic assignment
123 void operator= (const wxVariant
& variant
);
125 // Assignment using data, e.g.
126 // myVariant = new wxStringVariantData("hello");
127 void operator= (wxVariantData
* variantData
);
129 bool operator== (const wxVariant
& variant
) const;
130 bool operator!= (const wxVariant
& variant
) const;
133 inline void SetName(const wxString
& name
) { m_name
= name
; }
134 inline const wxString
& GetName() const { return m_name
; }
136 // Tests whether there is data
139 // For compatibility with wxWidgets <= 2.6, this doesn't increase
141 wxVariantData
* GetData() const { return m_data
; }
142 void SetData(wxVariantData
* data
) ;
144 // make a 'clone' of the object
145 void Ref(const wxVariant
& clone
);
147 // destroy a reference
150 // Make NULL (i.e. delete the data)
153 // Delete data and name
156 // Returns a string representing the type of the variant,
157 // e.g. "string", "bool", "stringlist", "list", "double", "long"
158 wxString
GetType() const;
160 bool IsType(const wxString
& type
) const;
161 bool IsValueKindOf(const wxClassInfo
* type
) const;
163 // write contents to a string (e.g. for debugging)
164 wxString
MakeString() const;
167 wxVariant(double val
, const wxString
& name
= wxEmptyString
);
168 bool operator== (double value
) const;
169 bool operator!= (double value
) const;
170 void operator= (double value
) ;
171 inline operator double () const { return GetDouble(); }
172 inline double GetReal() const { return GetDouble(); }
173 double GetDouble() const;
176 wxVariant(long val
, const wxString
& name
= wxEmptyString
);
177 wxVariant(int val
, const wxString
& name
= wxEmptyString
);
178 wxVariant(short val
, const wxString
& name
= wxEmptyString
);
179 bool operator== (long value
) const;
180 bool operator!= (long value
) const;
181 void operator= (long value
) ;
182 inline operator long () const { return GetLong(); }
183 inline long GetInteger() const { return GetLong(); }
184 long GetLong() const;
188 wxVariant(bool val
, const wxString
& name
= wxEmptyString
);
189 bool operator== (bool value
) const;
190 bool operator!= (bool value
) const;
191 void operator= (bool value
) ;
192 inline operator bool () const { return GetBool(); }
193 bool GetBool() const ;
198 wxVariant(const wxDateTime
& val
, const wxString
& name
= wxEmptyString
);
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
);
204 bool operator== (const wxDateTime
& value
) const;
205 bool operator!= (const wxDateTime
& value
) const;
206 void operator= (const wxDateTime
& value
) ;
208 void operator= (const DATE_STRUCT
* value
) ;
209 void operator= (const TIME_STRUCT
* value
) ;
210 void operator= (const TIMESTAMP_STRUCT
* value
) ;
212 inline operator wxDateTime () const { return GetDateTime(); }
213 wxDateTime
GetDateTime() const;
217 wxVariant(const wxString
& val
, const wxString
& name
= wxEmptyString
);
218 wxVariant(const wxChar
* val
, const wxString
& name
= wxEmptyString
); // Necessary or VC++ assumes bool!
219 bool operator== (const wxString
& value
) const;
220 bool operator!= (const wxString
& value
) const;
221 void operator= (const wxString
& value
) ;
222 void operator= (const wxChar
* value
) ; // Necessary or VC++ assumes bool!
223 inline operator wxString () const { return MakeString(); }
224 wxString
GetString() const;
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 ;
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;
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;
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;
258 #if WXWIN_COMPATIBILITY_2_4
259 wxDEPRECATED( wxVariant(const wxStringList
& val
, const wxString
& name
= wxEmptyString
) );
260 wxDEPRECATED( bool operator== (const wxStringList
& value
) const );
261 wxDEPRECATED( bool operator!= (const wxStringList
& value
) const );
262 wxDEPRECATED( void operator= (const wxStringList
& value
) );
263 wxDEPRECATED( wxStringList
& GetStringList() const );
266 // ------------------------------
268 // ------------------------------
270 wxVariant(const wxList
& val
, const wxString
& name
= wxEmptyString
); // List of variants
271 bool operator== (const wxList
& value
) const;
272 bool operator!= (const wxList
& value
) const;
273 void operator= (const wxList
& value
) ;
274 // Treat a list variant as an array
275 wxVariant
operator[] (size_t idx
) const;
276 wxVariant
& operator[] (size_t idx
) ;
277 wxList
& GetList() const ;
279 // Return the number of elements in a list
280 size_t GetCount() const;
286 void Append(const wxVariant
& value
);
288 // Insert at front of list
289 void Insert(const wxVariant
& value
);
291 // Returns true if the variant is a member of the list
292 bool Member(const wxVariant
& value
) const;
294 // Deletes the nth element of the list
295 bool Delete(size_t item
);
302 bool Convert(long* value
) const;
303 bool Convert(bool* value
) const;
304 bool Convert(double* value
) const;
305 bool Convert(wxString
* value
) const;
306 bool Convert(wxChar
* value
) const;
308 bool Convert(wxDateTime
* value
) const;
309 #endif // wxUSE_DATETIME
313 wxVariantData
* m_data
;
317 DECLARE_DYNAMIC_CLASS(wxVariant
)
320 #define DECLARE_VARIANT_OBJECT(classname) \
321 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
323 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
324 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
325 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
327 #define IMPLEMENT_VARIANT_OBJECT(classname) \
328 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
330 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
331 class classname##VariantData: public wxVariantData \
334 classname##VariantData() {} \
335 classname##VariantData( const classname &value ) { m_value = value; } \
337 classname &GetValue() { return m_value; } \
339 virtual bool Eq(wxVariantData& data) const; \
341 virtual wxString GetType() const; \
342 virtual wxClassInfo* GetValueClassInfo(); \
348 DECLARE_CLASS(classname##VariantData) \
351 IMPLEMENT_CLASS(classname##VariantData, wxVariantData)\
353 wxString classname##VariantData::GetType() const\
355 return m_value.GetClassInfo()->GetClassName();\
358 wxClassInfo* classname##VariantData::GetValueClassInfo()\
360 return m_value.GetClassInfo();\
363 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
365 wxASSERT( wxIsKindOf( variant.GetData(), classname##VariantData ) );\
367 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
368 value = data->GetValue();\
372 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
374 classname##VariantData *data = new classname##VariantData( value );\
375 variant.SetData( data );\
379 // implements a wxVariantData-derived class using for the Eq() method the
380 // operator== which must have been provided by "classname"
381 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
382 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
384 bool classname##VariantData::Eq(wxVariantData& data) const \
386 wxASSERT( wxIsKindOf((&data), classname##VariantData) );\
388 classname##VariantData & otherData = (classname##VariantData &) data;\
390 return otherData.m_value == m_value;\
394 // implements a wxVariantData-derived class using for the Eq() method a shallow
395 // comparison (through wxObject::IsSameAs function)
396 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
397 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
398 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
399 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
401 bool classname##VariantData::Eq(wxVariantData& data) const \
403 wxASSERT( wxIsKindOf((&data), classname##VariantData) );\
405 classname##VariantData & otherData = (classname##VariantData &) data;\
407 return (otherData.m_value.IsSameAs(m_value));\
411 // Since we want type safety wxVariant we need to fetch and dynamic_cast
412 // in a seemingly safe way so the compiler can check, so we define
413 // a dynamic_cast /wxDynamicCast analogue.
415 #define wxGetVariantCast(var,classname) \
416 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
417 var.GetWxObjectPtr() : NULL));
419 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
421 #endif // wxUSE_VARIANT
423 #endif // _WX_VARIANT_H_