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"
24 #include "wx/longlong.h"
27 #include "wx/datetime.h"
28 #endif // wxUSE_DATETIME
30 #include "wx/iosfwrap.h"
33 * wxVariantData stores the actual data in a wxVariant object,
34 * to allow it to store any type of data.
35 * Derive from this to provide custom data handling.
37 * NB: When you construct a wxVariantData, it will have refcount
38 * of one. Refcount will not be further increased when
39 * it is passed to wxVariant. This simulates old common
40 * scenario where wxVariant took ownership of wxVariantData
42 * If you create wxVariantData for other reasons than passing
43 * it to wxVariant, technically you are not required to call
44 * DecRef() before deleting it.
46 * TODO: in order to replace wxPropertyValue, we would need
47 * to consider adding constructors that take pointers to C++ variables,
48 * or removing that functionality from the wxProperty library.
49 * Essentially wxPropertyValue takes on some of the wxValidator functionality
50 * by storing pointers and not just actual values, allowing update of C++ data
51 * to be handled automatically. Perhaps there's another way of doing this without
52 * overloading wxVariant with unnecessary functionality.
55 class WXDLLIMPEXP_BASE wxVariantData
: public wxObjectRefData
57 friend class wxVariant
;
61 // Override these to provide common functionality
62 virtual bool Eq(wxVariantData
& data
) const = 0;
64 #if wxUSE_STD_IOSTREAM
65 virtual bool Write(wxSTD ostream
& WXUNUSED(str
)) const { return false; }
67 virtual bool Write(wxString
& WXUNUSED(str
)) const { return false; }
68 #if wxUSE_STD_IOSTREAM
69 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
71 virtual bool Read(wxString
& WXUNUSED(str
)) { return false; }
72 // What type is it? Return a string name.
73 virtual wxString
GetType() const = 0;
74 // If it based on wxObject return the ClassInfo.
75 virtual wxClassInfo
* GetValueClassInfo() { return NULL
; }
77 // Implement this to make wxVariant::UnShare work. Returns
78 // a copy of the data.
79 virtual wxVariantData
* Clone() const { return NULL
; }
82 // Protected dtor should make some incompatible code
83 // break more louder. That is, they should do data->DecRef()
84 // instead of delete data.
85 virtual ~wxVariantData() { }
89 * wxVariant can store any kind of data, but has some basic types
93 class WXDLLIMPEXP_FWD_BASE wxVariant
;
95 WX_DECLARE_LIST_WITH_DECL(wxVariant
, wxVariantList
, class WXDLLIMPEXP_BASE
);
97 class WXDLLIMPEXP_BASE wxVariant
: public wxObject
102 wxVariant(const wxVariant
& variant
);
103 wxVariant(wxVariantData
* data
, const wxString
& name
= wxEmptyString
);
104 virtual ~wxVariant();
106 // generic assignment
107 void operator= (const wxVariant
& variant
);
109 // Assignment using data, e.g.
110 // myVariant = new wxStringVariantData("hello");
111 void operator= (wxVariantData
* variantData
);
113 bool operator== (const wxVariant
& variant
) const;
114 bool operator!= (const wxVariant
& variant
) const;
117 inline void SetName(const wxString
& name
) { m_name
= name
; }
118 inline const wxString
& GetName() const { return m_name
; }
120 // Tests whether there is data
123 // For compatibility with wxWidgets <= 2.6, this doesn't increase
125 wxVariantData
* GetData() const
127 return (wxVariantData
*) m_refData
;
129 void SetData(wxVariantData
* data
) ;
131 // make a 'clone' of the object
132 void Ref(const wxVariant
& clone
) { wxObject::Ref(clone
); }
134 // ensure that the data is exclusive to this variant, and not shared
137 // Make NULL (i.e. delete the data)
140 // Delete data and name
143 // Returns a string representing the type of the variant,
144 // e.g. "string", "bool", "stringlist", "list", "double", "long"
145 wxString
GetType() const;
147 bool IsType(const wxString
& type
) const;
148 bool IsValueKindOf(const wxClassInfo
* type
) const;
150 // write contents to a string (e.g. for debugging)
151 wxString
MakeString() const;
154 wxVariant(double val
, const wxString
& name
= wxEmptyString
);
155 bool operator== (double value
) const;
156 bool operator!= (double value
) const;
157 void operator= (double value
) ;
158 inline operator double () const { return GetDouble(); }
159 inline double GetReal() const { return GetDouble(); }
160 double GetDouble() const;
163 wxVariant(long val
, const wxString
& name
= wxEmptyString
);
164 wxVariant(int val
, const wxString
& name
= wxEmptyString
);
165 wxVariant(short val
, const wxString
& name
= wxEmptyString
);
166 bool operator== (long value
) const;
167 bool operator!= (long value
) const;
168 void operator= (long value
) ;
169 inline operator long () const { return GetLong(); }
170 inline long GetInteger() const { return GetLong(); }
171 long GetLong() const;
174 wxVariant(bool val
, const wxString
& name
= wxEmptyString
);
175 bool operator== (bool value
) const;
176 bool operator!= (bool value
) const;
177 void operator= (bool value
) ;
178 inline operator bool () const { return GetBool(); }
179 bool GetBool() const ;
183 wxVariant(const wxDateTime
& val
, const wxString
& name
= wxEmptyString
);
184 bool operator== (const wxDateTime
& value
) const;
185 bool operator!= (const wxDateTime
& value
) const;
186 void operator= (const wxDateTime
& value
) ;
187 inline operator wxDateTime () const { return GetDateTime(); }
188 wxDateTime
GetDateTime() const;
192 wxVariant(const wxString
& val
, const wxString
& name
= wxEmptyString
);
193 // these overloads are necessary to prevent the compiler from using bool
194 // version instead of wxString one:
195 wxVariant(const char* val
, const wxString
& name
= wxEmptyString
);
196 wxVariant(const wchar_t* val
, const wxString
& name
= wxEmptyString
);
197 wxVariant(const wxCStrData
& val
, const wxString
& name
= wxEmptyString
);
198 wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
= wxEmptyString
);
199 wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
= wxEmptyString
);
201 bool operator== (const wxString
& value
) const;
202 bool operator!= (const wxString
& value
) const;
203 wxVariant
& operator=(const wxString
& value
);
204 // these overloads are necessary to prevent the compiler from using bool
205 // version instead of wxString one:
206 wxVariant
& operator=(const char* value
)
207 { return *this = wxString(value
); }
208 wxVariant
& operator=(const wchar_t* value
)
209 { return *this = wxString(value
); }
210 wxVariant
& operator=(const wxCStrData
& value
)
211 { return *this = value
.AsString(); }
213 wxVariant
& operator=(const wxScopedCharTypeBuffer
<T
>& value
)
214 { return *this = value
.data(); }
216 inline operator wxString () const { return MakeString(); }
217 wxString
GetString() const;
220 wxVariant(const wxUniChar
& val
, const wxString
& name
= wxEmptyString
);
221 wxVariant(const wxUniCharRef
& val
, const wxString
& name
= wxEmptyString
);
222 wxVariant(char val
, const wxString
& name
= wxEmptyString
);
223 wxVariant(wchar_t val
, const wxString
& name
= wxEmptyString
);
224 bool operator==(const wxUniChar
& value
) const;
225 bool operator==(const wxUniCharRef
& value
) const { return *this == wxUniChar(value
); }
226 bool operator==(char value
) const { return *this == wxUniChar(value
); }
227 bool operator==(wchar_t value
) const { return *this == wxUniChar(value
); }
228 bool operator!=(const wxUniChar
& value
) const { return !(*this == value
); }
229 bool operator!=(const wxUniCharRef
& value
) const { return !(*this == value
); }
230 bool operator!=(char value
) const { return !(*this == value
); }
231 bool operator!=(wchar_t value
) const { return !(*this == value
); }
232 wxVariant
& operator=(const wxUniChar
& value
);
233 wxVariant
& operator=(const wxUniCharRef
& value
) { return *this = wxUniChar(value
); }
234 wxVariant
& operator=(char value
) { return *this = wxUniChar(value
); }
235 wxVariant
& operator=(wchar_t value
) { return *this = wxUniChar(value
); }
236 operator wxUniChar() const { return GetChar(); }
237 operator char() const { return GetChar(); }
238 operator wchar_t() const { return GetChar(); }
239 wxUniChar
GetChar() const;
242 wxVariant(const wxArrayString
& val
, const wxString
& name
= wxEmptyString
);
243 bool operator== (const wxArrayString
& value
) const;
244 bool operator!= (const wxArrayString
& value
) const;
245 void operator= (const wxArrayString
& value
);
246 operator wxArrayString () const { return GetArrayString(); }
247 wxArrayString
GetArrayString() const;
250 wxVariant(void* ptr
, const wxString
& name
= wxEmptyString
);
251 bool operator== (void* value
) const;
252 bool operator!= (void* value
) const;
253 void operator= (void* value
);
254 operator void* () const { return GetVoidPtr(); }
255 void* GetVoidPtr() const;
258 wxVariant(wxObject
* ptr
, const wxString
& name
= wxEmptyString
);
259 bool operator== (wxObject
* value
) const;
260 bool operator!= (wxObject
* value
) const;
261 void operator= (wxObject
* value
);
262 wxObject
* GetWxObjectPtr() const;
266 wxVariant(wxLongLong
, const wxString
& name
= wxEmptyString
);
267 bool operator==(wxLongLong value
) const;
268 bool operator!=(wxLongLong value
) const;
269 void operator=(wxLongLong value
);
270 operator wxLongLong() const { return GetLongLong(); }
271 wxLongLong
GetLongLong() const;
274 wxVariant(wxULongLong
, const wxString
& name
= wxEmptyString
);
275 bool operator==(wxULongLong value
) const;
276 bool operator!=(wxULongLong value
) const;
277 void operator=(wxULongLong value
);
278 operator wxULongLong() const { return GetULongLong(); }
279 wxULongLong
GetULongLong() const;
282 // ------------------------------
284 // ------------------------------
286 wxVariant(const wxVariantList
& val
, const wxString
& name
= wxEmptyString
); // List of variants
287 bool operator== (const wxVariantList
& value
) const;
288 bool operator!= (const wxVariantList
& value
) const;
289 void operator= (const wxVariantList
& value
) ;
290 // Treat a list variant as an array
291 wxVariant
operator[] (size_t idx
) const;
292 wxVariant
& operator[] (size_t idx
) ;
293 wxVariantList
& GetList() const ;
295 // Return the number of elements in a list
296 size_t GetCount() const;
302 void Append(const wxVariant
& value
);
304 // Insert at front of list
305 void Insert(const wxVariant
& value
);
307 // Returns true if the variant is a member of the list
308 bool Member(const wxVariant
& value
) const;
310 // Deletes the nth element of the list
311 bool Delete(size_t item
);
318 bool Convert(long* value
) const;
319 bool Convert(bool* value
) const;
320 bool Convert(double* value
) const;
321 bool Convert(wxString
* value
) const;
322 bool Convert(wxUniChar
* value
) const;
323 bool Convert(char* value
) const;
324 bool Convert(wchar_t* value
) const;
326 bool Convert(wxDateTime
* value
) const;
327 #endif // wxUSE_DATETIME
329 bool Convert(wxLongLong
* value
) const;
330 bool Convert(wxULongLong
* value
) const;
331 #endif // wxUSE_LONGLONG
335 virtual wxObjectRefData
*CreateRefData() const;
336 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
341 DECLARE_DYNAMIC_CLASS(wxVariant
)
344 #define DECLARE_VARIANT_OBJECT(classname) \
345 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
347 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
348 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
349 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
351 #define IMPLEMENT_VARIANT_OBJECT(classname) \
352 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
354 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
355 class classname##VariantData: public wxVariantData \
358 classname##VariantData() {} \
359 classname##VariantData( const classname &value ) { m_value = value; } \
361 classname &GetValue() { return m_value; } \
363 virtual bool Eq(wxVariantData& data) const; \
365 virtual wxString GetType() const; \
366 virtual wxClassInfo* GetValueClassInfo(); \
368 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
374 wxString classname##VariantData::GetType() const\
376 return m_value.GetClassInfo()->GetClassName();\
379 wxClassInfo* classname##VariantData::GetValueClassInfo()\
381 return m_value.GetClassInfo();\
384 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
386 wxASSERT( variant.GetType() == #classname );\
388 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
389 value = data->GetValue();\
393 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
395 classname##VariantData *data = new classname##VariantData( value );\
396 variant.SetData( data );\
400 // implements a wxVariantData-derived class using for the Eq() method the
401 // operator== which must have been provided by "classname"
402 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
403 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
405 bool classname##VariantData::Eq(wxVariantData& data) const \
407 wxASSERT( GetType() == data.GetType() );\
409 classname##VariantData & otherData = (classname##VariantData &) data;\
411 return otherData.m_value == m_value;\
415 // implements a wxVariantData-derived class using for the Eq() method a shallow
416 // comparison (through wxObject::IsSameAs function)
417 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
418 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
419 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
420 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
422 bool classname##VariantData::Eq(wxVariantData& data) const \
424 wxASSERT( GetType() == data.GetType() );\
426 classname##VariantData & otherData = (classname##VariantData &) data;\
428 return (otherData.m_value.IsSameAs(m_value));\
432 // Since we want type safety wxVariant we need to fetch and dynamic_cast
433 // in a seemingly safe way so the compiler can check, so we define
434 // a dynamic_cast /wxDynamicCast analogue.
436 #define wxGetVariantCast(var,classname) \
437 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
438 var.GetWxObjectPtr() : NULL));
440 // Replacement for using wxDynamicCast on a wxVariantData object
441 #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
443 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
445 #endif // wxUSE_VARIANT
447 #endif // _WX_VARIANT_H_