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.

170 lines
5.5 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. typedef union
  14. {
  15. double dblVal;
  16. float fltVal;
  17. short iVal;
  18. long lVal;
  19. BYTE bVal;
  20. VARIANT_BOOL boolVal;
  21. } SA_ArrayScalar;
  22. // Conversion functions due to VC 5.0 Optimizer Problems
  23. inline SA_ArrayScalar ToSA_ArrayScalar( double dblVal )
  24. { SA_ArrayScalar sa; sa.dblVal = dblVal; return sa; }
  25. inline SA_ArrayScalar ToSA_ArrayScalar( float fltVal )
  26. { SA_ArrayScalar sa; sa.fltVal = fltVal; return sa; }
  27. inline SA_ArrayScalar ToSA_ArrayScalar( short iVal )
  28. { SA_ArrayScalar sa; sa.iVal = iVal; return sa; }
  29. inline SA_ArrayScalar ToSA_ArrayScalar( long lVal )
  30. { SA_ArrayScalar sa; sa.lVal = lVal; return sa; }
  31. inline SA_ArrayScalar ToSA_ArrayScalar( BYTE bVal )
  32. { SA_ArrayScalar sa; sa.bVal = bVal; return sa; }
  33. inline SA_ArrayScalar ToSA_ArrayScalarBool( VARIANT_BOOL boolVal )
  34. { SA_ArrayScalar sa; sa.boolVal = boolVal; return sa; }
  35. class CSafeArray
  36. {
  37. int m_nMaxElementUsed;
  38. int m_nFlags;
  39. int m_nGrowBy;
  40. int m_nStatus;
  41. int m_nVarType;
  42. SAFEARRAYBOUND m_bound;
  43. SAFEARRAY *m_pArray;
  44. void Empty(); // Empty array
  45. void Fatal(const char *);
  46. void CheckType(int n);
  47. int AddScalar(IN SA_ArrayScalar val);
  48. int SetScalarAt(IN int nIndex, IN SA_ArrayScalar val);
  49. SA_ArrayScalar GetScalarAt(IN int nIndex);
  50. public:
  51. enum { no_error, failed, range_error };
  52. enum { no_delete = 0x1, auto_delete = 0x2, bind = 0x4 };
  53. // Construction, destruction, and assignment.
  54. // ==========================================
  55. CSafeArray(
  56. IN int vt,
  57. IN int nFlags, // no_delete|auto_delete
  58. IN int nSize = 32,
  59. IN int nGrowBy = 32
  60. );
  61. CSafeArray(
  62. IN SAFEARRAY *pSrc,
  63. IN int nType, // VT_ type of SAFEARRAY.
  64. IN int nFlags, // no_delete|auto_delete [|bind]
  65. IN int nGrowBy = 32
  66. );
  67. CSafeArray &operator =(IN CSafeArray &Src);
  68. CSafeArray(IN CSafeArray &Src);
  69. ~CSafeArray();
  70. // Get functions.
  71. // ==============
  72. BYTE GetByteAt(IN int nIndex)
  73. { return GetScalarAt(nIndex).bVal; }
  74. LONG GetLongAt(IN int nIndex)
  75. { return GetScalarAt(nIndex).lVal; }
  76. SHORT GetShortAt(IN int nIndex)
  77. { return GetScalarAt(nIndex).iVal; }
  78. double GetDoubleAt(IN int nIndex)
  79. { return GetScalarAt(nIndex).dblVal; }
  80. float GetFloatAt(IN int nIndex)
  81. { return GetScalarAt(nIndex).fltVal; }
  82. VARIANT_BOOL GetBoolAt(IN int nIndex)
  83. { return GetScalarAt(nIndex).boolVal; }
  84. BSTR GetBSTRAt(IN int nIndex); // Caller must use SysFreeString
  85. VARIANT GetVariantAt(IN int nIndex); //
  86. IDispatch* GetDispatchAt(IN int nIndex);
  87. IUnknown* GetUnknownAt(IN int nIndex);
  88. // Set functions.
  89. // ==============
  90. int SetByteAt(IN int nIndex, IN BYTE byVal)
  91. { return SetScalarAt(nIndex, ToSA_ArrayScalar(byVal)); }
  92. int SetLongAt(IN int nIndex, IN LONG lVal)
  93. { return SetScalarAt(nIndex, ToSA_ArrayScalar(lVal)); }
  94. int SetFloatAt(IN int nIndex, IN float fltVal)
  95. { return SetScalarAt(nIndex, ToSA_ArrayScalar(fltVal)); }
  96. int SetDoubleAt(IN int nIndex, IN double dVal)
  97. { return SetScalarAt(nIndex, ToSA_ArrayScalar(dVal)); }
  98. int SetShortAt(IN int nIndex, IN SHORT iVal)
  99. { return SetScalarAt(nIndex, ToSA_ArrayScalar(iVal)); }
  100. int SetBoolAt(IN int nIndex, IN VARIANT_BOOL boolVal)
  101. { return SetScalarAt(nIndex, ToSA_ArrayScalarBool(boolVal)); }
  102. int SetBSTRAt(IN int nIndex, IN BSTR Str); // A copy of the BSTR is made
  103. int SetVariantAt(IN int nIndex, IN VARIANT *pSrc);
  104. int SetDispatchAt(IN int nIndex, IN IDispatch* pDisp);
  105. int SetUnknownAt(IN int nIndex, IN IUnknown* pUnk);
  106. // Add (append) functions.
  107. // =======================
  108. int AddByte(IN BYTE byVal) { return AddScalar(ToSA_ArrayScalar(byVal)); }
  109. int AddLong(IN LONG lVal) { return AddScalar(ToSA_ArrayScalar(lVal)); }
  110. int AddFloat(IN float fltVal) { return AddScalar(ToSA_ArrayScalar(fltVal)); }
  111. int AddDouble(IN double dVal) { return AddScalar(ToSA_ArrayScalar(dVal)); }
  112. int AddShort(IN SHORT iVal) { return AddScalar(ToSA_ArrayScalar(iVal)); }
  113. int AddBool(IN VARIANT_BOOL boolVal) { return AddScalar(ToSA_ArrayScalarBool(boolVal)); }
  114. int AddBSTR(IN BSTR Str);
  115. int AddVariant(IN VARIANT *pData);
  116. int AddDispatch(IN IDispatch* pDisp);
  117. int AddUnknown(IN IUnknown* pUnk);
  118. // Operations the array as a whole.
  119. // ================================
  120. int RemoveAt(IN int nIndex);
  121. int Size() { return m_nMaxElementUsed + 1; }
  122. int GetType() { return m_nVarType; }
  123. int Status() { return m_nStatus; }
  124. int Trim();
  125. void SetGrowGranularity(IN int n) { m_nGrowBy = n; }
  126. void SetDestructorPolicy(IN int n) { m_nFlags = n; } // auto_delete|no_delete
  127. SAFEARRAY *GetArrayCopy(); // Returns a copy of the array
  128. SAFEARRAY *GetArray() { return m_pArray; }
  129. int TextDump(IN FILE *fStream);
  130. };
  131. #endif