Coverage Report

Created: 2025-09-27 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wxwidgets/include/wx/variant.h
Line
Count
Source
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/variant.h
3
// Purpose:     wxVariant class, container for any type
4
// Author:      Julian Smart
5
// Created:     10/09/98
6
// Copyright:   (c) Julian Smart
7
// Licence:     wxWindows licence
8
/////////////////////////////////////////////////////////////////////////////
9
10
#ifndef _WX_VARIANT_H_
11
#define _WX_VARIANT_H_
12
13
#include "wx/defs.h"
14
15
#if wxUSE_VARIANT
16
17
#include "wx/object.h"
18
#include "wx/string.h"
19
#include "wx/arrstr.h"
20
#include "wx/list.h"
21
#include "wx/cpp.h"
22
#include "wx/longlong.h"
23
24
#if wxUSE_DATETIME
25
    #include "wx/datetime.h"
26
#endif // wxUSE_DATETIME
27
28
#include "wx/iosfwrap.h"
29
30
class wxAny;
31
32
/*
33
 * wxVariantData stores the actual data in a wxVariant object,
34
 * to allow it to store any type of data.
35
 * Derive from this to provide custom data handling.
36
 *
37
 * NB: When you construct a wxVariantData, it will have refcount
38
 *     of one. Refcount will not be further increased when
39
 *     it is passed to wxVariant. This simulates old common
40
 *     scenario where wxVariant took ownership of wxVariantData
41
 *     passed to it.
42
 *     If you create wxVariantData for other reasons than passing
43
 *     it to wxVariant, technically you are not required to call
44
 *     DecRef() before deleting it.
45
 *
46
 * TODO: in order to replace wxPropertyValue, we would need
47
 * to consider adding constructors that take pointers to C++ variables,
48
 * or removing that functionality from the wxProperty library.
49
 * Essentially wxPropertyValue takes on some of the wxValidator functionality
50
 * by storing pointers and not just actual values, allowing update of C++ data
51
 * to be handled automatically. Perhaps there's another way of doing this without
52
 * overloading wxVariant with unnecessary functionality.
53
 */
54
55
class WXDLLIMPEXP_BASE wxVariantData : public wxObjectRefData
56
{
57
    friend class wxVariant;
58
public:
59
0
    wxVariantData() = default;
60
61
    // Override these to provide common functionality
62
    virtual bool Eq(wxVariantData& data) const = 0;
63
64
#if wxUSE_STD_IOSTREAM
65
0
    virtual bool Write(std::ostream& WXUNUSED(str)) const { return false; }
66
#endif
67
0
    virtual bool Write(wxString& WXUNUSED(str)) const { return false; }
68
#if wxUSE_STD_IOSTREAM
69
0
    virtual bool Read(std::istream& WXUNUSED(str)) { return false; }
70
#endif
71
0
    virtual bool Read(wxString& WXUNUSED(str)) { return false; }
72
    // What type is it? Return a string name.
73
    virtual wxString GetType() const = 0;
74
    // If it based on wxObject return the ClassInfo.
75
0
    virtual wxClassInfo* GetValueClassInfo() { return nullptr; }
76
77
    // Implement this to make wxVariant::UnShare work. Returns
78
    // a copy of the data.
79
0
    wxNODISCARD virtual wxVariantData* Clone() const { return nullptr; }
80
81
#if wxUSE_ANY
82
    // Converts value to wxAny, if possible. Return true if successful.
83
0
    virtual bool GetAsAny(wxAny* WXUNUSED(any)) const { return false; }
84
#endif
85
86
protected:
87
    // Protected dtor should make some incompatible code
88
    // break more louder. That is, they should do data->DecRef()
89
    // instead of delete data.
90
    virtual ~wxVariantData() = default;
91
};
92
93
/*
94
 * wxVariant can store any kind of data, but has some basic types
95
 * built in.
96
 */
