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: variant.h,v 1.33 2005/05/04 18:52:05 JS Exp $
|
---|
8 | // Copyright: (c) Julian Smart
|
---|
9 | // Licence: wxWindows licence
|
---|
10 | /////////////////////////////////////////////////////////////////////////////
|
---|
11 |
|
---|
12 | #ifndef _WX_VARIANT_H_
|
---|
13 | #define _WX_VARIANT_H_
|
---|
14 |
|
---|
15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
|
---|
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/arrstr.h"
|
---|
23 | #include "wx/list.h"
|
---|
24 |
|
---|
25 | #if wxUSE_DATETIME
|
---|
26 | #include "wx/datetime.h"
|
---|
27 | #endif // wxUSE_DATETIME
|
---|
28 |
|
---|
29 | #if wxUSE_ODBC
|
---|
30 | #include "wx/db.h" // will #include sqltypes.h
|
---|
31 | #endif //ODBC
|
---|
32 |
|
---|
33 | #include "wx/iosfwrap.h"
|
---|
34 |
|
---|
35 | /*
|
---|
36 | * wxVariantData stores the actual data in a wxVariant object,
|
---|
37 | * to allow it to store any type of data.
|
---|
38 | * Derive from this to provide custom data handling.
|
---|
39 | *
|
---|
40 | * TODO: in order to replace wxPropertyValue, we would need
|
---|
41 | * to consider adding constructors that take pointers to C++ variables,
|
---|
42 | * or removing that functionality from the wxProperty library.
|
---|
43 | * Essentially wxPropertyValue takes on some of the wxValidator functionality
|
---|
44 | * by storing pointers and not just actual values, allowing update of C++ data
|
---|
45 | * to be handled automatically. Perhaps there's another way of doing this without
|
---|
46 | * overloading wxVariant with unnecessary functionality.
|
---|
47 | */
|
---|
48 |
|
---|
49 | class WXDLLIMPEXP_BASE wxVariantData: public wxObject
|
---|
50 | {
|
---|
51 | DECLARE_ABSTRACT_CLASS(wxVariantData)
|
---|
52 | public:
|
---|
53 |
|
---|
54 | // Construction & destruction
|
---|
55 | wxVariantData() {}
|
---|
56 |
|
---|
57 | // Override these to provide common functionality
|
---|
58 | // Copy to data
|
---|
59 | virtual void Copy(wxVariantData& data) = 0;
|
---|
60 | virtual bool Eq(wxVariantData& data) const = 0;
|
---|
61 | #if wxUSE_STD_IOSTREAM
|
---|
62 | virtual bool Write(wxSTD ostream& str) const = 0;
|
---|
63 | #endif
|
---|
64 | virtual bool Write(wxString& str) const = 0;
|
---|
65 | #if wxUSE_STD_IOSTREAM
|
---|
66 | virtual bool Read(wxSTD istream& str) = 0;
|
---|
67 | #endif
|
---|
68 | virtual bool Read(wxString& str) = 0;
|
---|
69 | // What type is it? Return a string name.
|
---|
70 | virtual wxString GetType() const = 0;
|
---|
71 | // If it based on wxObject return the ClassInfo.
|
---|
72 | virtual wxClassInfo* GetValueClassInfo() { return NULL; }
|
---|
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 WXDLLIMPEXP_BASE wxVariant: public wxObject
|
---|
84 | {
|
---|
85 | DECLARE_DYNAMIC_CLASS(wxVariant)
|
---|
86 | public:
|
---|
87 |
|
---|
88 | // Construction & destruction
|
---|
89 | wxVariant();
|
---|
90 | wxVariant(double val, const wxString& name = wxEmptyString);
|
---|
91 | wxVariant(long val, const wxString& name = wxEmptyString);
|
---|
92 | #ifdef HAVE_BOOL
|
---|
93 | wxVariant(bool val, const wxString& name = wxEmptyString);
|
---|
94 | #endif
|
---|
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 | #if WXWIN_COMPATIBILITY_2_4
|
---|
99 | wxDEPRECATED( wxVariant(const wxStringList& val, const wxString& name = wxEmptyString) );
|
---|
100 | #endif
|
---|
101 | wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants
|
---|
102 | wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
|
---|
103 | wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); //wxObject
|
---|
104 | wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
|
---|
105 | #if wxUSE_DATETIME
|
---|
106 | wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date
|
---|
107 | #endif // wxUSE_DATETIME
|
---|
108 | wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); // String array
|
---|
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 |
|
---|
115 | wxVariant(const wxVariant& variant);
|
---|
116 | ~wxVariant();
|
---|
117 |
|
---|
118 | // Generic operators
|
---|
119 | // Assignment
|
---|
120 | void operator= (const wxVariant& variant);
|
---|
121 |
|
---|
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 |
|
---|
137 | // Assignment using data, e.g.
|
---|
138 | // myVariant = new wxStringVariantData("hello");
|
---|
139 | void operator= (wxVariantData* variantData);
|
---|
140 | bool operator== (const wxVariant& variant) const;
|
---|
141 | bool operator!= (const wxVariant& variant) const;
|
---|
142 |
|
---|
143 | // Specific operators
|
---|
144 | bool operator== (double value) const;
|
---|
145 | bool operator!= (double value) const;
|
---|
146 | void operator= (double value) ;
|
---|
147 | bool operator== (long value) const;
|
---|
148 | bool operator!= (long value) const;
|
---|
149 | void operator= (long value) ;
|
---|
150 | bool operator== (char value) const;
|
---|
151 | bool operator!= (char value) const;
|
---|
152 | void operator= (char value) ;
|
---|
153 | #ifdef HAVE_BOOL
|
---|
154 | bool operator== (bool value) const;
|
---|
155 | bool operator!= (bool value) const;
|
---|
156 | void operator= (bool value) ;
|
---|
157 | #endif
|
---|
158 | bool operator== (const wxString& value) const;
|
---|
159 | bool operator!= (const wxString& value) const;
|
---|
160 | void operator= (const wxString& value) ;
|
---|
161 | void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
|
---|
162 | #if WXWIN_COMPATIBILITY_2_4
|
---|
163 | wxDEPRECATED( bool operator== (const wxStringList& value) const );
|
---|
164 | wxDEPRECATED( bool operator!= (const wxStringList& value) const );
|
---|
165 | wxDEPRECATED( void operator= (const wxStringList& value) );
|
---|
166 | #endif
|
---|
167 | bool operator== (const wxList& value) const;
|
---|
168 | bool operator!= (const wxList& value) const;
|
---|
169 | void operator= (const wxList& value) ;
|
---|
170 | bool operator== (void* value) const;
|
---|
171 | bool operator!= (void* value) const;
|
---|
172 | void operator= (void* value) ;
|
---|
173 |
|
---|
174 | // Treat a list variant as an array
|
---|
175 | wxVariant operator[] (size_t idx) const;
|
---|
176 | wxVariant& operator[] (size_t idx) ;
|
---|
177 |
|
---|
178 | // Implicit conversion to a wxString
|
---|
179 | inline operator wxString () const { return MakeString(); }
|
---|
180 | wxString MakeString() const;
|
---|
181 |
|
---|
182 | // Other implicit conversions
|
---|
183 | inline operator double () const { return GetDouble(); }
|
---|
184 | inline operator char () const { return GetChar(); }
|
---|
185 | inline operator long () const { return GetLong(); }
|
---|
186 | inline operator bool () const { return GetBool(); }
|
---|
187 | inline operator void* () const { return GetVoidPtr(); }
|
---|
188 | // No implicit conversion to wxObject, as that would really
|
---|
189 | // confuse people between conversion to our contained data
|
---|
190 | // and downcasting to see our base type.
|
---|
191 | #if wxUSE_DATETIME
|
---|
192 | inline operator wxDateTime () const { return GetDateTime(); }
|
---|
193 | #endif // wxUSE_DATETIME
|
---|
194 |
|
---|
195 | // Accessors
|
---|
196 | // Sets/gets name
|
---|
197 | inline void SetName(const wxString& name) { m_name = name; }
|
---|
198 | inline const wxString& GetName() const { return m_name; }
|
---|
199 |
|
---|
200 | // Tests whether there is data
|
---|
201 | inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
|
---|
202 |
|
---|
203 | wxVariantData* GetData() const { return m_data; }
|
---|
204 | void SetData(wxVariantData* data) ;
|
---|
205 |
|
---|
206 | // Returns a string representing the type of the variant,
|
---|
207 | // e.g. "string", "bool", "stringlist", "list", "double", "long"
|
---|
208 | wxString GetType() const;
|
---|
209 |
|
---|
210 | bool IsType(const wxString& type) const;
|
---|
211 | bool IsValueKindOf(const wxClassInfo* type) const;
|
---|
212 |
|
---|
213 | // Return the number of elements in a list
|
---|
214 | int GetCount() const;
|
---|
215 |
|
---|
216 | // Value accessors
|
---|
217 | double GetReal() const ;
|
---|
218 | inline double GetDouble() const { return GetReal(); };
|
---|
219 | long GetInteger() const ;
|
---|
220 | inline long GetLong() const { return GetInteger(); };
|
---|
221 | char GetChar() const ;
|
---|
222 | bool GetBool() const ;
|
---|
223 | wxString GetString() const ;
|
---|
224 | wxList& GetList() const ;
|
---|
225 | #if WXWIN_COMPATIBILITY_2_4
|
---|
226 | wxDEPRECATED( wxStringList& GetStringList() const );
|
---|
227 | #endif
|
---|
228 | void* GetVoidPtr() const ;
|
---|
229 | wxObject* GetWxObjectPtr() ;
|
---|
230 | #if wxUSE_DATETIME
|
---|
231 | wxDateTime GetDateTime() const ;
|
---|
232 | #endif // wxUSE_DATETIME
|
---|
233 | wxArrayString GetArrayString() const;
|
---|
234 |
|
---|
235 | // Operations
|
---|
236 | // Make NULL (i.e. delete the data)
|
---|
237 | void MakeNull();
|
---|
238 |
|
---|
239 | // Make empty list
|
---|
240 | void NullList();
|
---|
241 |
|
---|
242 | // Append to list
|
---|
243 | void Append(const wxVariant& value);
|
---|
244 |
|
---|
245 | // Insert at front of list
|
---|
246 | void Insert(const wxVariant& value);
|
---|
247 |
|
---|
248 | // Returns true if the variant is a member of the list
|
---|
249 | bool Member(const wxVariant& value) const;
|
---|
250 |
|
---|
251 | // Deletes the nth element of the list
|
---|
252 | bool Delete(int item);
|
---|
253 |
|
---|
254 | // Clear list
|
---|
255 | void ClearList();
|
---|
256 |
|
---|
257 | // Implementation
|
---|
258 | public:
|
---|
259 | // Type conversion
|
---|
260 | bool Convert(long* value) const;
|
---|
261 | bool Convert(bool* value) const;
|
---|
262 | bool Convert(double* value) const;
|
---|
263 | bool Convert(wxString* value) const;
|
---|
264 | bool Convert(char* value) const;
|
---|
265 | #if wxUSE_DATETIME
|
---|
266 | bool Convert(wxDateTime* value) const;
|
---|
267 | #endif // wxUSE_DATETIME
|
---|
268 |
|
---|
269 | // Attributes
|
---|
270 | protected:
|
---|
271 | wxVariantData* m_data;
|
---|
272 | wxString m_name;
|
---|
273 | };
|
---|
274 |
|
---|
275 | //Since we want type safety wxVariant we need to fetch and dynamic_cast
|
---|
276 | //in a seemingly safe way so the compiler can check, so we define
|
---|
277 | //a dynamic_cast /wxDynamicCast analogue.
|
---|
278 |
|
---|
279 | #define wxGetVariantCast(var,classname) \
|
---|
280 | ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
|
---|
281 | var.GetWxObjectPtr() : NULL));
|
---|
282 |
|
---|
283 | extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
|
---|
284 |
|
---|
285 | #endif
|
---|
286 | // _WX_VARIANT_H_
|
---|