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