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