97
98
class WXDLLIMPEXP_FWD_BASE wxVariant;
99
100
WX_DECLARE_LIST_WITH_DECL(wxVariant, wxVariantList, class WXDLLIMPEXP_BASE);
101
102
class WXDLLIMPEXP_BASE wxVariant: public wxObject
103
{
104
public:
105
    wxVariant();
106
107
    wxVariant(const wxVariant& variant);
108
    wxVariant(wxVariantData* data, const wxString& name = wxEmptyString);
109
#if wxUSE_ANY
110
    wxVariant(const wxAny& any);
111
#endif
112
    virtual ~wxVariant();
113
114
    // generic assignment
115
    void operator= (const wxVariant& variant);
116
117
    // Assignment using data, e.g.
118
    // myVariant = new wxStringVariantData("hello");
119
    void operator= (wxVariantData* variantData);
120
121
    bool operator== (const wxVariant& variant) const;
122
    bool operator!= (const wxVariant& variant) const;
123
124
    // Sets/gets name
125
0
    inline void SetName(const wxString& name) { m_name = name; }
126
0
    inline const wxString& GetName() const { return m_name; }
127
128
    // Tests whether there is data
129
    bool IsNull() const;
130
131
    // For compatibility with wxWidgets <= 2.6, this doesn't increase
132
    // reference count.
133
    wxVariantData* GetData() const
134
0
    {
135
0
        return (wxVariantData*) m_refData;
136
0
    }
137
    void SetData(wxVariantData* data) ;
138
139
    // make a 'clone' of the object
140
0
    void Ref(const wxVariant& clone) { wxObject::Ref(clone); }
141
142
    // ensure that the data is exclusive to this variant, and not shared
143
    bool Unshare();
144
145
    // Make null (i.e. delete the data)
146
    void MakeNull();
147
148
    // Delete data and name
149
    void Clear();
150
151
    // Returns a string representing the type of the variant,
152
    // e.g. "string", "bool", "stringlist", "list", "double", "long"
153
    wxString GetType() const;
154
155
    bool IsType(const wxString& type) const;
156
    bool IsValueKindOf(const wxClassInfo* type) const;
157
158
    // write contents to a string (e.g. for debugging)
159
    wxString MakeString() const;
160
161
#if wxUSE_ANY
162
    wxAny GetAny() const;
163
#endif
164
165
    // double
166
    wxVariant(double val, const wxString& name = wxEmptyString);
167
    bool operator== (double value) const;
168
    bool operator!= (double value) const;
169
    void operator= (double value) ;
170
0
    inline operator double () const {  return GetDouble(); }
171
0
    inline double GetReal() const { return GetDouble(); }
172
    double GetDouble() const;
173
174
    // long
175
    wxVariant(long val, const wxString& name = wxEmptyString);
176
    wxVariant(int val, const wxString& name = wxEmptyString);
177
    wxVariant(short val, const wxString& name = wxEmptyString);
178
    bool operator== (long value) const;
179
    bool operator!= (long value) const;
180
    void operator= (long value) ;
181
0
    inline operator long () const {  return GetLong(); }
182
0
    inline long GetInteger() const { return GetLong(); }
183
    long GetLong() const;
184
185
    // bool
186
    wxVariant(bool val, const wxString& name = wxEmptyString);
187
    bool operator== (bool value) const;
188
    bool operator!= (bool value) const;
189
    void operator= (bool value) ;
190
0
    inline operator bool () const {  return GetBool(); }
191
    bool GetBool() const ;
192
193
    // wxDateTime
194
#if wxUSE_DATETIME
195
    wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString);
196
    bool operator== (const wxDateTime& value) const;
197
    bool operator!= (const wxDateTime& value) const;
198
    void operator= (const wxDateTime& value) ;
199
0
    inline operator wxDateTime () const { return GetDateTime(); }
200
    wxDateTime GetDateTime() const;
201
#endif
202
203
    // wxString
204
    wxVariant(const wxString& val, const wxString& name = wxEmptyString);
205
    // these overloads are necessary to prevent the compiler from using bool
206
    // version instead of wxString one:
207
    wxVariant(const char* val, const wxString& name = wxEmptyString);
208
    wxVariant(const wchar_t* val, const wxString& name = wxEmptyString);
209
    wxVariant(const wxCStrData& val, const wxString& name = wxEmptyString);
210
    wxVariant(const wxScopedCharBuffer& val, const wxString& name = wxEmptyString);
211
    wxVariant(const wxScopedWCharBuffer& val, const wxString& name = wxEmptyString);
212
213
    bool operator== (const wxString& value) const;
214
    bool operator!= (const wxString& value) const;
215
    wxVariant& operator=(const wxString& value);
216
    // these overloads are necessary to prevent the compiler from using bool
217
    // version instead of wxString one:
218
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
219
    wxVariant& operator=(const char* value)
220
0
        { return *this = wxString(value); }
221
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
222
    wxVariant& operator=(const wchar_t* value)
223
0
        { return *this = wxString(value); }
224
    wxVariant& operator=(const wxCStrData& value)
225
0
        { return *this = value.AsString(); }
226
    template<typename T>
