]>
Commit | Line | Data |
---|---|---|
8cb50e4b | 1 | ///////////////////////////////////////////////////////////////////////////// |
43f06cfd | 2 | // Name: wx/variant.h |
8cb50e4b JS |
3 | // Purpose: wxVariant class, container for any type |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 10/09/98 | |
7 | // RCS-ID: $Id$ | |
99d80019 | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
8cb50e4b JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_VARIANT_H_ | |
13 | #define _WX_VARIANT_H_ | |
14 | ||
8cb50e4b | 15 | #include "wx/defs.h" |
d5dc103f VZ |
16 | |
17 | #if wxUSE_VARIANT | |
18 | ||
8cb50e4b JS |
19 | #include "wx/object.h" |
20 | #include "wx/string.h" | |
4c3ebca9 | 21 | #include "wx/arrstr.h" |
8cb50e4b JS |
22 | #include "wx/list.h" |
23 | ||
e2b87f38 VZ |
24 | #if wxUSE_DATETIME |
25 | #include "wx/datetime.h" | |
26 | #endif // wxUSE_DATETIME | |
edca7a82 GT |
27 | |
28 | #if wxUSE_ODBC | |
29 | #include "wx/db.h" // will #include sqltypes.h | |
30 | #endif //ODBC | |
31 | ||
65f19af1 | 32 | #include "wx/iosfwrap.h" |
8cb50e4b JS |
33 | |
34 | /* | |
35 | * wxVariantData stores the actual data in a wxVariant object, | |
36 | * to allow it to store any type of data. | |
37 | * Derive from this to provide custom data handling. | |
38 | * | |
2562c823 RR |
39 | * NB: To prevent addition of extra vtbl pointer to wxVariantData, |
40 | * we don't multiple-inherit from wxObjectRefData. Instead, | |
41 | * we simply replicate the wxObject ref-counting scheme. | |
42 | * | |
43 | * NB: When you construct a wxVariantData, it will have refcount | |
44 | * of one. Refcount will not be further increased when | |
45 | * it is passed to wxVariant. This simulates old common | |
46 | * scenario where wxVariant took ownership of wxVariantData | |
47 | * passed to it. | |
48 | * If you create wxVariantData for other reasons than passing | |
49 | * it to wxVariant, technically you are not required to call | |
50 | * DecRef() before deleting it. | |
51 | * | |
8cb50e4b JS |
52 | * TODO: in order to replace wxPropertyValue, we would need |
53 | * to consider adding constructors that take pointers to C++ variables, | |
54 | * or removing that functionality from the wxProperty library. | |
55 | * Essentially wxPropertyValue takes on some of the wxValidator functionality | |
56 | * by storing pointers and not just actual values, allowing update of C++ data | |
57 | * to be handled automatically. Perhaps there's another way of doing this without | |
58 | * overloading wxVariant with unnecessary functionality. | |
59 | */ | |
60 | ||
bddd7a8d | 61 | class WXDLLIMPEXP_BASE wxVariantData: public wxObject |
8cb50e4b | 62 | { |
2562c823 | 63 | friend class wxVariant; |
8cb50e4b | 64 | public: |
2562c823 RR |
65 | wxVariantData() |
66 | : wxObject(), m_count(1) | |
67 | { } | |
8cb50e4b | 68 | |
2562c823 | 69 | // Override these to provide common functionality |
8cb50e4b | 70 | virtual bool Eq(wxVariantData& data) const = 0; |
2562c823 | 71 | |
38830220 | 72 | #if wxUSE_STD_IOSTREAM |
2562c823 | 73 | virtual bool Write(wxSTD ostream& str) const { return false; } |
38830220 | 74 | #endif |
2562c823 | 75 | virtual bool Write(wxString& str) const { return false; } |
38830220 | 76 | #if wxUSE_STD_IOSTREAM |
2562c823 | 77 | virtual bool Read(wxSTD istream& str) { return false; } |
38830220 | 78 | #endif |
2562c823 | 79 | virtual bool Read(wxString& str) { return false; } |
8cb50e4b JS |
80 | // What type is it? Return a string name. |
81 | virtual wxString GetType() const = 0; | |
cf6ae290 RG |
82 | // If it based on wxObject return the ClassInfo. |
83 | virtual wxClassInfo* GetValueClassInfo() { return NULL; } | |
2562c823 RR |
84 | |
85 | void IncRef() { m_count++; } | |
86 | void DecRef() | |
87 | { | |
88 | if ( --m_count == 0 ) | |
89 | delete this; | |
90 | } | |
91 | ||
92 | int GetRefCount() const { return m_count; } | |
93 | ||
94 | protected: | |
95 | // Protected dtor should make some incompatible code | |
96 | // break more louder. That is, they should do data->DecRef() | |
97 | // instead of delete data. | |
98 | virtual ~wxVariantData() { } | |
99 | ||
100 | private: | |
101 | int m_count; | |
102 | ||
103 | private: | |
104 | DECLARE_ABSTRACT_CLASS(wxVariantData) | |
8cb50e4b JS |
105 | }; |
106 | ||
107 | /* | |
108 | * wxVariant can store any kind of data, but has some basic types | |
109 | * built in. | |
8cb50e4b JS |
110 | */ |
111 | ||
bddd7a8d | 112 | class WXDLLIMPEXP_BASE wxVariant: public wxObject |
8cb50e4b | 113 | { |
8cb50e4b | 114 | public: |
8cb50e4b | 115 | wxVariant(); |
2562c823 | 116 | |
8cb50e4b | 117 | wxVariant(const wxVariant& variant); |
2562c823 | 118 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); |
d3c7fc99 | 119 | virtual ~wxVariant(); |
8cb50e4b | 120 | |
2562c823 | 121 | // generic assignment |
8cb50e4b JS |
122 | void operator= (const wxVariant& variant); |
123 | ||
124 | // Assignment using data, e.g. | |
125 | // myVariant = new wxStringVariantData("hello"); | |
126 | void operator= (wxVariantData* variantData); | |
2562c823 | 127 | |
8cb50e4b JS |
128 | bool operator== (const wxVariant& variant) const; |
129 | bool operator!= (const wxVariant& variant) const; | |
130 | ||
2562c823 RR |
131 | // Sets/gets name |
132 | inline void SetName(const wxString& name) { m_name = name; } | |
133 | inline const wxString& GetName() const { return m_name; } | |
134 | ||
135 | // Tests whether there is data | |
136 | bool IsNull() const; | |
137 | ||
138 | // For compatibility with wxWidgets <= 2.6, this doesn't increase | |
139 | // reference count. | |
140 | wxVariantData* GetData() const { return m_data; } | |
141 | void SetData(wxVariantData* data) ; | |
142 | ||
143 | // make a 'clone' of the object | |
144 | void Ref(const wxVariant& clone); | |
145 | ||
146 | // destroy a reference | |
147 | void UnRef(); | |
148 | ||
149 | // Make NULL (i.e. delete the data) | |
150 | void MakeNull(); | |
151 | ||
152 | // Delete data and name | |
153 | void Clear(); | |
154 | ||
155 | // Returns a string representing the type of the variant, | |
156 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
157 | wxString GetType() const; | |
158 | ||
159 | bool IsType(const wxString& type) const; | |
160 | bool IsValueKindOf(const wxClassInfo* type) const; | |
161 | ||
162 | // write contents to a string (e.g. for debugging) | |
163 | wxString MakeString() const; | |
164 | ||
165 | // double | |
166 | wxVariant(double val, const wxString& name = wxEmptyString); | |
8cb50e4b JS |
167 | bool operator== (double value) const; |
168 | bool operator!= (double value) const; | |
169 | void operator= (double value) ; | |
2562c823 RR |
170 | inline operator double () const { return GetDouble(); } |
171 | 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); | |
8cb50e4b JS |
178 | bool operator== (long value) const; |
179 | bool operator!= (long value) const; | |
180 | void operator= (long value) ; | |
2562c823 RR |
181 | inline operator long () const { return GetLong(); } |
182 | inline long GetInteger() const { return GetLong(); } | |
183 | long GetLong() const; | |
184 | ||
185 | // bool | |
57493f9f | 186 | #ifdef HAVE_BOOL |
2562c823 | 187 | wxVariant(bool val, const wxString& name = wxEmptyString); |
8cb50e4b JS |
188 | bool operator== (bool value) const; |
189 | bool operator!= (bool value) const; | |
190 | void operator= (bool value) ; | |
2562c823 RR |
191 | inline operator bool () const { return GetBool(); } |
192 | bool GetBool() const ; | |
193 | #endif | |
194 | ||
195 | // wxDateTime | |
196 | #if wxUSE_DATETIME | |
197 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); | |
198 | #if wxUSE_ODBC | |
199 | wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); | |
200 | wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); | |
201 | wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); | |
202 | #endif | |
203 | bool operator== (const wxDateTime& value) const; | |
204 | bool operator!= (const wxDateTime& value) const; | |
205 | void operator= (const wxDateTime& value) ; | |
206 | #if wxUSE_ODBC | |
207 | void operator= (const DATE_STRUCT* value) ; | |
208 | void operator= (const TIME_STRUCT* value) ; | |
209 | void operator= (const TIMESTAMP_STRUCT* value) ; | |
210 | #endif | |
211 | inline operator wxDateTime () const { return GetDateTime(); } | |
212 | wxDateTime GetDateTime() const; | |
57493f9f | 213 | #endif |
2562c823 RR |
214 | |
215 | // wxString | |
216 | wxVariant(const wxString& val, const wxString& name = wxEmptyString); | |
217 | wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool! | |
8cb50e4b JS |
218 | bool operator== (const wxString& value) const; |
219 | bool operator!= (const wxString& value) const; | |
220 | void operator= (const wxString& value) ; | |
2ed57eb7 | 221 | void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool! |
2562c823 RR |
222 | inline operator wxString () const { return MakeString(); } |
223 | wxString GetString() const; | |
224 | ||
71520754 RR |
225 | // wxChar |
226 | wxVariant(wxChar val, const wxString& name = wxEmptyString); | |
227 | bool operator== (wxChar value) const; | |
228 | bool operator!= (wxChar value) const; | |
229 | void operator= (wxChar value) ; | |
230 | inline operator wxChar () const { return GetChar(); } | |
231 | wxChar GetChar() const ; | |
2562c823 RR |
232 | |
233 | // wxArrayString | |
234 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); | |
235 | bool operator== (const wxArrayString& value) const; | |
236 | bool operator!= (const wxArrayString& value) const; | |
237 | void operator= (const wxArrayString& value); | |
238 | inline operator wxArrayString () const { return GetArrayString(); } | |
239 | wxArrayString GetArrayString() const; | |
240 | ||
241 | // void* | |
242 | wxVariant(void* ptr, const wxString& name = wxEmptyString); | |
243 | bool operator== (void* value) const; | |
244 | bool operator!= (void* value) const; | |
245 | void operator= (void* value); | |
246 | inline operator void* () const { return GetVoidPtr(); } | |
247 | void* GetVoidPtr() const; | |
248 | ||
249 | // wxObject* | |
250 | wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); | |
251 | bool operator== (wxObject* value) const; | |
252 | bool operator!= (wxObject* value) const; | |
253 | void operator= (wxObject* value); | |
254 | wxObject* GetWxObjectPtr() const; | |
255 | ||
256 | ||
2c3a1064 | 257 | #if WXWIN_COMPATIBILITY_2_4 |
2562c823 | 258 | wxDEPRECATED( wxVariant(const wxStringList& val, const wxString& name = wxEmptyString) ); |
2c3a1064 RN |
259 | wxDEPRECATED( bool operator== (const wxStringList& value) const ); |
260 | wxDEPRECATED( bool operator!= (const wxStringList& value) const ); | |
261 | wxDEPRECATED( void operator= (const wxStringList& value) ); | |
2562c823 | 262 | wxDEPRECATED( wxStringList& GetStringList() const ); |
2c3a1064 | 263 | #endif |
2562c823 RR |
264 | |
265 | // ------------------------------ | |
266 | // list operations | |
267 | // ------------------------------ | |
268 | ||
269 | wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants | |
8cb50e4b JS |
270 | bool operator== (const wxList& value) const; |
271 | bool operator!= (const wxList& value) const; | |
272 | void operator= (const wxList& value) ; | |
8cb50e4b JS |
273 | // Treat a list variant as an array |
274 | wxVariant operator[] (size_t idx) const; | |
275 | wxVariant& operator[] (size_t idx) ; | |
2562c823 | 276 | wxList& GetList() const ; |
8cb50e4b JS |
277 | |
278 | // Return the number of elements in a list | |
43f06cfd | 279 | size_t GetCount() const; |
2562c823 | 280 | |
8cb50e4b JS |
281 | // Make empty list |
282 | void NullList(); | |
283 | ||
284 | // Append to list | |
285 | void Append(const wxVariant& value); | |
286 | ||
287 | // Insert at front of list | |
288 | void Insert(const wxVariant& value); | |
289 | ||
cab1a605 | 290 | // Returns true if the variant is a member of the list |
8cb50e4b JS |
291 | bool Member(const wxVariant& value) const; |
292 | ||
293 | // Deletes the nth element of the list | |
43f06cfd | 294 | bool Delete(size_t item); |
8cb50e4b JS |
295 | |
296 | // Clear list | |
297 | void ClearList(); | |
298 | ||
9708db20 | 299 | public: |
2562c823 | 300 | // Type conversion |
8cb50e4b JS |
301 | bool Convert(long* value) const; |
302 | bool Convert(bool* value) const; | |
303 | bool Convert(double* value) const; | |
304 | bool Convert(wxString* value) const; | |
305 | bool Convert(char* value) const; | |
e2b87f38 | 306 | #if wxUSE_DATETIME |
edca7a82 | 307 | bool Convert(wxDateTime* value) const; |
e2b87f38 | 308 | #endif // wxUSE_DATETIME |
8cb50e4b JS |
309 | |
310 | // Attributes | |
311 | protected: | |
312 | wxVariantData* m_data; | |
a0a302dc | 313 | wxString m_name; |
2562c823 RR |
314 | |
315 | private: | |
316 | DECLARE_DYNAMIC_CLASS(wxVariant) | |
8cb50e4b JS |
317 | }; |
318 | ||
cf6ae290 | 319 | //Since we want type safety wxVariant we need to fetch and dynamic_cast |
cab1a605 | 320 | //in a seemingly safe way so the compiler can check, so we define |
cf6ae290 RG |
321 | //a dynamic_cast /wxDynamicCast analogue. |
322 | ||
323 | #define wxGetVariantCast(var,classname) \ | |
cab1a605 WS |
324 | ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\ |
325 | var.GetWxObjectPtr() : NULL)); | |
cf6ae290 | 326 | |
bddd7a8d | 327 | extern wxVariant WXDLLIMPEXP_BASE wxNullVariant; |
a0a302dc | 328 | |
d5dc103f VZ |
329 | #endif // wxUSE_VARIANT |
330 | ||
331 | #endif // _WX_VARIANT_H_ |