]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
Fixed memory checking on Unix by removing inlines and apply patch
[wxWidgets.git] / include / wx / variant.h
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(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 wxVariant(const wxStringList& val, const wxString& name = wxEmptyString);
99 wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants
100 wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
101 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); //wxObject
102 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
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
113 wxVariant(const wxVariant& variant);
114 ~wxVariant();
115
116 // Generic operators
117 // Assignment
118 void operator= (const wxVariant& variant);
119
120 #if wxUSE_DATETIME
121 bool operator== (const wxDateTime& value) const;
122 bool operator!= (const wxDateTime& value) const;
123 void operator= (const wxDateTime& value) ;
124 #endif // wxUSE_DATETIME
125
126 bool operator== (const wxArrayString& value) const;
127 bool operator!= (const wxArrayString& value) const;
128 void operator= (const wxArrayString& value) ;
129 #if wxUSE_ODBC
130 void operator= (const DATE_STRUCT* value) ;
131 void operator= (const TIME_STRUCT* value) ;
132 void operator= (const TIMESTAMP_STRUCT* value) ;
133 #endif
134
135 // Assignment using data, e.g.
136 // myVariant = new wxStringVariantData("hello");
137 void operator= (wxVariantData* variantData);
138 bool operator== (const wxVariant& variant) const;
139 bool operator!= (const wxVariant& variant) const;
140
141 // Specific operators
142 bool operator== (double value) const;
143 bool operator!= (double value) const;
144 void operator= (double value) ;
145 bool operator== (long value) const;
146 bool operator!= (long value) const;
147 void operator= (long value) ;
148 bool operator== (char value) const;
149 bool operator!= (char value) const;
150 void operator= (char value) ;
151 #ifdef HAVE_BOOL
152 bool operator== (bool value) const;
153 bool operator!= (bool value) const;
154 void operator= (bool value) ;
155 #endif
156 bool operator== (const wxString& value) const;
157 bool operator!= (const wxString& value) const;
158 void operator= (const wxString& value) ;
159 void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
160 bool operator== (const wxStringList& value) const;
161 bool operator!= (const wxStringList& value) const;
162 void operator= (const wxStringList& value) ;
163 bool operator== (const wxList& value) const;
164 bool operator!= (const wxList& value) const;
165 void operator= (const wxList& value) ;
166 bool operator== (void* value) const;
167 bool operator!= (void* value) const;
168 void operator= (void* value) ;
169
170 // Treat a list variant as an array
171 wxVariant operator[] (size_t idx) const;
172 wxVariant& operator[] (size_t idx) ;
173
174 // Implicit conversion to a wxString
175 inline operator wxString () const { return MakeString(); }
176 wxString MakeString() const;
177
178 // Other implicit conversions
179 inline operator double () const { return GetDouble(); }
180 inline operator char () const { return GetChar(); }
181 inline operator long () const { return GetLong(); }
182 inline operator bool () const { return GetBool(); }
183 inline operator void* () const { return GetVoidPtr(); }
184 // No implicit conversion to wxObject, as that would really
185 // confuse people between conversion to our contained data
186 // and downcasting to see our base type.
187 #if wxUSE_DATETIME
188 inline operator wxDateTime () const { return GetDateTime(); }
189 #endif // wxUSE_DATETIME
190
191 // Accessors
192 // Sets/gets name
193 inline void SetName(const wxString& name) { m_name = name; }
194 inline const wxString& GetName() const { return m_name; }
195
196 // Tests whether there is data
197 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
198
199 wxVariantData* GetData() const { return m_data; }
200 void SetData(wxVariantData* data) ;
201
202 // Returns a string representing the type of the variant,
203 // e.g. "string", "bool", "stringlist", "list", "double", "long"
204 wxString GetType() const;
205
206 bool IsType(const wxString& type) const;
207 bool IsValueKindOf(const wxClassInfo* type) const;
208
209 // Return the number of elements in a list
210 int GetCount() const;
211
212 // Value accessors
213 double GetReal() const ;
214 inline double GetDouble() const { return GetReal(); };
215 long GetInteger() const ;
216 inline long GetLong() const { return GetInteger(); };
217 char GetChar() const ;
218 bool GetBool() const ;
219 wxString GetString() const ;
220 wxList& GetList() const ;
221 wxStringList& GetStringList() const ;
222
223 void* GetVoidPtr() const ;
224 wxObject* GetWxObjectPtr() ;
225 #if wxUSE_DATETIME
226 wxDateTime GetDateTime() const ;
227 #endif // wxUSE_DATETIME
228 wxArrayString GetArrayString() const;
229
230 // Operations
231 // Make NULL (i.e. delete the data)
232 void MakeNull();
233
234 // Make empty list
235 void NullList();
236
237 // Append to list
238 void Append(const wxVariant& value);
239
240 // Insert at front of list
241 void Insert(const wxVariant& value);
242
243 // Returns true if the variant is a member of the list
244 bool Member(const wxVariant& value) const;
245
246 // Deletes the nth element of the list
247 bool Delete(int item);
248
249 // Clear list
250 void ClearList();
251
252 // Implementation
253 public:
254 // Type conversion
255 bool Convert(long* value) const;
256 bool Convert(bool* value) const;
257 bool Convert(double* value) const;
258 bool Convert(wxString* value) const;
259 bool Convert(char* value) const;
260 #if wxUSE_DATETIME
261 bool Convert(wxDateTime* value) const;
262 #endif // wxUSE_DATETIME
263
264 // Attributes
265 protected:
266 wxVariantData* m_data;
267 wxString m_name;
268 };
269
270 //Since we want type safety wxVariant we need to fetch and dynamic_cast
271 //in a seemingly safe way so the compiler can check, so we define
272 //a dynamic_cast /wxDynamicCast analogue.
273
274 #define wxGetVariantCast(var,classname) \
275 ((classname*)(var.IsValueKindOf(&classname::ms_classInfo) ?\
276 var.GetWxObjectPtr() : NULL));
277
278 extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
279
280 #endif
281 // _WX_VARIANT_H_