]> git.saurik.com Git - wxWidgets.git/blame - include/wx/variant.h
Renamed GtkMyFixed to GtkPizza,
[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
a0a302dc 94#if wxUSE_TIMEDATE
e90c1d2a
VZ
95 wxVariant(const wxTime& val, const wxString& name = wxEmptyString); // Time
96 wxVariant(const wxDate& val, const wxString& name = wxEmptyString); // Date
a0a302dc 97#endif
e90c1d2a
VZ
98 wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
99 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
8cb50e4b 100 wxVariant(const wxVariant& variant);
8cb50e4b
JS
101 ~wxVariant();
102
103// Generic operators
104 // Assignment
105 void operator= (const wxVariant& variant);
106
107 // Assignment using data, e.g.
108 // myVariant = new wxStringVariantData("hello");
109 void operator= (wxVariantData* variantData);
110 bool operator== (const wxVariant& variant) const;
111 bool operator!= (const wxVariant& variant) const;
112
113// Specific operators
114 bool operator== (double value) const;
115 bool operator!= (double value) const;
116 void operator= (double value) ;
117 bool operator== (long value) const;
118 bool operator!= (long value) const;
119 void operator= (long value) ;
120 bool operator== (char value) const;
121 bool operator!= (char value) const;
122 void operator= (char value) ;
57493f9f 123#ifdef HAVE_BOOL
8cb50e4b
JS
124 bool operator== (bool value) const;
125 bool operator!= (bool value) const;
126 void operator= (bool value) ;
57493f9f 127#endif
8cb50e4b
JS
128 bool operator== (const wxString& value) const;
129 bool operator!= (const wxString& value) const;
130 void operator= (const wxString& value) ;
2ed57eb7 131 void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
8cb50e4b
JS
132 bool operator== (const wxStringList& value) const;
133 bool operator!= (const wxStringList& value) const;
134 void operator= (const wxStringList& value) ;
135 bool operator== (const wxList& value) const;
136 bool operator!= (const wxList& value) const;
137 void operator= (const wxList& value) ;
a0a302dc
JS
138#if wxUSE_TIMEDATE
139 bool operator== (const wxTime& value) const;
140 bool operator!= (const wxTime& value) const;
141 void operator= (const wxTime& value) ;
142 bool operator== (const wxDate& value) const;
143 bool operator!= (const wxDate& value) const;
144 void operator= (const wxDate& value) ;
145#endif
146 bool operator== (void* value) const;
147 bool operator!= (void* value) const;
148 void operator= (void* value) ;
8cb50e4b
JS
149
150 // Treat a list variant as an array
151 wxVariant operator[] (size_t idx) const;
152 wxVariant& operator[] (size_t idx) ;
153
154 // Implicit conversion to a wxString
155 inline operator wxString () const { return MakeString(); }
156 wxString MakeString() const;
157
158 // Other implicit conversions
159 inline operator double () const { return GetDouble(); }
a0a302dc 160 inline operator char () const { return GetChar(); }
8cb50e4b
JS
161 inline operator long () const { return GetLong(); }
162 inline operator bool () const { return GetBool(); }
a0a302dc
JS
163#if wxUSE_TIMEDATE
164 inline operator wxTime () const { return GetTime(); }
165 inline operator wxDate () const { return GetDate(); }
166#endif
167 inline operator void* () const { return GetVoidPtr(); }
8cb50e4b
JS
168
169// Accessors
a0a302dc
JS
170 // Sets/gets name
171 inline void SetName(const wxString& name) { m_name = name; }
172 inline const wxString& GetName() const { return m_name; }
173
8cb50e4b
JS
174 // Tests whether there is data
175 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
176
177 wxVariantData* GetData() const { return m_data; }
178 void SetData(wxVariantData* data) ;
179
180 // Returns a string representing the type of the variant,
181 // e.g. "string", "bool", "stringlist", "list", "double", "long"
182 wxString GetType() const;
183
184 bool IsType(const wxString& type) const;
185
186 // Return the number of elements in a list
187 int GetCount() const;
188
189// Value accessors
190 double GetReal() const ;
191 inline double GetDouble() const { return GetReal(); };
192 long GetInteger() const ;
193 inline long GetLong() const { return GetInteger(); };
194 char GetChar() const ;
195 bool GetBool() const ;
196 wxString GetString() const ;
197 wxList& GetList() const ;
198 wxStringList& GetStringList() const ;
a0a302dc
JS
199#if wxUSE_TIMEDATE
200 wxTime GetTime() const ;
201 wxDate GetDate() const ;
202#endif
203 void* GetVoidPtr() const ;
8cb50e4b
JS
204
205// Operations
206 // Make NULL (i.e. delete the data)
207 void MakeNull();
208
209 // Make empty list
210 void NullList();
211
212 // Append to list
213 void Append(const wxVariant& value);
214
215 // Insert at front of list
216 void Insert(const wxVariant& value);
217
218 // Returns TRUE if the variant is a member of the list
219 bool Member(const wxVariant& value) const;
220
221 // Deletes the nth element of the list
222 bool Delete(int item);
223
224 // Clear list
225 void ClearList();
226
a0a302dc
JS
227// Implementation
228protected:
8cb50e4b
JS
229// Type conversion
230 bool Convert(long* value) const;
231 bool Convert(bool* value) const;
232 bool Convert(double* value) const;
233 bool Convert(wxString* value) const;
234 bool Convert(char* value) const;
a0a302dc
JS
235#if wxUSE_TIMEDATE
236 bool Convert(wxTime* value) const;
237 bool Convert(wxDate* value) const;
238#endif
8cb50e4b
JS
239
240// Attributes
241protected:
242 wxVariantData* m_data;
a0a302dc 243 wxString m_name;
8cb50e4b
JS
244};
245
3ba055ac 246extern wxVariant WXDLLEXPORT wxNullVariant;
a0a302dc 247
8cb50e4b
JS
248#endif
249 // _WX_VARIANT_H_