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.1 2006/12/02 15:58:27 scara Exp $
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_TIMEDATE
25     #include "wx/time.h"
26     #include "wx/date.h"
27 #endif // time/date
28 
29 #include "wx/datetime.h"
30 
31 #if wxUSE_ODBC
32     #include "wx/db.h"  // will #include sqltypes.h
33 #endif //ODBC
34 
35 #include "wx/ioswrap.h"
36 
37 /*
38  * wxVariantData stores the actual data in a wxVariant object,
39  * to allow it to store any type of data.
40  * Derive from this to provide custom data handling.
41  *
42  * TODO: in order to replace wxPropertyValue, we would need
43  * to consider adding constructors that take pointers to C++ variables,
44  * or removing that functionality from the wxProperty library.
45  * Essentially wxPropertyValue takes on some of the wxValidator functionality
46  * by storing pointers and not just actual values, allowing update of C++ data
47  * to be handled automatically. Perhaps there's another way of doing this without
48  * overloading wxVariant with unnecessary functionality.
49  */
50 
51 class WXDLLEXPORT wxVariantData: public wxObject
52 {
DECLARE_ABSTRACT_CLASS(wxVariantData)53 DECLARE_ABSTRACT_CLASS(wxVariantData)
54 public:
55 
56 // Construction & destruction
57     wxVariantData() {};
58 
59 // Override these to provide common functionality
60     // Copy to data
61     virtual void Copy(wxVariantData& data) = 0;
62     virtual bool Eq(wxVariantData& data) const = 0;
63 #if wxUSE_STD_IOSTREAM
64     virtual bool Write(wxSTD ostream& str) const = 0;
65 #endif
66     virtual bool Write(wxString& str) const = 0;
67 #if wxUSE_STD_IOSTREAM
68     virtual bool Read(wxSTD istream& str) = 0;
69 #endif
70     virtual bool Read(wxString& str) = 0;
71     // What type is it? Return a string name.
72     virtual wxString GetType() const = 0;
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 WXDLLEXPORT 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 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
101 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
102     wxVariant(const wxTime& val, const wxString& name = wxEmptyString); // Time
103     wxVariant(const wxDate& val, const wxString& name = wxEmptyString); // Date
104 #endif
105     wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
106     wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
107 //TODO: Need to document
108     wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date
109     wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); // String array
110 #if wxUSE_ODBC
111     wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime
112     wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime
113     wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime
114 #endif
115 //TODO: End of Need to document
116 
117     wxVariant(const wxVariant& variant);
118     ~wxVariant();
119 
120 // Generic operators
121     // Assignment
122     void operator= (const wxVariant& variant);
123 
124 //TODO: Need to document
125     bool operator== (const wxDateTime& value) const;
126     bool operator!= (const wxDateTime& value) const;
127     void operator= (const wxDateTime& value) ;
128 
129     bool operator== (const wxArrayString& value) const;
130     bool operator!= (const wxArrayString& value) const;
131     void operator= (const wxArrayString& value) ;
132 #if wxUSE_ODBC
133     void operator= (const DATE_STRUCT* value) ;
134     void operator= (const TIME_STRUCT* value) ;
135     void operator= (const TIMESTAMP_STRUCT* value) ;
136 #endif
137 //TODO: End of Need to document
138 
139     // Assignment using data, e.g.
140     // myVariant = new wxStringVariantData("hello");
141     void operator= (wxVariantData* variantData);
142     bool operator== (const wxVariant& variant) const;
143     bool operator!= (const wxVariant& variant) const;
144 
145 // Specific operators
146     bool operator== (double value) const;
147     bool operator!= (double value) const;
148     void operator= (double value) ;
149     bool operator== (long value) const;
150     bool operator!= (long value) const;
151     void operator= (long value) ;
152     bool operator== (char value) const;
153     bool operator!= (char value) const;
154     void operator= (char value) ;
155 #ifdef HAVE_BOOL
156     bool operator== (bool value) const;
157     bool operator!= (bool value) const;
158     void operator= (bool value) ;
159 #endif
160     bool operator== (const wxString& value) const;
161     bool operator!= (const wxString& value) const;
162     void operator= (const wxString& value) ;
163     void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
164     bool operator== (const wxStringList& value) const;
165     bool operator!= (const wxStringList& value) const;
166     void operator= (const wxStringList& value) ;
167     bool operator== (const wxList& value) const;
168     bool operator!= (const wxList& value) const;
169     void operator= (const wxList& value) ;
170 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
171 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
172     bool operator== (const wxTime& value) const;
173     bool operator!= (const wxTime& value) const;
174     void operator= (const wxTime& value) ;
175     bool operator== (const wxDate& value) const;
176     bool operator!= (const wxDate& value) const;
177     void operator= (const wxDate& value) ;
178 #endif
179     bool operator== (void* value) const;
180     bool operator!= (void* value) const;
181     void operator= (void* value) ;
182 
183     // Treat a list variant as an array
184     wxVariant operator[] (size_t idx) const;
185     wxVariant& operator[] (size_t idx) ;
186 
187     // Implicit conversion to a wxString
wxString()188     inline operator wxString () const {  return MakeString(); }
189     wxString MakeString() const;
190 
191     // Other implicit conversions
192     inline operator double () const {  return GetDouble(); }
193     inline operator char () const {  return GetChar(); }
194     inline operator long () const {  return GetLong(); }
195     inline operator bool () const {  return GetBool(); }
196 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
197 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
wxTime()198     inline operator wxTime () const {  return GetTime(); }
wxDate()199     inline operator wxDate () const {  return GetDate(); }
200 #endif
201     inline operator void* () const {  return GetVoidPtr(); }
202 //TODO: Need to document
wxDateTime()203     inline operator wxDateTime () const { return GetDateTime(); }
204 //TODO: End of Need to document
205 
206 // Accessors
207     // Sets/gets name
SetName(const wxString & name)208     inline void SetName(const wxString& name) { m_name = name; }
GetName()209     inline const wxString& GetName() const { return m_name; }
210 
211     // Tests whether there is data
IsNull()212     inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
213 
GetData()214     wxVariantData* GetData() const { return m_data; }
215     void SetData(wxVariantData* data) ;
216 
217     // Returns a string representing the type of the variant,
218     // e.g. "string", "bool", "stringlist", "list", "double", "long"
219     wxString GetType() const;
220 
221     bool IsType(const wxString& type) const;
222 
223     // Return the number of elements in a list
224     int GetCount() const;
225 
226 // Value accessors
227     double GetReal() const ;
GetDouble()228     inline double GetDouble() const { return GetReal(); };
229     long GetInteger() const ;
GetLong()230     inline long GetLong() const { return GetInteger(); };
231     char GetChar() const ;
232     bool GetBool() const ;
233     wxString GetString() const ;
234     wxList& GetList() const ;
235     wxStringList& GetStringList() const ;
236 
237 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
238 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
239     wxTime GetTime() const ;
240     wxDate GetDate() const ;
241 #endif
242     void* GetVoidPtr() const ;
243 //TODO: Need to document
244     wxDateTime GetDateTime() const ;
245     wxArrayString GetArrayString() const;
246 //TODO: End of Need to document
247 
248 // Operations
249     // Make NULL (i.e. delete the data)
250     void MakeNull();
251 
252     // Make empty list
253     void NullList();
254 
255     // Append to list
256     void Append(const wxVariant& value);
257 
258     // Insert at front of list
259     void Insert(const wxVariant& value);
260 
261     // Returns TRUE if the variant is a member of the list
262     bool Member(const wxVariant& value) const;
263 
264     // Deletes the nth element of the list
265     bool Delete(int item);
266 
267     // Clear list
268     void ClearList();
269 
270 // Implementation
271 public:
272 // Type conversion
273     bool Convert(long* value) const;
274     bool Convert(bool* value) const;
275     bool Convert(double* value) const;
276     bool Convert(wxString* value) const;
277     bool Convert(char* value) const;
278 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
279 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
280     bool Convert(wxTime* value) const;
281     bool Convert(wxDate* value) const;
282 #endif
283 //TODO: Need to document
284     bool Convert(wxDateTime* value) const;
285 //TODO: End of Need to document
286 
287 // Attributes
288 protected:
289     wxVariantData*  m_data;
290     wxString        m_name;
291 };
292 
293 extern wxVariant WXDLLEXPORT wxNullVariant;
294 
295 #endif
296     // _WX_VARIANT_H_
297