KVIrc  4.9.2
DeveloperAPIs
KviKvsVariant.h
Go to the documentation of this file.
1 #ifndef _KVI_KVS_VARIANT_H_
2 #define _KVI_KVS_VARIANT_H_
3 //=============================================================================
4 //
5 // File : KviKvsVariant.h
6 // Creation date : Tue 07 Oct 2003 04:01:19 by Szymon Stefanek
7 //
8 // This file is part of the KVIrc IRC client distribution
9 // Copyright (C) 2003-2010 Szymon Stefanek <pragma at kvirc dot net>
10 //
11 // This program is FREE software. You can redistribute it and/or
12 // modify it under the terms of the GNU General Public License
13 // as published by the Free Software Foundation; either version 2
14 // of the License, or (at your option) any later version.
15 //
16 // This program is distributed in the HOPE that it will be USEFUL,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 // See the GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, write to the Free Software Foundation,
23 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
33 #include "kvi_settings.h"
34 #include "KviQString.h"
35 #include "KviHeapObject.h"
36 #include "KviKvsTypes.h"
37 
38 class KviKvsVariant;
39 class KviKvsArrayCast;
40 
41 // pre-declare.. the real declarations are included below
42 class KviKvsHash;
43 class KviKvsArray;
44 
50 {
51  friend class KviKvsVariant;
52 
53 public:
58  enum Type
59  {
60  Real,
61  Integer
62  };
63 
64 protected:
69  union DataType {
72  };
73 
74 protected:
77 
78 public:
83  Type type() const { return m_type; };
84 
89  bool isReal() const { return m_type == Real; };
90 
95  bool isInteger() const { return m_type == Integer; };
96 
101  kvs_real_t real() const { return m_u.dReal; };
102 
107  kvs_int_t integer() const { return m_u.iInt; };
108 };
109 
115 {
116 public:
121  enum Type
122  {
123  Nothing = 0,
124  String = 1,
125  Integer = 2,
126  Real = 4,
127  Array = 8,
128  Hash = 16,
129  Boolean = 32,
130  HObject = 64
131  };
132 
133 public:
138  union DataType {
141  QString * pString;
144  bool bBoolean;
146  };
147 
148 public:
149  unsigned int m_uRefs;
152 };
153 
159 {
160 public:
165  enum Result
166  {
168  Equal = 0,
170  };
171 
172 public:
179  static inline int compareIntString(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
180 
187  static inline int compareIntReal(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
188 
195  static inline int compareIntBool(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
196 
203  static inline int compareIntHash(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
204 
211  static inline int compareIntArray(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
212 
219  static inline int compareIntHObject(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
220 
227  static inline int compareRealHObject(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
228 
235  static inline int compareRealString(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
236 
243  static inline int compareRealBool(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
244 
251  static inline int compareRealHash(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
252 
259  static inline int compareRealArray(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
260 
267  static inline int compareStringHash(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
268 
275  static inline int compareStringArray(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
276 
283  static inline int compareStringHObject(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
284 
291  static inline int compareBoolString(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
292 
299  static inline int compareBoolHash(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
300 
307  static inline int compareBoolArray(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
308 
315  static inline int compareBoolHObject(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
316 
323  static inline int compareArrayHash(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
324 
331  static inline int compareHObjectHash(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
332 
339  static inline int compareHObjectArray(const KviKvsVariant * pV1, const KviKvsVariant * pV2);
340 };
341 
352 {
354 
355 public:
360  KviKvsVariant();
361 
369  KviKvsVariant(const KviKvsVariant & variant);
370 
377  KviKvsVariant(const QString & szString, bool bEscape = false);
378 
387  KviKvsVariant(const char * pcString, bool bEscape = false);
388 
395  KviKvsVariant(QString * pszString, bool bEscape = false);
396 
403  KviKvsVariant(kvs_int_t iInteger, bool bEscape = false);
404 
410  KviKvsVariant(kvs_real_t dReal);
411 
417  KviKvsVariant(kvs_real_t * pReal);
418 
424  KviKvsVariant(bool bBoolean);
425 
432 
438  KviKvsVariant(KviKvsHash * pHash);
439 
445  KviKvsVariant(kvs_hobject_t hObject);
446 
450  ~KviKvsVariant();
451 
452 protected:
454 
455 public:
460  KviKvsVariantData::Type type() { return m_pData ? m_pData->m_eType : KviKvsVariantData::Nothing; };
461 
467  void setReal(kvs_real_t dReal);
468 
474  void setReal(kvs_real_t * pReal);
475 
481  void setInteger(kvs_int_t iInteger);
482 
488  void setString(const QString & szString);
489 
495  void setString(QString * pszString);
496 
502  void setArray(KviKvsArray * pArray);
503 
509  void setHash(KviKvsHash * pHash);
510 
516  void setBoolean(bool bBoolean);
517 
523  void setHObject(kvs_hobject_t hObject);
524 
529  void setNothing();
530 
536  void getTypeName(QString & szBuffer) const;
537 
542  bool isNothing() const { return !m_pData; };
543 
548  bool isInteger() const { return m_pData ? (m_pData->m_eType == KviKvsVariantData::Integer) : false; };
549 
554  bool isReal() const { return m_pData ? (m_pData->m_eType == KviKvsVariantData::Real) : false; };
555 
560  bool isNumeric() const { return m_pData ? (m_pData->m_eType & (KviKvsVariantData::Integer | KviKvsVariantData::Real)) : false; };
561 
566  bool isString() const { return m_pData ? (m_pData->m_eType == KviKvsVariantData::String) : false; };
567 
572  bool isScalar() const { return m_pData ? (m_pData->m_eType & (KviKvsVariantData::String | KviKvsVariantData::Integer | KviKvsVariantData::Real)) : false; };
573 
578  bool isArray() const { return m_pData ? (m_pData->m_eType == KviKvsVariantData::Array) : false; };
579 
584  bool isHash() const { return m_pData ? (m_pData->m_eType == KviKvsVariantData::Hash) : false; };
585 
590  bool isBoolean() const { return m_pData ? (m_pData->m_eType == KviKvsVariantData::Boolean) : false; };
591 
596  bool isHObject() const { return m_pData ? (m_pData->m_eType == KviKvsVariantData::HObject) : false; };
597 
602  bool isEmpty() const;
603 
608  bool isEqualToNothing() const;
609 
615  bool asInteger(kvs_int_t & iVal) const;
616 
622  bool asReal(kvs_real_t & dVal) const;
623 
629  bool asNumber(KviKvsNumber & n) const;
630 
635  bool asBoolean() const;
636 
642  bool asHObject(kvs_hobject_t & hObject) const;
643 
649  void asString(QString & szBuffer) const;
650 
656  void appendAsString(QString & szBuffer) const;
657 
665  void castToNumber(KviKvsNumber & number) const;
666 
674  void castToInteger(kvs_int_t & iVal) const;
675 
683  void castToArray(KviKvsArrayCast * pCast) const;
684 
689  void convertToArray();
690 
695  kvs_int_t integer() const { return m_pData ? m_pData->m_u.iInt : 0; };
696 
701  kvs_real_t real() const { return m_pData ? *(m_pData->m_u.pReal) : 0.0; };
702 
707  const QString & string() const { return m_pData ? *(m_pData->m_u.pString) : KviQString::Empty; };
708 
713  bool boolean() const { return m_pData ? m_pData->m_u.bBoolean : false; };
714 
719  KviKvsArray * array() const { return m_pData ? m_pData->m_u.pArray : 0; };
720 
725  KviKvsHash * hash() const { return m_pData ? m_pData->m_u.pHash : 0; };
726 
731  kvs_hobject_t hobject() const { return m_pData ? m_pData->m_u.hObject : (kvs_hobject_t)0; };
732 
738  void copyFrom(const KviKvsVariant * pVariant);
739 
745  void copyFrom(const KviKvsVariant & variant);
746 
752  void takeFrom(KviKvsVariant * pVariant);
753 
759  void takeFrom(KviKvsVariant & variant);
760 
766  void dump(const char * pcPrefix) const;
767 
778  int compare(const KviKvsVariant * pOther, bool bPreferNumeric = false) const;
779 
780  // JSON serialization
786  void serialize(QString & szResult);
787 
793  static void serializeString(QString & szBuffer);
794 
800  static KviKvsVariant * unserialize(const QString & szBuffer);
801 
807  void operator=(const KviKvsVariant & variant) { copyFrom(variant); };
808 private:
814  static KviKvsVariant * unserialize(const QChar ** ppAux);
815 
822  static KviKvsVariant * unserializeBool(const QChar ** ppAux, bool bBool);
823 
829  static KviKvsVariant * unserializeNull(const QChar ** ppAux);
830 
836  static KviKvsVariant * unserializeArray(const QChar ** ppAux);
837 
843  static KviKvsVariant * unserializeHash(const QChar ** ppAux);
844 
851  static void unserializeString(const QChar ** ppAux, QString & szBuffer);
852 
858  static KviKvsVariant * unserializeString(const QChar ** ppAux);
859 
865  static KviKvsVariant * unserializeNumber(const QChar ** ppAux);
866 
873  static KviKvsVariant * unserializeReal(const QChar ** ppAux, QString & szData);
874 
881  static KviKvsVariant * unserializeInteger(const QChar ** ppAux, QString & szData);
882 };
883 
884 #include "KviKvsArray.h"
885 #include "KviKvsHash.h"
886 
887 #endif // _KVI_KVS_VARIANT_H_
static int compareRealHash(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a double floating point and a hash variant.
Definition: KviKvsVariant.cpp:134
static int compareIntArray(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an integer and an array variant.
Definition: KviKvsVariant.cpp:81
Definition: KviKvsVariant.h:123
Definition: KviHeapObject.h:124
KviKvsVariantData::Type type()
Returns the type of the variant data.
Definition: KviKvsVariant.h:460
The class which holds the type of the variant data.
Definition: KviKvsVariant.h:114
Definition: KviKvsVariant.h:60
Holds the value of the variant data.
Definition: KviKvsVariant.h:138
bool isReal() const
Returns true if the number is a double floating point.
Definition: KviKvsVariant.h:89
This class defines a new data type which contains variant data.
Definition: KviKvsVariant.h:351
bool isNumeric() const
Returns true if the variant is numeric.
Definition: KviKvsVariant.h:560
#define KVIRC_API
Definition: kvi_settings.h:128
Definition: KviKvsVariant.h:130
const QString & string() const
Returns the string contained in the variant data.
Definition: KviKvsVariant.h:707
kvs_hobject_t hObject
Definition: KviKvsVariant.h:145
Definition: KviKvsVariant.h:167
bool isBoolean() const
Returns true if the variant is boolean.
Definition: KviKvsVariant.h:590
Definition: KviKvsVariant.h:169
static int compareArrayHash(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an array and a hash variant.
Definition: KviKvsVariant.cpp:217
Type m_eType
Definition: KviKvsVariant.h:150
Definition: KviKvsVariant.h:129
kvs_int_t integer() const
Returns the integer contained in the variant data.
Definition: KviKvsVariant.h:695
DataType m_u
Definition: KviKvsVariant.h:76
bool boolean() const
Returns the boolean contained in the variant data.
Definition: KviKvsVariant.h:713
KviKvsVariantData * m_pData
Definition: KviKvsVariant.h:453
static int compareHObjectHash(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an object handle and a hash variant.
Definition: KviKvsVariant.cpp:226
kvs_real_t real() const
Returns the double floating point contained in the variant data.
Definition: KviKvsVariant.h:701
kvs_int_t integer() const
Return the integer number if the number itself is an integer.
Definition: KviKvsVariant.h:107
unsigned int m_uRefs
Definition: KviKvsVariant.h:149
static int compareIntReal(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an integer and a double floating point variant.
Definition: KviKvsVariant.cpp:58
static int compareBoolHObject(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a boolean and an object handle variant.
Definition: KviKvsVariant.cpp:209
#define n
Definition: detector.cpp:78
bool isHObject() const
Returns true if the variant is a hObject.
Definition: KviKvsVariant.h:596
kvs_int_t iInt
Definition: KviKvsVariant.h:139
void operator=(const KviKvsVariant &variant)
Allows to create a variant using a carbon copy method.
Definition: KviKvsVariant.h:807
KviKvsArray * pArray
Definition: libkviserverdb.cpp:476
static int compareRealBool(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a double floating point and a boolean variant.
Definition: KviKvsVariant.cpp:127
Type m_type
Definition: KviKvsVariant.h:75
KviKvsHash * hash() const
Returns the hash contained in the variant data.
Definition: KviKvsVariant.h:725
Handling of array data type in KVS.
static int compareRealHObject(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a double floating point and an object handle variant.
Definition: KviKvsVariant.cpp:95
This class defines a new data type which contains hash data.
Definition: KviKvsHash.h:47
kvs_real_t * pReal
Definition: KviKvsVariant.h:140
bool isNothing() const
Returns true if the variant is empty.
Definition: KviKvsVariant.h:542
kvs_real_t real() const
Return the real number if the number itself is a double floating point.
Definition: KviKvsVariant.h:101
static int compareStringHObject(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a string and an object handle variant.
Definition: KviKvsVariant.cpp:166
static int compareIntBool(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an integer and a boolean variant.
Definition: KviKvsVariant.cpp:67
QString * pString
Definition: KviKvsVariant.h:141
bool isHash() const
Returns true if the variant is an hash.
Definition: KviKvsVariant.h:584
Definition: KviKvsVariant.h:126
Definition: KviKvsVariant.h:124
Type
Holds the type of the variant data.
Definition: KviKvsVariant.h:121
const QString Empty
A global empty string (note that this is ALSO NULL under Qt 3.x)
Definition: KviQString.cpp:49
Type type() const
Returns the type of the number.
Definition: KviKvsVariant.h:83
Type
Describes the type of number.
Definition: KviKvsVariant.h:58
bool isScalar() const
Returns true if the variant is a scalar.
Definition: KviKvsVariant.h:572
bool isReal() const
Returns true if the variant is a double floating point.
Definition: KviKvsVariant.h:554
Definition: KviKvsVariant.h:168
static int compareIntString(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an integer and a string variant.
Definition: KviKvsVariant.cpp:33
DataType m_u
Definition: KviKvsVariant.h:151
static int compareIntHash(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an integer and a hash variant.
Definition: KviKvsVariant.cpp:74
bool isString() const
Returns true if the variant is a string.
Definition: KviKvsVariant.h:566
static int compareStringHash(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a string and a hash variant.
Definition: KviKvsVariant.cpp:148
double kvs_real_t
Definition: KviKvsTypes.h:35
kvs_int_t iInt
Definition: KviKvsVariant.h:70
KviKvsArray * array() const
Returns the array contained in the variant data.
Definition: KviKvsVariant.h:719
static int compareHObjectArray(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an object handle and an array variant.
Definition: KviKvsVariant.cpp:233
bool bBoolean
Definition: KviKvsVariant.h:144
Heap Object.
static int compareRealString(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a double floating point and a string variant.
Definition: KviKvsVariant.cpp:102
Definition: KviKvsVariant.h:128
Definition: KviKvsVariant.h:127
Result
Holds the result of the comparison.
Definition: KviKvsVariant.h:165
static int compareRealArray(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a double floating point and an array variant.
Definition: KviKvsVariant.cpp:141
KviKvsArray * pArray
Definition: KviKvsVariant.h:142
A class which maps every number.
Definition: KviKvsVariant.h:49
Definition: KviKvsVariant.h:125
kvs_real_t dReal
Definition: KviKvsVariant.h:71
This class defines a new data type which contains array data.
Definition: KviKvsArray.h:43
static int compareBoolString(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a boolean and a string variant.
Definition: KviKvsVariant.cpp:185
void * kvs_hobject_t
Definition: KviKvsTypes.h:32
static int compareIntHObject(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares an integer and an object handle variant.
Definition: KviKvsVariant.cpp:88
bool isArray() const
Returns true if the variant is an array.
Definition: KviKvsVariant.h:578
char szBuffer[4096]
Definition: winamp.cpp:77
bool isInteger() const
Returns true if the variant is an integer.
Definition: KviKvsVariant.h:548
static int compareBoolArray(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a boolean and an array variant.
Definition: KviKvsVariant.cpp:201
Handling of hash data type in KVS.
This file contains compile time settings.
A class for comparison between two variant data.
Definition: KviKvsVariant.h:158
static int compareStringArray(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a string and an array variant.
Definition: KviKvsVariant.cpp:157
kvi_i64_t kvs_int_t
Definition: KviKvsTypes.h:33
static int compareBoolHash(const KviKvsVariant *pV1, const KviKvsVariant *pV2)
Compares a boolean and a hash variant.
Definition: KviKvsVariant.cpp:193
KviKvsHash * pHash
Definition: KviKvsVariant.h:143
Helper functions for the QString class.
This union holds an integer or a double floating point number.
Definition: KviKvsVariant.h:69
kvs_hobject_t hobject() const
Returns the object handle contained in the variant data.
Definition: KviKvsVariant.h:731
bool isInteger() const
Returns true if the number is an integer.
Definition: KviKvsVariant.h:95
Definition: KviKvsArrayCast.h:30