227
    wxVariant& operator=(const wxScopedCharTypeBuffer<T>& value)
228
        { return *this = value.data(); }
229
230
0
    inline operator wxString () const {  return MakeString(); }
231
    wxString GetString() const;
232
233
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
234
    wxVariant(const std::string& val, const wxString& name = wxEmptyString);
235
    bool operator==(const std::string& value) const
236
0
        { return operator==(wxString(value)); }
237
    bool operator!=(const std::string& value) const
238
0
        { return operator!=(wxString(value)); }
239
    wxVariant& operator=(const std::string& value)
240
0
        { return operator=(wxString(value)); }
241
0
    operator std::string() const { return (operator wxString()).ToStdString(); }
242
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
243
244
    wxVariant(const std::wstring& val, const wxString& name = wxEmptyString);
245
    bool operator==(const std::wstring& value) const
246
0
        { return operator==(wxString(value)); }
247
    bool operator!=(const std::wstring& value) const
248
0
        { return operator!=(wxString(value)); }
249
    wxVariant& operator=(const std::wstring& value)
250
0
        { return operator=(wxString(value)); }
251
0
    operator std::wstring() const { return (operator wxString()).ToStdWstring(); }
252
253
    // wxUniChar
254
    wxVariant(const wxUniChar& val, const wxString& name = wxEmptyString);
255
    wxVariant(const wxUniCharRef& val, const wxString& name = wxEmptyString);
256
    wxVariant(char val, const wxString& name = wxEmptyString);
257
    wxVariant(wchar_t val, const wxString& name = wxEmptyString);
258
    bool operator==(const wxUniChar& value) const;
259
0
    bool operator==(const wxUniCharRef& value) const { return *this == wxUniChar(value); }
260
0
    bool operator==(char value) const { return *this == wxUniChar(value); }
261
0
    bool operator==(wchar_t value) const { return *this == wxUniChar(value); }
262
0
    bool operator!=(const wxUniChar& value) const { return !(*this == value); }
263
0
    bool operator!=(const wxUniCharRef& value) const { return !(*this == value); }
264
0
    bool operator!=(char value) const { return !(*this == value); }
265
0
    bool operator!=(wchar_t value) const { return !(*this == value); }
266
    wxVariant& operator=(const wxUniChar& value);
267
0
    wxVariant& operator=(const wxUniCharRef& value) { return *this = wxUniChar(value); }
268
0
    wxVariant& operator=(char value) { return *this = wxUniChar(value); }
269
0
    wxVariant& operator=(wchar_t value) { return *this = wxUniChar(value); }
270
0
    operator wxUniChar() const { return GetChar(); }
271
0
    operator char() const { return GetChar(); }
272
0
    operator wchar_t() const { return GetChar(); }
273
    wxUniChar GetChar() const;
274
275
    // wxArrayString
276
    wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString);
277
    bool operator== (const wxArrayString& value) const;
278
    bool operator!= (const wxArrayString& value) const;
279
    void operator= (const wxArrayString& value);
280
0
    operator wxArrayString () const { return GetArrayString(); }
281
    wxArrayString GetArrayString() const;
282
283
    // void*
284
    wxVariant(void* ptr, const wxString& name = wxEmptyString);
285
    bool operator== (void* value) const;
286
    bool operator!= (void* value) const;
287
    void operator= (void* value);
288
0
    operator void* () const {  return GetVoidPtr(); }
289
    void* GetVoidPtr() const;
290
291
    // wxObject*
292
    wxVariant(wxObject* ptr, const wxString& name = wxEmptyString);
293
    bool operator== (wxObject* value) const;
294
    bool operator!= (wxObject* value) const;
295
    void operator= (wxObject* value);
296
    wxObject* GetWxObjectPtr() const;
297
298
    // wxLongLong
299
    wxVariant(wxLongLong, const wxString& name = wxEmptyString);
300
    bool operator==(wxLongLong value) const;
301
    bool operator!=(wxLongLong value) const;
302
    void operator=(wxLongLong value);
303
0
    operator wxLongLong() const { return GetLongLong(); }
304
    wxLongLong GetLongLong() const;
305
306
    // wxULongLong
307
    wxVariant(wxULongLong, const wxString& name = wxEmptyString);
308
    bool operator==(wxULongLong value) const;
309
    bool operator!=(wxULongLong value) const;
310
    void operator=(wxULongLong value);
311
0
    operator wxULongLong() const { return GetULongLong(); }
312
    wxULongLong GetULongLong() const;
