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.

452 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: StgVarA.hxx - Storage Variant Class with/Allocation support
  7. //
  8. // Contents: C++ Alloc wrapper for PROPVARIANT.
  9. //
  10. // History: 01-Aug-94 KyleP Created
  11. // 09-May-96 MikeHill Use the 'boolVal' member of PropVariant,
  12. // rather than the member named 'bool'
  13. // (which is a reserved keyword).
  14. //
  15. //--------------------------------------------------------------------------
  16. #if !defined(__STGVARA_HXX__)
  17. #define __STGVARA_HXX__
  18. #include <stgvarb.hxx>
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Class: CAllocStorageVariant
  22. //
  23. // Purpose: C++ wrapper for PROPVARIANT
  24. //
  25. // History: 01-Aug-94 KyleP Created
  26. //
  27. // Notes: This class should only be instantiated through subclasses that
  28. // define PMemoryAllocator. CStorageVariant is the most common
  29. // subclass, which uses CoTaskMemAlloc/Free inside its allocator.
  30. //
  31. // Constructors that allocate memory require a PMemoryAllocator.
  32. // The destructor is private to force use of the ResetType()
  33. // method, which also requires a PMemoryAllocator.
  34. //
  35. // A couple of variant arms are not implemented below.
  36. // VT_BSTR because its type signature is identical to that of
  37. // VT_LPSTR.
  38. //
  39. // Some types are duplicate base types with a different variant
  40. // tag. These include:
  41. // VARIANT_BOOL (short)
  42. // SCODE (long)
  43. // DATE (double)
  44. // We cannot create trivial constructors for the above because
  45. // of the type collision. You must use the Set methods.
  46. //
  47. //--------------------------------------------------------------------------
  48. class CAllocStorageVariant: public CBaseStorageVariant
  49. {
  50. public:
  51. //
  52. // Simple types
  53. //
  54. CAllocStorageVariant() { vt = VT_EMPTY; }
  55. CAllocStorageVariant(short i) { vt = VT_I2; iVal = i; }
  56. CAllocStorageVariant(long l) { vt = VT_I4; lVal = l; }
  57. CAllocStorageVariant(LARGE_INTEGER h) { vt = VT_I8; hVal = h; }
  58. CAllocStorageVariant(float flt) { vt = VT_R4; fltVal = flt; }
  59. CAllocStorageVariant(double dbl) { vt = VT_R8; dblVal = dbl; }
  60. CAllocStorageVariant(CY cy) { vt = VT_CY; cyVal = cy; }
  61. CAllocStorageVariant(FILETIME ft) { vt = VT_FILETIME; filetime = ft; }
  62. //
  63. // Types with indirection
  64. //
  65. CAllocStorageVariant(BLOB b, PMemoryAllocator &ma)
  66. {
  67. new (this) CAllocStorageVariant(b.pBlobData, b.cbSize, ma);
  68. }
  69. CAllocStorageVariant(BYTE *pb, ULONG cb, PMemoryAllocator &ma);
  70. CAllocStorageVariant(char const *psz, PMemoryAllocator &ma);
  71. CAllocStorageVariant(WCHAR const *pwsz, PMemoryAllocator &ma);
  72. CAllocStorageVariant(CLSID const *pcid, PMemoryAllocator &ma);
  73. //
  74. // Interface types
  75. //
  76. CAllocStorageVariant(IStream *pstr, PMemoryAllocator &ma);
  77. CAllocStorageVariant(IStorage *pstor, PMemoryAllocator &ma);
  78. //
  79. // Counted array types. Elements initially zeroed. Use Set/Get/Size
  80. // for access.
  81. //
  82. CAllocStorageVariant(VARENUM vt, ULONG cElements, PMemoryAllocator &ma);
  83. //
  84. // To/From C style PROPVARIANT and copy constructor
  85. //
  86. CAllocStorageVariant(PROPVARIANT &var, PMemoryAllocator &ma);
  87. operator PROPVARIANT *() const { return((PROPVARIANT *) this); }
  88. operator PROPVARIANT &() const { return(*(PROPVARIANT *) this); }
  89. operator PROPVARIANT const *() { return((PROPVARIANT *) this); }
  90. operator PROPVARIANT const &() { return(*(PROPVARIANT *) this); }
  91. CAllocStorageVariant(PDeSerStream &stm, PMemoryAllocator &ma);
  92. //
  93. // Casts for simple types.
  94. //
  95. operator short() const { return(iVal); }
  96. operator USHORT() const { return(uiVal); }
  97. operator long() const { return(lVal); }
  98. operator ULONG() const { return(ulVal); }
  99. operator LARGE_INTEGER() const { return(hVal); }
  100. operator ULARGE_INTEGER() const { return(uhVal); }
  101. operator float() const { return(fltVal); }
  102. operator double() const { return(dblVal); }
  103. operator CY() const { return(cyVal); }
  104. operator FILETIME() const { return(filetime); }
  105. operator char const *() const { return(pszVal); }
  106. operator WCHAR const *() const { return(pwszVal); }
  107. operator IStream *() const { return(pStream); }
  108. operator IStorage *() const { return(pStorage); }
  109. BOOL IsValid() const;
  110. //
  111. // Member variable access
  112. //
  113. VARENUM Type() const { return((VARENUM) vt); }
  114. //
  115. // Set/Get, all types including arrays.
  116. //
  117. void SetEMPTY(PMemoryAllocator &ma)
  118. {
  119. ResetType(ma);
  120. vt = VT_EMPTY;
  121. }
  122. void SetNULL(PMemoryAllocator &ma)
  123. {
  124. ResetType(ma);
  125. vt = VT_NULL;
  126. }
  127. void SetI1(CHAR c, PMemoryAllocator &ma)
  128. {
  129. ResetType(ma);
  130. vt = VT_I1;
  131. cVal = c;
  132. }
  133. CHAR GetI1() const { return cVal; }
  134. void SetUI1(BYTE b, PMemoryAllocator &ma)
  135. {
  136. ResetType(ma);
  137. vt = VT_UI1;
  138. bVal = b;
  139. }
  140. BYTE GetUI1() const { return bVal; }
  141. void SetI2(short i, PMemoryAllocator &ma)
  142. {
  143. ResetType(ma);
  144. new (this) CAllocStorageVariant(i);
  145. }
  146. short GetI2() const { return(iVal); }
  147. void SetUI2(USHORT ui, PMemoryAllocator &ma)
  148. {
  149. ResetType(ma);
  150. vt = VT_UI2;
  151. uiVal = ui;
  152. }
  153. ULONG GetUI2() const { return(uiVal); }
  154. void SetI4(long l, PMemoryAllocator &ma)
  155. {
  156. ResetType(ma);
  157. new (this) CAllocStorageVariant(l);
  158. }
  159. long GetI4() const { return(lVal); }
  160. void SetUI4(ULONG ul, PMemoryAllocator &ma)
  161. {
  162. ResetType(ma);
  163. vt = VT_UI4;
  164. ulVal = ul;
  165. }
  166. ULONG GetUI4() const { return(ulVal); }
  167. void SetR4(float f, PMemoryAllocator &ma)
  168. {
  169. ResetType(ma);
  170. new (this) CAllocStorageVariant(f);
  171. }
  172. float GetR4() const { return(fltVal); }
  173. void SetR8(double d, PMemoryAllocator &ma)
  174. {
  175. ResetType(ma);
  176. new (this) CAllocStorageVariant(d);
  177. }
  178. double GetR8() const { return(dblVal); }
  179. void SetI8(LARGE_INTEGER li, PMemoryAllocator &ma)
  180. {
  181. ResetType(ma);
  182. new (this) CAllocStorageVariant(li);
  183. }
  184. LARGE_INTEGER GetI8() const { return(hVal); }
  185. void SetUI8(ULARGE_INTEGER uli, PMemoryAllocator &ma)
  186. {
  187. ResetType(ma);
  188. vt = VT_UI8;
  189. uhVal = uli;
  190. }
  191. ULARGE_INTEGER GetUI8() const { return(uhVal); }
  192. void SetBOOL(VARIANT_BOOL b, PMemoryAllocator &ma)
  193. {
  194. ResetType(ma);
  195. vt = VT_BOOL;
  196. boolVal = b;
  197. }
  198. VARIANT_BOOL GetBOOL() const { return(boolVal); }
  199. void SetERROR(SCODE sc, PMemoryAllocator &ma)
  200. {
  201. ResetType(ma);
  202. vt = VT_ERROR;
  203. scode = sc;
  204. }
  205. SCODE GetERROR() const { return(scode); }
  206. void SetCY(CY cy, PMemoryAllocator &ma)
  207. {
  208. ResetType(ma);
  209. new (this) CAllocStorageVariant(cy);
  210. }
  211. CY GetCY() const { return(cyVal); }
  212. void SetDATE(DATE d, PMemoryAllocator &ma)
  213. {
  214. ResetType(ma);
  215. vt = VT_DATE;
  216. date = d;
  217. }
  218. DATE GetDATE() const { return(date); }
  219. void SetBSTR(BSTR b, PMemoryAllocator &ma);
  220. BSTR GetBSTR() const { return(bstrVal); } // No ownership xfer!
  221. void SetLPSTR(char const *psz, PMemoryAllocator &ma)
  222. {
  223. ResetType(ma);
  224. new (this) CAllocStorageVariant(psz, ma);
  225. }
  226. char const *GetLPSTR() const { return(pszVal); } // No ownership xfer!
  227. void SetLPWSTR(WCHAR const *pwsz, PMemoryAllocator &ma)
  228. {
  229. ResetType(ma);
  230. new (this) CAllocStorageVariant(pwsz, ma);
  231. }
  232. WCHAR const *GetLPWSTR() const { return(pwszVal); } // No owner xfer!
  233. void SetFILETIME(FILETIME ft, PMemoryAllocator &ma)
  234. {
  235. ResetType(ma);
  236. new (this) CAllocStorageVariant(ft);
  237. }
  238. FILETIME GetFILETIME() const { return(filetime); }
  239. void SetBLOB(BLOB b, PMemoryAllocator &ma)
  240. {
  241. ResetType(ma);
  242. new (this) CAllocStorageVariant(b, ma);
  243. }
  244. BLOB GetBLOB() const { return(blob); } // No ownership xfer!
  245. void SetSTREAM(IStream *ps, PMemoryAllocator &ma)
  246. {
  247. ResetType(ma);
  248. vt = VT_STREAM;
  249. pStream = ps;
  250. ps->AddRef();
  251. }
  252. IStream *GetSTREAM() const { return(pStream); }
  253. void SetSTREAMED_OBJECT(IStream *ps, PMemoryAllocator &ma)
  254. {
  255. ResetType(ma);
  256. vt = VT_STREAMED_OBJECT;
  257. pStream = ps;
  258. ps->AddRef();
  259. }
  260. IStream *GetSTREAMED_OBJECT() const { return(pStream); }
  261. void SetSTORAGE(IStorage *ps, PMemoryAllocator &ma)
  262. {
  263. ResetType(ma);
  264. vt = VT_STORAGE;
  265. pStorage = ps;
  266. ps->AddRef();
  267. }
  268. IStorage *GetSTORAGE() const { return(pStorage); }
  269. void SetSTORED_OBJECT(IStorage *ps, PMemoryAllocator &ma)
  270. {
  271. ResetType(ma);
  272. vt = VT_STORED_OBJECT;
  273. pStorage = ps;
  274. ps->AddRef();
  275. }
  276. IStorage *GetSTORED_OBJECT() const { return(pStorage); }
  277. void SetCLSID(CLSID const *pc, PMemoryAllocator &ma)
  278. {
  279. ResetType(ma);
  280. new (this) CAllocStorageVariant(pc, ma);
  281. }
  282. CLSID const *GetCLSID() const { return(puuid); } // No ownership xfer!
  283. //
  284. // Array access
  285. //
  286. inline ULONG Count() const;
  287. void SetI1(CHAR b, unsigned pos, PMemoryAllocator &ma);
  288. CHAR GetI1(unsigned pos) const;
  289. void SetUI1(BYTE b, unsigned pos, PMemoryAllocator &ma);
  290. BYTE GetUI1(unsigned pos) const;
  291. void SetI2(short i, unsigned pos, PMemoryAllocator &ma);
  292. short GetI2(unsigned pos) const;
  293. void SetUI2(USHORT ui, unsigned pos, PMemoryAllocator &ma);
  294. USHORT GetUI2(unsigned pos) const;
  295. void SetI4(long l, unsigned pos, PMemoryAllocator &ma);
  296. long GetI4(unsigned pos) const;
  297. void SetUI4(ULONG ul, unsigned pos, PMemoryAllocator &ma);
  298. ULONG GetUI4(unsigned pos) const;
  299. void SetERROR(SCODE ul, unsigned pos, PMemoryAllocator &ma);
  300. SCODE GetERROR(unsigned pos) const;
  301. void SetI8(LARGE_INTEGER li, unsigned pos, PMemoryAllocator &ma);
  302. LARGE_INTEGER GetI8(unsigned pos) const;
  303. void SetUI8(ULARGE_INTEGER uli, unsigned pos, PMemoryAllocator &ma);
  304. ULARGE_INTEGER GetUI8(unsigned pos) const;
  305. void SetR4(float f, unsigned pos, PMemoryAllocator &ma);
  306. float GetR4(unsigned pos) const;
  307. void SetR8(double d, unsigned pos, PMemoryAllocator &ma);
  308. double GetR8(unsigned pos) const;
  309. void SetBOOL(VARIANT_BOOL b, unsigned pos, PMemoryAllocator &ma);
  310. VARIANT_BOOL GetBOOL(unsigned pos) const;
  311. void SetCY(CY c, unsigned pos, PMemoryAllocator &ma);
  312. CY GetCY(unsigned pos) const;
  313. void SetDATE(DATE d, unsigned pos, PMemoryAllocator &ma);
  314. DATE GetDATE(unsigned pos) const;
  315. void SetBSTR(BSTR b, unsigned pos, PMemoryAllocator &ma);
  316. BSTR GetBSTR(unsigned pos) const { return cabstr.pElems[pos]; }
  317. // void SetVARIANT(CAllocStorageVariant var, unsigned pos, PMemoryAllocator &ma);
  318. inline CAllocStorageVariant & GetVARIANT(unsigned pos) const;
  319. void SetLPSTR(char const *psz, unsigned pos, PMemoryAllocator &ma);
  320. char *GetLPSTR(unsigned pos) const;
  321. void SetLPWSTR(WCHAR const *pwsz, unsigned pos, PMemoryAllocator &ma);
  322. WCHAR *GetLPWSTR(unsigned pos) const;
  323. void SetFILETIME(FILETIME f, unsigned pos, PMemoryAllocator &ma);
  324. FILETIME GetFILETIME(unsigned pos) const;
  325. void SetCLSID(CLSID c, unsigned pos, PMemoryAllocator &ma);
  326. CLSID GetCLSID(unsigned pos) const;
  327. protected:
  328. //
  329. // Manual & Default Destructor
  330. //
  331. void ResetType(PMemoryAllocator &ma);
  332. ~CAllocStorageVariant();
  333. //
  334. // Memory allocation. Returns passed parameter only.
  335. //
  336. void *operator new(size_t size, void *p)
  337. {
  338. return(p);
  339. }
  340. #if _MSC_VER >= 1200
  341. void operator delete(void *p, void *pp)
  342. {
  343. return;
  344. }
  345. #endif
  346. private:
  347. BOOLEAN _AddStringToVector(
  348. unsigned pos,
  349. VOID *pv,
  350. ULONG cb,
  351. PMemoryAllocator &ma);
  352. };
  353. inline ULONG
  354. CAllocStorageVariant::Count() const
  355. {
  356. if (Type() & VT_VECTOR)
  357. {
  358. return( cai.cElems );
  359. }
  360. return( 0 );
  361. }
  362. inline CAllocStorageVariant & CAllocStorageVariant::GetVARIANT(unsigned pos) const
  363. {
  364. return (CAllocStorageVariant &) capropvar.pElems[pos];
  365. }
  366. #endif // __STGVARA_HXX__