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.

290 lines
9.3 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 <WT_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 CVar
  54. {
  55. int m_vt;
  56. METAVALUE m_value;
  57. int m_nStatus;
  58. BOOL m_bCanDelete;
  59. void Init();
  60. public:
  61. enum { no_error, unsupported, failed };
  62. CVar() { Init(); }
  63. ~CVar();
  64. CVar(CVar &);
  65. CVar& operator =(CVar &);
  66. CVar(char c) { Init(); SetChar(c); }
  67. CVar(BYTE b) { Init(); SetByte(b); }
  68. CVar(SHORT s) { Init(); SetShort(s); }
  69. CVar(WORD w) { Init(); SetWord(w); }
  70. CVar(LONG l) { Init(); SetLong(l); }
  71. CVar(DWORD dw) { Init(); SetDWORD(dw); }
  72. CVar(float f) { Init(); SetFloat(f); }
  73. CVar(double d) { Init(); SetDouble(d); }
  74. CVar(VARIANT_BOOL b,int){ Init(); SetBool(b); }
  75. CVar(LPSTR p, BOOL bAcquire = FALSE)
  76. { Init(); SetLPSTR(p, bAcquire); }
  77. CVar(LPWSTR p, BOOL bAcquire = FALSE)
  78. { Init(); SetLPWSTR(p, bAcquire); }
  79. CVar(int, BSTR b, BOOL bAcquire = FALSE)
  80. { Init(); SetBSTR(b, bAcquire); }
  81. // Dummy int required for context, since BSTR is also LPWSTR
  82. // from Win32 point of view, although the VT_ indicators differ.
  83. CVar(CLSID *p, BOOL bAcquire = FALSE)
  84. { Init(); SetClsId(p, bAcquire); }
  85. CVar(BLOB *p, BOOL bAcquire = FALSE)
  86. { Init(); SetBlob(p, bAcquire); }
  87. CVar(FILETIME *p) { Init(); SetFileTime(p); }
  88. CVar(CVarVector *p, BOOL bAcquire = FALSE) { Init(); SetVarVector(p, bAcquire); }
  89. CVar(VARIANT *p) { Init(); SetVariant(p); }
  90. CVar(int nType, SAFEARRAY *p) { Init(); SetSafeArray(nType, p); }
  91. int Status() { return m_nStatus; }
  92. int DumpText(FILE *fStream);
  93. int GetType() { return m_vt; }
  94. int GetOleType();
  95. void Empty();
  96. int operator ==(CVar &Other);
  97. BOOL CompareTo(CVar& Other, BOOL bIgnoreCase);
  98. void SetRaw(int vt, void* pvData, int nDataLen);
  99. void* GetRawData() {return (void*)&m_value;}
  100. BOOL CanDelete() {return m_bCanDelete;}
  101. void SetCanDelete(BOOL bCanDelete) {m_bCanDelete = bCanDelete;}
  102. // Numeric types.
  103. // ==============
  104. void SetAsNull() { m_vt = VT_NULL; m_value.lVal = 0; }
  105. BOOL IsNull() {return m_vt == VT_NULL;}
  106. BOOL IsDataNull();
  107. void SetChar(char c) { m_vt = VT_I1; m_value.cVal = c; }
  108. char GetChar() { return m_value.cVal; }
  109. operator char() { return m_value.cVal; }
  110. void SetByte(BYTE b) { m_vt = VT_UI1; m_value.bVal = b; }
  111. BYTE GetByte() { return m_value.bVal; }
  112. operator BYTE() { return m_value.bVal; }
  113. void SetShort(SHORT iVal) { m_vt = VT_I2; m_value.iVal = iVal; }
  114. SHORT GetShort() { return m_value.iVal; }
  115. operator SHORT() { return m_value.iVal; }
  116. void SetWord(WORD wVal) { m_vt = VT_UI2; m_value.wVal = wVal; }
  117. WORD GetWord() { return m_value.wVal; }
  118. operator WORD() { return m_value.wVal; }
  119. void SetLong(LONG lVal) { m_value.lVal = lVal; m_vt = VT_I4; }
  120. LONG GetLong() { return m_value.lVal; }
  121. operator LONG() { return m_value.lVal; }
  122. void SetDWORD(DWORD dwVal) { m_value.dwVal = dwVal; m_vt = VT_UI4; }
  123. DWORD GetDWORD() { return m_value.dwVal; }
  124. operator DWORD() { return m_value.dwVal; }
  125. void SetBool(VARIANT_BOOL b) { m_value.boolVal = b; m_vt = VT_BOOL; }
  126. VARIANT_BOOL GetBool() { return m_value.boolVal; }
  127. void SetFloat(float f) { m_value.fltVal = f; m_vt = VT_R4; }
  128. float GetFloat() { return m_value.fltVal; }
  129. operator float() { return m_value.fltVal; }
  130. void SetDouble(double dblVal) { m_value.dblVal = dblVal; m_vt = VT_R8; }
  131. double GetDouble() { return m_value.dblVal; }
  132. operator double() { return m_value.dblVal; }
  133. void SetDispatch(IDispatch* pDisp);
  134. IDispatch* GetDispatch()
  135. {if(m_value.pDisp) m_value.pDisp->AddRef(); return m_value.pDisp;}
  136. void SetUnknown(IUnknown* pUnk);
  137. IUnknown* GetUnknown()
  138. {if(m_value.pUnk) m_value.pUnk->AddRef(); return m_value.pUnk;}
  139. void SetEmbeddedObject(IUnknown* pUnk) {SetUnknown(pUnk);}
  140. IUnknown* GetEmbeddedObject() {return GetUnknown();}
  141. int SetVariant(VARIANT *pSrc);
  142. void FillVariant(VARIANT* pDest);
  143. VARIANT *GetNewVariant();
  144. // String types.
  145. // =============
  146. BOOL SetLPWSTR(LPWSTR pVal, BOOL bAcquire = FALSE);
  147. wchar_t *GetLPWSTR() { return m_value.pWStr; }
  148. operator LPWSTR() { return m_value.pWStr; }
  149. BOOL SetLPSTR(LPSTR pStr, BOOL bAcquire = FALSE);
  150. LPSTR GetLPSTR() { return m_value.pStr; }
  151. operator LPSTR() { return m_value.pStr; }
  152. BOOL SetBSTR(BSTR str, BOOL bAcquire = FALSE);
  153. BSTR GetBSTR(); // Makes a dynamic copy which must be freed.
  154. // Misc. types.
  155. // ============
  156. void SetFileTime(FILETIME *pft) { m_value.Time = *pft; m_vt = VT_FILETIME; }
  157. FILETIME GetFileTime() { return m_value.Time; }
  158. operator FILETIME() { return m_value.Time; }
  159. void SetBlob(BLOB *pBlob, BOOL bAcquire = FALSE);
  160. BLOB *GetBlob() { return &m_value.Blob; }
  161. operator BLOB *() { return &m_value.Blob; }
  162. void SetClsId(CLSID *pClsId, BOOL bAcquire);
  163. CLSID* GetClsId() { return m_value.pClsId; } // Return value is read-only
  164. operator CLSID*() { return m_value.pClsId; } // Return value is read-only
  165. void SetVarVector(CVarVector *pVec, BOOL bAcquire);
  166. CVarVector *GetVarVector() { return m_value.pVarVector; }
  167. operator CVarVector *() { return m_value.pVarVector; }
  168. void SetSafeArray(int nType, SAFEARRAY *pArray); // Copies the source
  169. SAFEARRAY *GetNewSafeArray(); // New SAFEARRAY which must be released
  170. static BSTR TypeToText(int nType);
  171. BSTR GetTypeText();
  172. BSTR GetText(long lFlags, long lType = 0);
  173. BOOL ChangeTypeTo(VARTYPE vtNew);
  174. BOOL ChangeTypeToEx(VARTYPE vtNew, LCID lcid = 0x409 );
  175. BOOL ToSingleChar();
  176. BOOL ToUI4();
  177. };
  178. class CVarVector
  179. {
  180. int m_nType;
  181. CFlexArray m_Array;
  182. int m_nStatus;
  183. public:
  184. enum { no_error, failed, unsupported };
  185. CVarVector();
  186. CVarVector(int nVarType, int nInitSize = 32, int nGrowBy = 32);
  187. // These two only support limited SAFEARRAY types.
  188. // ===============================================
  189. CVarVector(int nVarType, SAFEARRAY *pSrc);
  190. SAFEARRAY *GetNewSafeArray();
  191. int GetType() { return m_nType; }
  192. int Status() { return m_nStatus; }
  193. void Empty();
  194. ~CVarVector();
  195. CVarVector(CVarVector &Src);
  196. CVarVector& operator =(CVarVector &Src);
  197. int operator ==(CVarVector &Src);
  198. BOOL CompareTo(CVarVector& Other, BOOL bIgnoreCase);
  199. int Size() { return m_Array.Size(); }
  200. int Add(CVar &Value);
  201. int Add(CVar *pAcquiredPtr);
  202. CVar& GetAt(int nIndex) { return *(CVar *) m_Array[nIndex]; }
  203. CVar& operator [](int nIndex) { return *(CVar *) m_Array[nIndex]; }
  204. int RemoveAt(int nIndex);
  205. int InsertAt(int nIndex, CVar &Value);
  206. BSTR GetText(long lFlags, long lType = 0);
  207. BOOL ToSingleChar();
  208. BOOL ToUI4();
  209. };
  210. #endif