313
314
    // ------------------------------
315
    // list operations
316
    // ------------------------------
317
318
    wxVariant(const wxVariantList& val, const wxString& name = wxEmptyString); // List of variants
319
    bool operator== (const wxVariantList& value) const;
320
    bool operator!= (const wxVariantList& value) const;
321
    void operator= (const wxVariantList& value) ;
322
    // Treat a list variant as an array
323
    wxVariant operator[] (size_t idx) const;
324
    wxVariant& operator[] (size_t idx) ;
325
    wxVariantList& GetList() const ;
326
327
    // Return the number of elements in a list
328
    size_t GetCount() const;
329
330
    // Make empty list
331
    void NullList();
332
333
    // Append to list
334
    void Append(const wxVariant& value);
335
336
    // Insert at front of list
337
    void Insert(const wxVariant& value);
338
339
    // Returns true if the variant is a member of the list
340
    bool Member(const wxVariant& value) const;
341
342
    // Deletes the nth element of the list
343
    bool Delete(size_t item);
344
345
    // Clear list
346
    void ClearList();
347
348
public:
349
    // Type conversion
350
    bool Convert(long* value) const;
351
    bool Convert(bool* value) const;
352
    bool Convert(double* value) const;
353
    bool Convert(wxString* value) const;
354
    bool Convert(wxUniChar* value) const;
355
    bool Convert(char* value) const;
356
    bool Convert(wchar_t* value) const;
357
#if wxUSE_DATETIME
358
    bool Convert(wxDateTime* value) const;
359
#endif // wxUSE_DATETIME
360
    bool Convert(wxLongLong* value) const;
361
    bool Convert(wxULongLong* value) const;
362
    bool Convert(wxLongLong_t* value) const
363
0
    {
364
0
        wxLongLong temp;
365
0
        if ( !Convert(&temp) )
366
0
            return false;
367
0
        *value = temp.GetValue();
368
0
        return true;
369
0
    }
370
    bool Convert(wxULongLong_t* value) const
371
0
    {
372
0
        wxULongLong temp;
373
0
        if ( !Convert(&temp) )
374
0
            return false;
375
0
        *value = temp.GetValue();
376
0
        return true;
377
0
    }
378
379
// Attributes
380
protected:
381
    virtual wxObjectRefData *CreateRefData() const override;
382
    wxNODISCARD virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const override;
383
384
    wxString        m_name;
385
386
private:
387
    wxDECLARE_DYNAMIC_CLASS(wxVariant);
388
};
389
390
391
//
392
// wxVariant <-> wxAny conversion code
393
//
394
#if wxUSE_ANY
395
396
#include "wx/any.h"
397
398
// In order to convert wxAny to wxVariant, we need to be able to associate
399
// wxAnyValueType with a wxVariantData factory function.
400
typedef wxVariantData* (*wxVariantDataFactory)(const wxAny& any);
401
402
// Actual Any-to-Variant registration must be postponed to a time when all
403
// global variables have been initialized. Hence this arrangement.
404
// wxAnyToVariantRegistration instances are kept in global scope and
405
// wxAnyValueTypeGlobals in any.cpp will use their data when the time is
406
// right.
407
class WXDLLIMPEXP_BASE wxAnyToVariantRegistration
408
{
409
public:
410
    wxAnyToVariantRegistration(wxVariantDataFactory factory);
411
    virtual ~wxAnyToVariantRegistration();
412
413
    virtual wxAnyValueType* GetAssociatedType() = 0;
414
0
    wxVariantDataFactory GetFactory() const { return m_factory; }
415
private:
416
    wxVariantDataFactory    m_factory;
417
};
418
419
template<typename T>
420
class wxAnyToVariantRegistrationImpl : public wxAnyToVariantRegistration
421
{
422
public:
423
    wxAnyToVariantRegistrationImpl(wxVariantDataFactory factory)
424
26
        : wxAnyToVariantRegistration(factory)
425
26
    {
426
26
    }
wxAnyToVariantRegistrationImpl<double>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<bool>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<wxUniChar>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<wxString>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<char const*>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<wchar_t const*>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<wxObject*>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<void*>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<wxDateTime>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<wxArrayString>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<long long>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<unsigned long long>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
wxAnyToVariantRegistrationImpl<wxAnyList>::wxAnyToVariantRegistrationImpl(wxVariantData* (*)(wxAny const&))
Line
Count
Source
424
2
        : wxAnyToVariantRegistration(factory)
425
2
    {
426
2
    }
427
428
    virtual wxAnyValueType* GetAssociatedType() override
429
0
    {
430
0
        return wxAnyValueTypeImpl<T>::GetInstance();
431
0
    }
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<double>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<bool>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<wxUniChar>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<wxString>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<char const*>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<wchar_t const*>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<wxObject*>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<void*>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<wxDateTime>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<wxArrayString>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<long long>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<unsigned long long>::GetAssociatedType()
Unexecuted instantiation: wxAnyToVariantRegistrationImpl<wxAnyList>::GetAssociatedType()
432
private:
433
};
434
435
#define DECLARE_WXANY_CONVERSION() \
436
virtual bool GetAsAny(wxAny* any) const override; \
437
static wxVariantData* VariantDataFactory(const wxAny& any);
438
439
#define _REGISTER_WXANY_CONVERSION(T, CLASSNAME, FUNC) \
440
static wxAnyToVariantRegistrationImpl<T> \
441
    gs_##CLASSNAME##AnyToVariantRegistration = \
