]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/variant.h
introduce wxBG_STYLE_{ERASE,PAINT} and implement their documented semantics in wxGTK
[wxWidgets.git] / interface / wx / variant.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: variant.h
e54c96f1 3// Purpose: interface of wxVariant
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10 @class wxVariant
7c913512 11
09ad05fa 12 The wxVariant class represents a container for any type. A variant's value
c1996262 13 can be changed at run time, possibly to a different type of value.
7c913512 14
23324ae1 15 As standard, wxVariant can store values of type bool, wxChar, double, long,
09ad05fa
BP
16 string, string list, time, date, void pointer, list of strings, and list of
17 variants. However, an application can extend wxVariant's capabilities by
18 deriving from the class wxVariantData and using the wxVariantData form of
19 the wxVariant constructor or assignment operator to assign this data to a
20 variant. Actual values for user-defined types will need to be accessed via
21 the wxVariantData object, unlike the case for basic data types where
22 convenience functions such as GetLong() can be used.
23
24 Pointers to any wxObject derived class can also easily be stored in a
25 wxVariant. wxVariant will then use wxWidgets' built-in RTTI system to set
26 the type name (returned by GetType()) and to perform type-safety checks at
27 runtime.
28
29 This class is useful for reducing the programming for certain tasks, such
30 as an editor for different data types, or a remote procedure call protocol.
7c913512 31
c1996262 32 An optional name member is associated with a wxVariant. This might be used,
09ad05fa
BP
33 for example, in CORBA or OLE automation classes, where named parameters are
34 required.
7c913512 35
09ad05fa
BP
36 Note that as of wxWidgets 2.7.1, wxVariant is
37 @ref overview_refcount "reference counted". Additionally, the convenience
38 macros DECLARE_VARIANT_OBJECT() and IMPLEMENT_VARIANT_OBJECT() were added
39 so that adding (limited) support for conversion to and from wxVariant can
40 be very easily implemented without modifying either wxVariant or the class
41 to be stored by wxVariant. Since assignment operators cannot be declared
42 outside the class, the shift left operators are used like this:
7c913512 43
23324ae1
FM
44 @code
45 // in the header file
c1996262 46 DECLARE_VARIANT_OBJECT(MyClass)
7c913512 47
c1996262
RR
48 // in the implementation file
49 IMPLEMENT_VARIANT_OBJECT(MyClass)
7c913512 50
c1996262
RR
51 // in the user code
52 wxVariant variant;
53 MyClass value;
54 variant << value;
7c913512 55
c1996262
RR
56 // or
57 value << variant;
23324ae1 58 @endcode
7c913512 59
09ad05fa
BP
60 For this to work, MyClass must derive from wxObject, implement the
61 @ref overview_rtti "wxWidgets RTTI system" and support the assignment
62 operator and equality operator for itself. Ideally, it should also be
63 reference counted to make copying operations cheap and fast. This can be
64 most easily implemented using the reference counting support offered by
65 wxObject itself. By default, wxWidgets already implements the shift
66 operator conversion for a few of its drawing related classes:
7c913512 67
23324ae1
FM
68 @code
69 IMPLEMENT_VARIANT_OBJECT(wxColour)
70 IMPLEMENT_VARIANT_OBJECT(wxImage)
71 IMPLEMENT_VARIANT_OBJECT(wxIcon)
72 IMPLEMENT_VARIANT_OBJECT(wxBitmap)
73 @endcode
7c913512 74
09ad05fa
BP
75 Note that as of wxWidgets 2.9.0, wxVariantData no longer inherits from
76 wxObject and wxVariant no longer uses the type-unsafe wxList class for list
23324ae1 77 operations but the type-safe wxVariantList class. Also, wxVariantData now
09ad05fa
BP
78 supports the wxVariantData::Clone() function for implementing the Unshare()
79 function. wxVariantData::Clone() is implemented automatically by
80 IMPLEMENT_VARIANT_OBJECT().
7c913512 81
09ad05fa
BP
82 Since wxVariantData no longer derives from wxObject, any code that tests
83 the type of the data using wxDynamicCast() will require adjustment. You can
84 use the macro wxDynamicCastVariantData() with the same arguments as
85 wxDynamicCast(), to use C++ RTTI type information instead of wxWidgets
86 RTTI.
7c913512 87
23324ae1
FM
88 @library{wxbase}
89 @category{data}
7c913512 90
e54c96f1 91 @see wxVariantData
23324ae1
FM
92*/
93class wxVariant : public wxObject
94{
95public:
23324ae1 96 /**
c1996262 97 Default constructor.
23324ae1
FM
98 */
99 wxVariant();
da1ed74c 100
c1996262 101 /**
09ad05fa
BP
102 Constructs a variant directly with a wxVariantData object. wxVariant
103 will take ownership of the wxVariantData and will not increase its
104 reference count.
c1996262 105 */
da1ed74c
FM
106 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
107
c1996262 108 /**
09ad05fa
BP
109 Constructs a variant from another variant by increasing the reference
110 count.
c1996262 111 */
7c913512 112 wxVariant(const wxVariant& variant);
da1ed74c 113
c1996262
RR
114 /**
115 Constructs a variant from a wide string literal.
116 */
da1ed74c
FM
117 wxVariant(const wxChar* value, const wxString& name = wxEmptyString);
118
c1996262
RR
119 /**
120 Constructs a variant from a string.
121 */
da1ed74c
FM
122 wxVariant(const wxString& value, const wxString& name = wxEmptyString);
123
c1996262
RR
124 /**
125 Constructs a variant from a wide char.
126 */
da1ed74c
FM
127 wxVariant(wxChar value, const wxString& name = wxEmptyString);
128
c1996262
RR
129 /**
130 Constructs a variant from a long.
131 */
da1ed74c
FM
132 wxVariant(long value, const wxString& name = wxEmptyString);
133
c1996262
RR
134 /**
135 Constructs a variant from a bool.
136 */
da1ed74c
FM
137 wxVariant(bool value, const wxString& name = wxEmptyString);
138
c1996262
RR
139 /**
140 Constructs a variant from a double.
141 */
da1ed74c
FM
142 wxVariant(double value, const wxString& name = wxEmptyString);
143
4e00b908
JS
144 /**
145 Constructs a variant from a wxLongLong.
146 */
147 wxVariant(wxLongLong value, const wxString& name = wxEmptyString);
148
149 /**
150 Constructs a variant from a wxULongLong.
151 */
152 wxVariant(wxULongLong value, const wxString& name = wxEmptyString);
153
c1996262
RR
154 /**
155 Constructs a variant from a list of variants
156 */
da1ed74c
FM
157 wxVariant(const wxVariantList& value, const wxString& name = wxEmptyString);
158
c1996262
RR
159 /**
160 Constructs a variant from a void pointer.
161 */
da1ed74c
FM
162 wxVariant(void* value, const wxString& name = wxEmptyString);
163
c1996262
RR
164 /**
165 Constructs a variant from a pointer to an wxObject
166 derived class.
167 */
da1ed74c
FM
168 wxVariant(wxObject* value, const wxString& name = wxEmptyString);
169
c1996262
RR
170 /**
171 Constructs a variant from a wxDateTime.
172 */
cfbe5614 173 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
da1ed74c 174
c1996262
RR
175 /**
176 Constructs a variant from a wxArrayString.
177 */
cfbe5614 178 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
23324ae1
FM
179
180 /**
181 Destructor.
09ad05fa
BP
182
183 @note wxVariantData's destructor is protected, so wxVariantData cannot
184 usually be deleted. Instead, wxVariantData::DecRef() should be
185 called. See @ref overview_refcount_destruct
186 "reference-counted object destruction" for more info.
23324ae1 187 */
adaaa686 188 virtual ~wxVariant();
23324ae1 189
09ad05fa 190
c1996262 191 /**
09ad05fa 192 @name List Functionality
c1996262
RR
193 */
194 //@{
09ad05fa 195
c1996262
RR
196 /**
197 Returns the value at @a idx (zero-based).
198 */
09ad05fa 199 wxVariant operator [](size_t idx) const;
c1996262 200 /**
09ad05fa
BP
201 Returns a reference to the value at @a idx (zero-based). This can be
202 used to change the value at this index.
c1996262 203 */
09ad05fa
BP
204 wxVariant& operator [](size_t idx);
205
23324ae1
FM
206 /**
207 Appends a value to the list.
208 */
209 void Append(const wxVariant& value);
09ad05fa
BP
210
211 /**
212 Makes the variant null by deleting the internal data and set the name
213 to wxEmptyString.
214 */
215 void Clear();
216
c1996262
RR
217 /**
218 Deletes the contents of the list.
219 */
220 void ClearList();
09ad05fa 221
c1996262
RR
222 /**
223 Deletes the zero-based @a item from the list.
224 */
225 bool Delete(size_t item);
09ad05fa 226
c1996262
RR
227 /**
228 Returns the number of elements in the list.
229 */
230 size_t GetCount() const;
09ad05fa 231
c1996262 232 /**
09ad05fa
BP
233 Returns a reference to the wxVariantList class used by wxVariant if
234 this wxVariant is currently a list of variants.
c1996262
RR
235 */
236 wxVariantList& GetList() const;
09ad05fa 237
23324ae1 238 /**
c1996262 239 Inserts a value at the front of the list.
23324ae1 240 */
c1996262 241 void Insert(const wxVariant& value);
09ad05fa 242
c1996262 243 /**
09ad05fa
BP
244 Makes an empty list. This differs from a null variant which has no
245 data; a null list is of type list, but the number of elements in the
246 list is zero.
c1996262
RR
247 */
248 void NullList();
09ad05fa 249
c1996262 250 //@}
23324ae1 251
09ad05fa 252
23324ae1
FM
253 //@{
254 /**
09ad05fa
BP
255 Retrieves and converts the value of this variant to the type that
256 @a value is.
23324ae1 257 */
328f5751 258 bool Convert(long* value) const;
09ad05fa
BP
259 bool Convert(bool* value) const;
260 bool Convert(double* value) const;
261 bool Convert(wxString* value) const;
262 bool Convert(wxChar* value) const;
4e00b908
JS
263 bool Convert(wxLongLong* value) const;
264 bool Convert(wxULongLong* value) const;
09ad05fa 265 bool Convert(wxDateTime* value) const;
23324ae1
FM
266 //@}
267
23324ae1
FM
268 /**
269 Returns the string array value.
270 */
328f5751 271 wxArrayString GetArrayString() const;
23324ae1
FM
272
273 /**
274 Returns the boolean value.
275 */
328f5751 276 bool GetBool() const;
23324ae1
FM
277
278 /**
279 Returns the character value.
280 */
cfbe5614 281 wxUniChar GetChar() const;
23324ae1 282
23324ae1 283 /**
09ad05fa
BP
284 Returns a pointer to the internal variant data. To take ownership of
285 this data, you must call its wxVariantData::IncRef() method. When you
286 stop using it, wxVariantData::DecRef() must be called as well.
23324ae1 287 */
328f5751 288 wxVariantData* GetData() const;
23324ae1
FM
289
290 /**
291 Returns the date value.
292 */
328f5751 293 wxDateTime GetDateTime() const;
23324ae1
FM
294
295 /**
296 Returns the floating point value.
297 */
328f5751 298 double GetDouble() const;
23324ae1 299
23324ae1
FM
300 /**
301 Returns the integer value.
302 */
328f5751 303 long GetLong() const;
23324ae1 304
4e00b908
JS
305 /**
306 Returns the signed 64-bit integer value.
307 */
308 wxLongLong GetLongLong() const;
309
23324ae1
FM
310 /**
311 Returns a constant reference to the variant name.
312 */
cfbe5614 313 const wxString& GetName() const;
23324ae1
FM
314
315 /**
316 Gets the string value.
317 */
328f5751 318 wxString GetString() const;
23324ae1
FM
319
320 /**
09ad05fa
BP
321 Returns the value type as a string.
322
323 The built-in types are:
324 - "bool"
325 - "char"
326 - "datetime"
327 - "double"
328 - "list"
329 - "long"
4e00b908 330 - "longlong"
09ad05fa 331 - "string"
4e00b908 332 - "ulonglong"
09ad05fa
BP
333 - "arrstring"
334 - "void*"
335
336 If the variant is null, the value type returned is the string "null"
337 (not the empty string).
23324ae1 338 */
328f5751 339 wxString GetType() const;
23324ae1 340
4e00b908
JS
341 /**
342 Returns the unsigned 64-bit integer value.
343 */
344 wxULongLong GetULongLong() const;
345
23324ae1
FM
346 /**
347 Gets the void pointer value.
d099c754
VZ
348
349 Notice that this method can be used for null objects (i.e. those for
350 which IsNull() returns @true) and will return @NULL for them.
23324ae1 351 */
328f5751 352 void* GetVoidPtr() const;
23324ae1
FM
353
354 /**
355 Gets the wxObject pointer value.
356 */
328f5751 357 wxObject* GetWxObjectPtr() const;
23324ae1 358
23324ae1 359 /**
09ad05fa
BP
360 Returns @true if there is no data associated with this variant, @false
361 if there is data.
23324ae1 362 */
328f5751 363 bool IsNull() const;
23324ae1
FM
364
365 /**
09ad05fa
BP
366 Returns @true if @a type matches the type of the variant, @false
367 otherwise.
23324ae1 368 */
328f5751 369 bool IsType(const wxString& type) const;
23324ae1
FM
370
371 /**
09ad05fa
BP
372 Returns @true if the data is derived from the class described by
373 @a type, @false otherwise.
23324ae1 374 */
09ad05fa 375 bool IsValueKindOf(const wxClassInfo* type) const;
23324ae1
FM
376
377 /**
378 Makes the variant null by deleting the internal data.
379 */
380 void MakeNull();
381
382 /**
383 Makes a string representation of the variant value (for any type).
384 */
328f5751 385 wxString MakeString() const;
23324ae1
FM
386
387 /**
4cc4bfaf 388 Returns @true if @a value matches an element in the list.
23324ae1 389 */
328f5751 390 bool Member(const wxVariant& value) const;
23324ae1 391
23324ae1 392 /**
09ad05fa
BP
393 Sets the internal variant data, deleting the existing data if there is
394 any.
23324ae1
FM
395 */
396 void SetData(wxVariantData* data);
397
398 /**
09ad05fa
BP
399 Makes sure that any data associated with this variant is not shared
400 with other variants. For this to work, wxVariantData::Clone() must be
401 implemented for the data types you are working with.
402 wxVariantData::Clone() is implemented for all the default data types.
23324ae1
FM
403 */
404 bool Unshare();
405
406 //@{
407 /**
09ad05fa 408 Inequality test operator.
23324ae1 409 */
328f5751 410 bool operator !=(const wxVariant& value) const;
09ad05fa
BP
411 bool operator !=(const wxString& value) const;
412 bool operator !=(const wxChar* value) const;
413 bool operator !=(wxChar value) const;
11e3af6e
FM
414 bool operator !=(long value) const;
415 bool operator !=(bool value) const;
416 bool operator !=(double value) const;
4e00b908
JS
417 bool operator !=(wxLongLong value) const;
418 bool operator !=(wxULongLong value) const;
09ad05fa
BP
419 bool operator !=(void* value) const;
420 bool operator !=(wxObject* value) const;
421 bool operator !=(const wxVariantList& value) const;
422 bool operator !=(const wxArrayString& value) const;
423 bool operator !=(const wxDateTime& value) const;
23324ae1
FM
424 //@}
425
426 //@{
427 /**
09ad05fa
BP
428 Assignment operator, using @ref overview_refcount "reference counting"
429 if possible.
23324ae1
FM
430 */
431 void operator =(const wxVariant& value);
7c913512
FM
432 void operator =(wxVariantData* value);
433 void operator =(const wxString& value);
434 void operator =(const wxChar* value);
435 void operator =(wxChar value);
11e3af6e
FM
436 void operator =(long value);
437 void operator =(bool value);
438 void operator =(double value);
4e00b908
JS
439 bool operator =(wxLongLong value) const;
440 bool operator =(wxULongLong value) const;
7c913512
FM
441 void operator =(void* value);
442 void operator =(wxObject* value);
443 void operator =(const wxVariantList& value);
444 void operator =(const wxDateTime& value);
445 void operator =(const wxArrayString& value);
23324ae1
FM
446 //@}
447
448 //@{
449 /**
09ad05fa 450 Equality test operator.
23324ae1 451 */
328f5751 452 bool operator ==(const wxVariant& value) const;
09ad05fa
BP
453 bool operator ==(const wxString& value) const;
454 bool operator ==(const wxChar* value) const;
455 bool operator ==(wxChar value) const;
11e3af6e
FM
456 bool operator ==(long value) const;
457 bool operator ==(bool value) const;
458 bool operator ==(double value) const;
4e00b908
JS
459 bool operator ==(wxLongLong value) const;
460 bool operator ==(wxULongLong value) const;
09ad05fa
BP
461 bool operator ==(void* value) const;
462 bool operator ==(wxObject* value) const;
463 bool operator ==(const wxVariantList& value) const;
464 bool operator ==(const wxArrayString& value) const;
465 bool operator ==(const wxDateTime& value) const;
23324ae1
FM
466 //@}
467
23324ae1
FM
468 //@{
469 /**
4e00b908
JS
470 Operators for implicit conversion, using appropriate getter member
471 function.
23324ae1 472 */
328f5751 473 double operator double() const;
09ad05fa 474 long operator long() const;
4e00b908
JS
475 wxLongLong operator wxLongLong() const;
476 wxULongLong operator wxULongLong() const;
23324ae1
FM
477 //@}
478
479 /**
09ad05fa
BP
480 Operator for implicit conversion to a pointer to a void, using
481 GetVoidPtr().
23324ae1 482 */
328f5751 483 void* operator void*() const;
23324ae1
FM
484
485 /**
486 Operator for implicit conversion to a wxChar, using GetChar().
487 */
328f5751 488 char operator wxChar() const;
23324ae1
FM
489
490 /**
491 Operator for implicit conversion to a pointer to a wxDateTime, using
492 GetDateTime().
493 */
328f5751 494 void* operator wxDateTime() const;
23324ae1
FM
495
496 /**
497 Operator for implicit conversion to a string, using MakeString().
498 */
328f5751 499 wxString operator wxString() const;
23324ae1
FM
500};
501
502
e54c96f1 503
23324ae1
FM
504/**
505 @class wxVariantData
7c913512 506
09ad05fa 507 The wxVariantData class is used to implement a new type for wxVariant.
23324ae1 508 Derive from wxVariantData, and override the pure virtual functions.
7c913512 509
23324ae1 510 wxVariantData is @ref overview_refcount "reference counted", but you don't
09ad05fa
BP
511 normally have to care about this, as wxVariant manages the count
512 automatically. However, in case your application needs to take ownership of
513 wxVariantData, be aware that the object is created with a reference count
514 of 1, and passing it to wxVariant will not increase this. In other words,
515 IncRef() needs to be called only if you both take ownership of
516 wxVariantData and pass it to a wxVariant. Also note that the destructor is
517 protected, so you can never explicitly delete a wxVariantData instance.
518 Instead, DecRef() will delete the object automatically when the reference
519 count reaches zero.
7c913512 520
23324ae1 521 @library{wxbase}
09ad05fa 522 @category{data}
7c913512 523
09ad05fa 524 @see wxVariant, wxGetVariantCast()
23324ae1 525*/
7c913512 526class wxVariantData
23324ae1
FM
527{
528public:
529 /**
530 Default constructor.
531 */
532 wxVariantData();
533
534 /**
09ad05fa
BP
535 This function can be overridden to clone the data. You must implement
536 this function in order for wxVariant::Unshare() to work for your data.
537 This function is implemented for all built-in data types.
23324ae1 538 */
adaaa686 539 virtual wxVariantData* Clone() const;
23324ae1
FM
540
541 /**
542 Decreases reference count. If the count reaches zero, the object is
543 automatically deleted.
09ad05fa
BP
544
545 @note The destructor of wxVariantData is protected, so delete cannot be
546 used as normal. Instead, DecRef() should be called.
23324ae1
FM
547 */
548 void DecRef();
549
550 /**
09ad05fa 551 Returns @true if this object is equal to @a data.
23324ae1 552 */
da1ed74c 553 virtual bool Eq(wxVariantData& data) const = 0;
23324ae1
FM
554
555 /**
556 Returns the string type of the data.
557 */
da1ed74c 558 virtual wxString GetType() const = 0;
23324ae1
FM
559
560 /**
561 If the data is a wxObject returns a pointer to the objects wxClassInfo
09ad05fa 562 structure, if the data isn't a wxObject the method returns @NULL.
23324ae1 563 */
adaaa686 564 virtual wxClassInfo* GetValueClassInfo();
23324ae1
FM
565
566 /**
09ad05fa
BP
567 Increases reference count. Note that initially wxVariantData has
568 reference count of 1.
23324ae1
FM
569 */
570 void IncRef();
571
23324ae1 572 /**
09ad05fa 573 Reads the data from @a stream.
23324ae1 574 */
cfbe5614
FM
575 virtual bool Read(istream& stream);
576
09ad05fa
BP
577 /**
578 Reads the data from @a string.
579 */
adaaa686 580 virtual bool Read(wxString& string);
23324ae1 581
23324ae1 582 /**
09ad05fa 583 Writes the data to @a stream.
23324ae1 584 */
adaaa686 585 virtual bool Write(ostream& stream) const;
23324ae1 586 /**
09ad05fa 587 Writes the data to @a string.
23324ae1 588 */
adaaa686 589 virtual bool Write(wxString& string) const;
23324ae1 590};
e54c96f1 591
09ad05fa
BP
592
593
594// ============================================================================
595// Global functions/macros
596// ============================================================================
597
b21126db 598/** @addtogroup group_funcmacro_rtti */
09ad05fa
BP
599//@{
600
601/**
602 This macro returns a pointer to the data stored in @a var (wxVariant) cast
603 to the type @a classname if the data is of this type (the check is done
604 during the run-time) or @NULL otherwise.
605
606 @header{wx/variant.h}
607
608 @see @ref overview_rtti, wxDynamicCast()
609*/
610#define wxGetVariantCast(var, classname)
611
612//@}
613