]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
Use wxWebKitCtrlNameStr to adhere to the convention used by other wx classes.
[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 #include "wx/longlong.h"
25
26 #if wxUSE_DATETIME
27 #include "wx/datetime.h"
28 #endif // wxUSE_DATETIME
29
30 #include "wx/iosfwrap.h"
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 *
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 *
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
55 class WXDLLIMPEXP_BASE wxVariantData : public wxObjectRefData
56 {
57 friend class wxVariant;
58 public:
59 wxVariantData() { }
60
61 // Override these to provide common functionality
62 virtual bool Eq(wxVariantData& data) const = 0;
63
64 #if wxUSE_STD_IOSTREAM
65 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
66 #endif
67 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
68 #if wxUSE_STD_IOSTREAM
69 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
70 #endif
71 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
72 // What type is it? Return a string name.
73 virtual wxString GetType() const = 0;
74 // If it based on wxObject return the ClassInfo.
75 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
76
77 // Implement this to make wxVariant::UnShare work. Returns
78 // a copy of the data.
79 virtual wxVariantData* Clone() const { return NULL; }
80
81 protected:
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() { }
86 };
87
88 /*
89 * wxVariant can store any kind of data, but has some basic types
90 * built in.
91 */
92
93 class WXDLLIMPEXP_FWD_BASE wxVariant;
94
95 WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE);
96
97 class WXDLLIMPEXP_BASE wxVariant: public wxObject
98 {
99 public:
100 wxVariant();
101
102 wxVariant(const wxVariant& variant);
103 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
104 virtual ~wxVariant();
105
106 // generic assignment
107 void operator= (const wxVariant& variant);
108
109 // Assignment using data, e.g.
110 // myVariant = new wxStringVariantData("hello");
111 void operator= (wxVariantData* variantData);
112
113 bool operator== (const wxVariant& variant) const;
114 bool operator!= (const wxVariant& variant) const;
115
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
121 bool IsNull() const;
122
123 // For compatibility with wxWidgets <= 2.6, this doesn't increase
124 // reference count.
125 wxVariantData* GetData() const
126 {
127 return (wxVariantData*) m_refData;
128 }
129 void SetData(wxVariantData* data) ;
130
131 // make a 'clone' of the object
132 void Ref(const wxVariant& clone) { wxObject::Ref(clone); }
133
134 // ensure that the data is exclusive to this variant, and not shared
135 bool Unshare();
136
137 // Make NULL (i.e. delete the data)
138 void MakeNull();
139
140 // Delete data and name
141 void Clear();
142
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
150 // write contents to a string (e.g. for debugging)
151 wxString MakeString() const;
152
153 // double
154 wxVariant(double val, const wxString& name = wxEmptyString);
155 bool operator== (double value) const;
156 bool operator!= (double value) const;
157 void operator= (double value) ;
158 inline operator double () const { return GetDouble(); }
159 inline double GetReal() const { return GetDouble(); }
160 double GetDouble() const;
161
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);
166 bool operator== (long value) const;
167 bool operator!= (long value) const;
168 void operator= (long value) ;
169 inline operator long () const { return GetLong(); }
170 inline long GetInteger() const { return GetLong(); }
171 long GetLong() const;
172
173 // bool
174 wxVariant(bool val, const wxString& name = wxEmptyString);
175 bool operator== (bool value) const;
176 bool operator!= (bool value) const;
177 void operator= (bool value) ;
178 inline operator bool () const { return GetBool(); }
179 bool GetBool() const ;
180
181 // wxDateTime
182 #if wxUSE_DATETIME
183 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
184 bool operator== (const wxDateTime& value) const;
185 bool operator!= (const wxDateTime& value) const;
186 void operator= (const wxDateTime& value) ;
187 inline operator wxDateTime () const { return GetDateTime(); }
188 wxDateTime GetDateTime() const;
189 #endif
190
191 // wxString
192 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
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);
198 wxVariant(const wxScopedCharBuffer& val, const wxString& name = wxEmptyString);
199 wxVariant(const wxScopedWCharBuffer& val, const wxString& name = wxEmptyString);
200
201 bool operator== (const wxString& value) const;
202 bool operator!= (const wxString& value) const;
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>
213 wxVariant& operator=(const wxScopedCharTypeBuffer<T>& value)
214 { return *this = value.data(); }
215
216 inline operator wxString () const { return MakeString(); }
217 wxString GetString() const;
218
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;
240
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);
246 operator wxArrayString () const { return GetArrayString(); }
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);
254 operator void* () const { return GetVoidPtr(); }
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
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
281
282 // ------------------------------
283 // list operations
284 // ------------------------------
285
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) ;
290 // Treat a list variant as an array
291 wxVariant operator[] (size_t idx) const;
292 wxVariant& operator[] (size_t idx) ;
293 wxVariantList& GetList() const ;
294
295 // Return the number of elements in a list
296 size_t GetCount() const;
297
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
307 // Returns true if the variant is a member of the list
308 bool Member(const wxVariant& value) const;
309
310 // Deletes the nth element of the list
311 bool Delete(size_t item);
312
313 // Clear list
314 void ClearList();
315
316 public:
317 // Type conversion
318 bool Convert(long* value) const;
319 bool Convert(bool* value) const;
320 bool Convert(double* value) const;
321 bool Convert(wxString* value) const;
322 bool Convert(wxUniChar* value) const;
323 bool Convert(char* value) const;
324 bool Convert(wchar_t* value) const;
325 #if wxUSE_DATETIME
326 bool Convert(wxDateTime* value) const;
327 #endif // wxUSE_DATETIME
328 #if wxUSE_LONGLONG
329 bool Convert(wxLongLong* value) const;
330 bool Convert(wxULongLong* value) const;
331 #endif // wxUSE_LONGLONG
332
333 // Attributes
334 protected:
335 virtual wxObjectRefData *CreateRefData() const;
336 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
337
338 wxString m_name;
339
340 private:
341 DECLARE_DYNAMIC_CLASS(wxVariant)
342 };
343
344 #define DECLARE_VARIANT_OBJECT(classname) \
345 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
346
347 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
348 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
349 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
350
351 #define IMPLEMENT_VARIANT_OBJECT(classname) \
352 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
353
354 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
355 class classname##VariantData: public wxVariantData \
356 { \
357 public:\
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(); \
367 \
368 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
369 \
370 protected:\
371 classname m_value; \
372 };\
373 \
374 wxString classname##VariantData::GetType() const\
375 {\
376 return m_value.GetClassInfo()->GetClassName();\
377 }\
378 \
379 wxClassInfo* classname##VariantData::GetValueClassInfo()\
380 {\
381 return m_value.GetClassInfo();\
382 }\
383 \
384 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
385 {\
386 wxASSERT( variant.GetType() == #classname );\
387 \
388 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
389 value = data->GetValue();\
390 return value;\
391 }\
392 \
393 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
394 {\
395 classname##VariantData *data = new classname##VariantData( value );\
396 variant.SetData( data );\
397 return variant;\
398 }
399
400 // implements a wxVariantData-derived class using for the Eq() method the
401 // operator== which must have been provided by "classname"
402 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
403 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
404 \
405 bool classname##VariantData::Eq(wxVariantData& data) const \
406 {\
407 wxASSERT( GetType() == data.GetType() );\
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
416 // comparison (through wxObject::IsSameAs function)
417 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
418 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
419 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
420 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
421 \
422 bool classname##VariantData::Eq(wxVariantData& data) const \
423 {\
424 wxASSERT( GetType() == data.GetType() );\
425 \
426 classname##VariantData & otherData = (classname##VariantData &) data;\
427 \
428 return (otherData.m_value.IsSameAs(m_value));\
429 }\
430
431
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.
435
436 #define wxGetVariantCast(var,classname) \
437 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
438 var.GetWxObjectPtr() : NULL));
439
440 // Replacement for using wxDynamicCast on a wxVariantData object
441 #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
442
443 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
444
445 #endif // wxUSE_VARIANT
446
447 #endif // _WX_VARIANT_H_