442
    wxAnyToVariantRegistrationImpl<T>(&FUNC);
443
444
#define REGISTER_WXANY_CONVERSION(T, CLASSNAME) \
445
_REGISTER_WXANY_CONVERSION(T, CLASSNAME, CLASSNAME::VariantDataFactory)
446
447
#define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME) \
448
0
bool CLASSNAME::GetAsAny(wxAny* any) const \
449
0
{ \
450
0
    *any = m_value; \
451
0
    return true; \
452
0
} \
Unexecuted instantiation: wxVariantDoubleData::GetAsAny(wxAny*) const
Unexecuted instantiation: wxVariantDataBool::GetAsAny(wxAny*) const
Unexecuted instantiation: wxVariantDataChar::GetAsAny(wxAny*) const
Unexecuted instantiation: wxVariantDataString::GetAsAny(wxAny*) const
Unexecuted instantiation: wxVariantDataWxObjectPtr::GetAsAny(wxAny*) const
Unexecuted instantiation: wxVariantDataVoidPtr::GetAsAny(wxAny*) const
Unexecuted instantiation: wxVariantDataDateTime::GetAsAny(wxAny*) const
Unexecuted instantiation: wxVariantDataArrayString::GetAsAny(wxAny*) const
453
0
wxVariantData* CLASSNAME::VariantDataFactory(const wxAny& any) \
454
0
{ \
455
0
    return new CLASSNAME(any.As<T>()); \
456
0
} \
Unexecuted instantiation: wxVariantDoubleData::VariantDataFactory(wxAny const&)
Unexecuted instantiation: wxVariantDataBool::VariantDataFactory(wxAny const&)
Unexecuted instantiation: wxVariantDataChar::VariantDataFactory(wxAny const&)
Unexecuted instantiation: wxVariantDataString::VariantDataFactory(wxAny const&)
Unexecuted instantiation: wxVariantDataWxObjectPtr::VariantDataFactory(wxAny const&)
Unexecuted instantiation: wxVariantDataVoidPtr::VariantDataFactory(wxAny const&)
Unexecuted instantiation: wxVariantDataDateTime::VariantDataFactory(wxAny const&)
Unexecuted instantiation: wxVariantDataArrayString::VariantDataFactory(wxAny const&)
457
REGISTER_WXANY_CONVERSION(T, CLASSNAME)
458
459
#else // if !wxUSE_ANY
460
461
#define DECLARE_WXANY_CONVERSION()
462
#define REGISTER_WXANY_CONVERSION(T, CLASSNAME)
463
#define IMPLEMENT_TRIVIAL_WXANY_CONVERSION(T, CLASSNAME)
464
465
#endif // wxUSE_ANY/!wxUSE_ANY
466
467
// Note: these macros must be used inside "classname" declaration.
468
#define wxDECLARE_VARIANT_OBJECT(classname) \
469
    wxDECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
470
471
#define wxDECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
472
    friend expdecl classname& operator<<(classname &object, const wxVariant &variant); \
473
    friend expdecl wxVariant& operator<<(wxVariant &variant, const classname &object)
474
475
// These macros are deprecated, consider using wxDECLARE_VARIANT_OBJECT() above
476
// instead.
477
#define DECLARE_VARIANT_OBJECT(classname) \
478
    DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
