iovec.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <util/stream/output.h>
  3. #include <util/system/types.h>
  4. #include <util/system/yassert.h>
  5. class TContIOVector {
  6. using TPart = IOutputStream::TPart;
  7. public:
  8. inline TContIOVector(TPart* parts, size_t count)
  9. : Parts_(parts)
  10. , Count_(count)
  11. {
  12. }
  13. inline void Proceed(size_t len) noexcept {
  14. while (Count_) {
  15. if (len < Parts_->len) {
  16. Parts_->len -= len;
  17. Parts_->buf = (const char*)Parts_->buf + len;
  18. return;
  19. } else {
  20. len -= Parts_->len;
  21. --Count_;
  22. ++Parts_;
  23. }
  24. }
  25. if (len) {
  26. Y_ASSERT(0 && "non zero length left");
  27. }
  28. }
  29. inline const TPart* Parts() const noexcept {
  30. return Parts_;
  31. }
  32. inline size_t Count() const noexcept {
  33. return Count_;
  34. }
  35. static inline size_t Bytes(const TPart* parts, size_t count) noexcept {
  36. size_t ret = 0;
  37. for (size_t i = 0; i < count; ++i) {
  38. ret += parts[i].len;
  39. }
  40. return ret;
  41. }
  42. inline size_t Bytes() const noexcept {
  43. return Bytes(Parts_, Count_);
  44. }
  45. inline bool Complete() const noexcept {
  46. return !Count();
  47. }
  48. private:
  49. TPart* Parts_;
  50. size_t Count_;
  51. };