Changes related to stream includes
[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_IOSTREAMH
25 #include <iostream.h>
26 #else
27 #include <iostream>
28 # ifdef _MSC_VER
29 using namespace std;
30 # endif
31 #endif
32
33 /*
34 * wxVariantData stores the actual data in a wxVariant object,
35 * to allow it to store any type of data.
36 * Derive from this to provide custom data handling.
37 *
38 * TODO: in order to replace wxPropertyValue, we would need
39 * to consider adding constructors that take pointers to C++ variables,
40 * or removing that functionality from the wxProperty library.
41 * Essentially wxPropertyValue takes on some of the wxValidator functionality
42 * by storing pointers and not just actual values, allowing update of C++ data
43 * to be handled automatically. Perhaps there's another way of doing this without
44 * overloading wxVariant with unnecessary functionality.
45 */
46
47 class WXDLLEXPORT wxVariantData: public wxObject
48 {
49 DECLARE_ABSTRACT_CLASS(wxVariantData)
50 public:
51
52 // Construction & destruction
53 wxVariantData() {};
54
55 // Override these to provide common functionality
56 // Copy to data
57 virtual void Copy(wxVariantData& data) = 0;
58 virtual bool Eq(wxVariantData& data) const = 0;
59 virtual bool Write(ostream& str) const = 0;
60 virtual bool Write(wxString& str) const = 0;
61 virtual bool Read(istream& str) = 0;
62 virtual bool Read(wxString& str) = 0;
63 // What type is it? Return a string name.
64 virtual wxString GetType() const = 0;
65 };
66
67 /*
68 * wxVariant can store any kind of data, but has some basic types
69 * built in.
70 * NOTE: this eventually should have a reference-counting implementation.
71 * PLEASE, if you change it to ref-counting, make sure it doesn't involve bloating
72 * this class too much.
73 */
74
75 class WXDLLEXPORT wxVariant: public wxObject
76 {
77 DECLARE_DYNAMIC_CLASS(wxVariant)
78 public:
79
80 // Construction & destruction
81 wxVariant();
82 wxVariant(double val);
83 wxVariant(long val);
84 wxVariant(bool val);
85 wxVariant(char val);
86 wxVariant(const wxString& val);
87 wxVariant(const char* val); // Necessary or VC++ assumes bool!
88 /* Causes ambiguity
89 wxVariant(const wxStringList& val);
90 */
91 wxVariant(const wxList& val); // List of variants
92 wxVariant(const wxVariant& variant);
93 wxVariant(wxVariantData* data); // User-defined data
94 ~wxVariant();
95
96 // Generic operators
97 // Assignment
98 void operator= (const wxVariant& variant);
99
100 // Assignment using data, e.g.
101 // myVariant = new wxStringVariantData("hello");
102 void operator= (wxVariantData* variantData);
103 bool operator== (const wxVariant& variant) const;
104 bool operator!= (const wxVariant& variant) const;
105
106 // Specific operators
107 bool operator== (double value) const;
108 bool operator!= (double value) const;
109 void operator= (double value) ;
110 bool operator== (long value) const;
111 bool operator!= (long value) const;
112 void operator= (long value) ;
113 bool operator== (char value) const;
114 bool operator!= (char value) const;
115 void operator= (char value) ;
116 bool operator== (bool value) const;
117 bool operator!= (bool value) const;
118 void operator= (bool value) ;
119 bool operator== (const wxString& value) const;
120 bool operator!= (const wxString& value) const;
121 void operator= (const wxString& value) ;
122 void operator= (const char* value) ; // Necessary or VC++ assumes bool!
123 bool operator== (const wxStringList& value) const;
124 bool operator!= (const wxStringList& value) const;
125 void operator= (const wxStringList& value) ;
126 bool operator== (const wxList& value) const;
127 bool operator!= (const wxList& value) const;
128 void operator= (const wxList& value) ;
129
130 // Treat a list variant as an array
131 wxVariant operator[] (size_t idx) const;
132 wxVariant& operator[] (size_t idx) ;
133
134 // Implicit conversion to a wxString
135 inline operator wxString () const { return MakeString(); }
136 wxString MakeString() const;
137
138 // Other implicit conversions
139 inline operator double () const { return GetDouble(); }
140 inline operator long () const { return GetLong(); }
141 inline operator bool () const { return GetBool(); }
142
143 // Accessors
144 // Tests whether there is data
145 inline bool IsNull() const { return (m_data == (wxVariantData*) NULL); }
146
147 wxVariantData* GetData() const { return m_data; }
148 void SetData(wxVariantData* data) ;
149
150 // Returns a string representing the type of the variant,
151 // e.g. "string", "bool", "stringlist", "list", "double", "long"
152 wxString GetType() const;
153
154 bool IsType(const wxString& type) const;
155
156 // Return the number of elements in a list
157 int GetCount() const;
158
159 // Value accessors
160 double GetReal() const ;
161 inline double GetDouble() const { return GetReal(); };
162 long GetInteger() const ;
163 inline long GetLong() const { return GetInteger(); };
164 char GetChar() const ;
165 bool GetBool() const ;
166 wxString GetString() const ;
167 wxList& GetList() const ;
168 wxStringList& GetStringList() const ;
169
170 // Operations
171 // Make NULL (i.e. delete the data)
172 void MakeNull();
173
174 // Make empty list
175 void NullList();
176
177 // Append to list
178 void Append(const wxVariant& value);
179
180 // Insert at front of list
181 void Insert(const wxVariant& value);
182
183 // Returns TRUE if the variant is a member of the list
184 bool Member(const wxVariant& value) const;
185
186 // Deletes the nth element of the list
187 bool Delete(int item);
188
189 // Clear list
190 void ClearList();
191
192 // Type conversion
193 bool Convert(long* value) const;
194 bool Convert(bool* value) const;
195 bool Convert(double* value) const;
196 bool Convert(wxString* value) const;
197 bool Convert(char* value) const;
198
199 // Attributes
200 protected:
201 wxVariantData* m_data;
202 };
203
204 #endif
205 // _WX_VARIANT_H_