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