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
64 friend class wxVariant
;
66 wxVariantData() : m_count(1) { }
68 // Override these to provide common functionality
69 virtual bool Eq(wxVariantData
& data
) const = 0;
71 #if wxUSE_STD_IOSTREAM
72 virtual bool Write(wxSTD ostream
& WXUNUSED(str
)) const { return false; }
74 virtual bool Write(wxString
& WXUNUSED(str
)) const { return false; }
75 #if wxUSE_STD_IOSTREAM
76 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
78 virtual bool Read(wxString
& WXUNUSED(str
)) { return false; }
79 // What type is it? Return a string name.
80 virtual wxString
GetType() const = 0;
81 // If it based on wxObject return the ClassInfo.
82 virtual wxClassInfo
* GetValueClassInfo() { return NULL
; }
84 // Implement this to make wxVariant::AllocExcusive work. Returns
85 // a copy of the data.
86 virtual wxVariantData
* Clone() const { return NULL
; }
88 void IncRef() { m_count
++; }
95 int GetRefCount() const { return m_count
; }
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() { }
108 * wxVariant can store any kind of data, but has some basic types
112 class WXDLLIMPEXP_FWD_BASE wxVariant
;
114 WX_DECLARE_LIST_WITH_DECL(wxVariant
, wxVariantList
, class WXDLLIMPEXP_BASE
);
116 class WXDLLIMPEXP_BASE wxVariant
: public wxObject
121 wxVariant(const wxVariant
& variant
);
122 wxVariant(wxVariantData
* data
, const wxString
& name
= wxEmptyString
);
123 virtual ~wxVariant();
125 // generic assignment
126 void operator= (const wxVariant
& variant
);
128 // Assignment using data, e.g.
129 // myVariant = new wxStringVariantData("hello");
130 void operator= (wxVariantData
* variantData
);
132 bool operator== (const wxVariant
& variant
) const;
133 bool operator!= (const wxVariant
& variant
) const;
136 inline void SetName(const wxString
& name
) { m_name
= name
; }
137 inline const wxString
& GetName() const { return m_name
; }
139 // Tests whether there is data
142 // For compatibility with wxWidgets <= 2.6, this doesn't increase
144 wxVariantData
* GetData() const { return m_data
; }
145 void SetData(wxVariantData
* data
) ;
147 // make a 'clone' of the object
148 void Ref(const wxVariant
& clone
);
150 // destroy a reference
153 // ensure that the data is exclusive to this variant, and not shared
156 // Make NULL (i.e. delete the data)
159 // Delete data and name
162 // Returns a string representing the type of the variant,
163 // e.g. "string", "bool", "stringlist", "list", "double", "long"
164 wxString
GetType() const;
166 bool IsType(const wxString
& type
) const;
167 bool IsValueKindOf(const wxClassInfo
* type
) const;
169 // write contents to a string (e.g. for debugging)
170 wxString
MakeString() const;
173 wxVariant(double val
, const wxString
& name
= wxEmptyString
);
174 bool operator== (double value
) const;
175 bool operator!= (double value
) const;
176 void operator= (double value
) ;
177 inline operator double () const { return GetDouble(); }
178 inline double GetReal() const { return GetDouble(); }
179 double GetDouble() const;
182 wxVariant(long val
, const wxString
& name
= wxEmptyString
);
183 wxVariant(int val
, const wxString
& name
= wxEmptyString
);
184 wxVariant(short val
, const wxString
& name
= wxEmptyString
);
185 bool operator== (long value
) const;
186 bool operator!= (long value
) const;
187 void operator= (long value
) ;
188 inline operator long () const { return GetLong(); }
189 inline long GetInteger() const { return GetLong(); }
190 long GetLong() const;
194 wxVariant(bool val
, const wxString
& name
= wxEmptyString
);
195 bool operator== (bool value
) const;
196 bool operator!= (bool value
) const;
197 void operator= (bool value
) ;
198 inline operator bool () const { return GetBool(); }
199 bool GetBool() const ;
204 wxVariant(const wxDateTime
& val
, const wxString
& name
= wxEmptyString
);
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
);
210 bool operator== (const wxDateTime
& value
) const;
211 bool operator!= (const wxDateTime
& value
) const;
212 void operator= (const wxDateTime
& value
) ;
214 void operator= (const DATE_STRUCT
* value
) ;
215 void operator= (const TIME_STRUCT
* value
) ;
216 void operator= (const TIMESTAMP_STRUCT
* value
) ;
218 inline operator wxDateTime () const { return GetDateTime(); }
219 wxDateTime
GetDateTime() const;
223 wxVariant(const wxString
& val
, const wxString
& name
= wxEmptyString
);
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
);
232 bool operator== (const wxString
& value
) const;
233 bool operator!= (const wxString
& value
) const;
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(); }
244 wxVariant
& operator=(const wxCharTypeBuffer
<T
>& value
)
245 { return *this = value
.data(); }
247 inline operator wxString () const { return MakeString(); }
248 wxString
GetString() const;
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;
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
);
277 operator wxArrayString () const { return GetArrayString(); }
278 wxArrayString
GetArrayString() const;
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
);
285 operator void* () const { return GetVoidPtr(); }
286 void* GetVoidPtr() const;
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;
296 // ------------------------------
298 // ------------------------------
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
) ;
304 // Treat a list variant as an array
305 wxVariant
operator[] (size_t idx
) const;
306 wxVariant
& operator[] (size_t idx
) ;
307 wxVariantList
& GetList() const ;
309 // Return the number of elements in a list
310 size_t GetCount() const;
316 void Append(const wxVariant
& value
);
318 // Insert at front of list
319 void Insert(const wxVariant
& value
);
321 // Returns true if the variant is a member of the list
322 bool Member(const wxVariant
& value
) const;
324 // Deletes the nth element of the list
325 bool Delete(size_t item
);
332 bool Convert(long* value
) const;
333 bool Convert(bool* value
) const;
334 bool Convert(double* value
) const;
335 bool Convert(wxString
* value
) const;
336 bool Convert(wxUniChar
* value
) const;
337 bool Convert(char* value
) const;
338 bool Convert(wchar_t* value
) const;
340 bool Convert(wxDateTime
* value
) const;
341 #endif // wxUSE_DATETIME
345 wxVariantData
* m_data
;
349 DECLARE_DYNAMIC_CLASS(wxVariant
)
352 #define DECLARE_VARIANT_OBJECT(classname) \
353 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
355 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
356 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
357 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
359 #define IMPLEMENT_VARIANT_OBJECT(classname) \
360 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
362 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
363 class classname##VariantData: public wxVariantData \
366 classname##VariantData() {} \
367 classname##VariantData( const classname &value ) { m_value = value; } \
369 classname &GetValue() { return m_value; } \
371 virtual bool Eq(wxVariantData& data) const; \
373 virtual wxString GetType() const; \
374 virtual wxClassInfo* GetValueClassInfo(); \
376 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
382 wxString classname##VariantData::GetType() const\
384 return m_value.GetClassInfo()->GetClassName();\
387 wxClassInfo* classname##VariantData::GetValueClassInfo()\
389 return m_value.GetClassInfo();\
392 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
394 wxASSERT( variant.GetType() == #classname );\
396 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
397 value = data->GetValue();\
401 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
403 classname##VariantData *data = new classname##VariantData( value );\
404 variant.SetData( data );\
408 // implements a wxVariantData-derived class using for the Eq() method the
409 // operator== which must have been provided by "classname"
410 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
411 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
413 bool classname##VariantData::Eq(wxVariantData& data) const \
415 wxASSERT( GetType() == data.GetType() );\
417 classname##VariantData & otherData = (classname##VariantData &) data;\
419 return otherData.m_value == m_value;\
423 // implements a wxVariantData-derived class using for the Eq() method a shallow
424 // comparison (through wxObject::IsSameAs function)
425 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
426 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
427 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
428 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
430 bool classname##VariantData::Eq(wxVariantData& data) const \
432 wxASSERT( GetType() == data.GetType() );\
434 classname##VariantData & otherData = (classname##VariantData &) data;\
436 return (otherData.m_value.IsSameAs(m_value));\
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.
444 #define wxGetVariantCast(var,classname) \
445 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
446 var.GetWxObjectPtr() : NULL));
448 // Replacement for using wxDynamicCast on a wxVariantData object
449 #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
451 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
453 #endif // wxUSE_VARIANT
455 #endif // _WX_VARIANT_H_