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"
39 * wxVariantData stores the actual data in a wxVariant object,
40 * to allow it to store any type of data.
41 * Derive from this to provide custom data handling.
43 * TODO: in order to replace wxPropertyValue, we would need
44 * to consider adding constructors that take pointers to C++ variables,
45 * or removing that functionality from the wxProperty library.
46 * Essentially wxPropertyValue takes on some of the wxValidator functionality
47 * by storing pointers and not just actual values, allowing update of C++ data
48 * to be handled automatically. Perhaps there's another way of doing this without
49 * overloading wxVariant with unnecessary functionality.
52 class WXDLLEXPORT wxVariantData
: public wxObject
54 DECLARE_ABSTRACT_CLASS(wxVariantData
)
57 // Construction & destruction
60 // Override these to provide common functionality
62 virtual void Copy(wxVariantData
& data
) = 0;
63 virtual bool Eq(wxVariantData
& data
) const = 0;
64 virtual bool Write(ostream
& str
) const = 0;
65 virtual bool Write(wxString
& str
) const = 0;
66 virtual bool Read(istream
& str
) = 0;
67 virtual bool Read(wxString
& str
) = 0;
68 // What type is it? Return a string name.
69 virtual wxString
GetType() const = 0;
73 * wxVariant can store any kind of data, but has some basic types
75 * NOTE: this eventually should have a reference-counting implementation.
76 * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating
77 * this class too much.
80 class WXDLLEXPORT wxVariant
: public wxObject
82 DECLARE_DYNAMIC_CLASS(wxVariant
)
85 // Construction & destruction
87 wxVariant(double val
, const wxString
& name
= g_szNul
);
88 wxVariant(long val
, const wxString
& name
= g_szNul
);
89 wxVariant(bool val
, const wxString
& name
= g_szNul
);
90 wxVariant(char val
, const wxString
& name
= g_szNul
);
91 wxVariant(const wxString
& val
, const wxString
& name
= g_szNul
);
92 wxVariant(const char* val
, const wxString
& name
= g_szNul
); // Necessary or VC++ assumes bool!
93 wxVariant(const wxStringList
& val
, const wxString
& name
= g_szNul
);
94 wxVariant(const wxList
& val
, const wxString
& name
= g_szNul
); // List of variants
96 wxVariant(const wxTime
& val
, const wxString
& name
= g_szNul
); // Time
97 wxVariant(const wxDate
& val
, const wxString
& name
= g_szNul
); // Date
99 wxVariant(void* ptr
, const wxString
& name
= g_szNul
); // void* (general purpose)
100 wxVariant(wxVariantData
* data
, const wxString
& name
= g_szNul
); // 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
) ;
124 bool operator== (bool value
) const;
125 bool operator!= (bool value
) const;
126 void operator= (bool value
) ;
127 bool operator== (const wxString
& value
) const;
128 bool operator!= (const wxString
& value
) const;
129 void operator= (const wxString
& value
) ;
130 void operator= (const char* value
) ; // Necessary or VC++ assumes bool!
131 bool operator== (const wxStringList
& value
) const;
132 bool operator!= (const wxStringList
& value
) const;
133 void operator= (const wxStringList
& value
) ;
134 bool operator== (const wxList
& value
) const;
135 bool operator!= (const wxList
& value
) const;
136 void operator= (const wxList
& value
) ;
138 bool operator== (const wxTime
& value
) const;
139 bool operator!= (const wxTime
& value
) const;
140 void operator= (const wxTime
& value
) ;
141 bool operator== (const wxDate
& value
) const;
142 bool operator!= (const wxDate
& value
) const;
143 void operator= (const wxDate
& value
) ;
145 bool operator== (void* value
) const;
146 bool operator!= (void* value
) const;
147 void operator= (void* value
) ;
149 // Treat a list variant as an array
150 wxVariant
operator[] (size_t idx
) const;
151 wxVariant
& operator[] (size_t idx
) ;
153 // Implicit conversion to a wxString
154 inline operator wxString () const { return MakeString(); }
155 wxString
MakeString() const;
157 // Other implicit conversions
158 inline operator double () const { return GetDouble(); }
159 inline operator char () const { return GetChar(); }
160 inline operator long () const { return GetLong(); }
161 inline operator bool () const { return GetBool(); }
163 inline operator wxTime () const { return GetTime(); }
164 inline operator wxDate () const { return GetDate(); }
166 inline operator void* () const { return GetVoidPtr(); }
170 inline void SetName(const wxString
& name
) { m_name
= name
; }
171 inline const wxString
& GetName() const { return m_name
; }
173 // Tests whether there is data
174 inline bool IsNull() const { return (m_data
== (wxVariantData
*) NULL
); }
176 wxVariantData
* GetData() const { return m_data
; }
177 void SetData(wxVariantData
* data
) ;
179 // Returns a string representing the type of the variant,
180 // e.g. "string", "bool", "stringlist", "list", "double", "long"
181 wxString
GetType() const;
183 bool IsType(const wxString
& type
) const;
185 // Return the number of elements in a list
186 int GetCount() const;
189 double GetReal() const ;
190 inline double GetDouble() const { return GetReal(); };
191 long GetInteger() const ;
192 inline long GetLong() const { return GetInteger(); };
193 char GetChar() const ;
194 bool GetBool() const ;
195 wxString
GetString() const ;
196 wxList
& GetList() const ;
197 wxStringList
& GetStringList() const ;
199 wxTime
GetTime() const ;
200 wxDate
GetDate() const ;
202 void* GetVoidPtr() const ;
205 // Make NULL (i.e. delete the data)
212 void Append(const wxVariant
& value
);
214 // Insert at front of list
215 void Insert(const wxVariant
& value
);
217 // Returns TRUE if the variant is a member of the list
218 bool Member(const wxVariant
& value
) const;
220 // Deletes the nth element of the list
221 bool Delete(int item
);
229 bool Convert(long* value
) const;
230 bool Convert(bool* value
) const;
231 bool Convert(double* value
) const;
232 bool Convert(wxString
* value
) const;
233 bool Convert(char* value
) const;
235 bool Convert(wxTime
* value
) const;
236 bool Convert(wxDate
* value
) const;
241 wxVariantData
* m_data
;
245 extern wxVariant WXDLLEXPORT wxNullVariant
;