Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
11 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. VAR.H
  5. Abstract:
  6. CVar & CVarVector.
  7. These are thread-safe translators for VARIANT and SAFEARRAY
  8. and represent all types support by WBEM.
  9. These are mutually nestable to any level. A CVarVector can contain
  10. an array of CVar, and a CVar can contain a CVarVector. One CVar
  11. can therefore contain a whole tree of CVar objects, themselves
  12. containing homogeneous or heterogeneous arrays of CVar objects.
  13. Note: CVar should not be bound to one type and then immediately
  14. coerced to a new type. This object is designed for speed, not safety,
  15. so there is no checking to see if this has been done. A memory leak
  16. is likely to occur.
  17. The assignment operator and copy constructors are the only method
  18. of changing the type on a CVar. Do NOT construct the object as
  19. a BSTR, for example, and then call SetDWORD.
  20. History:
  21. 16-Apr-96 a-raymcc Created.
  22. 12//17/98 sanjes - Partially Reviewed for Out of Memory.
  23. 18-Mar-99 a-dcrews Added out-of-memory exception handling
  24. --*/
  25. #ifndef _VAR_H_
  26. #define _VAR_H_
  27. #include <flexarry.h>
  28. #include <safearry.h>
  29. #include <algorithm>
  30. #include <autobstr.h>
  31. #define VT_EX_CVAR (VT_USERDEFINED | 0x80010000)
  32. #define VT_EX_CVARVECTOR (VT_USERDEFINED | 0x80010002)
  33. class CVarVector;
  34. typedef union
  35. {
  36. char cVal; // VT_I1
  37. BYTE bVal; // VT_UI1
  38. SHORT iVal; // VT_I2
  39. WORD wVal; // VT_UI2
  40. LONG lVal; // VT_I4
  41. DWORD dwVal; // VT_UI4
  42. VARIANT_BOOL boolVal; // VT_BOOL
  43. float fltVal; // VT_R4
  44. double dblVal; // VT_R8
  45. LPSTR pStr; // VT_LPSTR
  46. LPWSTR pWStr; // VT_LPWSTR
  47. BSTR Str; // VT_BSTR (stored as VT_LPWSTR)
  48. FILETIME Time; // VT_FILETIME
  49. BLOB Blob; // VT_BLOB
  50. LPCLSID pClsId; // VT_CLSID
  51. IUnknown* pUnk; // VT_UNKNOWN
  52. IDispatch* pDisp; // VT_DISPATCH
  53. CVarVector *pVarVector; // VT_EX_CVARVECTOR
  54. } METAVALUE;
  55. class POLARITY CVar
  56. {
  57. private:
  58. int m_vt;
  59. METAVALUE m_value;
  60. int m_nStatus;
  61. BOOL m_bCanDelete;
  62. void Init();
  63. public:
  64. enum { no_error, unsupported, failed };
  65. CVar() { Init(); }
  66. ~CVar();
  67. CVar(const CVar &);
  68. CVar& operator =(const CVar &);
  69. CVar(char c) { Init(); SetChar(c); }
  70. CVar(BYTE b) { Init(); SetByte(b); }
  71. CVar(SHORT s) { Init(); SetShort(s); }
  72. CVar(WORD w) { Init(); SetWord(w); }
  73. CVar(LONG l) { Init(); SetLong(l); }
  74. CVar(DWORD dw) { Init(); SetDWORD(dw); }
  75. CVar(float f) { Init(); SetFloat(f); }
  76. CVar(double d) { Init(); SetDouble(d); }
  77. CVar(VARIANT_BOOL b,int){ Init(); SetBool(b); }
  78. CVar(LPSTR p, BOOL bAcquire = FALSE)
  79. { Init(); SetLPSTR(p, bAcquire); }
  80. CVar(LPWSTR p, BOOL bAcquire = FALSE)
  81. { Init(); SetLPWSTR(p, bAcquire); }
  82. CVar(int, auto_bstr b)
  83. { Init(); SetBSTR(b); }
  84. CVar(int, BSTR b)
  85. { Init(); SetBSTR(b); }
  86. // Dummy int required for context, since BSTR is also LPWSTR
  87. // from Win32 point of view, although the VT_ indicators differ.
  88. CVar(CLSID *p, BOOL bAcquire = FALSE)
  89. { Init(); SetClsId(p, bAcquire); }
  90. CVar(BLOB *p, BOOL bAcquire = FALSE)
  91. { Init(); SetBlob(p, bAcquire); }
  92. CVar(FILETIME *p) { Init(); SetFileTime(p); }
  93. CVar(CVarVector *p, BOOL bAcquire = FALSE) { Init(); SetVarVector(p, bAcquire); }
  94. CVar(VARIANT *p) { Init(); SetVariant(p); }
  95. CVar(int nType, SAFEARRAY *p) { Init(); SetSafeArray(nType, p); }
  96. int Status() { return m_nStatus; }
  97. int DumpText(FILE *fStream);
  98. int GetType() { return m_vt; }
  99. int GetOleType();
  100. void Empty();
  101. int operator ==(CVar &Other);
  102. BOOL CompareTo(CVar& Other, BOOL bIgnoreCase);
  103. void SetRaw(int vt, void* pvData, int nDataLen);
  104. void* GetRawData() {return (void*)&m_value;}
  105. BOOL CanDelete() {return m_bCanDelete;}
  106. void SetCanDelete(BOOL bCanDelete) {m_bCanDelete = bCanDelete;}
  107. // Numeric types.
  108. // ==============
  109. void SetAsNull() { m_vt = VT_NULL; m_value.lVal = 0; }
  110. BOOL IsNull() {return m_vt == VT_NULL;}
  111. BOOL IsDataNull();
  112. void SetChar(char c) { m_vt = VT_I1; m_value.cVal = c; }
  113. char GetChar() { return m_value.cVal; }
  114. operator char() { return m_value.cVal; }
  115. void SetByte(BYTE b) { m_vt = VT_UI1; m_value.bVal = b; }
  116. BYTE GetByte() { return m_value.bVal; }
  117. operator BYTE() { return m_value.bVal; }
  118. void SetShort(SHORT iVal) { m_vt = VT_I2; m_value.iVal = iVal; }
  119. SHORT GetShort() { return m_value.iVal; }
  120. operator SHORT() { return m_value.iVal; }
  121. void SetWord(WORD wVal) { m_vt = VT_UI2; m_value.wVal = wVal; }
  122. WORD GetWord() { return m_value.wVal; }
  123. operator WORD() { return m_value.wVal; }
  124. void SetLong(LONG lVal) { m_value.lVal = lVal; m_vt = VT_I4; }
  125. LONG GetLong() { return m_value.lVal; }
  126. operator LONG() { return m_value.lVal; }
  127. void SetDWORD(DWORD dwVal) { m_value.dwVal = dwVal; m_vt = VT_UI4; }
  128. DWORD GetDWORD() { return m_value.dwVal; }
  129. operator DWORD() { return m_value.dwVal; }
  130. void SetBool(VARIANT_BOOL b) { m_value.boolVal = b; m_vt = VT_BOOL; }
  131. VARIANT_BOOL GetBool() { return m_value.boolVal; }
  132. void SetFloat(float f) { m_value.fltVal = f; m_vt = VT_R4; }
  133. float GetFloat() { return m_value.fltVal; }
  134. operator float() { return m_value.fltVal; }
  135. void SetDouble(double dblVal) { m_value.dblVal = dblVal; m_vt = VT_R8; }
  136. double GetDouble() { return m_value.dblVal; }
  137. operator double() { return m_value.dblVal; }
  138. void SetDispatch(IDispatch* pDisp);
  139. IDispatch* GetDispatch()
  140. {if(m_value.pDisp) m_value.pDisp->AddRef(); return m_value.pDisp;}
  141. void SetUnknown(IUnknown* pUnk);
  142. IUnknown* GetUnknown()
  143. {if(m_value.pUnk) m_value.pUnk->AddRef(); return m_value.pUnk;}
  144. void SetEmbeddedObject(IUnknown* pUnk) {SetUnknown(pUnk);}
  145. IUnknown* GetEmbeddedObject() {return GetUnknown();}
  146. int SetVariant(VARIANT *pSrc, BOOL fOptimize = FALSE);
  147. void FillVariant(VARIANT* pDest, BOOL fOptimized = FALSE);
  148. VARIANT *GetNewVariant();
  149. // String types.
  150. // =============
  151. BOOL SetLPWSTR(LPWSTR pVal, BOOL bAcquire = FALSE);
  152. wchar_t *GetLPWSTR() { return m_value.pWStr; }
  153. operator LPWSTR() { return m_value.pWStr; }
  154. BOOL SetLPSTR(LPSTR pStr, BOOL bAcquire = FALSE);
  155. LPSTR GetLPSTR() { return m_value.pStr; }
  156. operator LPSTR() { return m_value.pStr; }
  157. BOOL SetBSTR(auto_bstr str);
  158. BOOL SetBSTR(BSTR str);
  159. BSTR GetBSTR(); // Makes a dynamic copy which must be freed.
  160. // Misc. types.
  161. // ============
  162. void SetFileTime(FILETIME *pft) { m_value.Time = *pft; m_vt = VT_FILETIME; }
  163. FILETIME GetFileTime() { return m_value.Time; }
  164. operator FILETIME() { return m_value.Time; }
  165. void SetBlob(BLOB *pBlob, BOOL bAcquire = FALSE);
  166. BLOB *GetBlob() { return &m_value.Blob; }
  167. operator BLOB *() { return &m_value.Blob; }
  168. void SetClsId(CLSID *pClsId, BOOL bAcquire);
  169. CLSID* GetClsId() { return m_value.pClsId; } // Return value is read-only
  170. operator CLSID*() { return m_value.pClsId; } // Return value is read-only
  171. void SetVarVector(CVarVector *pVec, BOOL bAcquire);
  172. CVarVector *GetVarVector() { return m_value.pVarVector; }
  173. operator CVarVector *() { return m_value.pVarVector; }
  174. void SetSafeArray(int nType, SAFEARRAY *pArray); // Copies the source
  175. SAFEARRAY *GetNewSafeArray(); // New SAFEARRAY which must be released
  176. void SetOptimizedSafeArray(int nType, SAFEARRAY *pArray, BOOL fAcquire = FALSE);
  177. SAFEARRAY* GetOptimizedSafeArray( void );
  178. static BSTR TypeToText(int nType);
  179. BSTR GetTypeText();
  180. BSTR GetText(long lFlags, long lType = 0, LPCWSTR szFormat = NULL);
  181. BOOL ChangeTypeTo(VARTYPE vtNew);
  182. BOOL ChangeTypeToEx(VARTYPE vtNew, LCID lcid = 0x409 );
  183. BOOL ToSingleChar();
  184. BOOL ToUI4();
  185. };
  186. class POLARITY CVarVector
  187. {
  188. int m_nType;
  189. CFlexArray m_Array;
  190. int m_nStatus;
  191. CSafeArray* m_pSafeArray;
  192. void* m_pRawData;
  193. public:
  194. enum { no_error, failed, unsupported };
  195. CVarVector();
  196. CVarVector(int nVarType, int nInitSize = 32, int nGrowBy = 32);
  197. // These two only support limited SAFEARRAY types.
  198. // ===============================================
  199. CVarVector(int nVarType, SAFEARRAY *pSrc, BOOL fOptimized = FALSE);
  200. SAFEARRAY *GetNewSafeArray();
  201. SAFEARRAY* GetSafeArray( BOOL fAcquire = FALSE );
  202. int GetType() { return m_nType; }
  203. int Status() { return m_nStatus; }
  204. void Empty();
  205. ~CVarVector();
  206. CVarVector(CVarVector &Src);
  207. CVarVector& operator =(CVarVector &Src);
  208. int operator ==(CVarVector &Src);
  209. BOOL CompareTo(CVarVector& Other, BOOL bIgnoreCase);
  210. int Size();
  211. int Add(CVar &Value);
  212. int Add(CVar *pAcquiredPtr);
  213. CVar& GetAt(int nIndex);
  214. CVar& operator [](int nIndex);
  215. int RemoveAt(int nIndex);
  216. int InsertAt(int nIndex, CVar &Value);
  217. BSTR GetText(long lFlags, long lType = 0);
  218. BOOL ToSingleChar();
  219. BOOL ToUI4();
  220. BOOL IsOptimized( void ) { return NULL != m_pSafeArray; }
  221. BOOL MakeOptimized( int nVarType, int nInitSize = 32, int nGrowBy = 32 );
  222. void SetRawArrayBinding( int nBinding );
  223. HRESULT AccessRawArray( void** ppv );
  224. HRESULT UnaccessRawArray( void );
  225. // We access raw data using internal data member.
  226. HRESULT InternalRawArrayAccess( void );
  227. static BOOL IsValidVectorType( int nVarType );
  228. static BOOL IsValidVectorArray( int nVarType, SAFEARRAY* pArray );
  229. void FillCVarAt(int nIndex, CVar& vTemp);
  230. BOOL DoesVectorTypeMatchArrayType( void );
  231. HRESULT SetRawArrayData( void* pvData, int nNumElements, int nElementSize );
  232. HRESULT GetRawArrayData( void* pvDest, int nBuffSize );
  233. BOOL SetRawArraySize( int nSize );
  234. int GetElementSize( void );
  235. };
  236. class CUnaccessVarVector
  237. {
  238. private:
  239. CVarVector* m_pVV;
  240. public:
  241. CUnaccessVarVector( CVarVector* pvv = NULL ) : m_pVV( pvv ) {}
  242. ~CUnaccessVarVector() { Unaccess(); }
  243. void SetVV( CVarVector* pvv ) { m_pVV = pvv; }
  244. void Unaccess( void ) { if ( NULL != m_pVV ) m_pVV->UnaccessRawArray(); m_pVV = NULL; }
  245. };
  246. #endif