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
29 #include "wx/iosfwrap.h"
32 * wxVariantData stores the actual data in a wxVariant object,
33 * to allow it to store any type of data.
34 * Derive from this to provide custom data handling.
36 * NB: When you construct a wxVariantData, it will have refcount
37 * of one. Refcount will not be further increased when
38 * it is passed to wxVariant. This simulates old common
39 * scenario where wxVariant took ownership of wxVariantData
41 * If you create wxVariantData for other reasons than passing
42 * it to wxVariant, technically you are not required to call
43 * DecRef() before deleting it.
45 * TODO: in order to replace wxPropertyValue, we would need
46 * to consider adding constructors that take pointers to C++ variables,
47 * or removing that functionality from the wxProperty library.
48 * Essentially wxPropertyValue takes on some of the wxValidator functionality
49 * by storing pointers and not just actual values, allowing update of C++ data
50 * to be handled automatically. Perhaps there's another way of doing this without
51 * overloading wxVariant with unnecessary functionality.
54 class WXDLLIMPEXP_BASE wxVariantData
: public wxObjectRefData
56 friend class wxVariant
;
60 // Override these to provide common functionality
61 virtual bool Eq(wxVariantData
& data
) const = 0;
63 #if wxUSE_STD_IOSTREAM
64 virtual bool Write(wxSTD ostream
& WXUNUSED(str
)) const { return false; }
66 virtual bool Write(wxString
& WXUNUSED(str
)) const { return false; }
67 #if wxUSE_STD_IOSTREAM
68 virtual bool Read(wxSTD istream
& WXUNUSED(str
)) { return false; }
70 virtual bool Read(wxString
& WXUNUSED(str
)) { return false; }
71 // What type is it? Return a string name.
72 virtual wxString
GetType() const = 0;
73 // If it based on wxObject return the ClassInfo.
74 virtual wxClassInfo
* GetValueClassInfo() { return NULL
; }
76 // Implement this to make wxVariant::UnShare work. Returns
77 // a copy of the data.
78 virtual wxVariantData
* Clone() const { return NULL
; }
81 // Protected dtor should make some incompatible code
82 // break more louder. That is, they should do data->DecRef()
83 // instead of delete data.
84 virtual ~wxVariantData() { }
88 * wxVariant can store any kind of data, but has some basic types
92 class WXDLLIMPEXP_FWD_BASE wxVariant
;
94 WX_DECLARE_LIST_WITH_DECL(wxVariant
, wxVariantList
, class WXDLLIMPEXP_BASE
);
96 class WXDLLIMPEXP_BASE wxVariant
: public wxObject
101 wxVariant(const wxVariant
& variant
);
102 wxVariant(wxVariantData
* data
, const wxString
& name
= wxEmptyString
);
103 virtual ~wxVariant();
105 // generic assignment
106 void operator= (const wxVariant
& variant
);
108 // Assignment using data, e.g.
109 // myVariant = new wxStringVariantData("hello");
110 void operator= (wxVariantData
* variantData
);
112 bool operator== (const wxVariant
& variant
) const;
113 bool operator!= (const wxVariant
& variant
) const;
116 inline void SetName(const wxString
& name
) { m_name
= name
; }
117 inline const wxString
& GetName() const { return m_name
; }
119 // Tests whether there is data
122 // For compatibility with wxWidgets <= 2.6, this doesn't increase
124 wxVariantData
* GetData() const
126 return (wxVariantData
*) m_refData
;
128 void SetData(wxVariantData
* data
) ;
130 // make a 'clone' of the object
131 void Ref(const wxVariant
& clone
) { wxObject::Ref(clone
); }
133 // ensure that the data is exclusive to this variant, and not shared
136 // Make NULL (i.e. delete the data)
139 // Delete data and name
142 // Returns a string representing the type of the variant,
143 // e.g. "string", "bool", "stringlist", "list", "double", "long"
144 wxString
GetType() const;
146 bool IsType(const wxString
& type
) const;
147 bool IsValueKindOf(const wxClassInfo
* type
) const;
149 // write contents to a string (e.g. for debugging)
150 wxString
MakeString() const;
153 wxVariant(double val
, const wxString
& name
= wxEmptyString
);
154 bool operator== (double value
) const;
155 bool operator!= (double value
) const;
156 void operator= (double value
) ;
157 inline operator double () const { return GetDouble(); }
158 inline double GetReal() const { return GetDouble(); }
159 double GetDouble() const;
162 wxVariant(long val
, const wxString
& name
= wxEmptyString
);
163 wxVariant(int val
, const wxString
& name
= wxEmptyString
);
164 wxVariant(short val
, const wxString
& name
= wxEmptyString
);
165 bool operator== (long value
) const;
166 bool operator!= (long value
) const;
167 void operator= (long value
) ;
168 inline operator long () const { return GetLong(); }
169 inline long GetInteger() const { return GetLong(); }
170 long GetLong() const;
173 wxVariant(bool val
, const wxString
& name
= wxEmptyString
);
174 bool operator== (bool value
) const;
175 bool operator!= (bool value
) const;
176 void operator= (bool value
) ;
177 inline operator bool () const { return GetBool(); }
178 bool GetBool() const ;
182 wxVariant(const wxDateTime
& val
, const wxString
& name
= wxEmptyString
);
183 bool operator== (const wxDateTime
& value
) const;
184 bool operator!= (const wxDateTime
& value
) const;
185 void operator= (const wxDateTime
& value
) ;
186 inline operator wxDateTime () const { return GetDateTime(); }
187 wxDateTime
GetDateTime() const;
191 wxVariant(const wxString
& val
, const wxString
& name
= wxEmptyString
);
192 // these overloads are necessary to prevent the compiler from using bool
193 // version instead of wxString one:
194 wxVariant(const char* val
, const wxString
& name
= wxEmptyString
);
195 wxVariant(const wchar_t* val
, const wxString
& name
= wxEmptyString
);
196 wxVariant(const wxCStrData
& val
, const wxString
& name
= wxEmptyString
);
197 wxVariant(const wxScopedCharBuffer
& val
, const wxString
& name
= wxEmptyString
);
198 wxVariant(const wxScopedWCharBuffer
& val
, const wxString
& name
= wxEmptyString
);
200 bool operator== (const wxString
& value
) const;
201 bool operator!= (const wxString
& value
) const;
202 wxVariant
& operator=(const wxString
& value
);
203 // these overloads are necessary to prevent the compiler from using bool
204 // version instead of wxString one:
205 wxVariant
& operator=(const char* value
)
206 { return *this = wxString(value
); }
207 wxVariant
& operator=(const wchar_t* value
)
208 { return *this = wxString(value
); }
209 wxVariant
& operator=(const wxCStrData
& value
)
210 { return *this = value
.AsString(); }
212 wxVariant
& operator=(const wxScopedCharTypeBuffer
<T
>& value
)
213 { return *this = value
.data(); }
215 inline operator wxString () const { return MakeString(); }
216 wxString
GetString() const;
219 wxVariant(const wxUniChar
& val
, const wxString
& name
= wxEmptyString
);
220 wxVariant(const wxUniCharRef
& val
, const wxString
& name
= wxEmptyString
);
221 wxVariant(char val
, const wxString
& name
= wxEmptyString
);
222 wxVariant(wchar_t val
, const wxString
& name
= wxEmptyString
);
223 bool operator==(const wxUniChar
& value
) const;
224 bool operator==(const wxUniCharRef
& value
) const { return *this == wxUniChar(value
); }
225 bool operator==(char value
) const { return *this == wxUniChar(value
); }
226 bool operator==(wchar_t value
) const { return *this == wxUniChar(value
); }
227 bool operator!=(const wxUniChar
& value
) const { return !(*this == value
); }
228 bool operator!=(const wxUniCharRef
& value
) const { return !(*this == value
); }
229 bool operator!=(char value
) const { return !(*this == value
); }
230 bool operator!=(wchar_t value
) const { return !(*this == value
); }
231 wxVariant
& operator=(const wxUniChar
& value
);
232 wxVariant
& operator=(const wxUniCharRef
& value
) { return *this = wxUniChar(value
); }
233 wxVariant
& operator=(char value
) { return *this = wxUniChar(value
); }
234 wxVariant
& operator=(wchar_t value
) { return *this = wxUniChar(value
); }
235 operator wxUniChar() const { return GetChar(); }
236 operator char() const { return GetChar(); }
237 operator wchar_t() const { return GetChar(); }
238 wxUniChar
GetChar() const;
241 wxVariant(const wxArrayString
& val
, const wxString
& name
= wxEmptyString
);
242 bool operator== (const wxArrayString
& value
) const;
243 bool operator!= (const wxArrayString
& value
) const;
244 void operator= (const wxArrayString
& value
);
245 operator wxArrayString () const { return GetArrayString(); }
246 wxArrayString
GetArrayString() const;
249 wxVariant(void* ptr
, const wxString
& name
= wxEmptyString
);
250 bool operator== (void* value
) const;
251 bool operator!= (void* value
) const;
252 void operator= (void* value
);
253 operator void* () const { return GetVoidPtr(); }
254 void* GetVoidPtr() const;
257 wxVariant(wxObject
* ptr
, const wxString
& name
= wxEmptyString
);
258 bool operator== (wxObject
* value
) const;
259 bool operator!= (wxObject
* value
) const;
260 void operator= (wxObject
* value
);
261 wxObject
* GetWxObjectPtr() const;
264 // ------------------------------
266 // ------------------------------
268 wxVariant(const wxVariantList
& val
, const wxString
& name
= wxEmptyString
); // List of variants
269 bool operator== (const wxVariantList
& value
) const;
270 bool operator!= (const wxVariantList
& value
) const;
271 void operator= (const wxVariantList
& value
) ;
272 // Treat a list variant as an array
273 wxVariant
operator[] (size_t idx
) const;
274 wxVariant
& operator[] (size_t idx
) ;
275 wxVariantList
& GetList() const ;
277 // Return the number of elements in a list
278 size_t GetCount() const;
284 void Append(const wxVariant
& value
);
286 // Insert at front of list
287 void Insert(const wxVariant
& value
);
289 // Returns true if the variant is a member of the list
290 bool Member(const wxVariant
& value
) const;
292 // Deletes the nth element of the list
293 bool Delete(size_t item
);
300 bool Convert(long* value
) const;
301 bool Convert(bool* value
) const;
302 bool Convert(double* value
) const;
303 bool Convert(wxString
* value
) const;
304 bool Convert(wxUniChar
* value
) const;
305 bool Convert(char* value
) const;
306 bool Convert(wchar_t* value
) const;
308 bool Convert(wxDateTime
* value
) const;
309 #endif // wxUSE_DATETIME
313 virtual wxObjectRefData
*CreateRefData() const;
314 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
319 DECLARE_DYNAMIC_CLASS(wxVariant
)
322 #define DECLARE_VARIANT_OBJECT(classname) \
323 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
325 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
326 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
327 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
329 #define IMPLEMENT_VARIANT_OBJECT(classname) \
330 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
332 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
333 class classname##VariantData: public wxVariantData \
336 classname##VariantData() {} \
337 classname##VariantData( const classname &value ) { m_value = value; } \
339 classname &GetValue() { return m_value; } \
341 virtual bool Eq(wxVariantData& data) const; \
343 virtual wxString GetType() const; \
344 virtual wxClassInfo* GetValueClassInfo(); \
346 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
352 wxString classname##VariantData::GetType() const\
354 return m_value.GetClassInfo()->GetClassName();\
357 wxClassInfo* classname##VariantData::GetValueClassInfo()\
359 return m_value.GetClassInfo();\
362 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
364 wxASSERT( variant.GetType() == #classname );\
366 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
367 value = data->GetValue();\
371 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
373 classname##VariantData *data = new classname##VariantData( value );\
374 variant.SetData( data );\
378 // implements a wxVariantData-derived class using for the Eq() method the
379 // operator== which must have been provided by "classname"
380 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
381 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
383 bool classname##VariantData::Eq(wxVariantData& data) const \
385 wxASSERT( GetType() == data.GetType() );\
387 classname##VariantData & otherData = (classname##VariantData &) data;\
389 return otherData.m_value == m_value;\
393 // implements a wxVariantData-derived class using for the Eq() method a shallow
394 // comparison (through wxObject::IsSameAs function)
395 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
396 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
397 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
398 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
400 bool classname##VariantData::Eq(wxVariantData& data) const \
402 wxASSERT( GetType() == data.GetType() );\
404 classname##VariantData & otherData = (classname##VariantData &) data;\
406 return (otherData.m_value.IsSameAs(m_value));\
410 // Since we want type safety wxVariant we need to fetch and dynamic_cast
411 // in a seemingly safe way so the compiler can check, so we define
412 // a dynamic_cast /wxDynamicCast analogue.
414 #define wxGetVariantCast(var,classname) \
415 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
416 var.GetWxObjectPtr() : NULL));
418 // Replacement for using wxDynamicCast on a wxVariantData object
419 #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
421 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant
;
423 #endif // wxUSE_VARIANT
425 #endif // _WX_VARIANT_H_