]>
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_DATETIME | |
25 | #include "wx/datetime.h" | |
26 | #endif // wxUSE_DATETIME | |
27 | ||
28 | #if wxUSE_ODBC | |
29 | #include "wx/db.h" // will #include sqltypes.h | |
30 | #endif //ODBC | |
31 | ||
32 | #include "wx/iosfwrap.h" | |
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 | ||
48 | class WXDLLEXPORT wxVariantData: public wxObject | |
49 | { | |
50 | DECLARE_ABSTRACT_CLASS(wxVariantData) | |
51 | public: | |
52 | ||
53 | // Construction & destruction | |
54 | wxVariantData() {}; | |
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; | |
60 | #if wxUSE_STD_IOSTREAM | |
61 | virtual bool Write(wxSTD ostream& str) const = 0; | |
62 | #endif | |
63 | virtual bool Write(wxString& str) const = 0; | |
64 | #if wxUSE_STD_IOSTREAM | |
65 | virtual bool Read(wxSTD istream& str) = 0; | |
66 | #endif | |
67 | virtual bool Read(wxString& str) = 0; | |
68 | // What type is it? Return a string name. | |
69 | virtual wxString GetType() const = 0; | |
70 | // If it based on wxObject return the ClassInfo. | |
71 | virtual wxClassInfo* GetValueClassInfo() { return NULL; } | |
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 | ||
82 | class WXDLLEXPORT wxVariant: public wxObject | |
83 | { | |
84 | DECLARE_DYNAMIC_CLASS(wxVariant) | |
85 | public: | |
86 | ||
87 | // Construction & destruction | |
88 | wxVariant(); | |
89 | wxVariant(double val, const wxString& name = wxEmptyString); | |
90 | wxVariant(long val, const wxString& name = wxEmptyString); | |
91 | #ifdef HAVE_BOOL | |
92 | wxVariant(bool val, const wxString& name = wxEmptyString); | |
93 | #endif | |
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! | |
97 | wxVariant(const wxStringList& val, const wxString& name = wxEmptyString); | |
98 | wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants | |
99 | wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose) | |
100 | wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); //wxObject | |
101 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data | |
102 | //TODO: Need to document | |
103 | #if wxUSE_DATETIME | |
104 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date | |
105 | #endif // wxUSE_DATETIME | |
106 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); // String array | |
107 | #if wxUSE_ODBC | |
108 | wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
109 | wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
110 | wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime | |
111 | #endif | |
112 | //TODO: End of Need to document | |
113 | ||
114 | wxVariant(const wxVariant& variant); | |
115 | ~wxVariant(); | |
116 | ||
117 | // Generic operators | |
118 | // Assignment | |
119 | void operator= (const wxVariant& variant); | |
120 | ||
121 | //TODO: Need to document | |
122 | #if wxUSE_DATETIME | |
123 | bool operator== (const wxDateTime& value) const; | |
124 | bool operator!= (const wxDateTime& value) const; | |
125 | void operator= (const wxDateTime& value) ; | |
126 | #endif // wxUSE_DATETIME | |
127 | ||
128 | bool operator== (const wxArrayString& value) const; | |
129 | bool operator!= (const wxArrayString& value) const; | |
130 | void operator= (const wxArrayString& value) ; | |
131 | #if wxUSE_ODBC | |
132 | void operator= (const DATE_STRUCT* value) ; | |
133 | void operator= (const TIME_STRUCT* value) ; | |
134 | void operator= (const TIMESTAMP_STRUCT* value) ; | |
135 | #endif | |
136 | //TODO: End of Need to document | |
137 | ||
138 | // Assignment using data, e.g. | |
139 | // myVariant = new wxStringVariantData("hello"); | |
140 | void operator= (wxVariantData* variantData); | |
141 | bool operator== (const wxVariant& variant) const; | |
142 | bool operator!= (const wxVariant& variant) const; | |
143 | ||
144 | // Specific operators | |
145 | bool operator== (double value) const; | |
146 | bool operator!= (double value) const; | |
147 | void operator= (double value) ; | |
148 | bool operator== (long value) const; | |
149 | bool operator!= (long value) const; | |
150 | void operator= (long value) ; | |
151 | bool operator== (char value) const; | |
152 | bool operator!= (char value) const; | |
153 | void operator= (char value) ; | |
154 | #ifdef HAVE_BOOL | |
155 | bool operator== (bool value) const; | |
156 | bool operator!= (bool value) const; | |
157 | void operator= (bool value) ; | |
158 | #endif | |
159 | bool operator== (const wxString& value) const; | |
160 | bool operator!= (const wxString& value) const; | |
161 | void operator= (const wxString& value) ; | |
162 | void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool! | |
163 | bool operator== (const wxStringList& value) const; | |
164 | bool operator!= (const wxStringList& value) const; | |
165 | void operator= (const wxStringList& value) ; | |
166 | bool operator== (const wxList& value) const; | |
167 | bool operator!= (const wxList& value) const; | |
168 | void operator= (const wxList& value) ; | |
169 | bool operator== (void* value) const; | |
170 | bool operator!= (void* value) const; | |
171 | void operator= (void* value) ; | |
172 | ||
173 | // Treat a list variant as an array | |
174 | wxVariant operator[] (size_t idx) const; | |
175 | wxVariant& operator[] (size_t idx) ; | |
176 | ||
177 | // Implicit conversion to a wxString | |
178 | inline operator wxString () const { return MakeString(); } | |
179 | wxString MakeString() const; | |
180 | ||
181 | // Other implicit conversions | |
182 | inline operator double () const { return GetDouble(); } | |
183 | inline operator char () const { return GetChar(); } | |
184 | inline operator long () const { return GetLong(); } | |
185 | inline operator bool () const { return GetBool(); } | |
186 | inline operator void* () const { return GetVoidPtr(); } | |
187 | // No implicit conversion to wxObject, as that would really | |
188 | // confuse people between conversion to our contained data | |
189 | // and downcasting to see our base type. | |
190 | //TODO: Need to document | |
191 | #if wxUSE_DATETIME | |
192 | inline operator wxDateTime () const { return GetDateTime(); } | |
193 | #endif // wxUSE_DATETIME | |
194 | //TODO: End of Need to document | |
195 | ||
196 | // Accessors | |
197 | // Sets/gets name | |
198 | inline void SetName(const wxString& name) { m_name = name; } | |
199 | inline const wxString& GetName() const { return m_name; } | |
200 | ||
201 | // Tests whether there is data | |
202 | inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); } | |
203 | ||
204 | wxVariantData* GetData() const { return m_data; } | |
205 | void SetData(wxVariantData* data) ; | |
206 | ||
207 | // Returns a string representing the type of the variant, | |
208 | // e.g. "string", "bool", "stringlist", "list", "double", "long" | |
209 | wxString GetType() const; | |
210 | ||
211 | bool IsType(const wxString& type) const; | |
212 | bool IsValueKindOf(const wxClassInfo* type) const; | |
213 | ||
214 | // Return the number of elements in a list | |
215 | int GetCount() const; | |
216 | ||
217 | // Value accessors | |
218 | double GetReal() const ; | |
219 | inline double GetDouble() const { return GetReal(); }; | |
220 | long GetInteger() const ; | |
221 | inline long GetLong() const { return GetInteger(); }; | |
222 | char GetChar() const ; | |
223 | bool GetBool() const ; | |
224 | wxString GetString() const ; | |
225 | wxList& GetList() const ; | |
226 | wxStringList& GetStringList() const ; | |
227 | ||
228 | void* GetVoidPtr() const ; | |
229 | wxObject* GetWxObjectPtr() ; | |
230 | //TODO: Need to document | |
231 | #if wxUSE_DATETIME | |
232 | wxDateTime GetDateTime() const ; | |
233 | #endif // wxUSE_DATETIME | |
234 | wxArrayString GetArrayString() const; | |
235 | //TODO: End of Need to document | |
236 | ||
237 | // Operations | |
238 | // Make NULL (i.e. delete the data) | |
239 | void MakeNull(); | |
240 | ||
241 | // Make empty list | |
242 | void NullList(); | |
243 | ||
244 | // Append to list | |
245 | void Append(const wxVariant& value); | |
246 | ||
247 | // Insert at front of list | |
248 | void Insert(const wxVariant& value); | |
249 | ||
250 | // Returns TRUE if the variant is a member of the list | |
251 | bool Member(const wxVariant& value) const; | |
252 | ||
253 | // Deletes the nth element of the list | |
254 | bool Delete(int item); | |
255 | ||
256 | // Clear list | |
257 | void ClearList(); | |
258 | ||
259 | // Implementation | |
260 | public: | |
261 | // Type conversion | |
262 | bool Convert(long* value) const; | |
263 | bool Convert(bool* value) const; | |
264 | bool Convert(double* value) const; | |
265 | bool Convert(wxString* value) const; | |
266 | bool Convert(char* value) const; | |
267 | //TODO: Need to document | |
268 | #if wxUSE_DATETIME | |
269 | bool Convert(wxDateTime* value) const; | |
270 | #endif // wxUSE_DATETIME | |
271 | //TODO: End of Need to document | |
272 | ||
273 | // Attributes | |
274 | protected: | |
275 | wxVariantData* m_data; | |
276 | wxString m_name; | |
277 | }; | |
278 | ||
279 | //Since we want type safety wxVariant we need to fetch and dynamic_cast | |
280 | //in a seemingly safe way so the compiler can check, so we define | |
281 | //a dynamic_cast /wxDynamicCast analogue. | |
282 | ||
283 | #define wxGetVariantCast(var,classname) \ | |
284 | ((classname*)(var.IsValueKindOf(&classname::sm_class##classname) ?\ | |
285 | var.GetWxObjectPtr() : NULL)); | |
286 | ||
287 | extern wxVariant WXDLLEXPORT wxNullVariant; | |
288 | ||
289 | #endif | |
290 | // _WX_VARIANT_H_ |