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_
16 #pragma interface "variant.h"
20 #include "wx/object.h"
21 #include "wx/string.h"
29 #include "wx/ioswrap.h"
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.
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.
45 class WXDLLEXPORT wxVariantData
: public wxObject
47 DECLARE_ABSTRACT_CLASS(wxVariantData
)
50 // Construction & destruction
53 // Override these to provide common functionality
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;
60 virtual bool Write(wxString
& str
) const = 0;
61 #if wxUSE_STD_IOSTREAM
62 virtual bool Read(istream
& str
) = 0;
64 virtual bool Read(wxString
& str
) = 0;
65 // What type is it? Return a string name.
66 virtual wxString
GetType() const = 0;
70 * wxVariant can store any kind of data, but has some basic types
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.
77 class WXDLLEXPORT wxVariant
: public wxObject
79 DECLARE_DYNAMIC_CLASS(wxVariant
)
82 // Construction & destruction
84 wxVariant(double val
, const wxString
& name
= wxEmptyString
);
85 wxVariant(long val
, const wxString
& name
= wxEmptyString
);
87 wxVariant(bool val
, const wxString
& name
= wxEmptyString
);
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
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
);
106 void operator= (const wxVariant
& variant
);
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;
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
) ;
125 bool operator== (bool value
) const;
126 bool operator!= (bool value
) const;
127 void operator= (bool value
) ;
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
) ;
148 bool operator== (void* value
) const;
149 bool operator!= (void* value
) const;
150 void operator= (void* value
) ;
152 // Treat a list variant as an array
153 wxVariant
operator[] (size_t idx
) const;
154 wxVariant
& operator[] (size_t idx
) ;
156 // Implicit conversion to a wxString
157 inline operator wxString () const { return MakeString(); }
158 wxString
MakeString() const;
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(); }
170 inline operator void* () const { return GetVoidPtr(); }
174 inline void SetName(const wxString
& name
) { m_name
= name
; }
175 inline const wxString
& GetName() const { return m_name
; }
177 // Tests whether there is data
178 inline bool IsNull() const { return (m_data
== (wxVariantData
*) NULL
); }
180 wxVariantData
* GetData() const { return m_data
; }
181 void SetData(wxVariantData
* data
) ;
183 // Returns a string representing the type of the variant,
184 // e.g. "string", "bool", "stringlist", "list", "double", "long"
185 wxString
GetType() const;
187 bool IsType(const wxString
& type
) const;
189 // Return the number of elements in a list
190 int GetCount() const;
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 ;
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 ;
208 void* GetVoidPtr() const ;
211 // Make NULL (i.e. delete the data)
218 void Append(const wxVariant
& value
);
220 // Insert at front of list
221 void Insert(const wxVariant
& value
);
223 // Returns TRUE if the variant is a member of the list
224 bool Member(const wxVariant
& value
) const;
226 // Deletes the nth element of the list
227 bool Delete(int item
);
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;
248 wxVariantData
* m_data
;
252 extern wxVariant WXDLLEXPORT wxNullVariant
;