]> git.saurik.com Git - wxWidgets.git/blame - include/wx/variant.h
Added GetTempDir
[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
JS
22#include "wx/list.h"
23
e2b87f38
VZ
24#if wxUSE_DATETIME
25 #include "wx/datetime.h"
26#endif // wxUSE_DATETIME
edca7a82
GT
27
28#if wxUSE_ODBC
29 #include "wx/db.h" // will #include sqltypes.h
30#endif //ODBC
31
65f19af1 32#include "wx/iosfwrap.h"
8cb50e4b
JS
33
34/*
35 * wxVariantData stores the actual data in a wxVariant object,
36 * to allow it to store any type of data.
37 * Derive from this to provide custom data handling.
38 *
2562c823
RR
39 * NB: To prevent addition of extra vtbl pointer to wxVariantData,
40 * we don't multiple-inherit from wxObjectRefData. Instead,
41 * we simply replicate the wxObject ref-counting scheme.
42 *
43 * NB: When you construct a wxVariantData, it will have refcount
44 * of one. Refcount will not be further increased when
45 * it is passed to wxVariant. This simulates old common
46 * scenario where wxVariant took ownership of wxVariantData
47 * passed to it.
48 * If you create wxVariantData for other reasons than passing
49 * it to wxVariant, technically you are not required to call
50 * DecRef() before deleting it.
51 *
8cb50e4b
JS
52 * TODO: in order to replace wxPropertyValue, we would need
53 * to consider adding constructors that take pointers to C++ variables,
54 * or removing that functionality from the wxProperty library.
55 * Essentially wxPropertyValue takes on some of the wxValidator functionality
56 * by storing pointers and not just actual values, allowing update of C++ data
57 * to be handled automatically. Perhaps there's another way of doing this without
58 * overloading wxVariant with unnecessary functionality.
59 */
60
bddd7a8d 61class WXDLLIMPEXP_BASE wxVariantData: public wxObject
8cb50e4b 62{
2562c823 63 friend class wxVariant;
8cb50e4b 64public:
2562c823
RR
65 wxVariantData()
66 : wxObject(), m_count(1)
67 { }
8cb50e4b 68
2562c823 69 // Override these to provide common functionality
8cb50e4b 70 virtual bool Eq(wxVariantData& data) const = 0;
07502d73 71
38830220 72#if wxUSE_STD_IOSTREAM
07502d73 73 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
38830220 74#endif
07502d73 75 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
38830220 76#if wxUSE_STD_IOSTREAM
07502d73 77 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
38830220 78#endif
07502d73 79 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
8cb50e4b
JS
80 // What type is it? Return a string name.
81 virtual wxString GetType() const = 0;
cf6ae290
RG
82 // If it based on wxObject return the ClassInfo.
83 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
2562c823
RR
84
85 void IncRef() { m_count++; }
86 void DecRef()
87 {
88 if ( --m_count == 0 )
89 delete this;
90 }
91
92 int GetRefCount() const { return m_count; }
93
94protected:
95 // Protected dtor should make some incompatible code
96 // break more louder. That is, they should do data->DecRef()
97 // instead of delete data.
98 virtual ~wxVariantData() { }
99
100private:
101 int m_count;
07502d73 102
2562c823
RR
103private:
104 DECLARE_ABSTRACT_CLASS(wxVariantData)
8cb50e4b
JS
105};
106
107/*
108 * wxVariant can store any kind of data, but has some basic types
109 * built in.
8cb50e4b
JS
110 */
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
149 // Make NULL (i.e. delete the data)
150 void MakeNull();
07502d73 151
2562c823
RR
152 // Delete data and name
153 void Clear();
07502d73 154
2562c823
RR
155 // Returns a string representing the type of the variant,
156 // e.g. "string", "bool", "stringlist", "list", "double", "long"
157 wxString GetType() const;
158
159 bool IsType(const wxString& type) const;
160 bool IsValueKindOf(const wxClassInfo* type) const;
161
07502d73 162 // write contents to a string (e.g. for debugging)
2562c823 163 wxString MakeString() const;
07502d73 164
2562c823
RR
165 // double
166 wxVariant(double val, const wxString& name = wxEmptyString);
8cb50e4b
JS
167 bool operator== (double value) const;
168 bool operator!= (double value) const;
169 void operator= (double value) ;
2562c823
RR
170 inline operator double () const { return GetDouble(); }
171 inline double GetReal() const { return GetDouble(); }
172 double GetDouble() const;
07502d73 173
2562c823
RR
174 // long
175 wxVariant(long val, const wxString& name = wxEmptyString);
176 wxVariant(int val, const wxString& name = wxEmptyString);
177 wxVariant(short val, const wxString& name = wxEmptyString);
8cb50e4b
JS
178 bool operator== (long value) const;
179 bool operator!= (long value) const;
180 void operator= (long value) ;
2562c823
RR
181 inline operator long () const { return GetLong(); }
182 inline long GetInteger() const { return GetLong(); }
183 long GetLong() const;
07502d73
WS
184
185 // bool
57493f9f 186#ifdef HAVE_BOOL
2562c823 187 wxVariant(bool val, const wxString& name = wxEmptyString);
8cb50e4b
JS
188 bool operator== (bool value) const;
189 bool operator!= (bool value) const;
190 void operator= (bool value) ;
2562c823
RR
191 inline operator bool () const { return GetBool(); }
192 bool GetBool() const ;
193#endif
194
195 // wxDateTime
196#if wxUSE_DATETIME
07502d73 197 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
2562c823
RR
198#if wxUSE_ODBC
199 wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString);
200 wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString);
201 wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString);
202#endif
203 bool operator== (const wxDateTime& value) const;
204 bool operator!= (const wxDateTime& value) const;
205 void operator= (const wxDateTime& value) ;
206#if wxUSE_ODBC
207 void operator= (const DATE_STRUCT* value) ;
208 void operator= (const TIME_STRUCT* value) ;
209 void operator= (const TIMESTAMP_STRUCT* value) ;
210#endif
211 inline operator wxDateTime () const { return GetDateTime(); }
212 wxDateTime GetDateTime() const;
57493f9f 213#endif
2562c823
RR
214
215 // wxString
216 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
217 wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool!
8cb50e4b
JS
218 bool operator== (const wxString& value) const;
219 bool operator!= (const wxString& value) const;
220 void operator= (const wxString& value) ;
2ed57eb7 221 void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
2562c823
RR
222 inline operator wxString () const { return MakeString(); }
223 wxString GetString() const;
224
71520754
RR
225 // wxChar
226 wxVariant(wxChar val, const wxString& name = wxEmptyString);
227 bool operator== (wxChar value) const;
228 bool operator!= (wxChar value) const;
229 void operator= (wxChar value) ;
230 inline operator wxChar () const { return GetChar(); }
231 wxChar GetChar() const ;
07502d73 232
2562c823
RR
233 // wxArrayString
234 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
235 bool operator== (const wxArrayString& value) const;
236 bool operator!= (const wxArrayString& value) const;
237 void operator= (const wxArrayString& value);
238 inline operator wxArrayString () const { return GetArrayString(); }
239 wxArrayString GetArrayString() const;
240
241 // void*
242 wxVariant(void* ptr, const wxString& name = wxEmptyString);
243 bool operator== (void* value) const;
244 bool operator!= (void* value) const;
245 void operator= (void* value);
246 inline operator void* () const { return GetVoidPtr(); }
247 void* GetVoidPtr() const;
248
249 // wxObject*
250 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString);
251 bool operator== (wxObject* value) const;
252 bool operator!= (wxObject* value) const;
253 void operator= (wxObject* value);
254 wxObject* GetWxObjectPtr() const;
255
256
2c3a1064 257#if WXWIN_COMPATIBILITY_2_4
2562c823 258 wxDEPRECATED( wxVariant(const wxStringList& val, const wxString& name = wxEmptyString) );
2c3a1064
RN
259 wxDEPRECATED( bool operator== (const wxStringList& value) const );
260 wxDEPRECATED( bool operator!= (const wxStringList& value) const );
261 wxDEPRECATED( void operator= (const wxStringList& value) );
2562c823 262 wxDEPRECATED( wxStringList& GetStringList() const );
2c3a1064 263#endif
2562c823
RR
264
265 // ------------------------------
266 // list operations
267 // ------------------------------
268
269 wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants
8cb50e4b
JS
270 bool operator== (const wxList& value) const;
271 bool operator!= (const wxList& value) const;
272 void operator= (const wxList& value) ;
8cb50e4b
JS
273 // Treat a list variant as an array
274 wxVariant operator[] (size_t idx) const;
275 wxVariant& operator[] (size_t idx) ;
2562c823 276 wxList& GetList() const ;
8cb50e4b
JS
277
278 // Return the number of elements in a list
43f06cfd 279 size_t GetCount() const;
07502d73 280
8cb50e4b
JS
281 // Make empty list
282 void NullList();
283
284 // Append to list
285 void Append(const wxVariant& value);
286
287 // Insert at front of list
288 void Insert(const wxVariant& value);
289
cab1a605 290 // Returns true if the variant is a member of the list
8cb50e4b
JS
291 bool Member(const wxVariant& value) const;
292
293 // Deletes the nth element of the list
43f06cfd 294 bool Delete(size_t item);
8cb50e4b
JS
295
296 // Clear list
297 void ClearList();
298
9708db20 299public:
2562c823 300 // Type conversion
8cb50e4b
JS
301 bool Convert(long* value) const;
302 bool Convert(bool* value) const;
303 bool Convert(double* value) const;
304 bool Convert(wxString* value) const;
38f82bf6 305 bool Convert(wxChar* value) const;
e2b87f38 306#if wxUSE_DATETIME
edca7a82 307 bool Convert(wxDateTime* value) const;
e2b87f38 308#endif // wxUSE_DATETIME
8cb50e4b
JS
309
310// Attributes
311protected:
312 wxVariantData* m_data;
a0a302dc 313 wxString m_name;
07502d73 314
2562c823
RR
315private:
316 DECLARE_DYNAMIC_CLASS(wxVariant)
8cb50e4b
JS
317};
318
6f5d7825
RR
319/* Fake macro parameter value */
320#ifdef EMPTY_PARAMETER_VALUE
321 #undef EMPTY_PARAMETER_VALUE
322#endif
07502d73 323#define EMPTY_PARAMETER_VALUE
3f90a399
RR
324
325#define DECLARE_VARIANT_OBJECT(classname) \
6f5d7825 326 DECLARE_VARIANT_OBJECT_EXPORTED(classname,EMPTY_PARAMETER_VALUE)
07502d73 327
6f5d7825 328#define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
39a48551
VZ
329expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
330expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
3f90a399
RR
331
332#define IMPLEMENT_VARIANT_OBJECT(classname) \
6f5d7825
RR
333 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,EMPTY_PARAMETER_VALUE)
334
335#define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
3f90a399
RR
336class classname##VariantData: public wxVariantData \
337{ \
338public:\
339 classname##VariantData() {} \
340 classname##VariantData( const classname &value ) { m_value = value; } \
341\
342 classname &GetValue() { return m_value; } \
343\
344 virtual bool Eq(wxVariantData& data) const; \
345\
346 virtual wxString GetType() const; \
347 virtual wxClassInfo* GetValueClassInfo(); \
348\
349protected:\
350 classname m_value; \
351\
352private: \
353 DECLARE_CLASS(classname##VariantData) \
354};\
355\
356IMPLEMENT_CLASS(classname##VariantData, wxVariantData)\
357\
358bool classname##VariantData::Eq(wxVariantData& data) const \
359{\
360 wxASSERT( wxIsKindOf((&data), classname##VariantData) );\
361\
362 classname##VariantData & otherData = (classname##VariantData &) data;\
363\
364 return (otherData.m_value == m_value);\
365}\
366\
367wxString classname##VariantData::GetType() const\
368{\
369 return m_value.GetClassInfo()->GetClassName();\
370}\
371\
372wxClassInfo* classname##VariantData::GetValueClassInfo()\
373{\
374 return m_value.GetClassInfo();\
375}\
376\
39a48551 377expdecl classname& operator << ( classname &value, const wxVariant &variant )\
3f90a399
RR
378{\
379 wxASSERT( wxIsKindOf( variant.GetData(), classname##VariantData ) );\
380 \
381 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
382 value = data->GetValue();\
383 return value;\
384}\
385\
39a48551 386expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
3f90a399
RR
387{\
388 classname##VariantData *data = new classname##VariantData( value );\
389 variant.SetData( data );\
390 return variant;\
391}
392
3f90a399
RR
393// Since we want type safety wxVariant we need to fetch and dynamic_cast
394// in a seemingly safe way so the compiler can check, so we define
395// a dynamic_cast /wxDynamicCast analogue.
cf6ae290
RG
396
397#define wxGetVariantCast(var,classname) \
cab1a605
WS
398 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
399 var.GetWxObjectPtr() : NULL));
cf6ae290 400
bddd7a8d 401extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
a0a302dc 402
d5dc103f
VZ
403#endif // wxUSE_VARIANT
404
405#endif // _WX_VARIANT_H_