]>
Commit | Line | Data |
---|---|---|
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 | |
c1996262 RR |
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. | |
7c913512 | 15 | |
23324ae1 | 16 | As standard, wxVariant can store values of type bool, wxChar, double, long, |
c1996262 | 17 | string, string list, time, date, void pointer, list of strings, and list of variants. |
23324ae1 | 18 | However, an application can extend wxVariant's capabilities by deriving from the |
c1996262 RR |
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 | |
23324ae1 FM |
22 | object, unlike the case for basic data types where convenience functions such as |
23 | wxVariant::GetLong can be used. | |
7c913512 | 24 | |
c1996262 RR |
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. | |
7c913512 | 28 | |
c1996262 RR |
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. | |
7c913512 | 31 | |
c1996262 RR |
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. | |
7c913512 | 34 | |
23324ae1 | 35 | Note that as of wxWidgets 2.7.1, wxVariant is @ref overview_trefcount |
7c913512 FM |
36 | "reference counted". |
37 | Additionally, the convenience macros @b DECLARE_VARIANT_OBJECT and | |
38 | @b IMPLEMENT_VARIANT_OBJECT were added so that adding (limited) support | |
c1996262 RR |
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: | |
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 | |
23324ae1 FM |
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, | |
63 | it | |
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: | |
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 | |
23324ae1 FM |
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. | |
7c913512 | 81 | |
23324ae1 | 82 | Since wxVariantData no longer derives from wxObject, any code that tests the |
c1996262 RR |
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. | |
7c913512 | 86 | |
23324ae1 FM |
87 | @library{wxbase} |
88 | @category{data} | |
7c913512 | 89 | |
e54c96f1 | 90 | @see wxVariantData |
23324ae1 FM |
91 | */ |
92 | class wxVariant : public wxObject | |
93 | { | |
94 | public: | |
23324ae1 | 95 | /** |
c1996262 | 96 | Default constructor. |
23324ae1 FM |
97 | */ |
98 | wxVariant(); | |
c1996262 RR |
99 | |
100 | /** | |
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. | |
104 | */ | |
105 | wxVariant(wxVariantData* data, const wxString& name = ""); | |
106 | ||
107 | /** | |
108 | Constructs a variant from another variant by increasing the | |
109 | reference count. | |
110 | */ | |
7c913512 | 111 | wxVariant(const wxVariant& variant); |
c1996262 RR |
112 | |
113 | /** | |
114 | Constructs a variant from a wide string literal. | |
115 | */ | |
7c913512 | 116 | wxVariant(const wxChar* value, const wxString& name = ""); |
c1996262 RR |
117 | |
118 | /** | |
119 | Constructs a variant from a string. | |
120 | */ | |
7c913512 | 121 | wxVariant(const wxString& value, const wxString& name = ""); |
c1996262 RR |
122 | |
123 | /** | |
124 | Constructs a variant from a wide char. | |
125 | */ | |
7c913512 | 126 | wxVariant(wxChar value, const wxString& name = ""); |
c1996262 RR |
127 | |
128 | /** | |
129 | Constructs a variant from a long. | |
130 | */ | |
7c913512 | 131 | wxVariant(long value, const wxString& name = ""); |
c1996262 RR |
132 | |
133 | /** | |
134 | Constructs a variant from a bool. | |
135 | */ | |
7c913512 | 136 | wxVariant(bool value, const wxString& name = ""); |
c1996262 RR |
137 | |
138 | /** | |
139 | Constructs a variant from a double. | |
140 | */ | |
7c913512 | 141 | wxVariant(double value, const wxString& name = ""); |
c1996262 RR |
142 | |
143 | /** | |
144 | Constructs a variant from a list of variants | |
145 | */ | |
7c913512 FM |
146 | wxVariant(const wxVariantList& value, |
147 | const wxString& name = ""); | |
c1996262 RR |
148 | |
149 | /** | |
150 | Constructs a variant from a void pointer. | |
151 | */ | |
7c913512 | 152 | wxVariant(void* value, const wxString& name = ""); |
c1996262 RR |
153 | |
154 | /** | |
155 | Constructs a variant from a pointer to an wxObject | |
156 | derived class. | |
157 | */ | |
7c913512 | 158 | wxVariant(wxObject* value, const wxString& name = ""); |
c1996262 RR |
159 | |
160 | /** | |
161 | Constructs a variant from a wxDateTime. | |
162 | */ | |
7c913512 | 163 | wxVariant(wxDateTime& val, const wxString& name = ""); |
c1996262 RR |
164 | |
165 | /** | |
166 | Constructs a variant from a wxArrayString. | |
167 | */ | |
7c913512 | 168 | wxVariant(wxArrayString& val, const wxString& name = ""); |
23324ae1 FM |
169 | |
170 | /** | |
171 | Destructor. | |
23324ae1 FM |
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 | |
175 | more info. | |
176 | */ | |
177 | ~wxVariant(); | |
178 | ||
c1996262 RR |
179 | /** |
180 | @name List functionality | |
181 | */ | |
182 | //@{ | |
183 | /** | |
184 | Returns the value at @a idx (zero-based). | |
185 | */ | |
186 | wxVariant operator [](size_t idx); | |
187 | /** | |
188 | Returns a reference to the value at @a idx (zero-based). This can be used | |
189 | to change the value at this index. | |
190 | */ | |
191 | const wxVariant& operator [](size_t idx); | |
23324ae1 FM |
192 | /** |
193 | Appends a value to the list. | |
194 | */ | |
195 | void Append(const wxVariant& value); | |
c1996262 RR |
196 | /** |
197 | Deletes the contents of the list. | |
198 | */ | |
199 | void ClearList(); | |
200 | /** | |
201 | Deletes the zero-based @a item from the list. | |
202 | */ | |
203 | bool Delete(size_t item); | |
204 | /** | |
205 | Returns the number of elements in the list. | |
206 | */ | |
207 | size_t GetCount() const; | |
208 | /** | |
209 | Returns a reference to the wxVariantList class used by | |
210 | wxVariant if this wxVariant is currently a list of variants. | |
211 | */ | |
212 | wxVariantList& GetList() const; | |
23324ae1 FM |
213 | /** |
214 | Makes the variant null by deleting the internal data and | |
215 | set the name to @e wxEmptyString. | |
216 | */ | |
217 | void Clear(); | |
23324ae1 | 218 | /** |
c1996262 | 219 | Inserts a value at the front of the list. |
23324ae1 | 220 | */ |
c1996262 RR |
221 | void Insert(const wxVariant& value); |
222 | /** | |
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. | |
225 | */ | |
226 | void NullList(); | |
227 | //@} | |
23324ae1 FM |
228 | |
229 | //@{ | |
230 | /** | |
4cc4bfaf | 231 | Retrieves and converts the value of this variant to the type that @a value is. |
23324ae1 | 232 | */ |
328f5751 FM |
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; | |
23324ae1 FM |
239 | //@} |
240 | ||
23324ae1 FM |
241 | /** |
242 | Returns the string array value. | |
243 | */ | |
328f5751 | 244 | wxArrayString GetArrayString() const; |
23324ae1 FM |
245 | |
246 | /** | |
247 | Returns the boolean value. | |
248 | */ | |
328f5751 | 249 | bool GetBool() const; |
23324ae1 FM |
250 | |
251 | /** | |
252 | Returns the character value. | |
253 | */ | |
328f5751 | 254 | wxChar GetChar() const; |
23324ae1 | 255 | |
23324ae1 FM |
256 | /** |
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. | |
261 | */ | |
328f5751 | 262 | wxVariantData* GetData() const; |
23324ae1 FM |
263 | |
264 | /** | |
265 | Returns the date value. | |
266 | */ | |
328f5751 | 267 | wxDateTime GetDateTime() const; |
23324ae1 FM |
268 | |
269 | /** | |
270 | Returns the floating point value. | |
271 | */ | |
328f5751 | 272 | double GetDouble() const; |
23324ae1 | 273 | |
23324ae1 FM |
274 | /** |
275 | Returns the integer value. | |
276 | */ | |
328f5751 | 277 | long GetLong() const; |
23324ae1 FM |
278 | |
279 | /** | |
280 | Returns a constant reference to the variant name. | |
281 | */ | |
328f5751 | 282 | const wxString GetName() const; |
23324ae1 FM |
283 | |
284 | /** | |
285 | Gets the string value. | |
286 | */ | |
328f5751 | 287 | wxString GetString() const; |
23324ae1 FM |
288 | |
289 | /** | |
290 | Returns the value type as a string. The built-in types are: bool, char, | |
291 | datetime, double, list, long, string, arrstring, void*. | |
23324ae1 FM |
292 | If the variant is null, the value type returned is the string "null" (not the |
293 | empty string). | |
294 | */ | |
328f5751 | 295 | wxString GetType() const; |
23324ae1 FM |
296 | |
297 | /** | |
298 | Gets the void pointer value. | |
299 | */ | |
328f5751 | 300 | void* GetVoidPtr() const; |
23324ae1 FM |
301 | |
302 | /** | |
303 | Gets the wxObject pointer value. | |
304 | */ | |
328f5751 | 305 | wxObject* GetWxObjectPtr() const; |
23324ae1 | 306 | |
23324ae1 FM |
307 | /** |
308 | Returns @true if there is no data associated with this variant, @false if there | |
309 | is data. | |
310 | */ | |
328f5751 | 311 | bool IsNull() const; |
23324ae1 FM |
312 | |
313 | /** | |
4cc4bfaf | 314 | Returns @true if @a type matches the type of the variant, @false otherwise. |
23324ae1 | 315 | */ |
328f5751 | 316 | bool IsType(const wxString& type) const; |
23324ae1 FM |
317 | |
318 | /** | |
319 | Returns @true if the data is derived from the class described by @e type, @false | |
320 | otherwise. | |
321 | */ | |
328f5751 | 322 | bool IsValueKindOf(const wxClassInfo* type type) const; |
23324ae1 FM |
323 | |
324 | /** | |
325 | Makes the variant null by deleting the internal data. | |
326 | */ | |
327 | void MakeNull(); | |
328 | ||
329 | /** | |
330 | Makes a string representation of the variant value (for any type). | |
331 | */ | |
328f5751 | 332 | wxString MakeString() const; |
23324ae1 FM |
333 | |
334 | /** | |
4cc4bfaf | 335 | Returns @true if @a value matches an element in the list. |
23324ae1 | 336 | */ |
328f5751 | 337 | bool Member(const wxVariant& value) const; |
23324ae1 | 338 | |
23324ae1 FM |
339 | /** |
340 | Sets the internal variant data, deleting the existing data if there is any. | |
341 | */ | |
342 | void SetData(wxVariantData* data); | |
343 | ||
344 | /** | |
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. | |
349 | */ | |
350 | bool Unshare(); | |
351 | ||
352 | //@{ | |
353 | /** | |
354 | Inequality test operators. | |
355 | */ | |
328f5751 FM |
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; | |
23324ae1 FM |
368 | //@} |
369 | ||
370 | //@{ | |
371 | /** | |
372 | Assignment operators, using @ref overview_trefcount "reference counting" when | |
373 | possible. | |
374 | */ | |
375 | void operator =(const wxVariant& value); | |
7c913512 FM |
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); | |
23324ae1 FM |
391 | //@} |
392 | ||
393 | //@{ | |
394 | /** | |
395 | Equality test operators. | |
396 | */ | |
328f5751 FM |
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; | |
23324ae1 FM |
409 | //@} |
410 | ||
23324ae1 FM |
411 | //@{ |
412 | /** | |
413 | Operator for implicit conversion to a long, using GetLong(). | |
414 | */ | |
328f5751 FM |
415 | double operator double() const; |
416 | const long operator long() const; | |
23324ae1 FM |
417 | //@} |
418 | ||
419 | /** | |
420 | Operator for implicit conversion to a pointer to a void, using GetVoidPtr(). | |
421 | */ | |
328f5751 | 422 | void* operator void*() const; |
23324ae1 FM |
423 | |
424 | /** | |
425 | Operator for implicit conversion to a wxChar, using GetChar(). | |
426 | */ | |
328f5751 | 427 | char operator wxChar() const; |
23324ae1 FM |
428 | |
429 | /** | |
430 | Operator for implicit conversion to a pointer to a wxDateTime, using | |
431 | GetDateTime(). | |
432 | */ | |
328f5751 | 433 | void* operator wxDateTime() const; |
23324ae1 FM |
434 | |
435 | /** | |
436 | Operator for implicit conversion to a string, using MakeString(). | |
437 | */ | |
328f5751 | 438 | wxString operator wxString() const; |
23324ae1 FM |
439 | }; |
440 | ||
441 | ||
e54c96f1 | 442 | |
23324ae1 FM |
443 | /** |
444 | @class wxVariantData | |
445 | @wxheader{variant.h} | |
7c913512 FM |
446 | |
447 | The @b wxVariantData class is used to implement a new type for wxVariant. | |
23324ae1 | 448 | Derive from wxVariantData, and override the pure virtual functions. |
7c913512 | 449 | |
23324ae1 | 450 | wxVariantData is @ref overview_refcount "reference counted", but you don't |
7c913512 | 451 | normally have to care about this, |
23324ae1 FM |
452 | as wxVariant manages the count automatically. However, in case your application |
453 | needs to take | |
454 | ownership of wxVariantData, be aware that the object is created with reference | |
455 | count of 1, | |
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 | |
459 | to a wxVariant. | |
460 | Also note that the destructor is protected, so you can never explicitly delete | |
461 | a wxVariantData | |
462 | instance. Instead, wxVariantData::DecRef will delete the object automatically | |
463 | when the reference count reaches zero. | |
7c913512 | 464 | |
23324ae1 FM |
465 | @library{wxbase} |
466 | @category{FIXME} | |
7c913512 | 467 | |
e54c96f1 | 468 | @see wxVariant |
23324ae1 | 469 | */ |
7c913512 | 470 | class wxVariantData |
23324ae1 FM |
471 | { |
472 | public: | |
473 | /** | |
474 | Default constructor. | |
475 | */ | |
476 | wxVariantData(); | |
477 | ||
478 | /** | |
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. | |
482 | */ | |
328f5751 | 483 | wxVariantData* Clone() const; |
23324ae1 FM |
484 | |
485 | /** | |
486 | Decreases reference count. If the count reaches zero, the object is | |
487 | automatically deleted. | |
23324ae1 FM |
488 | Note that destructor of wxVariantData is protected, so delete |
489 | cannot be used as normal. Instead, DecRef() should be called. | |
490 | */ | |
491 | void DecRef(); | |
492 | ||
493 | /** | |
494 | Returns @true if this object is equal to @e data. | |
495 | */ | |
328f5751 | 496 | bool Eq(wxVariantData& data) const; |
23324ae1 FM |
497 | |
498 | /** | |
499 | Returns the string type of the data. | |
500 | */ | |
328f5751 | 501 | wxString GetType() const; |
23324ae1 FM |
502 | |
503 | /** | |
504 | If the data is a wxObject returns a pointer to the objects wxClassInfo | |
505 | structure, if | |
506 | the data isn't a wxObject the method returns @NULL. | |
507 | */ | |
328f5751 | 508 | wxClassInfo* GetValueClassInfo() const; |
23324ae1 FM |
509 | |
510 | /** | |
511 | Increases reference count. Note that initially wxVariantData has reference | |
512 | count of 1. | |
513 | */ | |
514 | void IncRef(); | |
515 | ||
516 | //@{ | |
517 | /** | |
4cc4bfaf | 518 | Reads the data from @a stream or @e string. |
23324ae1 FM |
519 | */ |
520 | bool Read(ostream& stream); | |
7c913512 | 521 | bool Read(wxString& string); |
23324ae1 FM |
522 | //@} |
523 | ||
524 | //@{ | |
525 | /** | |
4cc4bfaf | 526 | Writes the data to @a stream or @e string. |
23324ae1 | 527 | */ |
328f5751 FM |
528 | bool Write(ostream& stream) const; |
529 | const bool Write(wxString& string) const; | |
23324ae1 FM |
530 | //@} |
531 | ||
532 | /** | |
533 | This macro returns the data stored in @e variant cast to the type @e classname | |
534 | * if | |
535 | the data is of this type (the check is done during the run-time) or | |
536 | @NULL otherwise. | |
537 | */ | |
4cc4bfaf | 538 | classname* wxGetVariantCast(); |
23324ae1 | 539 | }; |
e54c96f1 | 540 |