]> git.saurik.com Git - wxWidgets.git/blame - include/wx/variant.h
Revert r62598 which removed "unnecessary wxOSX_USE_COCOA checks."
[wxWidgets.git] / include / wx / variant.h
CommitLineData
8cb50e4b 1/////////////////////////////////////////////////////////////////////////////
43f06cfd 2// Name: wx/variant.h
8cb50e4b
JS
3// Purpose: wxVariant class, container for any type
4// Author: Julian Smart
5// Modified by:
6// Created: 10/09/98
7// RCS-ID: $Id$
99d80019 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
8cb50e4b
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_VARIANT_H_
13#define _WX_VARIANT_H_
14
8cb50e4b 15#include "wx/defs.h"
d5dc103f
VZ
16
17#if wxUSE_VARIANT
18
8cb50e4b
JS
19#include "wx/object.h"
20#include "wx/string.h"
4c3ebca9 21#include "wx/arrstr.h"
8cb50e4b 22#include "wx/list.h"
bde626ce 23#include "wx/cpp.h"
4e00b908 24#include "wx/longlong.h"
8cb50e4b 25
e2b87f38
VZ
26#if wxUSE_DATETIME
27 #include "wx/datetime.h"
28#endif // wxUSE_DATETIME
edca7a82 29
65f19af1 30#include "wx/iosfwrap.h"
8cb50e4b
JS
31
32/*
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.
36 *
2562c823
RR
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
41 * passed to it.
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.
45 *
8cb50e4b
JS
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.
53 */
54
cf25a599 55class WXDLLIMPEXP_BASE wxVariantData : public wxObjectRefData
8cb50e4b 56{
2562c823 57 friend class wxVariant;
8cb50e4b 58public:
92ffc98a 59 wxVariantData() { }
8cb50e4b 60
2562c823 61 // Override these to provide common functionality
8cb50e4b 62 virtual bool Eq(wxVariantData& data) const = 0;
07502d73 63
38830220 64#if wxUSE_STD_IOSTREAM
07502d73 65 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
38830220 66#endif
07502d73 67 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
38830220 68#if wxUSE_STD_IOSTREAM
07502d73 69 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
38830220 70#endif
07502d73 71 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
8cb50e4b
JS
72 // What type is it? Return a string name.
73 virtual wxString GetType() const = 0;
cf6ae290
RG
74 // If it based on wxObject return the ClassInfo.
75 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
2562c823 76
cf25a599 77 // Implement this to make wxVariant::UnShare work. Returns
c8058a09
JS
78 // a copy of the data.
79 virtual wxVariantData* Clone() const { return NULL; }
80
2562c823
RR
81protected:
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() { }
8cb50e4b
JS
86};
87
88/*
89 * wxVariant can store any kind of data, but has some basic types
90 * built in.
8cb50e4b 91 */
c8058a09 92
7e6b4780
RR
93class WXDLLIMPEXP_FWD_BASE wxVariant;
94
95WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE);
8cb50e4b 96
bddd7a8d 97class WXDLLIMPEXP_BASE wxVariant: public wxObject
8cb50e4b 98{
8cb50e4b 99public:
8cb50e4b 100 wxVariant();
07502d73 101
8cb50e4b 102 wxVariant(const wxVariant& variant);
2562c823 103 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
d3c7fc99 104 virtual ~wxVariant();
8cb50e4b 105
2562c823 106 // generic assignment
8cb50e4b
JS
107 void operator= (const wxVariant& variant);
108
109 // Assignment using data, e.g.
110 // myVariant = new wxStringVariantData("hello");
111 void operator= (wxVariantData* variantData);
07502d73 112
8cb50e4b
JS
113 bool operator== (const wxVariant& variant) const;
114 bool operator!= (const wxVariant& variant) const;
115
2562c823
RR
116 // Sets/gets name
117 inline void SetName(const wxString& name) { m_name = name; }
118 inline const wxString& GetName() const { return m_name; }
119
120 // Tests whether there is data
07502d73 121 bool IsNull() const;
2562c823
RR
122
123 // For compatibility with wxWidgets <= 2.6, this doesn't increase
124 // reference count.
cf25a599
JS
125 wxVariantData* GetData() const
126 {
127 return (wxVariantData*) m_refData;
128 }
2562c823
RR
129 void SetData(wxVariantData* data) ;
130
131 // make a 'clone' of the object
cf25a599 132 void Ref(const wxVariant& clone) { wxObject::Ref(clone); }
2562c823 133
c8058a09
JS
134 // ensure that the data is exclusive to this variant, and not shared
135 bool Unshare();
136
2562c823
RR
137 // Make NULL (i.e. delete the data)
138 void MakeNull();
07502d73 139
2562c823
RR
140 // Delete data and name
141 void Clear();
07502d73 142
2562c823
RR
143 // Returns a string representing the type of the variant,
144 // e.g. "string", "bool", "stringlist", "list", "double", "long"
145 wxString GetType() const;
146
147 bool IsType(const wxString& type) const;
148 bool IsValueKindOf(const wxClassInfo* type) const;
149
07502d73 150 // write contents to a string (e.g. for debugging)
2562c823 151 wxString MakeString() const;
07502d73 152
2562c823
RR
153 // double
154 wxVariant(double val, const wxString& name = wxEmptyString);
8cb50e4b
JS
155 bool operator== (double value) const;
156 bool operator!= (double value) const;
157 void operator= (double value) ;
2562c823
RR
158 inline operator double () const { return GetDouble(); }
159 inline double GetReal() const { return GetDouble(); }
160 double GetDouble() const;
07502d73 161
2562c823
RR
162 // long
163 wxVariant(long val, const wxString& name = wxEmptyString);
164 wxVariant(int val, const wxString& name = wxEmptyString);
165 wxVariant(short val, const wxString& name = wxEmptyString);
8cb50e4b
JS
166 bool operator== (long value) const;
167 bool operator!= (long value) const;
168 void operator= (long value) ;
2562c823
RR
169 inline operator long () const { return GetLong(); }
170 inline long GetInteger() const { return GetLong(); }
171 long GetLong() const;
07502d73
WS
172
173 // bool
2562c823 174 wxVariant(bool val, const wxString& name = wxEmptyString);
8cb50e4b
JS
175 bool operator== (bool value) const;
176 bool operator!= (bool value) const;
177 void operator= (bool value) ;
2562c823
RR
178 inline operator bool () const { return GetBool(); }
179 bool GetBool() const ;
2562c823
RR
180
181 // wxDateTime
182#if wxUSE_DATETIME
07502d73 183 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
2562c823
RR
184 bool operator== (const wxDateTime& value) const;
185 bool operator!= (const wxDateTime& value) const;
186 void operator= (const wxDateTime& value) ;
2562c823
RR
187 inline operator wxDateTime () const { return GetDateTime(); }
188 wxDateTime GetDateTime() const;
57493f9f 189#endif
2562c823
RR
190
191 // wxString
192 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
af717fa8
VS
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);
de4983f3
VS
198 wxVariant(const wxScopedCharBuffer& val, const wxString& name = wxEmptyString);
199 wxVariant(const wxScopedWCharBuffer& val, const wxString& name = wxEmptyString);
af717fa8 200
8cb50e4b
JS
201 bool operator== (const wxString& value) const;
202 bool operator!= (const wxString& value) const;
af717fa8
VS
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(); }
212 template<typename T>
de4983f3 213 wxVariant& operator=(const wxScopedCharTypeBuffer<T>& value)
af717fa8
VS
214 { return *this = value.data(); }
215
2562c823
RR
216 inline operator wxString () const { return MakeString(); }
217 wxString GetString() const;
218
af717fa8
VS
219 // wxUniChar
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;
07502d73 240
2562c823
RR
241 // wxArrayString
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);
af717fa8 246 operator wxArrayString () const { return GetArrayString(); }
2562c823
RR
247 wxArrayString GetArrayString() const;
248
249 // void*
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);
af717fa8 254 operator void* () const { return GetVoidPtr(); }
2562c823
RR
255 void* GetVoidPtr() const;
256
257 // wxObject*
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;
263
4e00b908
JS
264#if wxUSE_LONGLONG
265 // wxLongLong
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;
272
273 // wxULongLong
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;
280#endif
2562c823 281
2562c823
RR
282 // ------------------------------
283 // list operations
284 // ------------------------------
285
9a0a58f5
RR
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) ;
8cb50e4b
JS
290 // Treat a list variant as an array
291 wxVariant operator[] (size_t idx) const;
292 wxVariant& operator[] (size_t idx) ;
9a0a58f5 293 wxVariantList& GetList() const ;
8cb50e4b
JS
294
295 // Return the number of elements in a list
43f06cfd 296 size_t GetCount() const;
07502d73 297
8cb50e4b
JS
298 // Make empty list
299 void NullList();
300
301 // Append to list
302 void Append(const wxVariant& value);
303
304 // Insert at front of list
305 void Insert(const wxVariant& value);
306
cab1a605 307 // Returns true if the variant is a member of the list
8cb50e4b
JS
308 bool Member(const wxVariant& value) const;
309
310 // Deletes the nth element of the list
43f06cfd 311 bool Delete(size_t item);
8cb50e4b
JS
312
313 // Clear list
314 void ClearList();
315
9708db20 316public:
2562c823 317 // Type conversion
8cb50e4b
JS
318 bool Convert(long* value) const;
319 bool Convert(bool* value) const;
320 bool Convert(double* value) const;
321 bool Convert(wxString* value) const;
af717fa8
VS
322 bool Convert(wxUniChar* value) const;
323 bool Convert(char* value) const;
324 bool Convert(wchar_t* value) const;
e2b87f38 325#if wxUSE_DATETIME
edca7a82 326 bool Convert(wxDateTime* value) const;
e2b87f38 327#endif // wxUSE_DATETIME
4e00b908
JS
328#if wxUSE_LONGLONG
329 bool Convert(wxLongLong* value) const;
330 bool Convert(wxULongLong* value) const;
331#endif // wxUSE_LONGLONG
8cb50e4b
JS
332
333// Attributes
334protected:
cf25a599
JS
335 virtual wxObjectRefData *CreateRefData() const;
336 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
337
a0a302dc 338 wxString m_name;
07502d73 339
2562c823
RR
340private:
341 DECLARE_DYNAMIC_CLASS(wxVariant)
8cb50e4b
JS
342};
343
3f90a399 344#define DECLARE_VARIANT_OBJECT(classname) \
bde626ce 345 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
07502d73 346
6f5d7825 347#define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
39a48551
VZ
348expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
349expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
3f90a399
RR
350
351#define IMPLEMENT_VARIANT_OBJECT(classname) \
bde626ce 352 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
6f5d7825 353
55ccdb93 354#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
3f90a399
RR
355class classname##VariantData: public wxVariantData \
356{ \
357public:\
358 classname##VariantData() {} \
359 classname##VariantData( const classname &value ) { m_value = value; } \
360\
361 classname &GetValue() { return m_value; } \
362\
363 virtual bool Eq(wxVariantData& data) const; \
364\
365 virtual wxString GetType() const; \
366 virtual wxClassInfo* GetValueClassInfo(); \
c8058a09
JS
367\
368 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
3f90a399
RR
369\
370protected:\
371 classname m_value; \
3f90a399
RR
372};\
373\
3f90a399
RR
374wxString classname##VariantData::GetType() const\
375{\
376 return m_value.GetClassInfo()->GetClassName();\
377}\
378\
379wxClassInfo* classname##VariantData::GetValueClassInfo()\
380{\
381 return m_value.GetClassInfo();\
382}\
383\
39a48551 384expdecl classname& operator << ( classname &value, const wxVariant &variant )\
3f90a399 385{\
3586d10f 386 wxASSERT( variant.GetType() == #classname );\
3f90a399
RR
387 \
388 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
389 value = data->GetValue();\
390 return value;\
391}\
392\
39a48551 393expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
3f90a399
RR
394{\
395 classname##VariantData *data = new classname##VariantData( value );\
396 variant.SetData( data );\
397 return variant;\
398}
399
01df01eb
VZ
400// implements a wxVariantData-derived class using for the Eq() method the
401// operator== which must have been provided by "classname"
55ccdb93 402#define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
5ffa72f4 403IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
55ccdb93
VZ
404\
405bool classname##VariantData::Eq(wxVariantData& data) const \
406{\
3586d10f 407 wxASSERT( GetType() == data.GetType() );\
55ccdb93
VZ
408\
409 classname##VariantData & otherData = (classname##VariantData &) data;\
410\
411 return otherData.m_value == m_value;\
412}\
413
414
415// implements a wxVariantData-derived class using for the Eq() method a shallow
a3ab1c18 416// comparison (through wxObject::IsSameAs function)
01df01eb
VZ
417#define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
418 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
55ccdb93 419#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
5ffa72f4 420IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
55ccdb93
VZ
421\
422bool classname##VariantData::Eq(wxVariantData& data) const \
423{\
3586d10f 424 wxASSERT( GetType() == data.GetType() );\
55ccdb93
VZ
425\
426 classname##VariantData & otherData = (classname##VariantData &) data;\
427\
a3ab1c18 428 return (otherData.m_value.IsSameAs(m_value));\
55ccdb93
VZ
429}\
430
431
3f90a399
RR
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.
cf6ae290
RG
435
436#define wxGetVariantCast(var,classname) \
cab1a605
WS
437 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
438 var.GetWxObjectPtr() : NULL));
cf6ae290 439
c8058a09
JS
440// Replacement for using wxDynamicCast on a wxVariantData object
441#define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
442
bddd7a8d 443extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
a0a302dc 444
d5dc103f
VZ
445#endif // wxUSE_VARIANT
446
447#endif // _WX_VARIANT_H_