479
480
#define DECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
481
expdecl classname& operator << ( classname &object, const wxVariant &variant ); \
482
expdecl wxVariant& operator << ( wxVariant &variant, const classname &object );
483
484
// These macros use "wx" prefix and require a semicolon after them for
485
// consistency with the rest of wx macros, but are otherwise the same as the
486
// older IMPLEMENT_VARIANT_XXX macros.
487
#define wxIMPLEMENT_VARIANT_OBJECT(classname) \
488
    IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) \
489
    struct wxDummyVariantStructFwdDecl /* to force a semicolon */
490
491
#define IMPLEMENT_VARIANT_OBJECT(classname) \
492
    IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE)
493
494
#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,expdecl) \
495
class classname##VariantData: public wxVariantData \
496
{ \
497
public:\
498
    classname##VariantData() = default; \
499
    classname##VariantData( const classname &value ) : m_value(value) { } \
500
\
501
    classname &GetValue() { return m_value; } \
502
\
503
    virtual bool Eq(wxVariantData& data) const override; \
504
\
505
    virtual wxString GetType() const override; \
506
    virtual wxClassInfo* GetValueClassInfo() override; \
507
\
508
    wxNODISCARD virtual wxVariantData* Clone() const override { return new classname##VariantData(m_value); } \
509
\
510
    DECLARE_WXANY_CONVERSION() \
511
protected:\
512
    classname m_value; \
513
};\
514
\
515
wxString classname##VariantData::GetType() const\
516
{\
517
    return m_value.GetClassInfo()->GetClassName();\
518
}\
519
\
520
wxClassInfo* classname##VariantData::GetValueClassInfo()\
521
{\
522
    return m_value.GetClassInfo();\
523
}\
524
\
525
expdecl classname& operator << ( classname &value, const wxVariant &variant )\
526
{\
527
    wxASSERT( variant.GetType() == #classname );\
528
    \
529
    classname##VariantData *data = (classname##VariantData*) variant.GetData();\
530
    value = data->GetValue();\
531
    return value;\
532
}\
533
\
534
expdecl wxVariant& operator << ( wxVariant &variant, const classname &value )\
535
{\
536
    classname##VariantData *data = new classname##VariantData( value );\
537
    variant.SetData( data );\
538
    return variant;\
539
} \
540
IMPLEMENT_TRIVIAL_WXANY_CONVERSION(classname, classname##VariantData)
541
542
// implements a wxVariantData-derived class using for the Eq() method the
543
// operator== which must have been provided by "classname"
544
#define IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname,expdecl) \
545
IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
546
\
547
bool classname##VariantData::Eq(wxVariantData& data) const \
548
{\
549
    wxASSERT( GetType() == data.GetType() );\
550
\
551
    classname##VariantData & otherData = (classname##VariantData &) data;\
552
\
553
    return otherData.m_value == m_value;\
554
}\
555
556
557
// implements a wxVariantData-derived class using for the Eq() method a shallow
558
// comparison (through wxObject::IsSameAs function)
559
#define IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(classname) \
560
    IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname, wxEMPTY_PARAMETER_VALUE)
561
#define IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(classname,expdecl) \
562
IMPLEMENT_VARIANT_OBJECT_EXPORTED_NO_EQ(classname,wxEMPTY_PARAMETER_VALUE expdecl) \
563
\
564
bool classname##VariantData::Eq(wxVariantData& data) const \
565
{\
566
    wxASSERT( GetType() == data.GetType() );\
567
\
568
    classname##VariantData & otherData = (classname##VariantData &) data;\
569
\
570
    return (otherData.m_value.IsSameAs(m_value));\
571
}\
572
573
574
// Since we want type safety wxVariant we need to fetch and dynamic_cast
575
// in a seemingly safe way so the compiler can check, so we define
576
// a dynamic_cast /wxDynamicCast analogue.
577
578
#define wxGetVariantCast(var,classname) \
579
    ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
580
                  var.GetWxObjectPtr() : nullptr));
581
582
// Replacement for using wxDynamicCast on a wxVariantData object
583
#ifndef wxNO_RTTI
584
    #define wxDynamicCastVariantData(data, classname) dynamic_cast<classname*>(data)
585
#endif
586
587
#define wxStaticCastVariantData(data, classname) static_cast<classname*>(data)
588
589
extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
590
591
#else // !wxUSE_VARIANT
592
593
// Define these macros to allow using them without checking for wxUSE_VARIANT
594
// and simply do nothing in them in this case.
595
#define wxDECLARE_VARIANT_OBJECT(classname)
596
#define wxDECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl)
597
598
#endif // wxUSE_VARIANT/!wxUSE_VARIANT
599
600
#endif // _WX_VARIANT_H_