File: variant.h

package info (click to toggle)
wxwidgets3.0 3.0.2%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,808 kB
  • ctags: 118,010
  • sloc: cpp: 889,420; makefile: 52,980; ansic: 21,933; sh: 5,603; python: 2,935; xml: 1,534; perl: 281
file content (697 lines) | stat: -rw-r--r-- 20,851 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/////////////////////////////////////////////////////////////////////////////
// Name:        variant.h
// Purpose:     interface of wxVariant
// Author:      wxWidgets team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/**
    @class wxVariant

    The wxVariant class represents a container for any type. A variant's value
    can be changed at run time, possibly to a different type of value.

    @note As of wxWidgets 2.9.1, wxAny has become the preferred variant class.
          While most controls still use wxVariant in their interface, you
          can start using wxAny in your code because of an implicit conversion
          layer. See below for more information.

    As standard, wxVariant can store values of type bool, wxChar, double, long,
    string, string list, time, date, void pointer, list of strings, and list of
    variants. However, an application can extend wxVariant's capabilities by
    deriving from the class wxVariantData and using the wxVariantData form of
    the wxVariant constructor or assignment operator to assign this data to a
    variant. Actual values for user-defined types will need to be accessed via
    the wxVariantData object, unlike the case for basic data types where
    convenience functions such as GetLong() can be used.

    Under Microsoft Windows, three additional wxVariantData-derived classes --
    wxVariantDataCurrency, wxVariantDataErrorCode and wxVariantDataSafeArray --
    are available for interoperation with OLE VARIANT when using wxAutomationObject.

    Pointers to any wxObject derived class can also easily be stored in a
    wxVariant. wxVariant will then use wxWidgets' built-in RTTI system to set
    the type name  (returned by GetType()) and to perform type-safety checks at
    runtime.

    This class is useful for reducing the programming for certain tasks, such
    as an editor for different data types, or a remote procedure call protocol.

    An optional name member is associated with a wxVariant. This might be used,
    for example, in CORBA or OLE automation classes, where named parameters are
    required.

    Note that as of wxWidgets 2.7.1, wxVariant is
    @ref overview_refcount "reference counted". Additionally, the convenience
    macros DECLARE_VARIANT_OBJECT() and IMPLEMENT_VARIANT_OBJECT() were added
    so that adding (limited) support for conversion to and from wxVariant can
    be very easily implemented without modifying either wxVariant or the class
    to be stored by wxVariant. Since assignment operators cannot be declared
    outside the class, the shift left operators are used like this:

    @code
    // in the header file
    DECLARE_VARIANT_OBJECT(MyClass)

    // in the implementation file
    IMPLEMENT_VARIANT_OBJECT(MyClass)

    // in the user code
    wxVariant variant;
    MyClass value;
    variant << value;

    // or
    value << variant;
    @endcode

    For this to work, MyClass must derive from wxObject, implement the
    @ref overview_rtti "wxWidgets RTTI system" and support the assignment
    operator and equality operator for itself. Ideally, it should also be
    reference counted to make copying operations cheap and fast. This can be
    most easily implemented using the reference counting support offered by
    wxObject itself. By default, wxWidgets already implements the shift
    operator conversion for a few of its drawing related classes:

    @code
    IMPLEMENT_VARIANT_OBJECT(wxColour)
    IMPLEMENT_VARIANT_OBJECT(wxImage)
    IMPLEMENT_VARIANT_OBJECT(wxIcon)
    IMPLEMENT_VARIANT_OBJECT(wxBitmap)
    @endcode

    Note that as of wxWidgets 2.9.0, wxVariantData no longer inherits from
    wxObject and wxVariant no longer uses the type-unsafe wxList class for list
    operations but the type-safe wxVariantList class. Also, wxVariantData now
    supports the wxVariantData::Clone() function for implementing the Unshare()
    function. wxVariantData::Clone() is implemented automatically by
    IMPLEMENT_VARIANT_OBJECT().

    Since wxVariantData no longer derives from wxObject, any code that tests
    the type of the data using wxDynamicCast() will require adjustment. You can
    use the macro wxDynamicCastVariantData() with the same arguments as
    wxDynamicCast(), to use C++ RTTI type information instead of wxWidgets
    RTTI.

    @section variant_wxanyconversion wxVariant to wxAny Conversion Layer

    wxAny is a more modern, template-based variant class. It is not
    directly compatible with wxVariant, but there is a transparent conversion
    layer.

    Following is an example how to use these conversions with wxPropertyGrid's
    property class wxPGProperty (which currently uses wxVariants both
    internally and in the public API):

    @code
        // Get property value as wxAny instead of wxVariant
        wxAny value = property->GetValue();

        // Do something with it
        DoSomethingWithString(value.As<wxString>());

        // Write back new value to property
        value = "New Value";
        property->SetValue(value);

    @endcode

    Some caveats:
    @li In wxAny, there are no separate types for handling integers of
        different sizes, so converting wxAny with 'long long' value
        will yield wxVariant of "long" type when the value is small
        enough to fit in without overflow. Otherwise, variant type
        "longlong" is used. Also note that wxAny holding unsigned integer
        will always be converted to "ulonglong" wxVariant type.

    @li Unlike wxVariant, wxAny does not store a (rarely needed) name string.

    @li Because of implicit conversion of wxVariant to wxAny, wxAny cannot
        usually contain value of type wxVariant. In other words,
        any.CheckType<wxVariant>() can never return @true.

    Supplied conversion functions will automatically work with all
    built-in wxVariant types, and also with all user-specified types generated
    using IMPLEMENT_VARIANT_OBJECT(). For hand-built wxVariantData classes,
    you will need to use supplied macros in a following manner:

    @code

    // Declare wxVariantData for data type Foo
    class wxVariantDataFoo: public wxVariantData
    {
    public:
        // interface
        // ...

        DECLARE_WXANY_CONVERSION()
    protected:
        // data storage etc
        // ...
    };

    IMPLEMENT_TRIVIAL_WXANY_CONVERSION(Foo, wxVariantDataFoo)

    @endcode

    @library{wxbase}
    @category{data}

    @see wxVariantData, wxAny
