]>
Commit | Line | Data |
---|---|---|
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 | // Implement this to make wxVariant::AllocExcusive work. Returns | |
85 | // a copy of the data. | |
86 | virtual wxVariantData* Clone() const { return NULL; } | |
87 | ||
88 | void IncRef() { m_count++; } | |
89 | void DecRef() | |
90 | { | |
91 | if ( --m_count == 0 ) | |
92 | delete this; | |
93 | } | |
94 | ||
95 | int GetRefCount() const { return m_count; } | |
96 | ||
97 | protected: | |
98 | // Protected dtor should make some incompatible code | |
99 | // break more louder. That is, they should do data->DecRef() | |
100 | // instead of delete data. | |
101 | virtual ~wxVariantData() { } | |
102 | ||
103 | private: | |
104 | int m_count; | |
105 | }; | |
106 | ||
107 | /* | |
108 | * wxVariant can store any kind of data, but has some basic types | |
109 | * built in. | |
110 | */ | |
111 | ||
112 | class WXDLLIMPEXP_FWD_BASE wxVariant; | |
113 | ||
114 | WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE); | |
115 | ||
116 | class WXDLLIMPEXP_BASE wxVariant: public wxObject | |
117 | { | |
118 | public: | |
119 | wxVariant(); | |
120 | ||
121 | wxVariant(const wxVariant& variant); | |
122 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); | |
123 | virtual ~wxVariant(); | |
124 | ||
125 | // generic assignment | |
126 | void operator= (const wxVariant& variant); | |
127 | ||
128 | // Assignment using data, e.g. | |
129 | // myVariant = new wxStringVariantData("hello"); | |
130 | void operator= (wxVariantData* variantData); | |
131 | ||
132 | bool operator== (const wxVariant& variant) const; | |
133 | bool operator!= (const wxVariant& variant) const; | |
134 | ||
135 | // Sets/gets name | |
136 | inline void SetName(const wxString& name) { m_name = name; } | |
137 | inline const wxString& GetName() const { return m_name; } | |
138 | ||
139 | // Tests whether there is data | |
140 | bool IsNull() const; | |
141 | ||
142 | // For compatibility with wxWidgets <= 2.6, this doesn't increase | |
143 | // reference count. | |
144 | wxVariantData* GetData() const { return m_data; } | |
145 | void SetData(wxVariantData* data) ; | |
146 | ||
147 | // make a 'clone' of the object | |
148 | void Ref(const wxVariant& clone); | |
149 | ||
150 | // destroy a reference | |
151 | void UnRef(); | |
152 | ||
153 | // ensure that the data is exclusive to this variant, and not shared | |
154 | bool Unshare(); | |
155 | ||
156 | // Make NULL (i.e. delete the data) | |
157 | void MakeNull(); | |
158 | ||
159 | // Delete data and name | |
160 | void Clear(); | |
161 | ||
162 | // Returns a string representing the type of the variant, | |
163 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
164 | wxString GetType() const; | |
165 | ||
166 | bool IsType(const wxString& type) const; | |
167 | bool IsValueKindOf(const wxClassInfo* type) const; | |
168 | ||
169 | // write contents to a string (e.g. for debugging) | |
170 | wxString MakeString() const; | |
171 | ||
172 | // double | |
173 | wxVariant(double val, const wxString& name = wxEmptyString); | |
174 | bool operator== (double value) const; | |
175 | bool operator!= (double value) const; | |
176 | void operator= (double value) ; | |
177 | inline operator double () const { return GetDouble(); } | |
178 | inline double GetReal() const { return GetDouble(); } | |
179 | double GetDouble() const; | |
180 | ||
181 | // long | |
182 | wxVariant(long val, const wxString& name = wxEmptyString); | |
183 | wxVariant(int val, const wxString& name = wxEmptyString); | |
184 | wxVariant(short val, const wxString& name = wxEmptyString); | |
185 | bool operator== (long value) const; | |
186 | bool operator!= (long value) const; | |
187 | void operator= (long value) ; | |
188 | inline operator long () const { return GetLong(); } | |
189 | inline long GetInteger() const { return GetLong(); } | |
190 | long GetLong() const; | |
191 | ||
192 | // bool | |
193 | #ifdef HAVE_BOOL | |
194 | wxVariant(bool val, const wxString& name = wxEmptyString); | |
195 | bool operator== (bool value) const; | |
196 | bool operator!= (bool value) const; | |
197 | void operator= (bool value) ; | |
198 | inline operator bool () const { return GetBool(); } | |
199 | bool GetBool() const ; | |
200 | #endif | |
201 | ||
202 | // wxDateTime | |
203 | #if wxUSE_DATETIME | |
204 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); | |
205 | #if wxUSE_ODBC | |
206 | wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); | |
207 | wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); | |
208 | wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); | |
209 | #endif | |
210 | bool operator== (const wxDateTime& value) const; | |
211 | bool operator!= (const wxDateTime& value) const; | |
212 | void operator= (const wxDateTime& value) ; | |
213 | #if wxUSE_ODBC | |
214 | void operator= (const DATE_STRUCT* value) ; | |
215 | void operator= (const TIME_STRUCT* value) ; | |
216 | void operator= (const TIMESTAMP_STRUCT* value) ; | |
217 | #endif | |
218 | inline operator wxDateTime () const { return GetDateTime(); } | |
219 | wxDateTime GetDateTime() const; | |
220 | #endif | |
221 | ||
222 | // wxString | |
223 | wxVariant(const wxString& val, const wxString& name = wxEmptyString); | |
224 | // these overloads are necessary to prevent the compiler from using bool | |
225 | // version instead of wxString one: | |
226 | wxVariant(const char* val, const wxString& name = wxEmptyString); | |
227 | wxVariant(const wchar_t* val, const wxString& name = wxEmptyString); | |
228 | wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString); | |
229 | wxVariant(const wxCharBuffer& val, const wxString& name = wxEmptyString); | |
230 | wxVariant(const wxWCharBuffer& val, const wxString& name = wxEmptyString); | |
231 | ||
232 | bool operator== (const wxString& value) const; | |
233 | bool operator!= (const wxString& value) const; | |
234 | wxVariant& operator=(const wxString& value); | |
235 | // these overloads are necessary to prevent the compiler from using bool | |
236 | // version instead of wxString one: | |
237 | wxVariant& operator=(const char* value) | |
238 | { return *this = wxString(value); } | |
239 | wxVariant& operator=(const wchar_t* value) | |
240 | { return *this = wxString(value); } | |
241 | wxVariant& operator=(const wxCStrData& value) | |
242 | { return *this = value.AsString(); } | |
243 | template<typename T> | |
244 | wxVariant& operator=(const wxCharTypeBuffer<T>& value) | |
245 | { return *this = value.data(); } | |
246 | ||
247 | inline operator wxString () const { return MakeString(); } | |
248 | wxString GetString() const; | |
249 | ||
250 | // wxUniChar | |
251 | wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString); | |
252 | wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString); | |
253 | wxVariant(char val, const wxString& name = wxEmptyString); | |
254 | wxVariant(wchar_t val, const wxString& name = wxEmptyString); | |
255 | bool operator==(const wxUniChar& value) const; | |
256 | bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); } | |
257 | bool operator==(char value) const { return *this == wxUniChar(value); } | |
258 | bool operator==(wchar_t value) const { return *this == wxUniChar(value); } | |
259 | bool operator!=(const wxUniChar& value) const { return !(*this == value); } | |
260 | bool operator!=(const wxUniCharRef& value) const { return !(*this == value); } | |
261 | bool operator!=(char value) const { return !(*this == value); } | |
262 | bool operator!=(wchar_t value) const { return !(*this == value); } | |
263 | wxVariant& operator=(const wxUniChar& value); | |
264 | wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); } | |
265 | wxVariant& operator=(char value) { return *this = wxUniChar(value); } | |
266 | wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); } | |
267 | operator wxUniChar() const { return GetChar(); } | |
268 | operator char() const { return GetChar(); } | |
269 | operator wchar_t() const { return GetChar(); } | |
270 | wxUniChar GetChar() const; | |
271 | ||
272 | // wxArrayString | |
273 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); | |
274 | bool operator== (const wxArrayString& value) const; | |
275 | bool operator!= (const wxArrayString& value) const; | |
276 | void operator= (const wxArrayString& value); | |
277 | operator wxArrayString () const { return GetArrayString(); } | |
278 | wxArrayString GetArrayString() const; | |
279 | ||
280 | // void* | |
281 | wxVariant(void* ptr, const wxString& name = wxEmptyString); | |
282 | bool operator== (void* value) const; | |
283 | bool operator!= (void* value) const; | |
284 | void operator= (void* value); | |
285 | operator void* () const { return GetVoidPtr(); } | |
286 | void* GetVoidPtr() const; | |
287 | ||
288 | // wxObject* | |
289 | wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); | |
290 | bool operator== (wxObject* value) const; | |
291 | bool operator!= (wxObject* value) const; | |
292 | void operator= (wxObject* value); | |
293 | wxObject* GetWxObjectPtr() const; | |
294 | ||
295 | ||
296 | // ------------------------------ | |
297 | // list operations | |
298 | // ------------------------------ | |
299 | ||
300 | wxVariant(const wxVariantList& val, const wxString& name = wxEmptyString); // List of variants | |
301 | bool operator== (const wxVariantList& value) const; | |
302 | bool operator!= (const wxVariantList& value) const; | |
303 | void operator= (const wxVariantList& value) ; | |
304 | // Treat a list variant as an array | |
305 | wxVariant operator[] (size_t idx) const; | |
306 | wxVariant& operator[] (size_t idx) ; | |
307 | wxVariantList& GetList() const ; | |
308 | ||
309 | // Return the number of elements in a list | |
310 | size_t GetCount() const; | |
311 | ||
312 | // Make empty list | |
313 | void NullList(); | |
314 | ||
315 | // Append to list | |
316 | void Append(const wxVariant& value); | |
317 | ||
318 | // Insert at front of list | |
319 | void Insert(const wxVariant& value); | |
320 | ||
321 | // Returns true if the variant is a member of the list | |
322 | bool Member(const wxVariant& value) const; | |
323 | ||
324 | // Deletes the nth element of the list | |
325 | bool Delete(size_t item); | |
326 | ||
327 | // Clear list | |
328 | void ClearList(); | |
329 | ||
330 | public: | |
331 | // Type conversion | |
332 | bool Convert(long* value) const; | |
333 | bool Convert(bool* value) const; | |
334 | bool Convert(double* value) const; | |
335 | bool Convert(wxString* value) const; | |
336 | bool Convert(wxUniChar* value) const; | |
337 | bool Convert(char* value) const; | |
338 | bool Convert(wchar_t* value) const; | |
339 | #if wxUSE_DATETIME | |
340 | bool Convert(wxDateTime* value) const; | |
341 | #endif // wxUSE_DATETIME | |
342 | ||
343 | // Attributes | |
344 | protected: | |
345 | wxVariantData* m_data; | |
346 | wxString m_name; | |
347 | ||
348 | private: | |
349 | DECLARE_DYNAMIC_CLASS(wxVariant) | |
350 | }; | |
351 | ||
352 | #define DECLARE_VARIANT_OBJECT(classname) \ | |
353 | DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) | |
354 | ||
355 | #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \ | |
356 | expdecl classname& operator << ( classname &object, const wxVariant &variant ); \ | |
357 | expdecl wxVariant& operator << ( wxVariant &variant, const classname &object ); | |
358 | ||
359 | #define IMPLEMENT_VARIANT_OBJECT(classname) \ | |
360 | IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) | |
361 | ||
362 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \ | |
363 | class classname##VariantData: public wxVariantData \ | |
364 | { \ | |
365 | public:\ | |
366 | classname##VariantData() {} \ | |
367 | classname##VariantData( const classname &value ) { m_value = value; } \ | |
368 | \ | |
369 | classname &GetValue() { return m_value; } \ | |
370 | \ | |
371 | virtual bool Eq(wxVariantData& data) const; \ | |
372 | \ | |
373 | virtual wxString GetType() const; \ | |
374 | virtual wxClassInfo* GetValueClassInfo(); \ | |
375 | \ | |
376 | virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \ | |
377 | \ | |
378 | protected:\ | |
379 | classname m_value; \ | |
380 | };\ | |
381 | \ | |
382 | wxString classname##VariantData::GetType() const\ | |
383 | {\ | |
384 | return m_value.GetClassInfo()->GetClassName();\ | |
385 | }\ | |
386 | \ | |
387 | wxClassInfo* classname##VariantData::GetValueClassInfo()\ | |
388 | {\ | |
389 | return m_value.GetClassInfo();\ | |
390 | }\ | |
391 | \ | |
392 | expdecl classname& operator << ( classname &value, const wxVariant &variant )\ | |
393 | {\ | |
394 | wxASSERT( variant.GetType() == #classname );\ | |
395 | \ | |
396 | classname##VariantData *data = (classname##VariantData*) variant.GetData();\ | |
397 | value = data->GetValue();\ | |
398 | return value;\ | |
399 | }\ | |
400 | \ | |
401 | expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\ | |
402 | {\ | |
403 | classname##VariantData *data = new classname##VariantData( value );\ | |
404 | variant.SetData( data );\ | |
405 | return variant;\ | |
406 | } | |
407 | ||
408 | // implements a wxVariantData-derived class using for the Eq() method the | |
409 | // operator== which must have been provided by "classname" | |
410 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \ | |
411 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \ | |
412 | \ | |
413 | bool classname##VariantData::Eq(wxVariantData& data) const \ | |
414 | {\ | |
415 | wxASSERT( GetType() == data.GetType() );\ | |
416 | \ | |
417 | classname##VariantData & otherData = (classname##VariantData &) data;\ | |
418 | \ | |
419 | return otherData.m_value == m_value;\ | |
420 | }\ | |
421 | ||
422 | ||
423 | // implements a wxVariantData-derived class using for the Eq() method a shallow | |
424 | // comparison (through wxObject::IsSameAs function) | |
425 | #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \ | |
426 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE) | |
427 | #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \ | |
428 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \ | |
429 | \ | |
430 | bool classname##VariantData::Eq(wxVariantData& data) const \ | |
431 | {\ | |
432 | wxASSERT( GetType() == data.GetType() );\ | |
433 | \ | |
434 | classname##VariantData & otherData = (classname##VariantData &) data;\ | |
435 | \ | |
436 | return (otherData.m_value.IsSameAs(m_value));\ | |
437 | }\ | |
438 | ||
439 | ||
440 | // Since we want type safety wxVariant we need to fetch and dynamic_cast | |
441 | // in a seemingly safe way so the compiler can check, so we define | |
442 | // a dynamic_cast /wxDynamicCast analogue. | |
443 | ||
444 | #define wxGetVariantCast(var,classname) \ | |
445 | ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\ | |
446 | var.GetWxObjectPtr() : NULL)); | |
447 | ||
448 | // Replacement for using wxDynamicCast on a wxVariantData object | |
449 | #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data) | |
450 | ||
451 | extern wxVariant WXDLLIMPEXP_BASE wxNullVariant; | |
452 | ||
453 | #endif // wxUSE_VARIANT | |
454 | ||
455 | #endif // _WX_VARIANT_H_ |