]>
Commit | Line | Data |
---|---|---|
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) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_VARIANT_H_ | |
13 | #define _WX_VARIANT_H_ | |
14 | ||
15 | #if defined(__GNUG__) && !defined(__APPLE__) | |
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 | ||
24 | #if wxUSE_TIMEDATE | |
25 | #include "wx/time.h" | |
26 | #include "wx/date.h" | |
27 | #endif // time/date | |
28 | ||
29 | #if wxUSE_DATETIME | |
30 | #include "wx/datetime.h" | |
31 | #endif // wxUSE_DATETIME | |
32 | ||
33 | #if wxUSE_ODBC | |
34 | #include "wx/db.h" // will #include sqltypes.h | |
35 | #endif //ODBC | |
36 | ||
37 | #include "wx/iosfwrap.h" | |
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; | |
65 | #if wxUSE_STD_IOSTREAM | |
66 | virtual bool Write(wxSTD ostream& str) const = 0; | |
67 | #endif | |
68 | virtual bool Write(wxString& str) const = 0; | |
69 | #if wxUSE_STD_IOSTREAM | |
70 | virtual bool Read(wxSTD istream& str) = 0; | |
71 | #endif | |
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(); | |
92 | wxVariant(double val, const wxString& name = wxEmptyString); | |
93 | wxVariant(long val, const wxString& name = wxEmptyString); | |
94 | #ifdef HAVE_BOOL | |
95 | wxVariant(bool val, const wxString& name = wxEmptyString); | |
96 | #endif | |
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 | |
102 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled | |
103 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
104 | wxVariant(const wxTime& val, const wxString& name = wxEmptyString); // Time | |
105 | wxVariant(const wxDate& val, const wxString& name = wxEmptyString); // Date | |
106 | #endif | |
107 | wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose) | |
108 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data | |
109 | //TODO: Need to document | |
110 | #if wxUSE_DATETIME | |
111 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date | |
112 | #endif // wxUSE_DATETIME | |
113 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); // String array | |
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 | ||
121 | wxVariant(const wxVariant& variant); | |
122 | ~wxVariant(); | |
123 | ||
124 | // Generic operators | |
125 | // Assignment | |
126 | void operator= (const wxVariant& variant); | |
127 | ||
128 | //TODO: Need to document | |
129 | #if wxUSE_DATETIME | |
130 | bool operator== (const wxDateTime& value) const; | |
131 | bool operator!= (const wxDateTime& value) const; | |
132 | void operator= (const wxDateTime& value) ; | |
133 | #endif // wxUSE_DATETIME | |
134 | ||
135 | bool operator== (const wxArrayString& value) const; | |
136 | bool operator!= (const wxArrayString& value) const; | |
137 | void operator= (const wxArrayString& value) ; | |
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 | ||
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) ; | |
161 | #ifdef HAVE_BOOL | |
162 | bool operator== (bool value) const; | |
163 | bool operator!= (bool value) const; | |
164 | void operator= (bool value) ; | |
165 | #endif | |
166 | bool operator== (const wxString& value) const; | |
167 | bool operator!= (const wxString& value) const; | |
168 | void operator= (const wxString& value) ; | |
169 | void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool! | |
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) ; | |
176 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled | |
177 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
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) ; | |
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(); } | |
199 | inline operator char () const { return GetChar(); } | |
200 | inline operator long () const { return GetLong(); } | |
201 | inline operator bool () const { return GetBool(); } | |
202 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled | |
203 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
204 | inline operator wxTime () const { return GetTime(); } | |
205 | inline operator wxDate () const { return GetDate(); } | |
206 | #endif | |
207 | inline operator void* () const { return GetVoidPtr(); } | |
208 | //TODO: Need to document | |
209 | #if wxUSE_DATETIME | |
210 | inline operator wxDateTime () const { return GetDateTime(); } | |
211 | #endif // wxUSE_DATETIME | |
212 | //TODO: End of Need to document | |
213 | ||
214 | // Accessors | |
215 | // Sets/gets name | |
216 | inline void SetName(const wxString& name) { m_name = name; } | |
217 | inline const wxString& GetName() const { return m_name; } | |
218 | ||
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 ; | |
244 | ||
245 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled | |
246 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
247 | wxTime GetTime() const ; | |
248 | wxDate GetDate() const ; | |
249 | #endif | |
250 | void* GetVoidPtr() const ; | |
251 | //TODO: Need to document | |
252 | #if wxUSE_DATETIME | |
253 | wxDateTime GetDateTime() const ; | |
254 | #endif // wxUSE_DATETIME | |
255 | wxArrayString GetArrayString() const; | |
256 | //TODO: End of Need to document | |
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 | ||
280 | // Implementation | |
281 | public: | |
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; | |
288 | // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled | |
289 | #if wxUSE_TIMEDATE && !defined(__WATCOMC__) | |
290 | bool Convert(wxTime* value) const; | |
291 | bool Convert(wxDate* value) const; | |
292 | #endif | |
293 | //TODO: Need to document | |
294 | #if wxUSE_DATETIME | |
295 | bool Convert(wxDateTime* value) const; | |
296 | #endif // wxUSE_DATETIME | |
297 | //TODO: End of Need to document | |
298 | ||
299 | // Attributes | |
300 | protected: | |
301 | wxVariantData* m_data; | |
302 | wxString m_name; | |
303 | }; | |
304 | ||
305 | extern wxVariant WXDLLEXPORT wxNullVariant; | |
306 | ||
307 | #endif | |
308 | // _WX_VARIANT_H_ |