]>
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 | * | |
39 | * TODO: in order to replace wxPropertyValue, we would need | |
40 | * to consider adding constructors that take pointers to C++ variables, | |
41 | * or removing that functionality from the wxProperty library. | |
42 | * Essentially wxPropertyValue takes on some of the wxValidator functionality | |
43 | * by storing pointers and not just actual values, allowing update of C++ data | |
44 | * to be handled automatically. Perhaps there's another way of doing this without | |
45 | * overloading wxVariant with unnecessary functionality. | |
46 | */ | |
47 | ||
bddd7a8d | 48 | class WXDLLIMPEXP_BASE wxVariantData: public wxObject |
8cb50e4b JS |
49 | { |
50 | DECLARE_ABSTRACT_CLASS(wxVariantData) | |
51 | public: | |
52 | ||
53 | // Construction & destruction | |
6fb99eb3 | 54 | wxVariantData() {} |
8cb50e4b JS |
55 | |
56 | // Override these to provide common functionality | |
57 | // Copy to data | |
58 | virtual void Copy(wxVariantData& data) = 0; | |
59 | virtual bool Eq(wxVariantData& data) const = 0; | |
38830220 | 60 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 61 | virtual bool Write(wxSTD ostream& str) const = 0; |
38830220 | 62 | #endif |
8cb50e4b | 63 | virtual bool Write(wxString& str) const = 0; |
38830220 | 64 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 65 | virtual bool Read(wxSTD istream& str) = 0; |
38830220 | 66 | #endif |
8cb50e4b JS |
67 | virtual bool Read(wxString& str) = 0; |
68 | // What type is it? Return a string name. | |
69 | virtual wxString GetType() const = 0; | |
cf6ae290 RG |
70 | // If it based on wxObject return the ClassInfo. |
71 | virtual wxClassInfo* GetValueClassInfo() { return NULL; } | |
8cb50e4b JS |
72 | }; |
73 | ||
74 | /* | |
75 | * wxVariant can store any kind of data, but has some basic types | |
76 | * built in. | |
77 | * NOTE: this eventually should have a reference-counting implementation. | |
78 | * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating | |
79 | * this class too much. | |
80 | */ | |
81 | ||
bddd7a8d | 82 | class WXDLLIMPEXP_BASE wxVariant: public wxObject |
8cb50e4b JS |
83 | { |
84 | DECLARE_DYNAMIC_CLASS(wxVariant) | |
85 | public: | |
86 | ||
87 | // Construction & destruction | |
88 | wxVariant(); | |
e90c1d2a VZ |
89 | wxVariant(double val, const wxString& name = wxEmptyString); |
90 | wxVariant(long val, const wxString& name = wxEmptyString); | |
57493f9f | 91 | #ifdef HAVE_BOOL |
e90c1d2a | 92 | wxVariant(bool val, const wxString& name = wxEmptyString); |
57493f9f | 93 | #endif |
e90c1d2a VZ |
94 | wxVariant(char val, const wxString& name = wxEmptyString); |
95 | wxVariant(const wxString& val, const wxString& name = wxEmptyString); | |
96 | wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool! | |
2c3a1064 RN |
97 | #if WXWIN_COMPATIBILITY_2_4 |
98 | wxDEPRECATED( wxVariant(const wxStringList& val, const wxString& name = wxEmptyString) ); | |
99 | #endif | |
e90c1d2a | 100 | wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants |
e90c1d2a | 101 | wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose) |
cab1a605 | 102 | wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); //wxObject |
e90c1d2a | 103 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data |
e2b87f38 | 104 | #if wxUSE_DATETIME |
edca7a82 | 105 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date |
e2b87f38 | 106 | #endif // wxUSE_DATETIME |
fb42d7c3 | 107 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); // String array |
edca7a82 GT |
108 | #if wxUSE_ODBC |
109 | wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
110 | wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
111 | wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
112 | #endif | |
cab1a605 | 113 | |
8cb50e4b | 114 | wxVariant(const wxVariant& variant); |
d3c7fc99 | 115 | virtual ~wxVariant(); |
8cb50e4b JS |
116 | |
117 | // Generic operators | |
118 | // Assignment | |
119 | void operator= (const wxVariant& variant); | |
120 | ||
e2b87f38 | 121 | #if wxUSE_DATETIME |
edca7a82 GT |
122 | bool operator== (const wxDateTime& value) const; |
123 | bool operator!= (const wxDateTime& value) const; | |
124 | void operator= (const wxDateTime& value) ; | |
e2b87f38 | 125 | #endif // wxUSE_DATETIME |
fb42d7c3 VZ |
126 | |
127 | bool operator== (const wxArrayString& value) const; | |
128 | bool operator!= (const wxArrayString& value) const; | |
129 | void operator= (const wxArrayString& value) ; | |
edca7a82 GT |
130 | #if wxUSE_ODBC |
131 | void operator= (const DATE_STRUCT* value) ; | |
132 | void operator= (const TIME_STRUCT* value) ; | |
133 | void operator= (const TIMESTAMP_STRUCT* value) ; | |
134 | #endif | |
edca7a82 | 135 | |
8cb50e4b JS |
136 | // Assignment using data, e.g. |
137 | // myVariant = new wxStringVariantData("hello"); | |
138 | void operator= (wxVariantData* variantData); | |
139 | bool operator== (const wxVariant& variant) const; | |
140 | bool operator!= (const wxVariant& variant) const; | |
141 | ||
142 | // Specific operators | |
143 | bool operator== (double value) const; | |
144 | bool operator!= (double value) const; | |
145 | void operator= (double value) ; | |
146 | bool operator== (long value) const; | |
147 | bool operator!= (long value) const; | |
148 | void operator= (long value) ; | |
149 | bool operator== (char value) const; | |
150 | bool operator!= (char value) const; | |
151 | void operator= (char value) ; | |
57493f9f | 152 | #ifdef HAVE_BOOL |
8cb50e4b JS |
153 | bool operator== (bool value) const; |
154 | bool operator!= (bool value) const; | |
155 | void operator= (bool value) ; | |
57493f9f | 156 | #endif |
8cb50e4b JS |
157 | bool operator== (const wxString& value) const; |
158 | bool operator!= (const wxString& value) const; | |
159 | void operator= (const wxString& value) ; | |
2ed57eb7 | 160 | void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool! |
2c3a1064 RN |
161 | #if WXWIN_COMPATIBILITY_2_4 |
162 | wxDEPRECATED( bool operator== (const wxStringList& value) const ); | |
163 | wxDEPRECATED( bool operator!= (const wxStringList& value) const ); | |
164 | wxDEPRECATED( void operator= (const wxStringList& value) ); | |
165 | #endif | |
8cb50e4b JS |
166 | bool operator== (const wxList& value) const; |
167 | bool operator!= (const wxList& value) const; | |
168 | void operator= (const wxList& value) ; | |
a0a302dc JS |
169 | bool operator== (void* value) const; |
170 | bool operator!= (void* value) const; | |
2f620946 RR |
171 | void operator= (void* value); |
172 | bool operator== (wxObject* value) const; | |
173 | bool operator!= (wxObject* value) const; | |
174 | void operator= (wxObject* value); | |
175 | ||
8cb50e4b JS |
176 | |
177 | // Treat a list variant as an array | |
178 | wxVariant operator[] (size_t idx) const; | |
179 | wxVariant& operator[] (size_t idx) ; | |
180 | ||
181 | // Implicit conversion to a wxString | |
182 | inline operator wxString () const { return MakeString(); } | |
183 | wxString MakeString() const; | |
184 | ||
185 | // Other implicit conversions | |
186 | inline operator double () const { return GetDouble(); } | |
a0a302dc | 187 | inline operator char () const { return GetChar(); } |
8cb50e4b JS |
188 | inline operator long () const { return GetLong(); } |
189 | inline operator bool () const { return GetBool(); } | |
a0a302dc | 190 | inline operator void* () const { return GetVoidPtr(); } |
cf6ae290 RG |
191 | // No implicit conversion to wxObject, as that would really |
192 | // confuse people between conversion to our contained data | |
193 | // and downcasting to see our base type. | |
e2b87f38 | 194 | #if wxUSE_DATETIME |
edca7a82 | 195 | inline operator wxDateTime () const { return GetDateTime(); } |
e2b87f38 | 196 | #endif // wxUSE_DATETIME |
8cb50e4b JS |
197 | |
198 | // Accessors | |
a0a302dc JS |
199 | // Sets/gets name |
200 | inline void SetName(const wxString& name) { m_name = name; } | |
201 | inline const wxString& GetName() const { return m_name; } | |
202 | ||
8cb50e4b JS |
203 | // Tests whether there is data |
204 | inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); } | |
205 | ||
206 | wxVariantData* GetData() const { return m_data; } | |
207 | void SetData(wxVariantData* data) ; | |
208 | ||
209 | // Returns a string representing the type of the variant, | |
210 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
211 | wxString GetType() const; | |
212 | ||
213 | bool IsType(const wxString& type) const; | |
cf6ae290 | 214 | bool IsValueKindOf(const wxClassInfo* type) const; |
8cb50e4b JS |
215 | |
216 | // Return the number of elements in a list | |
43f06cfd | 217 | size_t GetCount() const; |
8cb50e4b JS |
218 | |
219 | // Value accessors | |
220 | double GetReal() const ; | |
221 | inline double GetDouble() const { return GetReal(); }; | |
222 | long GetInteger() const ; | |
223 | inline long GetLong() const { return GetInteger(); }; | |
224 | char GetChar() const ; | |
225 | bool GetBool() const ; | |
226 | wxString GetString() const ; | |
227 | wxList& GetList() const ; | |
2c3a1064 RN |
228 | #if WXWIN_COMPATIBILITY_2_4 |
229 | wxDEPRECATED( wxStringList& GetStringList() const ); | |
230 | #endif | |
63574b81 RR |
231 | void* GetVoidPtr() const; |
232 | wxObject* GetWxObjectPtr() const; | |
e2b87f38 | 233 | #if wxUSE_DATETIME |
edca7a82 | 234 | wxDateTime GetDateTime() const ; |
e2b87f38 | 235 | #endif // wxUSE_DATETIME |
fb42d7c3 | 236 | wxArrayString GetArrayString() const; |
8cb50e4b JS |
237 | |
238 | // Operations | |
239 | // Make NULL (i.e. delete the data) | |
240 | void MakeNull(); | |
241 | ||
242 | // Make empty list | |
243 | void NullList(); | |
244 | ||
245 | // Append to list | |
246 | void Append(const wxVariant& value); | |
247 | ||
248 | // Insert at front of list | |
249 | void Insert(const wxVariant& value); | |
250 | ||
cab1a605 | 251 | // Returns true if the variant is a member of the list |
8cb50e4b JS |
252 | bool Member(const wxVariant& value) const; |
253 | ||
254 | // Deletes the nth element of the list | |
43f06cfd | 255 | bool Delete(size_t item); |
8cb50e4b JS |
256 | |
257 | // Clear list | |
258 | void ClearList(); | |
259 | ||
a0a302dc | 260 | // Implementation |
9708db20 | 261 | public: |
8cb50e4b JS |
262 | // Type conversion |
263 | bool Convert(long* value) const; | |
264 | bool Convert(bool* value) const; | |
265 | bool Convert(double* value) const; | |
266 | bool Convert(wxString* value) const; | |
267 | bool Convert(char* value) const; | |
e2b87f38 | 268 | #if wxUSE_DATETIME |
edca7a82 | 269 | bool Convert(wxDateTime* value) const; |
e2b87f38 | 270 | #endif // wxUSE_DATETIME |
8cb50e4b JS |
271 | |
272 | // Attributes | |
273 | protected: | |
274 | wxVariantData* m_data; | |
a0a302dc | 275 | wxString m_name; |
8cb50e4b JS |
276 | }; |
277 | ||
cf6ae290 | 278 | //Since we want type safety wxVariant we need to fetch and dynamic_cast |
cab1a605 | 279 | //in a seemingly safe way so the compiler can check, so we define |
cf6ae290 RG |
280 | //a dynamic_cast /wxDynamicCast analogue. |
281 | ||
282 | #define wxGetVariantCast(var,classname) \ | |
cab1a605 WS |
283 | ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\ |
284 | var.GetWxObjectPtr() : NULL)); | |
cf6ae290 | 285 | |
bddd7a8d | 286 | extern wxVariant WXDLLIMPEXP_BASE wxNullVariant; |
a0a302dc | 287 | |
d5dc103f VZ |
288 | #endif // wxUSE_VARIANT |
289 | ||
290 | #endif // _WX_VARIANT_H_ |