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,2022 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 Type {
47  NullType=0, // Simple types
48  BoolType,
49  CharType,
50  IntType,
51  UIntType,
52  LongType,
53  ULongType,
54  FloatType,
55  DoubleType,
56  PointerType,
57  StringType, // Complex types
58  ArrayType,
59  MapType
60  };
61 private:
62  union Value {
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  Value value; // Current value
71  Type type; // Type of value
72 private:
73  FXbool init(Type t);
74 public:
75 
77  FXVariant();
78 
80  FXVariant(const FXVariant& other);
81 
83  explicit FXVariant(FXbool val);
84 
86  explicit FXVariant(FXchar val);
87 
89  explicit FXVariant(FXint val);
90 
92  explicit FXVariant(FXuint val);
93 
95  explicit FXVariant(FXlong val);
96 
98  explicit FXVariant(FXulong val);
99 
101  explicit FXVariant(FXfloat val);
102 
104  explicit FXVariant(FXdouble val);
105 
107  explicit FXVariant(FXptr val);
108 
110  explicit FXVariant(const FXchar *val);
111 
113  explicit FXVariant(const FXString& val);
114 
116  void setType(Type t);
117 
119  Type getType() const { return type; }
120 
122  FXival no() const;
123 
125  FXbool no(FXival n);
126 
128  FXbool isNull() const { return type==NullType; }
129 
131  FXbool isBool() const { return type==BoolType; }
132 
134  FXbool isChar() const { return type==CharType; }
135 
137  FXbool isInt() const { return type==IntType; }
138 
140  FXbool isUInt() const { return type==UIntType; }
141 
143  FXbool isLong() const { return type==LongType; }
144 
146  FXbool isULong() const { return type==ULongType; }
147 
149  FXbool isFloat() const { return type==FloatType; }
150 
152  FXbool isDouble() const { return type==DoubleType; }
153 
155  FXbool isInteger() const { return BoolType<=type && type<=ULongType; }
156 
158  FXbool isReal() const { return FloatType<=type && type<=DoubleType; }
159 
161  FXbool isNumber() const { return BoolType<=type && type<=DoubleType; }
162 
164  FXbool isPtr() const { return type==PointerType; }
165 
167  FXbool isString() const { return type==StringType; }
168 
170  FXbool isArray() const { return type==ArrayType; }
171 
173  FXbool isMap() const { return type==MapType; }
174 
176  FXbool toBool() const;
177 
179  FXptr toPtr() const;
180 
182  FXint toInt(FXbool* ok=nullptr) const;
183 
185  FXuint toUInt(FXbool* ok=nullptr) const;
186 
188  FXlong toLong(FXbool* ok=nullptr) const;
189 
191  FXulong toULong(FXbool* ok=nullptr) const;
192 
194  FXfloat toFloat(FXbool* ok=nullptr) const;
195 
197  FXdouble toDouble(FXbool* ok=nullptr) const;
198 
200  const FXchar* toChars() const;
201 
203  FXString toString(FXbool* ok=nullptr) const;
204 
206  operator FXbool() const { return toBool(); }
207 
209  operator FXptr() const { return toPtr(); }
210 
212  operator FXchar() const { return (FXchar)toInt(); }
213 
215  operator FXuchar() const { return (FXuchar)toUInt(); }
216 
218  operator FXshort() const { return (FXshort)toInt(); }
219 
221  operator FXushort() const { return (FXushort)toUInt(); }
222 
224  operator FXint() const { return toInt(); }
225 
227  operator FXuint() const { return toUInt(); }
228 
230  operator FXlong() const { return toLong(); }
231 
233  operator FXulong() const { return toULong(); }
234 
236  operator FXfloat() const { return toFloat(); }
237 
239  operator FXdouble() const { return toDouble(); }
240 
242  operator FXString() const { return toString(); }
243 
245  FXVariant& operator=(FXbool val);
246 
248  FXVariant& operator=(FXchar val);
249 
251  FXVariant& operator=(FXint val);
252 
254  FXVariant& operator=(FXuint val);
255 
257  FXVariant& operator=(FXlong val);
258 
260  FXVariant& operator=(FXulong val);
261 
263  FXVariant& operator=(FXfloat val);
264 
266  FXVariant& operator=(FXdouble val);
267 
269  FXVariant& operator=(FXptr val);
270 
272  FXVariant& operator=(const FXchar* val);
273 
275  FXVariant& operator=(const FXString& val);
276 
278  FXVariant& operator=(const FXVariant& val);
279 
281  FXVariant& assign(const FXVariant& other);
282 
284  FXVariant& adopt(FXVariant& other);
285 
287  FXVariant& at(const FXchar* key);
288 
290  const FXVariant& at(const FXchar* key) const;
291 
293  FXVariant& operator[](const FXchar* key){ return at(key); }
294 
296  const FXVariant& operator[](const FXchar* key) const { return at(key); }
297 
299  FXVariant& at(const FXString& key);
300 
302  const FXVariant& at(const FXString& key) const;
303 
305  FXVariant& operator[](const FXString& key){ return at(key); }
306 
308  const FXVariant& operator[](const FXString& key) const { return at(key); }
309 
311  FXVariant& at(FXival idx);
312 
314  const FXVariant& at(FXival idx) const;
315 
317  FXVariant& operator[](FXint idx){ return at(idx); }
318  const FXVariant& operator[](FXint idx) const { return at(idx); }
319 
321  FXVariant& operator[](FXival idx){ return at(idx); }
322  const FXVariant& operator[](FXival idx) const { return at(idx); }
323 
325  FXbool has(const FXchar* key) const;
326 
328  FXbool has(const FXString& key) const { return has(key.text()); }
329 
331  FXptr& asPtr(){ return value.p; }
332 
334  const FXptr& asPtr() const { return value.p; }
335 
337  FXlong& asLong(){ return value.i; }
338 
340  const FXlong& asLong() const { return value.i; }
341 
343  FXulong& asULong(){ return value.u; }
344 
346  const FXulong& asULong() const { return value.u; }
347 
349  FXdouble& asDouble(){ return value.d; }
350 
352  const FXdouble& asDouble() const { return value.d; }
353 
355  const FXchar* asChars() const { return value.s; }
356 
358  FXString& asString(){ return *reinterpret_cast<FXString*>(&value.p); }
359 
361  const FXString& asString() const { return *reinterpret_cast<const FXString*>(&value.p); }
362 
364  FXVariantArray& asArray(){ return *reinterpret_cast<FXVariantArray*>(&value.p); }
365 
367  const FXVariantArray& asArray() const { return *reinterpret_cast<const FXVariantArray*>(&value.p); }
368 
370  FXVariantMap& asMap(){ return *reinterpret_cast<FXVariantMap*>(&value.p); }
371 
373  const FXVariantMap& asMap() const { return *reinterpret_cast<const FXVariantMap*>(&value.p); }
374 
376  FXbool remove(const FXchar* key);
377 
379  FXbool remove(const FXString& key){ return remove(key.text()); }
380 
382  FXbool erase(FXival idx);
383 
385  FXbool clear();
386 
388  static const FXVariant null;
389 
391  ~FXVariant();
392  };
393 
394 }
395 
396 #endif
const FXdouble & asDouble() const
Return the value of the variant as a double; variant type MUST be DoubleType.
Definition: FXVariant.h:352
const FXulong & asULong() const
Return the value of the variant as an unsigned long; variant type MUST be ULongType.
Definition: FXVariant.h:346
FXVariantArray & asArray()
Return the value of the variant as an array-reference; variant type MUST be ArrayType.
Definition: FXVariant.h:364
FXbool isReal() const
Is it a real (float or double)?
Definition: FXVariant.h:158
FXdouble & asDouble()
Return the value of the variant as a double; variant type MUST be DoubleType.
Definition: FXVariant.h:349
FXbool isFloat() const
Is it a float?
Definition: FXVariant.h:149
FXbool isInt() const
Is it a int?
Definition: FXVariant.h:137
FXbool isLong() const
Is it a long?
Definition: FXVariant.h:143
A Variant type can hold any kind of object, be it a boolean, integer, real, string, or even array of Variants or dictionaries of variants.
Definition: FXVariant.h:44
FXulong & asULong()
Return the value of the variant as an unsigned long; variant type MUST be ULongType.
Definition: FXVariant.h:343
FXbool isInteger() const
Is it a integer (bool, char, ..., or long)?
Definition: FXVariant.h:155
FXbool isUInt() const
Is it a unsigned int?
Definition: FXVariant.h:140
const FXString & asString() const
Return the value of the variant as a const string-reference; variant type MUST be StringType...
Definition: FXVariant.h:361
FXString & asString()
Return the value of the variant as a string-reference; variant type MUST be StringType.
Definition: FXVariant.h:358
const FXVariantArray & asArray() const
Return the value of the variant as a const array-reference; variant type MUST be ArrayType.
Definition: FXVariant.h:367
const FXVariantMap & asMap() const
Return the value of the variant as a const map-reference; variant type MUST be MapType.
Definition: FXVariant.h:373
FXbool isChar() const
Is it a character?
Definition: FXVariant.h:134
FXbool isNumber() const
Is it any kind of number?
Definition: FXVariant.h:161
FXchar * text()
Get text contents as pointer.
Definition: FXString.h:119
const FXchar * asChars() const
Return the value of the variant as a char pointer; variant type MUST be StringType.
Definition: FXVariant.h:355
FXbool isBool() const
Is it a bool?
Definition: FXVariant.h:131
FXbool isString() const
Is it a string?
Definition: FXVariant.h:167
FXVariant & operator[](FXint idx)
Return value of array member.
Definition: FXVariant.h:317
FXbool isDouble() const
Is it a double?
Definition: FXVariant.h:152
FXlong & asLong()
Return the value of the variant as a long; variant type MUST be LongType.
Definition: FXVariant.h:337
Definition: FX4Splitter.h:28
FXVariant & operator[](const FXString &key)
Return value of object member.
Definition: FXVariant.h:305
FXbool isMap() const
Is it a map?
Definition: FXVariant.h:173
FXbool isULong() const
Is it a unsigned long?
Definition: FXVariant.h:146
FXptr & asPtr()
Return the value of the variant as a pointer; variant type MUST be PointerType.
Definition: FXVariant.h:331
static const FXVariant null
Default constant variant.
Definition: FXVariant.h:388
FXVariantMap & asMap()
Return the value of the variant as an map-reference; variant type MUST be MapType.
Definition: FXVariant.h:370
FXbool isNull() const
Is it a null?
Definition: FXVariant.h:128
FXVariant & operator[](FXival idx)
Return value of array member.
Definition: FXVariant.h:321
const FXptr & asPtr() const
Return the value of the variant as a pointer; variant type MUST be PointerType.
Definition: FXVariant.h:334
const FXlong & asLong() const
Return the value of the variant as a long; variant type MUST be LongType.
Definition: FXVariant.h:340
const FXVariant & operator[](const FXString &key) const
Return value of object member.
Definition: FXVariant.h:308
Variant map associates strings to variants using fast hash table.
Definition: FXVariantMap.h:31
FXbool isArray() const
Is it a array?
Definition: FXVariant.h:170
FXbool has(const FXString &key) const
Check if key is mapped.
Definition: FXVariant.h:328
Type getType() const
Return type.
Definition: FXVariant.h:119
FXVariant & operator[](const FXchar *key)
Return value of object member.
Definition: FXVariant.h:293
Array of variants.
Definition: FXVariantArray.h:28
const FXVariant & operator[](const FXchar *key) const
Return value of object member.
Definition: FXVariant.h:296
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42
FXbool isPtr() const
Is it a pointer?
Definition: FXVariant.h:164

Copyright © 1997-2022 Jeroen van der Zijp