1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_VARIANT_H_
13 #define _WX_VARIANT_H_
16 #pragma interface "variant.h"
20 #include "wx/object.h"
21 #include "wx/string.h"
29 #include "wx/ioswrap.h"
32 * wxVariantData stores the actual data in a wxVariant object,
33 * to allow it to store any type of data.
34 * Derive from this to provide custom data handling.
36 * TODO: in order to replace wxPropertyValue, we would need
37 * to consider adding constructors that take pointers to C++ variables,
38 * or removing that functionality from the wxProperty library.
39 * Essentially wxPropertyValue takes on some of the wxValidator functionality
40 * by storing pointers and not just actual values, allowing update of C++ data
41 * to be handled automatically. Perhaps there's another way of doing this without
42 * overloading wxVariant with unnecessary functionality.
45 class WXDLLEXPORT wxVariantData
: public wxObject
47 DECLARE_ABSTRACT_CLASS(wxVariantData
)
50 // Construction & destruction
53 // Override these to provide common functionality
55 virtual void Copy(wxVariantData
& data
) = 0;
56 virtual bool Eq(wxVariantData
& data
) const = 0;
57 virtual bool Write(ostream
& str
) const = 0;
58 virtual bool Write(wxString
& str
) const = 0;
59 virtual bool Read(istream
& str
) = 0;
60 virtual bool Read(wxString
& str
) = 0;
61 // What type is it? Return a string name.
62 virtual wxString
GetType() const = 0;
66 * wxVariant can store any kind of data, but has some basic types
68 * NOTE: this eventually should have a reference-counting implementation.
69 * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating
70 * this class too much.
73 class WXDLLEXPORT wxVariant
: public wxObject
75 DECLARE_DYNAMIC_CLASS(wxVariant
)
78 // Construction & destruction
80 wxVariant(double val
, const wxString
& name
= g_szNul
);
81 wxVariant(long val
, const wxString
& name
= g_szNul
);
82 wxVariant(bool val
, const wxString
& name
= g_szNul
);
83 wxVariant(char val
, const wxString
& name
= g_szNul
);
84 wxVariant(const wxString
& val
, const wxString
& name
= g_szNul
);
85 wxVariant(const char* val
, const wxString
& name
= g_szNul
); // Necessary or VC++ assumes bool!
86 wxVariant(const wxStringList
& val
, const wxString
& name
= g_szNul
);
87 wxVariant(const wxList
& val
, const wxString
& name
= g_szNul
); // List of variants
89 wxVariant(const wxTime
& val
, const wxString
& name
= g_szNul
); // Time
90 wxVariant(const wxDate
& val
, const wxString
& name
= g_szNul
); // Date
92 wxVariant(void* ptr
, const wxString
& name
= g_szNul
); // void* (general purpose)
93 wxVariant(wxVariantData
* data
, const wxString
& name
= g_szNul
); // User-defined data
94 wxVariant(const wxVariant
& variant
);
99 void operator= (const wxVariant
& variant
);
101 // Assignment using data, e.g.
102 // myVariant = new wxStringVariantData("hello");
103 void operator= (wxVariantData
* variantData
);
104 bool operator== (const wxVariant
& variant
) const;
105 bool operator!= (const wxVariant
& variant
) const;
107 // Specific operators
108 bool operator== (double value
) const;
109 bool operator!= (double value
) const;
110 void operator= (double value
) ;
111 bool operator== (long value
) const;
112 bool operator!= (long value
) const;
113 void operator= (long value
) ;
114 bool operator== (char value
) const;
115 bool operator!= (char value
) const;
116 void operator= (char value
) ;
117 bool operator== (bool value
) const;
118 bool operator!= (bool value
) const;
119 void operator= (bool value
) ;
120 bool operator== (const wxString
& value
) const;
121 bool operator!= (const wxString
& value
) const;
122 void operator= (const wxString
& value
) ;
123 void operator= (const char* value
) ; // Necessary or VC++ assumes bool!
124 bool operator== (const wxStringList
& value
) const;
125 bool operator!= (const wxStringList
& value
) const;
126 void operator= (const wxStringList
& value
) ;
127 bool operator== (const wxList
& value
) const;
128 bool operator!= (const wxList
& value
) const;
129 void operator= (const wxList
& value
) ;
131 bool operator== (const wxTime
& value
) const;
132 bool operator!= (const wxTime
& value
) const;
133 void operator= (const wxTime
& value
) ;
134 bool operator== (const wxDate
& value
) const;
135 bool operator!= (const wxDate
& value
) const;
136 void operator= (const wxDate
& value
) ;
138 bool operator== (void* value
) const;
139 bool operator!= (void* value
) const;
140 void operator= (void* value
) ;
142 // Treat a list variant as an array
143 wxVariant
operator[] (size_t idx
) const;
144 wxVariant
& operator[] (size_t idx
) ;
146 // Implicit conversion to a wxString
147 inline operator wxString () const { return MakeString(); }
148 wxString
MakeString() const;
150 // Other implicit conversions
151 inline operator double () const { return GetDouble(); }
152 inline operator char () const { return GetChar(); }
153 inline operator long () const { return GetLong(); }
154 inline operator bool () const { return GetBool(); }
156 inline operator wxTime () const { return GetTime(); }
157 inline operator wxDate () const { return GetDate(); }
159 inline operator void* () const { return GetVoidPtr(); }
163 inline void SetName(const wxString
& name
) { m_name
= name
; }
164 inline const wxString
& GetName() const { return m_name
; }
166 // Tests whether there is data
167 inline bool IsNull() const { return (m_data
== (wxVariantData
*) NULL
); }
169 wxVariantData
* GetData() const { return m_data
; }
170 void SetData(wxVariantData
* data
) ;
172 // Returns a string representing the type of the variant,
173 // e.g. "string", "bool", "stringlist", "list", "double", "long"
174 wxString
GetType() const;
176 bool IsType(const wxString
& type
) const;
178 // Return the number of elements in a list
179 int GetCount() const;
182 double GetReal() const ;
183 inline double GetDouble() const { return GetReal(); };
184 long GetInteger() const ;
185 inline long GetLong() const { return GetInteger(); };
186 char GetChar() const ;
187 bool GetBool() const ;
188 wxString
GetString() const ;
189 wxList
& GetList() const ;
190 wxStringList
& GetStringList() const ;
192 wxTime
GetTime() const ;
193 wxDate
GetDate() const ;
195 void* GetVoidPtr() const ;
198 // Make NULL (i.e. delete the data)
205 void Append(const wxVariant
& value
);
207 // Insert at front of list
208 void Insert(const wxVariant
& value
);
210 // Returns TRUE if the variant is a member of the list
211 bool Member(const wxVariant
& value
) const;
213 // Deletes the nth element of the list
214 bool Delete(int item
);
222 bool Convert(long* value
) const;
223 bool Convert(bool* value
) const;
224 bool Convert(double* value
) const;
225 bool Convert(wxString
* value
) const;
226 bool Convert(char* value
) const;
228 bool Convert(wxTime
* value
) const;
229 bool Convert(wxDate
* value
) const;
234 wxVariantData
* m_data
;
238 extern wxVariant WXDLLEXPORT wxNullVariant
;