libcommonc++  0.7
Variant.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Variant_hxx
24 #define __ccxx_Variant_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/Blob.h++>
28 #include <commonc++/String.h++>
29 
30 #include <sys/types.h>
31 #include <iostream>
32 
33 namespace ccxx {
34 
47 {
48  public:
49 
51  enum Type {
53  TypeNull = 0,
81  TypeMap
82  };
83 
85  Variant();
86 
88  Variant(const Variant& other);
89 
95  explicit Variant(bool val);
96 
102  explicit Variant(char val);
103 
109  explicit Variant(char16_t val);
110 
116  explicit Variant(int32_t val);
117 
123  explicit Variant(uint32_t val);
124 
130  explicit Variant(float val);
131 
137  explicit Variant(const double& val);
138 
144  explicit Variant(const int64_t& val);
145 
151  explicit Variant(const uint64_t& val);
152 
158  explicit Variant(const String& val);
159 
165  explicit Variant(const char* val);
166 
172  explicit Variant(const char16_t* val);
173 
179  explicit Variant(const Blob& val);
180 
186  explicit Variant(void* val);
187 
189  virtual ~Variant();
190 
192  inline Type getType() const
193  { return(_type); }
194 
201  void setType(Type type);
202 
204  inline bool isNull() const
205  { return(_type == TypeNull); }
206 
208  inline bool isBool() const
209  { return(_type == TypeBool); }
210 
212  inline bool isChar() const
213  { return(_type == TypeChar); }
214 
216  inline bool isChar16() const
217  { return(_type == TypeChar16); }
218 
220  inline bool isInt() const
221  { return(_type == TypeInt); }
222 
224  inline bool isUInt() const
225  { return(_type == TypeUInt); }
226 
228  inline bool isFloat() const
229  { return(_type == TypeFloat); }
230 
232  inline bool isDouble() const
233  { return(_type == TypeDouble); }
234 
236  inline bool isInt64() const
237  { return(_type == TypeInt64); }
238 
240  inline bool isUInt64() const
241  { return(_type == TypeUInt64); }
242 
244  inline bool isString() const
245  { return(_type == TypeString); }
246 
248  inline bool isBlob() const
249  { return(_type == TypeBlob); }
250 
252  inline bool isOpaque() const
253  { return(_type == TypeOpaque); }
254 
256  inline bool isList() const
257  { return(_type == TypeList); }
258 
260  inline bool isMap() const
261  { return(_type == TypeMap); }
262 
268  bool toBool() const;
269 
275  char toChar() const;
276 
282  char16_t toChar16() const;
283 
289  int32_t toInt() const;
290 
296  uint32_t toUInt() const;
297 
303  float toFloat() const;
304 
310  double toDouble() const;
311 
317  int64_t toInt64() const;
318 
324  uint64_t toUInt64() const;
325 
330  void* toOpaque() const;
331 
337  String toString() const;
338 
344  Blob toBlob() const;
345 
347  inline operator bool() const
348  { return(toBool()); }
349 
351  inline operator char() const
352  { return(toChar()); }
353 
355  inline operator char16_t() const
356  { return(toChar16()); }
357 
359  inline operator int32_t() const
360  { return(toInt()); }
361 
363  inline operator uint32_t() const
364  { return(toUInt()); }
365 
367  inline operator float() const
368  { return(toFloat()); }
369 
371  inline operator double() const
372  { return(toDouble()); }
373 
375  inline operator String() const
376  { return(toString()); }
377 
379  inline operator Blob() const
380  { return(toBlob()); }
381 
383  inline operator int64_t() const
384  { return(toInt64()); }
385 
387  inline operator uint64_t() const
388  { return(toUInt64()); }
389 
394  inline operator const void*() const
395  { return(isNull() ? NULL : this); }
396 
398  inline bool operator!() const
399  { return(isNull()); }
400 
406  uint_t length() const;
407 
412  void clear();
413 
415  void setNull();
416 
418  Variant& operator=(bool val);
419 
421  Variant& operator=(char val);
422 
424  Variant& operator=(char16_t val);
425 
427  Variant& operator=(int32_t val);
428 
430  Variant& operator=(uint32_t val);
431 
433  Variant& operator=(float val);
434 
436  Variant& operator=(const double& val);
437 
439  Variant& operator=(const int64_t& val);
440 
442  Variant& operator=(const uint64_t& val);
443 
445  Variant& operator=(const String& val);
446 
448  inline Variant& operator=(const char* val)
449  { return(operator=(String(val))); }
450 
452  Variant& operator=(const Blob& val);
453 
455  Variant& operator=(void* val);
456 
458  Variant& operator=(const Variant& other);
459 
470  Variant& operator[](const String& name);
471 
482  inline Variant& operator[](const char* name)
483  { return(operator[](String(name))); }
484 
493  const Variant &get(const String& name) const;
494 
503  inline const Variant &operator[](const String& name) const
504  { return(get(name)); }
505 
514  inline const Variant &operator[](const char* name) const
515  { return(get(String(name))); }
516 
529  Variant& operator[](int index);
530 
540  const Variant &get(int index) const;
541 
551  inline const Variant &operator[](int index) const
552  { return(get(index)); }
553 
561  bool getKeys(StringVec& vec) const;
562 
570  bool hasKey(const String& key) const;
571 
573  bool operator==(const Variant& other) const;
574 
576  inline bool operator!=(const Variant& other) const
577  { return(! operator==(other)); }
578 
584  void write(std::ostream &stream) const;
585 
587  static const Variant null;
588 
589  private:
590 
591  class VariantList; // fwd decl
592  class VariantMap; // fwd decl
593 
594  void _copy(const Variant& other);
595  void _deleteValue();
596  void _initValue();
597 
598  Type _type;
599 
600  union {
601  bool boolVal;
602  char charVal;
604  int32_t intVal;
605  uint32_t uintVal;
606  float floatVal;
607  double doubleVal;
608  int64_t int64Val;
609  uint64_t uint64Val;
612  VariantList* listVal;
613  VariantMap* mapVal;
614  void* opaqueVal;
615  } _value;
616 
617 };
618 
625 inline std::ostream& operator<<(std::ostream& stream, const Variant& v) {
626  v.write(stream);
627  return(stream);
628 }
629 
630 } // namespace ccxx
631 
632 #endif // __ccxx_Variant_hxx
bool isDouble() const
Test if the Variant&#39;s value is a double.
Definition: Variant.h++:232
double value
Definition: Variant.h++:67
char16_t char16Val
Definition: Variant.h++:603
String value.
Definition: Variant.h++:73
unsigned int value
Definition: Variant.h++:63
float floatVal
Definition: Variant.h++:606
const Variant & operator[](int index) const
Return the value at the given index from this Variant, whose value is assumed to be a list...
Definition: Variant.h++:551
list value
Definition: Variant.h++:79
bool isChar16() const
Test if the Variant&#39;s value is a char16_t.
Definition: Variant.h++:216
A String vector.
Definition: String.h++:1159
Type getType() const
Get the type of this Variant.
Definition: Variant.h++:192
VariantMap * mapVal
Definition: Variant.h++:613
char16_t value
Definition: Variant.h++:59
opaque pointer value
Definition: Variant.h++:77
bool isString() const
Test if the Variant&#39;s value is a String.
Definition: Variant.h++:244
void write(std::ostream &stream) const
Write the value of this Variant to a stream.
Definition: Variant.c++:1255
bool isMap() const
Test if the Variant&#39;s value is a map.
Definition: Variant.h++:260
bool operator==(const Blob &b1, const Blob &b2)
Definition: Blob.h++:344
int64_t int64Val
Definition: Variant.h++:608
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
bool boolVal
Definition: Variant.h++:601
void * opaqueVal
Definition: Variant.h++:614
bool isFloat() const
Test if the Variant&#39;s value is a float.
Definition: Variant.h++:228
VariantList * listVal
Definition: Variant.h++:612
bool operator!=(const Variant &other) const
Inequality operator.
Definition: Variant.h++:576
Blob value.
Definition: Variant.h++:75
String * stringVal
Definition: Variant.h++:610
const Variant & operator[](const String &name) const
Return the value for the given name from this Variant, whose value is assumed to be a map...
Definition: Variant.h++:503
Blob * blobVal
Definition: Variant.h++:611
bool operator!() const
Equivalent to isNull().
Definition: Variant.h++:398
char value
Definition: Variant.h++:57
uint64_t uint64Val
Definition: Variant.h++:609
std::ostream & operator<<(std::ostream &stream, const BitSet &bs)
Definition: BitSet.h++:383
const Variant & operator[](const char *name) const
Return the value for the given name from this Variant, whose value is assumed to be a map...
Definition: Variant.h++:514
bool isBool() const
Test if the Variant&#39;s value is a bool.
Definition: Variant.h++:208
int32_t intVal
Definition: Variant.h++:604
A general-purpose variant type.
Definition: Variant.h++:46
#define COMMONCPP_API
Definition: Common.h++:126
int value
Definition: Variant.h++:61
A reference-counting, copy-on-write, threadsafe container for arbitrary binary data.
Definition: Blob.h++:45
64-bit unsigned int value
Definition: Variant.h++:71
static const Variant null
A null Variant.
Definition: Variant.h++:587
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
bool isList() const
Test if the Variant&#39;s value is a list.
Definition: Variant.h++:256
char charVal
Definition: Variant.h++:602
bool isBlob() const
Test if the Variant&#39;s value is a Blob.
Definition: Variant.h++:248
Variant & operator[](const char *name)
Return the value for the given name from this Variant, whose value is assumed to be a map...
Definition: Variant.h++:482
bool isInt64() const
Test if the Variant&#39;s value is an int64_t.
Definition: Variant.h++:236
bool isChar() const
Test if the Variant&#39;s value is a char.
Definition: Variant.h++:212
uint16_t char16_t
A UTF-16 character.
Definition: Common.h++:201
float value
Definition: Variant.h++:65
64-bit int value
Definition: Variant.h++:69
double doubleVal
Definition: Variant.h++:607
uint32_t uintVal
Definition: Variant.h++:605
bool isNull() const
Test if the Variant is null.
Definition: Variant.h++:204
bool value
Definition: Variant.h++:55
bool isOpaque() const
Test if the Variant&#39;s value is opaque.
Definition: Variant.h++:252
Definition: AllocationMap.c++:25
bool isUInt64() const
Test if the Variant&#39;s value is a uint64_t.
Definition: Variant.h++:240
Variant & operator=(const char *val)
Assign a string value to the Variant.
Definition: Variant.h++:448
bool isUInt() const
Test if the Variant&#39;s value is a uint32_t.
Definition: Variant.h++:224
bool isInt() const
Test if the Variant&#39;s value is an int32_t.
Definition: Variant.h++:220
Type
Variant value types.
Definition: Variant.h++:51