]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
Use wmain() if supported by the compiler/CRT instead of main().
[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 class wxAny;
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: When you construct a wxVariantData, it will have refcount
40 * of one. Refcount will not be further increased when
41 * it is passed to wxVariant. This simulates old common
42 * scenario where wxVariant took ownership of wxVariantData
43 * passed to it.
44 * If you create wxVariantData for other reasons than passing
45 * it to wxVariant, technically you are not required to call
46 * DecRef() before deleting it.
47 *
48 * TODO: in order to replace wxPropertyValue, we would need
49 * to consider adding constructors that take pointers to C++ variables,
50 * or removing that functionality from the wxProperty library.
51 * Essentially wxPropertyValue takes on some of the wxValidator functionality
52 * by storing pointers and not just actual values, allowing update of C++ data
53 * to be handled automatically. Perhaps there's another way of doing this without
54 * overloading wxVariant with unnecessary functionality.
55 */
56
57 class WXDLLIMPEXP_BASE wxVariantData : public wxObjectRefData
58 {
59 friend class wxVariant;
60 public:
61 wxVariantData() { }
62
63 // Override these to provide common functionality
64 virtual bool Eq(wxVariantData& data) const = 0;
65
66 #if wxUSE_STD_IOSTREAM
67 virtual bool Write(wxSTD ostream& WXUNUSED(str)) const { return false; }
68 #endif
69 virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
70 #if wxUSE_STD_IOSTREAM
71 virtual bool Read(wxSTD istream& WXUNUSED(str)) { return false; }
72 #endif
73 virtual bool Read(wxString& WXUNUSED(str)) { return false; }
74 // What type is it? Return a string name.
75 virtual wxString GetType() const = 0;
76 // If it based on wxObject return the ClassInfo.
77 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
78
79 // Implement this to make wxVariant::UnShare work. Returns
80 // a copy of the data.
81 virtual wxVariantData* Clone() const { return NULL; }
82
83 #if wxUSE_ANY
84 // Converts value to wxAny, if possible. Return true if successful.
85 virtual bool GetAsAny(wxAny* WXUNUSED(any)) const { return false; }
86 #endif
87
88 protected:
89 // Protected dtor should make some incompatible code
90 // break more louder. That is, they should do data->DecRef()
91 // instead of delete data.
92 virtual ~wxVariantData() { }
93 };
94
95 /*
96 * wxVariant can store any kind of data, but has some basic types
97 * built in.
98 */
99
100 class WXDLLIMPEXP_FWD_BASE wxVariant;
101
102 WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE);
103
104 class WXDLLIMPEXP_BASE wxVariant: public wxObject
105 {
106 public:
107 wxVariant();
108
109 wxVariant(const wxVariant& variant);
110 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
111 #if wxUSE_ANY
112 wxVariant(const wxAny& any);
113 #endif
114 virtual ~wxVariant();
115
116 // generic assignment
117 void operator= (const wxVariant& variant);
118
119 // Assignment using data, e.g.
120 // myVariant = new wxStringVariantData("hello");
121 void operator= (wxVariantData* variantData);
122
123 bool operator== (const wxVariant& variant) const;
124 bool operator!= (const wxVariant& variant) const;
125
126 // Sets/gets name
127 inline void SetName(const wxString& name) { m_name = name; }
128 inline const wxString& GetName() const { return m_name; }
129
130 // Tests whether there is data
131 bool IsNull() const;
132
133 // For compatibility with wxWidgets <= 2.6, this doesn't increase
134 // reference count.
135 wxVariantData* GetData() const
136 {
137 return (wxVariantData*) m_refData;
138 }
139 void SetData(wxVariantData* data) ;
140
141 // make a 'clone' of the object
142 void Ref(const wxVariant& clone) { wxObject::Ref(clone); }
143
144 // ensure that the data is exclusive to this variant, and not shared
145 bool Unshare();
146
147 // Make NULL (i.e. delete the data)
148 void MakeNull();
149
150 // Delete data and name
151 void Clear();
152
153 // Returns a string representing the type of the variant,
154 // e.g. "string", "bool", "stringlist", "list", "double", "long"
155 wxString GetType() const;
156
157 bool IsType(const wxString& type) const;
158 bool IsValueKindOf(const wxClassInfo* type) const;
159
160 // write contents to a string (e.g. for debugging)
161 wxString MakeString() const;
162
163 #if wxUSE_ANY
164 wxAny GetAny() const;
165 #endif
166
167 // double
168 wxVariant(double val, const wxString& name = wxEmptyString);
169 bool operator== (double value) const;
170 bool operator!= (double value) const;
171 void operator= (double value) ;
172 inline operator double () const { return GetDouble(); }
173 inline double GetReal() const { return GetDouble(); }
174 double GetDouble() const;
175
176 // long
177 wxVariant(long val, const wxString& name = wxEmptyString);
178 wxVariant(int val, const wxString& name = wxEmptyString);
179 wxVariant(short val, const wxString& name = wxEmptyString);
180 bool operator== (long value) const;
181 bool operator!= (long value) const;
182 void operator= (long value) ;
183 inline operator long () const { return GetLong(); }
184 inline long GetInteger() const { return GetLong(); }
185 long GetLong() const;
186
187 // 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
195 // wxDateTime
196 #if wxUSE_DATETIME
197 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
198 bool operator== (const wxDateTime& value) const;
199 bool operator!= (const wxDateTime& value) const;
200 void operator= (const wxDateTime& value) ;
201 inline operator wxDateTime () const { return GetDateTime(); }
202 wxDateTime GetDateTime() const;
203 #endif
204
205 // wxString
206 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
207 // these overloads are necessary to prevent the compiler from using bool
208 // version instead of wxString one:
209 wxVariant(const char* val, const wxString& name = wxEmptyString);
210 wxVariant(const wchar_t* val, const wxString& name = wxEmptyString);
211 wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString);
212 wxVariant(const wxScopedCharBuffer& val, const wxString& name = wxEmptyString);
213 wxVariant(const wxScopedWCharBuffer& val, const wxString& name = wxEmptyString);
214
215 bool operator== (const wxString& value) const;
216 bool operator!= (const wxString& value) const;
217 wxVariant& operator=(const wxString& value);
218 // these overloads are necessary to prevent the compiler from using bool
219 // version instead of wxString one:
220 wxVariant& operator=(const char* value)
221 { return *this = wxString(value); }
222 wxVariant& operator=(const wchar_t* value)
223 { return *this = wxString(value); }
224 wxVariant& operator=(const wxCStrData& value)
225 { return *this = value.AsString(); }
226 template<typename T>
227 wxVariant& operator=(const wxScopedCharTypeBuffer<T>& value)
228 { return *this = value.data(); }
229
230 inline operator wxString () const { return MakeString(); }
231 wxString GetString() const;
232
233 #if wxUSE_STD_STRING
234 wxVariant(const std::string& val, const wxString& name = wxEmptyString);
235 bool operator==(const std::string& value) const
236 { return operator==(wxString(value)); }
237 bool operator!=(const std::string& value) const
238 { return operator!=(wxString(value)); }
239 wxVariant& operator=(const std::string& value)
240 { return operator=(wxString(value)); }
241 operator std::string() const { return (operator wxString()).ToStdString(); }
242
243 wxVariant(const wxStdWideString& val, const wxString& name = wxEmptyString);
244 bool operator==(const wxStdWideString& value) const
245 { return operator==(wxString(value)); }
246 bool operator!=(const wxStdWideString& value) const
247 { return operator!=(wxString(value)); }
248 wxVariant& operator=(const wxStdWideString& value)
249 { return operator=(wxString(value)); }
250 operator wxStdWideString() const { return (operator wxString()).ToStdWstring(); }
251 #endif // wxUSE_STD_STRING
252
253 // wxUniChar
254 wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString);
255 wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString);
256 wxVariant(char val, const wxString& name = wxEmptyString);
257 wxVariant(wchar_t val, const wxString& name = wxEmptyString);
258 bool operator==(const wxUniChar& value) const;
259 bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); }
260 bool operator==(char value) const { return *this == wxUniChar(value); }
261 bool operator==(wchar_t value) const { return *this == wxUniChar(value); }
262 bool operator!=(const wxUniChar& value) const { return !(*this == value); }
263 bool operator!=(const wxUniCharRef& value) const { return !(*this == value); }
264 bool operator!=(char value) const { return !(*this == value); }
265 bool operator!=(wchar_t value) const { return !(*this == value); }
266 wxVariant& operator=(const wxUniChar& value);
267 wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); }
268 wxVariant& operator=(char value) { return *this = wxUniChar(value); }
269 wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); }
270 operator wxUniChar() const { return GetChar(); }
271 operator char() const { return GetChar(); }
272 operator wchar_t() const { return GetChar(); }
273 wxUniChar GetChar() const;
274
275 // wxArrayString
276 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
277 bool operator== (const wxArrayString& value) const;
278 bool operator!= (const wxArrayString& value) const;
279 void operator= (const wxArrayString& value);
280 operator wxArrayString () const { return GetArrayString(); }
281 wxArrayString GetArrayString() const;
282
283 // void*
284 wxVariant(void* ptr, const wxString& name = wxEmptyString);
285 bool operator== (void* value) const;
286 bool operator!= (void* value) const;
287 void operator= (void* value);
288 operator void* () const { return GetVoidPtr(); }
289 void* GetVoidPtr() const;
290
291 // wxObject*
292 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString);
293 bool operator== (wxObject* value) const;
294 bool operator!= (wxObject* value) const;
295 void operator= (wxObject* value);
296 wxObject* GetWxObjectPtr() const;
297
298 #if wxUSE_LONGLONG
299 // wxLongLong
300 wxVariant(wxLongLong, const wxString& name = wxEmptyString);
301 bool operator==(wxLongLong value) const;
302 bool operator!=(wxLongLong value) const;
303 void operator=(wxLongLong value);
304 operator wxLongLong() const { return GetLongLong(); }
305 wxLongLong GetLongLong() const;
306
307 // wxULongLong
308 wxVariant(wxULongLong, const wxString& name = wxEmptyString);
309 bool operator==(wxULongLong value) const;
310 bool operator!=(wxULongLong value) const;
311 void operator=(wxULongLong value);
312 operator wxULongLong() const { return GetULongLong(); }
313 wxULongLong GetULongLong() const;
314 #endif
315
316 // ------------------------------
317 // list operations
318 // ------------------------------
319
320 wxVariant(const wxVariantList& val, const wxString& name = wxEmptyString); // List of variants
321 bool operator== (const wxVariantList& value) const;
322 bool operator!= (const wxVariantList& value) const;
323 void operator= (const wxVariantList& value) ;
324 // Treat a list variant as an array
325 wxVariant operator[] (size_t idx) const;
326 wxVariant& operator[] (size_t idx) ;
327 wxVariantList& GetList() const ;
328
329 // Return the number of elements in a list
330 size_t GetCount() const;
331
332 // Make empty list
333 void NullList();
334
335 // Append to list
336 void Append(const wxVariant& value);
337
338 // Insert at front of list
339 void Insert(const wxVariant& value);
340
341 // Returns true if the variant is a member of the list
342 bool Member(const wxVariant& value) const;
343
344 // Deletes the nth element of the list
345 bool Delete(size_t item);
346
347 // Clear list
348 void ClearList();
349
350 public:
351 // Type conversion
352 bool Convert(long* value) const;
353 bool Convert(bool* value) const;
354 bool Convert(double* value) const;
355 bool Convert(wxString* value) const;
356 bool Convert(wxUniChar* value) const;
357 bool Convert(char* value) const;
358 bool Convert(wchar_t* value) const;
359 #if wxUSE_DATETIME
360 bool Convert(wxDateTime* value) const;
361 #endif // wxUSE_DATETIME
362 #if wxUSE_LONGLONG
363 bool Convert(wxLongLong* value) const;
364 bool Convert(wxULongLong* value) const;
365 #ifdef wxLongLong_t
366 bool Convert(wxLongLong_t* value) const
367 {
368 wxLongLong temp;
369 if ( !Convert(&temp) )
370 return false;
371 *value = temp.GetValue();
372 return true;
373 }
374 bool Convert(wxULongLong_t* value) const
375 {
376 wxULongLong temp;
377 if ( !Convert(&temp) )
378 return false;
379 *value = temp.GetValue();
380 return true;
381 }
382 #endif // wxLongLong_t
383 #endif // wxUSE_LONGLONG
384
385 // Attributes
386 protected:
387 virtual wxObjectRefData *CreateRefData() const;
388 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
389
390 wxString m_name;
391
392 private:
393 DECLARE_DYNAMIC_CLASS(wxVariant)
394 };
395
396
397 //
398 // wxVariant <-> wxAny conversion code
399 //
400 #if wxUSE_ANY
401
402 #include "wx/any.h"
403
404 // In order to convert wxAny to wxVariant, we need to be able to associate
405 // wxAnyValueType with a wxVariantData factory function.
406 typedef wxVariantData* (*wxVariantDataFactory)(const wxAny& any);
407
408 // Actual Any-to-Variant registration must be postponed to a time when all
409 // global variables have been initialized. Hence this arrangement.
410 // wxAnyToVariantRegistration instances are kept in global scope and
411 // wxAnyValueTypeGlobals in any.cpp will use their data when the time is
412 // right.
413 class WXDLLIMPEXP_BASE wxAnyToVariantRegistration
414 {
415 public:
416 wxAnyToVariantRegistration(wxVariantDataFactory factory);
417 virtual ~wxAnyToVariantRegistration();
418
419 virtual wxAnyValueType* GetAssociatedType() = 0;
420 wxVariantDataFactory GetFactory() const { return m_factory; }
421 private:
422 wxVariantDataFactory m_factory;
423 };
424
425 template<typename T>
426 class wxAnyToVariantRegistrationImpl : public wxAnyToVariantRegistration
427 {
428 public:
429 wxAnyToVariantRegistrationImpl(wxVariantDataFactory factory)
430 : wxAnyToVariantRegistration(factory)
431 {
432 }
433
434 virtual wxAnyValueType* GetAssociatedType()
435 {
436 return wxAnyValueTypeImpl<T>::GetInstance();
437 }
438 private:
439 };
440
441 #define DECLARE_WXANY_CONVERSION() \
442 virtual bool GetAsAny(wxAny* any) const; \
443 static wxVariantData* VariantDataFactory(const wxAny& any);
444
445 #define _REGISTER_WXANY_CONVERSION(T, CLASSNAME, FUNC) \
446 static wxAnyToVariantRegistrationImpl<T> \
447 gs_##CLASSNAME##AnyToVariantRegistration = \
448 wxAnyToVariantRegistrationImpl<T>(&FUNC);
449
450 #define REGISTER_WXANY_CONVERSION(T, CLASSNAME) \
451 _REGISTER_WXANY_CONVERSION(T, CLASSNAME, CLASSNAME::VariantDataFactory)
452
453 #define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME) \
454 bool CLASSNAME::GetAsAny(wxAny* any) const \
455 { \
456 *any = m_value; \
457 return true; \
458 } \
459 wxVariantData* CLASSNAME::VariantDataFactory(const wxAny& any) \
460 { \
461 return new CLASSNAME(wxANY_AS(any, T)); \
462 } \
463 REGISTER_WXANY_CONVERSION(T, CLASSNAME)
464
465 #else // if !wxUSE_ANY
466
467 #define DECLARE_WXANY_CONVERSION()
468 #define REGISTER_WXANY_CONVERSION(T, CLASSNAME)
469 #define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME)
470
471 #endif // wxUSE_ANY/!wxUSE_ANY
472
473
474 #define DECLARE_VARIANT_OBJECT(classname) \
475 DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
476
477 #define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
478 expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
479 expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
480
481 #define IMPLEMENT_VARIANT_OBJECT(classname) \
482 IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
483
484 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
485 class classname##VariantData: public wxVariantData \
486 { \
487 public:\
488 classname##VariantData() {} \
489 classname##VariantData( const classname &value ) { m_value = value; } \
490 \
491 classname &GetValue() { return m_value; } \
492 \
493 virtual bool Eq(wxVariantData& data) const; \
494 \
495 virtual wxString GetType() const; \
496 virtual wxClassInfo* GetValueClassInfo(); \
497 \
498 virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
499 \
500 DECLARE_WXANY_CONVERSION() \
501 protected:\
502 classname m_value; \
503 };\
504 \
505 wxString classname##VariantData::GetType() const\
506 {\
507 return m_value.GetClassInfo()->GetClassName();\
508 }\
509 \
510 wxClassInfo* classname##VariantData::GetValueClassInfo()\
511 {\
512 return m_value.GetClassInfo();\
513 }\
514 \
515 expdecl classname& operator << ( classname &value, const wxVariant &variant )\
516 {\
517 wxASSERT( variant.GetType() == #classname );\
518 \
519 classname##VariantData *data = (classname##VariantData*) variant.GetData();\
520 value = data->GetValue();\
521 return value;\
522 }\
523 \
524 expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
525 {\
526 classname##VariantData *data = new classname##VariantData( value );\
527 variant.SetData( data );\
528 return variant;\
529 } \
530 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(classname, classname##VariantData)
531
532 // implements a wxVariantData-derived class using for the Eq() method the
533 // operator== which must have been provided by "classname"
534 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
535 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
536 \
537 bool classname##VariantData::Eq(wxVariantData& data) const \
538 {\
539 wxASSERT( GetType() == data.GetType() );\
540 \
541 classname##VariantData & otherData = (classname##VariantData &) data;\
542 \
543 return otherData.m_value == m_value;\
544 }\
545
546
547 // implements a wxVariantData-derived class using for the Eq() method a shallow
548 // comparison (through wxObject::IsSameAs function)
549 #define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
550 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
551 #define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
552 IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
553 \
554 bool classname##VariantData::Eq(wxVariantData& data) const \
555 {\
556 wxASSERT( GetType() == data.GetType() );\
557 \
558 classname##VariantData & otherData = (classname##VariantData &) data;\
559 \
560 return (otherData.m_value.IsSameAs(m_value));\
561 }\
562
563
564 // Since we want type safety wxVariant we need to fetch and dynamic_cast
565 // in a seemingly safe way so the compiler can check, so we define
566 // a dynamic_cast /wxDynamicCast analogue.
567
568 #define wxGetVariantCast(var,classname) \
569 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
570 var.GetWxObjectPtr() : NULL));
571
572 // Replacement for using wxDynamicCast on a wxVariantData object
573 #ifndef wxNO_RTTI
574 #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
575 #endif
576
577 #define wxStaticCastVariantData(data, classname) static_cast<classname*>(data)
578
579 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
580
581 #endif // wxUSE_VARIANT
582
583 #endif // _WX_VARIANT_H_