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.

113 lines
3.2 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: SimpRing.h
  4. //
  5. // Module: CMMON32.EXE
  6. //
  7. // Synopsis: Define a template for a round ring class
  8. //
  9. // Copyright (c) 1998-1999 Microsoft Corporation
  10. //
  11. // Author: ????? Created 10/27/97
  12. //
  13. //+----------------------------------------------------------------------------
  14. #ifndef SMPLRING_H
  15. #define SMPLRING_H
  16. //+---------------------------------------------------------------------------
  17. //
  18. // class : CSimpleRing template
  19. //
  20. // Synopsis: A simple round ring class. It does not check the full
  21. // or empty situation. That is the new item will overwrite the
  22. // old one, when the ring is full
  23. //
  24. // Template Arguments: class TYPE, the type of element in the ring
  25. // DWORD dwSize, the size of the ring
  26. //
  27. // History: fengsun created 10/97
  28. //
  29. //----------------------------------------------------------------------------
  30. template <class TYTE, DWORD dwSize> class CSimpleRing
  31. {
  32. public:
  33. CSimpleRing() {m_dwIndex = 0;}
  34. void Reset(); // Reset all data to 0, will not call destructor
  35. void Add(const TYTE& dwElement); // Add one elements, it will over write the last one in the ring
  36. const TYTE& GetLatest() const; // Get the one just put in
  37. const TYTE& GetOldest() const; // Get the last element in the ring,
  38. protected:
  39. DWORD m_dwIndex; // The index of next item to be added
  40. TYTE m_Elements[dwSize]; // The elements array
  41. public:
  42. #ifdef DEBUG
  43. void AssertValid() const;
  44. #endif
  45. };
  46. template <class TYPE, DWORD dwSize>
  47. inline void CSimpleRing<TYPE, dwSize>::Reset()
  48. {
  49. ZeroMemory(m_Elements, sizeof(m_Elements));
  50. m_dwIndex = 0;
  51. }
  52. template <class TYPE, DWORD dwSize>
  53. inline void CSimpleRing<TYPE, dwSize>::Add(const TYPE& dwElement)
  54. {
  55. m_Elements[m_dwIndex] = dwElement;
  56. m_dwIndex = (m_dwIndex + 1) % dwSize;
  57. }
  58. template <class TYPE, DWORD dwSize>
  59. inline const TYPE& CSimpleRing<TYPE, dwSize>::GetLatest() const
  60. {
  61. return m_Elements[(m_dwIndex-1)%dwSize];
  62. }
  63. //+----------------------------------------------------------------------------
  64. //
  65. // Function: DWORD CIdleStatistics::CDwordRing<dwSize>::GetOldest
  66. //
  67. // Synopsis: Get the oldest element in the ring,
  68. //
  69. // Arguments: None
  70. //
  71. // Returns: DWORD
  72. //
  73. // History: Created Header 10/15/97
  74. //
  75. //+----------------------------------------------------------------------------
  76. template <class TYPE, DWORD dwSize>
  77. inline const TYPE& CSimpleRing<TYPE, dwSize>::GetOldest() const
  78. {
  79. return m_Elements[m_dwIndex];
  80. }
  81. #ifdef DEBUG
  82. //+----------------------------------------------------------------------------
  83. //
  84. // Function: CSimpleRing::AssertValid
  85. //
  86. // Synopsis: For debug purpose only, assert the object is valid
  87. //
  88. // Arguments: None
  89. //
  90. // Returns: Nothing
  91. //
  92. // History: Created Header 2/12/98
  93. //
  94. //+----------------------------------------------------------------------------
  95. template <class TYPE, DWORD dwSize>
  96. inline void CSimpleRing<TYPE, dwSize>::AssertValid() const
  97. {
  98. MYDBGASSERT(m_dwIndex < dwSize);
  99. }
  100. #endif
  101. #endif