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.

182 lines
6.2 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. SAFEARRY.H
  5. Abstract:
  6. CSafeArray implementation.
  7. History:
  8. 08-Apr-96 a-raymcc Created.
  9. 18-Mar-99 a-dcrews Added out-of-memory exception handling
  10. --*/
  11. #ifndef _SAFEARRY_H_
  12. #define _SAFEARRY_H_
  13. #include <stdio.h>
  14. #include "corepol.h"
  15. #include "corex.h"
  16. typedef union
  17. {
  18. double dblVal;
  19. float fltVal;
  20. short iVal;
  21. long lVal;
  22. BYTE bVal;
  23. VARIANT_BOOL boolVal;
  24. } SA_ArrayScalar;
  25. // Conversion functions due to VC 5.0 Optimizer Problems
  26. inline SA_ArrayScalar ToSA_ArrayScalar( double dblVal )
  27. { SA_ArrayScalar sa; sa.dblVal = dblVal; return sa; }
  28. inline SA_ArrayScalar ToSA_ArrayScalar( float fltVal )
  29. { SA_ArrayScalar sa; sa.fltVal = fltVal; return sa; }
  30. inline SA_ArrayScalar ToSA_ArrayScalar( short iVal )
  31. { SA_ArrayScalar sa; sa.iVal = iVal; return sa; }
  32. inline SA_ArrayScalar ToSA_ArrayScalar( long lVal )
  33. { SA_ArrayScalar sa; sa.lVal = lVal; return sa; }
  34. inline SA_ArrayScalar ToSA_ArrayScalar( BYTE bVal )
  35. { SA_ArrayScalar sa; sa.bVal = bVal; return sa; }
  36. inline SA_ArrayScalar ToSA_ArrayScalarBool( VARIANT_BOOL boolVal )
  37. { SA_ArrayScalar sa; sa.boolVal = boolVal; return sa; }
  38. class POLARITY CSafeArray
  39. {
  40. int m_nMaxElementUsed;
  41. int m_nFlags;
  42. int m_nGrowBy;
  43. int m_nStatus;
  44. int m_nVarType;
  45. int m_nElementSize;
  46. SAFEARRAYBOUND m_bound;
  47. SAFEARRAY *m_pArray;
  48. void Fatal(const char *);
  49. void CheckType(int n);
  50. int AddScalar(IN SA_ArrayScalar val);
  51. int SetScalarAt(IN int nIndex, IN SA_ArrayScalar val);
  52. SA_ArrayScalar GetScalarAt(IN int nIndex);
  53. public:
  54. enum { no_error, failed, range_error };
  55. enum { no_delete = 0x1, auto_delete = 0x2, bind = 0x4 };
  56. void Empty(); // Empty array
  57. // Construction, destruction, and assignment.
  58. // ==========================================
  59. CSafeArray(
  60. IN int vt,
  61. IN int nFlags, // no_delete|auto_delete
  62. IN int nSize = 32,
  63. IN int nGrowBy = 32
  64. );
  65. CSafeArray(
  66. IN SAFEARRAY *pSrc,
  67. IN int nType, // VT_ type of SAFEARRAY.
  68. IN int nFlags, // no_delete|auto_delete [|bind]
  69. IN int nGrowBy = 32
  70. );
  71. CSafeArray &operator =(IN CSafeArray &Src);
  72. CSafeArray(IN CSafeArray &Src);
  73. ~CSafeArray();
  74. // Get functions.
  75. // ==============
  76. BYTE GetByteAt(IN int nIndex)
  77. { return GetScalarAt(nIndex).bVal; }
  78. LONG GetLongAt(IN int nIndex)
  79. { return GetScalarAt(nIndex).lVal; }
  80. SHORT GetShortAt(IN int nIndex)
  81. { return GetScalarAt(nIndex).iVal; }
  82. double GetDoubleAt(IN int nIndex)
  83. { return GetScalarAt(nIndex).dblVal; }
  84. float GetFloatAt(IN int nIndex)
  85. { return GetScalarAt(nIndex).fltVal; }
  86. VARIANT_BOOL GetBoolAt(IN int nIndex)
  87. { return GetScalarAt(nIndex).boolVal; }
  88. BSTR GetBSTRAt(IN int nIndex); // Caller must use SysFreeString
  89. BSTR GetBSTRAtThrow(int nIndex); // Caller must use SysFreeString
  90. VARIANT GetVariantAt(IN int nIndex); //
  91. IDispatch* GetDispatchAt(IN int nIndex);
  92. IUnknown* GetUnknownAt(IN int nIndex);
  93. // Set functions.
  94. // ==============
  95. int SetByteAt(IN int nIndex, IN BYTE byVal)
  96. { return SetScalarAt(nIndex, ToSA_ArrayScalar(byVal)); }
  97. int SetLongAt(IN int nIndex, IN LONG lVal)
  98. { return SetScalarAt(nIndex, ToSA_ArrayScalar(lVal)); }
  99. int SetFloatAt(IN int nIndex, IN float fltVal)
  100. { return SetScalarAt(nIndex, ToSA_ArrayScalar(fltVal)); }
  101. int SetDoubleAt(IN int nIndex, IN double dVal)
  102. { return SetScalarAt(nIndex, ToSA_ArrayScalar(dVal)); }
  103. int SetShortAt(IN int nIndex, IN SHORT iVal)
  104. { return SetScalarAt(nIndex, ToSA_ArrayScalar(iVal)); }
  105. int SetBoolAt(IN int nIndex, IN VARIANT_BOOL boolVal)
  106. { return SetScalarAt(nIndex, ToSA_ArrayScalarBool(boolVal)); }
  107. int SetBSTRAt(IN int nIndex, IN BSTR Str); // A copy of the BSTR is made
  108. int SetVariantAt(IN int nIndex, IN VARIANT *pSrc);
  109. int SetDispatchAt(IN int nIndex, IN IDispatch* pDisp);
  110. int SetUnknownAt(IN int nIndex, IN IUnknown* pUnk);
  111. // Add (append) functions.
  112. // =======================
  113. int AddByte(IN BYTE byVal) { return AddScalar(ToSA_ArrayScalar(byVal)); }
  114. int AddLong(IN LONG lVal) { return AddScalar(ToSA_ArrayScalar(lVal)); }
  115. int AddFloat(IN float fltVal) { return AddScalar(ToSA_ArrayScalar(fltVal)); }
  116. int AddDouble(IN double dVal) { return AddScalar(ToSA_ArrayScalar(dVal)); }
  117. int AddShort(IN SHORT iVal) { return AddScalar(ToSA_ArrayScalar(iVal)); }
  118. int AddBool(IN VARIANT_BOOL boolVal) { return AddScalar(ToSA_ArrayScalarBool(boolVal)); }
  119. int AddBSTR(IN BSTR Str);
  120. int AddVariant(IN VARIANT *pData);
  121. int AddDispatch(IN IDispatch* pDisp);
  122. int AddUnknown(IN IUnknown* pUnk);
  123. // Operations the array as a whole.
  124. // ================================
  125. int RemoveAt(IN int nIndex);
  126. int Size() { return m_nMaxElementUsed + 1; }
  127. int GetType() { return m_nVarType; }
  128. int Status() { return m_nStatus; }
  129. int ElementSize() { return m_nElementSize; }
  130. int Trim();
  131. void SetGrowGranularity(IN int n) { m_nGrowBy = n; }
  132. void SetDestructorPolicy(IN int n) { m_nFlags = n; } // auto_delete|no_delete
  133. int GetActualVarType( VARTYPE* pvt );
  134. SAFEARRAY *GetArrayCopy(); // Returns a copy of the array
  135. SAFEARRAY *GetArray() { return m_pArray; }
  136. int TextDump(IN FILE *fStream);
  137. HRESULT Access( void** ppv ) { return SafeArrayAccessData( m_pArray, ppv ); }
  138. HRESULT Unaccess( void ) { return SafeArrayUnaccessData( m_pArray ); }
  139. int SetRawData( void* pvSource, int nNumElements, int nElementSize );
  140. int GetRawData( void* pvDest, int nBuffSize );
  141. void SetRawArrayMaxElement( int nMaxElement ) { m_nMaxElementUsed = nMaxElement; }
  142. };
  143. #endif