]> git.saurik.com Git - wxWidgets.git/blob - include/wx/variant.h
check for empty data pointer in Len() and Empty()
[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 USE_IOSTREAMH
25 #include <iostream.h>
26 #else
27 #include <iostream>
28 #endif
29
30 /*
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.
34 *
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.
42 */
43
44 class WXDLLEXPORT wxVariantData: public wxObject
45 {
46 DECLARE_ABSTRACT_CLASS(wxVariantData)
47 public:
48
49 // Construction & destruction
50 wxVariantData() {};
51
52 // Override these to provide common functionality
53 // Copy to data
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;
62 };
63
64 /*
65 * wxVariant can store any kind of data, but has some basic types
66 * built in.
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.
70 */
71
72 class WXDLLEXPORT wxVariant: public wxObject
73 {
74 DECLARE_DYNAMIC_CLASS(wxVariant)
75 public:
76
77 // Construction & destruction
78 wxVariant();
79 wxVariant(double val);
80 wxVariant(long val);
81 wxVariant(bool val);
82 wxVariant(char val);
83 wxVariant(const wxString& val);
84 wxVariant(const char* val); // Necessary or VC++ assumes bool!
85 /* Causes ambiguity
86 wxVariant(const wxStringList& val);
87 */
88 wxVariant(const wxList& val); // List of variants
89 wxVariant(const wxVariant& variant);
90 wxVariant(wxVariantData* data); // User-defined data
91 ~wxVariant();
92
93 // Generic operators
94 // Assignment
95 void operator= (const wxVariant& variant);
96
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;
102
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) ;
126
127 // Treat a list variant as an array
128 wxVariant operator[] (size_t idx) const;
129 wxVariant& operator[] (size_t idx) ;
130
131 // Implicit conversion to a wxString
132 inline operator wxString () const { return MakeString(); }
133 wxString MakeString() const;
134
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(); }
139
140 // Accessors
141 // Tests whether there is data
142 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
143
144 wxVariantData* GetData() const { return m_data; }
145 void SetData(wxVariantData* data) ;
146
147 // Returns a string representing the type of the variant,
148 // e.g. "string", "bool", "stringlist", "list", "double", "long"
149 wxString GetType() const;
150
151 bool IsType(const wxString& type) const;
152
153 // Return the number of elements in a list
154 int GetCount() const;
155
156 // Value accessors
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 ;
166
167 // Operations
168 // Make NULL (i.e. delete the data)
169 void MakeNull();
170
171 // Make empty list
172 void NullList();
173
174 // Append to list
175 void Append(const wxVariant& value);
176
177 // Insert at front of list
178 void Insert(const wxVariant& value);
179
180 // Returns TRUE if the variant is a member of the list
181 bool Member(const wxVariant& value) const;
182
183 // Deletes the nth element of the list
184 bool Delete(int item);
185
186 // Clear list
187 void ClearList();
188
189 // Type conversion
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;
195
196 // Attributes
197 protected:
198 wxVariantData* m_data;
199 };
200
201 #endif
202 // _WX_VARIANT_H_