RingBuffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file RingBuffer.h
  13. @brief The RingBuffer class stores a fast raw byte buffer queue storage. */
  14. #include <vector>
  15. namespace kNet
  16. {
  17. /// Implements a byte-based ring buffer (of sorts) of raw bytes.
  18. class RingBuffer
  19. {
  20. public:
  21. explicit RingBuffer(int capacity)
  22. {
  23. data.resize(capacity);
  24. start = 0;
  25. end = 0;
  26. }
  27. /// Returns the total number of bytes that this RingBuffer can contain.
  28. int Capacity() const { return (int)data.size(); }
  29. /// Returns the number of bytes filled in the ring buffer.
  30. int Size() const { return end - start; }
  31. /// Compacts the ring buffer, i.e. moves all bytes to the beginning of the array.
  32. void Compact()
  33. {
  34. // If already compacted, do nothing.
  35. if (start == 0)
  36. return;
  37. const int numBytes = Size();
  38. for(int i = 0; i < numBytes; ++i)
  39. data[i] = data[start+i];
  40. start = 0;
  41. end = numBytes;
  42. }
  43. /// Enlarges the RingBuffer capacity so that it can fit at least the given number of bytes total.
  44. /// If the capacity of the RingBuffer was greater than this, does nothing.
  45. void Resize(int newSize)
  46. {
  47. assert(newSize > 0);
  48. if ((size_t)newSize <= data.size())
  49. return; // No need to resize.
  50. Compact();
  51. data.resize(newSize);
  52. }
  53. void Clear()
  54. {
  55. start = end = 0;
  56. }
  57. /// Returns a pointer to the first byte of actual data.
  58. char *Begin() { return &data[start]; }
  59. /// Returns a pointer to one past the last byte of actual data.
  60. char *End() { return &data[end]; }
  61. int StartIndex() const { return start; }
  62. /// Call after having inserted the given amount of bytes to the buffer.
  63. void Inserted(int numBytes)
  64. {
  65. end += numBytes;
  66. assert(end <= (int)data.size());
  67. }
  68. /// Call after having processed the given number of bytes from the buffer.
  69. void Consumed(int numBytes)
  70. {
  71. start += numBytes;
  72. assert(start <= end);
  73. if (start == end) // Free compact?
  74. start = end = 0;
  75. }
  76. /// Returns the total number of bytes that can be filled in this structure after compacting.
  77. int TotalFreeBytesLeft() const { return (int)data.size() - Size(); }
  78. /// Returns the number of bytes that can be added to this structure contiguously, without having to compact.
  79. int ContiguousFreeBytesLeft() const { return (int)data.size() - end; }
  80. private:
  81. std::vector<char> data;
  82. int start; ///< Points to the first used byte.
  83. int end; ///< Points to the first unused byte.
  84. };
  85. } // ~kNet