]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
Fixed return values
[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
24 #if wxUSE_DATETIME
25 #include "wx/datetime.h"
26 #endif // wxUSE_DATETIME
27
28 #if wxUSE_ODBC
29 #include "wx/db.h" // will #include sqltypes.h
30 #endif //ODBC
31
32 #include "wx/iosfwrap.h"
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 *
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 *
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
61 class WXDLLIMPEXP_BASE wxVariantData: public wxObject
62 {
63 friend class wxVariant;
64 public:
65 wxVariantData()
66 : wxObject(), m_count(1)
67 { }
68
69 // Override these to provide common functionality
70 virtual bool Eq(wxVariantData& data) const = 0;
71
72 #if wxUSE_STD_IOSTREAM
73 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
74 #endif
75 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
76 #if wxUSE_STD_IOSTREAM
77 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
78 #endif
79 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
80 // What type is it? Return a string name.
81 virtual wxString GetType() const = 0;
82 // If it based on wxObject return the ClassInfo.
83 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
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
94 protected:
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
100 private:
101 int m_count;
102
103 private:
104 DECLARE_ABSTRACT_CLASS(wxVariantData)
105 };
106
107 /*
108 * wxVariant can store any kind of data, but has some basic types
109 * built in.
110 */
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 wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool!
218 bool operator== (const wxString& value) const;
219 bool operator!= (const wxString& value) const;
220 void operator= (const wxString& value) ;
221 void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
222 inline operator wxString () const { return MakeString(); }
223 wxString GetString() const;
224
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 ;
232
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
257 #if WXWIN_COMPATIBILITY_2_4
258 wxDEPRECATED( wxVariant(const wxStringList& val, const wxString& name = wxEmptyString) );
259 wxDEPRECATED( bool operator== (const wxStringList& value) const );
260 wxDEPRECATED( bool operator!= (const wxStringList& value) const );
261 wxDEPRECATED( void operator= (const wxStringList& value) );
262 wxDEPRECATED( wxStringList& GetStringList() const );
263 #endif
264
265 // ------------------------------
266 // list operations
267 // ------------------------------
268
269 wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants
270 bool operator== (const wxList& value) const;
271 bool operator!= (const wxList& value) const;
272 void operator= (const wxList& value) ;
273 // Treat a list variant as an array
274 wxVariant operator[] (size_t idx) const;
275 wxVariant& operator[] (size_t idx) ;
276 wxList& GetList() const ;
277
278 // Return the number of elements in a list
279 size_t GetCount() const;
280
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
290 // Returns true if the variant is a member of the list
291 bool Member(const wxVariant& value) const;
292
293 // Deletes the nth element of the list
294 bool Delete(size_t item);
295
296 // Clear list
297 void ClearList();
298
299 public:
300 // Type conversion
301 bool Convert(long* value) const;
302 bool Convert(bool* value) const;
303 bool Convert(double* value) const;
304 bool Convert(wxString* value) const;
305 bool Convert(wxChar* value) const;
306 #if wxUSE_DATETIME
307 bool Convert(wxDateTime* value) const;
308 #endif // wxUSE_DATETIME
309
310 // Attributes
311 protected:
312 wxVariantData* m_data;
313 wxString m_name;
314
315 private:
316 DECLARE_DYNAMIC_CLASS(wxVariant)
317 };
318
319 /* Fake macro parameter value */
320 #ifdef EMPTY_PARAMETER_VALUE
321 #undef EMPTY_PARAMETER_VALUE
322 #endif
323 #define EMPTY_PARAMETER_VALUE
324
325 #define DECLARE_VARIANT_OBJECT(classname) \
326 DECLARE_VARIANT_OBJECT_EXPORTED(classname,EMPTY_PARAMETER_VALUE)
327
328 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
329 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
330 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
331
332 #define IMPLEMENT_VARIANT_OBJECT(classname) \
333 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,EMPTY_PARAMETER_VALUE)
334
335 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
336 class classname##VariantData: public wxVariantData \
337 { \
338 public:\
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 \
349 protected:\
350 classname m_value; \
351 \
352 private: \
353 DECLARE_CLASS(classname##VariantData) \
354 };\
355 \
356 IMPLEMENT_CLASS(classname##VariantData, wxVariantData)\
357 \
358 bool 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 \
367 wxString classname##VariantData::GetType() const\
368 {\
369 return m_value.GetClassInfo()->GetClassName();\
370 }\
371 \
372 wxClassInfo* classname##VariantData::GetValueClassInfo()\
373 {\
374 return m_value.GetClassInfo();\
375 }\
376 \
377 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
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 \
386 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
387 {\
388 classname##VariantData *data = new classname##VariantData( value );\
389 variant.SetData( data );\
390 return variant;\
391 }
392
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.
396
397 #define wxGetVariantCast(var,classname) \
398 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
399 var.GetWxObjectPtr() : NULL));
400
401 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
402
403 #endif // wxUSE_VARIANT
404
405 #endif // _WX_VARIANT_H_