Source code of Windows XP (NT5)
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.

336 lines
10 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. #define VT_EX_CVAR (VT_USERDEFINED | 0x80010000)
  30. #define VT_EX_CVARVECTOR (VT_USERDEFINED | 0x80010002)
  31. class CVarVector;
  32. typedef union
  33. {
  34. char cVal; // VT_I1
  35. BYTE bVal; // VT_UI1
  36. SHORT iVal; // VT_I2
  37. WORD wVal; // VT_UI2
  38. LONG lVal; // VT_I4
  39. DWORD dwVal; // VT_UI4
  40. VARIANT_BOOL boolVal; // VT_BOOL
  41. float fltVal; // VT_R4
  42. double dblVal; // VT_R8
  43. LPSTR pStr; // VT_LPSTR
  44. LPWSTR pWStr; // VT_LPWSTR
  45. BSTR Str; // VT_BSTR (stored as VT_LPWSTR)
  46. FILETIME Time; // VT_FILETIME
  47. BLOB Blob; // VT_BLOB
  48. LPCLSID pClsId; // VT_CLSID
  49. IUnknown* pUnk; // VT_UNKNOWN
  50. IDispatch* pDisp; // VT_DISPATCH
  51. CVarVector *pVarVector; // VT_EX_CVARVECTOR
  52. } METAVALUE;
  53. class POLARITY CVar
  54. {
  55. private:
  56. int m_vt;
  57. METAVALUE m_value;
  58. int m_nStatus;
  59. BOOL m_bCanDelete;
  60. void Init();
  61. public:
  62. enum { no_error, unsupported, failed };
  63. CVar() { Init(); }
  64. ~CVar();
  65. CVar(CVar &);
  66. CVar& operator =(CVar &);
  67. CVar(char c) { Init(); SetChar(c); }
  68. CVar(BYTE b) { Init(); SetByte(b); }
  69. CVar(SHORT s) { Init(); SetShort(s); }
  70. CVar(WORD w) { Init(); SetWord(w); }
  71. CVar(LONG l) { Init(); SetLong(l); }
  72. CVar(DWORD dw) { Init(); SetDWORD(dw); }
  73. CVar(float f) { Init(); SetFloat(f); }
  74. CVar(double d) { Init(); SetDouble(d); }
  75. CVar(VARIANT_BOOL b,int){ Init(); SetBool(b); }
  76. CVar(LPSTR p, BOOL bAcquire = FALSE)
  77. { Init(); SetLPSTR(p, bAcquire); }
  78. CVar(LPWSTR p, BOOL bAcquire = FALSE)
  79. { Init(); SetLPWSTR(p, bAcquire); }
  80. CVar(int, BSTR b, BOOL bAcquire = FALSE)
  81. { Init(); SetBSTR(b, bAcquire); }
  82. // Dummy int required for context, since BSTR is also LPWSTR
  83. // from Win32 point of view, although the VT_ indicators differ.
  84. CVar(CLSID *p, BOOL bAcquire = FALSE)
  85. { Init(); SetClsId(p, bAcquire); }
  86. CVar(BLOB *p, BOOL bAcquire = FALSE)
  87. { Init(); SetBlob(p, bAcquire); }
  88. CVar(FILETIME *p) { Init(); SetFileTime(p); }
  89. CVar(CVarVector *p, BOOL bAcquire = FALSE) { Init(); SetVarVector(p, bAcquire); }
  90. CVar(VARIANT *p) { Init(); SetVariant(p); }
  91. CVar(int nType, SAFEARRAY *p) { Init(); SetSafeArray(nType, p); }
  92. int Status() { return m_nStatus; }
  93. int DumpText(FILE *fStream);
  94. int GetType() { return m_vt; }
  95. int GetOleType();
  96. void Empty();
  97. int operator ==(CVar &Other);
  98. BOOL CompareTo(CVar& Other, BOOL bIgnoreCase);
  99. void SetRaw(int vt, void* pvData, int nDataLen);
  100. void* GetRawData() {return (void*)&m_value;}
  101. BOOL CanDelete() {return m_bCanDelete;}
  102. void SetCanDelete(BOOL bCanDelete) {m_bCanDelete = bCanDelete;}
  103. // Numeric types.
  104. // ==============
  105. void SetAsNull() { m_vt = VT_NULL; m_value.lVal = 0; }
  106. BOOL IsNull() {return m_vt == VT_NULL;}
  107. BOOL IsDataNull();
  108. void SetChar(char c) { m_vt = VT_I1; m_value.cVal = c; }
  109. char GetChar() { return m_value.cVal; }
  110. operator char() { return m_value.cVal; }
  111. void SetByte(BYTE b) { m_vt = VT_UI1; m_value.bVal = b; }
  112. BYTE GetByte() { return m_value.bVal; }
  113. operator BYTE() { return m_value.bVal; }
  114. void SetShort(SHORT iVal) { m_vt = VT_I2; m_value.iVal = iVal; }
  115. SHORT GetShort() { return m_value.iVal; }
  116. operator SHORT() { return m_value.iVal; }
  117. void SetWord(WORD wVal) { m_vt = VT_UI2; m_value.wVal = wVal; }
  118. WORD GetWord() { return m_value.wVal; }
  119. operator WORD() { return m_value.wVal; }
  120. void SetLong(LONG lVal) { m_value.lVal = lVal; m_vt = VT_I4; }
  121. LONG GetLong() { return m_value.lVal; }
  122. operator LONG() { return m_value.lVal; }
  123. void SetDWORD(DWORD dwVal) { m_value.dwVal = dwVal; m_vt = VT_UI4; }
  124. DWORD GetDWORD() { return m_value.dwVal; }
  125. operator DWORD() { return m_value.dwVal; }
  126. void SetBool(VARIANT_BOOL b) { m_value.boolVal = b; m_vt = VT_BOOL; }
  127. VARIANT_BOOL GetBool() { return m_value.boolVal; }
  128. void SetFloat(float f) { m_value.fltVal = f; m_vt = VT_R4; }
  129. float GetFloat() { return m_value.fltVal; }
  130. operator float() { return m_value.fltVal; }
  131. void SetDouble(double dblVal) { m_value.dblVal = dblVal; m_vt = VT_R8; }
  132. double GetDouble() { return m_value.dblVal; }
  133. operator double() { return m_value.dblVal; }
  134. void SetDispatch(IDispatch* pDisp);
  135. IDispatch* GetDispatch()
  136. {if(m_value.pDisp) m_value.pDisp->AddRef(); return m_value.pDisp;}
  137. void SetUnknown(IUnknown* pUnk);
  138. IUnknown* GetUnknown()
  139. {if(m_value.pUnk) m_value.pUnk->AddRef(); return m_value.pUnk;}
  140. void SetEmbeddedObject(IUnknown* pUnk) {SetUnknown(pUnk);}
  141. IUnknown* GetEmbeddedObject() {return GetUnknown();}
  142. int SetVariant(VARIANT *pSrc, BOOL fOptimize = FALSE);
  143. void FillVariant(VARIANT* pDest, BOOL fOptimized = FALSE);
  144. VARIANT *GetNewVariant();
  145. // String types.
  146. // =============
  147. BOOL SetLPWSTR(LPWSTR pVal, BOOL bAcquire = FALSE);
  148. wchar_t *GetLPWSTR() { return m_value.pWStr; }
  149. operator LPWSTR() { return m_value.pWStr; }
  150. BOOL SetLPSTR(LPSTR pStr, BOOL bAcquire = FALSE);
  151. LPSTR GetLPSTR() { return m_value.pStr; }
  152. operator LPSTR() { return m_value.pStr; }
  153. BOOL SetBSTR(BSTR str, BOOL bAcquire = FALSE);
  154. BSTR GetBSTR(); // Makes a dynamic copy which must be freed.
  155. // Misc. types.
  156. // ============
  157. void SetFileTime(FILETIME *pft) { m_value.Time = *pft; m_vt = VT_FILETIME; }
  158. FILETIME GetFileTime() { return m_value.Time; }
  159. operator FILETIME() { return m_value.Time; }
  160. void SetBlob(BLOB *pBlob, BOOL bAcquire = FALSE);
  161. BLOB *GetBlob() { return &m_value.Blob; }
  162. operator BLOB *() { return &m_value.Blob; }
  163. void SetClsId(CLSID *pClsId, BOOL bAcquire);
  164. CLSID* GetClsId() { return m_value.pClsId; } // Return value is read-only
  165. operator CLSID*() { return m_value.pClsId; } // Return value is read-only
  166. void SetVarVector(CVarVector *pVec, BOOL bAcquire);
  167. CVarVector *GetVarVector() { return m_value.pVarVector; }
  168. operator CVarVector *() { return m_value.pVarVector; }
  169. void SetSafeArray(int nType, SAFEARRAY *pArray); // Copies the source
  170. SAFEARRAY *GetNewSafeArray(); // New SAFEARRAY which must be released
  171. void SetOptimizedSafeArray(int nType, SAFEARRAY *pArray, BOOL fAcquire = FALSE);
  172. SAFEARRAY* GetOptimizedSafeArray( void );
  173. static BSTR TypeToText(int nType);
  174. BSTR GetTypeText();
  175. BSTR GetText(long lFlags, long lType = 0, LPCWSTR szFormat = NULL);
  176. BOOL ChangeTypeTo(VARTYPE vtNew);
  177. BOOL ChangeTypeToEx(VARTYPE vtNew, LCID lcid = 0x409 );
  178. BOOL ToSingleChar();
  179. BOOL ToUI4();
  180. };
  181. class POLARITY CVarVector
  182. {
  183. int m_nType;
  184. CFlexArray m_Array;
  185. int m_nStatus;
  186. CSafeArray* m_pSafeArray;
  187. void* m_pRawData;
  188. public:
  189. enum { no_error, failed, unsupported };
  190. CVarVector();
  191. CVarVector(int nVarType, int nInitSize = 32, int nGrowBy = 32);
  192. // These two only support limited SAFEARRAY types.
  193. // ===============================================
  194. CVarVector(int nVarType, SAFEARRAY *pSrc, BOOL fOptimized = FALSE);
  195. SAFEARRAY *GetNewSafeArray();
  196. SAFEARRAY* GetSafeArray( BOOL fAcquire = FALSE );
  197. int GetType() { return m_nType; }
  198. int Status() { return m_nStatus; }
  199. void Empty();
  200. ~CVarVector();
  201. CVarVector(CVarVector &Src);
  202. CVarVector& operator =(CVarVector &Src);
  203. int operator ==(CVarVector &Src);
  204. BOOL CompareTo(CVarVector& Other, BOOL bIgnoreCase);
  205. int Size();
  206. int Add(CVar &Value);
  207. int Add(CVar *pAcquiredPtr);
  208. CVar& GetAt(int nIndex);
  209. CVar& operator [](int nIndex);
  210. int RemoveAt(int nIndex);
  211. int InsertAt(int nIndex, CVar &Value);
  212. BSTR GetText(long lFlags, long lType = 0);
  213. BOOL ToSingleChar();
  214. BOOL ToUI4();
  215. BOOL IsOptimized( void ) { return NULL != m_pSafeArray; }
  216. BOOL MakeOptimized( int nVarType, int nInitSize = 32, int nGrowBy = 32 );
  217. void SetRawArrayBinding( int nBinding );
  218. HRESULT AccessRawArray( void** ppv );
  219. HRESULT UnaccessRawArray( void );
  220. // We access raw data using internal data member.
  221. HRESULT InternalRawArrayAccess( void );
  222. static BOOL IsValidVectorType( int nVarType );
  223. static BOOL IsValidVectorArray( int nVarType, SAFEARRAY* pArray );
  224. void FillCVarAt(int nIndex, CVar& vTemp);
  225. BOOL DoesVectorTypeMatchArrayType( void );
  226. HRESULT SetRawArrayData( void* pvData, int nNumElements, int nElementSize );
  227. HRESULT GetRawArrayData( void* pvDest, int nBuffSize );
  228. BOOL SetRawArraySize( int nSize );
  229. int GetElementSize( void );
  230. };
  231. class CUnaccessVarVector
  232. {
  233. private:
  234. CVarVector* m_pVV;
  235. public:
  236. CUnaccessVarVector( CVarVector* pvv = NULL ) : m_pVV( pvv ) {}
  237. ~CUnaccessVarVector() { Unaccess(); }
  238. void SetVV( CVarVector* pvv ) { m_pVV = pvv; }
  239. void Unaccess( void ) { if ( NULL != m_pVV ) m_pVV->UnaccessRawArray(); m_pVV = NULL; }
  240. };
  241. #endif