some Linux compilation fixes
[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
28
29 #if wxUSE_IOSTREAMH
30 #include <iostream.h>
31 #else
32 #include <iostream>
33 # ifdef _MSC_VER
34 using namespace std;
35 # endif
36 #endif
37
38 /*
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.
42 *
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.
50 */
51
52 class WXDLLEXPORT wxVariantData: public wxObject
53 {
54 DECLARE_ABSTRACT_CLASS(wxVariantData)
55 public:
56
57 // Construction & destruction
58 wxVariantData() {};
59
60 // Override these to provide common functionality
61 // Copy to data
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;
70 };
71
72 /*
73 * wxVariant can store any kind of data, but has some basic types
74 * built in.
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.
78 */
79
80 class WXDLLEXPORT wxVariant: public wxObject
81 {
82 DECLARE_DYNAMIC_CLASS(wxVariant)
83 public:
84
85 // Construction & destruction
86 wxVariant();
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
95 #if wxUSE_TIMEDATE
96 wxVariant(const wxTime& val, const wxString& name = g_szNul); // Time
97 wxVariant(const wxDate& val, const wxString& name = g_szNul); // Date
98 #endif
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);
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 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) ;
137 #if wxUSE_TIMEDATE
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) ;
144 #endif
145 bool operator== (void* value) const;
146 bool operator!= (void* value) const;
147 void operator= (void* value) ;
148
149 // Treat a list variant as an array
150 wxVariant operator[] (size_t idx) const;
151 wxVariant& operator[] (size_t idx) ;
152
153 // Implicit conversion to a wxString
154 inline operator wxString () const { return MakeString(); }
155 wxString MakeString() const;
156
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(); }
162 #if wxUSE_TIMEDATE
163 inline operator wxTime () const { return GetTime(); }
164 inline operator wxDate () const { return GetDate(); }
165 #endif
166 inline operator void* () const { return GetVoidPtr(); }
167
168 // Accessors
169 // Sets/gets name
170 inline void SetName(const wxString& name) { m_name = name; }
171 inline const wxString& GetName() const { return m_name; }
172
173 // Tests whether there is data
174 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
175
176 wxVariantData* GetData() const { return m_data; }
177 void SetData(wxVariantData* data) ;
178
179 // Returns a string representing the type of the variant,
180 // e.g. "string", "bool", "stringlist", "list", "double", "long"
181 wxString GetType() const;
182
183 bool IsType(const wxString& type) const;
184
185 // Return the number of elements in a list
186 int GetCount() const;
187
188 // Value accessors
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 ;
198 #if wxUSE_TIMEDATE
199 wxTime GetTime() const ;
200 wxDate GetDate() const ;
201 #endif
202 void* GetVoidPtr() const ;
203
204 // Operations
205 // Make NULL (i.e. delete the data)
206 void MakeNull();
207
208 // Make empty list
209 void NullList();
210
211 // Append to list
212 void Append(const wxVariant& value);
213
214 // Insert at front of list
215 void Insert(const wxVariant& value);
216
217 // Returns TRUE if the variant is a member of the list
218 bool Member(const wxVariant& value) const;
219
220 // Deletes the nth element of the list
221 bool Delete(int item);
222
223 // Clear list
224 void ClearList();
225
226 // Implementation
227 protected:
228 // Type conversion
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;
234 #if wxUSE_TIMEDATE
235 bool Convert(wxTime* value) const;
236 bool Convert(wxDate* value) const;
237 #endif
238
239 // Attributes
240 protected:
241 wxVariantData* m_data;
242 wxString m_name;
243 };
244
245 extern wxVariant wxNullVariant;
246
247 #endif
248 // _WX_VARIANT_H_