]> git.saurik.com Git - wxWidgets.git/blame - include/wx/variant.h
remove obsolete file
[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"
8cb50e4b 24
e2b87f38
VZ
25#if wxUSE_DATETIME
26 #include "wx/datetime.h"
27#endif // wxUSE_DATETIME
edca7a82 28
65f19af1 29#include "wx/iosfwrap.h"
8cb50e4b
JS
30
31/*
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.
35 *
2562c823
RR
36 * NB: To prevent addition of extra vtbl pointer to wxVariantData,
37 * we don't multiple-inherit from wxObjectRefData. Instead,
38 * we simply replicate the wxObject ref-counting scheme.
39 *
40 * NB: When you construct a wxVariantData, it will have refcount
41 * of one. Refcount will not be further increased when
42 * it is passed to wxVariant. This simulates old common
43 * scenario where wxVariant took ownership of wxVariantData
44 * passed to it.
45 * If you create wxVariantData for other reasons than passing
46 * it to wxVariant, technically you are not required to call
47 * DecRef() before deleting it.
48 *
8cb50e4b
JS
49 * TODO: in order to replace wxPropertyValue, we would need
50 * to consider adding constructors that take pointers to C++ variables,
51 * or removing that functionality from the wxProperty library.
52 * Essentially wxPropertyValue takes on some of the wxValidator functionality
53 * by storing pointers and not just actual values, allowing update of C++ data
54 * to be handled automatically. Perhaps there's another way of doing this without
55 * overloading wxVariant with unnecessary functionality.
56 */
57
3586d10f 58class WXDLLIMPEXP_BASE wxVariantData
8cb50e4b 59{
2562c823 60 friend class wxVariant;
8cb50e4b 61public:
3586d10f 62 wxVariantData() : m_count(1) { }
8cb50e4b 63
2562c823 64 // Override these to provide common functionality
8cb50e4b 65 virtual bool Eq(wxVariantData& data) const = 0;
07502d73 66
38830220 67#if wxUSE_STD_IOSTREAM
07502d73 68 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
38830220 69#endif
07502d73 70 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
38830220 71#if wxUSE_STD_IOSTREAM
07502d73 72 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
38830220 73#endif
07502d73 74 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
8cb50e4b
JS
75 // What type is it? Return a string name.
76 virtual wxString GetType() const = 0;
cf6ae290
RG
77 // If it based on wxObject return the ClassInfo.
78 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
2562c823 79
c8058a09
JS
80 // Implement this to make wxVariant::AllocExcusive work. Returns
81 // a copy of the data.
82 virtual wxVariantData* Clone() const { return NULL; }
83
2562c823
RR
84 void IncRef() { m_count++; }
85 void DecRef()
86 {
87 if ( --m_count == 0 )
88 delete this;
89 }
90
91 int GetRefCount() const { return m_count; }
92
93protected:
94 // Protected dtor should make some incompatible code
95 // break more louder. That is, they should do data->DecRef()
96 // instead of delete data.
97 virtual ~wxVariantData() { }
98
99private:
100 int m_count;
8cb50e4b
JS
101};
102
103/*
104 * wxVariant can store any kind of data, but has some basic types
105 * built in.
8cb50e4b 106 */
c8058a09 107
7e6b4780
RR
108class WXDLLIMPEXP_FWD_BASE wxVariant;
109
110WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE);
8cb50e4b 111
bddd7a8d 112class WXDLLIMPEXP_BASE wxVariant: public wxObject
8cb50e4b 113{
8cb50e4b 114public:
8cb50e4b 115 wxVariant();
07502d73 116
8cb50e4b 117 wxVariant(const wxVariant& variant);
2562c823 118 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
d3c7fc99 119 virtual ~wxVariant();
8cb50e4b 120
2562c823 121 // generic assignment
8cb50e4b
JS
122 void operator= (const wxVariant& variant);
123
124 // Assignment using data, e.g.
125 // myVariant = new wxStringVariantData("hello");
126 void operator= (wxVariantData* variantData);
07502d73 127
8cb50e4b
JS
128 bool operator== (const wxVariant& variant) const;
129 bool operator!= (const wxVariant& variant) const;
130
2562c823
RR
131 // Sets/gets name
132 inline void SetName(const wxString& name) { m_name = name; }
133 inline const wxString& GetName() const { return m_name; }
134
135 // Tests whether there is data
07502d73 136 bool IsNull() const;
2562c823
RR
137
138 // For compatibility with wxWidgets <= 2.6, this doesn't increase
139 // reference count.
140 wxVariantData* GetData() const { return m_data; }
141 void SetData(wxVariantData* data) ;
142
143 // make a 'clone' of the object
144 void Ref(const wxVariant& clone);
145
146 // destroy a reference
147 void UnRef();
148
c8058a09
JS
149 // ensure that the data is exclusive to this variant, and not shared
150 bool Unshare();
151
2562c823
RR
152 // Make NULL (i.e. delete the data)
153 void MakeNull();
07502d73 154
2562c823
RR
155 // Delete data and name
156 void Clear();
07502d73 157
2562c823
RR
158 // Returns a string representing the type of the variant,
159 // e.g. "string", "bool", "stringlist", "list", "double", "long"
160 wxString GetType() const;
161
162 bool IsType(const wxString& type) const;
163 bool IsValueKindOf(const wxClassInfo* type) const;
164
07502d73 165 // write contents to a string (e.g. for debugging)
2562c823 166 wxString MakeString() const;
07502d73 167
2562c823
RR
168 // double
169 wxVariant(double val, const wxString& name = wxEmptyString);
8cb50e4b
JS
170 bool operator== (double value) const;
171 bool operator!= (double value) const;
172 void operator= (double value) ;
2562c823
RR
173 inline operator double () const { return GetDouble(); }
174 inline double GetReal() const { return GetDouble(); }
175 double GetDouble() const;
07502d73 176
2562c823
RR
177 // long
178 wxVariant(long val, const wxString& name = wxEmptyString);
179 wxVariant(int val, const wxString& name = wxEmptyString);
180 wxVariant(short val, const wxString& name = wxEmptyString);
8cb50e4b
JS
181 bool operator== (long value) const;
182 bool operator!= (long value) const;
183 void operator= (long value) ;
2562c823
RR
184 inline operator long () const { return GetLong(); }
185 inline long GetInteger() const { return GetLong(); }
186 long GetLong() const;
07502d73
WS
187
188 // bool
57493f9f 189#ifdef HAVE_BOOL
2562c823 190 wxVariant(bool val, const wxString& name = wxEmptyString);
8cb50e4b
JS
191 bool operator== (bool value) const;
192 bool operator!= (bool value) const;
193 void operator= (bool value) ;
2562c823
RR
194 inline operator bool () const { return GetBool(); }
195 bool GetBool() const ;
196#endif
197
198 // wxDateTime
199#if wxUSE_DATETIME
07502d73 200 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
2562c823
RR
201 bool operator== (const wxDateTime& value) const;
202 bool operator!= (const wxDateTime& value) const;
203 void operator= (const wxDateTime& value) ;
2562c823
RR
204 inline operator wxDateTime () const { return GetDateTime(); }
205 wxDateTime GetDateTime() const;
57493f9f 206#endif
2562c823
RR
207
208 // wxString
209 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
af717fa8
VS
210 // these overloads are necessary to prevent the compiler from using bool
211 // version instead of wxString one:
212 wxVariant(const char* val, const wxString& name = wxEmptyString);
213 wxVariant(const wchar_t* val, const wxString& name = wxEmptyString);
214 wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString);
215 wxVariant(const wxCharBuffer& val, const wxString& name = wxEmptyString);
216 wxVariant(const wxWCharBuffer& val, const wxString& name = wxEmptyString);
217
8cb50e4b
JS
218 bool operator== (const wxString& value) const;
219 bool operator!= (const wxString& value) const;
af717fa8
VS
220 wxVariant& operator=(const wxString& value);
221 // these overloads are necessary to prevent the compiler from using bool
222 // version instead of wxString one:
223 wxVariant& operator=(const char* value)
224 { return *this = wxString(value); }
225 wxVariant& operator=(const wchar_t* value)
226 { return *this = wxString(value); }
227 wxVariant& operator=(const wxCStrData& value)
228 { return *this = value.AsString(); }
229 template<typename T>
230 wxVariant& operator=(const wxCharTypeBuffer<T>& value)
231 { return *this = value.data(); }
232
2562c823
RR
233 inline operator wxString () const { return MakeString(); }
234 wxString GetString() const;
235
af717fa8
VS
236 // wxUniChar
237 wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString);
238 wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString);
239 wxVariant(char val, const wxString& name = wxEmptyString);
240 wxVariant(wchar_t val, const wxString& name = wxEmptyString);
241 bool operator==(const wxUniChar& value) const;
242 bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); }
243 bool operator==(char value) const { return *this == wxUniChar(value); }
244 bool operator==(wchar_t value) const { return *this == wxUniChar(value); }
245 bool operator!=(const wxUniChar& value) const { return !(*this == value); }
246 bool operator!=(const wxUniCharRef& value) const { return !(*this == value); }
247 bool operator!=(char value) const { return !(*this == value); }
248 bool operator!=(wchar_t value) const { return !(*this == value); }
249 wxVariant& operator=(const wxUniChar& value);
250 wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); }
251 wxVariant& operator=(char value) { return *this = wxUniChar(value); }
252 wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); }
253 operator wxUniChar() const { return GetChar(); }
254 operator char() const { return GetChar(); }
255 operator wchar_t() const { return GetChar(); }
256 wxUniChar GetChar() const;
07502d73 257
2562c823
RR
258 // wxArrayString
259 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
260 bool operator== (const wxArrayString& value) const;
261 bool operator!= (const wxArrayString& value) const;
262 void operator= (const wxArrayString& value);
af717fa8 263 operator wxArrayString () const { return GetArrayString(); }
2562c823
RR
264 wxArrayString GetArrayString() const;
265
266 // void*
267 wxVariant(void* ptr, const wxString& name = wxEmptyString);
268 bool operator== (void* value) const;
269 bool operator!= (void* value) const;
270 void operator= (void* value);
af717fa8 271 operator void* () const { return GetVoidPtr(); }
2562c823
RR
272 void* GetVoidPtr() const;
273
274 // wxObject*
275 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString);
276 bool operator== (wxObject* value) const;
277 bool operator!= (wxObject* value) const;
278 void operator= (wxObject* value);
279 wxObject* GetWxObjectPtr() const;
280
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
8cb50e4b
JS
328
329// Attributes
330protected:
331 wxVariantData* m_data;
a0a302dc 332 wxString m_name;
07502d73 333
2562c823
RR
334private:
335 DECLARE_DYNAMIC_CLASS(wxVariant)
8cb50e4b
JS
336};
337
3f90a399 338#define DECLARE_VARIANT_OBJECT(classname) \
bde626ce 339 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
07502d73 340
6f5d7825 341#define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
39a48551
VZ
342expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
343expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
3f90a399
RR
344
345#define IMPLEMENT_VARIANT_OBJECT(classname) \
bde626ce 346 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
6f5d7825 347
55ccdb93 348#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
3f90a399
RR
349class classname##VariantData: public wxVariantData \
350{ \
351public:\
352 classname##VariantData() {} \
353 classname##VariantData( const classname &value ) { m_value = value; } \
354\
355 classname &GetValue() { return m_value; } \
356\
357 virtual bool Eq(wxVariantData& data) const; \
358\
359 virtual wxString GetType() const; \
360 virtual wxClassInfo* GetValueClassInfo(); \
c8058a09
JS
361\
362 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
3f90a399
RR
363\
364protected:\
365 classname m_value; \
3f90a399
RR
366};\
367\
3f90a399
RR
368wxString classname##VariantData::GetType() const\
369{\
370 return m_value.GetClassInfo()->GetClassName();\
371}\
372\
373wxClassInfo* classname##VariantData::GetValueClassInfo()\
374{\
375 return m_value.GetClassInfo();\
376}\
377\
39a48551 378expdecl classname& operator << ( classname &value, const wxVariant &variant )\
3f90a399 379{\
3586d10f 380 wxASSERT( variant.GetType() == #classname );\
3f90a399
RR
381 \
382 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
383 value = data->GetValue();\
384 return value;\
385}\
386\
39a48551 387expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
3f90a399
RR
388{\
389 classname##VariantData *data = new classname##VariantData( value );\
390 variant.SetData( data );\
391 return variant;\
392}
393
01df01eb
VZ
394// implements a wxVariantData-derived class using for the Eq() method the
395// operator== which must have been provided by "classname"
55ccdb93 396#define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
5ffa72f4 397IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
55ccdb93
VZ
398\
399bool classname##VariantData::Eq(wxVariantData& data) const \
400{\
3586d10f 401 wxASSERT( GetType() == data.GetType() );\
55ccdb93
VZ
402\
403 classname##VariantData & otherData = (classname##VariantData &) data;\
404\
405 return otherData.m_value == m_value;\
406}\
407
408
409// implements a wxVariantData-derived class using for the Eq() method a shallow
a3ab1c18 410// comparison (through wxObject::IsSameAs function)
01df01eb
VZ
411#define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
412 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
55ccdb93 413#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
5ffa72f4 414IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
55ccdb93
VZ
415\
416bool classname##VariantData::Eq(wxVariantData& data) const \
417{\
3586d10f 418 wxASSERT( GetType() == data.GetType() );\
55ccdb93
VZ
419\
420 classname##VariantData & otherData = (classname##VariantData &) data;\
421\
a3ab1c18 422 return (otherData.m_value.IsSameAs(m_value));\
55ccdb93
VZ
423}\
424
425
3f90a399
RR
426// Since we want type safety wxVariant we need to fetch and dynamic_cast
427// in a seemingly safe way so the compiler can check, so we define
428// a dynamic_cast /wxDynamicCast analogue.
cf6ae290
RG
429
430#define wxGetVariantCast(var,classname) \
cab1a605
WS
431 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
432 var.GetWxObjectPtr() : NULL));
cf6ae290 433
c8058a09
JS
434// Replacement for using wxDynamicCast on a wxVariantData object
435#define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
436
bddd7a8d 437extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
a0a302dc 438
d5dc103f
VZ
439#endif // wxUSE_VARIANT
440
441#endif // _WX_VARIANT_H_