]>
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 @b 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 variants.
18 However, an application can extend wxVariant's capabilities by deriving from the
19 class wxVariantData and using the wxVariantData form of the wxVariant constructor
20 or assignment operator to assign this data to a variant.
21 Actual values for user-defined types will need to be accessed via the wxVariantData
22 object, unlike the case for basic data types where convenience functions such as
23 wxVariant::GetLong can be used.
25 Pointers to any wxObject derived class can also easily be stored in a wxVariant.
26 wxVariant will then use wxWidgets' built-in RTTI system to set the type name
27 (returned by wxVariant::GetType) and to perform type-safety checks at runtime.
29 This class is useful for reducing the programming for certain tasks, such as
30 an editor for different data types, or a remote procedure call protocol.
32 An optional name member is associated with a wxVariant. This might be used,
33 for example, in CORBA or OLE automation classes, where named parameters are required.
35 Note that as of wxWidgets 2.7.1, wxVariant is @ref overview_trefcount
37 Additionally, the convenience macros @b DECLARE_VARIANT_OBJECT and
38 @b IMPLEMENT_VARIANT_OBJECT were added so that adding (limited) support
39 for conversion to and from wxVariant can be very easily implemented
40 without modifying either wxVariant or the class to be stored by wxVariant.
41 Since assignment operators cannot be declared outside the class, the shift
42 left operators are used like this:
46 DECLARE_VARIANT_OBJECT(MyClass)
48 // in the implementation file
49 IMPLEMENT_VARIANT_OBJECT(MyClass)
60 For this to work, MyClass must derive from wxObject, implement
61 the @ref overview_runtimeclassoverview "wxWidgets RTTI system"
62 and support the assignment operator and equality operator for itself. Ideally,
64 should also be reference counted to make copying operations cheap and fast. This
65 can be most easily implemented using the reference counting support offered by
66 wxObject itself. By default, wxWidgets already implements
67 the shift 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 wxObject
77 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 Clone function for implementing the wxVariant::Unshare function.
80 Clone is implemented automatically by IMPLEMENT_VARIANT_OBJECT.
82 Since wxVariantData no longer derives from wxObject, any code that tests the
83 type of the data using wxDynamicCast will require adjustment. You can use the
84 macro wxDynamicCastVariantData with the same arguments as wxDynamicCast, to
85 use C++ RTTI type information instead of wxWidgets RTTI.
92 class wxVariant
: public wxObject
101 Constructs a variant directly with a wxVariantData
102 object. wxVariant will take ownership of the wxVariantData
103 and will not increase its reference count.
105 wxVariant(wxVariantData
* data
, const wxString
& name
= "");
108 Constructs a variant from another variant by increasing the
111 wxVariant(const wxVariant
& variant
);
114 Constructs a variant from a wide string literal.
116 wxVariant(const wxChar
* value
, const wxString
& name
= "");
119 Constructs a variant from a string.
121 wxVariant(const wxString
& value
, const wxString
& name
= "");
124 Constructs a variant from a wide char.
126 wxVariant(wxChar value
, const wxString
& name
= "");
129 Constructs a variant from a long.
131 wxVariant(long value
, const wxString
& name
= "");
134 Constructs a variant from a bool.
136 wxVariant(bool value
, const wxString
& name
= "");
139 Constructs a variant from a double.
141 wxVariant(double value
, const wxString
& name
= "");
144 Constructs a variant from a list of variants
146 wxVariant(const wxVariantList
& value
,
147 const wxString
& name
= "");
150 Constructs a variant from a void pointer.
152 wxVariant(void* value
, const wxString
& name
= "");
155 Constructs a variant from a pointer to an wxObject
158 wxVariant(wxObject
* value
, const wxString
& name
= "");
161 Constructs a variant from a wxDateTime.
163 wxVariant(wxDateTime
& val
, const wxString
& name
= "");
166 Constructs a variant from a wxArrayString.
168 wxVariant(wxArrayString
& val
, const wxString
& name
= "");
172 Note that destructor is protected, so wxVariantData cannot usually
173 be deleted. Instead, wxVariantData::DecRef should be called.
174 See @ref overview_refcountdestruct "reference-counted object destruction" for
180 @name List functionality
184 Returns the value at @a idx (zero-based).
186 wxVariant
operator [](size_t idx
);
188 Returns a reference to the value at @a idx (zero-based). This can be used
189 to change the value at this index.
191 const wxVariant
& operator [](size_t idx
);
193 Appends a value to the list.
195 void Append(const wxVariant
& value
);
197 Deletes the contents of the list.
201 Deletes the zero-based @a item from the list.
203 bool Delete(size_t item
);
205 Returns the number of elements in the list.
207 size_t GetCount() const;
209 Returns a reference to the wxVariantList class used by
210 wxVariant if this wxVariant is currently a list of variants.
212 wxVariantList
& GetList() const;
214 Makes the variant null by deleting the internal data and
215 set the name to @e wxEmptyString.
219 Inserts a value at the front of the list.
221 void Insert(const wxVariant
& value
);
223 Makes an empty list. This differs from a null variant which has no data;
224 a null list is of type list, but the number of elements in the list is zero.
231 Retrieves and converts the value of this variant to the type that @a value is.
233 bool Convert(long* value
) const;
234 const bool Convert(bool* value
) const;
235 const bool Convert(double* value
) const;
236 const bool Convert(wxString
* value
) const;
237 const bool Convert(wxChar
* value
) const;
238 const bool Convert(wxDateTime
* value
) const;
242 Returns the string array value.
244 wxArrayString
GetArrayString() const;
247 Returns the boolean value.
249 bool GetBool() const;
252 Returns the character value.
254 wxChar
GetChar() const;
257 Returns a pointer to the internal variant data. To take ownership
258 of this data, you must call its wxVariantData::IncRef
259 method. When you stop using it, wxVariantData::DecRef
260 must be likewise called.
262 wxVariantData
* GetData() const;
265 Returns the date value.
267 wxDateTime
GetDateTime() const;
270 Returns the floating point value.
272 double GetDouble() const;
275 Returns the integer value.
277 long GetLong() const;
280 Returns a constant reference to the variant name.
282 const wxString
GetName() const;
285 Gets the string value.
287 wxString
GetString() const;
290 Returns the value type as a string. The built-in types are: bool, char,
291 datetime, double, list, long, string, arrstring, void*.
292 If the variant is null, the value type returned is the string "null" (not the
295 wxString
GetType() const;
298 Gets the void pointer value.
300 void* GetVoidPtr() const;
303 Gets the wxObject pointer value.
305 wxObject
* GetWxObjectPtr() const;
308 Returns @true if there is no data associated with this variant, @false if there
314 Returns @true if @a type matches the type of the variant, @false otherwise.
316 bool IsType(const wxString
& type
) const;
319 Returns @true if the data is derived from the class described by @e type, @false
322 bool IsValueKindOf(const wxClassInfo
* type type
) const;
325 Makes the variant null by deleting the internal data.
330 Makes a string representation of the variant value (for any type).
332 wxString
MakeString() const;
335 Returns @true if @a value matches an element in the list.
337 bool Member(const wxVariant
& value
) const;
340 Sets the internal variant data, deleting the existing data if there is any.
342 void SetData(wxVariantData
* data
);
345 Makes sure that any data associated with this variant is not shared with other
346 variants. For this to work, wxVariantData::Clone must
347 be implemented for the data types you are working with. Clone is implemented
348 for all the default data types.
354 Inequality test operators.
356 bool operator !=(const wxVariant
& value
) const;
357 const bool operator !=(const wxString
& value
) const;
358 const bool operator !=(const wxChar
* value
) const;
359 const bool operator !=(wxChar value
) const;
360 const bool operator !=(const long value
) const;
361 const bool operator !=(const bool value
) const;
362 const bool operator !=(const double value
) const;
363 const bool operator !=(void* value
) const;
364 const bool operator !=(wxObject
* value
) const;
365 const bool operator !=(const wxVariantList
& value
) const;
366 const bool operator !=(const wxArrayString
& value
) const;
367 const bool operator !=(const wxDateTime
& value
) const;
372 Assignment operators, using @ref overview_trefcount "reference counting" when
375 void operator =(const wxVariant
& value
);
376 void operator =(wxVariantData
* value
);
377 void operator =(const wxString
& value
);
378 void operator =(const wxChar
* value
);
379 void operator =(wxChar value
);
380 void operator =(const long value
);
381 void operator =(const bool value
);
382 void operator =(const double value
);
383 void operator =(void* value
);
384 void operator =(wxObject
* value
);
385 void operator =(const wxVariantList
& value
);
386 void operator =(const wxDateTime
& value
);
387 void operator =(const wxArrayString
& value
);
388 void operator =(const DATE_STRUCT
* value
);
389 void operator =(const TIME_STRUCT
* value
);
390 void operator =(const TIMESTAMP_STRUCT
* value
);
395 Equality test operators.
397 bool operator ==(const wxVariant
& value
) const;
398 const bool operator ==(const wxString
& value
) const;
399 const bool operator ==(const wxChar
* value
) const;
400 const bool operator ==(wxChar value
) const;
401 const bool operator ==(const long value
) const;
402 const bool operator ==(const bool value
) const;
403 const bool operator ==(const double value
) const;
404 const bool operator ==(void* value
) const;
405 const bool operator ==(wxObject
* value
) const;
406 const bool operator ==(const wxVariantList
& value
) const;
407 const bool operator ==(const wxArrayString
& value
) const;
408 const bool operator ==(const wxDateTime
& value
) const;
413 Operator for implicit conversion to a long, using GetLong().
415 double operator double() const;
416 const long operator long() const;
420 Operator for implicit conversion to a pointer to a void, using GetVoidPtr().
422 void* operator void*() const;
425 Operator for implicit conversion to a wxChar, using GetChar().
427 char operator wxChar() const;
430 Operator for implicit conversion to a pointer to a wxDateTime, using
433 void* operator wxDateTime() const;
436 Operator for implicit conversion to a string, using MakeString().
438 wxString
operator wxString() const;
447 The @b wxVariantData class is used to implement a new type for wxVariant.
448 Derive from wxVariantData, and override the pure virtual functions.
450 wxVariantData is @ref overview_refcount "reference counted", but you don't
451 normally have to care about this,
452 as wxVariant manages the count automatically. However, in case your application
454 ownership of wxVariantData, be aware that the object is created with reference
456 and passing it to wxVariant will not increase this. In other words,
457 wxVariantData::IncRef
458 needs to be called only if you both take ownership of wxVariantData and pass it
460 Also note that the destructor is protected, so you can never explicitly delete
462 instance. Instead, wxVariantData::DecRef will delete the object automatically
463 when the reference count reaches zero.
479 This function can be overridden to clone the data.
480 Implement Clone if you wish wxVariant::Unshare to work
481 for your data. This function is implemented for all built-in data types.
483 wxVariantData
* Clone() const;
486 Decreases reference count. If the count reaches zero, the object is
487 automatically deleted.
488 Note that destructor of wxVariantData is protected, so delete
489 cannot be used as normal. Instead, DecRef() should be called.
494 Returns @true if this object is equal to @e data.
496 bool Eq(wxVariantData
& data
) const;
499 Returns the string type of the data.
501 wxString
GetType() const;
504 If the data is a wxObject returns a pointer to the objects wxClassInfo
506 the data isn't a wxObject the method returns @NULL.
508 wxClassInfo
* GetValueClassInfo() const;
511 Increases reference count. Note that initially wxVariantData has reference
518 Reads the data from @a stream or @e string.
520 bool Read(ostream
& stream
);
521 bool Read(wxString
& string
);
526 Writes the data to @a stream or @e string.
528 bool Write(ostream
& stream
) const;
529 const bool Write(wxString
& string
) const;
533 This macro returns the data stored in @e variant cast to the type @e classname
535 the data is of this type (the check is done during the run-time) or
538 classname
* wxGetVariantCast();