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"
34 * wxVariantData stores the actual data in a wxVariant object,
35 * to allow it to store any type of data.
36 * Derive from this to provide custom data handling.
38 * TODO: in order to replace wxPropertyValue, we would need
39 * to consider adding constructors that take pointers to C++ variables,
40 * or removing that functionality from the wxProperty library.
41 * Essentially wxPropertyValue takes on some of the wxValidator functionality
42 * by storing pointers and not just actual values, allowing update of C++ data
43 * to be handled automatically. Perhaps there's another way of doing this without
44 * overloading wxVariant with unnecessary functionality.
47 class WXDLLEXPORT wxVariantData
: public wxObject
49 DECLARE_ABSTRACT_CLASS(wxVariantData
)
52 // Construction & destruction
55 // Override these to provide common functionality
57 virtual void Copy(wxVariantData
& data
) = 0;
58 virtual bool Eq(wxVariantData
& data
) const = 0;
59 virtual bool Write(ostream
& str
) const = 0;
60 virtual bool Write(wxString
& str
) const = 0;
61 virtual bool Read(istream
& str
) = 0;
62 virtual bool Read(wxString
& str
) = 0;
63 // What type is it? Return a string name.
64 virtual wxString
GetType() const = 0;
68 * wxVariant can store any kind of data, but has some basic types
70 * NOTE: this eventually should have a reference-counting implementation.
71 * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating
72 * this class too much.
75 class WXDLLEXPORT wxVariant
: public wxObject
77 DECLARE_DYNAMIC_CLASS(wxVariant
)
80 // Construction & destruction
82 wxVariant(double val
);
86 wxVariant(const wxString
& val
);
87 wxVariant(const char* val
); // Necessary or VC++ assumes bool!
89 wxVariant(const wxStringList& val);
91 wxVariant(const wxList
& val
); // List of variants
92 wxVariant(const wxVariant
& variant
);
93 wxVariant(wxVariantData
* data
); // User-defined data
98 void operator= (const wxVariant
& variant
);
100 // Assignment using data, e.g.
101 // myVariant = new wxStringVariantData("hello");
102 void operator= (wxVariantData
* variantData
);
103 bool operator== (const wxVariant
& variant
) const;
104 bool operator!= (const wxVariant
& variant
) const;
106 // Specific operators
107 bool operator== (double value
) const;
108 bool operator!= (double value
) const;
109 void operator= (double value
) ;
110 bool operator== (long value
) const;
111 bool operator!= (long value
) const;
112 void operator= (long value
) ;
113 bool operator== (char value
) const;
114 bool operator!= (char value
) const;
115 void operator= (char value
) ;
116 bool operator== (bool value
) const;
117 bool operator!= (bool value
) const;
118 void operator= (bool value
) ;
119 bool operator== (const wxString
& value
) const;
120 bool operator!= (const wxString
& value
) const;
121 void operator= (const wxString
& value
) ;
122 void operator= (const char* value
) ; // Necessary or VC++ assumes bool!
123 bool operator== (const wxStringList
& value
) const;
124 bool operator!= (const wxStringList
& value
) const;
125 void operator= (const wxStringList
& value
) ;
126 bool operator== (const wxList
& value
) const;
127 bool operator!= (const wxList
& value
) const;
128 void operator= (const wxList
& value
) ;
130 // Treat a list variant as an array
131 wxVariant
operator[] (size_t idx
) const;
132 wxVariant
& operator[] (size_t idx
) ;
134 // Implicit conversion to a wxString
135 inline operator wxString () const { return MakeString(); }
136 wxString
MakeString() const;
138 // Other implicit conversions
139 inline operator double () const { return GetDouble(); }
140 inline operator long () const { return GetLong(); }
141 inline operator bool () const { return GetBool(); }
144 // Tests whether there is data
145 inline bool IsNull() const { return (m_data
== (wxVariantData
*) NULL
); }
147 wxVariantData
* GetData() const { return m_data
; }
148 void SetData(wxVariantData
* data
) ;
150 // Returns a string representing the type of the variant,
151 // e.g. "string", "bool", "stringlist", "list", "double", "long"
152 wxString
GetType() const;
154 bool IsType(const wxString
& type
) const;
156 // Return the number of elements in a list
157 int GetCount() const;
160 double GetReal() const ;
161 inline double GetDouble() const { return GetReal(); };
162 long GetInteger() const ;
163 inline long GetLong() const { return GetInteger(); };
164 char GetChar() const ;
165 bool GetBool() const ;
166 wxString
GetString() const ;
167 wxList
& GetList() const ;
168 wxStringList
& GetStringList() const ;
171 // Make NULL (i.e. delete the data)
178 void Append(const wxVariant
& value
);
180 // Insert at front of list
181 void Insert(const wxVariant
& value
);
183 // Returns TRUE if the variant is a member of the list
184 bool Member(const wxVariant
& value
) const;
186 // Deletes the nth element of the list
187 bool Delete(int item
);
193 bool Convert(long* value
) const;
194 bool Convert(bool* value
) const;
195 bool Convert(double* value
) const;
196 bool Convert(wxString
* value
) const;
197 bool Convert(char* value
) const;
201 wxVariantData
* m_data
;