Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXVariant.h
1 /********************************************************************************
2 * *
3 * V a r i a n t T y p e *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2013,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXVARIANT_H
22 #define FXVARIANT_H
23 
24 namespace FX {
25 
26 
27 class FXString;
28 class FXVariantMap;
29 class FXVariantArray;
30 
31 
44 class FXAPI FXVariant {
45 public:
46  enum VType {
47  VNull=0, // Simple types
48  VBool,
49  VChar,
50  VInt,
51  VUInt,
52  VLong,
53  VULong,
54  VFloat,
55  VDouble,
56  VPointer,
57  VString, // Complex types
58  VArray,
59  VMap
60  };
61 private:
62  union VValue {
63  FXlong i; // Signed integral types
64  FXulong u; // Unsigned integral types
65  FXdouble d; // Floating point types
66  FXchar* s; // Character string
67  FXptr p; // Pointer types
68  };
69 private:
70  VValue value; // Current value
71  VType type; // Type of value
72 private:
73  FXVariant& copy(const FXVariant& other);
74  FXVariant& init(VType t);
75 public:
76 
78  FXVariant();
79 
81  FXVariant(const FXVariant& other);
82 
84  explicit FXVariant(FXbool val);
85 
87  explicit FXVariant(FXchar val);
88 
90  explicit FXVariant(FXint val);
91 
93  explicit FXVariant(FXuint val);
94 
96  explicit FXVariant(FXlong val);
97 
99  explicit FXVariant(FXulong val);
100 
102  explicit FXVariant(FXfloat val);
103 
105  explicit FXVariant(FXdouble val);
106 
108  explicit FXVariant(FXptr val);
109 
111  explicit FXVariant(const FXchar *val);
112 
114  explicit FXVariant(const FXString& val);
115 
117  explicit FXVariant(const FXVariantArray& val);
118 
120  explicit FXVariant(const FXVariantMap& val);
121 
123  void setType(VType t);
124 
126  VType getType() const { return type; }
127 
129  FXival no() const;
130 
132  FXbool no(FXival n);
133 
135  FXbool isNull() const { return type==VNull; }
136 
138  FXbool isBool() const { return type==VBool; }
139 
141  FXbool isChar() const { return type==VChar; }
142 
144  FXbool isInt() const { return type==VInt; }
145 
147  FXbool isUInt() const { return type==VUInt; }
148 
150  FXbool isLong() const { return type==VLong; }
151 
153  FXbool isULong() const { return type==VULong; }
154 
156  FXbool isFloat() const { return type==VFloat; }
157 
159  FXbool isDouble() const { return type==VDouble; }
160 
162  FXbool isPtr() const { return type==VPointer; }
163 
165  FXbool isString() const { return type==VString; }
166 
168  FXbool isArray() const { return type==VArray; }
169 
171  FXbool isMap() const { return type==VMap; }
172 
174  FXbool toBool() const;
175 
177  FXptr toPtr() const;
178 
180  const FXchar* toChars() const;
181 
183  FXint toInt(FXbool* ok=NULL) const;
184 
186  FXuint toUInt(FXbool* ok=NULL) const;
187 
189  FXlong toLong(FXbool* ok=NULL) const;
190 
192  FXulong toULong(FXbool* ok=NULL) const;
193 
195  FXfloat toFloat(FXbool* ok=NULL) const;
196 
198  FXdouble toDouble(FXbool* ok=NULL) const;
199 
201  FXString toString(FXbool* ok=NULL) const;
202 
204  operator FXbool() const { return toBool(); }
205 
207  operator FXptr() const { return toPtr(); }
208 
210  operator FXchar() const { return toInt(); }
211 
213  operator FXuchar() const { return toUInt(); }
214 
216  operator FXshort() const { return toInt(); }
217 
219  operator FXushort() const { return toUInt(); }
220 
222  operator FXint() const { return toInt(); }
223 
225  operator FXuint() const { return toUInt(); }
226 
228  operator FXlong() const { return toLong(); }
229 
231  operator FXulong() const { return toULong(); }
232 
234  operator FXfloat() const { return toFloat(); }
235 
237  operator FXdouble() const { return toDouble(); }
238 
240  operator FXString() const { return toString(); }
241 
243  FXVariant& operator=(FXbool val);
244 
246  FXVariant& operator=(FXchar val);
247 
249  FXVariant& operator=(FXint val);
250 
252  FXVariant& operator=(FXuint val);
253 
255  FXVariant& operator=(FXlong val);
256 
258  FXVariant& operator=(FXulong val);
259 
261  FXVariant& operator=(FXfloat val);
262 
264  FXVariant& operator=(FXdouble val);
265 
267  FXVariant& operator=(FXptr val);
268 
270  FXVariant& operator=(const FXchar* val);
271 
273  FXVariant& operator=(const FXString& val);
274 
276  FXVariant& operator=(const FXVariantArray& val);
277 
279  FXVariant& operator=(const FXVariantMap& val);
280 
282  FXVariant& operator=(const FXVariant& val);
283 
285  FXVariant& adopt(FXVariant& other);
286 
288  FXVariant& adopt(FXVariantArray& other);
289 
291  FXVariant& adopt(FXVariantMap& other);
292 
294  FXVariant& at(const FXchar* key);
295 
297  const FXVariant& at(const FXchar* key) const;
298 
300  FXVariant& operator[](const FXchar* key){ return at(key); }
301 
303  const FXVariant& operator[](const FXchar* key) const { return at(key); }
304 
306  FXVariant& at(const FXString& key);
307 
309  const FXVariant& at(const FXString& key) const;
310 
312  FXVariant& operator[](const FXString& key){ return at(key); }
313 
315  const FXVariant& operator[](const FXString& key) const { return at(key); }
316 
318  FXVariant& at(FXival idx);
319 
321  const FXVariant& at(FXival idx) const;
322 
324  FXVariant& operator[](FXint idx){ return at(idx); }
325  const FXVariant& operator[](FXint idx) const { return at(idx); }
326 
328  FXVariant& operator[](FXival idx){ return at(idx); }
329  const FXVariant& operator[](FXival idx) const { return at(idx); }
330 
332  FXbool has(const FXchar* key) const;
333 
335  FXbool has(const FXString& key) const { return has(key.text()); }
336 
338  FXptr& asPtr(){ return value.p; }
339 
341  const FXptr& asPtr() const { return value.p; }
342 
344  FXlong& asLong(){ return value.i; }
345 
347  const FXlong& asLong() const { return value.i; }
348 
350  FXulong& asULong(){ return value.u; }
351 
353  const FXulong& asULong() const { return value.u; }
354 
356  FXdouble& asDouble(){ return value.d; }
357 
359  const FXdouble& asDouble() const { return value.d; }
360 
362  const FXchar* asChars() const { return value.s; }
363 
365  FXString& asString(){ return *reinterpret_cast<FXString*>(&value.p); }
366 
368  const FXString& asString() const { return *reinterpret_cast<const FXString*>(&value.p); }
369 
371  FXVariantArray& asArray(){ return *reinterpret_cast<FXVariantArray*>(&value.p); }
372 
374  const FXVariantArray& asArray() const { return *reinterpret_cast<const FXVariantArray*>(&value.p); }
375 
377  FXVariantMap& asMap(){ return *reinterpret_cast<FXVariantMap*>(&value.p); }
378 
380  const FXVariantMap& asMap() const { return *reinterpret_cast<const FXVariantMap*>(&value.p); }
381 
383  void clear();
384 
386  void reset();
387 
389  static const FXVariant null;
390 
392  ~FXVariant();
393  };
394 
395 }
396 
397 #endif

Copyright © 1997-2016 Jeroen van der Zijp