Team Fortress 2 Source Code as on 22/4/2020
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.

98 lines
2.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Defines an interface for circular buffers. Data can be written to
  4. // and read from these buffers as though from a file. When it is
  5. // write-overflowed (you write more data in than the buffer can hold),
  6. // the read pointer is advanced to allow the new data to be written.
  7. // This means old data will be discarded if you write too much data
  8. // into the buffer.
  9. //
  10. // MikeD: Moved all the functions into a class.
  11. // Changed it so when the buffer overflows, the old data
  12. // is discarded rather than the new.
  13. //
  14. //=====================================================================================//
  15. #ifndef CIRCULARBUFFER_H
  16. #define CIRCULARBUFFER_H
  17. #pragma once
  18. class CCircularBuffer
  19. {
  20. public:
  21. CCircularBuffer();
  22. CCircularBuffer( int size );
  23. void SetSize( int nSize );
  24. protected:
  25. inline void AssertValid()
  26. {
  27. #ifdef _DEBUG
  28. Assert( this );
  29. Assert( m_nSize > 0 );
  30. Assert( m_nCount >= 0 );
  31. Assert( m_nCount <= m_nSize );
  32. Assert( m_nWrite < m_nSize );
  33. // Verify that m_nCount is correct.
  34. if( m_nRead == m_nWrite )
  35. {
  36. Assert( m_nCount == 0 || m_nCount == m_nSize );
  37. }
  38. else
  39. {
  40. int testCount=0;
  41. if ( m_nRead < m_nWrite )
  42. testCount = m_nWrite - m_nRead;
  43. else
  44. testCount = (m_nSize - m_nRead) + m_nWrite;
  45. Assert( testCount == m_nCount );
  46. }
  47. #endif
  48. }
  49. public:
  50. void Flush();
  51. int GetSize(); // Get the size of the buffer (how much can you write without reading
  52. // before losing data.
  53. int GetWriteAvailable(); // Get the amount available to write without overflowing.
  54. // Note: you can write however much you want, but it may overflow,
  55. // in which case the newest data is kept and the oldest is discarded.
  56. int GetReadAvailable(); // Get the amount available to read.
  57. int GetMaxUsed();
  58. int Peek(char *pchDest, int nCount);
  59. int Advance(int nCount);
  60. int Read(void *pchDest, int nCount);
  61. int Write(void *pchData, int nCount);
  62. public:
  63. int m_nCount; // Space between the read and write pointers (how much data we can read).
  64. int m_nRead; // Read index into circular buffer
  65. int m_nWrite; // Write index into circular buffer
  66. int m_nSize; // Size of circular buffer in bytes (how much data it can hold).
  67. char m_chData[1]; // Circular buffer holding data
  68. };
  69. // Use this to instantiate a CircularBuffer.
  70. template< int size >
  71. class CSizedCircularBuffer : public CCircularBuffer
  72. {
  73. public:
  74. CSizedCircularBuffer() : CCircularBuffer(size) {}
  75. private:
  76. char myData[size-1];
  77. };
  78. CCircularBuffer *AllocateCircularBuffer( int nSize );
  79. void FreeCircularBuffer( CCircularBuffer *pCircularBuffer );
  80. #endif // CIRCULARBUFFER_H