]> git.saurik.com Git - wxWidgets.git/blame - include/wx/variant.h
Works a little better when you remember to say "virtual"
[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
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
8cb50e4b
JS
16#pragma interface "variant.h"
17#endif
18
19#include "wx/defs.h"
20#include "wx/object.h"
21#include "wx/string.h"
4c3ebca9 22#include "wx/arrstr.h"
8cb50e4b
JS
23#include "wx/list.h"
24
e2b87f38
VZ
25#if wxUSE_DATETIME
26 #include "wx/datetime.h"
27#endif // wxUSE_DATETIME
edca7a82
GT
28
29#if wxUSE_ODBC
30 #include "wx/db.h" // will #include sqltypes.h
31#endif //ODBC
32
65f19af1 33#include "wx/iosfwrap.h"
8cb50e4b
JS
34
35/*
36 * wxVariantData stores the actual data in a wxVariant object,
37 * to allow it to store any type of data.
38 * Derive from this to provide custom data handling.
39 *
40 * TODO: in order to replace wxPropertyValue, we would need
41 * to consider adding constructors that take pointers to C++ variables,
42 * or removing that functionality from the wxProperty library.
43 * Essentially wxPropertyValue takes on some of the wxValidator functionality
44 * by storing pointers and not just actual values, allowing update of C++ data
45 * to be handled automatically. Perhaps there's another way of doing this without
46 * overloading wxVariant with unnecessary functionality.
47 */
48
bddd7a8d 49class WXDLLIMPEXP_BASE wxVariantData: public wxObject
8cb50e4b
JS
50{
51DECLARE_ABSTRACT_CLASS(wxVariantData)
52public:
53
54// Construction & destruction
55 wxVariantData() {};
56
57// Override these to provide common functionality
58 // Copy to data
59 virtual void Copy(wxVariantData& data) = 0;
60 virtual bool Eq(wxVariantData& data) const = 0;
38830220 61#if wxUSE_STD_IOSTREAM
dd107c50 62 virtual bool Write(wxSTD ostream& str) const = 0;
38830220 63#endif
8cb50e4b 64 virtual bool Write(wxString& str) const = 0;
38830220 65#if wxUSE_STD_IOSTREAM
dd107c50 66 virtual bool Read(wxSTD istream& str) = 0;
38830220 67#endif
8cb50e4b
JS
68 virtual bool Read(wxString& str) = 0;
69 // What type is it? Return a string name.
70 virtual wxString GetType() const = 0;
cf6ae290
RG
71 // If it based on wxObject return the ClassInfo.
72 virtual wxClassInfo* GetValueClassInfo() { return NULL; }
8cb50e4b
JS
73};
74
75/*
76 * wxVariant can store any kind of data, but has some basic types
77 * built in.
78 * NOTE: this eventually should have a reference-counting implementation.
79 * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating
80 * this class too much.
81 */
82
bddd7a8d 83class WXDLLIMPEXP_BASE wxVariant: public wxObject
8cb50e4b
JS
84{
85DECLARE_DYNAMIC_CLASS(wxVariant)
86public:
87
88// Construction & destruction
89 wxVariant();
e90c1d2a
VZ
90 wxVariant(double val, const wxString& name = wxEmptyString);
91 wxVariant(long val, const wxString& name = wxEmptyString);
57493f9f 92#ifdef HAVE_BOOL
e90c1d2a 93 wxVariant(bool val, const wxString& name = wxEmptyString);
57493f9f 94#endif
e90c1d2a
VZ
95 wxVariant(char val, const wxString& name = wxEmptyString);
96 wxVariant(const wxString& val, const wxString& name = wxEmptyString);
97 wxVariant(const wxChar* val, const wxString& name = wxEmptyString); // Necessary or VC++ assumes bool!
98 wxVariant(const wxStringList& val, const wxString& name = wxEmptyString);
99 wxVariant(const wxList& val, const wxString& name = wxEmptyString); // List of variants
e90c1d2a 100 wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose)
cf6ae290 101 wxVariant(wxObject* ptr, const wxString& name = wxEmptyString); //wxObject
e90c1d2a 102 wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data
edca7a82 103//TODO: Need to document
e2b87f38 104#if wxUSE_DATETIME
edca7a82 105 wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date
e2b87f38 106#endif // wxUSE_DATETIME
fb42d7c3 107 wxVariant(const wxArrayString& val, const wxString& name = wxEmptyString); // String array
edca7a82
GT
108#if wxUSE_ODBC
109 wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime
110 wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime
111 wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime
112#endif
113//TODO: End of Need to document
114
8cb50e4b 115 wxVariant(const wxVariant& variant);
8cb50e4b
JS
116 ~wxVariant();
117
118// Generic operators
119 // Assignment
120 void operator= (const wxVariant& variant);
121
edca7a82 122//TODO: Need to document
e2b87f38 123#if wxUSE_DATETIME
edca7a82
GT
124 bool operator== (const wxDateTime& value) const;
125 bool operator!= (const wxDateTime& value) const;
126 void operator= (const wxDateTime& value) ;
e2b87f38 127#endif // wxUSE_DATETIME
fb42d7c3
VZ
128
129 bool operator== (const wxArrayString& value) const;
130 bool operator!= (const wxArrayString& value) const;
131 void operator= (const wxArrayString& value) ;
edca7a82
GT
132#if wxUSE_ODBC
133 void operator= (const DATE_STRUCT* value) ;
134 void operator= (const TIME_STRUCT* value) ;
135 void operator= (const TIMESTAMP_STRUCT* value) ;
136#endif
137//TODO: End of Need to document
138
8cb50e4b
JS
139 // Assignment using data, e.g.
140 // myVariant = new wxStringVariantData("hello");
141 void operator= (wxVariantData* variantData);
142 bool operator== (const wxVariant& variant) const;
143 bool operator!= (const wxVariant& variant) const;
144
145// Specific operators
146 bool operator== (double value) const;
147 bool operator!= (double value) const;
148 void operator= (double value) ;
149 bool operator== (long value) const;
150 bool operator!= (long value) const;
151 void operator= (long value) ;
152 bool operator== (char value) const;
153 bool operator!= (char value) const;
154 void operator= (char value) ;
57493f9f 155#ifdef HAVE_BOOL
8cb50e4b
JS
156 bool operator== (bool value) const;
157 bool operator!= (bool value) const;
158 void operator= (bool value) ;
57493f9f 159#endif
8cb50e4b
JS
160 bool operator== (const wxString& value) const;
161 bool operator!= (const wxString& value) const;
162 void operator= (const wxString& value) ;
2ed57eb7 163 void operator= (const wxChar* value) ; // Necessary or VC++ assumes bool!
8cb50e4b
JS
164 bool operator== (const wxStringList& value) const;
165 bool operator!= (const wxStringList& value) const;
166 void operator= (const wxStringList& value) ;
167 bool operator== (const wxList& value) const;
168 bool operator!= (const wxList& value) const;
169 void operator= (const wxList& value) ;
a0a302dc
JS
170 bool operator== (void* value) const;
171 bool operator!= (void* value) const;
172 void operator= (void* value) ;
8cb50e4b
JS
173
174 // Treat a list variant as an array
175 wxVariant operator[] (size_t idx) const;
176 wxVariant& operator[] (size_t idx) ;
177
178 // Implicit conversion to a wxString
179 inline operator wxString () const { return MakeString(); }
180 wxString MakeString() const;
181
182 // Other implicit conversions
183 inline operator double () const { return GetDouble(); }
a0a302dc 184 inline operator char () const { return GetChar(); }
8cb50e4b
JS
185 inline operator long () const { return GetLong(); }
186 inline operator bool () const { return GetBool(); }
a0a302dc 187 inline operator void* () const { return GetVoidPtr(); }
cf6ae290
RG
188 // No implicit conversion to wxObject, as that would really
189 // confuse people between conversion to our contained data
190 // and downcasting to see our base type.
edca7a82 191//TODO: Need to document
e2b87f38 192#if wxUSE_DATETIME
edca7a82 193 inline operator wxDateTime () const { return GetDateTime(); }
e2b87f38 194#endif // wxUSE_DATETIME
edca7a82 195//TODO: End of Need to document
8cb50e4b
JS
196
197// Accessors
a0a302dc
JS
198 // Sets/gets name
199 inline void SetName(const wxString& name) { m_name = name; }
200 inline const wxString& GetName() const { return m_name; }
201
8cb50e4b
JS
202 // Tests whether there is data
203 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
204
205 wxVariantData* GetData() const { return m_data; }
206 void SetData(wxVariantData* data) ;
207
208 // Returns a string representing the type of the variant,
209 // e.g. "string", "bool", "stringlist", "list", "double", "long"
210 wxString GetType() const;
211
212 bool IsType(const wxString& type) const;
cf6ae290 213 bool IsValueKindOf(const wxClassInfo* type) const;
8cb50e4b
JS
214
215 // Return the number of elements in a list
216 int GetCount() const;
217
218// Value accessors
219 double GetReal() const ;
220 inline double GetDouble() const { return GetReal(); };
221 long GetInteger() const ;
222 inline long GetLong() const { return GetInteger(); };
223 char GetChar() const ;
224 bool GetBool() const ;
225 wxString GetString() const ;
226 wxList& GetList() const ;
227 wxStringList& GetStringList() const ;
457e6c54 228
a0a302dc 229 void* GetVoidPtr() const ;
cf6ae290 230 wxObject* GetWxObjectPtr() ;
edca7a82 231//TODO: Need to document
e2b87f38 232#if wxUSE_DATETIME
edca7a82 233 wxDateTime GetDateTime() const ;
e2b87f38 234#endif // wxUSE_DATETIME
fb42d7c3 235 wxArrayString GetArrayString() const;
edca7a82 236//TODO: End of Need to document
8cb50e4b
JS
237
238// Operations
239 // Make NULL (i.e. delete the data)
240 void MakeNull();
241
242 // Make empty list
243 void NullList();
244
245 // Append to list
246 void Append(const wxVariant& value);
247
248 // Insert at front of list
249 void Insert(const wxVariant& value);
250
251 // Returns TRUE if the variant is a member of the list
252 bool Member(const wxVariant& value) const;
253
254 // Deletes the nth element of the list
255 bool Delete(int item);
256
257 // Clear list
258 void ClearList();
259
a0a302dc 260// Implementation
9708db20 261public:
8cb50e4b
JS
262// Type conversion
263 bool Convert(long* value) const;
264 bool Convert(bool* value) const;
265 bool Convert(double* value) const;
266 bool Convert(wxString* value) const;
267 bool Convert(char* value) const;
edca7a82 268//TODO: Need to document
e2b87f38 269#if wxUSE_DATETIME
edca7a82 270 bool Convert(wxDateTime* value) const;
e2b87f38 271#endif // wxUSE_DATETIME
edca7a82 272//TODO: End of Need to document
8cb50e4b
JS
273
274// Attributes
275protected:
276 wxVariantData* m_data;
a0a302dc 277 wxString m_name;
8cb50e4b
JS
278};
279
cf6ae290
RG
280//Since we want type safety wxVariant we need to fetch and dynamic_cast
281//in a seemingly safe way so the compiler can check, so we define
282//a dynamic_cast /wxDynamicCast analogue.
283
284#define wxGetVariantCast(var,classname) \
285 ((classname*)(var.IsValueKindOf(&classname::sm_class##classname) ?\
286 var.GetWxObjectPtr() : NULL));
287
bddd7a8d 288extern wxVariant WXDLLIMPEXP_BASE wxNullVariant;
a0a302dc 289
8cb50e4b
JS
290#endif
291 // _WX_VARIANT_H_