]>
git.saurik.com Git - wxWidgets.git/blob - interface/variant.h
5034f9f0551fd1ebbc37aedf4c4090ab7a6b5f07
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxVariant class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 The @b wxVariant class represents a container for any type.
14 A variant's value can be changed at run time, possibly to a different type of
17 As standard, wxVariant can store values of type bool, wxChar, double, long,
19 string list, time, date, void pointer, list of strings, and list of variants.
20 However, an application can extend wxVariant's capabilities by deriving from the
21 class wxVariantData and using the wxVariantData form of
22 the wxVariant constructor or assignment operator to assign this data to a
24 Actual values for user-defined types will need to be accessed via the
26 object, unlike the case for basic data types where convenience functions such as
27 wxVariant::GetLong can be used.
29 Pointers to any wxObject derived class can also easily be stored
30 in a wxVariant. wxVariant will then use wxWidgets' built-in RTTI system to set
32 type name (returned by wxVariant::GetType) and to perform
33 type-safety checks at runtime.
35 This class is useful for reducing the programming for certain tasks, such as an
37 for different data types, or a remote procedure call protocol.
39 An optional name member is associated with a wxVariant. This might be used, for
41 in CORBA or OLE automation classes, where named parameters are required.
43 Note that as of wxWidgets 2.7.1, wxVariant is @ref overview_trefcount
45 Additionally, the convenience macros @b DECLARE_VARIANT_OBJECT and
46 @b IMPLEMENT_VARIANT_OBJECT were added so that adding (limited) support
47 for conversion to and from wxVariant can be very easily implemented without
49 either wxVariant or the class to be stored by wxVariant. Since assignment
51 cannot be declared outside the class, the shift left operators are used like
56 DECLARE_VARIANT_OBJECT(MyClass)
58 // in the implementation file
59 IMPLEMENT_VARIANT_OBJECT(MyClass)
70 For this to work, MyClass must derive from wxObject, implement
71 the @ref overview_runtimeclassoverview "wxWidgets RTTI system"
72 and support the assignment operator and equality operator for itself. Ideally,
74 should also be reference counted to make copying operations cheap and fast. This
75 can be most easily implemented using the reference counting support offered by
76 wxObject itself. By default, wxWidgets already implements
77 the shift operator conversion for a few of its drawing related classes:
80 IMPLEMENT_VARIANT_OBJECT(wxColour)
81 IMPLEMENT_VARIANT_OBJECT(wxImage)
82 IMPLEMENT_VARIANT_OBJECT(wxIcon)
83 IMPLEMENT_VARIANT_OBJECT(wxBitmap)
86 Note that as of wxWidgets 2.9.0, wxVariantData no longer inherits from wxObject
87 and wxVariant no longer uses the type-unsafe wxList class for list
88 operations but the type-safe wxVariantList class. Also, wxVariantData now
89 supports the Clone function for implementing the wxVariant::Unshare function.
90 Clone is implemented automatically by IMPLEMENT_VARIANT_OBJECT.
92 Since wxVariantData no longer derives from wxObject, any code that tests the
94 of the data using wxDynamicCast will require adjustment. You can use the macro
95 wxDynamicCastVariantData with the same arguments as wxDynamicCast, to use C++
97 type information instead of wxWidgets RTTI.
105 class wxVariant
: public wxObject
110 Construction from a ODBC timestamp value. Represented internally by a
114 wxVariant(const wxVariant
& variant
);
115 wxVariant(const wxChar
* value
, const wxString
& name
= "");
116 wxVariant(const wxString
& value
, const wxString
& name
= "");
117 wxVariant(wxChar value
, const wxString
& name
= "");
118 wxVariant(long value
, const wxString
& name
= "");
119 wxVariant(bool value
, const wxString
& name
= "");
120 wxVariant(double value
, const wxString
& name
= "");
121 wxVariant(const wxVariantList
& value
,
122 const wxString
& name
= "");
123 wxVariant(void* value
, const wxString
& name
= "");
124 wxVariant(wxObject
* value
, const wxString
& name
= "");
125 wxVariant(wxVariantData
* data
, const wxString
& name
= "");
126 wxVariant(wxDateTime
& val
, const wxString
& name
= "");
127 wxVariant(wxArrayString
& val
, const wxString
& name
= "");
128 wxVariant(DATE_STRUCT
* val
, const wxString
& name
= "");
129 wxVariant(TIME_STRUCT
* val
, const wxString
& name
= "");
130 wxVariant(TIMESTAMP_STRUCT
* val
, const wxString
& name
= "");
136 Note that destructor is protected, so wxVariantData cannot usually
137 be deleted. Instead, wxVariantData::DecRef should be called.
138 See @ref overview_refcountdestruct "reference-counted object destruction" for
144 Appends a value to the list.
146 void Append(const wxVariant
& value
);
149 Makes the variant null by deleting the internal data and
150 set the name to @e wxEmptyString.
155 Deletes the contents of the list.
161 Retrieves and converts the value of this variant to the type that @e value is.
163 bool Convert(long* value
);
164 bool Convert(bool* value
);
165 bool Convert(double* value
);
166 bool Convert(wxString
* value
);
167 bool Convert(wxChar
* value
);
168 bool Convert(wxDateTime
* value
);
172 Deletes the zero-based @e item from the list.
174 bool Delete(size_t item
);
177 Returns the string array value.
179 wxArrayString
GetArrayString();
182 Returns the boolean value.
187 Returns the character value.
192 Returns the number of elements in the list.
197 Returns a pointer to the internal variant data. To take ownership
198 of this data, you must call its wxVariantData::IncRef
199 method. When you stop using it, wxVariantData::DecRef
200 must be likewise called.
202 wxVariantData
* GetData();
205 Returns the date value.
207 wxDateTime
GetDateTime();
210 Returns the floating point value.
215 Returns a reference to the wxVariantList class used by
216 wxVariant if this wxVariant is currently a list of variants.
218 wxVariantList
GetList();
221 Returns the integer value.
226 Returns a constant reference to the variant name.
228 const wxString
GetName();
231 Gets the string value.
233 wxString
GetString();
236 Returns the value type as a string. The built-in types are: bool, char,
237 datetime, double, list, long, string, arrstring, void*.
239 If the variant is null, the value type returned is the string "null" (not the
245 Gets the void pointer value.
250 Gets the wxObject pointer value.
252 wxObject
* GetWxObjectPtr();
255 Inserts a value at the front of the list.
257 void Insert(const wxVariant
& value
);
260 Returns @true if there is no data associated with this variant, @false if there
266 Returns @true if @e type matches the type of the variant, @false otherwise.
268 bool IsType(const wxString
& type
);
271 Returns @true if the data is derived from the class described by @e type, @false
274 bool IsValueKindOf(const wxClassInfo
* type type
);
277 Makes the variant null by deleting the internal data.
282 Makes a string representation of the variant value (for any type).
284 wxString
MakeString();
287 Returns @true if @e value matches an element in the list.
289 bool Member(const wxVariant
& value
);
292 Makes an empty list. This differs from a null variant which has no data; a null
294 is of type list, but the number of elements in the list is zero.
299 Sets the internal variant data, deleting the existing data if there is any.
301 void SetData(wxVariantData
* data
);
304 Makes sure that any data associated with this variant is not shared with other
305 variants. For this to work, wxVariantData::Clone must
306 be implemented for the data types you are working with. Clone is implemented
307 for all the default data types.
313 Inequality test operators.
315 bool operator !=(const wxVariant
& value
);
316 bool operator !=(const wxString
& value
);
317 bool operator !=(const wxChar
* value
);
318 bool operator !=(wxChar value
);
319 bool operator !=(const long value
);
320 bool operator !=(const bool value
);
321 bool operator !=(const double value
);
322 bool operator !=(void* value
);
323 bool operator !=(wxObject
* value
);
324 bool operator !=(const wxVariantList
& value
);
325 bool operator !=(const wxArrayString
& value
);
326 bool operator !=(const wxDateTime
& value
);
331 Assignment operators, using @ref overview_trefcount "reference counting" when
334 void operator =(const wxVariant
& value
);
335 void operator =(wxVariantData
* value
);
336 void operator =(const wxString
& value
);
337 void operator =(const wxChar
* value
);
338 void operator =(wxChar value
);
339 void operator =(const long value
);
340 void operator =(const bool value
);
341 void operator =(const double value
);
342 void operator =(void* value
);
343 void operator =(wxObject
* value
);
344 void operator =(const wxVariantList
& value
);
345 void operator =(const wxDateTime
& value
);
346 void operator =(const wxArrayString
& value
);
347 void operator =(const DATE_STRUCT
* value
);
348 void operator =(const TIME_STRUCT
* value
);
349 void operator =(const TIMESTAMP_STRUCT
* value
);
354 Equality test operators.
356 bool operator ==(const wxVariant
& value
);
357 bool operator ==(const wxString
& value
);
358 bool operator ==(const wxChar
* value
);
359 bool operator ==(wxChar value
);
360 bool operator ==(const long value
);
361 bool operator ==(const bool value
);
362 bool operator ==(const double value
);
363 bool operator ==(void* value
);
364 bool operator ==(wxObject
* value
);
365 bool operator ==(const wxVariantList
& value
);
366 bool operator ==(const wxArrayString
& value
);
367 bool operator ==(const wxDateTime
& value
);
372 Returns a reference to the value at @e idx (zero-based). This can be used
373 to change the value at this index.
375 wxVariant
operator [](size_t idx
);
376 wxVariant
operator [](size_t idx
);
381 Operator for implicit conversion to a long, using GetLong().
383 double operator double();
384 long operator long();
388 Operator for implicit conversion to a pointer to a void, using GetVoidPtr().
390 void* operator void*();
393 Operator for implicit conversion to a wxChar, using GetChar().
395 char operator wxChar();
398 Operator for implicit conversion to a pointer to a wxDateTime, using
401 void* operator wxDateTime();
404 Operator for implicit conversion to a string, using MakeString().
406 wxString
operator wxString();
414 The @b wxVariantData class is used to implement a new type for wxVariant.
415 Derive from wxVariantData, and override the pure virtual functions.
417 wxVariantData is @ref overview_refcount "reference counted", but you don't
418 normally have to care about this,
419 as wxVariant manages the count automatically. However, in case your application
421 ownership of wxVariantData, be aware that the object is created with reference
423 and passing it to wxVariant will not increase this. In other words,
424 wxVariantData::IncRef
425 needs to be called only if you both take ownership of wxVariantData and pass it
427 Also note that the destructor is protected, so you can never explicitly delete
429 instance. Instead, wxVariantData::DecRef will delete the object automatically
430 when the reference count reaches zero.
447 This function can be overridden to clone the data.
448 Implement Clone if you wish wxVariant::Unshare to work
449 for your data. This function is implemented for all built-in data types.
451 wxVariantData
* Clone();
454 Decreases reference count. If the count reaches zero, the object is
455 automatically deleted.
457 Note that destructor of wxVariantData is protected, so delete
458 cannot be used as normal. Instead, DecRef() should be called.
463 Returns @true if this object is equal to @e data.
465 #define bool Eq(wxVariantData& data) /* implementation is private */
468 Returns the string type of the data.
473 If the data is a wxObject returns a pointer to the objects wxClassInfo
475 the data isn't a wxObject the method returns @NULL.
477 wxClassInfo
* GetValueClassInfo();
480 Increases reference count. Note that initially wxVariantData has reference
487 Reads the data from @e stream or @e string.
489 bool Read(ostream
& stream
);
490 bool Read(wxString
& string
);
495 Writes the data to @e stream or @e string.
497 bool Write(ostream
& stream
);
498 bool Write(wxString
& string
);
502 This macro returns the data stored in @e variant cast to the type @e classname
504 the data is of this type (the check is done during the run-time) or
507 classname
* wxGetVariantCast();