*/
class wxVariant : public wxObject
{
public:
    /**
        Default constructor.
    */
    wxVariant();

    /**
        Constructs a variant directly with a wxVariantData object. wxVariant
        will take ownership of the wxVariantData and will not increase its
        reference count.
    */
    wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from another variant by increasing the reference
        count.
    */
    wxVariant(const wxVariant& variant);

    /**
        Constructs a variant by converting it from wxAny.
    */
    wxVariant(const wxAny& any);

    /**
        Constructs a variant from a wide string literal.
    */
    wxVariant(const wxChar* value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a string.
    */
    wxVariant(const wxString& value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a wide char.
    */
    wxVariant(wxChar value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a long.
    */
    wxVariant(long value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a bool.
    */
    wxVariant(bool value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a double.
    */
    wxVariant(double value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a wxLongLong.
    */
    wxVariant(wxLongLong value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a wxULongLong.
    */
    wxVariant(wxULongLong value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a list of variants
    */
    wxVariant(const wxVariantList& value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a void pointer.
    */
    wxVariant(void* value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a pointer to an wxObject
        derived class.
    */
    wxVariant(wxObject* value, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a wxDateTime.
    */
    wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);

    /**
        Constructs a variant from a wxArrayString.
    */
    wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);

    /**
        Destructor.

        @note wxVariantData's destructor is protected, so wxVariantData cannot
              usually be deleted. Instead, wxVariantData::DecRef() should be
              called. See @ref overview_refcount_destruct
              "reference-counted object destruction" for more info.
    */
    virtual ~wxVariant();


    /**
        @name List Functionality
    */
    //@{

    /**
        Returns the value at @a idx (zero-based).
    */
    wxVariant operator [](size_t idx) const;
    /**
        Returns a reference to the value at @a idx (zero-based). This can be
        used to change the value at this index.
    */
    wxVariant& operator [](size_t idx);

    /**
        Appends a value to the list.
    */
    void Append(const wxVariant& value);

    /**
        Makes the variant null by deleting the internal data and set the name
        to wxEmptyString.
    */
    void Clear();

    /**
        Deletes the contents of the list.
    */
    void ClearList();

    /**
        Deletes the zero-based @a item from the list.
    */
    bool Delete(size_t item);

    /**
        Returns the number of elements in the list.
    */
    size_t GetCount() const;

    /**
        Returns a reference to the wxVariantList class used by wxVariant if
        this wxVariant is currently a list of variants.
    */
    wxVariantList& GetList() const;

    /**
        Inserts a value at the front of the list.
    */
    void Insert(const wxVariant& value);

    /**
        Makes an empty list. This differs from a null variant which has no
        data; a null list is of type list, but the number of elements in the
        list is zero.
    */
    void NullList();

    //@}


    //@{
    /**
        Retrieves and converts the value of this variant to the type that
        @a value is.
    */
    bool Convert(long* value) const;
    bool Convert(bool* value) const;
    bool Convert(double* value) const;
    bool Convert(wxString* value) const;
    bool Convert(wxChar* value) const;
    bool Convert(wxLongLong* value) const;
    bool Convert(wxULongLong* value) const;
    bool Convert(wxDateTime* value) const;
    //@}

    /**
        Converts wxVariant into wxAny.
    */
    wxAny GetAny() const;

    /**
        Returns the string array value.
    */
    wxArrayString GetArrayString() const;

    /**
        Returns the boolean value.
    */
    bool GetBool() const;

    /**
        Returns the character value.
    */
    wxUniChar GetChar() const;

    /**
        Returns a pointer to the internal variant data. To take ownership of
        this data, you must call its wxVariantData::IncRef() method. When you
        stop using it, wxVariantData::DecRef() must be called as well.
    */
    wxVariantData* GetData() const;

    /**
        Returns the date value.
    */
    wxDateTime GetDateTime() const;

    /**
        Returns the floating point value.
    */
    double GetDouble() const;

    /**
        Returns the integer value.
    */
    long GetLong() const;

    /**
        Returns the signed 64-bit integer value.
    */
    wxLongLong GetLongLong() const;

    /**
        Returns a constant reference to the variant name.
    */
    const wxString& GetName() const;

    /**
        Gets the string value.
    */
    wxString GetString() const;

    /**
        Returns the value type as a string.

        The built-in types are:
        - "bool"
        - "char"
        - "datetime"
        - "double"
        - "list"
        - "long"
        - "longlong"
        - "string"
        - "ulonglong"
        - "arrstring"
        - "void*"

        If the variant is null, the value type returned is the string "null"
        (not the empty string).
    */
    wxString GetType() const;

    /**
        Returns the unsigned 64-bit integer value.
    */
    wxULongLong GetULongLong() const;

    /**
        Gets the void pointer value.

        Notice that this method can be used for null objects (i.e. those for
        which IsNull() returns @true) and will return @NULL for them.
    */
    void* GetVoidPtr() const;

    /**
        Gets the wxObject pointer value.
    */
    wxObject* GetWxObjectPtr() const;

    /**
        Returns @true if there is no data associated with this variant, @false
        if there is data.
    */
    bool IsNull() const;

    /**
        Returns @true if @a type matches the type of the variant, @false
        otherwise.
    */
    bool IsType(const wxString& type) const;

    /**
        Returns @true if the data is derived from the class described by
        @a type, @false otherwise.
    */
    bool IsValueKindOf(const wxClassInfo* type) const;

    /**
        Makes the variant null by deleting the internal data.
    */
    void MakeNull();

    /**
        Makes a string representation of the variant value (for any type).
    */
    wxString MakeString() const;

    /**
        Returns @true if @a value matches an element in the list.
    */
    bool Member(const wxVariant& value) const;

    /**
        Sets the internal variant data, deleting the existing data if there is
        any.
    */
    void SetData(wxVariantData* data);

    /**
        Makes sure that any data associated with this variant is not shared
        with other variants. For this to work, wxVariantData::Clone() must be
        implemented for the data types you are working with.
        wxVariantData::Clone() is implemented for all the default data types.
    */
    bool Unshare();

    //@{
    /**
        Inequality test operator.
    */
    bool operator !=(const wxVariant& value) const;
    bool operator !=(const wxString& value) const;
    bool operator !=(const wxChar* value) const;
    bool operator !=(wxChar value) const;
    bool operator !=(long value) const;
    bool operator !=(bool value) const;
    bool operator !=(double value) const;
    bool operator !=(wxLongLong value) const;
    bool operator !=(wxULongLong value) const;
    bool operator !=(void* value) const;
    bool operator !=(wxObject* value) const;
    bool operator !=(const wxVariantList& value) const;
    bool operator !=(const wxArrayString& value) const;
    bool operator !=(const wxDateTime& value) const;
    //@}

    //@{
    /**
        Assignment operator, using @ref overview_refcount "reference counting"
        if possible.
    */
    void operator =(const wxVariant& value);
    void operator =(wxVariantData* value);
    void operator =(const wxString& value);
    void operator =(const wxChar* value);
    void operator =(wxChar value);
    void operator =(long value);
    void operator =(bool value);
    void operator =(double value);
    bool operator =(wxLongLong value) const;
    bool operator =(wxULongLong value) const;
    void operator =(void* value);
    void operator =(wxObject* value);
    void operator =(const wxVariantList& value);
    void operator =(const wxDateTime& value);
    void operator =(const wxArrayString& value);
    //@}

    //@{
    /**
        Equality test operator.
    */
    bool operator ==(const wxVariant& value) const;
    bool operator ==(const wxString& value) const;
    bool operator ==(const wxChar* value) const;
    bool operator ==(wxChar value) const;
    bool operator ==(long value) const;
    bool operator ==(bool value) const;
    bool operator ==(double value) const;
    bool operator ==(wxLongLong value) const;
    bool operator ==(wxULongLong value) const;
    bool operator ==(void* value) const;
    bool operator ==(wxObject* value) const;
    bool operator ==(const wxVariantList& value) const;
    bool operator ==(const wxArrayString& value) const;
    bool operator ==(const wxDateTime& value) const;
    //@}

    //@{
    /**
        Operators for implicit conversion, using appropriate getter member
        function.
    */
    double operator double() const;
    long operator long() const;
    wxLongLong operator wxLongLong() const;
    wxULongLong operator wxULongLong() const;
    //@}

    /**
        Operator for implicit conversion to a pointer to a void, using
        GetVoidPtr().
    */
    void* operator void*() const;

    /**
        Operator for implicit conversion to a wxChar, using GetChar().
    */
    char operator wxChar() const;

    /**
        Operator for implicit conversion to a pointer to a wxDateTime, using
        GetDateTime().
    */
    void* operator wxDateTime() const;

    /**
        Operator for implicit conversion to a string, using MakeString().
    */
    wxString operator wxString() const;
};



/**
    @class wxVariantData

    The wxVariantData class is used to implement a new type for wxVariant.
    Derive from wxVariantData, and override the pure virtual functions.

    wxVariantData is @ref overview_refcount "reference counted", but you don't
    normally have to care about this, as wxVariant manages the count
    automatically. However, in case your application needs to take ownership of
    wxVariantData, be aware that the object is created with a reference count
    of 1, and passing it to wxVariant will not increase this. In other words,
    IncRef() needs to be called only if you both take ownership of
    wxVariantData and pass it to a wxVariant. Also note that the destructor is
    protected, so you can never explicitly delete a wxVariantData instance.
    Instead, DecRef() will delete the object automatically when the reference
    count reaches zero.

    @library{wxbase}
    @category{data}

    @see wxVariant, wxGetVariantCast()
*/
class wxVariantData : public wxObjectRefData
{
public:
    /**
        Default constructor.
    */
    wxVariantData();

    /**
        This function can be overridden to clone the data. You must implement
        this function in order for wxVariant::Unshare() to work for your data.
        This function is implemented for all built-in data types.
    */
    virtual wxVariantData* Clone() const;

    /**
        Decreases reference count. If the count reaches zero, the object is
        automatically deleted.

        @note The destructor of wxVariantData is protected, so delete cannot be
              used as normal. Instead, DecRef() should be called.
    */
    void DecRef();

    /**
        Returns @true if this object is equal to @a data.
    */
    virtual bool Eq(wxVariantData& data) const = 0;

    /**
        Converts value to wxAny, if possible. Return @true if successful.
    */
    virtual bool GetAny(wxAny* any) const;

    /**
        Returns the string type of the data.
    */
    virtual wxString GetType() const = 0;

    /**
        If the data is a wxObject returns a pointer to the objects wxClassInfo
        structure, if the data isn't a wxObject the method returns @NULL.
    */
    virtual wxClassInfo* GetValueClassInfo();

    /**
        Increases reference count. Note that initially wxVariantData has
        reference count of 1.
    */
    void IncRef();

    /**
        Reads the data from @a stream.
    */
    virtual bool Read(istream& stream);

    /**
        Reads the data from @a string.
    */
    virtual bool Read(wxString& string);

    /**
        Writes the data to @a stream.
    */
    virtual bool Write(ostream& stream) const;
    /**
        Writes the data to @a string.
    */
    virtual bool Write(wxString& string) const;
};



// ============================================================================
// Global functions/macros
// ============================================================================

/** @addtogroup group_funcmacro_rtti */
//@{

/**
    This macro returns a pointer to the data stored in @a var (wxVariant) cast
    to the type @a classname if the data is of this type (the check is done
    during the run-time) or @NULL otherwise.

    @header{wx/variant.h}

    @see @ref overview_rtti, wxDynamicCast()
*/
#define wxGetVariantCast(var, classname)

//@}