]> git.saurik.com Git - wxWidgets.git/blame - include/wx/variant.h
extended RemoveLast() to take size_t parameter
[wxWidgets.git] / include / wx / variant.h
CommitLineData
8cb50e4b
JS
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)
3f4a0c5b 9// Licence: wxWindows licence
8cb50e4b
JS
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
a0a302dc 24#if wxUSE_TIMEDATE
3f4a0c5b
VZ
25 #include "wx/time.h"
26 #include "wx/date.h"
27#endif // time/date
a0a302dc 28
3f4a0c5b 29#include "wx/ioswrap.h"
8cb50e4b
JS
30
31/*
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.
35 *
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.
43 */
44
45class WXDLLEXPORT wxVariantData: public wxObject
46{
47DECLARE_ABSTRACT_CLASS(wxVariantData)
48public:
49
50// Construction & destruction
51 wxVariantData() {};
52
53// Override these to provide common functionality
54 // Copy to data
55 virtual void Copy(wxVariantData& data) = 0;
56 virtual bool Eq(wxVariantData& data) const = 0;
38830220 57#if wxUSE_STD_IOSTREAM
8cb50e4b 58 virtual bool Write(ostream& str) const = 0;
38830220 59#endif
8cb50e4b 60 virtual bool Write(wxString& str) const = 0;
38830220 61#if wxUSE_STD_IOSTREAM
8cb50e4b 62 virtual bool Read(istream& str) = 0;
38830220 63#endif
8cb50e4b
JS
64 virtual bool Read(wxString& str) = 0;
65 // What type is it? Return a string name.
66 virtual wxString GetType() const = 0;
67};
68
69/*
70 * wxVariant can store any kind of data, but has some basic types
71 * built in.
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.
75 */
76
77class WXDLLEXPORT wxVariant: public wxObject
78{
79DECLARE_DYNAMIC_CLASS(wxVariant)
80public:
81
82// Construction & destruction
83 wxVariant();
e90c1d2a
VZ
84 wxVariant(double val, const wxString& name = wxEmptyString);
85 wxVariant(long val, const wxString& name = wxEmptyString);
57493f9f 86#ifdef HAVE_BOOL
e90c1d2a 87 wxVariant(bool val, const wxString& name = wxEmptyString);
57493f9f 88#endif
e90c1d2a
VZ
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
457e6c54
JS
94// For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
95#if wxUSE_TIMEDATE && !defined(__WATCOMC__)
e90c1d2a
VZ
96 wxVariant(const wxTime& val, const wxString& name = wxEmptyString); // Time
97 wxVariant(const wxDate& val, const wxString& name = wxEmptyString); // Date
a0a302dc 98#endif
e90c1d2a
VZ
99 wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
100 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
8cb50e4b 101 wxVariant(const wxVariant& variant);
8cb50e4b
JS
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) ;
57493f9f 124#ifdef HAVE_BOOL
8cb50e4b
JS
125 bool operator== (bool value) const;
126 bool operator!= (bool value) const;
127 void operator= (bool value) ;
57493f9f 128#endif
8cb50e4b
JS
129 bool operator== (const wxString& value) const;
130 bool operator!= (const wxString& value) const;
131 void operator= (const wxString& value) ;
2ed57eb7 132 void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
8cb50e4b
JS
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) ;
457e6c54
JS
139// For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
140#if wxUSE_TIMEDATE && !defined(__WATCOMC__)
a0a302dc
JS
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) ;
147#endif
148 bool operator== (void* value) const;
149 bool operator!= (void* value) const;
150 void operator= (void* value) ;
8cb50e4b
JS
151
152 // Treat a list variant as an array
153 wxVariant operator[] (size_t idx) const;
154 wxVariant& operator[] (size_t idx) ;
155
156 // Implicit conversion to a wxString
157 inline operator wxString () const { return MakeString(); }
158 wxString MakeString() const;
159
160 // Other implicit conversions
161 inline operator double () const { return GetDouble(); }
a0a302dc 162 inline operator char () const { return GetChar(); }
8cb50e4b
JS
163 inline operator long () const { return GetLong(); }
164 inline operator bool () const { return GetBool(); }
457e6c54
JS
165// For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
166#if wxUSE_TIMEDATE && !defined(__WATCOMC__)
a0a302dc
JS
167 inline operator wxTime () const { return GetTime(); }
168 inline operator wxDate () const { return GetDate(); }
169#endif
170 inline operator void* () const { return GetVoidPtr(); }
8cb50e4b
JS
171
172// Accessors
a0a302dc
JS
173 // Sets/gets name
174 inline void SetName(const wxString& name) { m_name = name; }
175 inline const wxString& GetName() const { return m_name; }
176
8cb50e4b
JS
177 // Tests whether there is data
178 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
179
180 wxVariantData* GetData() const { return m_data; }
181 void SetData(wxVariantData* data) ;
182
183 // Returns a string representing the type of the variant,
184 // e.g. "string", "bool", "stringlist", "list", "double", "long"
185 wxString GetType() const;
186
187 bool IsType(const wxString& type) const;
188
189 // Return the number of elements in a list
190 int GetCount() const;
191
192// Value accessors
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 ;
457e6c54
JS
202
203// For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
204#if wxUSE_TIMEDATE && !defined(__WATCOMC__)
a0a302dc
JS
205 wxTime GetTime() const ;
206 wxDate GetDate() const ;
207#endif
208 void* GetVoidPtr() const ;
8cb50e4b
JS
209
210// Operations
211 // Make NULL (i.e. delete the data)
212 void MakeNull();
213
214 // Make empty list
215 void NullList();
216
217 // Append to list
218 void Append(const wxVariant& value);
219
220 // Insert at front of list
221 void Insert(const wxVariant& value);
222
223 // Returns TRUE if the variant is a member of the list
224 bool Member(const wxVariant& value) const;
225
226 // Deletes the nth element of the list
227 bool Delete(int item);
228
229 // Clear list
230 void ClearList();
231
a0a302dc
JS
232// Implementation
233protected:
8cb50e4b
JS
234// Type conversion
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;
457e6c54
JS
240// For some reason, Watcom C++ can't link variant.cpp with time/date classes compiled
241#if wxUSE_TIMEDATE && !defined(__WATCOMC__)
a0a302dc
JS
242 bool Convert(wxTime* value) const;
243 bool Convert(wxDate* value) const;
244#endif
8cb50e4b
JS
245
246// Attributes
247protected:
248 wxVariantData* m_data;
a0a302dc 249 wxString m_name;
8cb50e4b
JS
250};
251
3ba055ac 252extern wxVariant WXDLLEXPORT wxNullVariant;
a0a302dc 253
8cb50e4b
JS
254#endif
255 // _WX_VARIANT_H_