]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/variant.h
9730565dd24c4f0738e81560e6463e78aef9c368
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxVariant
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
12 The wxVariant class represents a container for any type. A variant's value
13 can be changed at run time, possibly to a different type of value.
15 @note As of wxWidgets 2.9.1, wxAny has become the preferred variant class.
16 While most controls still use wxVariant in their interface, you
17 can start using wxAny in your code because of an implicit conversion
18 layer. See below for more information.
20 As standard, wxVariant can store values of type bool, wxChar, double, long,
21 string, string list, time, date, void pointer, list of strings, and list of
22 variants. However, an application can extend wxVariant's capabilities by
23 deriving from the class wxVariantData and using the wxVariantData form of
24 the wxVariant constructor or assignment operator to assign this data to a
25 variant. Actual values for user-defined types will need to be accessed via
26 the wxVariantData object, unlike the case for basic data types where
27 convenience functions such as GetLong() can be used.
29 Under Microsoft Windows, three additional wxVariantData-derived classes --
30 wxVariantDataCurrency, wxVariantDataErrorCode and wxVariantDataSafeArray --
31 are available for interoperation with OLE VARIANT when using wxAutomationObject.
33 Pointers to any wxObject derived class can also easily be stored in a
34 wxVariant. wxVariant will then use wxWidgets' built-in RTTI system to set
35 the type name (returned by GetType()) and to perform type-safety checks at
38 This class is useful for reducing the programming for certain tasks, such
39 as an editor for different data types, or a remote procedure call protocol.
41 An optional name member is associated with a wxVariant. This might be used,
42 for example, in CORBA or OLE automation classes, where named parameters are
45 Note that as of wxWidgets 2.7.1, wxVariant is
46 @ref overview_refcount "reference counted". Additionally, the convenience
47 macros DECLARE_VARIANT_OBJECT() and IMPLEMENT_VARIANT_OBJECT() were added
48 so that adding (limited) support for conversion to and from wxVariant can
49 be very easily implemented without modifying either wxVariant or the class
50 to be stored by wxVariant. Since assignment operators cannot be declared
51 outside the class, the shift left operators are used like this:
55 DECLARE_VARIANT_OBJECT(MyClass)
57 // in the implementation file
58 IMPLEMENT_VARIANT_OBJECT(MyClass)
69 For this to work, MyClass must derive from wxObject, implement the
70 @ref overview_rtti "wxWidgets RTTI system" and support the assignment
71 operator and equality operator for itself. Ideally, it should also be
72 reference counted to make copying operations cheap and fast. This can be
73 most easily implemented using the reference counting support offered by
74 wxObject itself. By default, wxWidgets already implements the shift
75 operator conversion for a few of its drawing related classes:
78 IMPLEMENT_VARIANT_OBJECT(wxColour)
79 IMPLEMENT_VARIANT_OBJECT(wxImage)
80 IMPLEMENT_VARIANT_OBJECT(wxIcon)
81 IMPLEMENT_VARIANT_OBJECT(wxBitmap)
84 Note that as of wxWidgets 2.9.0, wxVariantData no longer inherits from
85 wxObject and wxVariant no longer uses the type-unsafe wxList class for list
86 operations but the type-safe wxVariantList class. Also, wxVariantData now
87 supports the wxVariantData::Clone() function for implementing the Unshare()
88 function. wxVariantData::Clone() is implemented automatically by
89 IMPLEMENT_VARIANT_OBJECT().
91 Since wxVariantData no longer derives from wxObject, any code that tests
92 the type of the data using wxDynamicCast() will require adjustment. You can
93 use the macro wxDynamicCastVariantData() with the same arguments as
94 wxDynamicCast(), to use C++ RTTI type information instead of wxWidgets
97 @section variant_wxanyconversion wxVariant to wxAny Conversion Layer
99 wxAny is a more modern, template-based variant class. It is not
100 directly compatible with wxVariant, but there is a transparent conversion
103 Following is an example how to use these conversions with wxPropertyGrid's
104 property class wxPGProperty (which currently uses wxVariants both
105 internally and in the public API):
108 // Get property value as wxAny instead of wxVariant
109 wxAny value = property->GetValue();
111 // Do something with it
112 DoSomethingWithString(value.As<wxString>());
114 // Write back new value to property
116 property->SetValue(value);
121 @li In wxAny, there are no separate types for handling integers of
122 different sizes, so converting wxAny with 'long long' value
123 will yield wxVariant of "long" type when the value is small
124 enough to fit in without overflow. Otherwise, variant type
125 "longlong" is used. Also note that wxAny holding unsigned integer
126 will always be converted to "ulonglong" wxVariant type.
128 @li Unlike wxVariant, wxAny does not store a (rarely needed) name string.
130 @li Because of implicit conversion of wxVariant to wxAny, wxAny cannot
131 usually contain value of type wxVariant. In other words,
132 any.CheckType<wxVariant>() can never return @true.
134 Supplied conversion functions will automatically work with all
135 built-in wxVariant types, and also with all user-specified types generated
136 using IMPLEMENT_VARIANT_OBJECT(). For hand-built wxVariantData classes,
137 you will need to use supplied macros in a following manner:
141 // Declare wxVariantData for data type Foo
142 class wxVariantDataFoo: public wxVariantData
148 DECLARE_WXANY_CONVERSION()
154 IMPLEMENT_TRIVIAL_WXANY_CONVERSION(Foo, wxVariantDataFoo)
161 @see wxVariantData, wxAny
163 class wxVariant
: public wxObject
172 Constructs a variant directly with a wxVariantData object. wxVariant
173 will take ownership of the wxVariantData and will not increase its
176 wxVariant(wxVariantData
* data
, const wxString
& name
= wxEmptyString
);
179 Constructs a variant from another variant by increasing the reference
182 wxVariant(const wxVariant
& variant
);
185 Constructs a variant by converting it from wxAny.
187 wxVariant(const wxAny
& any
);
190 Constructs a variant from a wide string literal.
192 wxVariant(const wxChar
* value
, const wxString
& name
= wxEmptyString
);
195 Constructs a variant from a string.
197 wxVariant(const wxString
& value
, const wxString
& name
= wxEmptyString
);
200 Constructs a variant from a wide char.
202 wxVariant(wxChar value
, const wxString
& name
= wxEmptyString
);
205 Constructs a variant from a long.
207 wxVariant(long value
, const wxString
& name
= wxEmptyString
);
210 Constructs a variant from a bool.
212 wxVariant(bool value
, const wxString
& name
= wxEmptyString
);
215 Constructs a variant from a double.
217 wxVariant(double value
, const wxString
& name
= wxEmptyString
);
220 Constructs a variant from a wxLongLong.
222 wxVariant(wxLongLong value
, const wxString
& name
= wxEmptyString
);
225 Constructs a variant from a wxULongLong.
227 wxVariant(wxULongLong value
, const wxString
& name
= wxEmptyString
);
230 Constructs a variant from a list of variants
232 wxVariant(const wxVariantList
& value
, const wxString
& name
= wxEmptyString
);
235 Constructs a variant from a void pointer.
237 wxVariant(void* value
, const wxString
& name
= wxEmptyString
);
240 Constructs a variant from a pointer to an wxObject
243 wxVariant(wxObject
* value
, const wxString
& name
= wxEmptyString
);
246 Constructs a variant from a wxDateTime.
248 wxVariant(const wxDateTime
& val
, const wxString
& name
= wxEmptyString
);
251 Constructs a variant from a wxArrayString.
253 wxVariant(const wxArrayString
& val
, const wxString
& name
= wxEmptyString
);
258 @note wxVariantData's destructor is protected, so wxVariantData cannot
259 usually be deleted. Instead, wxVariantData::DecRef() should be
260 called. See @ref overview_refcount_destruct
261 "reference-counted object destruction" for more info.
263 virtual ~wxVariant();
267 @name List Functionality
272 Returns the value at @a idx (zero-based).
274 wxVariant
operator [](size_t idx
) const;
276 Returns a reference to the value at @a idx (zero-based). This can be
277 used to change the value at this index.
279 wxVariant
& operator [](size_t idx
);
282 Appends a value to the list.
284 void Append(const wxVariant
& value
);
287 Makes the variant null by deleting the internal data and set the name
293 Deletes the contents of the list.
298 Deletes the zero-based @a item from the list.
300 bool Delete(size_t item
);
303 Returns the number of elements in the list.
305 size_t GetCount() const;
308 Returns a reference to the wxVariantList class used by wxVariant if
309 this wxVariant is currently a list of variants.
311 wxVariantList
& GetList() const;
314 Inserts a value at the front of the list.
316 void Insert(const wxVariant
& value
);
319 Makes an empty list. This differs from a null variant which has no
320 data; a null list is of type list, but the number of elements in the
330 Retrieves and converts the value of this variant to the type that
333 bool Convert(long* value
) const;
334 bool Convert(bool* value
) const;
335 bool Convert(double* value
) const;
336 bool Convert(wxString
* value
) const;
337 bool Convert(wxChar
* value
) const;
338 bool Convert(wxLongLong
* value
) const;
339 bool Convert(wxULongLong
* value
) const;
340 bool Convert(wxDateTime
* value
) const;
344 Converts wxVariant into wxAny.
346 wxAny
GetAny() const;
349 Returns the string array value.
351 wxArrayString
GetArrayString() const;
354 Returns the boolean value.
356 bool GetBool() const;
359 Returns the character value.
361 wxUniChar
GetChar() const;
364 Returns a pointer to the internal variant data. To take ownership of
365 this data, you must call its wxVariantData::IncRef() method. When you
366 stop using it, wxVariantData::DecRef() must be called as well.
368 wxVariantData
* GetData() const;
371 Returns the date value.
373 wxDateTime
GetDateTime() const;
376 Returns the floating point value.
378 double GetDouble() const;
381 Returns the integer value.
383 long GetLong() const;
386 Returns the signed 64-bit integer value.
388 wxLongLong
GetLongLong() const;
391 Returns a constant reference to the variant name.
393 const wxString
& GetName() const;
396 Gets the string value.
398 wxString
GetString() const;
401 Returns the value type as a string.
403 The built-in types are:
416 If the variant is null, the value type returned is the string "null"
417 (not the empty string).
419 wxString
GetType() const;
422 Returns the unsigned 64-bit integer value.
424 wxULongLong
GetULongLong() const;
427 Gets the void pointer value.
429 Notice that this method can be used for null objects (i.e. those for
430 which IsNull() returns @true) and will return @NULL for them.
432 void* GetVoidPtr() const;
435 Gets the wxObject pointer value.
437 wxObject
* GetWxObjectPtr() const;
440 Returns @true if there is no data associated with this variant, @false
446 Returns @true if @a type matches the type of the variant, @false
449 bool IsType(const wxString
& type
) const;
452 Returns @true if the data is derived from the class described by
453 @a type, @false otherwise.
455 bool IsValueKindOf(const wxClassInfo
* type
) const;
458 Makes the variant null by deleting the internal data.
463 Makes a string representation of the variant value (for any type).
465 wxString
MakeString() const;
468 Returns @true if @a value matches an element in the list.
470 bool Member(const wxVariant
& value
) const;
473 Sets the internal variant data, deleting the existing data if there is
476 void SetData(wxVariantData
* data
);
479 Makes sure that any data associated with this variant is not shared
480 with other variants. For this to work, wxVariantData::Clone() must be
481 implemented for the data types you are working with.
482 wxVariantData::Clone() is implemented for all the default data types.
488 Inequality test operator.
490 bool operator !=(const wxVariant
& value
) const;
491 bool operator !=(const wxString
& value
) const;
492 bool operator !=(const wxChar
* value
) const;
493 bool operator !=(wxChar value
) const;
494 bool operator !=(long value
) const;
495 bool operator !=(bool value
) const;
496 bool operator !=(double value
) const;
497 bool operator !=(wxLongLong value
) const;
498 bool operator !=(wxULongLong value
) const;
499 bool operator !=(void* value
) const;
500 bool operator !=(wxObject
* value
) const;
501 bool operator !=(const wxVariantList
& value
) const;
502 bool operator !=(const wxArrayString
& value
) const;
503 bool operator !=(const wxDateTime
& value
) const;
508 Assignment operator, using @ref overview_refcount "reference counting"
511 void operator =(const wxVariant
& value
);
512 void operator =(wxVariantData
* value
);
513 void operator =(const wxString
& value
);
514 void operator =(const wxChar
* value
);
515 void operator =(wxChar value
);
516 void operator =(long value
);
517 void operator =(bool value
);
518 void operator =(double value
);
519 bool operator =(wxLongLong value
) const;
520 bool operator =(wxULongLong value
) const;
521 void operator =(void* value
);
522 void operator =(wxObject
* value
);
523 void operator =(const wxVariantList
& value
);
524 void operator =(const wxDateTime
& value
);
525 void operator =(const wxArrayString
& value
);
530 Equality test operator.
532 bool operator ==(const wxVariant
& value
) const;
533 bool operator ==(const wxString
& value
) const;
534 bool operator ==(const wxChar
* value
) const;
535 bool operator ==(wxChar value
) const;
536 bool operator ==(long value
) const;
537 bool operator ==(bool value
) const;
538 bool operator ==(double value
) const;
539 bool operator ==(wxLongLong value
) const;
540 bool operator ==(wxULongLong value
) const;
541 bool operator ==(void* value
) const;
542 bool operator ==(wxObject
* value
) const;
543 bool operator ==(const wxVariantList
& value
) const;
544 bool operator ==(const wxArrayString
& value
) const;
545 bool operator ==(const wxDateTime
& value
) const;
550 Operators for implicit conversion, using appropriate getter member
553 double operator double() const;
554 long operator long() const;
555 wxLongLong
operator wxLongLong() const;
556 wxULongLong
operator wxULongLong() const;
560 Operator for implicit conversion to a pointer to a void, using
563 void* operator void*() const;
566 Operator for implicit conversion to a wxChar, using GetChar().
568 char operator wxChar() const;
571 Operator for implicit conversion to a pointer to a wxDateTime, using
574 void* operator wxDateTime() const;
577 Operator for implicit conversion to a string, using MakeString().
579 wxString
operator wxString() const;
587 The wxVariantData class is used to implement a new type for wxVariant.
588 Derive from wxVariantData, and override the pure virtual functions.
590 wxVariantData is @ref overview_refcount "reference counted", but you don't
591 normally have to care about this, as wxVariant manages the count
592 automatically. However, in case your application needs to take ownership of
593 wxVariantData, be aware that the object is created with a reference count
594 of 1, and passing it to wxVariant will not increase this. In other words,
595 IncRef() needs to be called only if you both take ownership of
596 wxVariantData and pass it to a wxVariant. Also note that the destructor is
597 protected, so you can never explicitly delete a wxVariantData instance.
598 Instead, DecRef() will delete the object automatically when the reference
604 @see wxVariant, wxGetVariantCast()
606 class wxVariantData
: public wxObjectRefData
615 This function can be overridden to clone the data. You must implement
616 this function in order for wxVariant::Unshare() to work for your data.
617 This function is implemented for all built-in data types.
619 virtual wxVariantData
* Clone() const;
622 Decreases reference count. If the count reaches zero, the object is
623 automatically deleted.
625 @note The destructor of wxVariantData is protected, so delete cannot be
626 used as normal. Instead, DecRef() should be called.
631 Returns @true if this object is equal to @a data.
633 virtual bool Eq(wxVariantData
& data
) const = 0;
636 Converts value to wxAny, if possible. Return @true if successful.
638 virtual bool GetAny(wxAny
* any
) const;
641 Returns the string type of the data.
643 virtual wxString
GetType() const = 0;
646 If the data is a wxObject returns a pointer to the objects wxClassInfo
647 structure, if the data isn't a wxObject the method returns @NULL.
649 virtual wxClassInfo
* GetValueClassInfo();
652 Increases reference count. Note that initially wxVariantData has
653 reference count of 1.
658 Reads the data from @a stream.
660 virtual bool Read(istream
& stream
);
663 Reads the data from @a string.
665 virtual bool Read(wxString
& string
);
668 Writes the data to @a stream.
670 virtual bool Write(ostream
& stream
) const;
672 Writes the data to @a string.
674 virtual bool Write(wxString
& string
) const;
679 // ============================================================================
680 // Global functions/macros
681 // ============================================================================
683 /** @addtogroup group_funcmacro_rtti */
687 This macro returns a pointer to the data stored in @a var (wxVariant) cast
688 to the type @a classname if the data is of this type (the check is done
689 during the run-time) or @NULL otherwise.
691 @header{wx/variant.h}
693 @see @ref overview_rtti, wxDynamicCast()
695 #define wxGetVariantCast(var, classname)