1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxVariant class, container for any type
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_VARIANT_H_
13 #define _WX_VARIANT_H_
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "variant.h"
20 #include "wx/object.h"
21 #include "wx/string.h"
30 #include "wx/datetime.h"
31 #endif // wxUSE_DATETIME
34 #include "wx/db.h" // will #include sqltypes.h
37 #include "wx/iosfwrap.h"
40 * wxVariantData stores the actual data in a wxVariant object,
41 * to allow it to store any type of data.
42 * Derive from this to provide custom data handling.
44 * TODO: in order to replace wxPropertyValue, we would need
45 * to consider adding constructors that take pointers to C++ variables,
46 * or removing that functionality from the wxProperty library.
47 * Essentially wxPropertyValue takes on some of the wxValidator functionality
48 * by storing pointers and not just actual values, allowing update of C++ data
49 * to be handled automatically. Perhaps there's another way of doing this without
50 * overloading wxVariant with unnecessary functionality.
53 class WXDLLEXPORT wxVariantData
: public wxObject
55 DECLARE_ABSTRACT_CLASS(wxVariantData
)
58 // Construction & destruction
61 // Override these to provide common functionality
63 virtual void Copy(wxVariantData
& data
) = 0;
64 virtual bool Eq(wxVariantData
& data
) const = 0;
65 #if wxUSE_STD_IOSTREAM
66 virtual bool Write(wxSTD ostream
& str
) const = 0;
68 virtual bool Write(wxString
& str
) const = 0;
69 #if wxUSE_STD_IOSTREAM
70 virtual bool Read(wxSTD istream
& str
) = 0;
72 virtual bool Read(wxString
& str
) = 0;
73 // What type is it? Return a string name.
74 virtual wxString
GetType() const = 0;
78 * wxVariant can store any kind of data, but has some basic types
80 * NOTE: this eventually should have a reference-counting implementation.
81 * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating
82 * this class too much.
85 class WXDLLEXPORT wxVariant
: public wxObject
87 DECLARE_DYNAMIC_CLASS(wxVariant
)
90 // Construction & destruction
92 wxVariant(double val
, const wxString
& name
= wxEmptyString
);
93 wxVariant(long val
, const wxString
& name
= wxEmptyString
);
95 wxVariant(bool val
, const wxString
& name
= wxEmptyString
);
97 wxVariant(char val
, const wxString
& name
= wxEmptyString
);
98 wxVariant(const wxString
& val
, const wxString
& name
= wxEmptyString
);
99 wxVariant(const wxChar
* val
, const wxString
& name
= wxEmptyString
); // Necessary or VC++ assumes bool!
100 wxVariant(const wxStringList
& val
, const wxString
& name
= wxEmptyString
);
101 wxVariant(const wxList
& val
, const wxString
& name
= wxEmptyString
); // List of variants
102 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
103 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
104 wxVariant(const wxTime
& val
, const wxString
& name
= wxEmptyString
); // Time
105 wxVariant(const wxDate
& val
, const wxString
& name
= wxEmptyString
); // Date
107 wxVariant(void* ptr
, const wxString
& name
= wxEmptyString
); // void* (general purpose)
108 wxVariant(wxVariantData
* data
, const wxString
& name
= wxEmptyString
); // User-defined data
109 //TODO: Need to document
111 wxVariant(const wxDateTime
& val
, const wxString
& name
= wxEmptyString
); // Date
112 #endif // wxUSE_DATETIME
113 wxVariant(const wxArrayString
& val
, const wxString
& name
= wxEmptyString
); // String array
115 wxVariant(const DATE_STRUCT
* valptr
, const wxString
& name
= wxEmptyString
); // DateTime
116 wxVariant(const TIME_STRUCT
* valptr
, const wxString
& name
= wxEmptyString
); // DateTime
117 wxVariant(const TIMESTAMP_STRUCT
* valptr
, const wxString
& name
= wxEmptyString
); // DateTime
119 //TODO: End of Need to document
121 wxVariant(const wxVariant
& variant
);
126 void operator= (const wxVariant
& variant
);
128 //TODO: Need to document
130 bool operator== (const wxDateTime
& value
) const;
131 bool operator!= (const wxDateTime
& value
) const;
132 void operator= (const wxDateTime
& value
) ;
133 #endif // wxUSE_DATETIME
135 bool operator== (const wxArrayString
& value
) const;
136 bool operator!= (const wxArrayString
& value
) const;
137 void operator= (const wxArrayString
& value
) ;
139 void operator= (const DATE_STRUCT
* value
) ;
140 void operator= (const TIME_STRUCT
* value
) ;
141 void operator= (const TIMESTAMP_STRUCT
* value
) ;
143 //TODO: End of Need to document
145 // Assignment using data, e.g.
146 // myVariant = new wxStringVariantData("hello");
147 void operator= (wxVariantData
* variantData
);
148 bool operator== (const wxVariant
& variant
) const;
149 bool operator!= (const wxVariant
& variant
) const;
151 // Specific operators
152 bool operator== (double value
) const;
153 bool operator!= (double value
) const;
154 void operator= (double value
) ;
155 bool operator== (long value
) const;
156 bool operator!= (long value
) const;
157 void operator= (long value
) ;
158 bool operator== (char value
) const;
159 bool operator!= (char value
) const;
160 void operator= (char value
) ;
162 bool operator== (bool value
) const;
163 bool operator!= (bool value
) const;
164 void operator= (bool value
) ;
166 bool operator== (const wxString
& value
) const;
167 bool operator!= (const wxString
& value
) const;
168 void operator= (const wxString
& value
) ;
169 void operator= (const wxChar
* value
) ; // Necessary or VC++ assumes bool!
170 bool operator== (const wxStringList
& value
) const;
171 bool operator!= (const wxStringList
& value
) const;
172 void operator= (const wxStringList
& value
) ;
173 bool operator== (const wxList
& value
) const;
174 bool operator!= (const wxList
& value
) const;
175 void operator= (const wxList
& value
) ;
176 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
177 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
178 bool operator== (const wxTime
& value
) const;
179 bool operator!= (const wxTime
& value
) const;
180 void operator= (const wxTime
& value
) ;
181 bool operator== (const wxDate
& value
) const;
182 bool operator!= (const wxDate
& value
) const;
183 void operator= (const wxDate
& value
) ;
185 bool operator== (void* value
) const;
186 bool operator!= (void* value
) const;
187 void operator= (void* value
) ;
189 // Treat a list variant as an array
190 wxVariant
operator[] (size_t idx
) const;
191 wxVariant
& operator[] (size_t idx
) ;
193 // Implicit conversion to a wxString
194 inline operator wxString () const { return MakeString(); }
195 wxString
MakeString() const;
197 // Other implicit conversions
198 inline operator double () const { return GetDouble(); }
199 inline operator char () const { return GetChar(); }
200 inline operator long () const { return GetLong(); }
201 inline operator bool () const { return GetBool(); }
202 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
203 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
204 inline operator wxTime () const { return GetTime(); }
205 inline operator wxDate () const { return GetDate(); }
207 inline operator void* () const { return GetVoidPtr(); }
208 //TODO: Need to document
210 inline operator wxDateTime () const { return GetDateTime(); }
211 #endif // wxUSE_DATETIME
212 //TODO: End of Need to document
216 inline void SetName(const wxString
& name
) { m_name
= name
; }
217 inline const wxString
& GetName() const { return m_name
; }
219 // Tests whether there is data
220 inline bool IsNull() const { return (m_data
== (wxVariantData
*) NULL
); }
222 wxVariantData
* GetData() const { return m_data
; }
223 void SetData(wxVariantData
* data
) ;
225 // Returns a string representing the type of the variant,
226 // e.g. "string", "bool", "stringlist", "list", "double", "long"
227 wxString
GetType() const;
229 bool IsType(const wxString
& type
) const;
231 // Return the number of elements in a list
232 int GetCount() const;
235 double GetReal() const ;
236 inline double GetDouble() const { return GetReal(); };
237 long GetInteger() const ;
238 inline long GetLong() const { return GetInteger(); };
239 char GetChar() const ;
240 bool GetBool() const ;
241 wxString
GetString() const ;
242 wxList
& GetList() const ;
243 wxStringList
& GetStringList() const ;
245 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
246 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
247 wxTime
GetTime() const ;
248 wxDate
GetDate() const ;
250 void* GetVoidPtr() const ;
251 //TODO: Need to document
253 wxDateTime
GetDateTime() const ;
254 #endif // wxUSE_DATETIME
255 wxArrayString
GetArrayString() const;
256 //TODO: End of Need to document
259 // Make NULL (i.e. delete the data)
266 void Append(const wxVariant
& value
);
268 // Insert at front of list
269 void Insert(const wxVariant
& value
);
271 // Returns TRUE if the variant is a member of the list
272 bool Member(const wxVariant
& value
) const;
274 // Deletes the nth element of the list
275 bool Delete(int item
);
283 bool Convert(long* value
) const;
284 bool Convert(bool* value
) const;
285 bool Convert(double* value
) const;
286 bool Convert(wxString
* value
) const;
287 bool Convert(char* value
) const;
288 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
289 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
290 bool Convert(wxTime
* value
) const;
291 bool Convert(wxDate
* value
) const;
293 //TODO: Need to document
295 bool Convert(wxDateTime
* value
) const;
296 #endif // wxUSE_DATETIME
297 //TODO: End of Need to document
301 wxVariantData
* m_data
;
305 extern wxVariant WXDLLEXPORT wxNullVariant
;