]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
Added PrependColumn methods and short cuts. Added test for DeleteColumn to sample
[wxWidgets.git] / include / wx / variant.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/variant.h
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 10/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_VARIANT_H_
13 #define _WX_VARIANT_H_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_VARIANT
18
19 #include "wx/object.h"
20 #include "wx/string.h"
21 #include "wx/arrstr.h"
22 #include "wx/list.h"
23 #include "wx/cpp.h"
24
25 #if wxUSE_DATETIME
26 #include "wx/datetime.h"
27 #endif // wxUSE_DATETIME
28
29 #if wxUSE_ODBC
30 #include "wx/db.h" // will #include sqltypes.h
31 #endif //ODBC
32
33 #include "wx/iosfwrap.h"
34
35 /*
36 * wxVariantData stores the actual data in a wxVariant object,
37 * to allow it to store any type of data.
38 * Derive from this to provide custom data handling.
39 *
40 * NB: To prevent addition of extra vtbl pointer to wxVariantData,
41 * we don't multiple-inherit from wxObjectRefData. Instead,
42 * we simply replicate the wxObject ref-counting scheme.
43 *
44 * NB: When you construct a wxVariantData, it will have refcount
45 * of one. Refcount will not be further increased when
46 * it is passed to wxVariant. This simulates old common
47 * scenario where wxVariant took ownership of wxVariantData
48 * passed to it.
49 * If you create wxVariantData for other reasons than passing
50 * it to wxVariant, technically you are not required to call
51 * DecRef() before deleting it.
52 *
53 * TODO: in order to replace wxPropertyValue, we would need
54 * to consider adding constructors that take pointers to C++ variables,
55 * or removing that functionality from the wxProperty library.
56 * Essentially wxPropertyValue takes on some of the wxValidator functionality
57 * by storing pointers and not just actual values, allowing update of C++ data
58 * to be handled automatically. Perhaps there's another way of doing this without
59 * overloading wxVariant with unnecessary functionality.
60 */
61
62 class WXDLLIMPEXP_BASE wxVariantData
63 {
64 friend class wxVariant;
65 public:
66 wxVariantData() : m_count(1) { }
67
68 // Override these to provide common functionality
69 virtual bool Eq(wxVariantData& data) const = 0;
70
71 #if wxUSE_STD_IOSTREAM
72 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
73 #endif
74 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
75 #if wxUSE_STD_IOSTREAM
76 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
77 #endif
78 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
79 // What type is it? Return a string name.
80 virtual wxString GetType() const = 0;
81 // If it based on wxObject return the ClassInfo.
82 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
83
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
93 protected:
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
99 private:
100 int m_count;
101 };
102
103 /*
104 * wxVariant can store any kind of data, but has some basic types
105 * built in.
106 */
107
108 class WXDLLIMPEXP_FWD_BASE wxVariant;
109
110 WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE);
111
112 class WXDLLIMPEXP_BASE wxVariant: public wxObject
113 {
114 public:
115 wxVariant();
116
117 wxVariant(const wxVariant& variant);
118 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
119 virtual ~wxVariant();
120
121 // generic assignment
122 void operator= (const wxVariant& variant);
123
124 // Assignment using data, e.g.
125 // myVariant = new wxStringVariantData("hello");
126 void operator= (wxVariantData* variantData);
127
128 bool operator== (const wxVariant& variant) const;
129 bool operator!= (const wxVariant& variant) const;
130
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
136 bool IsNull() const;
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();
151
152 // Delete data and name
153 void Clear();
154
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
162 // write contents to a string (e.g. for debugging)
163 wxString MakeString() const;
164
165 // double
166 wxVariant(double val, const wxString& name = wxEmptyString);
167 bool operator== (double value) const;
168 bool operator!= (double value) const;
169 void operator= (double value) ;
170 inline operator double () const { return GetDouble(); }
171 inline double GetReal() const { return GetDouble(); }
172 double GetDouble() const;
173
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);
178 bool operator== (long value) const;
179 bool operator!= (long value) const;
180 void operator= (long value) ;
181 inline operator long () const { return GetLong(); }
182 inline long GetInteger() const { return GetLong(); }
183 long GetLong() const;
184
185 // bool
186 #ifdef HAVE_BOOL
187 wxVariant(bool val, const wxString& name = wxEmptyString);
188 bool operator== (bool value) const;
189 bool operator!= (bool value) const;
190 void operator= (bool value) ;
191 inline operator bool () const { return GetBool(); }
192 bool GetBool() const ;
193 #endif
194
195 // wxDateTime
196 #if wxUSE_DATETIME
197 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
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;
213 #endif
214
215 // wxString
216 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
217 // these overloads are necessary to prevent the compiler from using bool
218 // version instead of wxString one:
219 wxVariant(const char* val, const wxString& name = wxEmptyString);
220 wxVariant(const wchar_t* val, const wxString& name = wxEmptyString);
221 wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString);
222 wxVariant(const wxCharBuffer& val, const wxString& name = wxEmptyString);
223 wxVariant(const wxWCharBuffer& val, const wxString& name = wxEmptyString);
224
225 bool operator== (const wxString& value) const;
226 bool operator!= (const wxString& value) const;
227 wxVariant& operator=(const wxString& value);
228 // these overloads are necessary to prevent the compiler from using bool
229 // version instead of wxString one:
230 wxVariant& operator=(const char* value)
231 { return *this = wxString(value); }
232 wxVariant& operator=(const wchar_t* value)
233 { return *this = wxString(value); }
234 wxVariant& operator=(const wxCStrData& value)
235 { return *this = value.AsString(); }
236 template<typename T>
237 wxVariant& operator=(const wxCharTypeBuffer<T>& value)
238 { return *this = value.data(); }
239
240 inline operator wxString () const { return MakeString(); }
241 wxString GetString() const;
242
243 // wxUniChar
244 wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString);
245 wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString);
246 wxVariant(char val, const wxString& name = wxEmptyString);
247 wxVariant(wchar_t val, const wxString& name = wxEmptyString);
248 bool operator==(const wxUniChar& value) const;
249 bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); }
250 bool operator==(char value) const { return *this == wxUniChar(value); }
251 bool operator==(wchar_t value) const { return *this == wxUniChar(value); }
252 bool operator!=(const wxUniChar& value) const { return !(*this == value); }
253 bool operator!=(const wxUniCharRef& value) const { return !(*this == value); }
254 bool operator!=(char value) const { return !(*this == value); }
255 bool operator!=(wchar_t value) const { return !(*this == value); }
256 wxVariant& operator=(const wxUniChar& value);
257 wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); }
258 wxVariant& operator=(char value) { return *this = wxUniChar(value); }
259 wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); }
260 operator wxUniChar() const { return GetChar(); }
261 operator char() const { return GetChar(); }
262 operator wchar_t() const { return GetChar(); }
263 wxUniChar GetChar() const;
264
265 // wxArrayString
266 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
267 bool operator== (const wxArrayString& value) const;
268 bool operator!= (const wxArrayString& value) const;
269 void operator= (const wxArrayString& value);
270 operator wxArrayString () const { return GetArrayString(); }
271 wxArrayString GetArrayString() const;
272
273 // void*
274 wxVariant(void* ptr, const wxString& name = wxEmptyString);
275 bool operator== (void* value) const;
276 bool operator!= (void* value) const;
277 void operator= (void* value);
278 operator void* () const { return GetVoidPtr(); }
279 void* GetVoidPtr() const;
280
281 // wxObject*
282 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString);
283 bool operator== (wxObject* value) const;
284 bool operator!= (wxObject* value) const;
285 void operator= (wxObject* value);
286 wxObject* GetWxObjectPtr() const;
287
288
289 // ------------------------------
290 // list operations
291 // ------------------------------
292
293 wxVariant(const wxVariantList& val, const wxString& name = wxEmptyString); // List of variants
294 bool operator== (const wxVariantList& value) const;
295 bool operator!= (const wxVariantList& value) const;
296 void operator= (const wxVariantList& value) ;
297 // Treat a list variant as an array
298 wxVariant operator[] (size_t idx) const;
299 wxVariant& operator[] (size_t idx) ;
300 wxVariantList& GetList() const ;
301
302 // Return the number of elements in a list
303 size_t GetCount() const;
304
305 // Make empty list
306 void NullList();
307
308 // Append to list
309 void Append(const wxVariant& value);
310
311 // Insert at front of list
312 void Insert(const wxVariant& value);
313
314 // Returns true if the variant is a member of the list
315 bool Member(const wxVariant& value) const;
316
317 // Deletes the nth element of the list
318 bool Delete(size_t item);
319
320 // Clear list
321 void ClearList();
322
323 public:
324 // Type conversion
325 bool Convert(long* value) const;
326 bool Convert(bool* value) const;
327 bool Convert(double* value) const;
328 bool Convert(wxString* value) const;
329 bool Convert(wxUniChar* value) const;
330 bool Convert(char* value) const;
331 bool Convert(wchar_t* value) const;
332 #if wxUSE_DATETIME
333 bool Convert(wxDateTime* value) const;
334 #endif // wxUSE_DATETIME
335
336 // Attributes
337 protected:
338 wxVariantData* m_data;
339 wxString m_name;
340
341 private:
342 DECLARE_DYNAMIC_CLASS(wxVariant)
343 };
344
345 #define DECLARE_VARIANT_OBJECT(classname) \
346 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
347
348 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
349 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
350 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
351
352 #define IMPLEMENT_VARIANT_OBJECT(classname) \
353 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
354
355 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
356 class classname##VariantData: public wxVariantData \
357 { \
358 public:\
359 classname##VariantData() {} \
360 classname##VariantData( const classname &value ) { m_value = value; } \
361 \
362 classname &GetValue() { return m_value; } \
363 \
364 virtual bool Eq(wxVariantData& data) const; \
365 \
366 virtual wxString GetType() const; \
367 virtual wxClassInfo* GetValueClassInfo(); \
368 \
369 protected:\
370 classname m_value; \
371 };\
372 \
373 wxString classname##VariantData::GetType() const\
374 {\
375 return m_value.GetClassInfo()->GetClassName();\
376 }\
377 \
378 wxClassInfo* classname##VariantData::GetValueClassInfo()\
379 {\
380 return m_value.GetClassInfo();\
381 }\
382 \
383 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
384 {\
385 wxASSERT( variant.GetType() == #classname );\
386 \
387 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
388 value = data->GetValue();\
389 return value;\
390 }\
391 \
392 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
393 {\
394 classname##VariantData *data = new classname##VariantData( value );\
395 variant.SetData( data );\
396 return variant;\
397 }
398
399 // implements a wxVariantData-derived class using for the Eq() method the
400 // operator== which must have been provided by "classname"
401 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
402 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
403 \
404 bool classname##VariantData::Eq(wxVariantData& data) const \
405 {\
406 wxASSERT( GetType() == data.GetType() );\
407 \
408 classname##VariantData & otherData = (classname##VariantData &) data;\
409 \
410 return otherData.m_value == m_value;\
411 }\
412
413
414 // implements a wxVariantData-derived class using for the Eq() method a shallow
415 // comparison (through wxObject::IsSameAs function)
416 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
417 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
418 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
419 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
420 \
421 bool classname##VariantData::Eq(wxVariantData& data) const \
422 {\
423 wxASSERT( GetType() == data.GetType() );\
424 \
425 classname##VariantData & otherData = (classname##VariantData &) data;\
426 \
427 return (otherData.m_value.IsSameAs(m_value));\
428 }\
429
430
431 // Since we want type safety wxVariant we need to fetch and dynamic_cast
432 // in a seemingly safe way so the compiler can check, so we define
433 // a dynamic_cast /wxDynamicCast analogue.
434
435 #define wxGetVariantCast(var,classname) \
436 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
437 var.GetWxObjectPtr() : NULL));
438
439 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
440
441 #endif // wxUSE_VARIANT
442
443 #endif // _WX_VARIANT_H_