left_right_buffer.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <util/generic/buffer.h>
  3. #include <util/generic/noncopyable.h>
  4. #include <util/system/yassert.h>
  5. namespace NBus {
  6. namespace NPrivate {
  7. class TLeftRightBuffer : TNonCopyable {
  8. private:
  9. TBuffer Buffer;
  10. size_t Left;
  11. void CheckInvariant() {
  12. Y_ASSERT(Left <= Buffer.Size());
  13. }
  14. public:
  15. TLeftRightBuffer()
  16. : Left(0)
  17. {
  18. }
  19. TBuffer& GetBuffer() {
  20. return Buffer;
  21. }
  22. size_t Capacity() {
  23. return Buffer.Capacity();
  24. }
  25. void Clear() {
  26. Buffer.Clear();
  27. Left = 0;
  28. }
  29. void Reset() {
  30. Buffer.Reset();
  31. Left = 0;
  32. }
  33. void Compact() {
  34. Buffer.ChopHead(Left);
  35. Left = 0;
  36. }
  37. char* LeftPos() {
  38. return Buffer.Data() + Left;
  39. }
  40. size_t LeftSize() {
  41. return Left;
  42. }
  43. void LeftProceed(size_t count) {
  44. Y_ASSERT(count <= Size());
  45. Left += count;
  46. }
  47. size_t Size() {
  48. return Buffer.Size() - Left;
  49. }
  50. bool Empty() {
  51. return Size() == 0;
  52. }
  53. char* RightPos() {
  54. return Buffer.Data() + Buffer.Size();
  55. }
  56. size_t Avail() {
  57. return Buffer.Avail();
  58. }
  59. };
  60. }
  61. }