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.

106 lines
1.9 KiB

  1. // SmartBuffer.h: interface for the CSmartBuffer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_SMARTBUFFER_H__47C64F5D_8388_452B_864B_1F55D6AE6F17__INCLUDED_)
  5. #define AFX_SMARTBUFFER_H__47C64F5D_8388_452B_864B_1F55D6AE6F17__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. template<class T> class CSmartBuffer
  10. {
  11. public:
  12. CSmartBuffer(int NumElements);
  13. virtual ~CSmartBuffer();
  14. T* Allocate(int SizeNeeded);
  15. T* Access()
  16. {return m_Array;}
  17. void AddElement(T elem);
  18. int GetNumElementsStored()
  19. {return m_NumElementsUsed;}
  20. void SetNumElementsStored(int x)
  21. {m_NumElementsUsed = x;}
  22. bool FindElement(T elem);
  23. protected:
  24. T* m_Array;
  25. int m_NumElements;
  26. int m_NumElementsUsed;
  27. };
  28. template <class T>
  29. CSmartBuffer<T>::CSmartBuffer(int NumElements)
  30. :m_NumElements(NumElements), m_NumElementsUsed(0)
  31. {
  32. if (NumElements > 0)
  33. m_Array = new T[NumElements];
  34. else
  35. m_Array = 0;
  36. }
  37. template <class T>
  38. CSmartBuffer<T>::~CSmartBuffer()
  39. {
  40. delete[] m_Array;
  41. }
  42. template <class T>
  43. T* CSmartBuffer<T>::Allocate(int SizeNeeded)
  44. {
  45. if (SizeNeeded > m_NumElements)
  46. {
  47. m_NumElements = SizeNeeded;
  48. delete[] m_Array;
  49. m_Array = new T[m_NumElements];
  50. }
  51. return m_Array;
  52. }
  53. template <class T>
  54. void CSmartBuffer<T>::AddElement(T elem)
  55. {
  56. m_NumElementsUsed++;
  57. if (m_NumElementsUsed > m_NumElements)
  58. {
  59. T* temp = new T[m_NumElements*2];
  60. for (int i=0; i<m_NumElements; i++)
  61. {
  62. temp[i] = m_Array[i];
  63. }
  64. delete[] m_Array;
  65. m_Array = temp;
  66. m_NumElements = m_NumElements*2;
  67. }
  68. m_Array[m_NumElementsUsed-1] = elem;
  69. }
  70. template <class T>
  71. bool CSmartBuffer<T>::FindElement(T elem)
  72. {
  73. for (int i=0; i < m_NumElementsUsed; i++)
  74. {
  75. if (m_Array[i] == elem)
  76. return true;
  77. }
  78. return false;
  79. }
  80. #endif // !defined(AFX_SMARTBUFFER_H__47C64F5D_8388_452B_864B_1F55D6AE6F17__INCLUDED_)