]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/variant.h
return wxStandardPaths, not wxStandardPathsBase, from wxStandardPathsBase::Get()...
[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
c1996262
RR
144 /**
145 Constructs a variant from a list of variants
146 */
da1ed74c
FM
147 wxVariant(const wxVariantList& value, const wxString& name = wxEmptyString);
148
c1996262
RR
149 /**
150 Constructs a variant from a void pointer.
151 */
da1ed74c
FM
152 wxVariant(void* value, const wxString& name = wxEmptyString);
153
c1996262
RR
154 /**
155 Constructs a variant from a pointer to an wxObject
156 derived class.
157 */
da1ed74c
FM
158 wxVariant(wxObject* value, const wxString& name = wxEmptyString);
159
c1996262
RR
160 /**
161 Constructs a variant from a wxDateTime.
162 */
cfbe5614 163 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
da1ed74c 164
c1996262
RR
165 /**
166 Constructs a variant from a wxArrayString.
167 */
cfbe5614 168 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
23324ae1
FM
169
170 /**
171 Destructor.
09ad05fa
BP
172
173 @note wxVariantData's destructor is protected, so wxVariantData cannot
174 usually be deleted. Instead, wxVariantData::DecRef() should be
175 called. See @ref overview_refcount_destruct
176 "reference-counted object destruction" for more info.
23324ae1 177 */
adaaa686 178 virtual ~wxVariant();
23324ae1 179
09ad05fa 180
c1996262 181 /**
09ad05fa 182 @name List Functionality
c1996262
RR
183 */
184 //@{
09ad05fa 185
c1996262
RR
186 /**
187 Returns the value at @a idx (zero-based).
188 */
09ad05fa 189 wxVariant operator [](size_t idx) const;
c1996262 190 /**
09ad05fa
BP
191 Returns a reference to the value at @a idx (zero-based). This can be
192 used to change the value at this index.
c1996262 193 */
09ad05fa
BP
194 wxVariant& operator [](size_t idx);
195
23324ae1
FM
196 /**
197 Appends a value to the list.
198 */
199 void Append(const wxVariant& value);
09ad05fa
BP
200
201 /**
202 Makes the variant null by deleting the internal data and set the name
203 to wxEmptyString.
204 */
205 void Clear();
206
c1996262
RR
207 /**
208 Deletes the contents of the list.
209 */
210 void ClearList();
09ad05fa 211
c1996262
RR
212 /**
213 Deletes the zero-based @a item from the list.
214 */
215 bool Delete(size_t item);
09ad05fa 216
c1996262
RR
217 /**
218 Returns the number of elements in the list.
219 */
220 size_t GetCount() const;
09ad05fa 221
c1996262 222 /**
09ad05fa
BP
223 Returns a reference to the wxVariantList class used by wxVariant if
224 this wxVariant is currently a list of variants.
c1996262
RR
225 */
226 wxVariantList& GetList() const;
09ad05fa 227
23324ae1 228 /**
c1996262 229 Inserts a value at the front of the list.
23324ae1 230 */
c1996262 231 void Insert(const wxVariant& value);
09ad05fa 232
c1996262 233 /**
09ad05fa
BP
234 Makes an empty list. This differs from a null variant which has no
235 data; a null list is of type list, but the number of elements in the
236 list is zero.
c1996262
RR
237 */
238 void NullList();
09ad05fa 239
c1996262 240 //@}
23324ae1 241
09ad05fa 242
23324ae1
FM
243 //@{
244 /**
09ad05fa
BP
245 Retrieves and converts the value of this variant to the type that
246 @a value is.
23324ae1 247 */
328f5751 248 bool Convert(long* value) const;
09ad05fa
BP
249 bool Convert(bool* value) const;
250 bool Convert(double* value) const;
251 bool Convert(wxString* value) const;
252 bool Convert(wxChar* value) const;
253 bool Convert(wxDateTime* value) const;
23324ae1
FM
254 //@}
255
23324ae1
FM
256 /**
257 Returns the string array value.
258 */
328f5751 259 wxArrayString GetArrayString() const;
23324ae1
FM
260
261 /**
262 Returns the boolean value.
263 */
328f5751 264 bool GetBool() const;
23324ae1
FM
265
266 /**
267 Returns the character value.
268 */
cfbe5614 269 wxUniChar GetChar() const;
23324ae1 270
23324ae1 271 /**
09ad05fa
BP
272 Returns a pointer to the internal variant data. To take ownership of
273 this data, you must call its wxVariantData::IncRef() method. When you
274 stop using it, wxVariantData::DecRef() must be called as well.
23324ae1 275 */
328f5751 276 wxVariantData* GetData() const;
23324ae1
FM
277
278 /**
279 Returns the date value.
280 */
328f5751 281 wxDateTime GetDateTime() const;
23324ae1
FM
282
283 /**
284 Returns the floating point value.
285 */
328f5751 286 double GetDouble() const;
23324ae1 287
23324ae1
FM
288 /**
289 Returns the integer value.
290 */
328f5751 291 long GetLong() const;
23324ae1
FM
292
293 /**
294 Returns a constant reference to the variant name.
295 */
cfbe5614 296 const wxString& GetName() const;
23324ae1
FM
297
298 /**
299 Gets the string value.
300 */
328f5751 301 wxString GetString() const;
23324ae1
FM
302
303 /**
09ad05fa
BP
304 Returns the value type as a string.
305
306 The built-in types are:
307 - "bool"
308 - "char"
309 - "datetime"
310 - "double"
311 - "list"
312 - "long"
313 - "string"
314 - "arrstring"
315 - "void*"
316
317 If the variant is null, the value type returned is the string "null"
318 (not the empty string).
23324ae1 319 */
328f5751 320 wxString GetType() const;
23324ae1
FM
321
322 /**
323 Gets the void pointer value.
d099c754
VZ
324
325 Notice that this method can be used for null objects (i.e. those for
326 which IsNull() returns @true) and will return @NULL for them.
23324ae1 327 */
328f5751 328 void* GetVoidPtr() const;
23324ae1
FM
329
330 /**
331 Gets the wxObject pointer value.
332 */
328f5751 333 wxObject* GetWxObjectPtr() const;
23324ae1 334
23324ae1 335 /**
09ad05fa
BP
336 Returns @true if there is no data associated with this variant, @false
337 if there is data.
23324ae1 338 */
328f5751 339 bool IsNull() const;
23324ae1
FM
340
341 /**
09ad05fa
BP
342 Returns @true if @a type matches the type of the variant, @false
343 otherwise.
23324ae1 344 */
328f5751 345 bool IsType(const wxString& type) const;
23324ae1
FM
346
347 /**
09ad05fa
BP
348 Returns @true if the data is derived from the class described by
349 @a type, @false otherwise.
23324ae1 350 */
09ad05fa 351 bool IsValueKindOf(const wxClassInfo* type) const;
23324ae1
FM
352
353 /**
354 Makes the variant null by deleting the internal data.
355 */
356 void MakeNull();
357
358 /**
359 Makes a string representation of the variant value (for any type).
360 */
328f5751 361 wxString MakeString() const;
23324ae1
FM
362
363 /**
4cc4bfaf 364 Returns @true if @a value matches an element in the list.
23324ae1 365 */
328f5751 366 bool Member(const wxVariant& value) const;
23324ae1 367
23324ae1 368 /**
09ad05fa
BP
369 Sets the internal variant data, deleting the existing data if there is
370 any.
23324ae1
FM
371 */
372 void SetData(wxVariantData* data);
373
374 /**
09ad05fa
BP
375 Makes sure that any data associated with this variant is not shared
376 with other variants. For this to work, wxVariantData::Clone() must be
377 implemented for the data types you are working with.
378 wxVariantData::Clone() is implemented for all the default data types.
23324ae1
FM
379 */
380 bool Unshare();
381
382 //@{
383 /**
09ad05fa 384 Inequality test operator.
23324ae1 385 */
328f5751 386 bool operator !=(const wxVariant& value) const;
09ad05fa
BP
387 bool operator !=(const wxString& value) const;
388 bool operator !=(const wxChar* value) const;
389 bool operator !=(wxChar value) const;
11e3af6e
FM
390 bool operator !=(long value) const;
391 bool operator !=(bool value) const;
392 bool operator !=(double value) const;
09ad05fa
BP
393 bool operator !=(void* value) const;
394 bool operator !=(wxObject* value) const;
395 bool operator !=(const wxVariantList& value) const;
396 bool operator !=(const wxArrayString& value) const;
397 bool operator !=(const wxDateTime& value) const;
23324ae1
FM
398 //@}
399
400 //@{
401 /**
09ad05fa
BP
402 Assignment operator, using @ref overview_refcount "reference counting"
403 if possible.
23324ae1
FM
404 */
405 void operator =(const wxVariant& value);
7c913512
FM
406 void operator =(wxVariantData* value);
407 void operator =(const wxString& value);
408 void operator =(const wxChar* value);
409 void operator =(wxChar value);
11e3af6e
FM
410 void operator =(long value);
411 void operator =(bool value);
412 void operator =(double value);
7c913512
FM
413 void operator =(void* value);
414 void operator =(wxObject* value);
415 void operator =(const wxVariantList& value);
416 void operator =(const wxDateTime& value);
417 void operator =(const wxArrayString& value);
23324ae1
FM
418 //@}
419
420 //@{
421 /**
09ad05fa 422 Equality test operator.
23324ae1 423 */
328f5751 424 bool operator ==(const wxVariant& value) const;
09ad05fa
BP
425 bool operator ==(const wxString& value) const;
426 bool operator ==(const wxChar* value) const;
427 bool operator ==(wxChar value) const;
11e3af6e
FM
428 bool operator ==(long value) const;
429 bool operator ==(bool value) const;
430 bool operator ==(double value) const;
09ad05fa
BP
431 bool operator ==(void* value) const;
432 bool operator ==(wxObject* value) const;
433 bool operator ==(const wxVariantList& value) const;
434 bool operator ==(const wxArrayString& value) const;
435 bool operator ==(const wxDateTime& value) const;
23324ae1
FM
436 //@}
437
23324ae1
FM
438 //@{
439 /**
440 Operator for implicit conversion to a long, using GetLong().
441 */
328f5751 442 double operator double() const;
09ad05fa 443 long operator long() const;
23324ae1
FM
444 //@}
445
446 /**
09ad05fa
BP
447 Operator for implicit conversion to a pointer to a void, using
448 GetVoidPtr().
23324ae1 449 */
328f5751 450 void* operator void*() const;
23324ae1
FM
451
452 /**
453 Operator for implicit conversion to a wxChar, using GetChar().
454 */
328f5751 455 char operator wxChar() const;
23324ae1
FM
456
457 /**
458 Operator for implicit conversion to a pointer to a wxDateTime, using
459 GetDateTime().
460 */
328f5751 461 void* operator wxDateTime() const;
23324ae1
FM
462
463 /**
464 Operator for implicit conversion to a string, using MakeString().
465 */
328f5751 466 wxString operator wxString() const;
23324ae1
FM
467};
468
469
e54c96f1 470
23324ae1
FM
471/**
472 @class wxVariantData
7c913512 473
09ad05fa 474 The wxVariantData class is used to implement a new type for wxVariant.
23324ae1 475 Derive from wxVariantData, and override the pure virtual functions.
7c913512 476
23324ae1 477 wxVariantData is @ref overview_refcount "reference counted", but you don't
09ad05fa
BP
478 normally have to care about this, as wxVariant manages the count
479 automatically. However, in case your application needs to take ownership of
480 wxVariantData, be aware that the object is created with a reference count
481 of 1, and passing it to wxVariant will not increase this. In other words,
482 IncRef() needs to be called only if you both take ownership of
483 wxVariantData and pass it to a wxVariant. Also note that the destructor is
484 protected, so you can never explicitly delete a wxVariantData instance.
485 Instead, DecRef() will delete the object automatically when the reference
486 count reaches zero.
7c913512 487
23324ae1 488 @library{wxbase}
09ad05fa 489 @category{data}
7c913512 490
09ad05fa 491 @see wxVariant, wxGetVariantCast()
23324ae1 492*/
7c913512 493class wxVariantData
23324ae1
FM
494{
495public:
496 /**
497 Default constructor.
498 */
499 wxVariantData();
500
501 /**
09ad05fa
BP
502 This function can be overridden to clone the data. You must implement
503 this function in order for wxVariant::Unshare() to work for your data.
504 This function is implemented for all built-in data types.
23324ae1 505 */
adaaa686 506 virtual wxVariantData* Clone() const;
23324ae1
FM
507
508 /**
509 Decreases reference count. If the count reaches zero, the object is
510 automatically deleted.
09ad05fa
BP
511
512 @note The destructor of wxVariantData is protected, so delete cannot be
513 used as normal. Instead, DecRef() should be called.
23324ae1
FM
514 */
515 void DecRef();
516
517 /**
09ad05fa 518 Returns @true if this object is equal to @a data.
23324ae1 519 */
da1ed74c 520 virtual bool Eq(wxVariantData& data) const = 0;
23324ae1
FM
521
522 /**
523 Returns the string type of the data.
524 */
da1ed74c 525 virtual wxString GetType() const = 0;
23324ae1
FM
526
527 /**
528 If the data is a wxObject returns a pointer to the objects wxClassInfo
09ad05fa 529 structure, if the data isn't a wxObject the method returns @NULL.
23324ae1 530 */
adaaa686 531 virtual wxClassInfo* GetValueClassInfo();
23324ae1
FM
532
533 /**
09ad05fa
BP
534 Increases reference count. Note that initially wxVariantData has
535 reference count of 1.
23324ae1
FM
536 */
537 void IncRef();
538
23324ae1 539 /**
09ad05fa 540 Reads the data from @a stream.
23324ae1 541 */
cfbe5614
FM
542 virtual bool Read(istream& stream);
543
09ad05fa
BP
544 /**
545 Reads the data from @a string.
546 */
adaaa686 547 virtual bool Read(wxString& string);
23324ae1 548
23324ae1 549 /**
09ad05fa 550 Writes the data to @a stream.
23324ae1 551 */
adaaa686 552 virtual bool Write(ostream& stream) const;
23324ae1 553 /**
09ad05fa 554 Writes the data to @a string.
23324ae1 555 */
adaaa686 556 virtual bool Write(wxString& string) const;
23324ae1 557};
e54c96f1 558
09ad05fa
BP
559
560
561// ============================================================================
562// Global functions/macros
563// ============================================================================
564
b21126db 565/** @addtogroup group_funcmacro_rtti */
09ad05fa
BP
566//@{
567
568/**
569 This macro returns a pointer to the data stored in @a var (wxVariant) cast
570 to the type @a classname if the data is of this type (the check is done
571 during the run-time) or @NULL otherwise.
572
573 @header{wx/variant.h}
574
575 @see @ref overview_rtti, wxDynamicCast()
576*/
577#define wxGetVariantCast(var, classname)
578
579//@}
580