]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
Have wxPGTextCtrlEditor::UpdateControl() update wxTextCtrl font boldness based on...
[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 #include "wx/iosfwrap.h"
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 *
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 *
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
58 class WXDLLIMPEXP_BASE wxVariantData
59 {
60 friend class wxVariant;
61 public:
62 wxVariantData() : m_count(1) { }
63
64 // Override these to provide common functionality
65 virtual bool Eq(wxVariantData& data) const = 0;
66
67 #if wxUSE_STD_IOSTREAM
68 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
69 #endif
70 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
71 #if wxUSE_STD_IOSTREAM
72 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
73 #endif
74 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
75 // What type is it? Return a string name.
76 virtual wxString GetType() const = 0;
77 // If it based on wxObject return the ClassInfo.
78 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
79
80 // Implement this to make wxVariant::AllocExcusive work. Returns
81 // a copy of the data.
82 virtual wxVariantData* Clone() const { 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 // ensure that the data is exclusive to this variant, and not shared
150 bool Unshare();
151
152 // Make NULL (i.e. delete the data)
153 void MakeNull();
154
155 // Delete data and name
156 void Clear();
157
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
165 // write contents to a string (e.g. for debugging)
166 wxString MakeString() const;
167
168 // double
169 wxVariant(double val, const wxString& name = wxEmptyString);
170 bool operator== (double value) const;
171 bool operator!= (double value) const;
172 void operator= (double value) ;
173 inline operator double () const { return GetDouble(); }
174 inline double GetReal() const { return GetDouble(); }
175 double GetDouble() const;
176
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);
181 bool operator== (long value) const;
182 bool operator!= (long value) const;
183 void operator= (long value) ;
184 inline operator long () const { return GetLong(); }
185 inline long GetInteger() const { return GetLong(); }
186 long GetLong() const;
187
188 // bool
189 wxVariant(bool val, const wxString& name = wxEmptyString);
190 bool operator== (bool value) const;
191 bool operator!= (bool value) const;
192 void operator= (bool value) ;
193 inline operator bool () const { return GetBool(); }
194 bool GetBool() const ;
195
196 // wxDateTime
197 #if wxUSE_DATETIME
198 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
199 bool operator== (const wxDateTime& value) const;
200 bool operator!= (const wxDateTime& value) const;
201 void operator= (const wxDateTime& value) ;
202 inline operator wxDateTime () const { return GetDateTime(); }
203 wxDateTime GetDateTime() const;
204 #endif
205
206 // wxString
207 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
208 // these overloads are necessary to prevent the compiler from using bool
209 // version instead of wxString one:
210 wxVariant(const char* val, const wxString& name = wxEmptyString);
211 wxVariant(const wchar_t* val, const wxString& name = wxEmptyString);
212 wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString);
213 wxVariant(const wxCharBuffer& val, const wxString& name = wxEmptyString);
214 wxVariant(const wxWCharBuffer& val, const wxString& name = wxEmptyString);
215
216 bool operator== (const wxString& value) const;
217 bool operator!= (const wxString& value) const;
218 wxVariant& operator=(const wxString& value);
219 // these overloads are necessary to prevent the compiler from using bool
220 // version instead of wxString one:
221 wxVariant& operator=(const char* value)
222 { return *this = wxString(value); }
223 wxVariant& operator=(const wchar_t* value)
224 { return *this = wxString(value); }
225 wxVariant& operator=(const wxCStrData& value)
226 { return *this = value.AsString(); }
227 template<typename T>
228 wxVariant& operator=(const wxCharTypeBuffer<T>& value)
229 { return *this = value.data(); }
230
231 inline operator wxString () const { return MakeString(); }
232 wxString GetString() const;
233
234 // wxUniChar
235 wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString);
236 wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString);
237 wxVariant(char val, const wxString& name = wxEmptyString);
238 wxVariant(wchar_t val, const wxString& name = wxEmptyString);
239 bool operator==(const wxUniChar& value) const;
240 bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); }
241 bool operator==(char value) const { return *this == wxUniChar(value); }
242 bool operator==(wchar_t value) const { return *this == wxUniChar(value); }
243 bool operator!=(const wxUniChar& value) const { return !(*this == value); }
244 bool operator!=(const wxUniCharRef& value) const { return !(*this == value); }
245 bool operator!=(char value) const { return !(*this == value); }
246 bool operator!=(wchar_t value) const { return !(*this == value); }
247 wxVariant& operator=(const wxUniChar& value);
248 wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); }
249 wxVariant& operator=(char value) { return *this = wxUniChar(value); }
250 wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); }
251 operator wxUniChar() const { return GetChar(); }
252 operator char() const { return GetChar(); }
253 operator wchar_t() const { return GetChar(); }
254 wxUniChar GetChar() const;
255
256 // wxArrayString
257 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
258 bool operator== (const wxArrayString& value) const;
259 bool operator!= (const wxArrayString& value) const;
260 void operator= (const wxArrayString& value);
261 operator wxArrayString () const { return GetArrayString(); }
262 wxArrayString GetArrayString() const;
263
264 // void*
265 wxVariant(void* ptr, const wxString& name = wxEmptyString);
266 bool operator== (void* value) const;
267 bool operator!= (void* value) const;
268 void operator= (void* value);
269 operator void* () const { return GetVoidPtr(); }
270 void* GetVoidPtr() const;
271
272 // wxObject*
273 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString);
274 bool operator== (wxObject* value) const;
275 bool operator!= (wxObject* value) const;
276 void operator= (wxObject* value);
277 wxObject* GetWxObjectPtr() const;
278
279
280 // ------------------------------
281 // list operations
282 // ------------------------------
283
284 wxVariant(const wxVariantList& val, const wxString& name = wxEmptyString); // List of variants
285 bool operator== (const wxVariantList& value) const;
286 bool operator!= (const wxVariantList& value) const;
287 void operator= (const wxVariantList& value) ;
288 // Treat a list variant as an array
289 wxVariant operator[] (size_t idx) const;
290 wxVariant& operator[] (size_t idx) ;
291 wxVariantList& GetList() const ;
292
293 // Return the number of elements in a list
294 size_t GetCount() const;
295
296 // Make empty list
297 void NullList();
298
299 // Append to list
300 void Append(const wxVariant& value);
301
302 // Insert at front of list
303 void Insert(const wxVariant& value);
304
305 // Returns true if the variant is a member of the list
306 bool Member(const wxVariant& value) const;
307
308 // Deletes the nth element of the list
309 bool Delete(size_t item);
310
311 // Clear list
312 void ClearList();
313
314 public:
315 // Type conversion
316 bool Convert(long* value) const;
317 bool Convert(bool* value) const;
318 bool Convert(double* value) const;
319 bool Convert(wxString* value) const;
320 bool Convert(wxUniChar* value) const;
321 bool Convert(char* value) const;
322 bool Convert(wchar_t* value) const;
323 #if wxUSE_DATETIME
324 bool Convert(wxDateTime* value) const;
325 #endif // wxUSE_DATETIME
326
327 // Attributes
328 protected:
329 wxVariantData* m_data;
330 wxString m_name;
331
332 private:
333 DECLARE_DYNAMIC_CLASS(wxVariant)
334 };
335
336 #define DECLARE_VARIANT_OBJECT(classname) \
337 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
338
339 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
340 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
341 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
342
343 #define IMPLEMENT_VARIANT_OBJECT(classname) \
344 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
345
346 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
347 class classname##VariantData: public wxVariantData \
348 { \
349 public:\
350 classname##VariantData() {} \
351 classname##VariantData( const classname &value ) { m_value = value; } \
352 \
353 classname &GetValue() { return m_value; } \
354 \
355 virtual bool Eq(wxVariantData& data) const; \
356 \
357 virtual wxString GetType() const; \
358 virtual wxClassInfo* GetValueClassInfo(); \
359 \
360 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
361 \
362 protected:\
363 classname m_value; \
364 };\
365 \
366 wxString classname##VariantData::GetType() const\
367 {\
368 return m_value.GetClassInfo()->GetClassName();\
369 }\
370 \
371 wxClassInfo* classname##VariantData::GetValueClassInfo()\
372 {\
373 return m_value.GetClassInfo();\
374 }\
375 \
376 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
377 {\
378 wxASSERT( variant.GetType() == #classname );\
379 \
380 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
381 value = data->GetValue();\
382 return value;\
383 }\
384 \
385 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
386 {\
387 classname##VariantData *data = new classname##VariantData( value );\
388 variant.SetData( data );\
389 return variant;\
390 }
391
392 // implements a wxVariantData-derived class using for the Eq() method the
393 // operator== which must have been provided by "classname"
394 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
395 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
396 \
397 bool classname##VariantData::Eq(wxVariantData& data) const \
398 {\
399 wxASSERT( GetType() == data.GetType() );\
400 \
401 classname##VariantData & otherData = (classname##VariantData &) data;\
402 \
403 return otherData.m_value == m_value;\
404 }\
405
406
407 // implements a wxVariantData-derived class using for the Eq() method a shallow
408 // comparison (through wxObject::IsSameAs function)
409 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
410 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
411 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
412 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
413 \
414 bool classname##VariantData::Eq(wxVariantData& data) const \
415 {\
416 wxASSERT( GetType() == data.GetType() );\
417 \
418 classname##VariantData & otherData = (classname##VariantData &) data;\
419 \
420 return (otherData.m_value.IsSameAs(m_value));\
421 }\
422
423
424 // Since we want type safety wxVariant we need to fetch and dynamic_cast
425 // in a seemingly safe way so the compiler can check, so we define
426 // a dynamic_cast /wxDynamicCast analogue.
427
428 #define wxGetVariantCast(var,classname) \
429 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
430 var.GetWxObjectPtr() : NULL));
431
432 // Replacement for using wxDynamicCast on a wxVariantData object
433 #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
434
435 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
436
437 #endif // wxUSE_VARIANT
438
439 #endif // _WX_VARIANT_H_