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