]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
1. wxShell fixes: now really uses shell (it wasn't different from wxExecute!)
[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 #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 #if wxUSE_STD_IOSTREAM
58 virtual bool Write(ostream& str) const = 0;
59 #endif
60 virtual bool Write(wxString& str) const = 0;
61 #if wxUSE_STD_IOSTREAM
62 virtual bool Read(istream& str) = 0;
63 #endif
64 virtual bool Read(wxString& str) = 0;
65 // What type is it? Return a string name.
66 virtual wxString GetType() const = 0;
67 };
68
69 /*
70 * wxVariant can store any kind of data, but has some basic types
71 * built in.
72 * NOTE: this eventually should have a reference-counting implementation.
73 * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating
74 * this class too much.
75 */
76
77 class WXDLLEXPORT wxVariant: public wxObject
78 {
79 DECLARE_DYNAMIC_CLASS(wxVariant)
80 public:
81
82 // Construction & destruction
83 wxVariant();
84 wxVariant(double val, const wxString& name = wxEmptyString);
85 wxVariant(long val, const wxString& name = wxEmptyString);
86 #ifdef HAVE_BOOL
87 wxVariant(bool val, const wxString& name = wxEmptyString);
88 #endif
89 wxVariant(char val, const wxString& name = wxEmptyString);
90 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
91 wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool!
92 wxVariant(const wxStringList& val, const wxString& name = wxEmptyString);
93 wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants
94 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
95 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
96 wxVariant(const wxTime& val, const wxString& name = wxEmptyString); // Time
97 wxVariant(const wxDate& val, const wxString& name = wxEmptyString); // Date
98 #endif
99 wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
100 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
101 wxVariant(const wxVariant& variant);
102 ~wxVariant();
103
104 // Generic operators
105 // Assignment
106 void operator= (const wxVariant& variant);
107
108 // Assignment using data, e.g.
109 // myVariant = new wxStringVariantData("hello");
110 void operator= (wxVariantData* variantData);
111 bool operator== (const wxVariant& variant) const;
112 bool operator!= (const wxVariant& variant) const;
113
114 // Specific operators
115 bool operator== (double value) const;
116 bool operator!= (double value) const;
117 void operator= (double value) ;
118 bool operator== (long value) const;
119 bool operator!= (long value) const;
120 void operator= (long value) ;
121 bool operator== (char value) const;
122 bool operator!= (char value) const;
123 void operator= (char value) ;
124 #ifdef HAVE_BOOL
125 bool operator== (bool value) const;
126 bool operator!= (bool value) const;
127 void operator= (bool value) ;
128 #endif
129 bool operator== (const wxString& value) const;
130 bool operator!= (const wxString& value) const;
131 void operator= (const wxString& value) ;
132 void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
133 bool operator== (const wxStringList& value) const;
134 bool operator!= (const wxStringList& value) const;
135 void operator= (const wxStringList& value) ;
136 bool operator== (const wxList& value) const;
137 bool operator!= (const wxList& value) const;
138 void operator= (const wxList& value) ;
139 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
140 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
141 bool operator== (const wxTime& value) const;
142 bool operator!= (const wxTime& value) const;
143 void operator= (const wxTime& value) ;
144 bool operator== (const wxDate& value) const;
145 bool operator!= (const wxDate& value) const;
146 void operator= (const wxDate& value) ;
147 #endif
148 bool operator== (void* value) const;
149 bool operator!= (void* value) const;
150 void operator= (void* value) ;
151
152 // Treat a list variant as an array
153 wxVariant operator[] (size_t idx) const;
154 wxVariant& operator[] (size_t idx) ;
155
156 // Implicit conversion to a wxString
157 inline operator wxString () const { return MakeString(); }
158 wxString MakeString() const;
159
160 // Other implicit conversions
161 inline operator double () const { return GetDouble(); }
162 inline operator char () const { return GetChar(); }
163 inline operator long () const { return GetLong(); }
164 inline operator bool () const { return GetBool(); }
165 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
166 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
167 inline operator wxTime () const { return GetTime(); }
168 inline operator wxDate () const { return GetDate(); }
169 #endif
170 inline operator void* () const { return GetVoidPtr(); }
171
172 // Accessors
173 // Sets/gets name
174 inline void SetName(const wxString& name) { m_name = name; }
175 inline const wxString& GetName() const { return m_name; }
176
177 // Tests whether there is data
178 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
179
180 wxVariantData* GetData() const { return m_data; }
181 void SetData(wxVariantData* data) ;
182
183 // Returns a string representing the type of the variant,
184 // e.g. "string", "bool", "stringlist", "list", "double", "long"
185 wxString GetType() const;
186
187 bool IsType(const wxString& type) const;
188
189 // Return the number of elements in a list
190 int GetCount() const;
191
192 // Value accessors
193 double GetReal() const ;
194 inline double GetDouble() const { return GetReal(); };
195 long GetInteger() const ;
196 inline long GetLong() const { return GetInteger(); };
197 char GetChar() const ;
198 bool GetBool() const ;
199 wxString GetString() const ;
200 wxList& GetList() const ;
201 wxStringList& GetStringList() const ;
202
203 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
204 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
205 wxTime GetTime() const ;
206 wxDate GetDate() const ;
207 #endif
208 void* GetVoidPtr() const ;
209
210 // Operations
211 // Make NULL (i.e. delete the data)
212 void MakeNull();
213
214 // Make empty list
215 void NullList();
216
217 // Append to list
218 void Append(const wxVariant& value);
219
220 // Insert at front of list
221 void Insert(const wxVariant& value);
222
223 // Returns TRUE if the variant is a member of the list
224 bool Member(const wxVariant& value) const;
225
226 // Deletes the nth element of the list
227 bool Delete(int item);
228
229 // Clear list
230 void ClearList();
231
232 // Implementation
233 protected:
234 // Type conversion
235 bool Convert(long* value) const;
236 bool Convert(bool* value) const;
237 bool Convert(double* value) const;
238 bool Convert(wxString* value) const;
239 bool Convert(char* value) const;
240 // For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
241 #if wxUSE_TIMEDATE && !defined(__WATCOMC__)
242 bool Convert(wxTime* value) const;
243 bool Convert(wxDate* value) const;
244 #endif
245
246 // Attributes
247 protected:
248 wxVariantData* m_data;
249 wxString m_name;
250 };
251
252 extern wxVariant WXDLLEXPORT wxNullVariant;
253
254 #endif
255 // _WX_VARIANT_H_