Mobile API Reference  MicroStrategy 2019
Variant.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : Variant.h
3 // AUTHOR : Yuxiao Xiao
4 // CREATION : 8/24/01
5 // Copyright (C) MicroStrategy, Inc. 2001
6 //==============================================================================================
7 
8 #ifndef _MSIVARIANT_H_
9 #define _MSIVARIANT_H_
10 
11 #include "DataType.h"
12 #include "Base/Base/String.h"
13 #include "Base/Base/ReturnString.h"
16 #include "Base/Base/Buffer.h"
17 #if defined(_NO_EXCEPTION)
18 #include <stdio.h>
19 #endif
20 
21 namespace MDataType {
22 
23 class DateTime;
24 class BigDecimal;
25 class Binary;
26 class CellFmtData;
27 
29 
30 //==============================================================================================
31 // Variant represents variant structure used MSI wide
32 //
33 // It's size is IMMUTABLE, you can not make bug fix which create new member!!!
34 //==============================================================================================
35 
37 {
38 
39 public :
40 
41  // according to DssDataTypeXXX
42  enum DATA_TYPE
43  {
44  MSI_SHORT = 21,
45  MSI_INTEGER = 1,
46  MSI_LONG = 22,
47  MSI_FLOAT = 7,
48  MSI_DOUBLE = 6,
49  MSI_DATE = 14,
50  MSI_TIME = 15,
51  MSI_DATETIME = 16,
52  MSI_STRING = 9,
53  MSI_MBCS_STRING = 23,
54  MSI_BINARY = 11,
55  MSI_BIGDECIMAL = 30,
56  MSI_CELLFMTDATA = 31,
57  MSI_INVALID = -1 ,
58  MSI_UNSIGNED_INTEGER = 2, // DssDataTypeUnsigned = 2,
59  MSI_UTF8_STRING = 33
60 
61  };
62 
63  //
64  // constructors.
65  //
66  Variant(MBase::Buffer* ipBuffer = NULL);
67  Variant(short iSval, MBase::Buffer* ipBuffer = NULL);
68  Variant(Int32 iIval, MBase::Buffer* ipBuffer = NULL);
69  Variant(unsigned Int32 iIval, MBase::Buffer* ipBuffer = NULL);
70  Variant(float iFval, MBase::Buffer* ipBuffer = NULL);
71  Variant(double iDval, MBase::Buffer* ipBuffer = NULL);
72  Variant(const char* iStval, MBase::Buffer* ipBuffer = NULL);
73 #ifndef NO_WCHAR_SUPPORT
74  Variant(const wchar_t* iStval, MBase::Buffer* ipBuffer = NULL);
75 #endif
76  Variant(const DateTime& iDtval, MBase::Buffer* ipBuffer = NULL);
77  Variant(const BigDecimal& iBDval, MBase::Buffer* ipBuffer = NULL);
78  Variant(const Binary* irBinary, MBase::Buffer* ipBuffer = NULL);
79  Variant(const CellFmtData &iCellFmtData, MBase::Buffer* ipBuffer = NULL);
80 
81  // yluo 11/16/06: we need a Variant that is governed by MemoryContract, but not by Buffer.
82  Variant(MBase::MemoryContract* ipMemContract);
83 
84  // copy constructor
85  Variant(const Variant& iVariant);
86 
87  // destructor
88  ~Variant() throw();
89 
90  // = operators
91  Variant& operator = (short iSval);
92  Variant& operator = (Int32 iIval);
93  Variant& operator = (unsigned Int32 iIval);
94  Variant& operator = (float iFval);
95  Variant& operator = (double iDval);
96  Variant& operator = (const char* iStval);
97 #ifndef NO_WCHAR_SUPPORT
98  Variant& operator = (const wchar_t* iStval);
99 #endif
100  Variant& operator = (const DateTime& irDateTime);
101  Variant& operator = (const BigDecimal& irBigDecimal);
102  Variant& operator = (const Binary* irBinary);
103  Variant& operator = (const CellFmtData& iCellFmtData);
104  Variant& operator = (const Variant& irVariant);
105 
106  //lcui: TQMS 243505 8/25/2006
107  void SetDate(const DateTime& irDateTime);
108 
109  // casting operators. Each of these methods convert the Variant type to the
110  // return type indicated.
111 
112  // throws VariantException if mDateType is not numeric
113  operator short() const;
114  // throws VariantException if mDateType is not numeric
115  operator Int32() const;
116  // throws VariantException if mDateType is not numeric
117  operator unsigned Int32() const;
118  // throws VariantException if mDateType is not numeric
119  operator float() const;
120  // throws VariantException if mDateType is not numeric
121  operator double() const;
122 
123  // throws VariantException if mDateType is not MSI_DATETIME
124  operator DateTime() const;
125  const DateTime* ToDateTime() const;
126 
127  // throws VariantException if mDateType is not MSI_BIGDECIMAL
128  const BigDecimal* ToBigDecimal() const;
129 
130  operator CellFmtData() const;
131 
132  // throws VariantException if mDateType is not MSI_BINARY
133  // For performance concern, this call returns a weak reference to the internal data.
134  // DO NOT delete or make any modification on the pointer.
135  // If caller want to keep a copy, he should make a duplicate.
136  const Binary* ToBinary() const;
137 
138  DATA_TYPE GetVarType() const;
139 
140  // MSI_STRING, MSI_DATETIME, MSI_BINARY is only comparable with the exact same type
141  // build types (short, long, float, double) are all comparable.
142  bool IsTypeComparable(const Variant& irVariant) const;
143 
144  // return false is two varant types are incomparable
145  // otherwise based on the value comparision
146  bool operator == (short iSval) const;
147  bool operator == (Int32 iIval) const;
148  bool operator == (unsigned Int32 iIval) const;
149  bool operator == (float iFval) const;
150  bool operator == (double iDval) const;
151  bool operator == (const char* iStval) const;
152 #ifndef NO_WCHAR_SUPPORT
153  bool operator == (const wchar_t* iStval) const;
154 #endif
155  bool operator == (const DateTime& iDtval) const;
156  bool operator == (const BigDecimal& iBdval) const;
157  bool operator == (const Binary& irBinary) const;
158  bool operator == (const Variant& irVariant) const;
159  bool operator == (const CellFmtData& iCellFmtData) const;
160 
161  // throws VariantException is two types are not comparable
162  // otherwise based on the value comparision
163  bool operator < (const Variant& irVariant);
164 
165  // Return mpStval if data type is MSI_STRING, otherwise return NULL
166  const MBase::String* GetRawWString() const;
167 
168 #ifndef NO_WCHAR_SUPPORT
169  // if it's a Binary type, this call will always return a NULL string
170  MBase::ReturnWString ToWString() const;
171 
172  // the following two methods are counterpart of ToWString, with less memory copies.
173  // it's optimized for MSI_STRING, NOT recommented for other types.
174 
175  // returns the length of wstring representation of the variant.
176  unsigned Int32 GetWStringLength() const;
177 
178  // inBufferLen is the size of opcBuffer
179  // if inBufferLen is less than or equal to the length of the string, a null character is NOT appended automatically to opcBuffer
180  // if inBufferLen is greater than the length of the string, the destination string is padded with null characters up to length count.
181  void ToWString(wchar_t* opcBuffer, unsigned Int32 inBufferLen) const;
182 #endif
183 
184  // if it's a Binary type, this call will always return a NULL string
186 
187  // the following methods are counterpart of ToString, with less memory copies.
188  // it's optimized for MSI_STRING, NOT recommented for other types.
189 
190  // returns the length of string representation of the variant.
191  unsigned Int32 GetStringLength() const;
192 
193  // inBufferLen is the size of opcBuffer
194  // if inBufferLen is less than or equal to the length of the string, a null character is NOT appended automatically to opcBuffer
195  // if inBufferLen is greater than the length of the string, the destination string is padded with null characters up to length count.
196  void ToString(char* opcBuffer, unsigned Int32 inBufferLen) const;
197 
198  void swap(Variant& irOther) throw ();
199 
200  friend void swap(Variant& irVar1, Variant& irVar2) throw ();
201 
202  // long pan 2010-04-29: TQMS 388539, specially handle bulk export result
203  MBase::ReturnString ToStringVBBehavior() const;
204 
205 private:
206 
207  void Reset() throw();
208  void TakeValue(Variant& irOther) throw ();
209 
210  inline void CheckMemoryAvailability(unsigned Int64 iRequestSize)
211  {
212  if ( ! mpMemContract.IsNull() && mpMemContract->Alloc64(iRequestSize) != MCM_OK )
213  {
214 #if !defined(_NO_EXCEPTION)
215  throw ("Not enough memory left.");
216 #else
217  printf("%s\n","Not enough memory left.");
218  // TODO: What should we do here?
219 #endif
220 
221  }
222  };
223 
224  DATA_TYPE mDataType;
225 
226  union
227  {
228  short mSval;
230  unsigned Int32 mUIval;
232  float mFval;
233  double mDval;
239  void* mpReserved;
240  };
241 
242 
243  MBase::Buffer* mpBuffer;
244 
245  MBase::MemoryContractPtr mpMemContract;
246 };
247 
248 }
249 
250 #endif
Definition: BaseMemoryContract.h:40
bool operator<(const ::GUID &irGUID1, const ::GUID &irGUID2)
Definition: Base/Base/GUID.h:156
void * mpReserved
Definition: Variant.h:239
MDataType::Binary represents binary data.
Definition: Binary.h:33
Definition: ReturnString.h:36
bool operator==(const Allocator< _Ty > &left, const Allocator< _UT > &right)
Definition: Allocator.h:204
float mFval
Definition: Variant.h:232
Definition: Buffer.h:58
#define Int64
Definition: BasicTypes.h:36
MBase::String * mpStval
Definition: Variant.h:236
#define DLL_DATATYPE_EXIM
Definition: DataType.h:22
Definition: ReturnString.h:85
Definition: BigDecimal.h:18
short mSval
Definition: Variant.h:228
Binary * mpBinary
Definition: Variant.h:237
BigDecimal * mpBdval
Definition: Variant.h:235
Int32 mIval
Definition: Variant.h:229
#define Int32
Definition: BasicTypes.h:20
DATA_TYPE
Definition: Variant.h:42
Definition: Message.h:32
unsigned Int32 mUIval
Definition: Variant.h:230
Definition: BigDecimal.h:27
DLL_BASE_EXIM const char * ToString(MBase::MessageDomain iDomain)
Definition: Variant.h:36
Definition: Variant.h:28
std::basic_string< WCHAR, std::char_traits< WCHAR >, Allocator< WCHAR > > String
Definition: BaseString.h:26
#define MCM_OK
Definition: BaseMemoryContract.h:16
Definition: CellFmtData.h:76
CellFmtData * mpCellFmtData
Definition: Variant.h:238
Int32 mLval
Definition: Variant.h:231
Definition: DateTime.h:74
#define NULL
Definition: Null.h:10
double mDval
Definition: Variant.h:233
DateTime * mpDtval
Definition: Variant.h:234