]>
git.saurik.com Git - wxWidgets.git/blob - interface/variant.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxVariant
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 The wxVariant class represents a container for any type. A variant's value
14 can be changed at run time, possibly to a different type of value.
16 As standard, wxVariant can store values of type bool, wxChar, double, long,
17 string, string list, time, date, void pointer, list of strings, and list of
18 variants. However, an application can extend wxVariant's capabilities by
19 deriving from the class wxVariantData and using the wxVariantData form of
20 the wxVariant constructor or assignment operator to assign this data to a
21 variant. Actual values for user-defined types will need to be accessed via
22 the wxVariantData object, unlike the case for basic data types where
23 convenience functions such as GetLong() can be used.
25 Pointers to any wxObject derived class can also easily be stored in a
26 wxVariant. wxVariant will then use wxWidgets' built-in RTTI system to set
27 the type name (returned by GetType()) and to perform type-safety checks at
30 This class is useful for reducing the programming for certain tasks, such
31 as an editor for different data types, or a remote procedure call protocol.
33 An optional name member is associated with a wxVariant. This might be used,
34 for example, in CORBA or OLE automation classes, where named parameters are
37 Note that as of wxWidgets 2.7.1, wxVariant is
38 @ref overview_refcount "reference counted". Additionally, the convenience
39 macros DECLARE_VARIANT_OBJECT() and IMPLEMENT_VARIANT_OBJECT() were added
40 so that adding (limited) support for conversion to and from wxVariant can
41 be very easily implemented without modifying either wxVariant or the class
42 to be stored by wxVariant. Since assignment operators cannot be declared
43 outside the class, the shift left operators are used like this:
47 DECLARE_VARIANT_OBJECT(MyClass)
49 // in the implementation file
50 IMPLEMENT_VARIANT_OBJECT(MyClass)
61 For this to work, MyClass must derive from wxObject, implement the
62 @ref overview_rtti "wxWidgets RTTI system" and support the assignment
63 operator and equality operator for itself. Ideally, it should also be
64 reference counted to make copying operations cheap and fast. This can be
65 most easily implemented using the reference counting support offered by
66 wxObject itself. By default, wxWidgets already implements the shift
67 operator conversion for a few of its drawing related classes:
70 IMPLEMENT_VARIANT_OBJECT(wxColour)
71 IMPLEMENT_VARIANT_OBJECT(wxImage)
72 IMPLEMENT_VARIANT_OBJECT(wxIcon)
73 IMPLEMENT_VARIANT_OBJECT(wxBitmap)
76 Note that as of wxWidgets 2.9.0, wxVariantData no longer inherits from
77 wxObject and wxVariant no longer uses the type-unsafe wxList class for list
78 operations but the type-safe wxVariantList class. Also, wxVariantData now
79 supports the wxVariantData::Clone() function for implementing the Unshare()
80 function. wxVariantData::Clone() is implemented automatically by
81 IMPLEMENT_VARIANT_OBJECT().
83 Since wxVariantData no longer derives from wxObject, any code that tests
84 the type of the data using wxDynamicCast() will require adjustment. You can
85 use the macro wxDynamicCastVariantData() with the same arguments as
86 wxDynamicCast(), to use C++ RTTI type information instead of wxWidgets
94 class wxVariant
: public wxObject
103 Constructs a variant directly with a wxVariantData object. wxVariant
104 will take ownership of the wxVariantData and will not increase its
107 wxVariant(wxVariantData
* data
, const wxString
& name
= "");
110 Constructs a variant from another variant by increasing the reference
113 wxVariant(const wxVariant
& variant
);
116 Constructs a variant from a wide string literal.
118 wxVariant(const wxChar
* value
, const wxString
& name
= "");
121 Constructs a variant from a string.
123 wxVariant(const wxString
& value
, const wxString
& name
= "");
126 Constructs a variant from a wide char.
128 wxVariant(wxChar value
, const wxString
& name
= "");
131 Constructs a variant from a long.
133 wxVariant(long value
, const wxString
& name
= "");
136 Constructs a variant from a bool.
138 wxVariant(bool value
, const wxString
& name
= "");
141 Constructs a variant from a double.
143 wxVariant(double value
, const wxString
& name
= "");
146 Constructs a variant from a list of variants
148 wxVariant(const wxVariantList
& value
, const wxString
& name
= "");
151 Constructs a variant from a void pointer.
153 wxVariant(void* value
, const wxString
& name
= "");
156 Constructs a variant from a pointer to an wxObject
159 wxVariant(wxObject
* value
, const wxString
& name
= "");
162 Constructs a variant from a wxDateTime.
164 wxVariant(wxDateTime
& val
, const wxString
& name
= "");
167 Constructs a variant from a wxArrayString.
169 wxVariant(wxArrayString
& val
, const wxString
& name
= "");
174 @note wxVariantData's destructor is protected, so wxVariantData cannot
175 usually be deleted. Instead, wxVariantData::DecRef() should be
176 called. See @ref overview_refcount_destruct
177 "reference-counted object destruction" for more info.
183 @name List Functionality
188 Returns the value at @a idx (zero-based).
190 wxVariant
operator [](size_t idx
) const;
192 Returns a reference to the value at @a idx (zero-based). This can be
193 used to change the value at this index.
195 wxVariant
& operator [](size_t idx
);
198 Appends a value to the list.
200 void Append(const wxVariant
& value
);
203 Makes the variant null by deleting the internal data and set the name
209 Deletes the contents of the list.
214 Deletes the zero-based @a item from the list.
216 bool Delete(size_t item
);
219 Returns the number of elements in the list.
221 size_t GetCount() const;
224 Returns a reference to the wxVariantList class used by wxVariant if
225 this wxVariant is currently a list of variants.
227 wxVariantList
& GetList() const;
230 Inserts a value at the front of the list.
232 void Insert(const wxVariant
& value
);
235 Makes an empty list. This differs from a null variant which has no
236 data; a null list is of type list, but the number of elements in the
246 Retrieves and converts the value of this variant to the type that
249 bool Convert(long* value
) const;
250 bool Convert(bool* value
) const;
251 bool Convert(double* value
) const;
252 bool Convert(wxString
* value
) const;
253 bool Convert(wxChar
* value
) const;
254 bool Convert(wxDateTime
* value
) const;
258 Returns the string array value.
260 wxArrayString
GetArrayString() const;
263 Returns the boolean value.
265 bool GetBool() const;
268 Returns the character value.
270 wxChar
GetChar() const;
273 Returns a pointer to the internal variant data. To take ownership of
274 this data, you must call its wxVariantData::IncRef() method. When you
275 stop using it, wxVariantData::DecRef() must be called as well.
277 wxVariantData
* GetData() const;
280 Returns the date value.
282 wxDateTime
GetDateTime() const;
285 Returns the floating point value.
287 double GetDouble() const;
290 Returns the integer value.
292 long GetLong() const;
295 Returns a constant reference to the variant name.
297 const wxString
GetName() const;
300 Gets the string value.
302 wxString
GetString() const;
305 Returns the value type as a string.
307 The built-in types are:
318 If the variant is null, the value type returned is the string "null"
319 (not the empty string).
321 wxString
GetType() const;
324 Gets the void pointer value.
326 void* GetVoidPtr() const;
329 Gets the wxObject pointer value.
331 wxObject
* GetWxObjectPtr() const;
334 Returns @true if there is no data associated with this variant, @false
340 Returns @true if @a type matches the type of the variant, @false
343 bool IsType(const wxString
& type
) const;
346 Returns @true if the data is derived from the class described by
347 @a type, @false otherwise.
349 bool IsValueKindOf(const wxClassInfo
* type
) const;
352 Makes the variant null by deleting the internal data.
357 Makes a string representation of the variant value (for any type).
359 wxString
MakeString() const;
362 Returns @true if @a value matches an element in the list.
364 bool Member(const wxVariant
& value
) const;
367 Sets the internal variant data, deleting the existing data if there is
370 void SetData(wxVariantData
* data
);
373 Makes sure that any data associated with this variant is not shared
374 with other variants. For this to work, wxVariantData::Clone() must be
375 implemented for the data types you are working with.
376 wxVariantData::Clone() is implemented for all the default data types.
382 Inequality test operator.
384 bool operator !=(const wxVariant
& value
) const;
385 bool operator !=(const wxString
& value
) const;
386 bool operator !=(const wxChar
* value
) const;
387 bool operator !=(wxChar value
) const;
388 bool operator !=(const long value
) const;
389 bool operator !=(const bool value
) const;
390 bool operator !=(const double value
) const;
391 bool operator !=(void* value
) const;
392 bool operator !=(wxObject
* value
) const;
393 bool operator !=(const wxVariantList
& value
) const;
394 bool operator !=(const wxArrayString
& value
) const;
395 bool operator !=(const wxDateTime
& value
) const;
400 Assignment operator, using @ref overview_refcount "reference counting"
403 void operator =(const wxVariant
& value
);
404 void operator =(wxVariantData
* value
);
405 void operator =(const wxString
& value
);
406 void operator =(const wxChar
* value
);
407 void operator =(wxChar value
);
408 void operator =(const long value
);
409 void operator =(const bool value
);
410 void operator =(const double value
);
411 void operator =(void* value
);
412 void operator =(wxObject
* value
);
413 void operator =(const wxVariantList
& value
);
414 void operator =(const wxDateTime
& value
);
415 void operator =(const wxArrayString
& value
);
420 Equality test operator.
422 bool operator ==(const wxVariant
& value
) const;
423 bool operator ==(const wxString
& value
) const;
424 bool operator ==(const wxChar
* value
) const;
425 bool operator ==(wxChar value
) const;
426 bool operator ==(const long value
) const;
427 bool operator ==(const bool value
) const;
428 bool operator ==(const double value
) const;
429 bool operator ==(void* value
) const;
430 bool operator ==(wxObject
* value
) const;
431 bool operator ==(const wxVariantList
& value
) const;
432 bool operator ==(const wxArrayString
& value
) const;
433 bool operator ==(const wxDateTime
& value
) const;
438 Operator for implicit conversion to a long, using GetLong().
440 double operator double() const;
441 long operator long() const;
445 Operator for implicit conversion to a pointer to a void, using
448 void* operator void*() const;
451 Operator for implicit conversion to a wxChar, using GetChar().
453 char operator wxChar() const;
456 Operator for implicit conversion to a pointer to a wxDateTime, using
459 void* operator wxDateTime() const;
462 Operator for implicit conversion to a string, using MakeString().
464 wxString
operator wxString() const;
473 The wxVariantData class is used to implement a new type for wxVariant.
474 Derive from wxVariantData, and override the pure virtual functions.
476 wxVariantData is @ref overview_refcount "reference counted", but you don't
477 normally have to care about this, as wxVariant manages the count
478 automatically. However, in case your application needs to take ownership of
479 wxVariantData, be aware that the object is created with a reference count
480 of 1, and passing it to wxVariant will not increase this. In other words,
481 IncRef() needs to be called only if you both take ownership of
482 wxVariantData and pass it to a wxVariant. Also note that the destructor is
483 protected, so you can never explicitly delete a wxVariantData instance.
484 Instead, DecRef() will delete the object automatically when the reference
490 @see wxVariant, wxGetVariantCast()
501 This function can be overridden to clone the data. You must implement
502 this function in order for wxVariant::Unshare() to work for your data.
503 This function is implemented for all built-in data types.
505 wxVariantData
* Clone() const;
508 Decreases reference count. If the count reaches zero, the object is
509 automatically deleted.
511 @note The destructor of wxVariantData is protected, so delete cannot be
512 used as normal. Instead, DecRef() should be called.
517 Returns @true if this object is equal to @a data.
519 bool Eq(wxVariantData
& data
) const;
522 Returns the string type of the data.
524 wxString
GetType() const;
527 If the data is a wxObject returns a pointer to the objects wxClassInfo
528 structure, if the data isn't a wxObject the method returns @NULL.
530 wxClassInfo
* GetValueClassInfo() const;
533 Increases reference count. Note that initially wxVariantData has
534 reference count of 1.
539 Reads the data from @a stream.
541 bool Read(ostream
& stream
);
543 Reads the data from @a string.
545 bool Read(wxString
& string
);
548 Writes the data to @a stream.
550 bool Write(ostream
& stream
) const;
552 Writes the data to @a string.
554 bool Write(wxString
& string
) const;
559 // ============================================================================
560 // Global functions/macros
561 // ============================================================================
563 /** @ingroup group_funcmacro_rtti */
567 This macro returns a pointer to the data stored in @a var (wxVariant) cast
568 to the type @a classname if the data is of this type (the check is done
569 during the run-time) or @NULL otherwise.
571 @header{wx/variant.h}
573 @see @ref overview_rtti, wxDynamicCast()
575 #define wxGetVariantCast(var, classname)