]> git.saurik.com Git - wxWidgets.git/blame - interface/variant.h
mac paths updated
[wxWidgets.git] / interface / 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
11 @wxheader{variant.h}
7c913512 12
09ad05fa 13 The wxVariant class represents a container for any type. A variant's value
c1996262 14 can be changed at run time, possibly to a different type of value.
7c913512 15
23324ae1 16 As standard, wxVariant can store values of type bool, wxChar, double, long,
09ad05fa
BP
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.
24
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
28 runtime.
29
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.
7c913512 32
c1996262 33 An optional name member is associated with a wxVariant. This might be used,
09ad05fa
BP
34 for example, in CORBA or OLE automation classes, where named parameters are
35 required.
7c913512 36
09ad05fa
BP
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:
7c913512 44
23324ae1
FM
45 @code
46 // in the header file
c1996262 47 DECLARE_VARIANT_OBJECT(MyClass)
7c913512 48
c1996262
RR
49 // in the implementation file
50 IMPLEMENT_VARIANT_OBJECT(MyClass)
7c913512 51
c1996262
RR
52 // in the user code
53 wxVariant variant;
54 MyClass value;
55 variant << value;
7c913512 56
c1996262
RR
57 // or
58 value << variant;
23324ae1 59 @endcode
7c913512 60
09ad05fa
BP
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:
7c913512 68
23324ae1
FM
69 @code
70 IMPLEMENT_VARIANT_OBJECT(wxColour)
71 IMPLEMENT_VARIANT_OBJECT(wxImage)
72 IMPLEMENT_VARIANT_OBJECT(wxIcon)
73 IMPLEMENT_VARIANT_OBJECT(wxBitmap)
74 @endcode
7c913512 75
09ad05fa
BP
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
23324ae1 78 operations but the type-safe wxVariantList class. Also, wxVariantData now
09ad05fa
BP
79 supports the wxVariantData::Clone() function for implementing the Unshare()
80 function. wxVariantData::Clone() is implemented automatically by
81 IMPLEMENT_VARIANT_OBJECT().
7c913512 82
09ad05fa
BP
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
87 RTTI.
7c913512 88
23324ae1
FM
89 @library{wxbase}
90 @category{data}
7c913512 91
e54c96f1 92 @see wxVariantData
23324ae1
FM
93*/
94class wxVariant : public wxObject
95{
96public:
23324ae1 97 /**
c1996262 98 Default constructor.
23324ae1
FM
99 */
100 wxVariant();
c1996262
RR
101
102 /**
09ad05fa
BP
103 Constructs a variant directly with a wxVariantData object. wxVariant
104 will take ownership of the wxVariantData and will not increase its
105 reference count.
c1996262
RR
106 */
107 wxVariant(wxVariantData* data, const wxString& name = "");
108
109 /**
09ad05fa
BP
110 Constructs a variant from another variant by increasing the reference
111 count.
c1996262 112 */
7c913512 113 wxVariant(const wxVariant& variant);
c1996262
RR
114
115 /**
116 Constructs a variant from a wide string literal.
117 */
7c913512 118 wxVariant(const wxChar* value, const wxString& name = "");
c1996262
RR
119
120 /**
121 Constructs a variant from a string.
122 */
7c913512 123 wxVariant(const wxString& value, const wxString& name = "");
c1996262
RR
124
125 /**
126 Constructs a variant from a wide char.
127 */
7c913512 128 wxVariant(wxChar value, const wxString& name = "");
c1996262
RR
129
130 /**
131 Constructs a variant from a long.
132 */
7c913512 133 wxVariant(long value, const wxString& name = "");
c1996262
RR
134
135 /**
136 Constructs a variant from a bool.
137 */
7c913512 138 wxVariant(bool value, const wxString& name = "");
c1996262
RR
139
140 /**
141 Constructs a variant from a double.
142 */
7c913512 143 wxVariant(double value, const wxString& name = "");
c1996262
RR
144
145 /**
146 Constructs a variant from a list of variants
147 */
09ad05fa 148 wxVariant(const wxVariantList& value, const wxString& name = "");
c1996262
RR
149
150 /**
151 Constructs a variant from a void pointer.
152 */
7c913512 153 wxVariant(void* value, const wxString& name = "");
c1996262
RR
154
155 /**
156 Constructs a variant from a pointer to an wxObject
157 derived class.
158 */
7c913512 159 wxVariant(wxObject* value, const wxString& name = "");
c1996262
RR
160
161 /**
162 Constructs a variant from a wxDateTime.
163 */
7c913512 164 wxVariant(wxDateTime& val, const wxString& name = "");
c1996262
RR
165
166 /**
167 Constructs a variant from a wxArrayString.
168 */
7c913512 169 wxVariant(wxArrayString& val, const wxString& name = "");
23324ae1
FM
170
171 /**
172 Destructor.
09ad05fa
BP
173
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.
23324ae1
FM
178 */
179 ~wxVariant();
180
09ad05fa 181
c1996262 182 /**
09ad05fa 183 @name List Functionality
c1996262
RR
184 */
185 //@{
09ad05fa 186
c1996262
RR
187 /**
188 Returns the value at @a idx (zero-based).
189 */
09ad05fa 190 wxVariant operator [](size_t idx) const;
c1996262 191 /**
09ad05fa
BP
192 Returns a reference to the value at @a idx (zero-based). This can be
193 used to change the value at this index.
c1996262 194 */
09ad05fa
BP
195 wxVariant& operator [](size_t idx);
196
23324ae1
FM
197 /**
198 Appends a value to the list.
199 */
200 void Append(const wxVariant& value);
09ad05fa
BP
201
202 /**
203 Makes the variant null by deleting the internal data and set the name
204 to wxEmptyString.
205 */
206 void Clear();
207
c1996262
RR
208 /**
209 Deletes the contents of the list.
210 */
211 void ClearList();
09ad05fa 212
c1996262
RR
213 /**
214 Deletes the zero-based @a item from the list.
215 */
216 bool Delete(size_t item);
09ad05fa 217
c1996262
RR
218 /**
219 Returns the number of elements in the list.
220 */
221 size_t GetCount() const;
09ad05fa 222
c1996262 223 /**
09ad05fa
BP
224 Returns a reference to the wxVariantList class used by wxVariant if
225 this wxVariant is currently a list of variants.
c1996262
RR
226 */
227 wxVariantList& GetList() const;
09ad05fa 228
23324ae1 229 /**
c1996262 230 Inserts a value at the front of the list.
23324ae1 231 */
c1996262 232 void Insert(const wxVariant& value);
09ad05fa 233
c1996262 234 /**
09ad05fa
BP
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
237 list is zero.
c1996262
RR
238 */
239 void NullList();
09ad05fa 240
c1996262 241 //@}
23324ae1 242
09ad05fa 243
23324ae1
FM
244 //@{
245 /**
09ad05fa
BP
246 Retrieves and converts the value of this variant to the type that
247 @a value is.
23324ae1 248 */
328f5751 249 bool Convert(long* value) const;
09ad05fa
BP
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;
23324ae1
FM
255 //@}
256
23324ae1
FM
257 /**
258 Returns the string array value.
259 */
328f5751 260 wxArrayString GetArrayString() const;
23324ae1
FM
261
262 /**
263 Returns the boolean value.
264 */
328f5751 265 bool GetBool() const;
23324ae1
FM
266
267 /**
268 Returns the character value.
269 */
328f5751 270 wxChar GetChar() const;
23324ae1 271
23324ae1 272 /**
09ad05fa
BP
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.
23324ae1 276 */
328f5751 277 wxVariantData* GetData() const;
23324ae1
FM
278
279 /**
280 Returns the date value.
281 */
328f5751 282 wxDateTime GetDateTime() const;
23324ae1
FM
283
284 /**
285 Returns the floating point value.
286 */
328f5751 287 double GetDouble() const;
23324ae1 288
23324ae1
FM
289 /**
290 Returns the integer value.
291 */
328f5751 292 long GetLong() const;
23324ae1
FM
293
294 /**
295 Returns a constant reference to the variant name.
296 */
328f5751 297 const wxString GetName() const;
23324ae1
FM
298
299 /**
300 Gets the string value.
301 */
328f5751 302 wxString GetString() const;
23324ae1
FM
303
304 /**
09ad05fa
BP
305 Returns the value type as a string.
306
307 The built-in types are:
308 - "bool"
309 - "char"
310 - "datetime"
311 - "double"
312 - "list"
313 - "long"
314 - "string"
315 - "arrstring"
316 - "void*"
317
318 If the variant is null, the value type returned is the string "null"
319 (not the empty string).
23324ae1 320 */
328f5751 321 wxString GetType() const;
23324ae1
FM
322
323 /**
324 Gets the void pointer value.
325 */
328f5751 326 void* GetVoidPtr() const;
23324ae1
FM
327
328 /**
329 Gets the wxObject pointer value.
330 */
328f5751 331 wxObject* GetWxObjectPtr() const;
23324ae1 332
23324ae1 333 /**
09ad05fa
BP
334 Returns @true if there is no data associated with this variant, @false
335 if there is data.
23324ae1 336 */
328f5751 337 bool IsNull() const;
23324ae1
FM
338
339 /**
09ad05fa
BP
340 Returns @true if @a type matches the type of the variant, @false
341 otherwise.
23324ae1 342 */
328f5751 343 bool IsType(const wxString& type) const;
23324ae1
FM
344
345 /**
09ad05fa
BP
346 Returns @true if the data is derived from the class described by
347 @a type, @false otherwise.
23324ae1 348 */
09ad05fa 349 bool IsValueKindOf(const wxClassInfo* type) const;
23324ae1
FM
350
351 /**
352 Makes the variant null by deleting the internal data.
353 */
354 void MakeNull();
355
356 /**
357 Makes a string representation of the variant value (for any type).
358 */
328f5751 359 wxString MakeString() const;
23324ae1
FM
360
361 /**
4cc4bfaf 362 Returns @true if @a value matches an element in the list.
23324ae1 363 */
328f5751 364 bool Member(const wxVariant& value) const;
23324ae1 365
23324ae1 366 /**
09ad05fa
BP
367 Sets the internal variant data, deleting the existing data if there is
368 any.
23324ae1
FM
369 */
370 void SetData(wxVariantData* data);
371
372 /**
09ad05fa
BP
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.
23324ae1
FM
377 */
378 bool Unshare();
379
380 //@{
381 /**
09ad05fa 382 Inequality test operator.
23324ae1 383 */
328f5751 384 bool operator !=(const wxVariant& value) const;
09ad05fa
BP
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;
23324ae1
FM
396 //@}
397
398 //@{
399 /**
09ad05fa
BP
400 Assignment operator, using @ref overview_refcount "reference counting"
401 if possible.
23324ae1
FM
402 */
403 void operator =(const wxVariant& value);
7c913512
FM
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);
23324ae1
FM
416 //@}
417
418 //@{
419 /**
09ad05fa 420 Equality test operator.
23324ae1 421 */
328f5751 422 bool operator ==(const wxVariant& value) const;
09ad05fa
BP
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;
23324ae1
FM
434 //@}
435
23324ae1
FM
436 //@{
437 /**
438 Operator for implicit conversion to a long, using GetLong().
439 */
328f5751 440 double operator double() const;
09ad05fa 441 long operator long() const;
23324ae1
FM
442 //@}
443
444 /**
09ad05fa
BP
445 Operator for implicit conversion to a pointer to a void, using
446 GetVoidPtr().
23324ae1 447 */
328f5751 448 void* operator void*() const;
23324ae1
FM
449
450 /**
451 Operator for implicit conversion to a wxChar, using GetChar().
452 */
328f5751 453 char operator wxChar() const;
23324ae1
FM
454
455 /**
456 Operator for implicit conversion to a pointer to a wxDateTime, using
457 GetDateTime().
458 */
328f5751 459 void* operator wxDateTime() const;
23324ae1
FM
460
461 /**
462 Operator for implicit conversion to a string, using MakeString().
463 */
328f5751 464 wxString operator wxString() const;
23324ae1
FM
465};
466
467
e54c96f1 468
23324ae1
FM
469/**
470 @class wxVariantData
471 @wxheader{variant.h}
7c913512 472
09ad05fa 473 The wxVariantData class is used to implement a new type for wxVariant.
23324ae1 474 Derive from wxVariantData, and override the pure virtual functions.
7c913512 475
23324ae1 476 wxVariantData is @ref overview_refcount "reference counted", but you don't
09ad05fa
BP
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
485 count reaches zero.
7c913512 486
23324ae1 487 @library{wxbase}
09ad05fa 488 @category{data}
7c913512 489
09ad05fa 490 @see wxVariant, wxGetVariantCast()
23324ae1 491*/
7c913512 492class wxVariantData
23324ae1
FM
493{
494public:
495 /**
496 Default constructor.
497 */
498 wxVariantData();
499
500 /**
09ad05fa
BP
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.
23324ae1 504 */
328f5751 505 wxVariantData* Clone() const;
23324ae1
FM
506
507 /**
508 Decreases reference count. If the count reaches zero, the object is
509 automatically deleted.
09ad05fa
BP
510
511 @note The destructor of wxVariantData is protected, so delete cannot be
512 used as normal. Instead, DecRef() should be called.
23324ae1
FM
513 */
514 void DecRef();
515
516 /**
09ad05fa 517 Returns @true if this object is equal to @a data.
23324ae1 518 */
328f5751 519 bool Eq(wxVariantData& data) const;
23324ae1
FM
520
521 /**
522 Returns the string type of the data.
523 */
328f5751 524 wxString GetType() const;
23324ae1
FM
525
526 /**
527 If the data is a wxObject returns a pointer to the objects wxClassInfo
09ad05fa 528 structure, if the data isn't a wxObject the method returns @NULL.
23324ae1 529 */
328f5751 530 wxClassInfo* GetValueClassInfo() const;
23324ae1
FM
531
532 /**
09ad05fa
BP
533 Increases reference count. Note that initially wxVariantData has
534 reference count of 1.
23324ae1
FM
535 */
536 void IncRef();
537
23324ae1 538 /**
09ad05fa 539 Reads the data from @a stream.
23324ae1
FM
540 */
541 bool Read(ostream& stream);
09ad05fa
BP
542 /**
543 Reads the data from @a string.
544 */
7c913512 545 bool Read(wxString& string);
23324ae1 546
23324ae1 547 /**
09ad05fa 548 Writes the data to @a stream.
23324ae1 549 */
328f5751 550 bool Write(ostream& stream) const;
23324ae1 551 /**
09ad05fa 552 Writes the data to @a string.
23324ae1 553 */
09ad05fa 554 bool Write(wxString& string) const;
23324ae1 555};
e54c96f1 556
09ad05fa
BP
557
558
559// ============================================================================
560// Global functions/macros
561// ============================================================================
562
563/** @ingroup group_funcmacro_rtti */
564//@{
565
566/**
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.
570
571 @header{wx/variant.h}
572
573 @see @ref overview_rtti, wxDynamicCast()
574*/
575#define wxGetVariantCast(var, classname)
576
577//@}
578