]>
Commit | Line | Data |
---|---|---|
8cb50e4b JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: variant.h | |
3 | // Purpose: wxVariant class, container for any type | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 10/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) | |
3f4a0c5b | 9 | // Licence: wxWindows licence |
8cb50e4b JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_VARIANT_H_ | |
13 | #define _WX_VARIANT_H_ | |
14 | ||
af49c4b8 | 15 | #if defined(__GNUG__) && !defined(__APPLE__) |
8cb50e4b JS |
16 | #pragma interface "variant.h" |
17 | #endif | |
18 | ||
19 | #include "wx/defs.h" | |
20 | #include "wx/object.h" | |
21 | #include "wx/string.h" | |
22 | #include "wx/list.h" | |
23 | ||
a0a302dc | 24 | #if wxUSE_TIMEDATE |
3f4a0c5b VZ |
25 | #include "wx/time.h" |
26 | #include "wx/date.h" | |
27 | #endif // time/date | |
a0a302dc | 28 | |
e2b87f38 VZ |
29 | #if wxUSE_DATETIME |
30 | #include "wx/datetime.h" | |
31 | #endif // wxUSE_DATETIME | |
edca7a82 GT |
32 | |
33 | #if wxUSE_ODBC | |
34 | #include "wx/db.h" // will #include sqltypes.h | |
35 | #endif //ODBC | |
36 | ||
65f19af1 | 37 | #include "wx/iosfwrap.h" |
8cb50e4b JS |
38 | |
39 | /* | |
40 | * wxVariantData stores the actual data in a wxVariant object, | |
41 | * to allow it to store any type of data. | |
42 | * Derive from this to provide custom data handling. | |
43 | * | |
44 | * TODO: in order to replace wxPropertyValue, we would need | |
45 | * to consider adding constructors that take pointers to C++ variables, | |
46 | * or removing that functionality from the wxProperty library. | |
47 | * Essentially wxPropertyValue takes on some of the wxValidator functionality | |
48 | * by storing pointers and not just actual values, allowing update of C++ data | |
49 | * to be handled automatically. Perhaps there's another way of doing this without | |
50 | * overloading wxVariant with unnecessary functionality. | |
51 | */ | |
52 | ||
53 | class WXDLLEXPORT wxVariantData: public wxObject | |
54 | { | |
55 | DECLARE_ABSTRACT_CLASS(wxVariantData) | |
56 | public: | |
57 | ||
58 | // Construction & destruction | |
59 | wxVariantData() {}; | |
60 | ||
61 | // Override these to provide common functionality | |
62 | // Copy to data | |
63 | virtual void Copy(wxVariantData& data) = 0; | |
64 | virtual bool Eq(wxVariantData& data) const = 0; | |
38830220 | 65 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 66 | virtual bool Write(wxSTD ostream& str) const = 0; |
38830220 | 67 | #endif |
8cb50e4b | 68 | virtual bool Write(wxString& str) const = 0; |
38830220 | 69 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 70 | virtual bool Read(wxSTD istream& str) = 0; |
38830220 | 71 | #endif |
8cb50e4b JS |
72 | virtual bool Read(wxString& str) = 0; |
73 | // What type is it? Return a string name. | |
74 | virtual wxString GetType() const = 0; | |
75 | }; | |
76 | ||
77 | /* | |
78 | * wxVariant can store any kind of data, but has some basic types | |
79 | * built in. | |
80 | * NOTE: this eventually should have a reference-counting implementation. | |
81 | * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating | |
82 | * this class too much. | |
83 | */ | |
84 | ||
85 | class WXDLLEXPORT wxVariant: public wxObject | |
86 | { | |
87 | DECLARE_DYNAMIC_CLASS(wxVariant) | |
88 | public: | |
89 | ||
90 | // Construction & destruction | |
91 | wxVariant(); | |
e90c1d2a VZ |
92 | wxVariant(double val, const wxString& name = wxEmptyString); |
93 | wxVariant(long val, const wxString& name = wxEmptyString); | |
57493f9f | 94 | #ifdef HAVE_BOOL |
e90c1d2a | 95 | wxVariant(bool val, const wxString& name = wxEmptyString); |
57493f9f | 96 | #endif |
e90c1d2a VZ |
97 | wxVariant(char val, const wxString& name = wxEmptyString); |
98 | wxVariant(const wxString& val, const wxString& name = wxEmptyString); | |
99 | wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool! | |
100 | wxVariant(const wxStringList& val, const wxString& name = wxEmptyString); | |
101 | wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants | |
457e6c54 JS |
102 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled |
103 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
e90c1d2a VZ |
104 | wxVariant(const wxTime& val, const wxString& name = wxEmptyString); // Time |
105 | wxVariant(const wxDate& val, const wxString& name = wxEmptyString); // Date | |
a0a302dc | 106 | #endif |
e90c1d2a VZ |
107 | wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose) |
108 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data | |
edca7a82 | 109 | //TODO: Need to document |
e2b87f38 | 110 | #if wxUSE_DATETIME |
edca7a82 | 111 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date |
e2b87f38 | 112 | #endif // wxUSE_DATETIME |
fb42d7c3 | 113 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); // String array |
edca7a82 GT |
114 | #if wxUSE_ODBC |
115 | wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
116 | wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
117 | wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
118 | #endif | |
119 | //TODO: End of Need to document | |
120 | ||
8cb50e4b | 121 | wxVariant(const wxVariant& variant); |
8cb50e4b JS |
122 | ~wxVariant(); |
123 | ||
124 | // Generic operators | |
125 | // Assignment | |
126 | void operator= (const wxVariant& variant); | |
127 | ||
edca7a82 | 128 | //TODO: Need to document |
e2b87f38 | 129 | #if wxUSE_DATETIME |
edca7a82 GT |
130 | bool operator== (const wxDateTime& value) const; |
131 | bool operator!= (const wxDateTime& value) const; | |
132 | void operator= (const wxDateTime& value) ; | |
e2b87f38 | 133 | #endif // wxUSE_DATETIME |
fb42d7c3 VZ |
134 | |
135 | bool operator== (const wxArrayString& value) const; | |
136 | bool operator!= (const wxArrayString& value) const; | |
137 | void operator= (const wxArrayString& value) ; | |
edca7a82 GT |
138 | #if wxUSE_ODBC |
139 | void operator= (const DATE_STRUCT* value) ; | |
140 | void operator= (const TIME_STRUCT* value) ; | |
141 | void operator= (const TIMESTAMP_STRUCT* value) ; | |
142 | #endif | |
143 | //TODO: End of Need to document | |
144 | ||
8cb50e4b JS |
145 | // Assignment using data, e.g. |
146 | // myVariant = new wxStringVariantData("hello"); | |
147 | void operator= (wxVariantData* variantData); | |
148 | bool operator== (const wxVariant& variant) const; | |
149 | bool operator!= (const wxVariant& variant) const; | |
150 | ||
151 | // Specific operators | |
152 | bool operator== (double value) const; | |
153 | bool operator!= (double value) const; | |
154 | void operator= (double value) ; | |
155 | bool operator== (long value) const; | |
156 | bool operator!= (long value) const; | |
157 | void operator= (long value) ; | |
158 | bool operator== (char value) const; | |
159 | bool operator!= (char value) const; | |
160 | void operator= (char value) ; | |
57493f9f | 161 | #ifdef HAVE_BOOL |
8cb50e4b JS |
162 | bool operator== (bool value) const; |
163 | bool operator!= (bool value) const; | |
164 | void operator= (bool value) ; | |
57493f9f | 165 | #endif |
8cb50e4b JS |
166 | bool operator== (const wxString& value) const; |
167 | bool operator!= (const wxString& value) const; | |
168 | void operator= (const wxString& value) ; | |
2ed57eb7 | 169 | void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool! |
8cb50e4b JS |
170 | bool operator== (const wxStringList& value) const; |
171 | bool operator!= (const wxStringList& value) const; | |
172 | void operator= (const wxStringList& value) ; | |
173 | bool operator== (const wxList& value) const; | |
174 | bool operator!= (const wxList& value) const; | |
175 | void operator= (const wxList& value) ; | |
457e6c54 JS |
176 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled |
177 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
a0a302dc JS |
178 | bool operator== (const wxTime& value) const; |
179 | bool operator!= (const wxTime& value) const; | |
180 | void operator= (const wxTime& value) ; | |
181 | bool operator== (const wxDate& value) const; | |
182 | bool operator!= (const wxDate& value) const; | |
183 | void operator= (const wxDate& value) ; | |
184 | #endif | |
185 | bool operator== (void* value) const; | |
186 | bool operator!= (void* value) const; | |
187 | void operator= (void* value) ; | |
8cb50e4b JS |
188 | |
189 | // Treat a list variant as an array | |
190 | wxVariant operator[] (size_t idx) const; | |
191 | wxVariant& operator[] (size_t idx) ; | |
192 | ||
193 | // Implicit conversion to a wxString | |
194 | inline operator wxString () const { return MakeString(); } | |
195 | wxString MakeString() const; | |
196 | ||
197 | // Other implicit conversions | |
198 | inline operator double () const { return GetDouble(); } | |
a0a302dc | 199 | inline operator char () const { return GetChar(); } |
8cb50e4b JS |
200 | inline operator long () const { return GetLong(); } |
201 | inline operator bool () const { return GetBool(); } | |
457e6c54 JS |
202 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled |
203 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
a0a302dc JS |
204 | inline operator wxTime () const { return GetTime(); } |
205 | inline operator wxDate () const { return GetDate(); } | |
206 | #endif | |
207 | inline operator void* () const { return GetVoidPtr(); } | |
edca7a82 | 208 | //TODO: Need to document |
e2b87f38 | 209 | #if wxUSE_DATETIME |
edca7a82 | 210 | inline operator wxDateTime () const { return GetDateTime(); } |
e2b87f38 | 211 | #endif // wxUSE_DATETIME |
edca7a82 | 212 | //TODO: End of Need to document |
8cb50e4b JS |
213 | |
214 | // Accessors | |
a0a302dc JS |
215 | // Sets/gets name |
216 | inline void SetName(const wxString& name) { m_name = name; } | |
217 | inline const wxString& GetName() const { return m_name; } | |
218 | ||
8cb50e4b JS |
219 | // Tests whether there is data |
220 | inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); } | |
221 | ||
222 | wxVariantData* GetData() const { return m_data; } | |
223 | void SetData(wxVariantData* data) ; | |
224 | ||
225 | // Returns a string representing the type of the variant, | |
226 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
227 | wxString GetType() const; | |
228 | ||
229 | bool IsType(const wxString& type) const; | |
230 | ||
231 | // Return the number of elements in a list | |
232 | int GetCount() const; | |
233 | ||
234 | // Value accessors | |
235 | double GetReal() const ; | |
236 | inline double GetDouble() const { return GetReal(); }; | |
237 | long GetInteger() const ; | |
238 | inline long GetLong() const { return GetInteger(); }; | |
239 | char GetChar() const ; | |
240 | bool GetBool() const ; | |
241 | wxString GetString() const ; | |
242 | wxList& GetList() const ; | |
243 | wxStringList& GetStringList() const ; | |
457e6c54 JS |
244 | |
245 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled | |
246 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
a0a302dc JS |
247 | wxTime GetTime() const ; |
248 | wxDate GetDate() const ; | |
249 | #endif | |
250 | void* GetVoidPtr() const ; | |
edca7a82 | 251 | //TODO: Need to document |
e2b87f38 | 252 | #if wxUSE_DATETIME |
edca7a82 | 253 | wxDateTime GetDateTime() const ; |
e2b87f38 | 254 | #endif // wxUSE_DATETIME |
fb42d7c3 | 255 | wxArrayString GetArrayString() const; |
edca7a82 | 256 | //TODO: End of Need to document |
8cb50e4b JS |
257 | |
258 | // Operations | |
259 | // Make NULL (i.e. delete the data) | |
260 | void MakeNull(); | |
261 | ||
262 | // Make empty list | |
263 | void NullList(); | |
264 | ||
265 | // Append to list | |
266 | void Append(const wxVariant& value); | |
267 | ||
268 | // Insert at front of list | |
269 | void Insert(const wxVariant& value); | |
270 | ||
271 | // Returns TRUE if the variant is a member of the list | |
272 | bool Member(const wxVariant& value) const; | |
273 | ||
274 | // Deletes the nth element of the list | |
275 | bool Delete(int item); | |
276 | ||
277 | // Clear list | |
278 | void ClearList(); | |
279 | ||
a0a302dc | 280 | // Implementation |
9708db20 | 281 | public: |
8cb50e4b JS |
282 | // Type conversion |
283 | bool Convert(long* value) const; | |
284 | bool Convert(bool* value) const; | |
285 | bool Convert(double* value) const; | |
286 | bool Convert(wxString* value) const; | |
287 | bool Convert(char* value) const; | |
457e6c54 JS |
288 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled |
289 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
a0a302dc JS |
290 | bool Convert(wxTime* value) const; |
291 | bool Convert(wxDate* value) const; | |
292 | #endif | |
edca7a82 | 293 | //TODO: Need to document |
e2b87f38 | 294 | #if wxUSE_DATETIME |
edca7a82 | 295 | bool Convert(wxDateTime* value) const; |
e2b87f38 | 296 | #endif // wxUSE_DATETIME |
edca7a82 | 297 | //TODO: End of Need to document |
8cb50e4b JS |
298 | |
299 | // Attributes | |
300 | protected: | |
301 | wxVariantData* m_data; | |
a0a302dc | 302 | wxString m_name; |
8cb50e4b JS |
303 | }; |
304 | ||
3ba055ac | 305 | extern wxVariant WXDLLEXPORT wxNullVariant; |
a0a302dc | 306 | |
8cb50e4b JS |
307 | #endif |
308 | // _WX_VARIANT_H_ |